test/testatomic.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Oct 2009 09:55:20 +0000
changeset 3338 9de326b3099c
parent 3327 35387815b155
child 5003 3a95a2b93eb3
permissions -rw-r--r--
Fixed bug #817

Daniele Forghieri 2009-09-30 15:48:24 PDT

Some tests doesn't use the correct include statement (and there are some
missing declaration) and some test use C++ variable after statement, preventing
compile wicth Open Watcom

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