2005-01-28 Martin Schwidefsky <schwidefsky@de.ibm.com>
[glibc.git] / csu / tst-atomic.c
blob7a2e3d08655987b00013a83b166acd579c32e934
1 /* Tests for atomic.h macros.
2 Copyright (C) 2003, 2004 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_acq (&mem, 31) != 64
103 || mem != 31)
105 puts ("atomic_exchange_acq 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 = 2;
134 if (atomic_increment_val (&mem) != 3)
136 puts ("atomic_increment_val test failed");
137 ret = 1;
140 mem = 0;
141 if (atomic_increment_and_test (&mem)
142 || mem != 1)
144 puts ("atomic_increment_and_test test 1 failed");
145 ret = 1;
148 mem = 35;
149 if (atomic_increment_and_test (&mem)
150 || mem != 36)
152 puts ("atomic_increment_and_test test 2 failed");
153 ret = 1;
156 mem = -1;
157 if (! atomic_increment_and_test (&mem)
158 || mem != 0)
160 puts ("atomic_increment_and_test test 3 failed");
161 ret = 1;
164 mem = 17;
165 atomic_decrement (&mem);
166 if (mem != 16)
168 puts ("atomic_decrement test failed");
169 ret = 1;
172 if (atomic_decrement_val (&mem) != 15)
174 puts ("atomic_decrement_val test failed");
175 ret = 1;
178 mem = 0;
179 if (atomic_decrement_and_test (&mem)
180 || mem != -1)
182 puts ("atomic_decrement_and_test test 1 failed");
183 ret = 1;
186 mem = 15;
187 if (atomic_decrement_and_test (&mem)
188 || mem != 14)
190 puts ("atomic_decrement_and_test test 2 failed");
191 ret = 1;
194 mem = 1;
195 if (! atomic_decrement_and_test (&mem)
196 || mem != 0)
198 puts ("atomic_decrement_and_test test 3 failed");
199 ret = 1;
202 mem = 1;
203 if (atomic_decrement_if_positive (&mem) != 1
204 || mem != 0)
206 puts ("atomic_decrement_if_positive test 1 failed");
207 ret = 1;
210 mem = 0;
211 if (atomic_decrement_if_positive (&mem) != 0
212 || mem != 0)
214 puts ("atomic_decrement_if_positive test 2 failed");
215 ret = 1;
218 mem = -1;
219 if (atomic_decrement_if_positive (&mem) != -1
220 || mem != -1)
222 puts ("atomic_decrement_if_positive test 3 failed");
223 ret = 1;
226 mem = -12;
227 if (! atomic_add_negative (&mem, 10)
228 || mem != -2)
230 puts ("atomic_add_negative test 1 failed");
231 ret = 1;
234 mem = 0;
235 if (atomic_add_negative (&mem, 100)
236 || mem != 100)
238 puts ("atomic_add_negative test 2 failed");
239 ret = 1;
242 mem = 15;
243 if (atomic_add_negative (&mem, -10)
244 || mem != 5)
246 puts ("atomic_add_negative test 3 failed");
247 ret = 1;
250 mem = -12;
251 if (atomic_add_negative (&mem, 14)
252 || mem != 2)
254 puts ("atomic_add_negative test 4 failed");
255 ret = 1;
258 mem = 0;
259 if (! atomic_add_negative (&mem, -1)
260 || mem != -1)
262 puts ("atomic_add_negative test 5 failed");
263 ret = 1;
266 mem = -31;
267 if (atomic_add_negative (&mem, 31)
268 || mem != 0)
270 puts ("atomic_add_negative test 6 failed");
271 ret = 1;
274 mem = -34;
275 if (atomic_add_zero (&mem, 31)
276 || mem != -3)
278 puts ("atomic_add_zero test 1 failed");
279 ret = 1;
282 mem = -36;
283 if (! atomic_add_zero (&mem, 36)
284 || mem != 0)
286 puts ("atomic_add_zero test 2 failed");
287 ret = 1;
290 mem = 113;
291 if (atomic_add_zero (&mem, -13)
292 || mem != 100)
294 puts ("atomic_add_zero test 3 failed");
295 ret = 1;
298 mem = -18;
299 if (atomic_add_zero (&mem, 20)
300 || mem != 2)
302 puts ("atomic_add_zero test 4 failed");
303 ret = 1;
306 mem = 10;
307 if (atomic_add_zero (&mem, -20)
308 || mem != -10)
310 puts ("atomic_add_zero test 5 failed");
311 ret = 1;
314 mem = 10;
315 if (! atomic_add_zero (&mem, -10)
316 || mem != 0)
318 puts ("atomic_add_zero test 6 failed");
319 ret = 1;
322 mem = 0;
323 atomic_bit_set (&mem, 1);
324 if (mem != 2)
326 puts ("atomic_bit_set test 1 failed");
327 ret = 1;
330 mem = 8;
331 atomic_bit_set (&mem, 3);
332 if (mem != 8)
334 puts ("atomic_bit_set test 2 failed");
335 ret = 1;
338 #ifdef TEST_ATOMIC64
339 mem = 16;
340 atomic_bit_set (&mem, 35);
341 if (mem != 0x800000010LL)
343 puts ("atomic_bit_set test 3 failed");
344 ret = 1;
346 #endif
348 mem = 0;
349 if (atomic_bit_test_set (&mem, 1)
350 || mem != 2)
352 puts ("atomic_bit_test_set test 1 failed");
353 ret = 1;
356 mem = 8;
357 if (! atomic_bit_test_set (&mem, 3)
358 || mem != 8)
360 puts ("atomic_bit_test_set test 2 failed");
361 ret = 1;
364 #ifdef TEST_ATOMIC64
365 mem = 16;
366 if (atomic_bit_test_set (&mem, 35)
367 || mem != 0x800000010LL)
369 puts ("atomic_bit_test_set test 3 failed");
370 ret = 1;
373 mem = 0x100000000LL;
374 if (! atomic_bit_test_set (&mem, 32)
375 || mem != 0x100000000LL)
377 puts ("atomic_bit_test_set test 4 failed");
378 ret = 1;
380 #endif
382 return ret;
385 #define TEST_FUNCTION do_test ()
386 #include "../test-skeleton.c"