test/testatomic.c
author Bob Pendleton <bob@pendleton.com>
Tue, 11 Aug 2009 21:27:19 +0000
changeset 3237 916f396fe65d
parent 3216 48a80f2a7ff2
child 3261 72b542f34739
permissions -rw-r--r--
Start of redesign, getting rid of 8 and 16 bit operations in .h and test files.
     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_bool tfret = SDL_FALSE;
    33 
    34    printf("32 bit -----------------------------------------\n\n");
    35 
    36    ret32 = SDL_AtomicExchange32(&val32, 10);
    37    printf("Exchange32           ret=%d val=%d\n", ret32, val32);
    38    ret32 = SDL_AtomicExchange32(&val32, 0);
    39    printf("Exchange32           ret=%d val=%d\n", ret32, val32);
    40 
    41    val32 = 10;
    42    tfret = SDL_AtomicCompareThenSet32(&val32, 10, 20);
    43    printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
    44    val32 = 10;
    45    tfret = SDL_AtomicCompareThenSet32(&val32, 0, 20);
    46    printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
    47 
    48    val32 = 0;
    49    tfret = SDL_AtomicTestThenSet32(&val32);
    50    printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
    51    tfret = SDL_AtomicTestThenSet32(&val32);
    52    printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
    53 
    54    SDL_AtomicClear32(&val32);
    55    printf("Clear32              val=%d\n", val32);
    56 
    57    ret32 = SDL_AtomicFetchThenIncrement32(&val32);
    58    printf("FetchThenIncrement32 ret=%d val=%d\n", ret32, val32);
    59 
    60    ret32 = SDL_AtomicFetchThenDecrement32(&val32);
    61    printf("FetchThenDecrement32 ret=%d val=%d\n", ret32, val32);
    62 
    63    ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
    64    printf("FetchThenAdd32       ret=%d val=%d\n", ret32, val32);
    65 
    66    ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
    67    printf("FetchThenSubtract32  ret=%d val=%d\n", ret32, val32);
    68 
    69    ret32 = SDL_AtomicIncrementThenFetch32(&val32);
    70    printf("IncrementThenFetch32 ret=%d val=%d\n", ret32, val32);
    71 
    72    ret32 = SDL_AtomicDecrementThenFetch32(&val32);
    73    printf("DecrementThenFetch32 ret=%d val=%d\n", ret32, val32);
    74 
    75    ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
    76    printf("AddThenFetch32       ret=%d val=%d\n", ret32, val32);
    77 
    78    ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
    79    printf("SubtractThenFetch32  ret=%d val=%d\n", ret32, val32);
    80 
    81 #ifdef SDL_HAS_64BIT_TYPE
    82    printf("64 bit -----------------------------------------\n\n");
    83 
    84    ret64 = SDL_AtomicExchange64(&val64, 10);
    85    printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
    86    ret64 = SDL_AtomicExchange64(&val64, 0);
    87    printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
    88 
    89    val64 = 10;
    90    tfret = SDL_AtomicCompareThenSet64(&val64, 10, 20);
    91    printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
    92    val64 = 10;
    93    tfret = SDL_AtomicCompareThenSet64(&val64, 0, 20);
    94    printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
    95 
    96    val64 = 0;
    97    tfret = SDL_AtomicTestThenSet64(&val64);
    98    printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
    99    tfret = SDL_AtomicTestThenSet64(&val64);
   100    printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
   101 
   102    SDL_AtomicClear64(&val64);
   103    printf("Clear64              val=%lld\n", val64);
   104 
   105    ret64 = SDL_AtomicFetchThenIncrement64(&val64);
   106    printf("FetchThenIncrement64 ret=%lld val=%lld\n", ret64, val64);
   107 
   108    ret64 = SDL_AtomicFetchThenDecrement64(&val64);
   109    printf("FetchThenDecrement64 ret=%lld val=%lld\n", ret64, val64);
   110 
   111    ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
   112    printf("FetchThenAdd64       ret=%lld val=%lld\n", ret64, val64);
   113 
   114    ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
   115    printf("FetchThenSubtract64  ret=%lld val=%lld\n", ret64, val64);
   116 
   117    ret64 = SDL_AtomicIncrementThenFetch64(&val64);
   118    printf("IncrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
   119 
   120    ret64 = SDL_AtomicDecrementThenFetch64(&val64);
   121    printf("DecrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
   122 
   123    ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
   124    printf("AddThenFetch64       ret=%lld val=%lld\n", ret64, val64);
   125 
   126    ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
   127    printf("SubtractThenFetch64  ret=%lld val=%lld\n", ret64, val64);
   128 #endif
   129 
   130    return 0;
   131    }