This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testplatform.c
365 lines (302 loc) · 9.89 KB
1
2
/**
* Original code: automated SDL platform test written by Edgar Simo "bobbens"
3
* Extended and updated by aschiffler at ferzkopp dot net
4
5
6
7
8
9
10
11
12
13
*/
#include <stdio.h>
#include <SDL/SDL.h>
#include "../SDL_test.h"
/* Test cases */
static const TestCaseReference test1 =
14
(TestCaseReference){ "platform_testTypes", "Tests predefined types", TEST_ENABLED, 0, 0 };
15
16
static const TestCaseReference test2 =
17
(TestCaseReference){ "platform_testEndianessAndSwap", "Tests endianess and swap functions", TEST_ENABLED, 0, 0 };
18
19
static const TestCaseReference test3 =
20
(TestCaseReference){ "platform_testGetFunctions", "Tests various SDL_GetXYZ functions", TEST_ENABLED, 0, 0 };
21
22
static const TestCaseReference test4 =
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(TestCaseReference){ "platform_testHasFunctions", "Tests various SDL_HasXYZ functions", TEST_ENABLED, 0, 0 };
static const TestCaseReference test5 =
(TestCaseReference){ "platform_testGetVersion", "Tests SDL_GetVersion function", TEST_ENABLED, 0, 0 };
static const TestCaseReference test6 =
(TestCaseReference){ "platform_testSDLVersion", "Tests SDL_VERSION macro", TEST_ENABLED, 0, 0 };
static const TestCaseReference test7 =
(TestCaseReference){ "platform_testDefaultInit", "Tests default SDL_Init", TEST_ENABLED, 0, 0 };
static const TestCaseReference test8 =
(TestCaseReference){ "platform_testGetSetClearError", "Tests SDL_Get/Set/ClearError", TEST_ENABLED, 0, 0 };
static const TestCaseReference test9 =
(TestCaseReference){ "platform_testSetErrorEmptyInput", "Tests SDL_SetError with empty input", TEST_ENABLED, 0, 0 };
static const TestCaseReference test10 =
(TestCaseReference){ "platform_testSetErrorInvalidInput", "Tests SDL_SetError with invalid input", TEST_ENABLED, 0, 0 };
42
43
44
/* Test suite */
extern const TestCaseReference *testSuite[] = {
45
&test1, &test2, &test3, &test4, &test5, &test6, &test7, &test8, &test9, &test10, NULL
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
};
TestCaseReference **QueryTestSuite() {
return (TestCaseReference **)testSuite;
}
/**
* @brief Compare sizes of types.
*
* @note Watcom C flags these as Warning 201: "Unreachable code" if you just
* compare them directly, so we push it through a function to keep the
* compiler quiet. --ryan.
*/
static int _compareSizeOfType( size_t sizeoftype, size_t hardcodetype )
{
return sizeoftype != hardcodetype;
}
/**
* @brief Tests type sizes.
*/
int platform_testTypes(void *arg)
{
int ret;
ret = _compareSizeOfType( sizeof(Uint8), 1 );
72
AssertTrue( ret == 0, "sizeof(Uint8) = %lu, expected 1", sizeof(Uint8) );
73
74
ret = _compareSizeOfType( sizeof(Uint16), 2 );
75
AssertTrue( ret == 0, "sizeof(Uint16) = %lu, expected 2", sizeof(Uint16) );
76
77
ret = _compareSizeOfType( sizeof(Uint32), 4 );
78
AssertTrue( ret == 0, "sizeof(Uint32) = %lu, expected 4", sizeof(Uint32) );
79
80
ret = _compareSizeOfType( sizeof(Uint64), 8 );
81
AssertTrue( ret == 0, "sizeof(Uint64) = %lu, expected 8", sizeof(Uint64) );
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
}
/**
* @brief Tests platform endianness and SDL_SwapXY functions.
*/
int platform_testEndianessAndSwap(void *arg)
{
int real_byteorder;
Uint16 value = 0x1234;
Uint16 value16 = 0xCDAB;
Uint16 swapped16 = 0xABCD;
Uint32 value32 = 0xEFBEADDE;
Uint32 swapped32 = 0xDEADBEEF;
Uint64 value64, swapped64;
value64 = 0xEFBEADDE;
value64 <<= 32;
value64 |= 0xCDAB3412;
swapped64 = 0x1234ABCD;
swapped64 <<= 32;
swapped64 |= 0xDEADBEEF;
if ((*((char *) &value) >> 4) == 0x1) {
real_byteorder = SDL_BIG_ENDIAN;
} else {
real_byteorder = SDL_LIL_ENDIAN;
}
/* Test endianness. */
AssertTrue( real_byteorder == SDL_BYTEORDER,
112
"Machine detected as %s endian, appears to be %s endian.",
113
114
115
116
117
(SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big",
(real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big" );
/* Test 16 swap. */
AssertTrue( SDL_Swap16(value16) == swapped16,
118
"SDL_Swap16(): 16 bit swapped: 0x%X => 0x%X",
119
120
121
122
value16, SDL_Swap16(value16) );
/* Test 32 swap. */
AssertTrue( SDL_Swap32(value32) == swapped32,
123
"SDL_Swap32(): 32 bit swapped: 0x%X => 0x%X",
124
125
126
127
128
value32, SDL_Swap32(value32) );
/* Test 64 swap. */
AssertTrue( SDL_Swap64(value64) == swapped64,
#ifdef _MSC_VER
129
"SDL_Swap64(): 64 bit swapped: 0x%I64X => 0x%I64X",
130
#else
131
"SDL_Swap64(): 64 bit swapped: 0x%llX => 0x%llX",
132
133
134
135
136
137
#endif
value64, SDL_Swap64(value64) );
}
/*!
* \brief Tests SDL_GetXYZ() functions
138
139
* \sa
* http://wiki.libsdl.org/moin.cgi/SDL_GetPlatform
140
141
142
*/
int platform_testGetFunctions (void *arg)
{
143
144
char *platform;
char *revision;
145
int ret;
146
int len;
147
148
platform = (char *)SDL_GetPlatform();
149
AssertPass("SDL_GetPlatform()");
150
151
152
153
154
155
156
157
158
AssertTrue(platform != NULL, "SDL_GetPlatform() != NULL");
if (platform != NULL) {
len = strlen(platform);
AssertTrue(len > 0,
"SDL_GetPlatform(): expected non-empty platform, was platform: '%s', len: %i",
platform,
len);
}
159
160
ret = SDL_GetCPUCount();
AssertPass("SDL_GetCPUCount()");
161
162
163
164
165
166
167
revision = (char *)SDL_GetRevision();
AssertPass("SDL_GetRevision()");
AssertTrue(revision != NULL, "SDL_GetRevision() != NULL");
ret = SDL_GetRevisionNumber();
AssertPass("SDL_GetRevisionNumber()");
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
}
/*!
* \brief Tests SDL_HasXYZ() functions
*/
int platform_testHasFunctions (void *arg)
{
int ret;
// TODO: independently determine and compare values as well
ret = SDL_HasRDTSC();
AssertPass("SDL_HasRDTSC()");
ret = SDL_HasAltiVec();
AssertPass("SDL_HasAltiVec()");
ret = SDL_HasMMX();
AssertPass("SDL_HasMMX()");
ret = SDL_Has3DNow();
AssertPass("SDL_Has3DNow()");
ret = SDL_HasSSE();
AssertPass("SDL_HasSSE()");
ret = SDL_HasSSE2();
AssertPass("SDL_HasSSE2()");
ret = SDL_HasSSE3();
AssertPass("SDL_HasSSE3()");
ret = SDL_HasSSE41();
AssertPass("SDL_HasSSE41()");
ret = SDL_HasSSE42();
AssertPass("SDL_HasSSE42()");
}
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*!
* \brief Tests SDL_GetVersion
*/
int platform_testGetVersion(void *arg)
{
SDL_version linked;
SDL_GetVersion(&linked);
AssertTrue( linked.major >= SDL_MAJOR_VERSION,
"SDL_GetVersion(): returned major %i (>= %i)",
linked.major,
SDL_MAJOR_VERSION);
AssertTrue( linked.minor >= SDL_MINOR_VERSION,
"SDL_GetVersion(): returned minor %i (>= %i)",
linked.minor,
SDL_MINOR_VERSION);
}
/*!
* \brief Tests SDL_VERSION macro
*/
int platform_testSDLVersion(void *arg)
{
SDL_version compiled;
SDL_VERSION(&compiled);
AssertTrue( compiled.major >= SDL_MAJOR_VERSION,
"SDL_VERSION() returned major %i (>= %i)",
compiled.major,
SDL_MAJOR_VERSION);
AssertTrue( compiled.minor >= SDL_MINOR_VERSION,
"SDL_VERSION() returned minor %i (>= %i)",
compiled.minor,
SDL_MINOR_VERSION);
}
/*!
* \brief Tests default SDL_Init
*/
int platform_testDefaultInit(void *arg)
{
int ret;
int subsystem;
ret = SDL_Init(0);
AssertTrue( ret == 0,
"SDL_Init(0): returned %i, expected 0, error: %s",
ret,
SDL_GetError());
subsystem = SDL_WasInit(0);
AssertTrue( subsystem == 0,
"SDL_WasInit(0): returned %i, expected 0",
ret);
SDL_Quit();
}
/*!
* \brief Tests SDL_Get/Set/ClearError
* \sa
* http://wiki.libsdl.org/moin.cgi/SDL_GetError
* http://wiki.libsdl.org/moin.cgi/SDL_SetError
* http://wiki.libsdl.org/moin.cgi/SDL_ClearError
*/
int platform_testGetSetClearError(void *arg)
{
const char *testError = "Testing";
char *lastError;
int len;
SDL_ClearError();
AssertPass("SDL_ClearError()");
lastError = (char *)SDL_GetError();
AssertPass("SDL_GetError()");
AssertTrue(lastError != NULL,
"SDL_GetError() != NULL");
if (lastError != NULL)
{
len = strlen(lastError);
AssertTrue(len == 0,
"SDL_GetError(): no message expected, len: %i", len);
}
SDL_SetError("%s", testError);
AssertPass("SDL_SetError()");
lastError = (char *)SDL_GetError();
AssertTrue(lastError != NULL,
"SDL_GetError() != NULL");
if (lastError != NULL)
{
len = strlen(lastError);
AssertTrue(len == strlen(testError),
"SDL_GetError(): expected message len %i, was len: %i",
strlen(testError),
len);
AssertTrue(strcmp(lastError, testError) == 0,
"SDL_GetError(): expected message %s, was message: %s",
testError,
lastError);
}
// Clean up
SDL_ClearError();
}
/*!
* \brief Tests SDL_SetError with empty input
* \sa
* http://wiki.libsdl.org/moin.cgi/SDL_SetError
*/
int platform_testSetErrorEmptyInput(void *arg)
{
const char *testError = "";
char *lastError;
int len;
SDL_SetError("%s", testError);
AssertPass("SDL_SetError()");
lastError = (char *)SDL_GetError();
AssertTrue(lastError != NULL,
"SDL_GetError() != NULL");
if (lastError != NULL)
{
len = strlen(lastError);
AssertTrue(len == strlen(testError),
"SDL_GetError(): expected message len %i, was len: %i",
strlen(testError),
len);
AssertTrue(strcmp(lastError, testError) == 0,
"SDL_GetError(): expected message '%s', was message: '%s'",
testError,
lastError);
}
// Clean up
SDL_ClearError();
}
/*!
* \brief Tests SDL_SetError with invalid input
* \sa
* http://wiki.libsdl.org/moin.cgi/SDL_SetError
*/
int platform_testSetErrorInvalidInput(void *arg)
{
const char *testError = NULL;
char *lastError;
SDL_SetError(testError);
AssertPass("SDL_SetError()");
lastError = (char *)SDL_GetError();
AssertTrue(lastError == NULL,
"SDL_GetError() == NULL");
// Clean up
SDL_ClearError();
}