atomic-test.c 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN

#include <glib.h>

/* Obviously we can't test that the operations are atomic, but we can
 * at least test, that they do, what they ought to do */

int 
main (int   argc,
      char *argv[])
{
  gint i;
14
  gint atomic = -5;
15
  gpointer atomic_pointer = NULL;
16
  gpointer biggest_pointer = (gpointer)((gsize)atomic_pointer - 1);
17 18 19 20 21 22 23 24 25

  for (i = 0; i < 15; i++)
    g_atomic_int_inc (&atomic);
  g_assert (atomic == 10);
  for (i = 0; i < 9; i++)
    g_assert (!g_atomic_int_dec_and_test (&atomic));
  g_assert (g_atomic_int_dec_and_test (&atomic));
  g_assert (atomic == 0);

26
  g_assert (g_atomic_int_add (&atomic, 5) == 0);
27 28
  g_assert (atomic == 5);

29
  g_assert (g_atomic_int_add (&atomic, -10) == 5);
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  g_assert (atomic == -5);

  g_atomic_int_add (&atomic, 20);
  g_assert (atomic == 15);

  g_atomic_int_add (&atomic, -35);
  g_assert (atomic == -20);

  g_assert (atomic == g_atomic_int_get (&atomic));

  g_assert (g_atomic_int_compare_and_exchange (&atomic, -20, 20));
  g_assert (atomic == 20);
  
  g_assert (!g_atomic_int_compare_and_exchange (&atomic, 42, 12));
  g_assert (atomic == 20);
  
46 47 48 49 50 51
  g_assert (g_atomic_int_compare_and_exchange (&atomic, 20, G_MAXINT));
  g_assert (atomic == G_MAXINT);

  g_assert (g_atomic_int_compare_and_exchange (&atomic, G_MAXINT, G_MININT));
  g_assert (atomic == G_MININT);

52 53 54 55 56 57 58 59 60 61 62 63
  g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer, 
						   NULL, biggest_pointer));
  g_assert (atomic_pointer == biggest_pointer);

  g_assert (atomic_pointer == g_atomic_pointer_get (&atomic_pointer));

  g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer, 
						   biggest_pointer, NULL));
  g_assert (atomic_pointer == NULL);
  
  return 0;
}