]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - tests/stdatomic_test.cpp
am 6cdab387: Merge "Restore <nsswitch.h> which is BSD API, not private."
[android-sdk/platform-bionic.git] / tests / stdatomic_test.cpp
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 #include <gtest/gtest.h>
19 #if !defined(__GLIBC__) /* TODO: fix our prebuilt toolchains! */
21 #include <stdatomic.h>
23 TEST(stdatomic, LOCK_FREE) {
24   ASSERT_TRUE(ATOMIC_BOOL_LOCK_FREE);
25   ASSERT_TRUE(ATOMIC_CHAR16_T_LOCK_FREE);
26   ASSERT_TRUE(ATOMIC_CHAR32_T_LOCK_FREE);
27   ASSERT_TRUE(ATOMIC_CHAR_LOCK_FREE);
28   ASSERT_TRUE(ATOMIC_INT_LOCK_FREE);
29   ASSERT_TRUE(ATOMIC_LLONG_LOCK_FREE);
30   ASSERT_TRUE(ATOMIC_LONG_LOCK_FREE);
31   ASSERT_TRUE(ATOMIC_POINTER_LOCK_FREE);
32   ASSERT_TRUE(ATOMIC_SHORT_LOCK_FREE);
33   ASSERT_TRUE(ATOMIC_WCHAR_T_LOCK_FREE);
34 }
36 TEST(stdatomic, init) {
37   atomic_int v = ATOMIC_VAR_INIT(123);
38   ASSERT_EQ(123, atomic_load(&v));
40   atomic_init(&v, 456);
41   ASSERT_EQ(456, atomic_load(&v));
43   atomic_flag f = ATOMIC_FLAG_INIT;
44   ASSERT_FALSE(atomic_flag_test_and_set(&f));
45 }
47 TEST(stdatomic, atomic_thread_fence) {
48   atomic_thread_fence(memory_order_relaxed);
49   atomic_thread_fence(memory_order_consume);
50   atomic_thread_fence(memory_order_acquire);
51   atomic_thread_fence(memory_order_release);
52   atomic_thread_fence(memory_order_acq_rel);
53   atomic_thread_fence(memory_order_seq_cst);
54 }
56 TEST(stdatomic, atomic_signal_fence) {
57   atomic_signal_fence(memory_order_relaxed);
58   atomic_signal_fence(memory_order_consume);
59   atomic_signal_fence(memory_order_acquire);
60   atomic_signal_fence(memory_order_release);
61   atomic_signal_fence(memory_order_acq_rel);
62   atomic_signal_fence(memory_order_seq_cst);
63 }
65 TEST(stdatomic, atomic_is_lock_free) {
66   atomic_char small;
67   atomic_intmax_t big;
68   ASSERT_TRUE(atomic_is_lock_free(&small));
69   ASSERT_TRUE(atomic_is_lock_free(&big));
70 }
72 TEST(stdatomic, atomic_flag) {
73   atomic_flag f = ATOMIC_FLAG_INIT;
74   ASSERT_FALSE(atomic_flag_test_and_set(&f));
75   ASSERT_TRUE(atomic_flag_test_and_set(&f));
77   atomic_flag_clear(&f);
79   ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
80   ASSERT_TRUE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
82   atomic_flag_clear_explicit(&f, memory_order_relaxed);
83   ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
84 }
86 TEST(stdatomic, atomic_store) {
87   atomic_int i;
88   atomic_store(&i, 123);
89   ASSERT_EQ(123, atomic_load(&i));
90   atomic_store_explicit(&i, 123, memory_order_relaxed);
91   ASSERT_EQ(123, atomic_load_explicit(&i, memory_order_relaxed));
92 }
94 TEST(stdatomic, atomic_exchange) {
95   atomic_int i;
96   atomic_store(&i, 123);
97   ASSERT_EQ(123, atomic_exchange(&i, 456));
98   ASSERT_EQ(456, atomic_exchange_explicit(&i, 123, memory_order_relaxed));
99 }
101 TEST(stdatomic, atomic_compare_exchange) {
102   atomic_int i;
103   int expected;
105   atomic_store(&i, 123);
106   expected = 123;
107   ASSERT_TRUE(atomic_compare_exchange_strong(&i, &expected, 456));
108   ASSERT_FALSE(atomic_compare_exchange_strong(&i, &expected, 456));
109   ASSERT_EQ(456, expected);
111   atomic_store(&i, 123);
112   expected = 123;
113   ASSERT_TRUE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed, memory_order_relaxed));
114   ASSERT_FALSE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed, memory_order_relaxed));
115   ASSERT_EQ(456, expected);
117   atomic_store(&i, 123);
118   expected = 123;
119   ASSERT_TRUE(atomic_compare_exchange_weak(&i, &expected, 456));
120   ASSERT_FALSE(atomic_compare_exchange_weak(&i, &expected, 456));
121   ASSERT_EQ(456, expected);
123   atomic_store(&i, 123);
124   expected = 123;
125   ASSERT_TRUE(atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed, memory_order_relaxed));
126   ASSERT_FALSE(atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed, memory_order_relaxed));
127   ASSERT_EQ(456, expected);
130 TEST(stdatomic, atomic_fetch_add) {
131   atomic_int i = ATOMIC_VAR_INIT(123);
132   ASSERT_EQ(123, atomic_fetch_add(&i, 1));
133   ASSERT_EQ(124, atomic_fetch_add_explicit(&i, 1, memory_order_relaxed));
134   ASSERT_EQ(125, atomic_load(&i));
137 TEST(stdatomic, atomic_fetch_sub) {
138   atomic_int i = ATOMIC_VAR_INIT(123);
139   ASSERT_EQ(123, atomic_fetch_sub(&i, 1));
140   ASSERT_EQ(122, atomic_fetch_sub_explicit(&i, 1, memory_order_relaxed));
141   ASSERT_EQ(121, atomic_load(&i));
144 TEST(stdatomic, atomic_fetch_or) {
145   atomic_int i = ATOMIC_VAR_INIT(0x100);
146   ASSERT_EQ(0x100, atomic_fetch_or(&i, 0x020));
147   ASSERT_EQ(0x120, atomic_fetch_or_explicit(&i, 0x003, memory_order_relaxed));
148   ASSERT_EQ(0x123, atomic_load(&i));
151 TEST(stdatomic, atomic_fetch_xor) {
152   atomic_int i = ATOMIC_VAR_INIT(0x100);
153   ASSERT_EQ(0x100, atomic_fetch_xor(&i, 0x120));
154   ASSERT_EQ(0x020, atomic_fetch_xor_explicit(&i, 0x103, memory_order_relaxed));
155   ASSERT_EQ(0x123, atomic_load(&i));
158 TEST(stdatomic, atomic_fetch_and) {
159   atomic_int i = ATOMIC_VAR_INIT(0x123);
160   ASSERT_EQ(0x123, atomic_fetch_and(&i, 0x00f));
161   ASSERT_EQ(0x003, atomic_fetch_and_explicit(&i, 0x2, memory_order_relaxed));
162   ASSERT_EQ(0x002, atomic_load(&i));
165 #endif