test/testatomic.c
changeset 7639 9406b7dd2f2d
parent 7538 1d187795e103
child 8149 681eb46b8ac4
     1.1 --- a/test/testatomic.c	Mon Aug 12 22:00:21 2013 -0700
     1.2 +++ b/test/testatomic.c	Wed Aug 14 23:30:10 2013 -0700
     1.3 @@ -45,39 +45,39 @@
     1.4      SDL_atomic_t v;
     1.5      SDL_bool tfret = SDL_FALSE;
     1.6  
     1.7 -    printf("\nspin lock---------------------------------------\n\n");
     1.8 +    SDL_Log("\nspin lock---------------------------------------\n\n");
     1.9  
    1.10      SDL_AtomicLock(&lock);
    1.11 -    printf("AtomicLock                   lock=%d\n", lock);
    1.12 +    SDL_Log("AtomicLock                   lock=%d\n", lock);
    1.13      SDL_AtomicUnlock(&lock);
    1.14 -    printf("AtomicUnlock                 lock=%d\n", lock);
    1.15 +    SDL_Log("AtomicUnlock                 lock=%d\n", lock);
    1.16  
    1.17 -    printf("\natomic -----------------------------------------\n\n");
    1.18 +    SDL_Log("\natomic -----------------------------------------\n\n");
    1.19  
    1.20      SDL_AtomicSet(&v, 0);
    1.21 -    tfret = SDL_AtomicSet(&v, 10) == 0;
    1.22 -    printf("AtomicSet(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.23 -    tfret = SDL_AtomicAdd(&v, 10) == 10;
    1.24 -    printf("AtomicAdd(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.25 +    tfret = SDL_AtomicSet(&v, 10) == 0 ? SDL_TRUE : SDL_FALSE;
    1.26 +    SDL_Log("AtomicSet(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.27 +    tfret = SDL_AtomicAdd(&v, 10) == 10 ? SDL_TRUE : SDL_FALSE;
    1.28 +    SDL_Log("AtomicAdd(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.29  
    1.30      SDL_AtomicSet(&v, 0);
    1.31      SDL_AtomicIncRef(&v);
    1.32 -    tfret = (SDL_AtomicGet(&v) == 1);
    1.33 -    printf("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.34 +    tfret = (SDL_AtomicGet(&v) == 1) ? SDL_TRUE : SDL_FALSE;
    1.35 +    SDL_Log("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.36      SDL_AtomicIncRef(&v);
    1.37 -    tfret = (SDL_AtomicGet(&v) == 2);
    1.38 -    printf("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.39 -    tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE);
    1.40 -    printf("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.41 -    tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE);
    1.42 -    printf("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.43 +    tfret = (SDL_AtomicGet(&v) == 2) ? SDL_TRUE : SDL_FALSE;
    1.44 +    SDL_Log("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.45 +    tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE;
    1.46 +    SDL_Log("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.47 +    tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE;
    1.48 +    SDL_Log("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.49  
    1.50      SDL_AtomicSet(&v, 10);
    1.51 -    tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE);
    1.52 -    printf("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.53 +    tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE;
    1.54 +    SDL_Log("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.55      value = SDL_AtomicGet(&v);
    1.56 -    tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE);
    1.57 -    printf("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.58 +    tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE;
    1.59 +    SDL_Log("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.60  }
    1.61  
    1.62  /**************************************************************************/
    1.63 @@ -120,7 +120,7 @@
    1.64  int adder(void* junk)
    1.65  {
    1.66      unsigned long N=NInter;
    1.67 -    printf("Thread subtracting %d %lu times\n",CountInc,N);
    1.68 +    SDL_Log("Thread subtracting %d %lu times\n",CountInc,N);
    1.69      while (N--) {
    1.70          SDL_AtomicAdd(&good, -CountInc);
    1.71          bad-=CountInc;
    1.72 @@ -152,7 +152,7 @@
    1.73  
    1.74      end = SDL_GetTicks();
    1.75  
    1.76 -    printf("Finished in %f sec\n", (end - start) / 1000.f);
    1.77 +    SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
    1.78  }
    1.79  
    1.80  static
    1.81 @@ -161,28 +161,28 @@
    1.82      int b;
    1.83      atomicValue v;
    1.84  
    1.85 -    printf("\nepic test---------------------------------------\n\n");
    1.86 +    SDL_Log("\nepic test---------------------------------------\n\n");
    1.87  
    1.88 -    printf("Size asserted to be >= 32-bit\n");
    1.89 +    SDL_Log("Size asserted to be >= 32-bit\n");
    1.90      SDL_assert(sizeof(atomicValue)>=4);
    1.91  
    1.92 -    printf("Check static initializer\n");
    1.93 +    SDL_Log("Check static initializer\n");
    1.94      v=SDL_AtomicGet(&good);
    1.95      SDL_assert(v==42);
    1.96  
    1.97      SDL_assert(bad==42);
    1.98  
    1.99 -    printf("Test negative values\n");
   1.100 +    SDL_Log("Test negative values\n");
   1.101      SDL_AtomicSet(&good, -5);
   1.102      v=SDL_AtomicGet(&good);
   1.103      SDL_assert(v==-5);
   1.104  
   1.105 -    printf("Verify maximum value\n");
   1.106 +    SDL_Log("Verify maximum value\n");
   1.107      SDL_AtomicSet(&good, CountTo);
   1.108      v=SDL_AtomicGet(&good);
   1.109      SDL_assert(v==CountTo);
   1.110  
   1.111 -    printf("Test compare and exchange\n");
   1.112 +    SDL_Log("Test compare and exchange\n");
   1.113  
   1.114      b=SDL_AtomicCAS(&good, 500, 43);
   1.115      SDL_assert(!b); /* no swap since CountTo!=500 */
   1.116 @@ -194,7 +194,7 @@
   1.117      v=SDL_AtomicGet(&good);
   1.118      SDL_assert(v==44);
   1.119  
   1.120 -    printf("Test Add\n");
   1.121 +    SDL_Log("Test Add\n");
   1.122  
   1.123      v=SDL_AtomicAdd(&good, 1);
   1.124      SDL_assert(v==44);
   1.125 @@ -206,7 +206,7 @@
   1.126      v=SDL_AtomicGet(&good);
   1.127      SDL_assert(v==55);
   1.128  
   1.129 -    printf("Test Add (Negative values)\n");
   1.130 +    SDL_Log("Test Add (Negative values)\n");
   1.131  
   1.132      v=SDL_AtomicAdd(&good, -20);
   1.133      SDL_assert(v==55);
   1.134 @@ -223,7 +223,7 @@
   1.135      v=SDL_AtomicGet(&good);
   1.136      SDL_assert(v==15);
   1.137  
   1.138 -    printf("Reset before count down test\n");
   1.139 +    SDL_Log("Reset before count down test\n");
   1.140      SDL_AtomicSet(&good, CountTo);
   1.141      v=SDL_AtomicGet(&good);
   1.142      SDL_assert(v==CountTo);
   1.143 @@ -231,11 +231,11 @@
   1.144      bad=CountTo;
   1.145      SDL_assert(bad==CountTo);
   1.146  
   1.147 -    printf("Counting down from %d, Expect %d remaining\n",CountTo,Expect);
   1.148 +    SDL_Log("Counting down from %d, Expect %d remaining\n",CountTo,Expect);
   1.149      runAdder();
   1.150  
   1.151      v=SDL_AtomicGet(&good);
   1.152 -    printf("Atomic %d Non-Atomic %d\n",v,bad);
   1.153 +    SDL_Log("Atomic %d Non-Atomic %d\n",v,bad);
   1.154      SDL_assert(v==Expect);
   1.155      SDL_assert(bad!=Expect);
   1.156  }
   1.157 @@ -429,7 +429,7 @@
   1.158      } else if (delta < 0) {
   1.159          /* We ran into an old queue entry, which means it still needs to be dequeued */
   1.160      } else {
   1.161 -        printf("ERROR: mutex failed!\n");
   1.162 +        SDL_Log("ERROR: mutex failed!\n");
   1.163      }
   1.164  
   1.165      SDL_UnlockMutex(queue->mutex);
   1.166 @@ -462,7 +462,7 @@
   1.167      } else if (delta < 0) {
   1.168          /* We ran into an old queue entry, which means we've hit empty */
   1.169      } else {
   1.170 -        printf("ERROR: mutex failed!\n");
   1.171 +        SDL_Log("ERROR: mutex failed!\n");
   1.172      }
   1.173  
   1.174      SDL_UnlockMutex(queue->mutex);
   1.175 @@ -598,9 +598,11 @@
   1.176      Uint32 start, end;
   1.177      int i, j;
   1.178      int grand_total;
   1.179 +	char textBuffer[1024];
   1.180 +	int len;
   1.181  
   1.182 -    printf("\nFIFO test---------------------------------------\n\n");
   1.183 -    printf("Mode: %s\n", lock_free ? "LockFree" : "Mutex");
   1.184 +    SDL_Log("\nFIFO test---------------------------------------\n\n");
   1.185 +    SDL_Log("Mode: %s\n", lock_free ? "LockFree" : "Mutex");
   1.186  
   1.187      readersDone = SDL_CreateSemaphore(0);
   1.188      writersDone = SDL_CreateSemaphore(0);
   1.189 @@ -622,7 +624,7 @@
   1.190  #endif
   1.191  
   1.192      /* Start the readers first */
   1.193 -    printf("Starting %d readers\n", NUM_READERS);
   1.194 +    SDL_Log("Starting %d readers\n", NUM_READERS);
   1.195      SDL_zero(readerData);
   1.196      SDL_AtomicSet(&readersRunning, NUM_READERS);
   1.197      for (i = 0; i < NUM_READERS; ++i) {
   1.198 @@ -634,7 +636,7 @@
   1.199      }
   1.200  
   1.201      /* Start up the writers */
   1.202 -    printf("Starting %d writers\n", NUM_WRITERS);
   1.203 +    SDL_Log("Starting %d writers\n", NUM_WRITERS);
   1.204      SDL_zero(writerData);
   1.205      SDL_AtomicSet(&writersRunning, NUM_WRITERS);
   1.206      for (i = 0; i < NUM_WRITERS; ++i) {
   1.207 @@ -668,16 +670,16 @@
   1.208          SDL_DestroyMutex(queue.mutex);
   1.209      }
   1.210  
   1.211 -    printf("Finished in %f sec\n", (end - start) / 1000.f);
   1.212 +    SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
   1.213  
   1.214 -    printf("\n");
   1.215 +    SDL_Log("\n");
   1.216      for (i = 0; i < NUM_WRITERS; ++i) {
   1.217 -        printf("Writer %d wrote %d events, had %d waits\n", i, EVENTS_PER_WRITER, writerData[i].waits);
   1.218 +        SDL_Log("Writer %d wrote %d events, had %d waits\n", i, EVENTS_PER_WRITER, writerData[i].waits);
   1.219      }
   1.220 -    printf("Writers wrote %d total events\n", NUM_WRITERS*EVENTS_PER_WRITER);
   1.221 +    SDL_Log("Writers wrote %d total events\n", NUM_WRITERS*EVENTS_PER_WRITER);
   1.222  
   1.223      /* Print a breakdown of which readers read messages from which writer */
   1.224 -    printf("\n");
   1.225 +    SDL_Log("\n");
   1.226      grand_total = 0;
   1.227      for (i = 0; i < NUM_READERS; ++i) {
   1.228          int total = 0;
   1.229 @@ -685,17 +687,21 @@
   1.230              total += readerData[i].counters[j];
   1.231          }
   1.232          grand_total += total;
   1.233 -        printf("Reader %d read %d events, had %d waits\n", i, total, readerData[i].waits);
   1.234 -        printf("  { ");
   1.235 +        SDL_Log("Reader %d read %d events, had %d waits\n", i, total, readerData[i].waits);
   1.236 +		SDL_snprintf(textBuffer, sizeof(textBuffer), "  { ");
   1.237          for (j = 0; j < NUM_WRITERS; ++j) {
   1.238              if (j > 0) {
   1.239 -                printf(", ");
   1.240 +				len = SDL_strlen(textBuffer);
   1.241 +                SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, ", ");
   1.242              }
   1.243 -            printf("%d", readerData[i].counters[j]);
   1.244 +            len = SDL_strlen(textBuffer);
   1.245 +            SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, "%d", readerData[i].counters[j]);
   1.246          }
   1.247 -        printf(" }\n");
   1.248 +        len = SDL_strlen(textBuffer);
   1.249 +        SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, " }\n");
   1.250 +		SDL_Log(textBuffer);
   1.251      }
   1.252 -    printf("Readers read %d total events\n", grand_total);
   1.253 +    SDL_Log("Readers read %d total events\n", grand_total);
   1.254  }
   1.255  
   1.256  /* End FIFO test */
   1.257 @@ -704,6 +710,9 @@
   1.258  int
   1.259  main(int argc, char *argv[])
   1.260  {
   1.261 +	/* Enable standard application logging */
   1.262 +	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   1.263 +
   1.264      RunBasicTest();
   1.265      RunEpicTest();
   1.266  /* This test is really slow, so don't run it by default */