glib/tsrc/BC/tests/atomic-test.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */
       
     2 #undef G_DISABLE_ASSERT
       
     3 #undef G_LOG_DOMAIN
       
     4 
       
     5 #include <glib.h>
       
     6 #include <stdio.h>
       
     7 
       
     8 #ifdef SYMBIAN
       
     9 #include "mrt2_glib2_test.h"
       
    10 #endif /*SYMBIAN*/
       
    11 
       
    12 
       
    13 /* Obviously we can't test that the operations are atomic, but we can
       
    14  * at least test, that they do, what they ought to do */
       
    15 
       
    16 int 
       
    17 main (int   argc,
       
    18       char *argv[])
       
    19 {
       
    20   gint i;
       
    21   gint atomic = -5;
       
    22   gpointer atomic_pointer = NULL;
       
    23   
       
    24   gpointer biggest_pointer = (gpointer)((gsize)atomic_pointer - 1);
       
    25   
       
    26   #ifdef SYMBIAN
       
    27   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
    28   g_set_print_handler(mrtPrintHandler);
       
    29   #endif /*SYMBIAN*/
       
    30 
       
    31   for (i = 0; i < 15; i++)
       
    32     g_atomic_int_inc (&atomic);
       
    33   g_assert (atomic == 10);
       
    34   for (i = 0; i < 9; i++)
       
    35     g_assert (!g_atomic_int_dec_and_test (&atomic));
       
    36   g_assert (g_atomic_int_dec_and_test (&atomic));
       
    37   g_assert (atomic == 0);
       
    38 
       
    39   g_assert (g_atomic_int_exchange_and_add (&atomic, 5) == 0);
       
    40   g_assert (atomic == 5);
       
    41 
       
    42   g_assert (g_atomic_int_exchange_and_add (&atomic, -10) == 5);
       
    43   g_assert (atomic == -5);
       
    44 
       
    45   g_atomic_int_add (&atomic, 20);
       
    46   g_assert (atomic == 15);
       
    47 
       
    48   g_atomic_int_add (&atomic, -35);
       
    49   g_assert (atomic == -20);
       
    50 
       
    51   g_assert (atomic == g_atomic_int_get (&atomic));
       
    52 
       
    53   g_assert (g_atomic_int_compare_and_exchange (&atomic, -20, 20));
       
    54   g_assert (atomic == 20);
       
    55   
       
    56   g_assert (!g_atomic_int_compare_and_exchange (&atomic, 42, 12));
       
    57   g_assert (atomic == 20);
       
    58   
       
    59   g_assert (g_atomic_int_compare_and_exchange (&atomic, 20, G_MAXINT));
       
    60   g_assert (atomic == G_MAXINT);
       
    61 
       
    62   g_assert (g_atomic_int_compare_and_exchange (&atomic, G_MAXINT, G_MININT));
       
    63   g_assert (atomic == G_MININT);
       
    64 
       
    65   g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer, 
       
    66 						   NULL, biggest_pointer));
       
    67   g_assert (atomic_pointer == biggest_pointer);
       
    68 
       
    69   g_assert (atomic_pointer == g_atomic_pointer_get (&atomic_pointer));
       
    70 
       
    71   g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer, 
       
    72 						   biggest_pointer, NULL));
       
    73   g_assert (atomic_pointer == NULL);
       
    74   
       
    75   #ifdef SYMBIAN
       
    76   testResultXml("atomic-test");
       
    77   #endif /* EMULATOR */
       
    78   
       
    79   return 0;
       
    80 }