utest.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009 Helge Bahmann
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <boost/memory_order.hpp>
8 #include "ros/atomic.h"
9 #include <typeinfo>
10 
11 #include <gtest/gtest.h>
12 
13 using namespace ros;
14 
15 
16 template<typename T>
18 {
19  atomic<T> i(41);
20 
21  T n;
22 
23  fprintf(stderr, "Type=%s, size=%ld, atomic_size=%ld, lockfree=%d\n",
24  typeid(T).name(), (long)sizeof(n), (long)sizeof(i), i.is_lock_free());
25 
26  ASSERT_TRUE(sizeof(i)>=sizeof(n));
27 
28  bool success;
29 
30  n=i++;
31  ASSERT_TRUE(i==42);
32  ASSERT_TRUE(n==41);
33 
34  n=i--;
35  ASSERT_TRUE(n==42);
36  ASSERT_TRUE(i==41);
37 
38  n=++i;
39  ASSERT_TRUE(i==42);
40  ASSERT_TRUE(n==42);
41 
42  n=--i;
43  ASSERT_TRUE(n==41);
44  ASSERT_TRUE(i==41);
45 
46  n=i.fetch_and(15);
47  ASSERT_TRUE(n==41);
48  ASSERT_TRUE(i==9);
49 
50  n=i.fetch_or(17);
51  ASSERT_TRUE(n==9);
52  ASSERT_TRUE(i==25);
53 
54  n=i.fetch_xor(3);
55  ASSERT_TRUE(n==25);
56  ASSERT_TRUE(i==26);
57 
58  n=i.exchange(12);
59  ASSERT_TRUE(n==26);
60  ASSERT_TRUE(i==12);
61 
62  n=12;
63  success=i.compare_exchange_strong(n, 17);
64  ASSERT_TRUE(success);
65  ASSERT_TRUE(n==12);
66  ASSERT_TRUE(i==17);
67 
68  n=12;
69  success=i.compare_exchange_strong(n, 19);
70  ASSERT_TRUE(!success);
71  ASSERT_TRUE(n==17);
72  ASSERT_TRUE(i==17);
73 }
74 
75 template<typename T>
76 void test_atomic_base(void)
77 {
78  atomic<T> i;
79  T n;
80 
81  fprintf(stderr, "Type=%s, size=%ld, atomic_size=%ld, lockfree=%d\n",
82  typeid(T).name(), (long)sizeof(n), (long)sizeof(i), i.is_lock_free());
83 
84  ASSERT_TRUE(sizeof(i)>=sizeof(n));
85 
86  bool success;
87 
88  i.store((T)0);
89  n=(T)40;
90  success=i.compare_exchange_strong(n, (T)44 /*boost::memory_order_relaxed*/);
91  ASSERT_TRUE(!success);
92  ASSERT_TRUE(n==(T)0);
93  ASSERT_TRUE(i.load()==(T)0);
94 
95  n=(T)0;
96  success=i.compare_exchange_strong(n, (T)44);
97  ASSERT_TRUE(success);
98  ASSERT_TRUE(n==(T)0);
99  ASSERT_TRUE(i.load()==(T)44);
100 
101  n=i.exchange((T)20);
102  ASSERT_EQ(n, (T)44);
103  ASSERT_TRUE(i.load()==(T)20);
104 }
105 
106 template<typename T>
107 void test_atomic_ptr(void)
108 {
109  test_atomic_base<T *>();
110 
111  T array[10], *p;
112  atomic<T *> ptr;
113 
114  ptr=&array[0];
115 
116  p=ptr++;
117  ASSERT_TRUE(p==&array[0]);
118  ASSERT_TRUE(ptr==&array[1]);
119  p=++ptr;
120  ASSERT_TRUE(p==&array[2]);
121  ASSERT_TRUE(ptr==&array[2]);
122 
123  p=ptr.fetch_add(4);
124  ASSERT_TRUE(p==&array[2]);
125  ASSERT_TRUE(ptr==&array[6]);
126 
127  p=ptr.fetch_sub(4);
128  ASSERT_TRUE(p==&array[6]);
129  ASSERT_TRUE(ptr==&array[2]);
130 
131  p=ptr--;
132  ASSERT_TRUE(p==&array[2]);
133  ASSERT_TRUE(ptr==&array[1]);
134  p=--ptr;
135  ASSERT_TRUE(p==&array[0]);
136  ASSERT_TRUE(ptr==&array[0]);
137 }
138 
139 template<>
141 {
142  atomic<bool> i;
143  bool n;
144 
145  fprintf(stderr, "Type=bool, size=%ld, atomic_size=%ld, lockfree=%d\n",
146  (long)sizeof(n), (long)sizeof(i), i.is_lock_free());
147 
148  ASSERT_TRUE(sizeof(i)>=sizeof(n));
149 
150  bool success;
151  i=false;
152  n=true;
153  success=i.compare_exchange_strong(n, true);
154  ASSERT_TRUE(!success);
155  ASSERT_TRUE(n==false);
156  ASSERT_TRUE(i==false);
157 
158  n=false;
159  success=i.compare_exchange_strong(n, true);
160  ASSERT_TRUE(success);
161  ASSERT_TRUE(n==false);
162  ASSERT_TRUE(i==true);
163 
164  n=i.exchange(false);
165  ASSERT_TRUE(n==true);
166  ASSERT_TRUE(i==false);
167 }
168 
170 {
171  atomic_flag f;
172 
173  ASSERT_TRUE(!f.test_and_set());
174  ASSERT_TRUE(f.test_and_set());
175  f.clear();
176  ASSERT_TRUE(!f.test_and_set());
177 }
178 
179 struct Compound {
180  int i;
181 
182  inline bool operator==(const Compound &c) const {return i==c.i;}
183 };
184 
186 {
188  Compound n;
189 
190  Compound zero={0}, one={1}, two={2};
191 
192  ASSERT_TRUE(sizeof(i)>=sizeof(n));
193 
194  bool success;
195 
196  i.store(zero);
197  n=one;
198  success=i.compare_exchange_strong(n, two);
199  ASSERT_TRUE(!success);
200  ASSERT_TRUE(n==zero);
201  ASSERT_TRUE(i.load()==zero);
202 
203  n=zero;
204  success=i.compare_exchange_strong(n, two);
205  ASSERT_TRUE(success);
206  ASSERT_TRUE(n==zero);
207  ASSERT_TRUE(i.load()==two);
208 
209  n=i.exchange(one);
210  ASSERT_TRUE(n==two);
211  ASSERT_TRUE(i.load()==one);
212 }
213 
214 enum TestEnum {
215  Foo, Bar, Baz=1000
216 };
217 
219 {
221 }
222 
223 TEST(Atomic, all)
224 {
225  test_atomic_arithmetic<char>();
226  test_atomic_arithmetic<signed char>();
227  test_atomic_arithmetic<unsigned char>();
228  test_atomic_arithmetic<uint8_t>();
229  test_atomic_arithmetic<int8_t>();
230  test_atomic_arithmetic<short>();
231  test_atomic_arithmetic<unsigned short>();
232  test_atomic_arithmetic<uint16_t>();
233  test_atomic_arithmetic<int16_t>();
234  test_atomic_arithmetic<int>();
235  test_atomic_arithmetic<unsigned int>();
236  test_atomic_arithmetic<uint32_t>();
237  test_atomic_arithmetic<int32_t>();
238  test_atomic_arithmetic<long>();
239  test_atomic_arithmetic<unsigned long>();
240  test_atomic_arithmetic<uint64_t>();
241  test_atomic_arithmetic<int64_t>();
242  test_atomic_arithmetic<long long>();
243  test_atomic_arithmetic<unsigned long long>();
244 
246 
247  test_atomic_base<void *>();
248  test_atomic_ptr<int>();
250  test_atomic_base<TestEnum>();
251 
253 
254  test_fence();
255 
257 }
258 
259 int main(int argc, char** argv)
260 {
261  testing::InitGoogleTest(&argc, argv);
262  return RUN_ALL_TESTS();
263 }
bool compare_exchange_strong(value_type &expected, value_type desired, memory_order, memory_order) volatile
Definition: base.hpp:166
static void atomic_thread_fence(memory_order order)
Definition: gcc-armv6+.hpp:179
bool operator==(const Compound &c) const
Definition: utest.cpp:182
bool test_and_set(memory_order order=memory_order_seq_cst)
Definition: atomic.hpp:127
void test_atomic_arithmetic(void)
Definition: utest.cpp:17
void test_atomic_base(void)
Definition: utest.cpp:76
Definition: utest.cpp:215
TEST(Atomic, all)
Definition: utest.cpp:223
int main(int argc, char **argv)
Definition: utest.cpp:259
void test_atomic_ptr(void)
Definition: utest.cpp:107
Definition: atomic.h:40
int i
Definition: utest.cpp:180
Definition: utest.cpp:215
void test_atomic_struct(void)
Definition: utest.cpp:185
void test_fence()
Definition: utest.cpp:218
void test_atomic_base< bool >(void)
Definition: utest.cpp:140
void clear(memory_order order=memory_order_seq_cst) volatile
Definition: atomic.hpp:133
void test_atomic_flag()
Definition: utest.cpp:169
Definition: utest.cpp:215
TestEnum
Definition: utest.cpp:214


rosatomic
Author(s): Josh Faust
autogenerated on Fri Apr 5 2019 02:16:35