summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testautomation_stdlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testautomation_stdlib.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testautomation_stdlib.c1520
1 files changed, 1520 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testautomation_stdlib.c b/src/contrib/SDL-3.2.20/test/testautomation_stdlib.c
new file mode 100644
index 0000000..e3f2d47
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testautomation_stdlib.c
@@ -0,0 +1,1520 @@
1/**
2 * Standard C library routine test suite
3 */
4#include <SDL3/SDL.h>
5#include <SDL3/SDL_test.h>
6#include "testautomation_suites.h"
7
8/* Test case functions */
9
10/**
11 * Call to SDL_strnlen
12 */
13static int SDLCALL stdlib_strnlen(void *arg)
14{
15 size_t result;
16 char *text_result;
17 const char *text = "food";
18 const char *expected;
19
20 result = SDL_strnlen(text, 6);
21 SDLTest_AssertPass("Call to SDL_strndup(\"food\", 6)");
22 SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", (int)result);
23
24 result = SDL_strnlen(text, 3);
25 SDLTest_AssertPass("Call to SDL_strndup(\"food\", 3)");
26 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int)result);
27
28 text_result = SDL_strndup(text, 3);
29 expected = "foo";
30 SDLTest_AssertPass("Call to SDL_strndup(\"food\", 3)");
31 SDLTest_AssertCheck(SDL_strcmp(text_result, expected) == 0, "Check text, expected: %s, got: %s", expected, text_result);
32 SDL_free(text_result);
33
34 return TEST_COMPLETED;
35}
36
37/**
38 * Call to SDL_strlcpy
39 */
40static int SDLCALL stdlib_strlcpy(void *arg)
41{
42 size_t result;
43 char text[1024];
44 const char *expected;
45
46 result = SDL_strlcpy(text, "foo", sizeof(text));
47 expected = "foo";
48 SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
49 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
50 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), (int)result);
51
52 result = SDL_strlcpy(text, "foo", 2);
53 expected = "f";
54 SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
55 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
56 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int)result);
57
58 return TEST_COMPLETED;
59}
60
61/**
62 * Call to SDL_strstr
63 */
64static int SDLCALL stdlib_strstr(void *arg)
65{
66 char *result;
67 const char *text = "abcdef";
68 const char *expected;
69
70 result = SDL_strstr(text, "");
71 expected = text;
72 SDLTest_AssertPass("Call to SDL_strstr(text, \"\")");
73 SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
74
75 result = SDL_strstr(text, "abc");
76 expected = text;
77 SDLTest_AssertPass("Call to SDL_strstr(text, \"abc\")");
78 SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
79
80 result = SDL_strstr(text, "bcd");
81 expected = text+1;
82 SDLTest_AssertPass("Call to SDL_strstr(text, \"bcd\")");
83 SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
84
85 result = SDL_strstr(text, "xyz");
86 expected = NULL;
87 SDLTest_AssertPass("Call to SDL_strstr(text, \"xyz\")");
88 SDLTest_AssertCheck(result == expected, "Check result, expected: (null), got: %s", result);
89
90 result = SDL_strnstr(text, "", SDL_strlen(text));
91 expected = text;
92 SDLTest_AssertPass("Call to SDL_strnstr(text, \"\", SDL_strlen(text))");
93 SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
94
95 result = SDL_strnstr(text, "abc", SDL_strlen(text));
96 expected = text;
97 SDLTest_AssertPass("Call to SDL_strnstr(text, \"abc\", SDL_strlen(text))");
98 SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
99
100 result = SDL_strnstr(text, "bcd", SDL_strlen(text));
101 expected = text+1;
102 SDLTest_AssertPass("Call to SDL_strnstr(text, \"bcd\", SDL_strlen(text))");
103 SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
104
105 result = SDL_strnstr(text, "bcd", 3);
106 expected = NULL;
107 SDLTest_AssertPass("Call to SDL_strnstr(text, \"bcd\", 3)");
108 SDLTest_AssertCheck(result == expected, "Check result, expected: (null), got: %s", result);
109
110 result = SDL_strnstr(text, "xyz", 3);
111 expected = NULL;
112 SDLTest_AssertPass("Call to SDL_strnstr(text, \"xyz\", 3)");
113 SDLTest_AssertCheck(result == expected, "Check result, expected: (null), got: %s", result);
114
115 result = SDL_strnstr(text, "xyz", SDL_strlen(text)*100000);
116 expected = NULL;
117 SDLTest_AssertPass("Call to SDL_strnstr(text, \"xyz\", SDL_strlen(text)*100000)");
118 SDLTest_AssertCheck(result == expected, "Check result, expected: (null), got: %s", result);
119
120 return TEST_COMPLETED;
121}
122
123#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
124#pragma GCC diagnostic push
125#ifdef HAVE_WFORMAT
126#pragma GCC diagnostic ignored "-Wformat"
127#endif
128#ifdef HAVE_WFORMAT_EXTRA_ARGS
129#pragma GCC diagnostic ignored "-Wformat-extra-args"
130#endif
131#endif
132
133/**
134 * Call to SDL_snprintf
135 */
136static int SDLCALL stdlib_snprintf(void *arg)
137{
138 int result;
139 int predicted;
140 char text[1024];
141 const char *expected, *expected2, *expected3, *expected4, *expected5;
142 size_t size;
143
144 result = SDL_snprintf(text, sizeof(text), "%s", "foo");
145 expected = "foo";
146 SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
147 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
148 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
149
150 result = SDL_snprintf(text, sizeof(text), "%10sA", "foo");
151 expected = " fooA";
152 SDLTest_AssertPass("Call to SDL_snprintf(\"%%10sA\", \"foo\")");
153 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
154 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
155
156 result = SDL_snprintf(text, sizeof(text), "%-10sA", "foo");
157 expected = "foo A";
158 SDLTest_AssertPass("Call to SDL_snprintf(\"%%-10sA\", \"foo\")");
159 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
160 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
161
162 result = SDL_snprintf(text, sizeof(text), "%S", L"foo");
163 expected = "foo";
164 SDLTest_AssertPass("Call to SDL_snprintf(\"%%S\", \"foo\")");
165 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
166 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
167
168 result = SDL_snprintf(text, sizeof(text), "%ls", L"foo");
169 expected = "foo";
170 SDLTest_AssertPass("Call to SDL_snprintf(\"%%ls\", \"foo\")");
171 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
172 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
173
174 result = SDL_snprintf(text, 2, "%s", "foo");
175 expected = "f";
176 SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
177 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
178 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
179
180 result = SDL_snprintf(NULL, 0, "%s", "foo");
181 SDLTest_AssertPass("Call to SDL_snprintf(NULL, 0, \"%%s\", \"foo\")");
182 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
183
184 result = SDL_snprintf(text, 2, "%s\n", "foo");
185 expected = "f";
186 SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\\n\", \"foo\") with buffer size 2");
187 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
188 SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", result);
189
190 result = SDL_snprintf(text, sizeof(text), "%f", 0.0);
191 predicted = SDL_snprintf(NULL, 0, "%f", 0.0);
192 expected = "0.000000";
193 SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 0.0)");
194 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
195 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
196 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
197
198 result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
199 predicted = SDL_snprintf(NULL, 0, "%f", 1.0);
200 expected = "1.000000";
201 SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
202 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
203 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
204 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
205
206 result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
207 predicted = SDL_snprintf(NULL, 0, "%.f", 1.0);
208 expected = "1";
209 SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
210 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
211 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
212 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
213
214 result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
215 predicted = SDL_snprintf(NULL, 0, "%#.f", 1.0);
216 expected = "1.";
217 SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
218 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
219 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
220 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
221
222 result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
223 predicted = SDL_snprintf(NULL, 0, "%f", 1.0 + 1.0 / 3.0);
224 expected = "1.333333";
225 SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
226 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
227 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
228 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
229
230 result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
231 predicted = SDL_snprintf(NULL, 0, "%+f", 1.0 + 1.0 / 3.0);
232 expected = "+1.333333";
233 SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
234 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
235 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
236 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
237
238 result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
239 predicted = SDL_snprintf(NULL, 0, "%.2f", 1.0 + 1.0 / 3.0);
240 expected = "1.33";
241 SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
242 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
243 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
244 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
245
246 result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
247 predicted = SDL_snprintf(NULL, 0, "%6.2f", 1.0 + 1.0 / 3.0);
248 expected = " 1.33";
249 SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
250 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
251 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
252 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
253
254 result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
255 predicted = SDL_snprintf(NULL, 0, "%06.2f", 1.0 + 1.0 / 3.0);
256 expected = "001.33";
257 SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
258 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
259 SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
260 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
261
262 result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
263 expected = "001.";
264 SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
265 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
266 SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
267
268 {
269 static struct
270 {
271 int precision;
272 float value;
273 const char *expected_f;
274 const char *expected_g;
275 } f_and_g_test_cases[] = {
276 { 6, 100.0f, "100.000000", "100" },
277 { 6, -100.0f, "-100.000000", "-100" },
278 { 6, 100.75f, "100.750000", "100.75" },
279 { 6, -100.75f, "-100.750000", "-100.75" },
280 { 6, ((100 * 60 * 1000) / 1001) / 100.0f, "59.939999", "59.94" },
281 { 6, -((100 * 60 * 1000) / 1001) / 100.0f, "-59.939999", "-59.94" },
282 { 6, ((100 * 120 * 1000) / 1001) / 100.0f, "119.879997", "119.88" },
283 { 6, -((100 * 120 * 1000) / 1001) / 100.0f, "-119.879997", "-119.88" },
284 { 6, 0.9999999f, "1.000000", "1" },
285 { 6, -0.9999999f, "-1.000000", "-1" },
286 { 5, 9.999999f, "10.00000", "10" },
287 { 5, -9.999999f, "-10.00000", "-10" },
288 };
289 int i;
290
291 for (i = 0; i < SDL_arraysize(f_and_g_test_cases); ++i) {
292 float value = f_and_g_test_cases[i].value;
293 int prec = f_and_g_test_cases[i].precision;
294
295 result = SDL_snprintf(text, sizeof(text), "%.*f", prec, value);
296 predicted = SDL_snprintf(NULL, 0, "%.*f", prec, value);
297 expected = f_and_g_test_cases[i].expected_f;
298 SDLTest_AssertPass("Call to SDL_snprintf(\"%%.5f\", %g)", value);
299 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
300 SDLTest_AssertCheck(result == SDL_strlen(expected), "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
301 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
302
303 result = SDL_snprintf(text, sizeof(text), "%g", value);
304 predicted = SDL_snprintf(NULL, 0, "%g", value);
305 expected = f_and_g_test_cases[i].expected_g;
306 SDLTest_AssertPass("Call to SDL_snprintf(\"%%g\", %g)", value);
307 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
308 SDLTest_AssertCheck(result == SDL_strlen(expected), "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
309 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
310 }
311 }
312
313 size = 64;
314 result = SDL_snprintf(text, sizeof(text), "%zu %s", size, "test");
315 expected = "64 test";
316 SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%zu %%s\", size, \"test\")");
317 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
318 SDLTest_AssertCheck(result == 7, "Check result value, expected: 7, got: %d", result);
319
320 result = SDL_snprintf(text, sizeof(text), "%p", (void *)0x1234abcd);
321 expected = "0x1234abcd";
322 expected2 = "1234ABCD";
323 expected3 = "000000001234ABCD";
324 expected4 = "1234abcd";
325 expected5 = "000000001234abcd";
326 SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%p\", 0x1234abcd)");
327 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 ||
328 SDL_strcmp(text, expected2) == 0 ||
329 SDL_strcmp(text, expected3) == 0 ||
330 SDL_strcmp(text, expected4) == 0 ||
331 SDL_strcmp(text, expected5) == 0,
332 "Check text, expected: '%s', got: '%s'", expected, text);
333 SDLTest_AssertCheck(result == SDL_strlen(expected) ||
334 result == SDL_strlen(expected2) ||
335 result == SDL_strlen(expected3) ||
336 result == SDL_strlen(expected4) ||
337 result == SDL_strlen(expected5),
338 "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
339
340 result = SDL_snprintf(text, sizeof(text), "A %p B", (void *)0x1234abcd);
341 expected = "A 0x1234abcd B";
342 expected2 = "A 1234ABCD B";
343 expected3 = "A 000000001234ABCD B";
344 expected4 = "A 1234abcd B";
345 expected5 = "A 000000001234abcd B";
346 SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"A %%p B\", 0x1234abcd)");
347 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 ||
348 SDL_strcmp(text, expected2) == 0 ||
349 SDL_strcmp(text, expected3) == 0 ||
350 SDL_strcmp(text, expected4) == 0 ||
351 SDL_strcmp(text, expected5) == 0,
352 "Check text, expected: '%s', got: '%s'", expected, text);
353 SDLTest_AssertCheck(result == SDL_strlen(expected) ||
354 result == SDL_strlen(expected2) ||
355 result == SDL_strlen(expected3) ||
356 result == SDL_strlen(expected4) ||
357 result == SDL_strlen(expected5),
358 "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
359
360 if (sizeof(void *) >= 8) {
361 result = SDL_snprintf(text, sizeof(text), "%p", (void *)SDL_SINT64_C(0x1ba07bddf60));
362 expected = "0x1ba07bddf60";
363 expected2 = "000001BA07BDDF60";
364 expected3 = "000001ba07bddf60";
365 SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%p\", 0x1ba07bddf60)");
366 SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 ||
367 SDL_strcmp(text, expected2) == 0 ||
368 SDL_strcmp(text, expected3) == 0,
369 "Check text, expected: '%s', got: '%s'", expected, text);
370 SDLTest_AssertCheck(result == SDL_strlen(expected) ||
371 result == SDL_strlen(expected2) ||
372 result == SDL_strlen(expected3),
373 "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
374 }
375 return TEST_COMPLETED;
376}
377
378/**
379 * Call to SDL_swprintf
380 */
381static int SDLCALL stdlib_swprintf(void *arg)
382{
383 int result;
384 int predicted;
385 wchar_t text[1024];
386 const wchar_t *expected;
387 size_t size;
388
389 result = SDL_swprintf(text, SDL_arraysize(text), L"%s", "hello, world");
390 expected = L"hello, world";
391 SDLTest_AssertPass("Call to SDL_swprintf(\"%%s\", \"hello, world\")");
392 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
393 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
394
395 result = SDL_swprintf(text, 2, L"%s", "hello, world");
396 expected = L"h";
397 SDLTest_AssertPass("Call to SDL_swprintf(\"%%s\", \"hello, world\") with buffer size 2");
398 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
399 SDLTest_AssertCheck(result == 12, "Check result value, expected: 12, got: %d", result);
400
401 result = SDL_swprintf(NULL, 0, L"%s", "hello, world");
402 SDLTest_AssertPass("Call to SDL_swprintf(NULL, 0, \"%%s\", \"hello, world\")");
403 SDLTest_AssertCheck(result == 12, "Check result value, expected: 12, got: %d", result);
404
405 result = SDL_swprintf(text, SDL_arraysize(text), L"%s", "foo");
406 expected = L"foo";
407 SDLTest_AssertPass("Call to SDL_swprintf(\"%%s\", \"foo\")");
408 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
409 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
410
411 result = SDL_swprintf(text, 2, L"%s", "foo");
412 expected = L"f";
413 SDLTest_AssertPass("Call to SDL_swprintf(\"%%s\", \"foo\") with buffer size 2");
414 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
415 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
416
417 result = SDL_swprintf(NULL, 0, L"%s", "foo");
418 SDLTest_AssertPass("Call to SDL_swprintf(NULL, 0, \"%%s\", \"foo\")");
419 SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
420
421 result = SDL_swprintf(text, 2, L"%s\n", "foo");
422 expected = L"f";
423 SDLTest_AssertPass("Call to SDL_swprintf(\"%%s\\n\", \"foo\") with buffer size 2");
424 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
425 SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", result);
426
427 result = SDL_swprintf(text, sizeof(text), L"%f", 0.0);
428 predicted = SDL_swprintf(NULL, 0, L"%f", 0.0);
429 expected = L"0.000000";
430 SDLTest_AssertPass("Call to SDL_swprintf(\"%%f\", 0.0)");
431 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
432 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
433 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
434
435 result = SDL_swprintf(text, sizeof(text), L"%f", 1.0);
436 predicted = SDL_swprintf(NULL, 0, L"%f", 1.0);
437 expected = L"1.000000";
438 SDLTest_AssertPass("Call to SDL_swprintf(\"%%f\", 1.0)");
439 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
440 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
441 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
442
443 result = SDL_swprintf(text, sizeof(text), L"%.f", 1.0);
444 predicted = SDL_swprintf(NULL, 0, L"%.f", 1.0);
445 expected = L"1";
446 SDLTest_AssertPass("Call to SDL_swprintf(\"%%.f\", 1.0)");
447 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
448 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
449 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
450
451 result = SDL_swprintf(text, sizeof(text), L"%#.f", 1.0);
452 predicted = SDL_swprintf(NULL, 0, L"%#.f", 1.0);
453 expected = L"1.";
454 SDLTest_AssertPass("Call to SDL_swprintf(\"%%#.f\", 1.0)");
455 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
456 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
457 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
458
459 result = SDL_swprintf(text, sizeof(text), L"%f", 1.0 + 1.0 / 3.0);
460 predicted = SDL_swprintf(NULL, 0, L"%f", 1.0 + 1.0 / 3.0);
461 expected = L"1.333333";
462 SDLTest_AssertPass("Call to SDL_swprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
463 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
464 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
465 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
466
467 result = SDL_swprintf(text, sizeof(text), L"%+f", 1.0 + 1.0 / 3.0);
468 predicted = SDL_swprintf(NULL, 0, L"%+f", 1.0 + 1.0 / 3.0);
469 expected = L"+1.333333";
470 SDLTest_AssertPass("Call to SDL_swprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
471 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
472 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
473 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
474
475 result = SDL_swprintf(text, sizeof(text), L"%.2f", 1.0 + 1.0 / 3.0);
476 predicted = SDL_swprintf(NULL, 0, L"%.2f", 1.0 + 1.0 / 3.0);
477 expected = L"1.33";
478 SDLTest_AssertPass("Call to SDL_swprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
479 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
480 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
481 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
482
483 result = SDL_swprintf(text, sizeof(text), L"%6.2f", 1.0 + 1.0 / 3.0);
484 predicted = SDL_swprintf(NULL, 0, L"%6.2f", 1.0 + 1.0 / 3.0);
485 expected = L" 1.33";
486 SDLTest_AssertPass("Call to SDL_swprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
487 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
488 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
489 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
490
491 result = SDL_swprintf(text, sizeof(text), L"%06.2f", 1.0 + 1.0 / 3.0);
492 predicted = SDL_swprintf(NULL, 0, L"%06.2f", 1.0 + 1.0 / 3.0);
493 expected = L"001.33";
494 SDLTest_AssertPass("Call to SDL_swprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
495 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
496 SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
497 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
498
499 result = SDL_swprintf(text, 5, L"%06.2f", 1.0 + 1.0 / 3.0);
500 expected = L"001.";
501 SDLTest_AssertPass("Call to SDL_swprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
502 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
503 SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
504
505 {
506 static struct
507 {
508 float value;
509 const wchar_t *expected_f;
510 const wchar_t *expected_g;
511 } f_and_g_test_cases[] = {
512 { 100.0f, L"100.000000", L"100" },
513 { -100.0f, L"-100.000000", L"-100" },
514 { 100.75f, L"100.750000", L"100.75" },
515 { -100.75f, L"-100.750000", L"-100.75" },
516 { ((100 * 60 * 1000) / 1001) / 100.0f, L"59.939999", L"59.94" },
517 { -((100 * 60 * 1000) / 1001) / 100.0f, L"-59.939999", L"-59.94" },
518 { ((100 * 120 * 1000) / 1001) / 100.0f, L"119.879997", L"119.88" },
519 { -((100 * 120 * 1000) / 1001) / 100.0f, L"-119.879997", L"-119.88" },
520 { 9.9999999f, L"10.000000", L"10" },
521 { -9.9999999f, L"-10.000000", L"-10" },
522 };
523 int i;
524
525 for (i = 0; i < SDL_arraysize(f_and_g_test_cases); ++i) {
526 float value = f_and_g_test_cases[i].value;
527
528 result = SDL_swprintf(text, sizeof(text), L"%f", value);
529 predicted = SDL_swprintf(NULL, 0, L"%f", value);
530 expected = f_and_g_test_cases[i].expected_f;
531 SDLTest_AssertPass("Call to SDL_swprintf(\"%%f\", %g)", value);
532 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
533 SDLTest_AssertCheck(result == SDL_wcslen(expected), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(expected), result);
534 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
535
536 result = SDL_swprintf(text, sizeof(text), L"%g", value);
537 predicted = SDL_swprintf(NULL, 0, L"%g", value);
538 expected = f_and_g_test_cases[i].expected_g;
539 SDLTest_AssertPass("Call to SDL_swprintf(\"%%g\", %g)", value);
540 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
541 SDLTest_AssertCheck(result == SDL_wcslen(expected), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(expected), result);
542 SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
543 }
544 }
545
546 size = 64;
547 result = SDL_swprintf(text, sizeof(text), L"%zu %s", size, "test");
548 expected = L"64 test";
549 SDLTest_AssertPass("Call to SDL_swprintf(text, sizeof(text), \"%%zu %%s\", size, \"test\")");
550 SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
551 SDLTest_AssertCheck(result == 7, "Check result value, expected: 7, got: %d", result);
552
553 return TEST_COMPLETED;
554}
555
556#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
557#pragma GCC diagnostic pop
558#endif
559
560/**
561 * Call to SDL_GetEnvironmentVariable() and SDL_SetEnvironmentVariable()
562 */
563static int SDLCALL stdlib_getsetenv(void *arg)
564{
565 SDL_Environment *env = SDL_GetEnvironment();
566 const int nameLen = 16;
567 char name[17];
568 int counter;
569 int result;
570 char *value1;
571 char *value2;
572 char *expected;
573 int overwrite;
574 const char *text;
575
576 /* Create a random name. This tests SDL_GetEnvironmentVariable, since we need to */
577 /* make sure the variable is not set yet (it shouldn't). */
578 do {
579 for (counter = 0; counter < nameLen; counter++) {
580 name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
581 }
582 name[nameLen] = '\0';
583
584 text = SDL_GetEnvironmentVariable(env, name);
585 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, '%s')", name);
586 if (text) {
587 SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, (int)SDL_strlen(text));
588 }
589 } while (text);
590
591 /* Create random values to set */
592 value1 = SDLTest_RandomAsciiStringOfSize(10);
593 value2 = SDLTest_RandomAsciiStringOfSize(10);
594
595 /* Set value 1 without overwrite */
596 overwrite = 0;
597 expected = value1;
598 result = SDL_SetEnvironmentVariable(env, name, value1, overwrite);
599 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, '%s','%s', %i)", name, value1, overwrite);
600 SDLTest_AssertCheck(result == true, "Check result, expected: 1, got: %i", result);
601
602 /* Check value */
603 text = SDL_GetEnvironmentVariable(env, name);
604 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, '%s')", name);
605 SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
606 if (text != NULL) {
607 SDLTest_AssertCheck(
608 SDL_strcmp(text, expected) == 0,
609 "Verify returned text, expected: %s, got: %s",
610 expected,
611 text);
612 }
613
614 /* Set value 2 with overwrite */
615 overwrite = 1;
616 expected = value2;
617 result = SDL_SetEnvironmentVariable(env, name, value2, overwrite);
618 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, '%s','%s', %i)", name, value2, overwrite);
619 SDLTest_AssertCheck(result == true, "Check result, expected: 1, got: %i", result);
620
621 /* Check value */
622 text = SDL_GetEnvironmentVariable(env, name);
623 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, '%s')", name);
624 SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
625 if (text != NULL) {
626 SDLTest_AssertCheck(
627 SDL_strcmp(text, expected) == 0,
628 "Verify returned text, expected: %s, got: %s",
629 expected,
630 text);
631 }
632
633 /* Set value 1 without overwrite */
634 overwrite = 0;
635 expected = value2;
636 result = SDL_SetEnvironmentVariable(env, name, value1, overwrite);
637 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, '%s','%s', %i)", name, value1, overwrite);
638 SDLTest_AssertCheck(result == true, "Check result, expected: 1, got: %i", result);
639
640 /* Check value */
641 text = SDL_GetEnvironmentVariable(env, name);
642 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, '%s')", name);
643 SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
644 if (text != NULL) {
645 SDLTest_AssertCheck(
646 SDL_strcmp(text, expected) == 0,
647 "Verify returned text, expected: %s, got: %s",
648 expected,
649 text);
650 }
651
652 /* Set value 1 with overwrite */
653 overwrite = 1;
654 expected = value1;
655 result = SDL_SetEnvironmentVariable(env, name, value1, overwrite);
656 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, '%s','%s', %i)", name, value1, overwrite);
657 SDLTest_AssertCheck(result == true, "Check result, expected: 1, got: %i", result);
658
659 /* Check value */
660 text = SDL_GetEnvironmentVariable(env, name);
661 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, '%s')", name);
662 SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
663 if (text != NULL) {
664 SDLTest_AssertCheck(
665 SDL_strcmp(text, expected) == 0,
666 "Verify returned text, expected: %s, got: %s",
667 expected,
668 text);
669 }
670
671 /* Verify setenv() with empty string vs unsetenv() */
672 result = SDL_SetEnvironmentVariable(env, "FOO", "1", 1);
673 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, 'FOO','1', 1)");
674 SDLTest_AssertCheck(result == true, "Check result, expected: 1, got: %i", result);
675 expected = "1";
676 text = SDL_GetEnvironmentVariable(env, "FOO");
677 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, 'FOO')");
678 SDLTest_AssertCheck(text && SDL_strcmp(text, expected) == 0, "Verify returned text, expected: %s, got: %s", expected, text);
679 result = SDL_SetEnvironmentVariable(env, "FOO", "", 1);
680 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, 'FOO','', 1)");
681 SDLTest_AssertCheck(result == true, "Check result, expected: 1, got: %i", result);
682 expected = "";
683 text = SDL_GetEnvironmentVariable(env, "FOO");
684 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, 'FOO')");
685 SDLTest_AssertCheck(text && SDL_strcmp(text, expected) == 0, "Verify returned text, expected: '%s', got: '%s'", expected, text);
686 result = SDL_UnsetEnvironmentVariable(env, "FOO");
687 SDLTest_AssertPass("Call to SDL_UnsetEnvironmentVariable(env, 'FOO')");
688 SDLTest_AssertCheck(result == true, "Check result, expected: 1, got: %i", result);
689 text = SDL_GetEnvironmentVariable(env, "FOO");
690 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, 'FOO')");
691 SDLTest_AssertCheck(text == NULL, "Verify returned text, expected: (null), got: %s", text);
692 result = SDL_SetEnvironmentVariable(env, "FOO", "0", 0);
693 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, 'FOO','0', 0)");
694 SDLTest_AssertCheck(result == true, "Check result, expected: 1, got: %i", result);
695 expected = "0";
696 text = SDL_GetEnvironmentVariable(env, "FOO");
697 SDLTest_AssertPass("Call to SDL_GetEnvironmentVariable(env, 'FOO')");
698 SDLTest_AssertCheck(text && SDL_strcmp(text, expected) == 0, "Verify returned text, expected: %s, got: %s", expected, text);
699
700 /* Negative cases */
701 for (overwrite = 0; overwrite <= 1; overwrite++) {
702 result = SDL_SetEnvironmentVariable(env, NULL, value1, overwrite);
703 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, NULL,'%s', %i)", value1, overwrite);
704 SDLTest_AssertCheck(result == false, "Check result, expected: 0, got: %i", result);
705 result = SDL_SetEnvironmentVariable(env, "", value1, overwrite);
706 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, '','%s', %i)", value1, overwrite);
707 SDLTest_AssertCheck(result == false, "Check result, expected: 0, got: %i", result);
708 result = SDL_SetEnvironmentVariable(env, "=", value1, overwrite);
709 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, '=','%s', %i)", value1, overwrite);
710 SDLTest_AssertCheck(result == false, "Check result, expected: 0, got: %i", result);
711 result = SDL_SetEnvironmentVariable(env, name, NULL, overwrite);
712 SDLTest_AssertPass("Call to SDL_SetEnvironmentVariable(env, '%s', NULL, %i)", name, overwrite);
713 SDLTest_AssertCheck(result == false, "Check result, expected: 0, got: %i", result);
714 }
715
716 /* Clean up */
717 SDL_free(value1);
718 SDL_free(value2);
719
720 return TEST_COMPLETED;
721}
722
723#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
724#pragma GCC diagnostic push
725#ifdef HAVE_WFORMAT
726#pragma GCC diagnostic ignored "-Wformat"
727#endif
728#ifdef HAVE_WFORMAT_EXTRA_ARGS
729#pragma GCC diagnostic ignored "-Wformat-extra-args"
730#endif
731#endif
732
733#define FMT_PRILLd "%" SDL_PRILLd
734#define FMT_PRILLdn "%" SDL_PRILLd "%" SDL_PRILL_PREFIX "n"
735#define FMT_PRILLu "%" SDL_PRILLu
736
737/**
738 * Call to SDL_sscanf
739 */
740static int SDLCALL stdlib_sscanf(void *arg)
741{
742 int output;
743 int result;
744 int length;
745 int expected_output;
746 int expected_result;
747 short short_output, expected_short_output, short_length;
748 long long_output, expected_long_output, long_length;
749 long long long_long_output, expected_long_long_output, long_long_length;
750 size_t size_output, expected_size_output;
751 void *ptr_output, *expected_ptr_output;
752 char text[128], text2[128];
753 unsigned int r = 0, g = 0, b = 0;
754
755 expected_output = output = 123;
756 expected_result = -1;
757 result = SDL_sscanf("", "%i", &output);
758 SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)");
759 SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
760 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
761
762 expected_output = output = 123;
763 expected_result = 0;
764 result = SDL_sscanf("a", "%i", &output);
765 SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)");
766 SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
767 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
768
769 output = 123;
770 length = 0;
771 expected_output = 2;
772 expected_result = 1;
773 result = SDL_sscanf("2", "%i%n", &output, &length);
774 SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i%%n\", &output, &length)");
775 SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
776 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
777 SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length);
778
779 output = 123;
780 length = 0;
781 expected_output = 0xa;
782 expected_result = 1;
783 result = SDL_sscanf("aa", "%1x%n", &output, &length);
784 SDLTest_AssertPass("Call to SDL_sscanf(\"aa\", \"%%1x%%n\", &output, &length)");
785 SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
786 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
787 SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length);
788
789 expected_result = 3;
790 result = SDL_sscanf("#026", "#%1x%1x%1x", &r, &g, &b);
791 SDLTest_AssertPass("Call to SDL_sscanf(\"#026\", \"#%%1x%%1x%%1x\", &r, &g, &b)");
792 expected_output = 0;
793 SDLTest_AssertCheck(r == expected_output, "Check output for r, expected: %i, got: %i", expected_output, r);
794 expected_output = 2;
795 SDLTest_AssertCheck(g == expected_output, "Check output for g, expected: %i, got: %i", expected_output, g);
796 expected_output = 6;
797 SDLTest_AssertCheck(b == expected_output, "Check output for b, expected: %i, got: %i", expected_output, b);
798 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
799
800#define SIZED_TEST_CASE(type, var, printf_specifier, scanf_specifier) \
801 var##_output = 123; \
802 var##_length = 0; \
803 expected_##var##_output = (type)(((unsigned type)(~0)) >> 1); \
804 expected_result = 1; \
805 result = SDL_snprintf(text, sizeof(text), printf_specifier, expected_##var##_output); \
806 result = SDL_sscanf(text, scanf_specifier, &var##_output, &var##_length); \
807 SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", %s, &output, &length)", text, #scanf_specifier); \
808 SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " printf_specifier ", got: " printf_specifier, expected_##var##_output, var##_output); \
809 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \
810 SDLTest_AssertCheck(var##_length == (type)SDL_strlen(text), "Check length, expected: %i, got: %i", (int)SDL_strlen(text), (int)var##_length); \
811 \
812 var##_output = 123; \
813 var##_length = 0; \
814 expected_##var##_output = ~(type)(((unsigned type)(~0)) >> 1); \
815 expected_result = 1; \
816 result = SDL_snprintf(text, sizeof(text), printf_specifier, expected_##var##_output); \
817 result = SDL_sscanf(text, scanf_specifier, &var##_output, &var##_length); \
818 SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", %s, &output, &length)", text, #scanf_specifier); \
819 SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " printf_specifier ", got: " printf_specifier, expected_##var##_output, var##_output); \
820 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \
821 SDLTest_AssertCheck(var##_length == (type)SDL_strlen(text), "Check length, expected: %i, got: %i", (int)SDL_strlen(text), (int)var##_length); \
822
823 SIZED_TEST_CASE(short, short, "%hd", "%hd%hn")
824 SIZED_TEST_CASE(long, long, "%ld", "%ld%ln")
825 SIZED_TEST_CASE(long long, long_long, FMT_PRILLd, FMT_PRILLdn)
826
827 size_output = 123;
828 expected_size_output = ~((size_t)0);
829 expected_result = 1;
830 result = SDL_snprintf(text, sizeof(text), "%zu", expected_size_output);
831 result = SDL_sscanf(text, "%zu", &size_output);
832 SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%zu\", &output)", text);
833 SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output);
834 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
835
836 ptr_output = (void *)123;
837 expected_ptr_output = (void *)0x1234567;
838 expected_result = 1;
839 result = SDL_snprintf(text, sizeof(text), "%p", expected_ptr_output);
840 result = SDL_sscanf(text, "%p", &ptr_output);
841 SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%p\", &output)", text);
842 SDLTest_AssertCheck(expected_ptr_output == ptr_output, "Check output, expected: %p, got: %p", expected_ptr_output, ptr_output);
843 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
844
845 expected_result = 1;
846 text[0] = '\0';
847 result = SDL_sscanf("abc def", "%s", text);
848 SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%s\", text)");
849 SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
850 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
851
852 expected_result = 1;
853 text[0] = '\0';
854 result = SDL_sscanf("abc,def", "%s", text);
855 SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%s\", text)");
856 SDLTest_AssertCheck(SDL_strcmp(text, "abc,def") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
857 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
858
859 expected_result = 1;
860 text[0] = '\0';
861 result = SDL_sscanf("abc,def", "%[cba]", text);
862 SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[cba]\", text)");
863 SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
864 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
865
866 expected_result = 1;
867 text[0] = '\0';
868 result = SDL_sscanf("abc,def", "%[a-z]", text);
869 SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[z-a]\", text)");
870 SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
871 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
872
873 expected_result = 1;
874 text[0] = '\0';
875 result = SDL_sscanf("abc,def", "%[^,]", text);
876 SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[^,]\", text)");
877 SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
878 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
879
880 expected_result = 0;
881 text[0] = '\0';
882 result = SDL_sscanf("abc,def", "%[A-Z]", text);
883 SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[A-Z]\", text)");
884 SDLTest_AssertCheck(SDL_strcmp(text, "") == 0, "Check output, expected: \"\", got: \"%s\"", text);
885 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
886
887 expected_result = 2;
888 text[0] = '\0';
889 text2[0] = '\0';
890 result = SDL_sscanf("abc,def", "%[abc],%[def]", text, text2);
891 SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc],%%[def]\", text)");
892 SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
893 SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
894 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
895
896 expected_result = 2;
897 text[0] = '\0';
898 text2[0] = '\0';
899 result = SDL_sscanf("abc,def", "%[abc]%*[,]%[def]", text, text2);
900 SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc]%%*[,]%%[def]\", text)");
901 SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
902 SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
903 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
904
905 expected_result = 2;
906 text[0] = '\0';
907 text2[0] = '\0';
908 result = SDL_sscanf("abc def", "%[abc] %[def]", text, text2);
909 SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%[abc] %%[def]\", text)");
910 SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
911 SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
912 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
913
914 expected_result = 1;
915 text[0] = '\0';
916 result = SDL_sscanf("abc123XYZ", "%[a-zA-Z0-9]", text);
917 SDLTest_AssertPass("Call to SDL_sscanf(\"abc123XYZ\", \"%%[a-zA-Z0-9]\", text)");
918 SDLTest_AssertCheck(SDL_strcmp(text, "abc123XYZ") == 0, "Check output, expected: \"abc123XYZ\", got: \"%s\"", text);
919 SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
920
921 return TEST_COMPLETED;
922}
923
924#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
925#pragma GCC diagnostic pop
926#endif
927
928#ifdef _WIN64
929#define SIZE_FORMAT "I64u"
930#elif defined(SDL_PLATFORM_WIN32)
931#define SIZE_FORMAT "I32u"
932#else
933#define SIZE_FORMAT "zu"
934#endif
935
936/**
937 * Call to SDL_aligned_alloc
938 */
939static int SDLCALL stdlib_aligned_alloc(void *arg)
940{
941 size_t i, alignment;
942 void *ptr;
943
944 for (i = 0; i < 2*sizeof(void *); ++i) {
945 SDLTest_AssertPass("Call to SDL_aligned_alloc(%"SIZE_FORMAT")", i);
946 ptr = SDL_aligned_alloc(i, 1);
947 if (i < sizeof(void *)) {
948 alignment = sizeof(void *);
949 } else {
950 alignment = i;
951 }
952 SDLTest_AssertCheck(ptr != NULL, "Check output, expected non-NULL, got: %p", ptr);
953 SDLTest_AssertCheck((((size_t)ptr) % alignment) == 0, "Check output, expected aligned pointer, actual offset: %"SIZE_FORMAT, (((size_t)ptr) % alignment));
954 if (ptr != NULL) {
955 SDLTest_AssertPass("Filling memory to alignment value");
956 SDL_memset(ptr, 0xAA, alignment);
957 SDL_aligned_free(ptr);
958 }
959 }
960
961 return TEST_COMPLETED;
962}
963
964typedef struct
965{
966 size_t a;
967 size_t b;
968 size_t result;
969 bool status;
970} overflow_test;
971
972static const overflow_test multiplications[] = {
973 { 1, 1, 1, true },
974 { 0, 0, 0, true },
975 { SDL_SIZE_MAX, 0, 0, true },
976 { SDL_SIZE_MAX, 1, SDL_SIZE_MAX, true },
977 { SDL_SIZE_MAX / 2, 2, SDL_SIZE_MAX - (SDL_SIZE_MAX % 2), true },
978 { SDL_SIZE_MAX / 23, 23, SDL_SIZE_MAX - (SDL_SIZE_MAX % 23), true },
979
980 { (SDL_SIZE_MAX / 2) + 1, 2, 0, false },
981 { (SDL_SIZE_MAX / 23) + 42, 23, 0, false },
982 { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, false },
983};
984
985static const overflow_test additions[] = {
986 { 1, 1, 2, true },
987 { 0, 0, 0, true },
988 { SDL_SIZE_MAX, 0, SDL_SIZE_MAX, true },
989 { SDL_SIZE_MAX - 1, 1, SDL_SIZE_MAX, true },
990 { SDL_SIZE_MAX - 42, 23, SDL_SIZE_MAX - (42 - 23), true },
991
992 { SDL_SIZE_MAX, 1, 0, false },
993 { SDL_SIZE_MAX, 23, 0, false },
994 { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, false },
995};
996
997static int SDLCALL
998stdlib_overflow(void *arg)
999{
1000 size_t i;
1001 size_t useBuiltin;
1002
1003 for (useBuiltin = 0; useBuiltin < 2; useBuiltin++) {
1004 if (useBuiltin) {
1005 SDLTest_Log("Using gcc/clang builtins if possible");
1006 } else {
1007 SDLTest_Log("Not using gcc/clang builtins");
1008 }
1009
1010 for (i = 0; i < SDL_arraysize(multiplications); i++) {
1011 const overflow_test *t = &multiplications[i];
1012 int status;
1013 size_t result = ~t->result;
1014
1015 if (useBuiltin) {
1016 status = SDL_size_mul_check_overflow(t->a, t->b, &result);
1017 } else {
1018 /* This disables the macro that tries to use a gcc/clang
1019 * builtin, so we test the fallback implementation instead. */
1020 status = (SDL_size_mul_check_overflow)(t->a, t->b, &result);
1021 }
1022
1023 if (t->status) {
1024 SDLTest_AssertCheck(status,
1025 "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
1026 t->a, t->b);
1027 SDLTest_AssertCheck(result == t->result,
1028 "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
1029 t->a, t->b, t->result, result);
1030 } else {
1031 SDLTest_AssertCheck(!status,
1032 "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
1033 t->a, t->b);
1034 }
1035
1036 if (t->a == t->b) {
1037 continue;
1038 }
1039
1040 result = ~t->result;
1041
1042 if (useBuiltin) {
1043 status = SDL_size_mul_check_overflow(t->b, t->a, &result);
1044 } else {
1045 status = (SDL_size_mul_check_overflow)(t->b, t->a, &result);
1046 }
1047
1048 if (t->status) {
1049 SDLTest_AssertCheck(status,
1050 "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
1051 t->b, t->a);
1052 SDLTest_AssertCheck(result == t->result,
1053 "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
1054 t->b, t->a, t->result, result);
1055 } else {
1056 SDLTest_AssertCheck(!status,
1057 "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
1058 t->b, t->a);
1059 }
1060 }
1061
1062 for (i = 0; i < SDL_arraysize(additions); i++) {
1063 const overflow_test *t = &additions[i];
1064 bool status;
1065 size_t result = ~t->result;
1066
1067 if (useBuiltin) {
1068 status = SDL_size_add_check_overflow(t->a, t->b, &result);
1069 } else {
1070 status = (SDL_size_add_check_overflow)(t->a, t->b, &result);
1071 }
1072
1073 if (t->status) {
1074 SDLTest_AssertCheck(status,
1075 "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
1076 t->a, t->b);
1077 SDLTest_AssertCheck(result == t->result,
1078 "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
1079 t->a, t->b, t->result, result);
1080 } else {
1081 SDLTest_AssertCheck(!status,
1082 "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
1083 t->a, t->b);
1084 }
1085
1086 if (t->a == t->b) {
1087 continue;
1088 }
1089
1090 result = ~t->result;
1091
1092 if (useBuiltin) {
1093 status = SDL_size_add_check_overflow(t->b, t->a, &result);
1094 } else {
1095 status = (SDL_size_add_check_overflow)(t->b, t->a, &result);
1096 }
1097
1098 if (t->status) {
1099 SDLTest_AssertCheck(status,
1100 "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
1101 t->b, t->a);
1102 SDLTest_AssertCheck(result == t->result,
1103 "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
1104 t->b, t->a, t->result, result);
1105 } else {
1106 SDLTest_AssertCheck(!status,
1107 "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
1108 t->b, t->a);
1109 }
1110 }
1111 }
1112
1113 return TEST_COMPLETED;
1114}
1115
1116static void format_for_description(char *buffer, size_t buflen, const char *text) {
1117 if (text == NULL) {
1118 SDL_strlcpy(buffer, "NULL", buflen);
1119 } else {
1120 SDL_snprintf(buffer, buflen, "\"%s\"", text);
1121 }
1122}
1123
1124static int SDLCALL
1125stdlib_iconv(void *arg)
1126{
1127 struct {
1128 bool expect_success;
1129 const char *from_encoding;
1130 const char *text;
1131 const char *to_encoding;
1132 const char *expected;
1133 } inputs[] = {
1134 { false, "bogus-from-encoding", NULL, "bogus-to-encoding", NULL },
1135 { false, "bogus-from-encoding", "hello world", "bogus-to-encoding", NULL },
1136 { false, "bogus-from-encoding", "hello world", "ascii", NULL },
1137 { true, "utf-8", NULL, "ascii", "" },
1138 { true, "utf-8", "hello world", "ascii", "hello world" },
1139 { true, "utf-8", "\xe2\x8c\xa8\xf0\x9f\x92\xbb", "utf-16le", "\x28\x23\x3d\xd8\xbb\xdc\x00" },
1140 };
1141 SDL_iconv_t cd;
1142 size_t i;
1143
1144 for (i = 0; i < SDL_arraysize(inputs); i++) {
1145 char to_encoding_str[32];
1146 char from_encoding_str[32];
1147 char text_str[32];
1148 size_t len_text = 0;
1149 int r;
1150 char out_buffer[6];
1151 const char *in_ptr;
1152 size_t in_pos;
1153 char *out_ptr;
1154 char *output;
1155 size_t iconv_result;
1156 size_t out_len;
1157 bool is_error;
1158 size_t out_pos;
1159
1160 SDLTest_AssertPass("case %d", (int)i);
1161 format_for_description(to_encoding_str, SDL_arraysize(to_encoding_str), inputs[i].to_encoding);
1162 format_for_description(from_encoding_str, SDL_arraysize(from_encoding_str), inputs[i].from_encoding);
1163 format_for_description(text_str, SDL_arraysize(text_str), inputs[i].text);
1164
1165 if (inputs[i].text) {
1166 len_text = SDL_strlen(inputs[i].text) + 1;
1167 }
1168
1169 SDLTest_AssertPass("About to call SDL_iconv_open(%s, %s)", to_encoding_str, from_encoding_str);
1170 cd = SDL_iconv_open(inputs[i].to_encoding, inputs[i].from_encoding);
1171 if (inputs[i].expect_success) {
1172 SDLTest_AssertCheck(cd != (SDL_iconv_t)SDL_ICONV_ERROR, "result must NOT be SDL_ICONV_ERROR");
1173 } else {
1174 SDLTest_AssertCheck(cd == (SDL_iconv_t)SDL_ICONV_ERROR, "result must be SDL_ICONV_ERROR");
1175 }
1176
1177 in_ptr = inputs[i].text;
1178 in_pos = 0;
1179 out_pos = 0;
1180 do {
1181 size_t in_left;
1182 size_t count_written;
1183 size_t count_read;
1184
1185 in_left = len_text - in_pos;
1186 out_ptr = out_buffer;
1187 out_len = SDL_arraysize(out_buffer);
1188 SDLTest_AssertPass("About to call SDL_iconv(cd, %s+%d, .., dest, ..)", text_str, (int)in_pos);
1189 iconv_result = SDL_iconv(cd, &in_ptr, &in_left, &out_ptr, &out_len);
1190 count_written = SDL_arraysize(out_buffer) - out_len;
1191 count_read = in_ptr - inputs[i].text - in_pos;
1192 in_pos += count_read;
1193
1194 is_error = iconv_result == SDL_ICONV_ERROR
1195 || iconv_result == SDL_ICONV_EILSEQ
1196 || iconv_result == SDL_ICONV_EINVAL;
1197 if (inputs[i].expect_success) {
1198 SDLTest_AssertCheck(!is_error, "result must NOT be an error code");
1199 SDLTest_AssertCheck(count_written > 0 || inputs[i].expected[out_pos] == '\0', "%" SDL_PRIu64 " bytes have been written", (Uint64)count_written);
1200 SDLTest_AssertCheck(out_pos <= SDL_strlen(inputs[i].expected), "Data written by SDL_iconv cannot be longer then reference output");
1201 SDLTest_CompareMemory(out_buffer, count_written, inputs[i].expected + out_pos, count_written);
1202 } else {
1203 SDLTest_AssertCheck(is_error, "result must be an error code");
1204 break;
1205 }
1206 out_pos += count_written;
1207 if (count_written == 0) {
1208 break;
1209 }
1210 if (count_read == 0) {
1211 SDLTest_AssertCheck(false, "SDL_iconv wrote data, but read no data");
1212 break;
1213 }
1214 } while (!is_error && in_pos < len_text);
1215
1216 SDLTest_AssertPass("About to call SDL_iconv_close(cd)");
1217 r = SDL_iconv_close(cd);
1218 if (inputs[i].expect_success) {
1219 SDLTest_AssertCheck(r == 0, "result must be 0");
1220 } else {
1221 SDLTest_AssertCheck(r == -1, "result must be -1");
1222 }
1223
1224 SDLTest_AssertPass("About to call SDL_iconv_string(%s, %s, %s, %" SDL_PRIu64 ")",
1225 to_encoding_str, from_encoding_str, text_str, (Uint64)len_text);
1226 output = SDL_iconv_string(inputs[i].to_encoding, inputs[i].from_encoding, inputs[i].text, len_text);
1227 if (inputs[i].expect_success) {
1228 SDLTest_AssertCheck(output != NULL, "result must NOT be NULL");
1229 SDLTest_AssertCheck(SDL_strncmp(inputs[i].expected, output, SDL_strlen(inputs[i].expected)) == 0,
1230 "converted string should be correct");
1231 } else {
1232 SDLTest_AssertCheck(output == NULL, "result must be NULL");
1233 }
1234 SDL_free(output);
1235 }
1236
1237 return TEST_COMPLETED;
1238}
1239
1240
1241static int SDLCALL
1242stdlib_strpbrk(void *arg)
1243{
1244 struct {
1245 const char *input;
1246 const char *accept;
1247 int expected[3]; /* negative if NULL */
1248 } test_cases[] = {
1249 { "", "", { -1, -1, -1 } },
1250 { "abc", "", { -1, -1, -1 } },
1251 { "Abc", "a", { -1, -1, -1 } },
1252 { "abc", "a", { 0, -1, -1 } },
1253 { "abcbd", "bbbb", { 1, 3, -1 } },
1254 { "a;b;c", ";", { 1, 3, -1 } },
1255 { "a;b;c", ",", { -1, -1, -1 } },
1256 { "a:bbbb;c", ";:", { 1, 6, -1 } },
1257 { "Hello\tS DL\n", " \t\r\n", { 5, 7, 10 } },
1258 };
1259 int i;
1260
1261 for (i = 0; i < SDL_arraysize(test_cases); i++) {
1262 int j;
1263 const char *input = test_cases[i].input;
1264
1265 for (j = 0; j < SDL_arraysize(test_cases[i].expected); j++) {
1266 char *result;
1267
1268 SDLTest_AssertPass("About to call SDL_strpbrk(\"%s\", \"%s\")", input, test_cases[i].accept);
1269 result = SDL_strpbrk(input, test_cases[i].accept);
1270 if (test_cases[i].expected[j] < 0) {
1271 SDLTest_AssertCheck(result == NULL, "Expected NULL, got %p", result);
1272 } else {
1273 SDLTest_AssertCheck(result == test_cases[i].input + test_cases[i].expected[j], "Expected %p, got %p", test_cases[i].input + test_cases[i].expected[j], result);
1274 input = test_cases[i].input + test_cases[i].expected[j] + 1;
1275 }
1276 }
1277 }
1278 return TEST_COMPLETED;
1279}
1280
1281static int SDLCALL stdlib_wcstol(void *arg)
1282{
1283 const long long_max = (~0UL) >> 1;
1284 const long long_min = ((~0UL) >> 1) + 1UL;
1285
1286#define WCSTOL_TEST_CASE(str, base, expected_result, expected_endp_offset) do { \
1287 const wchar_t *s = str; \
1288 long r, expected_r = expected_result; \
1289 wchar_t *ep, *expected_ep = (wchar_t *)s + expected_endp_offset; \
1290 r = SDL_wcstol(s, &ep, base); \
1291 SDLTest_AssertPass("Call to SDL_wcstol(" #str ", &endp, " #base ")"); \
1292 SDLTest_AssertCheck(r == expected_r, "Check result value, expected: %ld, got: %ld", expected_r, r); \
1293 SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \
1294 } while (0)
1295
1296 // infer decimal
1297 WCSTOL_TEST_CASE(L"\t 123abcxyz", 0, 123, 6); // skip leading space
1298 WCSTOL_TEST_CASE(L"+123abcxyz", 0, 123, 4);
1299 WCSTOL_TEST_CASE(L"-123abcxyz", 0, -123, 4);
1300 WCSTOL_TEST_CASE(L"99999999999999999999abcxyz", 0, long_max, 20);
1301 WCSTOL_TEST_CASE(L"-99999999999999999999abcxyz", 0, long_min, 21);
1302
1303 // infer hexadecimal
1304 WCSTOL_TEST_CASE(L"0x123abcxyz", 0, 0x123abc, 8);
1305 WCSTOL_TEST_CASE(L"0X123ABCXYZ", 0, 0x123abc, 8); // uppercase X
1306
1307 // infer octal
1308 WCSTOL_TEST_CASE(L"0123abcxyz", 0, 0123, 4);
1309
1310 // arbitrary bases
1311 WCSTOL_TEST_CASE(L"00110011", 2, 51, 8);
1312 WCSTOL_TEST_CASE(L"-uvwxyz", 32, -991, 3);
1313 WCSTOL_TEST_CASE(L"ZzZzZzZzZzZzZ", 36, long_max, 13);
1314
1315 WCSTOL_TEST_CASE(L"-0", 10, 0, 2);
1316 WCSTOL_TEST_CASE(L" - 1", 0, 0, 0); // invalid input
1317
1318 // values near the bounds of the type
1319 if (sizeof(long) == 4) {
1320 WCSTOL_TEST_CASE(L"2147483647", 10, 2147483647, 10);
1321 WCSTOL_TEST_CASE(L"2147483648", 10, 2147483647, 10);
1322 WCSTOL_TEST_CASE(L"-2147483648", 10, -2147483647L - 1, 11);
1323 WCSTOL_TEST_CASE(L"-2147483649", 10, -2147483647L - 1, 11);
1324 WCSTOL_TEST_CASE(L"-9999999999999999999999999999999999999999", 10, -2147483647L - 1, 41);
1325 }
1326
1327#undef WCSTOL_TEST_CASE
1328
1329 return TEST_COMPLETED;
1330}
1331
1332static int SDLCALL stdlib_strtox(void *arg)
1333{
1334 const unsigned long long ullong_max = ~0ULL;
1335
1336#define STRTOX_TEST_CASE(func_name, type, format_spec, str, base, expected_result, expected_endp_offset) do { \
1337 const char *s = str; \
1338 type r, expected_r = expected_result; \
1339 char *ep, *expected_ep = (char *)s + expected_endp_offset; \
1340 r = func_name(s, &ep, base); \
1341 SDLTest_AssertPass("Call to " #func_name "(" #str ", &endp, " #base ")"); \
1342 SDLTest_AssertCheck(r == expected_r, "Check result value, expected: " format_spec ", got: " format_spec, expected_r, r); \
1343 SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \
1344 } while (0)
1345
1346 // infer decimal
1347 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "\t 123abcxyz", 0, 123, 6); // skip leading space
1348 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "+123abcxyz", 0, 123, 4);
1349 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "+123abcxyz", 0, 123, 4);
1350 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-123abcxyz", 0, -123, 4);
1351 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "9999999999999999999999999999999999999999abcxyz", 0, ullong_max, 40);
1352
1353 // infer hexadecimal
1354 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0x123abcxyz", 0, 0x123abc, 8);
1355 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0X123ABCXYZ", 0, 0x123abc, 8); // uppercase X
1356
1357 // infer octal
1358 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0123abcxyz", 0, 0123, 4);
1359
1360 // arbitrary bases
1361 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "00110011", 2, 51, 8);
1362 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-uvwxyz", 32, -991, 3);
1363 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "ZzZzZzZzZzZzZzZzZzZzZzZzZ", 36, ullong_max, 25);
1364
1365 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0", 0, 0, 1);
1366 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0", 10, 0, 1);
1367 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-0", 0, 0, 2);
1368 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-0", 10, 0, 2);
1369 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, " - 1", 0, 0, 0); // invalid input
1370
1371 // We know that SDL_strtol, SDL_strtoul and SDL_strtoll share the same code path as SDL_strtoull under the hood,
1372 // so the most interesting test cases are those close to the bounds of the integer type.
1373
1374 // For simplicity, we only run long/long long tests when they are 32-bit/64-bit, respectively.
1375 // Suppressing warnings would be difficult otherwise.
1376 // Since the CI runs the tests against a variety of targets, this should be fine in practice.
1377
1378 if (sizeof(long) == 4) {
1379 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "0", 0, 0, 1);
1380 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "0", 10, 0, 1);
1381 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-0", 0, 0, 2);
1382 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-0", 10, 0, 2);
1383 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "2147483647", 10, 2147483647, 10);
1384 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "2147483648", 10, 2147483647, 10);
1385 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-2147483648", 10, -2147483647L - 1, 11);
1386 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-2147483649", 10, -2147483647L - 1, 11);
1387 STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-9999999999999999999999999999999999999999", 10, -2147483647L - 1, 41);
1388
1389 STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "4294967295", 10, 4294967295UL, 10);
1390 STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "4294967296", 10, 4294967295UL, 10);
1391 STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "-4294967295", 10, 1, 11);
1392 }
1393
1394 if (sizeof(long long) == 8) {
1395 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "0", 0, 0LL, 1);
1396 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "0", 10, 0LL, 1);
1397 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-0", 0, 0LL, 2);
1398 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-0", 10, 0LL, 2);
1399 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "9223372036854775807", 10, 9223372036854775807LL, 19);
1400 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "9223372036854775808", 10, 9223372036854775807LL, 19);
1401 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9223372036854775808", 10, -9223372036854775807LL - 1, 20);
1402 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9223372036854775809", 10, -9223372036854775807LL - 1, 20);
1403 STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9999999999999999999999999999999999999999", 10, -9223372036854775807LL - 1, 41);
1404
1405 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "18446744073709551615", 10, 18446744073709551615ULL, 20);
1406 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "18446744073709551616", 10, 18446744073709551615ULL, 20);
1407 STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "-18446744073709551615", 10, 1, 21);
1408 }
1409
1410#undef STRTOX_TEST_CASE
1411
1412 return TEST_COMPLETED;
1413}
1414
1415static int SDLCALL stdlib_strtod(void *arg)
1416{
1417#define STRTOD_TEST_CASE(str, expected_result, expected_endp_offset) do { \
1418 const char *s = str; \
1419 double r, expected_r = expected_result; \
1420 char *ep, *expected_ep = (char *)s + expected_endp_offset; \
1421 r = SDL_strtod(s, &ep); \
1422 SDLTest_AssertPass("Call to SDL_strtod(" #str ", &endp)"); \
1423 SDLTest_AssertCheck(r == expected_r, "Check result value, expected: %f, got: %f", expected_r, r); \
1424 SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \
1425 } while (0)
1426
1427 STRTOD_TEST_CASE("\t 123.75abcxyz", 123.75, 9); // skip leading space
1428 STRTOD_TEST_CASE("+999.555", 999.555, 8);
1429 STRTOD_TEST_CASE("-999.555", -999.555, 8);
1430
1431#undef STRTOD_TEST_CASE
1432
1433 return TEST_COMPLETED;
1434}
1435
1436/* ================= Test References ================== */
1437
1438/* Standard C routine test cases */
1439static const SDLTest_TestCaseReference stdlibTest_strnlen = {
1440 stdlib_strnlen, "stdlib_strnlen", "Call to SDL_strnlen", TEST_ENABLED
1441};
1442
1443static const SDLTest_TestCaseReference stdlibTest_strlcpy = {
1444 stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED
1445};
1446
1447static const SDLTest_TestCaseReference stdlibTest_strstr = {
1448 stdlib_strstr, "stdlib_strstr", "Call to SDL_strstr", TEST_ENABLED
1449};
1450
1451static const SDLTest_TestCaseReference stdlibTest_snprintf = {
1452 stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED
1453};
1454
1455static const SDLTest_TestCaseReference stdlibTest_swprintf = {
1456 stdlib_swprintf, "stdlib_swprintf", "Call to SDL_swprintf", TEST_ENABLED
1457};
1458
1459static const SDLTest_TestCaseReference stdlibTest_getsetenv = {
1460 stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_GetEnvironmentVariable and SDL_SetEnvironmentVariable", TEST_ENABLED
1461};
1462
1463static const SDLTest_TestCaseReference stdlibTest_sscanf = {
1464 stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED
1465};
1466
1467static const SDLTest_TestCaseReference stdlibTest_aligned_alloc = {
1468 stdlib_aligned_alloc, "stdlib_aligned_alloc", "Call to SDL_aligned_alloc", TEST_ENABLED
1469};
1470
1471static const SDLTest_TestCaseReference stdlibTestOverflow = {
1472 stdlib_overflow, "stdlib_overflow", "Overflow detection", TEST_ENABLED
1473};
1474
1475static const SDLTest_TestCaseReference stdlibTest_iconv = {
1476 stdlib_iconv, "stdlib_iconv", "Calls to SDL_iconv", TEST_ENABLED
1477};
1478
1479static const SDLTest_TestCaseReference stdlibTest_strpbrk = {
1480 stdlib_strpbrk, "stdlib_strpbrk", "Calls to SDL_strpbrk", TEST_ENABLED
1481};
1482
1483static const SDLTest_TestCaseReference stdlibTest_wcstol = {
1484 stdlib_wcstol, "stdlib_wcstol", "Calls to SDL_wcstol", TEST_ENABLED
1485};
1486
1487static const SDLTest_TestCaseReference stdlibTest_strtox = {
1488 stdlib_strtox, "stdlib_strtox", "Calls to SDL_strtol, SDL_strtoul, SDL_strtoll and SDL_strtoull", TEST_ENABLED
1489};
1490
1491static const SDLTest_TestCaseReference stdlibTest_strtod = {
1492 stdlib_strtod, "stdlib_strtod", "Calls to SDL_strtod", TEST_ENABLED
1493};
1494
1495/* Sequence of Standard C routine test cases */
1496static const SDLTest_TestCaseReference *stdlibTests[] = {
1497 &stdlibTest_strnlen,
1498 &stdlibTest_strlcpy,
1499 &stdlibTest_strstr,
1500 &stdlibTest_snprintf,
1501 &stdlibTest_swprintf,
1502 &stdlibTest_getsetenv,
1503 &stdlibTest_sscanf,
1504 &stdlibTest_aligned_alloc,
1505 &stdlibTestOverflow,
1506 &stdlibTest_iconv,
1507 &stdlibTest_strpbrk,
1508 &stdlibTest_wcstol,
1509 &stdlibTest_strtox,
1510 &stdlibTest_strtod,
1511 NULL
1512};
1513
1514/* Standard C routine test suite (global) */
1515SDLTest_TestSuiteReference stdlibTestSuite = {
1516 "Stdlib",
1517 NULL,
1518 stdlibTests,
1519 NULL
1520};