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