.
[glibc/history.git] / csu / tst-atomic.c
blob943c05073511e14cb3066910e2625fa0f31d19a9
1 /* Tests for atomic.h macros.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <stdio.h>
22 #include <atomic.h>
24 #ifndef atomic_t
25 # define atomic_t int
26 #endif
28 /* Test various atomic.h macros. */
29 static int
30 do_test (void)
32 atomic_t mem;
33 int ret = 0;
35 #ifdef atomic_compare_and_exchange_val_acq
36 mem = 24;
37 if (atomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
38 || mem != 35)
40 puts ("atomic_compare_and_exchange_val_acq test 1 failed");
41 ret = 1;
44 mem = 12;
45 if (atomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
46 || mem != 12)
48 puts ("atomic_compare_and_exchange_val_acq test 2 failed");
49 ret = 1;
52 mem = -15;
53 if (atomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
54 || mem != -56)
56 puts ("atomic_compare_and_exchange_val_acq test 3 failed");
57 ret = 1;
60 mem = -1;
61 if (atomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
62 || mem != -1)
64 puts ("atomic_compare_and_exchange_val_acq test 4 failed");
65 ret = 1;
67 #endif
69 mem = 24;
70 if (atomic_compare_and_exchange_bool_acq (&mem, 35, 24)
71 || mem != 35)
73 puts ("atomic_compare_and_exchange_bool_acq test 1 failed");
74 ret = 1;
77 mem = 12;
78 if (! atomic_compare_and_exchange_bool_acq (&mem, 10, 15)
79 || mem != 12)
81 puts ("atomic_compare_and_exchange_bool_acq test 2 failed");
82 ret = 1;
85 mem = -15;
86 if (atomic_compare_and_exchange_bool_acq (&mem, -56, -15)
87 || mem != -56)
89 puts ("atomic_compare_and_exchange_bool_acq test 3 failed");
90 ret = 1;
93 mem = -1;
94 if (! atomic_compare_and_exchange_bool_acq (&mem, 17, 0)
95 || mem != -1)
97 puts ("atomic_compare_and_exchange_bool_acq test 4 failed");
98 ret = 1;
101 mem = 64;
102 if (atomic_exchange (&mem, 31) != 64
103 || mem != 31)
105 puts ("atomic_exchange test failed");
106 ret = 1;
109 mem = 2;
110 if (atomic_exchange_and_add (&mem, 11) != 2
111 || mem != 13)
113 puts ("atomic_exchange_and_add test failed");
114 ret = 1;
117 mem = -21;
118 atomic_add (&mem, 22);
119 if (mem != 1)
121 puts ("atomic_add test failed");
122 ret = 1;
125 mem = -1;
126 atomic_increment (&mem);
127 if (mem != 0)
129 puts ("atomic_increment test failed");
130 ret = 1;
133 mem = 0;
134 if (atomic_increment_and_test (&mem)
135 || mem != 1)
137 puts ("atomic_increment_and_test test 1 failed");
138 ret = 1;
141 mem = 35;
142 if (atomic_increment_and_test (&mem)
143 || mem != 36)
145 puts ("atomic_increment_and_test test 2 failed");
146 ret = 1;
149 mem = -1;
150 if (! atomic_increment_and_test (&mem)
151 || mem != 0)
153 puts ("atomic_increment_and_test test 3 failed");
154 ret = 1;
157 mem = 17;
158 atomic_decrement (&mem);
159 if (mem != 16)
161 puts ("atomic_decrement test failed");
162 ret = 1;
165 mem = 0;
166 if (atomic_decrement_and_test (&mem)
167 || mem != -1)
169 puts ("atomic_decrement_and_test test 1 failed");
170 ret = 1;
173 mem = 15;
174 if (atomic_decrement_and_test (&mem)
175 || mem != 14)
177 puts ("atomic_decrement_and_test test 2 failed");
178 ret = 1;
181 mem = 1;
182 if (! atomic_decrement_and_test (&mem)
183 || mem != 0)
185 puts ("atomic_decrement_and_test test 3 failed");
186 ret = 1;
189 mem = 1;
190 if (atomic_decrement_if_positive (&mem) != 1
191 || mem != 0)
193 puts ("atomic_decrement_if_positive test 1 failed");
194 ret = 1;
197 mem = 0;
198 if (atomic_decrement_if_positive (&mem) != 0
199 || mem != 0)
201 puts ("atomic_decrement_if_positive test 2 failed");
202 ret = 1;
205 mem = -1;
206 if (atomic_decrement_if_positive (&mem) != -1
207 || mem != -1)
209 puts ("atomic_decrement_if_positive test 3 failed");
210 ret = 1;
213 mem = -12;
214 if (! atomic_add_negative (&mem, 10)
215 || mem != -2)
217 puts ("atomic_add_negative test 1 failed");
218 ret = 1;
221 mem = 0;
222 if (atomic_add_negative (&mem, 100)
223 || mem != 100)
225 puts ("atomic_add_negative test 2 failed");
226 ret = 1;
229 mem = 15;
230 if (atomic_add_negative (&mem, -10)
231 || mem != 5)
233 puts ("atomic_add_negative test 3 failed");
234 ret = 1;
237 mem = -12;
238 if (atomic_add_negative (&mem, 14)
239 || mem != 2)
241 puts ("atomic_add_negative test 4 failed");
242 ret = 1;
245 mem = 0;
246 if (! atomic_add_negative (&mem, -1)
247 || mem != -1)
249 puts ("atomic_add_negative test 5 failed");
250 ret = 1;
253 mem = -31;
254 if (atomic_add_negative (&mem, 31)
255 || mem != 0)
257 puts ("atomic_add_negative test 6 failed");
258 ret = 1;
261 mem = -34;
262 if (atomic_add_zero (&mem, 31)
263 || mem != -3)
265 puts ("atomic_add_zero test 1 failed");
266 ret = 1;
269 mem = -36;
270 if (! atomic_add_zero (&mem, 36)
271 || mem != 0)
273 puts ("atomic_add_zero test 2 failed");
274 ret = 1;
277 mem = 113;
278 if (atomic_add_zero (&mem, -13)
279 || mem != 100)
281 puts ("atomic_add_zero test 3 failed");
282 ret = 1;
285 mem = -18;
286 if (atomic_add_zero (&mem, 20)
287 || mem != 2)
289 puts ("atomic_add_zero test 4 failed");
290 ret = 1;
293 mem = 10;
294 if (atomic_add_zero (&mem, -20)
295 || mem != -10)
297 puts ("atomic_add_zero test 5 failed");
298 ret = 1;
301 mem = 10;
302 if (! atomic_add_zero (&mem, -10)
303 || mem != 0)
305 puts ("atomic_add_zero test 6 failed");
306 ret = 1;
309 mem = 0;
310 atomic_bit_set (&mem, 1);
311 if (mem != 2)
313 puts ("atomic_bit_set test 1 failed");
314 ret = 1;
317 mem = 8;
318 atomic_bit_set (&mem, 3);
319 if (mem != 8)
321 puts ("atomic_bit_set test 2 failed");
322 ret = 1;
325 #ifdef TEST_ATOMIC64
326 mem = 16;
327 atomic_bit_set (&mem, 35);
328 if (mem != 0x800000010LL)
330 puts ("atomic_bit_set test 3 failed");
331 ret = 1;
333 #endif
335 mem = 0;
336 if (atomic_bit_test_set (&mem, 1)
337 || mem != 2)
339 puts ("atomic_bit_test_set test 1 failed");
340 ret = 1;
343 mem = 8;
344 if (! atomic_bit_test_set (&mem, 3)
345 || mem != 8)
347 puts ("atomic_bit_test_set test 2 failed");
348 ret = 1;
351 #ifdef TEST_ATOMIC64
352 mem = 16;
353 if (atomic_bit_test_set (&mem, 35)
354 || mem != 0x800000010LL)
356 puts ("atomic_bit_test_set test 3 failed");
357 ret = 1;
360 mem = 0x100000000LL;
361 if (! atomic_bit_test_set (&mem, 32)
362 || mem != 0x100000000LL)
364 puts ("atomic_bit_test_set test 4 failed");
365 ret = 1;
367 #endif
369 return ret;
372 #define TEST_FUNCTION do_test ()
373 #include "../test-skeleton.c"