comparison test/testatomic.c @ 3327:35387815b155

Better cross-platform macros for printing 32 and 64 bit values
author Sam Lantinga <slouken@libsdl.org>
date Tue, 29 Sep 2009 01:19:14 +0000
parents bb9c3d04f126
children 9de326b3099c
comparison
equal deleted inserted replaced
3326:11c079bb52a8 3327:35387815b155
1 #include "SDL.h" 1 #include "SDL.h"
2
3 /* Make sure we have good macros for printing 32 and 64 bit values */
4 #ifndef PRIu32
5 #define PRIu32 "u"
6 #endif
7 #ifndef PRIu64
8 #ifdef __WIN32__
9 #define PRIu64 "I64u"
10 #else
11 #define PRIu64 "llu"
12 #endif
13 #endif
2 14
3 /* 15 /*
4 Absolutely basic tests just to see if we get the expected value 16 Absolutely basic tests just to see if we get the expected value
5 after calling each function. 17 after calling each function.
6 */ 18 */
42 54
43 printf("\n32 bit -----------------------------------------\n\n"); 55 printf("\n32 bit -----------------------------------------\n\n");
44 56
45 val32 = 0; 57 val32 = 0;
46 tfret = SDL_AtomicTestThenSet32(&val32); 58 tfret = SDL_AtomicTestThenSet32(&val32);
47 printf("TestThenSet32 tfret=%s val=%d\n", tf(tfret), val32); 59 printf("TestThenSet32 tfret=%s val=%"PRIu32"\n", tf(tfret), val32);
48 tfret = SDL_AtomicTestThenSet32(&val32); 60 tfret = SDL_AtomicTestThenSet32(&val32);
49 printf("TestThenSet32 tfret=%s val=%d\n", tf(tfret), val32); 61 printf("TestThenSet32 tfret=%s val=%"PRIu32"\n", tf(tfret), val32);
50 62
51 SDL_AtomicClear32(&val32); 63 SDL_AtomicClear32(&val32);
52 printf("Clear32 val=%d\n", val32); 64 printf("Clear32 val=%"PRIu32"\n", val32);
53 65
54 ret32 = SDL_AtomicFetchThenIncrement32(&val32); 66 ret32 = SDL_AtomicFetchThenIncrement32(&val32);
55 printf("FetchThenIncrement32 ret=%d val=%d\n", ret32, val32); 67 printf("FetchThenIncrement32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
56 68
57 ret32 = SDL_AtomicFetchThenDecrement32(&val32); 69 ret32 = SDL_AtomicFetchThenDecrement32(&val32);
58 printf("FetchThenDecrement32 ret=%d val=%d\n", ret32, val32); 70 printf("FetchThenDecrement32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
59 71
60 ret32 = SDL_AtomicFetchThenAdd32(&val32, 10); 72 ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
61 printf("FetchThenAdd32 ret=%d val=%d\n", ret32, val32); 73 printf("FetchThenAdd32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
62 74
63 ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10); 75 ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
64 printf("FetchThenSubtract32 ret=%d val=%d\n", ret32, val32); 76 printf("FetchThenSubtract32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
65 77
66 ret32 = SDL_AtomicIncrementThenFetch32(&val32); 78 ret32 = SDL_AtomicIncrementThenFetch32(&val32);
67 printf("IncrementThenFetch32 ret=%d val=%d\n", ret32, val32); 79 printf("IncrementThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
68 80
69 ret32 = SDL_AtomicDecrementThenFetch32(&val32); 81 ret32 = SDL_AtomicDecrementThenFetch32(&val32);
70 printf("DecrementThenFetch32 ret=%d val=%d\n", ret32, val32); 82 printf("DecrementThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
71 83
72 ret32 = SDL_AtomicAddThenFetch32(&val32, 10); 84 ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
73 printf("AddThenFetch32 ret=%d val=%d\n", ret32, val32); 85 printf("AddThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
74 86
75 ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10); 87 ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
76 printf("SubtractThenFetch32 ret=%d val=%d\n", ret32, val32); 88 printf("SubtractThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
77 89
78 #ifdef SDL_HAS_64BIT_TYPE 90 #ifdef SDL_HAS_64BIT_TYPE
79 printf("\n64 bit -----------------------------------------\n\n"); 91 printf("\n64 bit -----------------------------------------\n\n");
80 92
81 val64 = 0; 93 val64 = 0;
82 tfret = SDL_AtomicTestThenSet64(&val64); 94 tfret = SDL_AtomicTestThenSet64(&val64);
83 printf("TestThenSet64 tfret=%s val=%llu\n", tf(tfret), ((unsigned long long)val64)); 95 printf("TestThenSet64 tfret=%s val=%"PRIu64"\n", tf(tfret), val64);
84 tfret = SDL_AtomicTestThenSet64(&val64); 96 tfret = SDL_AtomicTestThenSet64(&val64);
85 printf("TestThenSet64 tfret=%s val=%llu\n", tf(tfret), ((unsigned long long)val64)); 97 printf("TestThenSet64 tfret=%s val=%"PRIu64"\n", tf(tfret), val64);
86 98
87 SDL_AtomicClear64(&val64); 99 SDL_AtomicClear64(&val64);
88 printf("Clear64 val=%llu\n", ((unsigned long long)val64)); 100 printf("Clear64 val=%"PRIu64"\n", val64);
89 101
90 ret64 = SDL_AtomicFetchThenIncrement64(&val64); 102 ret64 = SDL_AtomicFetchThenIncrement64(&val64);
91 printf("FetchThenIncrement64 ret=%llu val=%llu\n", ((unsigned long long)ret64), ((unsigned long long)val64)); 103 printf("FetchThenIncrement64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
92 104
93 ret64 = SDL_AtomicFetchThenDecrement64(&val64); 105 ret64 = SDL_AtomicFetchThenDecrement64(&val64);
94 printf("FetchThenDecrement64 ret=%llu val=%llu\n", ((unsigned long long)ret64), ((unsigned long long)val64)); 106 printf("FetchThenDecrement64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
95 107
96 ret64 = SDL_AtomicFetchThenAdd64(&val64, 10); 108 ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
97 printf("FetchThenAdd64 ret=%llu val=%llu\n", ((unsigned long long)ret64), ((unsigned long long)val64)); 109 printf("FetchThenAdd64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
98 110
99 ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10); 111 ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
100 printf("FetchThenSubtract64 ret=%llu val=%llu\n", ((unsigned long long)ret64), ((unsigned long long)val64)); 112 printf("FetchThenSubtract64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
101 113
102 ret64 = SDL_AtomicIncrementThenFetch64(&val64); 114 ret64 = SDL_AtomicIncrementThenFetch64(&val64);
103 printf("IncrementThenFetch64 ret=%llu val=%llu\n", ((unsigned long long)ret64), ((unsigned long long)val64)); 115 printf("IncrementThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
104 116
105 ret64 = SDL_AtomicDecrementThenFetch64(&val64); 117 ret64 = SDL_AtomicDecrementThenFetch64(&val64);
106 printf("DecrementThenFetch64 ret=%llu val=%llu\n", ((unsigned long long)ret64), ((unsigned long long)val64)); 118 printf("DecrementThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
107 119
108 ret64 = SDL_AtomicAddThenFetch64(&val64, 10); 120 ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
109 printf("AddThenFetch64 ret=%llu val=%llu\n", ((unsigned long long)ret64), ((unsigned long long)val64)); 121 printf("AddThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
110 122
111 ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10); 123 ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
112 printf("SubtractThenFetch64 ret=%llu val=%llu\n", ((unsigned long long)ret64), ((unsigned long long)val64)); 124 printf("SubtractThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
113 #endif 125 #endif
114 126
115 return 0; 127 return 0;
116 } 128 }