Back to index

glibc  2.9
tst-atomic.c
Go to the documentation of this file.
00001 /* Tests for atomic.h macros.
00002    Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdio.h>
00022 #include <atomic.h>
00023 
00024 #ifndef atomic_t
00025 # define atomic_t int
00026 #endif
00027 
00028 /* Test various atomic.h macros.  */
00029 static int
00030 do_test (void)
00031 {
00032   atomic_t mem;
00033   int ret = 0;
00034 
00035 #ifdef atomic_compare_and_exchange_val_acq
00036   mem = 24;
00037   if (atomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
00038       || mem != 35)
00039     {
00040       puts ("atomic_compare_and_exchange_val_acq test 1 failed");
00041       ret = 1;
00042     }
00043 
00044   mem = 12;
00045   if (atomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
00046       || mem != 12)
00047     {
00048       puts ("atomic_compare_and_exchange_val_acq test 2 failed");
00049       ret = 1;
00050     }
00051 
00052   mem = -15;
00053   if (atomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
00054       || mem != -56)
00055     {
00056       puts ("atomic_compare_and_exchange_val_acq test 3 failed");
00057       ret = 1;
00058     }
00059 
00060   mem = -1;
00061   if (atomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
00062       || mem != -1)
00063     {
00064       puts ("atomic_compare_and_exchange_val_acq test 4 failed");
00065       ret = 1;
00066     }
00067 #endif
00068 
00069   mem = 24;
00070   if (atomic_compare_and_exchange_bool_acq (&mem, 35, 24)
00071       || mem != 35)
00072     {
00073       puts ("atomic_compare_and_exchange_bool_acq test 1 failed");
00074       ret = 1;
00075     }
00076 
00077   mem = 12;
00078   if (! atomic_compare_and_exchange_bool_acq (&mem, 10, 15)
00079       || mem != 12)
00080     {
00081       puts ("atomic_compare_and_exchange_bool_acq test 2 failed");
00082       ret = 1;
00083     }
00084 
00085   mem = -15;
00086   if (atomic_compare_and_exchange_bool_acq (&mem, -56, -15)
00087       || mem != -56)
00088     {
00089       puts ("atomic_compare_and_exchange_bool_acq test 3 failed");
00090       ret = 1;
00091     }
00092 
00093   mem = -1;
00094   if (! atomic_compare_and_exchange_bool_acq (&mem, 17, 0)
00095       || mem != -1)
00096     {
00097       puts ("atomic_compare_and_exchange_bool_acq test 4 failed");
00098       ret = 1;
00099     }
00100 
00101   mem = 64;
00102   if (atomic_exchange_acq (&mem, 31) != 64
00103       || mem != 31)
00104     {
00105       puts ("atomic_exchange_acq test failed");
00106       ret = 1;
00107     }
00108 
00109   mem = 2;
00110   if (atomic_exchange_and_add (&mem, 11) != 2
00111       || mem != 13)
00112     {
00113       puts ("atomic_exchange_and_add test failed");
00114       ret = 1;
00115     }
00116 
00117   mem = -21;
00118   atomic_add (&mem, 22);
00119   if (mem != 1)
00120     {
00121       puts ("atomic_add test failed");
00122       ret = 1;
00123     }
00124 
00125   mem = -1;
00126   atomic_increment (&mem);
00127   if (mem != 0)
00128     {
00129       puts ("atomic_increment test failed");
00130       ret = 1;
00131     }
00132 
00133   mem = 2;
00134   if (atomic_increment_val (&mem) != 3)
00135     {
00136       puts ("atomic_increment_val test failed");
00137       ret = 1;
00138     }
00139 
00140   mem = 0;
00141   if (atomic_increment_and_test (&mem)
00142       || mem != 1)
00143     {
00144       puts ("atomic_increment_and_test test 1 failed");
00145       ret = 1;
00146     }
00147 
00148   mem = 35;
00149   if (atomic_increment_and_test (&mem)
00150       || mem != 36)
00151     {
00152       puts ("atomic_increment_and_test test 2 failed");
00153       ret = 1;
00154     }
00155 
00156   mem = -1;
00157   if (! atomic_increment_and_test (&mem)
00158       || mem != 0)
00159     {
00160       puts ("atomic_increment_and_test test 3 failed");
00161       ret = 1;
00162     }
00163 
00164   mem = 17;
00165   atomic_decrement (&mem);
00166   if (mem != 16)
00167     {
00168       puts ("atomic_decrement test failed");
00169       ret = 1;
00170     }
00171 
00172   if (atomic_decrement_val (&mem) != 15)
00173     {
00174       puts ("atomic_decrement_val test failed");
00175       ret = 1;
00176     }
00177 
00178   mem = 0;
00179   if (atomic_decrement_and_test (&mem)
00180       || mem != -1)
00181     {
00182       puts ("atomic_decrement_and_test test 1 failed");
00183       ret = 1;
00184     }
00185 
00186   mem = 15;
00187   if (atomic_decrement_and_test (&mem)
00188       || mem != 14)
00189     {
00190       puts ("atomic_decrement_and_test test 2 failed");
00191       ret = 1;
00192     }
00193 
00194   mem = 1;
00195   if (! atomic_decrement_and_test (&mem)
00196       || mem != 0)
00197     {
00198       puts ("atomic_decrement_and_test test 3 failed");
00199       ret = 1;
00200     }
00201 
00202   mem = 1;
00203   if (atomic_decrement_if_positive (&mem) != 1
00204       || mem != 0)
00205     {
00206       puts ("atomic_decrement_if_positive test 1 failed");
00207       ret = 1;
00208     }
00209 
00210   mem = 0;
00211   if (atomic_decrement_if_positive (&mem) != 0
00212       || mem != 0)
00213     {
00214       puts ("atomic_decrement_if_positive test 2 failed");
00215       ret = 1;
00216     }
00217 
00218   mem = -1;
00219   if (atomic_decrement_if_positive (&mem) != -1
00220       || mem != -1)
00221     {
00222       puts ("atomic_decrement_if_positive test 3 failed");
00223       ret = 1;
00224     }
00225 
00226   mem = -12;
00227   if (! atomic_add_negative (&mem, 10)
00228       || mem != -2)
00229     {
00230       puts ("atomic_add_negative test 1 failed");
00231       ret = 1;
00232     }
00233 
00234   mem = 0;
00235   if (atomic_add_negative (&mem, 100)
00236       || mem != 100)
00237     {
00238       puts ("atomic_add_negative test 2 failed");
00239       ret = 1;
00240     }
00241 
00242   mem = 15;
00243   if (atomic_add_negative (&mem, -10)
00244       || mem != 5)
00245     {
00246       puts ("atomic_add_negative test 3 failed");
00247       ret = 1;
00248     }
00249 
00250   mem = -12;
00251   if (atomic_add_negative (&mem, 14)
00252       || mem != 2)
00253     {
00254       puts ("atomic_add_negative test 4 failed");
00255       ret = 1;
00256     }
00257 
00258   mem = 0;
00259   if (! atomic_add_negative (&mem, -1)
00260       || mem != -1)
00261     {
00262       puts ("atomic_add_negative test 5 failed");
00263       ret = 1;
00264     }
00265 
00266   mem = -31;
00267   if (atomic_add_negative (&mem, 31)
00268       || mem != 0)
00269     {
00270       puts ("atomic_add_negative test 6 failed");
00271       ret = 1;
00272     }
00273 
00274   mem = -34;
00275   if (atomic_add_zero (&mem, 31)
00276       || mem != -3)
00277     {
00278       puts ("atomic_add_zero test 1 failed");
00279       ret = 1;
00280     }
00281 
00282   mem = -36;
00283   if (! atomic_add_zero (&mem, 36)
00284       || mem != 0)
00285     {
00286       puts ("atomic_add_zero test 2 failed");
00287       ret = 1;
00288     }
00289 
00290   mem = 113;
00291   if (atomic_add_zero (&mem, -13)
00292       || mem != 100)
00293     {
00294       puts ("atomic_add_zero test 3 failed");
00295       ret = 1;
00296     }
00297 
00298   mem = -18;
00299   if (atomic_add_zero (&mem, 20)
00300       || mem != 2)
00301     {
00302       puts ("atomic_add_zero test 4 failed");
00303       ret = 1;
00304     }
00305 
00306   mem = 10;
00307   if (atomic_add_zero (&mem, -20)
00308       || mem != -10)
00309     {
00310       puts ("atomic_add_zero test 5 failed");
00311       ret = 1;
00312     }
00313 
00314   mem = 10;
00315   if (! atomic_add_zero (&mem, -10)
00316       || mem != 0)
00317     {
00318       puts ("atomic_add_zero test 6 failed");
00319       ret = 1;
00320     }
00321 
00322   mem = 0;
00323   atomic_bit_set (&mem, 1);
00324   if (mem != 2)
00325     {
00326       puts ("atomic_bit_set test 1 failed");
00327       ret = 1;
00328     }
00329 
00330   mem = 8;
00331   atomic_bit_set (&mem, 3);
00332   if (mem != 8)
00333     {
00334       puts ("atomic_bit_set test 2 failed");
00335       ret = 1;
00336     }
00337 
00338 #ifdef TEST_ATOMIC64
00339   mem = 16;
00340   atomic_bit_set (&mem, 35);
00341   if (mem != 0x800000010LL)
00342     {
00343       puts ("atomic_bit_set test 3 failed");
00344       ret = 1;
00345     }
00346 #endif
00347 
00348   mem = 0;
00349   if (atomic_bit_test_set (&mem, 1)
00350       || mem != 2)
00351     {
00352       puts ("atomic_bit_test_set test 1 failed");
00353       ret = 1;
00354     }
00355 
00356   mem = 8;
00357   if (! atomic_bit_test_set (&mem, 3)
00358       || mem != 8)
00359     {
00360       puts ("atomic_bit_test_set test 2 failed");
00361       ret = 1;
00362     }
00363 
00364 #ifdef TEST_ATOMIC64
00365   mem = 16;
00366   if (atomic_bit_test_set (&mem, 35)
00367       || mem != 0x800000010LL)
00368     {
00369       puts ("atomic_bit_test_set test 3 failed");
00370       ret = 1;
00371     }
00372 
00373   mem = 0x100000000LL;
00374   if (! atomic_bit_test_set (&mem, 32)
00375       || mem != 0x100000000LL)
00376     {
00377       puts ("atomic_bit_test_set test 4 failed");
00378       ret = 1;
00379     }
00380 #endif
00381 
00382 #ifdef catomic_compare_and_exchange_val_acq
00383   mem = 24;
00384   if (catomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
00385       || mem != 35)
00386     {
00387       puts ("catomic_compare_and_exchange_val_acq test 1 failed");
00388       ret = 1;
00389     }
00390 
00391   mem = 12;
00392   if (catomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
00393       || mem != 12)
00394     {
00395       puts ("catomic_compare_and_exchange_val_acq test 2 failed");
00396       ret = 1;
00397     }
00398 
00399   mem = -15;
00400   if (catomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
00401       || mem != -56)
00402     {
00403       puts ("catomic_compare_and_exchange_val_acq test 3 failed");
00404       ret = 1;
00405     }
00406 
00407   mem = -1;
00408   if (catomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
00409       || mem != -1)
00410     {
00411       puts ("catomic_compare_and_exchange_val_acq test 4 failed");
00412       ret = 1;
00413     }
00414 #endif
00415 
00416   mem = 24;
00417   if (catomic_compare_and_exchange_bool_acq (&mem, 35, 24)
00418       || mem != 35)
00419     {
00420       puts ("catomic_compare_and_exchange_bool_acq test 1 failed");
00421       ret = 1;
00422     }
00423 
00424   mem = 12;
00425   if (! catomic_compare_and_exchange_bool_acq (&mem, 10, 15)
00426       || mem != 12)
00427     {
00428       puts ("catomic_compare_and_exchange_bool_acq test 2 failed");
00429       ret = 1;
00430     }
00431 
00432   mem = -15;
00433   if (catomic_compare_and_exchange_bool_acq (&mem, -56, -15)
00434       || mem != -56)
00435     {
00436       puts ("catomic_compare_and_exchange_bool_acq test 3 failed");
00437       ret = 1;
00438     }
00439 
00440   mem = -1;
00441   if (! catomic_compare_and_exchange_bool_acq (&mem, 17, 0)
00442       || mem != -1)
00443     {
00444       puts ("catomic_compare_and_exchange_bool_acq test 4 failed");
00445       ret = 1;
00446     }
00447 
00448   mem = 2;
00449   if (catomic_exchange_and_add (&mem, 11) != 2
00450       || mem != 13)
00451     {
00452       puts ("catomic_exchange_and_add test failed");
00453       ret = 1;
00454     }
00455 
00456   mem = -21;
00457   catomic_add (&mem, 22);
00458   if (mem != 1)
00459     {
00460       puts ("catomic_add test failed");
00461       ret = 1;
00462     }
00463 
00464   mem = -1;
00465   catomic_increment (&mem);
00466   if (mem != 0)
00467     {
00468       puts ("catomic_increment test failed");
00469       ret = 1;
00470     }
00471 
00472   mem = 2;
00473   if (catomic_increment_val (&mem) != 3)
00474     {
00475       puts ("catomic_increment_val test failed");
00476       ret = 1;
00477     }
00478 
00479   mem = 17;
00480   catomic_decrement (&mem);
00481   if (mem != 16)
00482     {
00483       puts ("catomic_decrement test failed");
00484       ret = 1;
00485     }
00486 
00487   if (catomic_decrement_val (&mem) != 15)
00488     {
00489       puts ("catomic_decrement_val test failed");
00490       ret = 1;
00491     }
00492 
00493   return ret;
00494 }
00495 
00496 #define TEST_FUNCTION do_test ()
00497 #include "../test-skeleton.c"