2
* Properties test suite
6
#include <SDL3/SDL_test.h>
7
#include "testautomation_suites.h"
9
/* Test case functions */
12
* Test basic functionality
14
static void SDLCALL count_properties(void *userdata, SDL_PropertiesID props, const char *name)
16
int *count = (int *)userdata;
19
static void SDLCALL count_foo_properties(void *userdata, SDL_PropertiesID props, const char *name)
21
int *count = (int *)userdata;
22
if (SDL_strcmp(name, "foo") == 0) {
26
static int properties_testBasic(void *arg)
28
SDL_PropertiesID props;
29
char key[2], expected_value[2];
30
SDL_PropertyType type;
32
const char *value_string;
38
props = SDL_CreateProperties();
39
SDLTest_AssertPass("Call to SDL_CreateProperties()");
40
SDLTest_AssertCheck(props != 0,
41
"Verify props were created, got: %" SDL_PRIu32, props);
43
for (i = 0; i < 10; ++i) {
44
SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
45
SDL_snprintf(expected_value, SDL_arraysize(expected_value), "%c", 'a' + i);
46
result = SDL_SetPointerProperty(props, key, expected_value);
47
SDLTest_AssertPass("Call to SDL_SetPointerProperty()");
48
SDLTest_AssertCheck(result == 0,
49
"Verify property value was set, got: %d", result);
50
value = SDL_GetPointerProperty(props, key, NULL);
51
SDLTest_AssertPass("Call to SDL_GetPointerProperty()");
52
SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, expected_value) == 0,
53
"Verify property value was set, got %s, expected %s", value ? (const char *)value : "NULL", expected_value);
57
SDL_EnumerateProperties(props, count_properties, &count);
58
SDLTest_AssertCheck(count == 10,
59
"Verify property count, expected 10, got: %d", count);
61
for (i = 0; i < 10; ++i) {
62
SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
63
result = SDL_SetPointerProperty(props, key, NULL);
64
SDLTest_AssertPass("Call to SDL_SetPointerProperty(NULL)");
65
SDLTest_AssertCheck(result == 0,
66
"Verify property value was set, got: %d", result);
67
value = SDL_GetPointerProperty(props, key, NULL);
68
SDLTest_AssertPass("Call to SDL_GetPointerProperty()");
69
SDLTest_AssertCheck(value == NULL,
70
"Verify property value was set, got %s, expected NULL", (const char *)value);
74
SDL_EnumerateProperties(props, count_properties, &count);
75
SDLTest_AssertCheck(count == 0,
76
"Verify property count, expected 0, got: %d", count);
78
/* Check default values */
79
value = SDL_GetPointerProperty(props, "foo", (void *)0xabcd);
80
SDLTest_AssertCheck(value == (void *)0xabcd,
81
"Verify property, expected 0xabcd, got: %p", value);
82
value_string = SDL_GetStringProperty(props, "foo", "abcd");
83
SDLTest_AssertCheck(value_string && SDL_strcmp(value_string, "abcd") == 0,
84
"Verify string property, expected abcd, got: %s", value_string);
85
value_number = SDL_GetNumberProperty(props, "foo", 1234);
86
SDLTest_AssertCheck(value_number == 1234,
87
"Verify number property, expected 1234, got: %" SDL_PRIu64, value_number);
88
value_float = SDL_GetFloatProperty(props, "foo", 1234.0f);
89
SDLTest_AssertCheck(value_float == 1234.0f,
90
"Verify float property, expected 1234, got: %f", value_float);
91
value_bool = SDL_GetBooleanProperty(props, "foo", SDL_TRUE);
92
SDLTest_AssertCheck(value_bool == SDL_TRUE,
93
"Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
95
/* Check data value */
96
SDLTest_AssertPass("Call to SDL_SetPointerProperty(\"foo\", 0x01)");
97
SDL_SetPointerProperty(props, "foo", (void *)0x01);
98
type = SDL_GetPropertyType(props, "foo");
99
SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_POINTER,
100
"Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_POINTER, type);
101
value = SDL_GetPointerProperty(props, "foo", NULL);
102
SDLTest_AssertCheck(value == (void *)0x01,
103
"Verify property, expected 0x01, got: %p", value);
104
value_string = SDL_GetStringProperty(props, "foo", NULL);
105
SDLTest_AssertCheck(value_string == NULL,
106
"Verify string property, expected NULL, got: %s", value_string);
107
value_number = SDL_GetNumberProperty(props, "foo", 0);
108
SDLTest_AssertCheck(value_number == 0,
109
"Verify number property, expected 0, got: %" SDL_PRIu64, value_number);
110
value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
111
SDLTest_AssertCheck(value_float == 0.0f,
112
"Verify float property, expected 0, got: %f", value_float);
113
value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
114
SDLTest_AssertCheck(value_bool == SDL_FALSE,
115
"Verify boolean property, expected SDL_FALSE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
117
/* Check string value */
118
SDLTest_AssertPass("Call to SDL_SetStringProperty(\"foo\", \"bar\")");
119
SDL_SetStringProperty(props, "foo", "bar");
120
type = SDL_GetPropertyType(props, "foo");
121
SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_STRING,
122
"Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_STRING, type);
123
value = SDL_GetPointerProperty(props, "foo", NULL);
124
SDLTest_AssertCheck(value == NULL,
125
"Verify property, expected NULL, got: %p", value);
126
value_string = SDL_GetStringProperty(props, "foo", NULL);
127
SDLTest_AssertCheck(value_string != NULL && SDL_strcmp(value_string, "bar") == 0,
128
"Verify string property, expected bar, got: %s", value_string);
129
value_number = SDL_GetNumberProperty(props, "foo", 0);
130
SDLTest_AssertCheck(value_number == 0,
131
"Verify number property, expected 0, got: %" SDL_PRIu64, value_number);
132
value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
133
SDLTest_AssertCheck(value_float == 0.0f,
134
"Verify float property, expected 0, got: %f", value_float);
135
value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
136
SDLTest_AssertCheck(value_bool == SDL_TRUE,
137
"Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
139
/* Check number value */
140
SDLTest_AssertPass("Call to SDL_SetNumberProperty(\"foo\", 1)");
141
SDL_SetNumberProperty(props, "foo", 1);
142
type = SDL_GetPropertyType(props, "foo");
143
SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_NUMBER,
144
"Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_NUMBER, type);
145
value = SDL_GetPointerProperty(props, "foo", NULL);
146
SDLTest_AssertCheck(value == NULL,
147
"Verify property, expected NULL, got: %p", value);
148
value_string = SDL_GetStringProperty(props, "foo", NULL);
149
SDLTest_AssertCheck(value_string && SDL_strcmp(value_string, "1") == 0,
150
"Verify string property, expected 1, got: %s", value_string);
151
value_number = SDL_GetNumberProperty(props, "foo", 0);
152
SDLTest_AssertCheck(value_number == 1,
153
"Verify number property, expected 1, got: %" SDL_PRIu64, value_number);
154
value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
155
SDLTest_AssertCheck(value_float == 1.0f,
156
"Verify float property, expected 1, got: %f", value_float);
157
value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
158
SDLTest_AssertCheck(value_bool == SDL_TRUE,
159
"Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
161
/* Check float value */
162
SDLTest_AssertPass("Call to SDL_SetFloatProperty(\"foo\", 1)");
163
SDL_SetFloatProperty(props, "foo", 1.75f);
164
type = SDL_GetPropertyType(props, "foo");
165
SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_FLOAT,
166
"Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_FLOAT, type);
167
value = SDL_GetPointerProperty(props, "foo", NULL);
168
SDLTest_AssertCheck(value == NULL,
169
"Verify property, expected NULL, got: %p", value);
170
value_string = SDL_GetStringProperty(props, "foo", NULL);
171
SDLTest_AssertCheck(value_string && SDL_strcmp(value_string, "1.750000") == 0,
172
"Verify string property, expected 1.750000, got: %s", value_string);
173
value_number = SDL_GetNumberProperty(props, "foo", 0);
174
SDLTest_AssertCheck(value_number == 2,
175
"Verify number property, expected 2, got: %" SDL_PRIu64, value_number);
176
value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
177
SDLTest_AssertCheck(value_float == 1.75f,
178
"Verify float property, expected 1.75, got: %f", value_float);
179
value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
180
SDLTest_AssertCheck(value_bool == SDL_TRUE,
181
"Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
183
/* Check boolean value */
184
SDLTest_AssertPass("Call to SDL_SetBooleanProperty(\"foo\", SDL_TRUE)");
185
SDL_SetBooleanProperty(props, "foo", 3); /* Note we're testing non-true/false value here */
186
type = SDL_GetPropertyType(props, "foo");
187
SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_BOOLEAN,
188
"Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_BOOLEAN, type);
189
value = SDL_GetPointerProperty(props, "foo", NULL);
190
SDLTest_AssertCheck(value == NULL,
191
"Verify property, expected NULL, got: %p", value);
192
value_string = SDL_GetStringProperty(props, "foo", NULL);
193
SDLTest_AssertCheck(value_string && SDL_strcmp(value_string, "true") == 0,
194
"Verify string property, expected true, got: %s", value_string);
195
value_number = SDL_GetNumberProperty(props, "foo", 0);
196
SDLTest_AssertCheck(value_number == 1,
197
"Verify number property, expected 1, got: %" SDL_PRIu64, value_number);
198
value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
199
SDLTest_AssertCheck(value_float == 1.0f,
200
"Verify float property, expected 1, got: %f", value_float);
201
value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
202
SDLTest_AssertCheck(value_bool == SDL_TRUE,
203
"Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
205
/* Make sure we have exactly one property named foo */
207
SDL_EnumerateProperties(props, count_foo_properties, &count);
208
SDLTest_AssertCheck(count == 1,
209
"Verify foo property count, expected 1, got: %d", count);
211
SDL_DestroyProperties(props);
213
return TEST_COMPLETED;
217
* Test copy functionality
219
static void SDLCALL copy_cleanup(void *userdata, void *value)
222
static int properties_testCopy(void *arg)
224
SDL_PropertiesID a, b;
230
a = SDL_CreateProperties();
231
SDL_SetNumberProperty(a, "num", 1);
232
SDL_SetStringProperty(a, "string", "foo");
233
SDL_SetPointerProperty(a, "data", &a);
234
SDL_SetPointerPropertyWithCleanup(a, "cleanup", &a, copy_cleanup, &a);
236
b = SDL_CreateProperties();
237
SDL_SetNumberProperty(b, "num", 2);
239
SDLTest_AssertPass("Call to SDL_CopyProperties(a, 0)");
240
result = SDL_CopyProperties(a, 0);
241
SDLTest_AssertCheck(result == -1,
242
"SDL_CopyProperties() result, got %d, expected -1", result);
244
SDLTest_AssertPass("Call to SDL_CopyProperties(0, b)");
245
result = SDL_CopyProperties(0, b);
246
SDLTest_AssertCheck(result == -1,
247
"SDL_CopyProperties() result, got %d, expected -1", result);
249
SDLTest_AssertPass("Call to SDL_CopyProperties(a, b)");
250
result = SDL_CopyProperties(a, b);
251
SDLTest_AssertCheck(result == 0,
252
"SDL_CopyProperties() result, got %d, expected 0", result);
254
SDL_DestroyProperties(a);
256
num = (int)SDL_GetNumberProperty(b, "num", 0);
257
SDLTest_AssertCheck(num == 1,
258
"Checking number property, got %d, expected 1", num);
260
string = SDL_GetStringProperty(b, "string", NULL);
261
SDLTest_AssertCheck(string && SDL_strcmp(string, "foo") == 0,
262
"Checking string property, got \"%s\", expected \"foo\"", string);
264
data = SDL_GetPointerProperty(b, "data", NULL);
265
SDLTest_AssertCheck(data == &a,
266
"Checking data property, got %p, expected %p", data, &a);
268
data = SDL_GetPointerProperty(b, "cleanup", NULL);
269
SDLTest_AssertCheck(data == NULL,
270
"Checking cleanup property, got %p, expected NULL", data);
272
SDL_DestroyProperties(b);
274
return TEST_COMPLETED;
278
* Test cleanup functionality
280
static void SDLCALL cleanup(void *userdata, void *value)
282
int *count = (int *)userdata;
285
static int properties_testCleanup(void *arg)
287
SDL_PropertiesID props;
288
char key[2], expected_value[2];
291
props = SDL_CreateProperties();
293
SDLTest_AssertPass("Call to SDL_SetPointerProperty(cleanup)");
295
SDL_SetPointerPropertyWithCleanup(props, "a", "0", cleanup, &count);
296
SDL_ClearProperty(props, "a");
297
SDLTest_AssertCheck(count == 1,
298
"Verify cleanup for deleting property, got %d, expected 1", count);
300
SDLTest_AssertPass("Call to SDL_DestroyProperties()");
302
for (i = 0; i < 10; ++i) {
303
SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
304
SDL_snprintf(expected_value, SDL_arraysize(expected_value), "%c", 'a' + i);
305
SDL_SetPointerPropertyWithCleanup(props, key, expected_value, cleanup, &count);
307
SDL_DestroyProperties(props);
308
SDLTest_AssertCheck(count == 10,
309
"Verify cleanup for destroying properties, got %d, expected 10", count);
311
return TEST_COMPLETED;
315
* Test locking functionality
317
struct properties_thread_data
320
SDL_PropertiesID props;
322
static int properties_thread(void *arg)
324
struct properties_thread_data *data = (struct properties_thread_data *)arg;
326
while (!data->done) {
327
SDL_LockProperties(data->props);
328
SDL_SetPointerProperty(data->props, "a", "thread_loop");
329
SDL_UnlockProperties(data->props);
331
SDL_LockProperties(data->props);
332
SDL_SetPointerProperty(data->props, "a", "thread_done");
333
SDL_UnlockProperties(data->props);
336
static int properties_testLocking(void *arg)
338
struct properties_thread_data data;
342
SDLTest_AssertPass("Testing property locking");
343
data.done = SDL_FALSE;
344
data.props = SDL_CreateProperties();
345
SDLTest_AssertPass("Setting property to 'init'");
346
SDL_SetPointerProperty(data.props, "a", "init");
347
thread = SDL_CreateThread(properties_thread, "properties_thread", &data);
349
SDLTest_AssertPass("Waiting for property to change to 'thread_loop'");
353
SDL_LockProperties(data.props);
354
value = SDL_GetPointerProperty(data.props, "a", NULL);
355
SDL_UnlockProperties(data.props);
357
if (!value || SDL_strcmp((const char *)value, "thread_loop") == 0) {
361
SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "thread_loop") == 0,
362
"After thread loop, property is %s, expected 'thread_loop'", value ? (const char *)value : "NULL");
364
SDLTest_AssertPass("Setting property to 'main'");
365
SDL_LockProperties(data.props);
366
SDL_SetPointerProperty(data.props, "a", "main");
368
value = SDL_GetPointerProperty(data.props, "a", NULL);
369
SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "main") == 0,
370
"After 100ms sleep, property is %s, expected 'main'", value ? (const char *)value : "NULL");
371
SDL_UnlockProperties(data.props);
373
data.done = SDL_TRUE;
374
SDL_WaitThread(thread, NULL);
376
value = SDL_GetPointerProperty(data.props, "a", NULL);
377
SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "thread_done") == 0,
378
"After thread complete, property is %s, expected 'thread_done'", value ? (const char *)value : "NULL");
380
SDL_DestroyProperties(data.props);
382
return TEST_COMPLETED;
385
/* ================= Test References ================== */
387
/* Properties test cases */
388
static const SDLTest_TestCaseReference propertiesTestBasic = {
389
(SDLTest_TestCaseFp)properties_testBasic, "properties_testBasic", "Test basic property functionality", TEST_ENABLED
392
static const SDLTest_TestCaseReference propertiesTestCopy = {
393
(SDLTest_TestCaseFp)properties_testCopy, "properties_testCopy", "Test property copy functionality", TEST_ENABLED
396
static const SDLTest_TestCaseReference propertiesTestCleanup = {
397
(SDLTest_TestCaseFp)properties_testCleanup, "properties_testCleanup", "Test property cleanup functionality", TEST_ENABLED
400
static const SDLTest_TestCaseReference propertiesTestLocking = {
401
(SDLTest_TestCaseFp)properties_testLocking, "properties_testLocking", "Test property locking functionality", TEST_ENABLED
404
/* Sequence of Properties test cases */
405
static const SDLTest_TestCaseReference *propertiesTests[] = {
406
&propertiesTestBasic,
408
&propertiesTestCleanup,
409
&propertiesTestLocking,
413
/* Properties test suite (global) */
414
SDLTest_TestSuiteReference propertiesTestSuite = {