test/testatomic.c
changeset 3202 3aa519a5c676
parent 3201 c297230efc75
child 3216 48a80f2a7ff2
equal deleted inserted replaced
3201:c297230efc75 3202:3aa519a5c676
     1 #include "SDL.h"
     1 #include "SDL.h"
     2 
     2 
     3 /*
     3 /*
     4   Absolutely basic test just to see if we get the expected value after
     4   Absolutely basic tests just to see if we get the expected value
     5   calling each function.
     5   after calling each function.
     6 */
     6 */
     7 
     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   
     8 int
    22 int
     9 main(int argc, char **argv)
    23 main(int argc, char **argv)
    10 {
    24 {
    11 
    25 
       
    26    Uint8 val8 = 0;
       
    27    Uint8 ret8 = 0;
       
    28 
       
    29    Uint16 val16 = 0;
       
    30    Uint16 ret16 = 0;
       
    31 
    12    Uint32 val32 = 0;
    32    Uint32 val32 = 0;
    13    Uint32 ret32 = 0;
    33    Uint32 ret32 = 0;
    14 
    34 
    15    Uint64 val64 = 0;
    35    Uint64 val64 = 0;
    16    Uint64 ret64 = 0;
    36    Uint64 ret64 = 0;
    17 
    37 
    18    SDL_bool tfval = SDL_FALSE;
    38    SDL_bool tfret = SDL_FALSE;
       
    39 
       
    40 
       
    41    printf("8 bit -----------------------------------------\n\n");
       
    42 
       
    43    ret8 = SDL_AtomicExchange8(&val8, 10);
       
    44    printf("Exchange8           ret=%d val=%d\n", ret8, val8);
       
    45    ret8 = SDL_AtomicExchange8(&val8, 0);
       
    46    printf("Exchange8           ret=%d val=%d\n", ret8, val8);
       
    47 
       
    48    val8 = 10;
       
    49    tfret = SDL_AtomicCompareThenSet8(&val8, 10, 20);
       
    50    printf("CompareThenSet8     tfret=%s val=%d\n", tf(tfret), val8);
       
    51    val8 = 10;
       
    52    tfret = SDL_AtomicCompareThenSet8(&val8, 0, 20);
       
    53    printf("CompareThenSet8     tfret=%s val=%d\n", tf(tfret), val8);
       
    54 
       
    55    val8 = 0;
       
    56    tfret = SDL_AtomicTestThenSet8(&val8);
       
    57    printf("TestThenSet8        tfret=%s val=%d\n", tf(tfret), val8);
       
    58    tfret = SDL_AtomicTestThenSet8(&val8);
       
    59    printf("TestThenSet8        tfret=%s val=%d\n", tf(tfret), val8);
       
    60 
       
    61    SDL_AtomicClear8(&val8);
       
    62    printf("Clear8              val=%d\n", val8);
       
    63 
       
    64    ret8 = SDL_AtomicFetchThenIncrement8(&val8);
       
    65    printf("FetchThenIncrement8 ret=%d val=%d\n", ret8, val8);
       
    66 
       
    67    ret8 = SDL_AtomicFetchThenDecrement8(&val8);
       
    68    printf("FetchThenDecrement8 ret=%d val=%d\n", ret8, val8);
       
    69 
       
    70    ret8 = SDL_AtomicFetchThenAdd8(&val8, 10);
       
    71    printf("FetchThenAdd8       ret=%d val=%d\n", ret8, val8);
       
    72 
       
    73    ret8 = SDL_AtomicFetchThenSubtract8(&val8, 10);
       
    74    printf("FetchThenSubtract8  ret=%d val=%d\n", ret8, val8);
       
    75 
       
    76    ret8 = SDL_AtomicIncrementThenFetch8(&val8);
       
    77    printf("IncrementThenFetch8 ret=%d val=%d\n", ret8, val8);
       
    78 
       
    79    ret8 = SDL_AtomicDecrementThenFetch8(&val8);
       
    80    printf("DecrementThenFetch8 ret=%d val=%d\n", ret8, val8);
       
    81 
       
    82    ret8 = SDL_AtomicAddThenFetch8(&val8, 10);
       
    83    printf("AddThenFetch8       ret=%d val=%d\n", ret8, val8);
       
    84 
       
    85    ret8 = SDL_AtomicSubtractThenFetch8(&val8, 10);
       
    86    printf("SubtractThenFetch8  ret=%d val=%d\n", ret8, val8);
       
    87 
       
    88 
       
    89    printf("16 bit -----------------------------------------\n\n");
       
    90 
       
    91    ret16 = SDL_AtomicExchange16(&val16, 10);
       
    92    printf("Exchange16           ret=%d val=%d\n", ret16, val16);
       
    93    ret16 = SDL_AtomicExchange16(&val16, 0);
       
    94    printf("Exchange16           ret=%d val=%d\n", ret16, val16);
       
    95 
       
    96    val16 = 10;
       
    97    tfret = SDL_AtomicCompareThenSet16(&val16, 10, 20);
       
    98    printf("CompareThenSet16     tfret=%s val=%d\n", tf(tfret), val16);
       
    99    val16 = 10;
       
   100    tfret = SDL_AtomicCompareThenSet16(&val16, 0, 20);
       
   101    printf("CompareThenSet16     tfret=%s val=%d\n", tf(tfret), val16);
       
   102 
       
   103    val16 = 0;
       
   104    tfret = SDL_AtomicTestThenSet16(&val16);
       
   105    printf("TestThenSet16        tfret=%s val=%d\n", tf(tfret), val16);
       
   106    tfret = SDL_AtomicTestThenSet16(&val16);
       
   107    printf("TestThenSet16        tfret=%s val=%d\n", tf(tfret), val16);
       
   108 
       
   109    SDL_AtomicClear16(&val16);
       
   110    printf("Clear16              val=%d\n", val16);
       
   111 
       
   112    ret16 = SDL_AtomicFetchThenIncrement16(&val16);
       
   113    printf("FetchThenIncrement16 ret=%d val=%d\n", ret16, val16);
       
   114 
       
   115    ret16 = SDL_AtomicFetchThenDecrement16(&val16);
       
   116    printf("FetchThenDecrement16 ret=%d val=%d\n", ret16, val16);
       
   117 
       
   118    ret16 = SDL_AtomicFetchThenAdd16(&val16, 10);
       
   119    printf("FetchThenAdd16       ret=%d val=%d\n", ret16, val16);
       
   120 
       
   121    ret16 = SDL_AtomicFetchThenSubtract16(&val16, 10);
       
   122    printf("FetchThenSubtract16  ret=%d val=%d\n", ret16, val16);
       
   123 
       
   124    ret16 = SDL_AtomicIncrementThenFetch16(&val16);
       
   125    printf("IncrementThenFetch16 ret=%d val=%d\n", ret16, val16);
       
   126 
       
   127    ret16 = SDL_AtomicDecrementThenFetch16(&val16);
       
   128    printf("DecrementThenFetch16 ret=%d val=%d\n", ret16, val16);
       
   129 
       
   130    ret16 = SDL_AtomicAddThenFetch16(&val16, 10);
       
   131    printf("AddThenFetch16       ret=%d val=%d\n", ret16, val16);
       
   132 
       
   133    ret16 = SDL_AtomicSubtractThenFetch16(&val16, 10);
       
   134    printf("SubtractThenFetch16  ret=%d val=%d\n", ret16, val16);
       
   135 
       
   136    printf("32 bit -----------------------------------------\n\n");
    19 
   137 
    20    ret32 = SDL_AtomicExchange32(&val32, 10);
   138    ret32 = SDL_AtomicExchange32(&val32, 10);
    21    tfval = SDL_AtomicCompareThenSet32(&val32, 10, 20);
   139    printf("Exchange32           ret=%d val=%d\n", ret32, val32);
    22    tfval = SDL_AtomicTestThenSet32(&val32);
   140    ret32 = SDL_AtomicExchange32(&val32, 0);
       
   141    printf("Exchange32           ret=%d val=%d\n", ret32, val32);
       
   142 
       
   143    val32 = 10;
       
   144    tfret = SDL_AtomicCompareThenSet32(&val32, 10, 20);
       
   145    printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
       
   146    val32 = 10;
       
   147    tfret = SDL_AtomicCompareThenSet32(&val32, 0, 20);
       
   148    printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
       
   149 
       
   150    val32 = 0;
       
   151    tfret = SDL_AtomicTestThenSet32(&val32);
       
   152    printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
       
   153    tfret = SDL_AtomicTestThenSet32(&val32);
       
   154    printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
       
   155 
    23    SDL_AtomicClear32(&val32);
   156    SDL_AtomicClear32(&val32);
       
   157    printf("Clear32              val=%d\n", val32);
       
   158 
    24    ret32 = SDL_AtomicFetchThenIncrement32(&val32);
   159    ret32 = SDL_AtomicFetchThenIncrement32(&val32);
       
   160    printf("FetchThenIncrement32 ret=%d val=%d\n", ret32, val32);
       
   161 
    25    ret32 = SDL_AtomicFetchThenDecrement32(&val32);
   162    ret32 = SDL_AtomicFetchThenDecrement32(&val32);
       
   163    printf("FetchThenDecrement32 ret=%d val=%d\n", ret32, val32);
       
   164 
    26    ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
   165    ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
       
   166    printf("FetchThenAdd32       ret=%d val=%d\n", ret32, val32);
       
   167 
    27    ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
   168    ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
       
   169    printf("FetchThenSubtract32  ret=%d val=%d\n", ret32, val32);
       
   170 
    28    ret32 = SDL_AtomicIncrementThenFetch32(&val32);
   171    ret32 = SDL_AtomicIncrementThenFetch32(&val32);
       
   172    printf("IncrementThenFetch32 ret=%d val=%d\n", ret32, val32);
       
   173 
    29    ret32 = SDL_AtomicDecrementThenFetch32(&val32);
   174    ret32 = SDL_AtomicDecrementThenFetch32(&val32);
       
   175    printf("DecrementThenFetch32 ret=%d val=%d\n", ret32, val32);
       
   176 
    30    ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
   177    ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
       
   178    printf("AddThenFetch32       ret=%d val=%d\n", ret32, val32);
       
   179 
    31    ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
   180    ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
    32 
   181    printf("SubtractThenFetch32  ret=%d val=%d\n", ret32, val32);
    33 /* #ifdef SDL_HAS_64BIT_TYPE */
   182 
    34 #if 0
   183 #ifdef SDL_HAS_64BIT_TYPE
       
   184    printf("64 bit -----------------------------------------\n\n");
    35 
   185 
    36    ret64 = SDL_AtomicExchange64(&val64, 10);
   186    ret64 = SDL_AtomicExchange64(&val64, 10);
    37    tfval = SDL_AtomicCompareThenSet64(&val64, 10, 20);
   187    printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
    38    tfval = SDL_AtomicTestThenSet64(&val64);
   188    ret64 = SDL_AtomicExchange64(&val64, 0);
       
   189    printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
       
   190 
       
   191    val64 = 10;
       
   192    tfret = SDL_AtomicCompareThenSet64(&val64, 10, 20);
       
   193    printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
       
   194    val64 = 10;
       
   195    tfret = SDL_AtomicCompareThenSet64(&val64, 0, 20);
       
   196    printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
       
   197 
       
   198    val64 = 0;
       
   199    tfret = SDL_AtomicTestThenSet64(&val64);
       
   200    printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
       
   201    tfret = SDL_AtomicTestThenSet64(&val64);
       
   202    printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
       
   203 
    39    SDL_AtomicClear64(&val64);
   204    SDL_AtomicClear64(&val64);
       
   205    printf("Clear64              val=%lld\n", val64);
       
   206 
    40    ret64 = SDL_AtomicFetchThenIncrement64(&val64);
   207    ret64 = SDL_AtomicFetchThenIncrement64(&val64);
       
   208    printf("FetchThenIncrement64 ret=%lld val=%lld\n", ret64, val64);
       
   209 
    41    ret64 = SDL_AtomicFetchThenDecrement64(&val64);
   210    ret64 = SDL_AtomicFetchThenDecrement64(&val64);
       
   211    printf("FetchThenDecrement64 ret=%lld val=%lld\n", ret64, val64);
       
   212 
    42    ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
   213    ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
       
   214    printf("FetchThenAdd64       ret=%lld val=%lld\n", ret64, val64);
       
   215 
    43    ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
   216    ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
       
   217    printf("FetchThenSubtract64  ret=%lld val=%lld\n", ret64, val64);
       
   218 
    44    ret64 = SDL_AtomicIncrementThenFetch64(&val64);
   219    ret64 = SDL_AtomicIncrementThenFetch64(&val64);
       
   220    printf("IncrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
       
   221 
    45    ret64 = SDL_AtomicDecrementThenFetch64(&val64);
   222    ret64 = SDL_AtomicDecrementThenFetch64(&val64);
       
   223    printf("DecrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
       
   224 
    46    ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
   225    ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
       
   226    printf("AddThenFetch64       ret=%lld val=%lld\n", ret64, val64);
       
   227 
    47    ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
   228    ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
       
   229    printf("SubtractThenFetch64  ret=%lld val=%lld\n", ret64, val64);
    48 #endif
   230 #endif
    49 
   231 
    50    return 0;
   232    return 0;
    51    }
   233    }