test/testatomic.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 29 Sep 2009 01:19:14 +0000
changeset 3327 35387815b155
parent 3325 bb9c3d04f126
child 3338 9de326b3099c
permissions -rw-r--r--
Better cross-platform macros for printing 32 and 64 bit values
     1 #include "SDL.h"
     2 
     3 /* Make sure we have good macros for printing 32 and 64 bit values */
     4 #ifndef PRIu32
     5 #define PRIu32 "u"
     6 #endif
     7 #ifndef PRIu64
     8 #ifdef __WIN32__
     9 #define PRIu64 "I64u"
    10 #else
    11 #define PRIu64 "llu"
    12 #endif
    13 #endif
    14 
    15 /*
    16   Absolutely basic tests just to see if we get the expected value
    17   after calling each function.
    18 */
    19 
    20 char * 
    21 tf(SDL_bool tf)
    22 {
    23    static char *t = "true";
    24    static char *f = "false";
    25 
    26    if (tf)
    27    {
    28       return t;
    29    }
    30 
    31    return f;
    32 }
    33   
    34 int
    35 main(int argc, char *argv[])
    36 {
    37 
    38    volatile Uint32 val32 = 0;
    39    Uint32 ret32 = 0;
    40 
    41    volatile Uint64 val64 = 0;
    42    Uint64 ret64 = 0;
    43 
    44    SDL_SpinLock lock = 0;
    45 
    46    SDL_bool tfret = SDL_FALSE;
    47 
    48    printf("\nspin lock---------------------------------------\n\n");
    49 
    50    SDL_AtomicLock(&lock);
    51    printf("AtomicLock                   lock=%d\n", lock);
    52    SDL_AtomicUnlock(&lock);
    53    printf("AtomicUnlock                 lock=%d\n", lock);
    54 
    55    printf("\n32 bit -----------------------------------------\n\n");
    56 
    57    val32 = 0;
    58    tfret = SDL_AtomicTestThenSet32(&val32);
    59    printf("TestThenSet32        tfret=%s val=%"PRIu32"\n", tf(tfret), val32);
    60    tfret = SDL_AtomicTestThenSet32(&val32);
    61    printf("TestThenSet32        tfret=%s val=%"PRIu32"\n", tf(tfret), val32);
    62 
    63    SDL_AtomicClear32(&val32);
    64    printf("Clear32              val=%"PRIu32"\n", val32);
    65 
    66    ret32 = SDL_AtomicFetchThenIncrement32(&val32);
    67    printf("FetchThenIncrement32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
    68 
    69    ret32 = SDL_AtomicFetchThenDecrement32(&val32);
    70    printf("FetchThenDecrement32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
    71 
    72    ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
    73    printf("FetchThenAdd32       ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
    74 
    75    ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
    76    printf("FetchThenSubtract32  ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
    77 
    78    ret32 = SDL_AtomicIncrementThenFetch32(&val32);
    79    printf("IncrementThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
    80 
    81    ret32 = SDL_AtomicDecrementThenFetch32(&val32);
    82    printf("DecrementThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
    83 
    84    ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
    85    printf("AddThenFetch32       ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
    86 
    87    ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
    88    printf("SubtractThenFetch32  ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
    89 
    90 #ifdef SDL_HAS_64BIT_TYPE
    91    printf("\n64 bit -----------------------------------------\n\n");
    92 
    93    val64 = 0;
    94    tfret = SDL_AtomicTestThenSet64(&val64);
    95    printf("TestThenSet64        tfret=%s val=%"PRIu64"\n", tf(tfret), val64);
    96    tfret = SDL_AtomicTestThenSet64(&val64);
    97    printf("TestThenSet64        tfret=%s val=%"PRIu64"\n", tf(tfret), val64);
    98 
    99    SDL_AtomicClear64(&val64);
   100    printf("Clear64              val=%"PRIu64"\n", val64);
   101 
   102    ret64 = SDL_AtomicFetchThenIncrement64(&val64);
   103    printf("FetchThenIncrement64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
   104 
   105    ret64 = SDL_AtomicFetchThenDecrement64(&val64);
   106    printf("FetchThenDecrement64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
   107 
   108    ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
   109    printf("FetchThenAdd64       ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
   110 
   111    ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
   112    printf("FetchThenSubtract64  ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
   113 
   114    ret64 = SDL_AtomicIncrementThenFetch64(&val64);
   115    printf("IncrementThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
   116 
   117    ret64 = SDL_AtomicDecrementThenFetch64(&val64);
   118    printf("DecrementThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
   119 
   120    ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
   121    printf("AddThenFetch64       ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
   122 
   123    ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
   124    printf("SubtractThenFetch64  ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
   125 #endif
   126 
   127    return 0;
   128    }