Home | History | Annotate | Download | only in tests
      1 #undef G_DISABLE_ASSERT
      2 #undef G_LOG_DOMAIN
      3 
      4 #include <glib.h>
      5 
      6 /* Obviously we can't test that the operations are atomic, but we can
      7  * at least test, that they do, what they ought to do */
      8 
      9 int
     10 main (int   argc,
     11       char *argv[])
     12 {
     13   gint i;
     14   gint atomic = -5;
     15   gpointer atomic_pointer = NULL;
     16   gpointer biggest_pointer = (gpointer)((gsize)atomic_pointer - 1);
     17 
     18   for (i = 0; i < 15; i++)
     19     g_atomic_int_inc (&atomic);
     20   g_assert (atomic == 10);
     21   for (i = 0; i < 9; i++)
     22     g_assert (!g_atomic_int_dec_and_test (&atomic));
     23   g_assert (g_atomic_int_dec_and_test (&atomic));
     24   g_assert (atomic == 0);
     25 
     26   g_assert (g_atomic_int_exchange_and_add (&atomic, 5) == 0);
     27   g_assert (atomic == 5);
     28 
     29   g_assert (g_atomic_int_exchange_and_add (&atomic, -10) == 5);
     30   g_assert (atomic == -5);
     31 
     32   g_atomic_int_add (&atomic, 20);
     33   g_assert (atomic == 15);
     34 
     35   g_atomic_int_add (&atomic, -35);
     36   g_assert (atomic == -20);
     37 
     38   g_assert (atomic == g_atomic_int_get (&atomic));
     39 
     40   g_assert (g_atomic_int_compare_and_exchange (&atomic, -20, 20));
     41   g_assert (atomic == 20);
     42 
     43   g_assert (!g_atomic_int_compare_and_exchange (&atomic, 42, 12));
     44   g_assert (atomic == 20);
     45 
     46   g_assert (g_atomic_int_compare_and_exchange (&atomic, 20, G_MAXINT));
     47   g_assert (atomic == G_MAXINT);
     48 
     49   g_assert (g_atomic_int_compare_and_exchange (&atomic, G_MAXINT, G_MININT));
     50   g_assert (atomic == G_MININT);
     51 
     52   g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer,
     53 						   NULL, biggest_pointer));
     54   g_assert (atomic_pointer == biggest_pointer);
     55 
     56   g_assert (atomic_pointer == g_atomic_pointer_get (&atomic_pointer));
     57 
     58   g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer,
     59 						   biggest_pointer, NULL));
     60   g_assert (atomic_pointer == NULL);
     61 
     62   return 0;
     63 }
     64