Update.
[glibc.git] / csu / tst-atomic.c
blobcb6b6ba3d4b33ff6383203aee5b6aba466262e14
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 if (atomic_increment_val (&mem) != 1)
135 puts ("atomic_increment_val test failed");
136 ret = 1;
139 mem = 0;
140 if (atomic_increment_and_test (&mem)
141 || mem != 1)
143 puts ("atomic_increment_and_test test 1 failed");
144 ret = 1;
147 mem = 35;
148 if (atomic_increment_and_test (&mem)
149 || mem != 36)
151 puts ("atomic_increment_and_test test 2 failed");
152 ret = 1;
155 mem = -1;
156 if (! atomic_increment_and_test (&mem)
157 || mem != 0)
159 puts ("atomic_increment_and_test test 3 failed");
160 ret = 1;
163 mem = 17;
164 atomic_decrement (&mem);
165 if (mem != 16)
167 puts ("atomic_decrement test failed");
168 ret = 1;
171 if (atomic_decrement_val (&mem) != 15)
173 puts ("atomic_decrement_val test failed");
174 ret = 1;
177 mem = 0;
178 if (atomic_decrement_and_test (&mem)
179 || mem != -1)
181 puts ("atomic_decrement_and_test test 1 failed");
182 ret = 1;
185 mem = 15;
186 if (atomic_decrement_and_test (&mem)
187 || mem != 14)
189 puts ("atomic_decrement_and_test test 2 failed");
190 ret = 1;
193 mem = 1;
194 if (! atomic_decrement_and_test (&mem)
195 || mem != 0)
197 puts ("atomic_decrement_and_test test 3 failed");
198 ret = 1;
201 mem = 1;
202 if (atomic_decrement_if_positive (&mem) != 1
203 || mem != 0)
205 puts ("atomic_decrement_if_positive test 1 failed");
206 ret = 1;
209 mem = 0;
210 if (atomic_decrement_if_positive (&mem) != 0
211 || mem != 0)
213 puts ("atomic_decrement_if_positive test 2 failed");
214 ret = 1;
217 mem = -1;
218 if (atomic_decrement_if_positive (&mem) != -1
219 || mem != -1)
221 puts ("atomic_decrement_if_positive test 3 failed");
222 ret = 1;
225 mem = -12;
226 if (! atomic_add_negative (&mem, 10)
227 || mem != -2)
229 puts ("atomic_add_negative test 1 failed");
230 ret = 1;
233 mem = 0;
234 if (atomic_add_negative (&mem, 100)
235 || mem != 100)
237 puts ("atomic_add_negative test 2 failed");
238 ret = 1;
241 mem = 15;
242 if (atomic_add_negative (&mem, -10)
243 || mem != 5)
245 puts ("atomic_add_negative test 3 failed");
246 ret = 1;
249 mem = -12;
250 if (atomic_add_negative (&mem, 14)
251 || mem != 2)
253 puts ("atomic_add_negative test 4 failed");
254 ret = 1;
257 mem = 0;
258 if (! atomic_add_negative (&mem, -1)
259 || mem != -1)
261 puts ("atomic_add_negative test 5 failed");
262 ret = 1;
265 mem = -31;
266 if (atomic_add_negative (&mem, 31)
267 || mem != 0)
269 puts ("atomic_add_negative test 6 failed");
270 ret = 1;
273 mem = -34;
274 if (atomic_add_zero (&mem, 31)
275 || mem != -3)
277 puts ("atomic_add_zero test 1 failed");
278 ret = 1;
281 mem = -36;
282 if (! atomic_add_zero (&mem, 36)
283 || mem != 0)
285 puts ("atomic_add_zero test 2 failed");
286 ret = 1;
289 mem = 113;
290 if (atomic_add_zero (&mem, -13)
291 || mem != 100)
293 puts ("atomic_add_zero test 3 failed");
294 ret = 1;
297 mem = -18;
298 if (atomic_add_zero (&mem, 20)
299 || mem != 2)
301 puts ("atomic_add_zero test 4 failed");
302 ret = 1;
305 mem = 10;
306 if (atomic_add_zero (&mem, -20)
307 || mem != -10)
309 puts ("atomic_add_zero test 5 failed");
310 ret = 1;
313 mem = 10;
314 if (! atomic_add_zero (&mem, -10)
315 || mem != 0)
317 puts ("atomic_add_zero test 6 failed");
318 ret = 1;
321 mem = 0;
322 atomic_bit_set (&mem, 1);
323 if (mem != 2)
325 puts ("atomic_bit_set test 1 failed");
326 ret = 1;
329 mem = 8;
330 atomic_bit_set (&mem, 3);
331 if (mem != 8)
333 puts ("atomic_bit_set test 2 failed");
334 ret = 1;
337 #ifdef TEST_ATOMIC64
338 mem = 16;
339 atomic_bit_set (&mem, 35);
340 if (mem != 0x800000010LL)
342 puts ("atomic_bit_set test 3 failed");
343 ret = 1;
345 #endif
347 mem = 0;
348 if (atomic_bit_test_set (&mem, 1)
349 || mem != 2)
351 puts ("atomic_bit_test_set test 1 failed");
352 ret = 1;
355 mem = 8;
356 if (! atomic_bit_test_set (&mem, 3)
357 || mem != 8)
359 puts ("atomic_bit_test_set test 2 failed");
360 ret = 1;
363 #ifdef TEST_ATOMIC64
364 mem = 16;
365 if (atomic_bit_test_set (&mem, 35)
366 || mem != 0x800000010LL)
368 puts ("atomic_bit_test_set test 3 failed");
369 ret = 1;
372 mem = 0x100000000LL;
373 if (! atomic_bit_test_set (&mem, 32)
374 || mem != 0x100000000LL)
376 puts ("atomic_bit_test_set test 4 failed");
377 ret = 1;
379 #endif
381 return ret;
384 #define TEST_FUNCTION do_test ()
385 #include "../test-skeleton.c"