Skip to content

Commit 645006e

Browse files
committed
[utest][cpp]add cpp-atomic testcase.
1 parent 34b98d9 commit 645006e

File tree

1 file changed

+46
-179
lines changed

1 file changed

+46
-179
lines changed

examples/utest/testcases/cpp11/tc_atomic.cpp

Lines changed: 46 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ static void test_atomic_load_store_int32(void)
2323
constexpr int kRound = 10000000;
2424
std::atomic<int32_t> thread_count(0);
2525
std::atomic<int32_t> count(100);
26-
if (count != 100)
27-
{
28-
uassert_false(true);
29-
}
26+
uassert_int_equal(count.load(), 100);
3027

3128
auto func1 = [&]() mutable {
3229
for (int i = 0; i < kRound; ++i)
@@ -49,17 +46,8 @@ static void test_atomic_load_store_int32(void)
4946
t1.join();
5047
t2.join();
5148

52-
if (count != 100)
53-
{
54-
uassert_false(true);
55-
}
56-
57-
if (thread_count != 2)
58-
{
59-
uassert_false(true);
60-
}
61-
62-
uassert_true(true);
49+
uassert_int_equal(count.load(), 100);
50+
uassert_int_equal(thread_count.load(), 2);
6351
}
6452

6553
/**
@@ -71,10 +59,7 @@ static void test_atomic_load_store_int64(void)
7159
constexpr int kRound = 10000000;
7260
std::atomic<int64_t> thread_count(0);
7361
std::atomic<int64_t> count(100);
74-
if (count != 100)
75-
{
76-
uassert_false(true);
77-
}
62+
uassert_int_equal(count.load(), 100);
7863

7964
auto func1 = [&]() mutable {
8065
for (int i = 0; i < kRound; ++i)
@@ -97,17 +82,8 @@ static void test_atomic_load_store_int64(void)
9782
t1.join();
9883
t2.join();
9984

100-
if (count != 100)
101-
{
102-
uassert_false(true);
103-
}
104-
105-
if (thread_count != 2)
106-
{
107-
uassert_false(true);
108-
}
109-
110-
uassert_true(true);
85+
uassert_int_equal(count.load(), 100);
86+
uassert_int_equal(thread_count.load(), 2);
11187
}
11288

11389
/**
@@ -118,83 +94,43 @@ static void test_atomic_basic_int32(void)
11894
{
11995
std::atomic<int32_t> val;
12096
val = 10;
121-
if (val.load() != 10)
122-
{
123-
uassert_false(true);
124-
}
97+
uassert_int_equal(val.load(), 10);
12598

12699
val++;
127-
128-
if (val.load() != 11)
129-
{
130-
uassert_false(true);
131-
}
100+
uassert_int_equal(val.load(), 11);
132101

133102
val--;
134-
if (val.load() != 10)
135-
{
136-
uassert_false(true);
137-
}
103+
uassert_int_equal(val.load(), 10);
138104

139105
auto a = val.load();
140106
val.store(a);
141107
val.fetch_add(1);
142-
if (val.load() != 11)
143-
{
144-
uassert_false(true);
145-
}
108+
uassert_int_equal(val.load(), 11);
146109
val.fetch_sub(1);
147-
if (val.load() != 10)
148-
{
149-
uassert_false(true);
150-
}
110+
uassert_int_equal(val.load(), 10);
151111

152112
val.fetch_and(14);
153-
if (val.load() != 10)
154-
{
155-
uassert_false(true);
156-
}
113+
uassert_int_equal(val.load(), 10);
157114

158115
val.fetch_or(11); //1010
159-
if (val.load() != 11)
160-
{
161-
uassert_false(true);
162-
}
116+
uassert_int_equal(val.load(), 11);
163117

164118
val.fetch_xor(4);
165-
if (val.load() != 15)
166-
{
167-
uassert_false(true);
168-
}
119+
uassert_int_equal(val.load(), 15);
169120

170121
val.exchange(1);
171-
if (val.load() != 1)
172-
{
173-
uassert_false(true);
174-
}
122+
uassert_int_equal(val.load(), 1);
175123

176124
int32_t x = 2;
177125
int32_t y = 3;
178126
bool exchanged = val.compare_exchange_strong(x, y);
179-
if (exchanged)
180-
{
181-
uassert_false(true);
182-
}
183-
if (val.load() != 1 || x != 1)
184-
{
185-
uassert_false(true);
186-
}
127+
uassert_false(exchanged);
128+
uassert_int_equal(val.load(), 1);
129+
uassert_int_equal(x, 1);
187130
exchanged = val.compare_exchange_strong(x, y);
188-
if (!exchanged)
189-
{
190-
uassert_false(true);
191-
}
192-
if (val.load() != 3 || x != 1)
193-
{
194-
uassert_false(true);
195-
}
196-
197-
uassert_true(true);
131+
uassert_true(exchanged);
132+
uassert_int_equal(val.load(), 3);
133+
uassert_int_equal(x, 1);
198134
}
199135

200136
/**
@@ -205,82 +141,43 @@ static void test_atomic_basic_int64(void)
205141
{
206142
std::atomic<int64_t> val;
207143
val = 10;
208-
if (val.load() != 10)
209-
{
210-
uassert_false(true);
211-
}
144+
uassert_int_equal(val.load(), 10);
212145

213146
val++;
214-
if (val.load() != 11)
215-
{
216-
uassert_false(true);
217-
}
147+
uassert_int_equal(val.load(), 11);
218148

219149
val--;
220-
if (val.load() != 10)
221-
{
222-
uassert_false(true);
223-
}
150+
uassert_int_equal(val.load(), 10);
224151

225152
auto a = val.load();
226153
val.store(a);
227154
val.fetch_add(1);
228-
if (val.load() != 11)
229-
{
230-
uassert_false(true);
231-
}
155+
uassert_int_equal(val.load(), 11);
232156
val.fetch_sub(1);
233-
if (val.load() != 10)
234-
{
235-
uassert_false(true);
236-
}
157+
uassert_int_equal(val.load(), 10);
237158

238159
val.fetch_and(14);
239-
if (val.load() != 10)
240-
{
241-
uassert_false(true);
242-
}
160+
uassert_int_equal(val.load(), 10);
243161

244162
val.fetch_or(11); //1010
245-
if (val.load() != 11)
246-
{
247-
uassert_false(true);
248-
}
163+
uassert_int_equal(val.load(), 11);
249164

250165
val.fetch_xor(4);
251-
if (val.load() != 15)
252-
{
253-
uassert_false(true);
254-
}
166+
uassert_int_equal(val.load(), 15);
255167

256168
val.exchange(1);
257-
if (val.load() != 1)
258-
{
259-
uassert_false(true);
260-
}
169+
uassert_int_equal(val.load(), 1);
261170

262171
int64_t x = 2;
263172
int64_t y = 3;
264173
bool exchanged = val.compare_exchange_strong(x, y);
265-
if (exchanged)
266-
{
267-
uassert_false(true);
268-
}
269-
if (val.load() != 1 || x != 1)
270-
{
271-
uassert_false(true);
272-
}
174+
uassert_false(exchanged);
175+
uassert_int_equal(val.load(), 1);
176+
uassert_int_equal(x, 1);
273177
exchanged = val.compare_exchange_strong(x, y);
274-
if (!exchanged)
275-
{
276-
uassert_false(true);
277-
}
278-
if (val.load() != 3 || x != 1)
279-
{
280-
uassert_false(true);
281-
}
282-
283-
uassert_true(true);
178+
uassert_true(exchanged);
179+
uassert_int_equal(val.load(), 3);
180+
uassert_int_equal(x, 1);
284181
}
285182

286183
/**
@@ -290,26 +187,13 @@ static void test_atomic_bool(void)
290187
{
291188
std::atomic<bool> flag(false);
292189
flag.store(true);
293-
if (!flag.load())
294-
{
295-
uassert_false(true);
296-
}
190+
uassert_true(flag.load());
297191
flag.exchange(false);
298-
if (flag.load())
299-
{
300-
uassert_false(true);
301-
}
192+
uassert_false(flag.load());
302193
bool expected = false;
303194
bool desired = true;
304-
if (!flag.compare_exchange_strong(expected, desired))
305-
{
306-
uassert_false(true);
307-
}
308-
if (!flag.load())
309-
{
310-
uassert_false(true);
311-
}
312-
uassert_true(true);
195+
uassert_true(flag.compare_exchange_strong(expected, desired));
196+
uassert_true(flag.load());
313197
}
314198

315199
/**
@@ -320,16 +204,10 @@ static void test_atomic_pointer(void)
320204
int a = 1, b = 2;
321205
std::atomic<int *> ptr(&a);
322206
ptr.store(&b);
323-
if (*ptr.load() != 2)
324-
{
325-
uassert_false(true);
326-
}
207+
uassert_int_equal(*ptr.load(), 2);
327208
int *old = ptr.exchange(&a);
328-
if (old != &b || *ptr.load() != 1)
329-
{
330-
uassert_false(true);
331-
}
332-
uassert_true(true);
209+
uassert_ptr_equal(old, &b);
210+
uassert_int_equal(*ptr.load(), 1);
333211
}
334212

335213
/**
@@ -342,15 +220,8 @@ static void test_memory_order(void)
342220
// Simple test for memory order
343221
x.store(1, std::memory_order_release);
344222
y.store(2, std::memory_order_release);
345-
if (x.load(std::memory_order_acquire) != 1)
346-
{
347-
uassert_false(true);
348-
}
349-
if (y.load(std::memory_order_acquire) != 2)
350-
{
351-
uassert_false(true);
352-
}
353-
uassert_true(true);
223+
uassert_int_equal(x.load(std::memory_order_acquire), 1);
224+
uassert_int_equal(y.load(std::memory_order_acquire), 2);
354225
}
355226

356227
/**
@@ -365,11 +236,7 @@ static void test_compare_exchange_weak(void)
365236
{
366237
expected = 1; // reset
367238
}
368-
if (val.load() != 2)
369-
{
370-
uassert_false(true);
371-
}
372-
uassert_true(true);
239+
uassert_int_equal(val.load(), 2);
373240
}
374241

375242
static rt_err_t utest_tc_init(void)

0 commit comments

Comments
 (0)