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