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