SDL

Форк
0
/
testautomation_properties.c 
419 строк · 17.1 Кб
1
/**
2
 * Properties test suite
3
 */
4

5
#include <SDL3/SDL.h>
6
#include <SDL3/SDL_test.h>
7
#include "testautomation_suites.h"
8

9
/* Test case functions */
10

11
/**
12
 * Test basic functionality
13
 */
14
static void SDLCALL count_properties(void *userdata, SDL_PropertiesID props, const char *name)
15
{
16
    int *count = (int *)userdata;
17
    ++(*count);
18
}
19
static void SDLCALL count_foo_properties(void *userdata, SDL_PropertiesID props, const char *name)
20
{
21
    int *count = (int *)userdata;
22
    if (SDL_strcmp(name, "foo") == 0) {
23
        ++(*count);
24
    }
25
}
26
static int properties_testBasic(void *arg)
27
{
28
    SDL_PropertiesID props;
29
    char key[2], expected_value[2];
30
    SDL_PropertyType type;
31
    void *value;
32
    const char *value_string;
33
    Sint64 value_number;
34
    float value_float;
35
    SDL_bool value_bool;
36
    int i, result, count;
37

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);
42

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);
54
    }
55

56
    count = 0;
57
    SDL_EnumerateProperties(props, count_properties, &count);
58
    SDLTest_AssertCheck(count == 10,
59
            "Verify property count, expected 10, got: %d", count);
60

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);
71
    }
72

73
    count = 0;
74
    SDL_EnumerateProperties(props, count_properties, &count);
75
    SDLTest_AssertCheck(count == 0,
76
            "Verify property count, expected 0, got: %d", count);
77

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");
94

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");
116

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");
138

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");
160

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");
182

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");
204

205
    /* Make sure we have exactly one property named foo */
206
    count = 0;
207
    SDL_EnumerateProperties(props, count_foo_properties, &count);
208
    SDLTest_AssertCheck(count == 1,
209
            "Verify foo property count, expected 1, got: %d", count);
210

211
    SDL_DestroyProperties(props);
212

213
    return TEST_COMPLETED;
214
}
215

216
/**
217
 * Test copy functionality
218
 */
219
static void SDLCALL copy_cleanup(void *userdata, void *value)
220
{
221
}
222
static int properties_testCopy(void *arg)
223
{
224
    SDL_PropertiesID a, b;
225
    int num;
226
    const char *string;
227
    void *data;
228
    int result;
229

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);
235

236
    b = SDL_CreateProperties();
237
    SDL_SetNumberProperty(b, "num", 2);
238

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);
243

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);
248

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);
253

254
    SDL_DestroyProperties(a);
255

256
    num = (int)SDL_GetNumberProperty(b, "num", 0);
257
    SDLTest_AssertCheck(num == 1,
258
        "Checking number property, got %d, expected 1", num);
259

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);
263

264
    data = SDL_GetPointerProperty(b, "data", NULL);
265
    SDLTest_AssertCheck(data == &a,
266
        "Checking data property, got %p, expected %p", data, &a);
267

268
    data = SDL_GetPointerProperty(b, "cleanup", NULL);
269
    SDLTest_AssertCheck(data == NULL,
270
        "Checking cleanup property, got %p, expected NULL", data);
271

272
    SDL_DestroyProperties(b);
273

274
    return TEST_COMPLETED;
275
}
276

277
/**
278
 * Test cleanup functionality
279
 */
280
static void SDLCALL cleanup(void *userdata, void *value)
281
{
282
    int *count = (int *)userdata;
283
    ++(*count);
284
}
285
static int properties_testCleanup(void *arg)
286
{
287
    SDL_PropertiesID props;
288
    char key[2], expected_value[2];
289
    int i, count;
290

291
    props = SDL_CreateProperties();
292

293
    SDLTest_AssertPass("Call to SDL_SetPointerProperty(cleanup)");
294
    count = 0;
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);
299

300
    SDLTest_AssertPass("Call to SDL_DestroyProperties()");
301
    count = 0;
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);
306
    }
307
    SDL_DestroyProperties(props);
308
    SDLTest_AssertCheck(count == 10,
309
        "Verify cleanup for destroying properties, got %d, expected 10", count);
310

311
    return TEST_COMPLETED;
312
}
313

314
/**
315
 * Test locking functionality
316
 */
317
struct properties_thread_data
318
{
319
    SDL_bool done;
320
    SDL_PropertiesID props;
321
};
322
static int properties_thread(void *arg)
323
{
324
    struct properties_thread_data *data = (struct properties_thread_data *)arg;
325

326
    while (!data->done) {
327
        SDL_LockProperties(data->props);
328
        SDL_SetPointerProperty(data->props, "a", "thread_loop");
329
        SDL_UnlockProperties(data->props);
330
    }
331
    SDL_LockProperties(data->props);
332
    SDL_SetPointerProperty(data->props, "a", "thread_done");
333
    SDL_UnlockProperties(data->props);
334
    return 0;
335
}
336
static int properties_testLocking(void *arg)
337
{
338
    struct properties_thread_data data;
339
    SDL_Thread *thread;
340
    void *value;
341

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);
348
    if (thread) {
349
        SDLTest_AssertPass("Waiting for property to change to 'thread_loop'");
350
        for ( ; ; )
351
        {
352
            SDL_Delay(10);
353
            SDL_LockProperties(data.props);
354
            value = SDL_GetPointerProperty(data.props, "a", NULL);
355
            SDL_UnlockProperties(data.props);
356

357
            if (!value || SDL_strcmp((const char *)value, "thread_loop") == 0) {
358
                break;
359
            }
360
        }
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");
363

364
        SDLTest_AssertPass("Setting property to 'main'");
365
        SDL_LockProperties(data.props);
366
        SDL_SetPointerProperty(data.props, "a", "main");
367
        SDL_Delay(100);
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);
372

373
        data.done = SDL_TRUE;
374
        SDL_WaitThread(thread, NULL);
375

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");
379
    }
380
    SDL_DestroyProperties(data.props);
381

382
    return TEST_COMPLETED;
383
}
384

385
/* ================= Test References ================== */
386

387
/* Properties test cases */
388
static const SDLTest_TestCaseReference propertiesTestBasic = {
389
    (SDLTest_TestCaseFp)properties_testBasic, "properties_testBasic", "Test basic property functionality", TEST_ENABLED
390
};
391

392
static const SDLTest_TestCaseReference propertiesTestCopy = {
393
    (SDLTest_TestCaseFp)properties_testCopy, "properties_testCopy", "Test property copy functionality", TEST_ENABLED
394
};
395

396
static const SDLTest_TestCaseReference propertiesTestCleanup = {
397
    (SDLTest_TestCaseFp)properties_testCleanup, "properties_testCleanup", "Test property cleanup functionality", TEST_ENABLED
398
};
399

400
static const SDLTest_TestCaseReference propertiesTestLocking = {
401
    (SDLTest_TestCaseFp)properties_testLocking, "properties_testLocking", "Test property locking functionality", TEST_ENABLED
402
};
403

404
/* Sequence of Properties test cases */
405
static const SDLTest_TestCaseReference *propertiesTests[] = {
406
    &propertiesTestBasic,
407
    &propertiesTestCopy,
408
    &propertiesTestCleanup,
409
    &propertiesTestLocking,
410
    NULL
411
};
412

413
/* Properties test suite (global) */
414
SDLTest_TestSuiteReference propertiesTestSuite = {
415
    "Properties",
416
    NULL,
417
    propertiesTests,
418
    NULL
419
};
420

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.