Update.
[jlayton/glibc.git] / csu / tst-atomic.c
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.
5
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.
10
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.
15
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.  */
20
21 #include <stdio.h>
22 #include <atomic.h>
23
24 #ifndef atomic_t
25 # define atomic_t int
26 #endif
27
28 /* Test various atomic.h macros.  */
29 static int
30 do_test (void)
31 {
32   atomic_t mem;
33   int ret = 0;
34
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)
39     {
40       puts ("atomic_compare_and_exchange_val_acq test 1 failed");
41       ret = 1;
42     }
43
44   mem = 12;
45   if (atomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
46       || mem != 12)
47     {
48       puts ("atomic_compare_and_exchange_val_acq test 2 failed");
49       ret = 1;
50     }
51 #endif
52
53   mem = 24;
54   if (atomic_compare_and_exchange_bool_acq (&mem, 35, 24)
55       || mem != 35)
56     {
57       puts ("atomic_compare_and_exchange_bool_acq test 1 failed");
58       ret = 1;
59     }
60
61   mem = 12;
62   if (! atomic_compare_and_exchange_bool_acq (&mem, 10, 15)
63       || mem != 12)
64     {
65       puts ("atomic_compare_and_exchange_bool_acq test 2 failed");
66       ret = 1;
67     }
68
69   mem = 64;
70   if (atomic_exchange (&mem, 31) != 64
71       || mem != 31)
72     {
73       puts ("atomic_exchange test failed");
74       ret = 1;
75     }
76
77   mem = 2;
78   if (atomic_exchange_and_add (&mem, 11) != 2
79       || mem != 13)
80     {
81       puts ("atomic_exchange_and_add test failed");
82       ret = 1;
83     }
84
85   mem = -21;
86   atomic_add (&mem, 22);
87   if (mem != 1)
88     {
89       puts ("atomic_add test failed");
90       ret = 1;
91     }
92
93   mem = -1;
94   atomic_increment (&mem);
95   if (mem != 0)
96     {
97       puts ("atomic_increment test failed");
98       ret = 1;
99     }
100
101   mem = 0;
102   if (atomic_increment_and_test (&mem)
103       || mem != 1)
104     {
105       puts ("atomic_increment_and_test test 1 failed");
106       ret = 1;
107     }
108
109   mem = 35;
110   if (atomic_increment_and_test (&mem)
111       || mem != 36)
112     {
113       puts ("atomic_increment_and_test test 2 failed");
114       ret = 1;
115     }
116
117   mem = -1;
118   if (! atomic_increment_and_test (&mem)
119       || mem != 0)
120     {
121       puts ("atomic_increment_and_test test 3 failed");
122       ret = 1;
123     }
124
125   mem = 17;
126   atomic_decrement (&mem);
127   if (mem != 16)
128     {
129       puts ("atomic_decrement test failed");
130       ret = 1;
131     }
132
133   mem = 0;
134   if (atomic_decrement_and_test (&mem)
135       || mem != -1)
136     {
137       puts ("atomic_decrement_and_test test 1 failed");
138       ret = 1;
139     }
140
141   mem = 15;
142   if (atomic_decrement_and_test (&mem)
143       || mem != 14)
144     {
145       puts ("atomic_decrement_and_test test 2 failed");
146       ret = 1;
147     }
148
149   mem = 1;
150   if (! atomic_decrement_and_test (&mem)
151       || mem != 0)
152     {
153       puts ("atomic_decrement_and_test test 1 failed");
154       ret = 1;
155     }
156
157   mem = 1;
158   if (atomic_decrement_if_positive (&mem) != 1
159       || mem != 0)
160     {
161       puts ("atomic_decrement_if_positive test 1 failed");
162       ret = 1;
163     }
164
165   mem = 0;
166   if (atomic_decrement_if_positive (&mem) != 0
167       || mem != 0)
168     {
169       puts ("atomic_decrement_if_positive test 2 failed");
170       ret = 1;
171     }
172
173   mem = -1;
174   if (atomic_decrement_if_positive (&mem) != -1
175       || mem != -1)
176     {
177       puts ("atomic_decrement_if_positive test 3 failed");
178       ret = 1;
179     }
180
181   mem = -12;
182   if (! atomic_add_negative (&mem, 10)
183       || mem != -2)
184     {
185       puts ("atomic_add_negative test 1 failed");
186       ret = 1;
187     }
188
189   mem = 0;
190   if (atomic_add_negative (&mem, 100)
191       || mem != 100)
192     {
193       puts ("atomic_add_negative test 2 failed");
194       ret = 1;
195     }
196
197   mem = 15;
198   if (atomic_add_negative (&mem, -10)
199       || mem != 5)
200     {
201       puts ("atomic_add_negative test 3 failed");
202       ret = 1;
203     }
204
205   mem = -34;
206   if (atomic_add_zero (&mem, 31)
207       || mem != -3)
208     {
209       puts ("atomic_add_zero test 1 failed");
210       ret = 1;
211     }
212
213   mem = -36;
214   if (! atomic_add_zero (&mem, 36)
215       || mem != 0)
216     {
217       puts ("atomic_add_zero test 2 failed");
218       ret = 1;
219     }
220
221   mem = 113;
222   if (atomic_add_zero (&mem, -13)
223       || mem != 100)
224     {
225       puts ("atomic_add_zero test 3 failed");
226       ret = 1;
227     }
228
229   mem = 0;
230   atomic_bit_set (&mem, 1);
231   if (mem != 2)
232     {
233       puts ("atomic_bit_set test 1 failed");
234       ret = 1;
235     }
236
237   mem = 8;
238   atomic_bit_set (&mem, 3);
239   if (mem != 8)
240     {
241       puts ("atomic_bit_set test 2 failed");
242       ret = 1;
243     }
244
245 #ifdef TEST_ATOMIC64
246   mem = 16;
247   atomic_bit_set (&mem, 35);
248   if (mem != 0x800000010LL)
249     {
250       puts ("atomic_bit_set test 3 failed");
251       ret = 1;
252     }
253 #endif
254
255   mem = 0;
256   if (atomic_bit_test_set (&mem, 1)
257       || mem != 2)
258     {
259       puts ("atomic_bit_test_set test 1 failed");
260       ret = 1;
261     }
262
263   mem = 8;
264   if (! atomic_bit_test_set (&mem, 3)
265       || mem != 8)
266     {
267       puts ("atomic_bit_test_set test 2 failed");
268       ret = 1;
269     }
270
271 #ifdef TEST_ATOMIC64
272   mem = 16;
273   if (atomic_bit_test_set (&mem, 35)
274       || mem != 0x800000010LL)
275     {
276       puts ("atomic_bit_test_set test 3 failed");
277       ret = 1;
278     }
279
280   mem = 0x100000000LL;
281   if (! atomic_bit_test_set (&mem, 32)
282       || mem != 0x100000000LL)
283     {
284       puts ("atomic_bit_test_set test 4 failed");
285       ret = 1;
286     }
287 #endif
288
289   return ret;
290 }
291
292 #define TEST_FUNCTION do_test ()
293 #include "../test-skeleton.c"