diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testautomation_stdlib.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testautomation_stdlib.c | 1508 |
1 files changed, 1508 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testautomation_stdlib.c b/contrib/SDL-3.2.8/test/testautomation_stdlib.c new file mode 100644 index 0000000..bd7e17a --- /dev/null +++ b/contrib/SDL-3.2.8/test/testautomation_stdlib.c | |||
| @@ -0,0 +1,1508 @@ | |||
| 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 | */ | ||
| 13 | static 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 | */ | ||
| 40 | static 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 | */ | ||
| 64 | static 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 | */ | ||
| 136 | static 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 | */ | ||
| 381 | static 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 | */ | ||
| 563 | static 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 | */ | ||
| 740 | static 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 | |||
| 754 | expected_output = output = 123; | ||
| 755 | expected_result = -1; | ||
| 756 | result = SDL_sscanf("", "%i", &output); | ||
| 757 | SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)"); | ||
| 758 | SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output); | ||
| 759 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 760 | |||
| 761 | expected_output = output = 123; | ||
| 762 | expected_result = 0; | ||
| 763 | result = SDL_sscanf("a", "%i", &output); | ||
| 764 | SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)"); | ||
| 765 | SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output); | ||
| 766 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 767 | |||
| 768 | output = 123; | ||
| 769 | length = 0; | ||
| 770 | expected_output = 2; | ||
| 771 | expected_result = 1; | ||
| 772 | result = SDL_sscanf("2", "%i%n", &output, &length); | ||
| 773 | SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i%%n\", &output, &length)"); | ||
| 774 | SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output); | ||
| 775 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 776 | SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length); | ||
| 777 | |||
| 778 | output = 123; | ||
| 779 | length = 0; | ||
| 780 | expected_output = 0xa; | ||
| 781 | expected_result = 1; | ||
| 782 | result = SDL_sscanf("aa", "%1x%n", &output, &length); | ||
| 783 | SDLTest_AssertPass("Call to SDL_sscanf(\"aa\", \"%%1x%%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 | #define SIZED_TEST_CASE(type, var, printf_specifier, scanf_specifier) \ | ||
| 789 | var##_output = 123; \ | ||
| 790 | var##_length = 0; \ | ||
| 791 | expected_##var##_output = (type)(((unsigned type)(~0)) >> 1); \ | ||
| 792 | expected_result = 1; \ | ||
| 793 | result = SDL_snprintf(text, sizeof(text), printf_specifier, expected_##var##_output); \ | ||
| 794 | result = SDL_sscanf(text, scanf_specifier, &var##_output, &var##_length); \ | ||
| 795 | SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", %s, &output, &length)", text, #scanf_specifier); \ | ||
| 796 | SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " printf_specifier ", got: " printf_specifier, expected_##var##_output, var##_output); \ | ||
| 797 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \ | ||
| 798 | SDLTest_AssertCheck(var##_length == (type)SDL_strlen(text), "Check length, expected: %i, got: %i", (int)SDL_strlen(text), (int)var##_length); \ | ||
| 799 | \ | ||
| 800 | var##_output = 123; \ | ||
| 801 | var##_length = 0; \ | ||
| 802 | expected_##var##_output = ~(type)(((unsigned type)(~0)) >> 1); \ | ||
| 803 | expected_result = 1; \ | ||
| 804 | result = SDL_snprintf(text, sizeof(text), printf_specifier, expected_##var##_output); \ | ||
| 805 | result = SDL_sscanf(text, scanf_specifier, &var##_output, &var##_length); \ | ||
| 806 | SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", %s, &output, &length)", text, #scanf_specifier); \ | ||
| 807 | SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " printf_specifier ", got: " printf_specifier, expected_##var##_output, var##_output); \ | ||
| 808 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \ | ||
| 809 | SDLTest_AssertCheck(var##_length == (type)SDL_strlen(text), "Check length, expected: %i, got: %i", (int)SDL_strlen(text), (int)var##_length); \ | ||
| 810 | |||
| 811 | SIZED_TEST_CASE(short, short, "%hd", "%hd%hn") | ||
| 812 | SIZED_TEST_CASE(long, long, "%ld", "%ld%ln") | ||
| 813 | SIZED_TEST_CASE(long long, long_long, FMT_PRILLd, FMT_PRILLdn) | ||
| 814 | |||
| 815 | size_output = 123; | ||
| 816 | expected_size_output = ~((size_t)0); | ||
| 817 | expected_result = 1; | ||
| 818 | result = SDL_snprintf(text, sizeof(text), "%zu", expected_size_output); | ||
| 819 | result = SDL_sscanf(text, "%zu", &size_output); | ||
| 820 | SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%zu\", &output)", text); | ||
| 821 | SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output); | ||
| 822 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 823 | |||
| 824 | ptr_output = (void *)123; | ||
| 825 | expected_ptr_output = (void *)0x1234567; | ||
| 826 | expected_result = 1; | ||
| 827 | result = SDL_snprintf(text, sizeof(text), "%p", expected_ptr_output); | ||
| 828 | result = SDL_sscanf(text, "%p", &ptr_output); | ||
| 829 | SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%p\", &output)", text); | ||
| 830 | SDLTest_AssertCheck(expected_ptr_output == ptr_output, "Check output, expected: %p, got: %p", expected_ptr_output, ptr_output); | ||
| 831 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 832 | |||
| 833 | expected_result = 1; | ||
| 834 | text[0] = '\0'; | ||
| 835 | result = SDL_sscanf("abc def", "%s", text); | ||
| 836 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%s\", text)"); | ||
| 837 | SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); | ||
| 838 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 839 | |||
| 840 | expected_result = 1; | ||
| 841 | text[0] = '\0'; | ||
| 842 | result = SDL_sscanf("abc,def", "%s", text); | ||
| 843 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%s\", text)"); | ||
| 844 | SDLTest_AssertCheck(SDL_strcmp(text, "abc,def") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); | ||
| 845 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 846 | |||
| 847 | expected_result = 1; | ||
| 848 | text[0] = '\0'; | ||
| 849 | result = SDL_sscanf("abc,def", "%[cba]", text); | ||
| 850 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[cba]\", text)"); | ||
| 851 | SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); | ||
| 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", "%[a-z]", text); | ||
| 857 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[z-a]\", 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", "%[^,]", text); | ||
| 864 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[^,]\", text)"); | ||
| 865 | SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 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 = 0; | ||
| 869 | text[0] = '\0'; | ||
| 870 | result = SDL_sscanf("abc,def", "%[A-Z]", text); | ||
| 871 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[A-Z]\", text)"); | ||
| 872 | SDLTest_AssertCheck(SDL_strcmp(text, "") == 0, "Check output, expected: \"\", got: \"%s\"", text); | ||
| 873 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 874 | |||
| 875 | expected_result = 2; | ||
| 876 | text[0] = '\0'; | ||
| 877 | text2[0] = '\0'; | ||
| 878 | result = SDL_sscanf("abc,def", "%[abc],%[def]", text, text2); | ||
| 879 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc],%%[def]\", text)"); | ||
| 880 | SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); | ||
| 881 | SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2); | ||
| 882 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 883 | |||
| 884 | expected_result = 2; | ||
| 885 | text[0] = '\0'; | ||
| 886 | text2[0] = '\0'; | ||
| 887 | result = SDL_sscanf("abc,def", "%[abc]%*[,]%[def]", text, text2); | ||
| 888 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc]%%*[,]%%[def]\", text)"); | ||
| 889 | SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); | ||
| 890 | SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2); | ||
| 891 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 892 | |||
| 893 | expected_result = 2; | ||
| 894 | text[0] = '\0'; | ||
| 895 | text2[0] = '\0'; | ||
| 896 | result = SDL_sscanf("abc def", "%[abc] %[def]", text, text2); | ||
| 897 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%[abc] %%[def]\", text)"); | ||
| 898 | SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); | ||
| 899 | SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2); | ||
| 900 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 901 | |||
| 902 | expected_result = 1; | ||
| 903 | text[0] = '\0'; | ||
| 904 | result = SDL_sscanf("abc123XYZ", "%[a-zA-Z0-9]", text); | ||
| 905 | SDLTest_AssertPass("Call to SDL_sscanf(\"abc123XYZ\", \"%%[a-zA-Z0-9]\", text)"); | ||
| 906 | SDLTest_AssertCheck(SDL_strcmp(text, "abc123XYZ") == 0, "Check output, expected: \"abc123XYZ\", got: \"%s\"", text); | ||
| 907 | SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); | ||
| 908 | |||
| 909 | return TEST_COMPLETED; | ||
| 910 | } | ||
| 911 | |||
| 912 | #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS) | ||
| 913 | #pragma GCC diagnostic pop | ||
| 914 | #endif | ||
| 915 | |||
| 916 | #ifdef _WIN64 | ||
| 917 | #define SIZE_FORMAT "I64u" | ||
| 918 | #elif defined(SDL_PLATFORM_WIN32) | ||
| 919 | #define SIZE_FORMAT "I32u" | ||
| 920 | #else | ||
| 921 | #define SIZE_FORMAT "zu" | ||
| 922 | #endif | ||
| 923 | |||
| 924 | /** | ||
| 925 | * Call to SDL_aligned_alloc | ||
| 926 | */ | ||
| 927 | static int SDLCALL stdlib_aligned_alloc(void *arg) | ||
| 928 | { | ||
| 929 | size_t i, alignment; | ||
| 930 | void *ptr; | ||
| 931 | |||
| 932 | for (i = 0; i < 2*sizeof(void *); ++i) { | ||
| 933 | SDLTest_AssertPass("Call to SDL_aligned_alloc(%"SIZE_FORMAT")", i); | ||
| 934 | ptr = SDL_aligned_alloc(i, 1); | ||
| 935 | if (i < sizeof(void *)) { | ||
| 936 | alignment = sizeof(void *); | ||
| 937 | } else { | ||
| 938 | alignment = i; | ||
| 939 | } | ||
| 940 | SDLTest_AssertCheck(ptr != NULL, "Check output, expected non-NULL, got: %p", ptr); | ||
| 941 | SDLTest_AssertCheck((((size_t)ptr) % alignment) == 0, "Check output, expected aligned pointer, actual offset: %"SIZE_FORMAT, (((size_t)ptr) % alignment)); | ||
| 942 | if (ptr != NULL) { | ||
| 943 | SDLTest_AssertPass("Filling memory to alignment value"); | ||
| 944 | SDL_memset(ptr, 0xAA, alignment); | ||
| 945 | SDL_aligned_free(ptr); | ||
| 946 | } | ||
| 947 | } | ||
| 948 | |||
| 949 | return TEST_COMPLETED; | ||
| 950 | } | ||
| 951 | |||
| 952 | typedef struct | ||
| 953 | { | ||
| 954 | size_t a; | ||
| 955 | size_t b; | ||
| 956 | size_t result; | ||
| 957 | bool status; | ||
| 958 | } overflow_test; | ||
| 959 | |||
| 960 | static const overflow_test multiplications[] = { | ||
| 961 | { 1, 1, 1, true }, | ||
| 962 | { 0, 0, 0, true }, | ||
| 963 | { SDL_SIZE_MAX, 0, 0, true }, | ||
| 964 | { SDL_SIZE_MAX, 1, SDL_SIZE_MAX, true }, | ||
| 965 | { SDL_SIZE_MAX / 2, 2, SDL_SIZE_MAX - (SDL_SIZE_MAX % 2), true }, | ||
| 966 | { SDL_SIZE_MAX / 23, 23, SDL_SIZE_MAX - (SDL_SIZE_MAX % 23), true }, | ||
| 967 | |||
| 968 | { (SDL_SIZE_MAX / 2) + 1, 2, 0, false }, | ||
| 969 | { (SDL_SIZE_MAX / 23) + 42, 23, 0, false }, | ||
| 970 | { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, false }, | ||
| 971 | }; | ||
| 972 | |||
| 973 | static const overflow_test additions[] = { | ||
| 974 | { 1, 1, 2, true }, | ||
| 975 | { 0, 0, 0, true }, | ||
| 976 | { SDL_SIZE_MAX, 0, SDL_SIZE_MAX, true }, | ||
| 977 | { SDL_SIZE_MAX - 1, 1, SDL_SIZE_MAX, true }, | ||
| 978 | { SDL_SIZE_MAX - 42, 23, SDL_SIZE_MAX - (42 - 23), true }, | ||
| 979 | |||
| 980 | { SDL_SIZE_MAX, 1, 0, false }, | ||
| 981 | { SDL_SIZE_MAX, 23, 0, false }, | ||
| 982 | { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, false }, | ||
| 983 | }; | ||
| 984 | |||
| 985 | static int SDLCALL | ||
| 986 | stdlib_overflow(void *arg) | ||
| 987 | { | ||
| 988 | size_t i; | ||
| 989 | size_t useBuiltin; | ||
| 990 | |||
| 991 | for (useBuiltin = 0; useBuiltin < 2; useBuiltin++) { | ||
| 992 | if (useBuiltin) { | ||
| 993 | SDLTest_Log("Using gcc/clang builtins if possible"); | ||
| 994 | } else { | ||
| 995 | SDLTest_Log("Not using gcc/clang builtins"); | ||
| 996 | } | ||
| 997 | |||
| 998 | for (i = 0; i < SDL_arraysize(multiplications); i++) { | ||
| 999 | const overflow_test *t = &multiplications[i]; | ||
| 1000 | int status; | ||
| 1001 | size_t result = ~t->result; | ||
| 1002 | |||
| 1003 | if (useBuiltin) { | ||
| 1004 | status = SDL_size_mul_check_overflow(t->a, t->b, &result); | ||
| 1005 | } else { | ||
| 1006 | /* This disables the macro that tries to use a gcc/clang | ||
| 1007 | * builtin, so we test the fallback implementation instead. */ | ||
| 1008 | status = (SDL_size_mul_check_overflow)(t->a, t->b, &result); | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | if (t->status) { | ||
| 1012 | SDLTest_AssertCheck(status, | ||
| 1013 | "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed", | ||
| 1014 | t->a, t->b); | ||
| 1015 | SDLTest_AssertCheck(result == t->result, | ||
| 1016 | "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT, | ||
| 1017 | t->a, t->b, t->result, result); | ||
| 1018 | } else { | ||
| 1019 | SDLTest_AssertCheck(!status, | ||
| 1020 | "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail", | ||
| 1021 | t->a, t->b); | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | if (t->a == t->b) { | ||
| 1025 | continue; | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | result = ~t->result; | ||
| 1029 | |||
| 1030 | if (useBuiltin) { | ||
| 1031 | status = SDL_size_mul_check_overflow(t->b, t->a, &result); | ||
| 1032 | } else { | ||
| 1033 | status = (SDL_size_mul_check_overflow)(t->b, t->a, &result); | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | if (t->status) { | ||
| 1037 | SDLTest_AssertCheck(status, | ||
| 1038 | "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed", | ||
| 1039 | t->b, t->a); | ||
| 1040 | SDLTest_AssertCheck(result == t->result, | ||
| 1041 | "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT, | ||
| 1042 | t->b, t->a, t->result, result); | ||
| 1043 | } else { | ||
| 1044 | SDLTest_AssertCheck(!status, | ||
| 1045 | "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail", | ||
| 1046 | t->b, t->a); | ||
| 1047 | } | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | for (i = 0; i < SDL_arraysize(additions); i++) { | ||
| 1051 | const overflow_test *t = &additions[i]; | ||
| 1052 | bool status; | ||
| 1053 | size_t result = ~t->result; | ||
| 1054 | |||
| 1055 | if (useBuiltin) { | ||
| 1056 | status = SDL_size_add_check_overflow(t->a, t->b, &result); | ||
| 1057 | } else { | ||
| 1058 | status = (SDL_size_add_check_overflow)(t->a, t->b, &result); | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | if (t->status) { | ||
| 1062 | SDLTest_AssertCheck(status, | ||
| 1063 | "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed", | ||
| 1064 | t->a, t->b); | ||
| 1065 | SDLTest_AssertCheck(result == t->result, | ||
| 1066 | "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT, | ||
| 1067 | t->a, t->b, t->result, result); | ||
| 1068 | } else { | ||
| 1069 | SDLTest_AssertCheck(!status, | ||
| 1070 | "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail", | ||
| 1071 | t->a, t->b); | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | if (t->a == t->b) { | ||
| 1075 | continue; | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | result = ~t->result; | ||
| 1079 | |||
| 1080 | if (useBuiltin) { | ||
| 1081 | status = SDL_size_add_check_overflow(t->b, t->a, &result); | ||
| 1082 | } else { | ||
| 1083 | status = (SDL_size_add_check_overflow)(t->b, t->a, &result); | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | if (t->status) { | ||
| 1087 | SDLTest_AssertCheck(status, | ||
| 1088 | "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed", | ||
| 1089 | t->b, t->a); | ||
| 1090 | SDLTest_AssertCheck(result == t->result, | ||
| 1091 | "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT, | ||
| 1092 | t->b, t->a, t->result, result); | ||
| 1093 | } else { | ||
| 1094 | SDLTest_AssertCheck(!status, | ||
| 1095 | "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail", | ||
| 1096 | t->b, t->a); | ||
| 1097 | } | ||
| 1098 | } | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | return TEST_COMPLETED; | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | static void format_for_description(char *buffer, size_t buflen, const char *text) { | ||
| 1105 | if (text == NULL) { | ||
| 1106 | SDL_strlcpy(buffer, "NULL", buflen); | ||
| 1107 | } else { | ||
| 1108 | SDL_snprintf(buffer, buflen, "\"%s\"", text); | ||
| 1109 | } | ||
| 1110 | } | ||
| 1111 | |||
| 1112 | static int SDLCALL | ||
| 1113 | stdlib_iconv(void *arg) | ||
| 1114 | { | ||
| 1115 | struct { | ||
| 1116 | bool expect_success; | ||
| 1117 | const char *from_encoding; | ||
| 1118 | const char *text; | ||
| 1119 | const char *to_encoding; | ||
| 1120 | const char *expected; | ||
| 1121 | } inputs[] = { | ||
| 1122 | { false, "bogus-from-encoding", NULL, "bogus-to-encoding", NULL }, | ||
| 1123 | { false, "bogus-from-encoding", "hello world", "bogus-to-encoding", NULL }, | ||
| 1124 | { false, "bogus-from-encoding", "hello world", "ascii", NULL }, | ||
| 1125 | { true, "utf-8", NULL, "ascii", "" }, | ||
| 1126 | { true, "utf-8", "hello world", "ascii", "hello world" }, | ||
| 1127 | { true, "utf-8", "\xe2\x8c\xa8\xf0\x9f\x92\xbb", "utf-16le", "\x28\x23\x3d\xd8\xbb\xdc\x00" }, | ||
| 1128 | }; | ||
| 1129 | SDL_iconv_t cd; | ||
| 1130 | size_t i; | ||
| 1131 | |||
| 1132 | for (i = 0; i < SDL_arraysize(inputs); i++) { | ||
| 1133 | char to_encoding_str[32]; | ||
| 1134 | char from_encoding_str[32]; | ||
| 1135 | char text_str[32]; | ||
| 1136 | size_t len_text = 0; | ||
| 1137 | int r; | ||
| 1138 | char out_buffer[6]; | ||
| 1139 | const char *in_ptr; | ||
| 1140 | size_t in_pos; | ||
| 1141 | char *out_ptr; | ||
| 1142 | char *output; | ||
| 1143 | size_t iconv_result; | ||
| 1144 | size_t out_len; | ||
| 1145 | bool is_error; | ||
| 1146 | size_t out_pos; | ||
| 1147 | |||
| 1148 | SDLTest_AssertPass("case %d", (int)i); | ||
| 1149 | format_for_description(to_encoding_str, SDL_arraysize(to_encoding_str), inputs[i].to_encoding); | ||
| 1150 | format_for_description(from_encoding_str, SDL_arraysize(from_encoding_str), inputs[i].from_encoding); | ||
| 1151 | format_for_description(text_str, SDL_arraysize(text_str), inputs[i].text); | ||
| 1152 | |||
| 1153 | if (inputs[i].text) { | ||
| 1154 | len_text = SDL_strlen(inputs[i].text) + 1; | ||
| 1155 | } | ||
| 1156 | |||
| 1157 | SDLTest_AssertPass("About to call SDL_iconv_open(%s, %s)", to_encoding_str, from_encoding_str); | ||
| 1158 | cd = SDL_iconv_open(inputs[i].to_encoding, inputs[i].from_encoding); | ||
| 1159 | if (inputs[i].expect_success) { | ||
| 1160 | SDLTest_AssertCheck(cd != (SDL_iconv_t)SDL_ICONV_ERROR, "result must NOT be SDL_ICONV_ERROR"); | ||
| 1161 | } else { | ||
| 1162 | SDLTest_AssertCheck(cd == (SDL_iconv_t)SDL_ICONV_ERROR, "result must be SDL_ICONV_ERROR"); | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | in_ptr = inputs[i].text; | ||
| 1166 | in_pos = 0; | ||
| 1167 | out_pos = 0; | ||
| 1168 | do { | ||
| 1169 | size_t in_left; | ||
| 1170 | size_t count_written; | ||
| 1171 | size_t count_read; | ||
| 1172 | |||
| 1173 | in_left = len_text - in_pos; | ||
| 1174 | out_ptr = out_buffer; | ||
| 1175 | out_len = SDL_arraysize(out_buffer); | ||
| 1176 | SDLTest_AssertPass("About to call SDL_iconv(cd, %s+%d, .., dest, ..)", text_str, (int)in_pos); | ||
| 1177 | iconv_result = SDL_iconv(cd, &in_ptr, &in_left, &out_ptr, &out_len); | ||
| 1178 | count_written = SDL_arraysize(out_buffer) - out_len; | ||
| 1179 | count_read = in_ptr - inputs[i].text - in_pos; | ||
| 1180 | in_pos += count_read; | ||
| 1181 | |||
| 1182 | is_error = iconv_result == SDL_ICONV_ERROR | ||
| 1183 | || iconv_result == SDL_ICONV_EILSEQ | ||
| 1184 | || iconv_result == SDL_ICONV_EINVAL; | ||
| 1185 | if (inputs[i].expect_success) { | ||
| 1186 | SDLTest_AssertCheck(!is_error, "result must NOT be an error code"); | ||
| 1187 | SDLTest_AssertCheck(count_written > 0 || inputs[i].expected[out_pos] == '\0', "%" SDL_PRIu64 " bytes have been written", (Uint64)count_written); | ||
| 1188 | SDLTest_AssertCheck(out_pos <= SDL_strlen(inputs[i].expected), "Data written by SDL_iconv cannot be longer then reference output"); | ||
| 1189 | SDLTest_CompareMemory(out_buffer, count_written, inputs[i].expected + out_pos, count_written); | ||
| 1190 | } else { | ||
| 1191 | SDLTest_AssertCheck(is_error, "result must be an error code"); | ||
| 1192 | break; | ||
| 1193 | } | ||
| 1194 | out_pos += count_written; | ||
| 1195 | if (count_written == 0) { | ||
| 1196 | break; | ||
| 1197 | } | ||
| 1198 | if (count_read == 0) { | ||
| 1199 | SDLTest_AssertCheck(false, "SDL_iconv wrote data, but read no data"); | ||
| 1200 | break; | ||
| 1201 | } | ||
| 1202 | } while (!is_error && in_pos < len_text); | ||
| 1203 | |||
| 1204 | SDLTest_AssertPass("About to call SDL_iconv_close(cd)"); | ||
| 1205 | r = SDL_iconv_close(cd); | ||
| 1206 | if (inputs[i].expect_success) { | ||
| 1207 | SDLTest_AssertCheck(r == 0, "result must be 0"); | ||
| 1208 | } else { | ||
| 1209 | SDLTest_AssertCheck(r == -1, "result must be -1"); | ||
| 1210 | } | ||
| 1211 | |||
| 1212 | SDLTest_AssertPass("About to call SDL_iconv_string(%s, %s, %s, %" SDL_PRIu64 ")", | ||
| 1213 | to_encoding_str, from_encoding_str, text_str, (Uint64)len_text); | ||
| 1214 | output = SDL_iconv_string(inputs[i].to_encoding, inputs[i].from_encoding, inputs[i].text, len_text); | ||
| 1215 | if (inputs[i].expect_success) { | ||
| 1216 | SDLTest_AssertCheck(output != NULL, "result must NOT be NULL"); | ||
| 1217 | SDLTest_AssertCheck(SDL_strncmp(inputs[i].expected, output, SDL_strlen(inputs[i].expected)) == 0, | ||
| 1218 | "converted string should be correct"); | ||
| 1219 | } else { | ||
| 1220 | SDLTest_AssertCheck(output == NULL, "result must be NULL"); | ||
| 1221 | } | ||
| 1222 | SDL_free(output); | ||
| 1223 | } | ||
| 1224 | |||
| 1225 | return TEST_COMPLETED; | ||
| 1226 | } | ||
| 1227 | |||
| 1228 | |||
| 1229 | static int SDLCALL | ||
| 1230 | stdlib_strpbrk(void *arg) | ||
| 1231 | { | ||
| 1232 | struct { | ||
| 1233 | const char *input; | ||
| 1234 | const char *accept; | ||
| 1235 | int expected[3]; /* negative if NULL */ | ||
| 1236 | } test_cases[] = { | ||
| 1237 | { "", "", { -1, -1, -1 } }, | ||
| 1238 | { "abc", "", { -1, -1, -1 } }, | ||
| 1239 | { "Abc", "a", { -1, -1, -1 } }, | ||
| 1240 | { "abc", "a", { 0, -1, -1 } }, | ||
| 1241 | { "abcbd", "bbbb", { 1, 3, -1 } }, | ||
| 1242 | { "a;b;c", ";", { 1, 3, -1 } }, | ||
| 1243 | { "a;b;c", ",", { -1, -1, -1 } }, | ||
| 1244 | { "a:bbbb;c", ";:", { 1, 6, -1 } }, | ||
| 1245 | { "Hello\tS DL\n", " \t\r\n", { 5, 7, 10 } }, | ||
| 1246 | }; | ||
| 1247 | int i; | ||
| 1248 | |||
| 1249 | for (i = 0; i < SDL_arraysize(test_cases); i++) { | ||
| 1250 | int j; | ||
| 1251 | const char *input = test_cases[i].input; | ||
| 1252 | |||
| 1253 | for (j = 0; j < SDL_arraysize(test_cases[i].expected); j++) { | ||
| 1254 | char *result; | ||
| 1255 | |||
| 1256 | SDLTest_AssertPass("About to call SDL_strpbrk(\"%s\", \"%s\")", input, test_cases[i].accept); | ||
| 1257 | result = SDL_strpbrk(input, test_cases[i].accept); | ||
| 1258 | if (test_cases[i].expected[j] < 0) { | ||
| 1259 | SDLTest_AssertCheck(result == NULL, "Expected NULL, got %p", result); | ||
| 1260 | } else { | ||
| 1261 | 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); | ||
| 1262 | input = test_cases[i].input + test_cases[i].expected[j] + 1; | ||
| 1263 | } | ||
| 1264 | } | ||
| 1265 | } | ||
| 1266 | return TEST_COMPLETED; | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | static int SDLCALL stdlib_wcstol(void *arg) | ||
| 1270 | { | ||
| 1271 | const long long_max = (~0UL) >> 1; | ||
| 1272 | const long long_min = ((~0UL) >> 1) + 1UL; | ||
| 1273 | |||
| 1274 | #define WCSTOL_TEST_CASE(str, base, expected_result, expected_endp_offset) do { \ | ||
| 1275 | const wchar_t *s = str; \ | ||
| 1276 | long r, expected_r = expected_result; \ | ||
| 1277 | wchar_t *ep, *expected_ep = (wchar_t *)s + expected_endp_offset; \ | ||
| 1278 | r = SDL_wcstol(s, &ep, base); \ | ||
| 1279 | SDLTest_AssertPass("Call to SDL_wcstol(" #str ", &endp, " #base ")"); \ | ||
| 1280 | SDLTest_AssertCheck(r == expected_r, "Check result value, expected: %ld, got: %ld", expected_r, r); \ | ||
| 1281 | SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \ | ||
| 1282 | } while (0) | ||
| 1283 | |||
| 1284 | // infer decimal | ||
| 1285 | WCSTOL_TEST_CASE(L"\t 123abcxyz", 0, 123, 6); // skip leading space | ||
| 1286 | WCSTOL_TEST_CASE(L"+123abcxyz", 0, 123, 4); | ||
| 1287 | WCSTOL_TEST_CASE(L"-123abcxyz", 0, -123, 4); | ||
| 1288 | WCSTOL_TEST_CASE(L"99999999999999999999abcxyz", 0, long_max, 20); | ||
| 1289 | WCSTOL_TEST_CASE(L"-99999999999999999999abcxyz", 0, long_min, 21); | ||
| 1290 | |||
| 1291 | // infer hexadecimal | ||
| 1292 | WCSTOL_TEST_CASE(L"0x123abcxyz", 0, 0x123abc, 8); | ||
| 1293 | WCSTOL_TEST_CASE(L"0X123ABCXYZ", 0, 0x123abc, 8); // uppercase X | ||
| 1294 | |||
| 1295 | // infer octal | ||
| 1296 | WCSTOL_TEST_CASE(L"0123abcxyz", 0, 0123, 4); | ||
| 1297 | |||
| 1298 | // arbitrary bases | ||
| 1299 | WCSTOL_TEST_CASE(L"00110011", 2, 51, 8); | ||
| 1300 | WCSTOL_TEST_CASE(L"-uvwxyz", 32, -991, 3); | ||
| 1301 | WCSTOL_TEST_CASE(L"ZzZzZzZzZzZzZ", 36, long_max, 13); | ||
| 1302 | |||
| 1303 | WCSTOL_TEST_CASE(L"-0", 10, 0, 2); | ||
| 1304 | WCSTOL_TEST_CASE(L" - 1", 0, 0, 0); // invalid input | ||
| 1305 | |||
| 1306 | // values near the bounds of the type | ||
| 1307 | if (sizeof(long) == 4) { | ||
| 1308 | WCSTOL_TEST_CASE(L"2147483647", 10, 2147483647, 10); | ||
| 1309 | WCSTOL_TEST_CASE(L"2147483648", 10, 2147483647, 10); | ||
| 1310 | WCSTOL_TEST_CASE(L"-2147483648", 10, -2147483647L - 1, 11); | ||
| 1311 | WCSTOL_TEST_CASE(L"-2147483649", 10, -2147483647L - 1, 11); | ||
| 1312 | WCSTOL_TEST_CASE(L"-9999999999999999999999999999999999999999", 10, -2147483647L - 1, 41); | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | #undef WCSTOL_TEST_CASE | ||
| 1316 | |||
| 1317 | return TEST_COMPLETED; | ||
| 1318 | } | ||
| 1319 | |||
| 1320 | static int SDLCALL stdlib_strtox(void *arg) | ||
| 1321 | { | ||
| 1322 | const unsigned long long ullong_max = ~0ULL; | ||
| 1323 | |||
| 1324 | #define STRTOX_TEST_CASE(func_name, type, format_spec, str, base, expected_result, expected_endp_offset) do { \ | ||
| 1325 | const char *s = str; \ | ||
| 1326 | type r, expected_r = expected_result; \ | ||
| 1327 | char *ep, *expected_ep = (char *)s + expected_endp_offset; \ | ||
| 1328 | r = func_name(s, &ep, base); \ | ||
| 1329 | SDLTest_AssertPass("Call to " #func_name "(" #str ", &endp, " #base ")"); \ | ||
| 1330 | SDLTest_AssertCheck(r == expected_r, "Check result value, expected: " format_spec ", got: " format_spec, expected_r, r); \ | ||
| 1331 | SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \ | ||
| 1332 | } while (0) | ||
| 1333 | |||
| 1334 | // infer decimal | ||
| 1335 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "\t 123abcxyz", 0, 123, 6); // skip leading space | ||
| 1336 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "+123abcxyz", 0, 123, 4); | ||
| 1337 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "+123abcxyz", 0, 123, 4); | ||
| 1338 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-123abcxyz", 0, -123, 4); | ||
| 1339 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "9999999999999999999999999999999999999999abcxyz", 0, ullong_max, 40); | ||
| 1340 | |||
| 1341 | // infer hexadecimal | ||
| 1342 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0x123abcxyz", 0, 0x123abc, 8); | ||
| 1343 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0X123ABCXYZ", 0, 0x123abc, 8); // uppercase X | ||
| 1344 | |||
| 1345 | // infer octal | ||
| 1346 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0123abcxyz", 0, 0123, 4); | ||
| 1347 | |||
| 1348 | // arbitrary bases | ||
| 1349 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "00110011", 2, 51, 8); | ||
| 1350 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-uvwxyz", 32, -991, 3); | ||
| 1351 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "ZzZzZzZzZzZzZzZzZzZzZzZzZ", 36, ullong_max, 25); | ||
| 1352 | |||
| 1353 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0", 0, 0, 1); | ||
| 1354 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0", 10, 0, 1); | ||
| 1355 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-0", 0, 0, 2); | ||
| 1356 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-0", 10, 0, 2); | ||
| 1357 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, " - 1", 0, 0, 0); // invalid input | ||
| 1358 | |||
| 1359 | // We know that SDL_strtol, SDL_strtoul and SDL_strtoll share the same code path as SDL_strtoull under the hood, | ||
| 1360 | // so the most interesting test cases are those close to the bounds of the integer type. | ||
| 1361 | |||
| 1362 | // For simplicity, we only run long/long long tests when they are 32-bit/64-bit, respectively. | ||
| 1363 | // Suppressing warnings would be difficult otherwise. | ||
| 1364 | // Since the CI runs the tests against a variety of targets, this should be fine in practice. | ||
| 1365 | |||
| 1366 | if (sizeof(long) == 4) { | ||
| 1367 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "0", 0, 0, 1); | ||
| 1368 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "0", 10, 0, 1); | ||
| 1369 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-0", 0, 0, 2); | ||
| 1370 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-0", 10, 0, 2); | ||
| 1371 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "2147483647", 10, 2147483647, 10); | ||
| 1372 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "2147483648", 10, 2147483647, 10); | ||
| 1373 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-2147483648", 10, -2147483647L - 1, 11); | ||
| 1374 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-2147483649", 10, -2147483647L - 1, 11); | ||
| 1375 | STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-9999999999999999999999999999999999999999", 10, -2147483647L - 1, 41); | ||
| 1376 | |||
| 1377 | STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "4294967295", 10, 4294967295UL, 10); | ||
| 1378 | STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "4294967296", 10, 4294967295UL, 10); | ||
| 1379 | STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "-4294967295", 10, 1, 11); | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | if (sizeof(long long) == 8) { | ||
| 1383 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "0", 0, 0LL, 1); | ||
| 1384 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "0", 10, 0LL, 1); | ||
| 1385 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-0", 0, 0LL, 2); | ||
| 1386 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-0", 10, 0LL, 2); | ||
| 1387 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "9223372036854775807", 10, 9223372036854775807LL, 19); | ||
| 1388 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "9223372036854775808", 10, 9223372036854775807LL, 19); | ||
| 1389 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9223372036854775808", 10, -9223372036854775807LL - 1, 20); | ||
| 1390 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9223372036854775809", 10, -9223372036854775807LL - 1, 20); | ||
| 1391 | STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9999999999999999999999999999999999999999", 10, -9223372036854775807LL - 1, 41); | ||
| 1392 | |||
| 1393 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "18446744073709551615", 10, 18446744073709551615ULL, 20); | ||
| 1394 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "18446744073709551616", 10, 18446744073709551615ULL, 20); | ||
| 1395 | STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "-18446744073709551615", 10, 1, 21); | ||
| 1396 | } | ||
| 1397 | |||
| 1398 | #undef STRTOX_TEST_CASE | ||
| 1399 | |||
| 1400 | return TEST_COMPLETED; | ||
| 1401 | } | ||
| 1402 | |||
| 1403 | static int SDLCALL stdlib_strtod(void *arg) | ||
| 1404 | { | ||
| 1405 | #define STRTOD_TEST_CASE(str, expected_result, expected_endp_offset) do { \ | ||
| 1406 | const char *s = str; \ | ||
| 1407 | double r, expected_r = expected_result; \ | ||
| 1408 | char *ep, *expected_ep = (char *)s + expected_endp_offset; \ | ||
| 1409 | r = SDL_strtod(s, &ep); \ | ||
| 1410 | SDLTest_AssertPass("Call to SDL_strtod(" #str ", &endp)"); \ | ||
| 1411 | SDLTest_AssertCheck(r == expected_r, "Check result value, expected: %f, got: %f", expected_r, r); \ | ||
| 1412 | SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \ | ||
| 1413 | } while (0) | ||
| 1414 | |||
| 1415 | STRTOD_TEST_CASE("\t 123.75abcxyz", 123.75, 9); // skip leading space | ||
| 1416 | STRTOD_TEST_CASE("+999.555", 999.555, 8); | ||
| 1417 | STRTOD_TEST_CASE("-999.555", -999.555, 8); | ||
| 1418 | |||
| 1419 | #undef STRTOD_TEST_CASE | ||
| 1420 | |||
| 1421 | return TEST_COMPLETED; | ||
| 1422 | } | ||
| 1423 | |||
| 1424 | /* ================= Test References ================== */ | ||
| 1425 | |||
| 1426 | /* Standard C routine test cases */ | ||
| 1427 | static const SDLTest_TestCaseReference stdlibTest_strnlen = { | ||
| 1428 | stdlib_strnlen, "stdlib_strnlen", "Call to SDL_strnlen", TEST_ENABLED | ||
| 1429 | }; | ||
| 1430 | |||
| 1431 | static const SDLTest_TestCaseReference stdlibTest_strlcpy = { | ||
| 1432 | stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED | ||
| 1433 | }; | ||
| 1434 | |||
| 1435 | static const SDLTest_TestCaseReference stdlibTest_strstr = { | ||
| 1436 | stdlib_strstr, "stdlib_strstr", "Call to SDL_strstr", TEST_ENABLED | ||
| 1437 | }; | ||
| 1438 | |||
| 1439 | static const SDLTest_TestCaseReference stdlibTest_snprintf = { | ||
| 1440 | stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED | ||
| 1441 | }; | ||
| 1442 | |||
| 1443 | static const SDLTest_TestCaseReference stdlibTest_swprintf = { | ||
| 1444 | stdlib_swprintf, "stdlib_swprintf", "Call to SDL_swprintf", TEST_ENABLED | ||
| 1445 | }; | ||
| 1446 | |||
| 1447 | static const SDLTest_TestCaseReference stdlibTest_getsetenv = { | ||
| 1448 | stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_GetEnvironmentVariable and SDL_SetEnvironmentVariable", TEST_ENABLED | ||
| 1449 | }; | ||
| 1450 | |||
| 1451 | static const SDLTest_TestCaseReference stdlibTest_sscanf = { | ||
| 1452 | stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED | ||
| 1453 | }; | ||
| 1454 | |||
| 1455 | static const SDLTest_TestCaseReference stdlibTest_aligned_alloc = { | ||
| 1456 | stdlib_aligned_alloc, "stdlib_aligned_alloc", "Call to SDL_aligned_alloc", TEST_ENABLED | ||
| 1457 | }; | ||
| 1458 | |||
| 1459 | static const SDLTest_TestCaseReference stdlibTestOverflow = { | ||
| 1460 | stdlib_overflow, "stdlib_overflow", "Overflow detection", TEST_ENABLED | ||
| 1461 | }; | ||
| 1462 | |||
| 1463 | static const SDLTest_TestCaseReference stdlibTest_iconv = { | ||
| 1464 | stdlib_iconv, "stdlib_iconv", "Calls to SDL_iconv", TEST_ENABLED | ||
| 1465 | }; | ||
| 1466 | |||
| 1467 | static const SDLTest_TestCaseReference stdlibTest_strpbrk = { | ||
| 1468 | stdlib_strpbrk, "stdlib_strpbrk", "Calls to SDL_strpbrk", TEST_ENABLED | ||
| 1469 | }; | ||
| 1470 | |||
| 1471 | static const SDLTest_TestCaseReference stdlibTest_wcstol = { | ||
| 1472 | stdlib_wcstol, "stdlib_wcstol", "Calls to SDL_wcstol", TEST_ENABLED | ||
| 1473 | }; | ||
| 1474 | |||
| 1475 | static const SDLTest_TestCaseReference stdlibTest_strtox = { | ||
| 1476 | stdlib_strtox, "stdlib_strtox", "Calls to SDL_strtol, SDL_strtoul, SDL_strtoll and SDL_strtoull", TEST_ENABLED | ||
| 1477 | }; | ||
| 1478 | |||
| 1479 | static const SDLTest_TestCaseReference stdlibTest_strtod = { | ||
| 1480 | stdlib_strtod, "stdlib_strtod", "Calls to SDL_strtod", TEST_ENABLED | ||
| 1481 | }; | ||
| 1482 | |||
| 1483 | /* Sequence of Standard C routine test cases */ | ||
| 1484 | static const SDLTest_TestCaseReference *stdlibTests[] = { | ||
| 1485 | &stdlibTest_strnlen, | ||
| 1486 | &stdlibTest_strlcpy, | ||
| 1487 | &stdlibTest_strstr, | ||
| 1488 | &stdlibTest_snprintf, | ||
| 1489 | &stdlibTest_swprintf, | ||
| 1490 | &stdlibTest_getsetenv, | ||
| 1491 | &stdlibTest_sscanf, | ||
| 1492 | &stdlibTest_aligned_alloc, | ||
| 1493 | &stdlibTestOverflow, | ||
| 1494 | &stdlibTest_iconv, | ||
| 1495 | &stdlibTest_strpbrk, | ||
| 1496 | &stdlibTest_wcstol, | ||
| 1497 | &stdlibTest_strtox, | ||
| 1498 | &stdlibTest_strtod, | ||
| 1499 | NULL | ||
| 1500 | }; | ||
| 1501 | |||
| 1502 | /* Standard C routine test suite (global) */ | ||
| 1503 | SDLTest_TestSuiteReference stdlibTestSuite = { | ||
| 1504 | "Stdlib", | ||
| 1505 | NULL, | ||
| 1506 | stdlibTests, | ||
| 1507 | NULL | ||
| 1508 | }; | ||
