| 1 |
/** |
| 2 |
* Standard C library routine test suite |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
|
| 7 |
#include "SDL.h" |
| 8 |
#include "SDL_config.h" |
| 9 |
#include "SDL_test.h" |
| 10 |
|
| 11 |
/* Compile-time assertions: even better than runtime assertions! */ |
| 12 |
SDL_COMPILE_TIME_ASSERT(SIZEOF_VOIDP, SIZEOF_VOIDP == sizeof(void *)); |
| 13 |
|
| 14 |
/* Test case functions */ |
| 15 |
|
| 16 |
/** |
| 17 |
* @brief Call to SDL_strlcpy |
| 18 |
*/ |
| 19 |
#undef SDL_strlcpy |
| 20 |
int stdlib_strlcpy(void *arg) |
| 21 |
{ |
| 22 |
size_t result; |
| 23 |
char text[1024]; |
| 24 |
const char *expected; |
| 25 |
|
| 26 |
result = SDL_strlcpy(text, "foo", sizeof(text)); |
| 27 |
expected = "foo"; |
| 28 |
SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")"); |
| 29 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 30 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), (int)result); |
| 31 |
|
| 32 |
result = SDL_strlcpy(text, "foo", 2); |
| 33 |
expected = "f"; |
| 34 |
SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2"); |
| 35 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 36 |
SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int)result); |
| 37 |
|
| 38 |
return TEST_COMPLETED; |
| 39 |
} |
| 40 |
|
| 41 |
#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS) |
| 42 |
#pragma GCC diagnostic push |
| 43 |
#if defined(HAVE_WFORMAT) |
| 44 |
#pragma GCC diagnostic ignored "-Wformat" |
| 45 |
#endif |
| 46 |
#if defined(HAVE_WFORMAT_EXTRA_ARGS) |
| 47 |
#pragma GCC diagnostic ignored "-Wformat-extra-args" |
| 48 |
#endif |
| 49 |
#endif |
| 50 |
|
| 51 |
/** |
| 52 |
* @brief Call to SDL_snprintf |
| 53 |
*/ |
| 54 |
#undef SDL_snprintf |
| 55 |
int stdlib_snprintf(void *arg) |
| 56 |
{ |
| 57 |
int result; |
| 58 |
int predicted; |
| 59 |
char text[1024]; |
| 60 |
const char *expected, *expected2, *expected3, *expected4, *expected5; |
| 61 |
size_t size; |
| 62 |
|
| 63 |
result = SDL_snprintf(text, sizeof(text), "%s", "foo"); |
| 64 |
expected = "foo"; |
| 65 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")"); |
| 66 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 67 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 68 |
|
| 69 |
result = SDL_snprintf(text, sizeof(text), "%10sA", "foo"); |
| 70 |
expected = " fooA"; |
| 71 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%10sA\", \"foo\")"); |
| 72 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 73 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 74 |
|
| 75 |
result = SDL_snprintf(text, sizeof(text), "%-10sA", "foo"); |
| 76 |
expected = "foo A"; |
| 77 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%-10sA\", \"foo\")"); |
| 78 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 79 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 80 |
|
| 81 |
result = SDL_snprintf(text, sizeof(text), "%S", L"foo"); |
| 82 |
expected = "foo"; |
| 83 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%S\", \"foo\")"); |
| 84 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 85 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 86 |
|
| 87 |
result = SDL_snprintf(text, sizeof(text), "%ls", L"foo"); |
| 88 |
expected = "foo"; |
| 89 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%ls\", \"foo\")"); |
| 90 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 91 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 92 |
|
| 93 |
result = SDL_snprintf(text, 2, "%s", "foo"); |
| 94 |
expected = "f"; |
| 95 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2"); |
| 96 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 97 |
SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result); |
| 98 |
|
| 99 |
result = SDL_snprintf(NULL, 0, "%s", "foo"); |
| 100 |
SDLTest_AssertPass("Call to SDL_snprintf(NULL, 0, \"%%s\", \"foo\")"); |
| 101 |
SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result); |
| 102 |
|
| 103 |
result = SDL_snprintf(text, 2, "%s\n", "foo"); |
| 104 |
expected = "f"; |
| 105 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\\n\", \"foo\") with buffer size 2"); |
| 106 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 107 |
SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", result); |
| 108 |
|
| 109 |
result = SDL_snprintf(text, sizeof(text), "%f", 0.0); |
| 110 |
predicted = SDL_snprintf(NULL, 0, "%f", 0.0); |
| 111 |
expected = "0.000000"; |
| 112 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 0.0)"); |
| 113 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 114 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 115 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 116 |
|
| 117 |
result = SDL_snprintf(text, sizeof(text), "%f", 1.0); |
| 118 |
predicted = SDL_snprintf(NULL, 0, "%f", 1.0); |
| 119 |
expected = "1.000000"; |
| 120 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)"); |
| 121 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 122 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 123 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 124 |
|
| 125 |
result = SDL_snprintf(text, sizeof(text), "%.f", 1.0); |
| 126 |
predicted = SDL_snprintf(NULL, 0, "%.f", 1.0); |
| 127 |
expected = "1"; |
| 128 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)"); |
| 129 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 130 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 131 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 132 |
|
| 133 |
result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0); |
| 134 |
predicted = SDL_snprintf(NULL, 0, "%#.f", 1.0); |
| 135 |
expected = "1."; |
| 136 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)"); |
| 137 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 138 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 139 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 140 |
|
| 141 |
result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0); |
| 142 |
predicted = SDL_snprintf(NULL, 0, "%f", 1.0 + 1.0 / 3.0); |
| 143 |
expected = "1.333333"; |
| 144 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)"); |
| 145 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 146 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 147 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 148 |
|
| 149 |
result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0); |
| 150 |
predicted = SDL_snprintf(NULL, 0, "%+f", 1.0 + 1.0 / 3.0); |
| 151 |
expected = "+1.333333"; |
| 152 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)"); |
| 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 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 156 |
|
| 157 |
result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0); |
| 158 |
predicted = SDL_snprintf(NULL, 0, "%.2f", 1.0 + 1.0 / 3.0); |
| 159 |
expected = "1.33"; |
| 160 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)"); |
| 161 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text); |
| 162 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 163 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 164 |
|
| 165 |
result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0); |
| 166 |
predicted = SDL_snprintf(NULL, 0, "%6.2f", 1.0 + 1.0 / 3.0); |
| 167 |
expected = " 1.33"; |
| 168 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)"); |
| 169 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text); |
| 170 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 171 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 172 |
|
| 173 |
result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0); |
| 174 |
predicted = SDL_snprintf(NULL, 0, "%06.2f", 1.0 + 1.0 / 3.0); |
| 175 |
expected = "001.33"; |
| 176 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)"); |
| 177 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text); |
| 178 |
SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result); |
| 179 |
SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted); |
| 180 |
|
| 181 |
result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0); |
| 182 |
expected = "001."; |
| 183 |
SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5"); |
| 184 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text); |
| 185 |
SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result); |
| 186 |
|
| 187 |
size = 64; |
| 188 |
result = SDL_snprintf(text, sizeof(text), "%zu %s", size, "test"); |
| 189 |
expected = "64 test"; |
| 190 |
SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%zu %%s\", size, \"test\")"); |
| 191 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text); |
| 192 |
SDLTest_AssertCheck(result == 7, "Check result value, expected: 7, got: %d", result); |
| 193 |
|
| 194 |
result = SDL_snprintf(text, sizeof(text), "%p", (void *)0x1234abcd); |
| 195 |
expected = "0x1234abcd"; |
| 196 |
expected2 = "1234ABCD"; |
| 197 |
expected3 = "000000001234ABCD"; |
| 198 |
expected4 = "1234abcd"; |
| 199 |
expected5 = "000000001234abcd"; |
| 200 |
SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%p\", 0x1234abcd)"); |
| 201 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 || |
| 202 |
SDL_strcmp(text, expected2) == 0 || |
| 203 |
SDL_strcmp(text, expected3) == 0 || |
| 204 |
SDL_strcmp(text, expected4) == 0 || |
| 205 |
SDL_strcmp(text, expected5) == 0, |
| 206 |
"Check text, expected: '%s', got: '%s'", expected, text); |
| 207 |
SDLTest_AssertCheck(result == SDL_strlen(expected) || |
| 208 |
result == SDL_strlen(expected2) || |
| 209 |
result == SDL_strlen(expected3) || |
| 210 |
result == SDL_strlen(expected4) || |
| 211 |
result == SDL_strlen(expected5), |
| 212 |
"Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result); |
| 213 |
|
| 214 |
result = SDL_snprintf(text, sizeof(text), "A %p B", (void *)0x1234abcd); |
| 215 |
expected = "A 0x1234abcd B"; |
| 216 |
expected2 = "A 1234ABCD B"; |
| 217 |
expected3 = "A 000000001234ABCD B"; |
| 218 |
expected4 = "A 1234abcd B"; |
| 219 |
expected5 = "A 000000001234abcd B"; |
| 220 |
SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"A %%p B\", 0x1234abcd)"); |
| 221 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 || |
| 222 |
SDL_strcmp(text, expected2) == 0 || |
| 223 |
SDL_strcmp(text, expected3) == 0 || |
| 224 |
SDL_strcmp(text, expected4) == 0 || |
| 225 |
SDL_strcmp(text, expected5) == 0, |
| 226 |
"Check text, expected: '%s', got: '%s'", expected, text); |
| 227 |
SDLTest_AssertCheck(result == SDL_strlen(expected) || |
| 228 |
result == SDL_strlen(expected2) || |
| 229 |
result == SDL_strlen(expected3) || |
| 230 |
result == SDL_strlen(expected4) || |
| 231 |
result == SDL_strlen(expected5), |
| 232 |
"Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result); |
| 233 |
|
| 234 |
if (sizeof(void *) >= 8) { |
| 235 |
result = SDL_snprintf(text, sizeof(text), "%p", (void *)0x1ba07bddf60L); |
| 236 |
expected = "0x1ba07bddf60"; |
| 237 |
expected2 = "000001BA07BDDF60"; |
| 238 |
expected3 = "000001ba07bddf60"; |
| 239 |
SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%p\", 0x1ba07bddf60)"); |
| 240 |
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 || |
| 241 |
SDL_strcmp(text, expected2) == 0 || |
| 242 |
SDL_strcmp(text, expected3) == 0, |
| 243 |
"Check text, expected: '%s', got: '%s'", expected, text); |
| 244 |
SDLTest_AssertCheck(result == SDL_strlen(expected) || |
| 245 |
result == SDL_strlen(expected2) || |
| 246 |
result == SDL_strlen(expected3), |
| 247 |
"Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result); |
| 248 |
} |
| 249 |
return TEST_COMPLETED; |
| 250 |
} |
| 251 |
|
| 252 |
#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS) |
| 253 |
#pragma GCC diagnostic pop |
| 254 |
#endif |
| 255 |
|
| 256 |
/** |
| 257 |
* @brief Call to SDL_getenv and SDL_setenv |
| 258 |
*/ |
| 259 |
int stdlib_getsetenv(void *arg) |
| 260 |
{ |
| 261 |
const int nameLen = 16; |
| 262 |
char name[17]; |
| 263 |
int counter; |
| 264 |
int result; |
| 265 |
char *value1; |
| 266 |
char *value2; |
| 267 |
char *expected; |
| 268 |
int overwrite; |
| 269 |
char *text; |
| 270 |
|
| 271 |
/* Create a random name. This tests SDL_getenv, since we need to */ |
| 272 |
/* make sure the variable is not set yet (it shouldn't). */ |
| 273 |
do { |
| 274 |
for (counter = 0; counter < nameLen; counter++) { |
| 275 |
name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90); |
| 276 |
} |
| 277 |
name[nameLen] = '\0'; |
| 278 |
|
| 279 |
text = SDL_getenv(name); |
| 280 |
SDLTest_AssertPass("Call to SDL_getenv('%s')", name); |
| 281 |
if (text) { |
| 282 |
SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, (int)SDL_strlen(text)); |
| 283 |
} |
| 284 |
} while (text); |
| 285 |
|
| 286 |
/* Create random values to set */ |
| 287 |
value1 = SDLTest_RandomAsciiStringOfSize(10); |
| 288 |
value2 = SDLTest_RandomAsciiStringOfSize(10); |
| 289 |
|
| 290 |
/* Set value 1 without overwrite */ |
| 291 |
overwrite = 0; |
| 292 |
expected = value1; |
| 293 |
result = SDL_setenv(name, value1, overwrite); |
| 294 |
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite); |
| 295 |
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result); |
| 296 |
|
| 297 |
/* Check value */ |
| 298 |
text = SDL_getenv(name); |
| 299 |
SDLTest_AssertPass("Call to SDL_getenv('%s')", name); |
| 300 |
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL"); |
| 301 |
if (text != NULL) { |
| 302 |
SDLTest_AssertCheck( |
| 303 |
SDL_strcmp(text, expected) == 0, |
| 304 |
"Verify returned text, expected: %s, got: %s", |
| 305 |
expected, |
| 306 |
text); |
| 307 |
} |
| 308 |
|
| 309 |
/* Set value 2 with overwrite */ |
| 310 |
overwrite = 1; |
| 311 |
expected = value2; |
| 312 |
result = SDL_setenv(name, value2, overwrite); |
| 313 |
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite); |
| 314 |
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result); |
| 315 |
|
| 316 |
/* Check value */ |
| 317 |
text = SDL_getenv(name); |
| 318 |
SDLTest_AssertPass("Call to SDL_getenv('%s')", name); |
| 319 |
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL"); |
| 320 |
if (text != NULL) { |
| 321 |
SDLTest_AssertCheck( |
| 322 |
SDL_strcmp(text, expected) == 0, |
| 323 |
"Verify returned text, expected: %s, got: %s", |
| 324 |
expected, |
| 325 |
text); |
| 326 |
} |
| 327 |
|
| 328 |
/* Set value 1 without overwrite */ |
| 329 |
overwrite = 0; |
| 330 |
expected = value2; |
| 331 |
result = SDL_setenv(name, value1, overwrite); |
| 332 |
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite); |
| 333 |
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result); |
| 334 |
|
| 335 |
/* Check value */ |
| 336 |
text = SDL_getenv(name); |
| 337 |
SDLTest_AssertPass("Call to SDL_getenv('%s')", name); |
| 338 |
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL"); |
| 339 |
if (text != NULL) { |
| 340 |
SDLTest_AssertCheck( |
| 341 |
SDL_strcmp(text, expected) == 0, |
| 342 |
"Verify returned text, expected: %s, got: %s", |
| 343 |
expected, |
| 344 |
text); |
| 345 |
} |
| 346 |
|
| 347 |
/* Set value 1 without overwrite */ |
| 348 |
overwrite = 1; |
| 349 |
expected = value1; |
| 350 |
result = SDL_setenv(name, value1, overwrite); |
| 351 |
SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite); |
| 352 |
SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result); |
| 353 |
|
| 354 |
/* Check value */ |
| 355 |
text = SDL_getenv(name); |
| 356 |
SDLTest_AssertPass("Call to SDL_getenv('%s')", name); |
| 357 |
SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL"); |
| 358 |
if (text != NULL) { |
| 359 |
SDLTest_AssertCheck( |
| 360 |
SDL_strcmp(text, expected) == 0, |
| 361 |
"Verify returned text, expected: %s, got: %s", |
| 362 |
expected, |
| 363 |
text); |
| 364 |
} |
| 365 |
|
| 366 |
/* Negative cases */ |
| 367 |
for (overwrite = 0; overwrite <= 1; overwrite++) { |
| 368 |
result = SDL_setenv(NULL, value1, overwrite); |
| 369 |
SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite); |
| 370 |
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result); |
| 371 |
result = SDL_setenv("", value1, overwrite); |
| 372 |
SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite); |
| 373 |
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result); |
| 374 |
result = SDL_setenv("=", value1, overwrite); |
| 375 |
SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite); |
| 376 |
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result); |
| 377 |
result = SDL_setenv(name, NULL, overwrite); |
| 378 |
SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite); |
| 379 |
SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result); |
| 380 |
} |
| 381 |
|
| 382 |
/* Clean up */ |
| 383 |
SDL_free(value1); |
| 384 |
SDL_free(value2); |
| 385 |
|
| 386 |
return TEST_COMPLETED; |
| 387 |
} |
| 388 |
|
| 389 |
#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS) |
| 390 |
#pragma GCC diagnostic push |
| 391 |
#if defined(HAVE_WFORMAT) |
| 392 |
#pragma GCC diagnostic ignored "-Wformat" |
| 393 |
#endif |
| 394 |
#if defined(HAVE_WFORMAT_EXTRA_ARGS) |
| 395 |
#pragma GCC diagnostic ignored "-Wformat-extra-args" |
| 396 |
#endif |
| 397 |
#endif |
| 398 |
|
| 399 |
#define FMT_PRILLd "%lld" |
| 400 |
#define FMT_PRILLdn "%lld%lln" |
| 401 |
#define FMT_PRILLu "%llu" |
| 402 |
|
| 403 |
/** |
| 404 |
* @brief Call to SDL_sscanf |
| 405 |
*/ |
| 406 |
#undef SDL_sscanf |
| 407 |
int stdlib_sscanf(void *arg) |
| 408 |
{ |
| 409 |
int output; |
| 410 |
int result; |
| 411 |
int length; |
| 412 |
int expected_output; |
| 413 |
int expected_result; |
| 414 |
short short_output, expected_short_output, short_length; |
| 415 |
long long_output, expected_long_output, long_length; |
| 416 |
long long long_long_output, expected_long_long_output, long_long_length; |
| 417 |
size_t size_output, expected_size_output; |
| 418 |
char text[128], text2[128]; |
| 419 |
unsigned int r = 0, g = 0, b = 0; |
| 420 |
|
| 421 |
expected_output = output = 123; |
| 422 |
expected_result = -1; |
| 423 |
result = SDL_sscanf("", "%i", &output); |
| 424 |
SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)"); |
| 425 |
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output); |
| 426 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 427 |
|
| 428 |
expected_output = output = 123; |
| 429 |
expected_result = 0; |
| 430 |
result = SDL_sscanf("a", "%i", &output); |
| 431 |
SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)"); |
| 432 |
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output); |
| 433 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 434 |
|
| 435 |
output = 123; |
| 436 |
length = 0; |
| 437 |
expected_output = 2; |
| 438 |
expected_result = 1; |
| 439 |
result = SDL_sscanf("2", "%i%n", &output, &length); |
| 440 |
SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i%%n\", &output, &length)"); |
| 441 |
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output); |
| 442 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 443 |
SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length); |
| 444 |
|
| 445 |
output = 123; |
| 446 |
length = 0; |
| 447 |
expected_output = 0xa; |
| 448 |
expected_result = 1; |
| 449 |
result = SDL_sscanf("aa", "%1x%n", &output, &length); |
| 450 |
SDLTest_AssertPass("Call to SDL_sscanf(\"aa\", \"%%1x%%n\", &output, &length)"); |
| 451 |
SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output); |
| 452 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 453 |
SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length); |
| 454 |
|
| 455 |
expected_result = 3; |
| 456 |
result = SDL_sscanf("#026", "#%1x%1x%1x", &r, &g, &b); |
| 457 |
SDLTest_AssertPass("Call to SDL_sscanf(\"#026\", \"#%%1x%%1x%%1x\", &r, &g, &b)"); |
| 458 |
expected_output = 0; |
| 459 |
SDLTest_AssertCheck(r == expected_output, "Check output for r, expected: %i, got: %i", expected_output, r); |
| 460 |
expected_output = 2; |
| 461 |
SDLTest_AssertCheck(g == expected_output, "Check output for g, expected: %i, got: %i", expected_output, g); |
| 462 |
expected_output = 6; |
| 463 |
SDLTest_AssertCheck(b == expected_output, "Check output for b, expected: %i, got: %i", expected_output, b); |
| 464 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 465 |
|
| 466 |
#define SIZED_TEST_CASE(type, var, printf_specifier, scanf_specifier) \ |
| 467 |
var##_output = 123; \ |
| 468 |
var##_length = 0; \ |
| 469 |
expected_##var##_output = (type)(((unsigned type)(~0)) >> 1); \ |
| 470 |
expected_result = 1; \ |
| 471 |
result = SDL_snprintf(text, sizeof(text), printf_specifier, expected_##var##_output); \ |
| 472 |
result = SDL_sscanf(text, scanf_specifier, &var##_output, &var##_length); \ |
| 473 |
SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", %s, &output, &length)", text, #scanf_specifier); \ |
| 474 |
SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " printf_specifier ", got: " printf_specifier, expected_##var##_output, var##_output); \ |
| 475 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \ |
| 476 |
SDLTest_AssertCheck(var##_length == (type)SDL_strlen(text), "Check length, expected: %i, got: %i", (int)SDL_strlen(text), (int)var##_length); \ |
| 477 |
\ |
| 478 |
var##_output = 123; \ |
| 479 |
var##_length = 0; \ |
| 480 |
expected_##var##_output = ~(type)(((unsigned type)(~0)) >> 1); \ |
| 481 |
expected_result = 1; \ |
| 482 |
result = SDL_snprintf(text, sizeof(text), printf_specifier, expected_##var##_output); \ |
| 483 |
result = SDL_sscanf(text, scanf_specifier, &var##_output, &var##_length); \ |
| 484 |
SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", %s, &output, &length)", text, #scanf_specifier); \ |
| 485 |
SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " printf_specifier ", got: " printf_specifier, expected_##var##_output, var##_output); \ |
| 486 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \ |
| 487 |
SDLTest_AssertCheck(var##_length == (type)SDL_strlen(text), "Check length, expected: %i, got: %i", (int)SDL_strlen(text), (int)var##_length); \ |
| 488 |
|
| 489 |
SIZED_TEST_CASE(short, short, "%hd", "%hd%hn") |
| 490 |
SIZED_TEST_CASE(long, long, "%ld", "%ld%ln") |
| 491 |
SIZED_TEST_CASE(long long, long_long, FMT_PRILLd, FMT_PRILLdn) |
| 492 |
|
| 493 |
size_output = 123; |
| 494 |
expected_size_output = ~((size_t)0); |
| 495 |
expected_result = 1; |
| 496 |
result = SDL_snprintf(text, sizeof(text), "%zu", expected_size_output); |
| 497 |
result = SDL_sscanf(text, "%zu", &size_output); |
| 498 |
SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%zu\", &output)", text); |
| 499 |
SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output); |
| 500 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 501 |
|
| 502 |
expected_result = 1; |
| 503 |
text[0] = '\0'; |
| 504 |
result = SDL_sscanf("abc def", "%s", text); |
| 505 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%s\", text)"); |
| 506 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); |
| 507 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 508 |
|
| 509 |
expected_result = 1; |
| 510 |
text[0] = '\0'; |
| 511 |
result = SDL_sscanf("abc,def", "%s", text); |
| 512 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%s\", text)"); |
| 513 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc,def") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); |
| 514 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 515 |
|
| 516 |
expected_result = 1; |
| 517 |
text[0] = '\0'; |
| 518 |
result = SDL_sscanf("abc,def", "%[cba]", text); |
| 519 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[cba]\", text)"); |
| 520 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); |
| 521 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 522 |
|
| 523 |
expected_result = 1; |
| 524 |
text[0] = '\0'; |
| 525 |
result = SDL_sscanf("abc,def", "%[a-z]", text); |
| 526 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[z-a]\", text)"); |
| 527 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); |
| 528 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 529 |
|
| 530 |
expected_result = 1; |
| 531 |
text[0] = '\0'; |
| 532 |
result = SDL_sscanf("abc,def", "%[^,]", text); |
| 533 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[^,]\", text)"); |
| 534 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); |
| 535 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 536 |
|
| 537 |
expected_result = 0; |
| 538 |
text[0] = '\0'; |
| 539 |
result = SDL_sscanf("abc,def", "%[A-Z]", text); |
| 540 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[A-Z]\", text)"); |
| 541 |
SDLTest_AssertCheck(SDL_strcmp(text, "") == 0, "Check output, expected: \"\", got: \"%s\"", text); |
| 542 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 543 |
|
| 544 |
expected_result = 2; |
| 545 |
text[0] = '\0'; |
| 546 |
text2[0] = '\0'; |
| 547 |
result = SDL_sscanf("abc,def", "%[abc],%[def]", text, text2); |
| 548 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc],%%[def]\", text)"); |
| 549 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); |
| 550 |
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2); |
| 551 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 552 |
|
| 553 |
expected_result = 2; |
| 554 |
text[0] = '\0'; |
| 555 |
text2[0] = '\0'; |
| 556 |
result = SDL_sscanf("abc,def", "%[abc]%*[,]%[def]", text, text2); |
| 557 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc]%%*[,]%%[def]\", text)"); |
| 558 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); |
| 559 |
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2); |
| 560 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 561 |
|
| 562 |
expected_result = 2; |
| 563 |
text[0] = '\0'; |
| 564 |
text2[0] = '\0'; |
| 565 |
result = SDL_sscanf("abc def", "%[abc] %[def]", text, text2); |
| 566 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%[abc] %%[def]\", text)"); |
| 567 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text); |
| 568 |
SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2); |
| 569 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 570 |
|
| 571 |
expected_result = 1; |
| 572 |
text[0] = '\0'; |
| 573 |
result = SDL_sscanf("abc123XYZ", "%[a-zA-Z0-9]", text); |
| 574 |
SDLTest_AssertPass("Call to SDL_sscanf(\"abc123XYZ\", \"%%[a-zA-Z0-9]\", text)"); |
| 575 |
SDLTest_AssertCheck(SDL_strcmp(text, "abc123XYZ") == 0, "Check output, expected: \"abc123XYZ\", got: \"%s\"", text); |
| 576 |
SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); |
| 577 |
|
| 578 |
return TEST_COMPLETED; |
| 579 |
} |
| 580 |
|
| 581 |
#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS) |
| 582 |
#pragma GCC diagnostic pop |
| 583 |
#endif |
| 584 |
|
| 585 |
#if defined(_WIN64) |
| 586 |
#define SIZE_FORMAT "I64u" |
| 587 |
#elif defined(__WIN32__) |
| 588 |
#define SIZE_FORMAT "I32u" |
| 589 |
#else |
| 590 |
#define SIZE_FORMAT "zu" |
| 591 |
#endif |
| 592 |
|
| 593 |
typedef struct |
| 594 |
{ |
| 595 |
size_t a; |
| 596 |
size_t b; |
| 597 |
size_t result; |
| 598 |
int status; |
| 599 |
} overflow_test; |
| 600 |
|
| 601 |
static const overflow_test multiplications[] = { |
| 602 |
{ 1, 1, 1, 0 }, |
| 603 |
{ 0, 0, 0, 0 }, |
| 604 |
{ SDL_SIZE_MAX, 0, 0, 0 }, |
| 605 |
{ SDL_SIZE_MAX, 1, SDL_SIZE_MAX, 0 }, |
| 606 |
{ SDL_SIZE_MAX / 2, 2, SDL_SIZE_MAX - (SDL_SIZE_MAX % 2), 0 }, |
| 607 |
{ SDL_SIZE_MAX / 23, 23, SDL_SIZE_MAX - (SDL_SIZE_MAX % 23), 0 }, |
| 608 |
|
| 609 |
{ (SDL_SIZE_MAX / 2) + 1, 2, 0, -1 }, |
| 610 |
{ (SDL_SIZE_MAX / 23) + 42, 23, 0, -1 }, |
| 611 |
{ SDL_SIZE_MAX, SDL_SIZE_MAX, 0, -1 }, |
| 612 |
}; |
| 613 |
|
| 614 |
static const overflow_test additions[] = { |
| 615 |
{ 1, 1, 2, 0 }, |
| 616 |
{ 0, 0, 0, 0 }, |
| 617 |
{ SDL_SIZE_MAX, 0, SDL_SIZE_MAX, 0 }, |
| 618 |
{ SDL_SIZE_MAX - 1, 1, SDL_SIZE_MAX, 0 }, |
| 619 |
{ SDL_SIZE_MAX - 42, 23, SDL_SIZE_MAX - (42 - 23), 0 }, |
| 620 |
|
| 621 |
{ SDL_SIZE_MAX, 1, 0, -1 }, |
| 622 |
{ SDL_SIZE_MAX, 23, 0, -1 }, |
| 623 |
{ SDL_SIZE_MAX, SDL_SIZE_MAX, 0, -1 }, |
| 624 |
}; |
| 625 |
|
| 626 |
static int |
| 627 |
stdlib_overflow(void *arg) |
| 628 |
{ |
| 629 |
size_t i; |
| 630 |
size_t useBuiltin; |
| 631 |
|
| 632 |
for (useBuiltin = 0; useBuiltin < 2; useBuiltin++) { |
| 633 |
if (useBuiltin) { |
| 634 |
SDLTest_Log("Using gcc/clang builtins if possible"); |
| 635 |
} else { |
| 636 |
SDLTest_Log("Not using gcc/clang builtins"); |
| 637 |
} |
| 638 |
|
| 639 |
for (i = 0; i < SDL_arraysize(multiplications); i++) { |
| 640 |
const overflow_test *t = &multiplications[i]; |
| 641 |
int status; |
| 642 |
size_t result = ~t->result; |
| 643 |
|
| 644 |
if (useBuiltin) { |
| 645 |
status = SDL_size_mul_overflow(t->a, t->b, &result); |
| 646 |
} else { |
| 647 |
/* This disables the macro that tries to use a gcc/clang |
| 648 |
* builtin, so we test the fallback implementation instead. */ |
| 649 |
status = (SDL_size_mul_overflow)(t->a, t->b, &result); |
| 650 |
} |
| 651 |
|
| 652 |
if (t->status == 0) { |
| 653 |
SDLTest_AssertCheck(status == 0, |
| 654 |
"(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed", |
| 655 |
t->a, t->b); |
| 656 |
SDLTest_AssertCheck(result == t->result, |
| 657 |
"(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT, |
| 658 |
t->a, t->b, t->result, result); |
| 659 |
} else { |
| 660 |
SDLTest_AssertCheck(status == -1, |
| 661 |
"(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail", |
| 662 |
t->a, t->b); |
| 663 |
} |
| 664 |
|
| 665 |
if (t->a == t->b) { |
| 666 |
continue; |
| 667 |
} |
| 668 |
|
| 669 |
result = ~t->result; |
| 670 |
|
| 671 |
if (useBuiltin) { |
| 672 |
status = SDL_size_mul_overflow(t->b, t->a, &result); |
| 673 |
} else { |
| 674 |
status = (SDL_size_mul_overflow)(t->b, t->a, &result); |
| 675 |
} |
| 676 |
|
| 677 |
if (t->status == 0) { |
| 678 |
SDLTest_AssertCheck(status == 0, |
| 679 |
"(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed", |
| 680 |
t->b, t->a); |
| 681 |
SDLTest_AssertCheck(result == t->result, |
| 682 |
"(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT, |
| 683 |
t->b, t->a, t->result, result); |
| 684 |
} else { |
| 685 |
SDLTest_AssertCheck(status == -1, |
| 686 |
"(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail", |
| 687 |
t->b, t->a); |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
for (i = 0; i < SDL_arraysize(additions); i++) { |
| 692 |
const overflow_test *t = &additions[i]; |
| 693 |
int status; |
| 694 |
size_t result = ~t->result; |
| 695 |
|
| 696 |
if (useBuiltin) { |
| 697 |
status = SDL_size_add_overflow(t->a, t->b, &result); |
| 698 |
} else { |
| 699 |
status = (SDL_size_add_overflow)(t->a, t->b, &result); |
| 700 |
} |
| 701 |
|
| 702 |
if (t->status == 0) { |
| 703 |
SDLTest_AssertCheck(status == 0, |
| 704 |
"(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed", |
| 705 |
t->a, t->b); |
| 706 |
SDLTest_AssertCheck(result == t->result, |
| 707 |
"(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT, |
| 708 |
t->a, t->b, t->result, result); |
| 709 |
} else { |
| 710 |
SDLTest_AssertCheck(status == -1, |
| 711 |
"(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail", |
| 712 |
t->a, t->b); |
| 713 |
} |
| 714 |
|
| 715 |
if (t->a == t->b) { |
| 716 |
continue; |
| 717 |
} |
| 718 |
|
| 719 |
result = ~t->result; |
| 720 |
|
| 721 |
if (useBuiltin) { |
| 722 |
status = SDL_size_add_overflow(t->b, t->a, &result); |
| 723 |
} else { |
| 724 |
status = (SDL_size_add_overflow)(t->b, t->a, &result); |
| 725 |
} |
| 726 |
|
| 727 |
if (t->status == 0) { |
| 728 |
SDLTest_AssertCheck(status == 0, |
| 729 |
"(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed", |
| 730 |
t->b, t->a); |
| 731 |
SDLTest_AssertCheck(result == t->result, |
| 732 |
"(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT, |
| 733 |
t->b, t->a, t->result, result); |
| 734 |
} else { |
| 735 |
SDLTest_AssertCheck(status == -1, |
| 736 |
"(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail", |
| 737 |
t->b, t->a); |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
return TEST_COMPLETED; |
| 743 |
} |
| 744 |
|
| 745 |
static int |
| 746 |
stdlib_strtox(void *arg) |
| 747 |
{ |
| 748 |
const unsigned long long ullong_max = ~0ULL; |
| 749 |
|
| 750 |
#define STRTOX_TEST_CASE(func_name, type, format_spec, str, base, expected_result, expected_endp_offset) do { \ |
| 751 |
const char *s = str; \ |
| 752 |
type r, expected_r = expected_result; \ |
| 753 |
char *ep, *expected_ep = (char *)s + expected_endp_offset; \ |
| 754 |
r = func_name(s, &ep, base); \ |
| 755 |
SDLTest_AssertPass("Call to " #func_name "(" #str ", &endp, " #base ")"); \ |
| 756 |
SDLTest_AssertCheck(r == expected_r, "Check result value, expected: " format_spec ", got: " format_spec, expected_r, r); \ |
| 757 |
SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \ |
| 758 |
} while (0) |
| 759 |
|
| 760 |
// infer decimal |
| 761 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "\t 123abcxyz", 0, 123, 6); // skip leading space |
| 762 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "+123abcxyz", 0, 123, 4); |
| 763 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "+123abcxyz", 0, 123, 4); |
| 764 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-123abcxyz", 0, -123, 4); |
| 765 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "9999999999999999999999999999999999999999abcxyz", 0, ullong_max, 40); |
| 766 |
|
| 767 |
// infer hexadecimal |
| 768 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0x123abcxyz", 0, 0x123abc, 8); |
| 769 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0X123ABCXYZ", 0, 0x123abc, 8); // uppercase X |
| 770 |
|
| 771 |
// infer octal |
| 772 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0123abcxyz", 0, 0123, 4); |
| 773 |
|
| 774 |
// arbitrary bases |
| 775 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "00110011", 2, 51, 8); |
| 776 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-uvwxyz", 32, -991, 3); |
| 777 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "ZzZzZzZzZzZzZzZzZzZzZzZzZ", 36, ullong_max, 25); |
| 778 |
|
| 779 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0", 0, 0, 1); |
| 780 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0", 10, 0, 1); |
| 781 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-0", 0, 0, 2); |
| 782 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-0", 10, 0, 2); |
| 783 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, " - 1", 0, 0, 0); // invalid input |
| 784 |
|
| 785 |
// We know that SDL_strtol, SDL_strtoul and SDL_strtoll share the same code path as SDL_strtoull under the hood, |
| 786 |
// so the most interesting test cases are those close to the bounds of the integer type. |
| 787 |
|
| 788 |
// For simplicity, we only run long/long long tests when they are 32-bit/64-bit, respectively. |
| 789 |
// Suppressing warnings would be difficult otherwise. |
| 790 |
// Since the CI runs the tests against a variety of targets, this should be fine in practice. |
| 791 |
|
| 792 |
if (sizeof(long) == 4) { |
| 793 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "0", 0, 0, 1); |
| 794 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "0", 10, 0, 1); |
| 795 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-0", 0, 0, 2); |
| 796 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-0", 10, 0, 2); |
| 797 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "2147483647", 10, 2147483647, 10); |
| 798 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "2147483648", 10, 2147483647, 10); |
| 799 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-2147483648", 10, -2147483647L - 1, 11); |
| 800 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-2147483649", 10, -2147483647L - 1, 11); |
| 801 |
STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-9999999999999999999999999999999999999999", 10, -2147483647L - 1, 41); |
| 802 |
|
| 803 |
STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "4294967295", 10, 4294967295UL, 10); |
| 804 |
STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "4294967296", 10, 4294967295UL, 10); |
| 805 |
STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "-4294967295", 10, 1, 11); |
| 806 |
} |
| 807 |
|
| 808 |
if (sizeof(long long) == 8) { |
| 809 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "0", 0, 0LL, 1); |
| 810 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "0", 10, 0LL, 1); |
| 811 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-0", 0, 0LL, 2); |
| 812 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-0", 10, 0LL, 2); |
| 813 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "9223372036854775807", 10, 9223372036854775807LL, 19); |
| 814 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "9223372036854775808", 10, 9223372036854775807LL, 19); |
| 815 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9223372036854775808", 10, -9223372036854775807LL - 1, 20); |
| 816 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9223372036854775809", 10, -9223372036854775807LL - 1, 20); |
| 817 |
STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9999999999999999999999999999999999999999", 10, -9223372036854775807LL - 1, 41); |
| 818 |
|
| 819 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "18446744073709551615", 10, 18446744073709551615ULL, 20); |
| 820 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "18446744073709551616", 10, 18446744073709551615ULL, 20); |
| 821 |
STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "-18446744073709551615", 10, 1, 21); |
| 822 |
} |
| 823 |
|
| 824 |
#undef STRTOX_TEST_CASE |
| 825 |
|
| 826 |
return TEST_COMPLETED; |
| 827 |
} |
| 828 |
|
| 829 |
static int |
| 830 |
stdlib_strtod(void *arg) |
| 831 |
{ |
| 832 |
#define STRTOD_TEST_CASE(str, expected_result, expected_endp_offset) do { \ |
| 833 |
const char *s = str; \ |
| 834 |
double r, expected_r = expected_result; \ |
| 835 |
char *ep, *expected_ep = (char *)s + expected_endp_offset; \ |
| 836 |
r = SDL_strtod(s, &ep); \ |
| 837 |
SDLTest_AssertPass("Call to SDL_strtod(" #str ", &endp)"); \ |
| 838 |
SDLTest_AssertCheck(r == expected_r, "Check result value, expected: %f, got: %f", expected_r, r); \ |
| 839 |
SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \ |
| 840 |
} while (0) |
| 841 |
|
| 842 |
STRTOD_TEST_CASE("\t 123.75abcxyz", 123.75, 9); // skip leading space |
| 843 |
STRTOD_TEST_CASE("+999.555", 999.555, 8); |
| 844 |
STRTOD_TEST_CASE("-999.555", -999.555, 8); |
| 845 |
|
| 846 |
#undef STRTOD_TEST_CASE |
| 847 |
|
| 848 |
return TEST_COMPLETED; |
| 849 |
} |
| 850 |
|
| 851 |
/* ================= Test References ================== */ |
| 852 |
|
| 853 |
/* Standard C routine test cases */ |
| 854 |
static const SDLTest_TestCaseReference stdlibTest1 = { |
| 855 |
(SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED |
| 856 |
}; |
| 857 |
|
| 858 |
static const SDLTest_TestCaseReference stdlibTest2 = { |
| 859 |
(SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED |
| 860 |
}; |
| 861 |
|
| 862 |
static const SDLTest_TestCaseReference stdlibTest3 = { |
| 863 |
(SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED |
| 864 |
}; |
| 865 |
|
| 866 |
static const SDLTest_TestCaseReference stdlibTest4 = { |
| 867 |
(SDLTest_TestCaseFp)stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED |
| 868 |
}; |
| 869 |
|
| 870 |
static const SDLTest_TestCaseReference stdlibTestOverflow = { |
| 871 |
stdlib_overflow, "stdlib_overflow", "Overflow detection", TEST_ENABLED |
| 872 |
}; |
| 873 |
|
| 874 |
static const SDLTest_TestCaseReference stdlibTest_strtox = { |
| 875 |
stdlib_strtox, "stdlib_strtox", "Calls to SDL_strtol, SDL_strtoul, SDL_strtoll and SDL_strtoull", TEST_ENABLED |
| 876 |
}; |
| 877 |
|
| 878 |
static const SDLTest_TestCaseReference stdlibTest_strtod = { |
| 879 |
stdlib_strtod, "stdlib_strtod", "Calls to SDL_strtod", TEST_ENABLED |
| 880 |
}; |
| 881 |
|
| 882 |
/* Sequence of Standard C routine test cases */ |
| 883 |
static const SDLTest_TestCaseReference *stdlibTests[] = { |
| 884 |
&stdlibTest1, |
| 885 |
&stdlibTest2, |
| 886 |
&stdlibTest3, |
| 887 |
&stdlibTest4, |
| 888 |
&stdlibTestOverflow, |
| 889 |
&stdlibTest_strtox, |
| 890 |
&stdlibTest_strtod, |
| 891 |
NULL |
| 892 |
}; |
| 893 |
|
| 894 |
/* Standard C routine test suite (global) */ |
| 895 |
SDLTest_TestSuiteReference stdlibTestSuite = { |
| 896 |
"Stdlib", |
| 897 |
NULL, |
| 898 |
stdlibTests, |
| 899 |
NULL |
| 900 |
}; |