🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

612 lines
27 KiB

  1. /**
  2. * Standard C library routine test suite
  3. */
  4. #include <stdio.h>
  5. #include "SDL.h"
  6. #include "SDL_test.h"
  7. /* Test case functions */
  8. /**
  9. * @brief Call to SDL_strlcpy
  10. */
  11. #undef SDL_strlcpy
  12. int stdlib_strlcpy(void *arg)
  13. {
  14. size_t result;
  15. char text[1024];
  16. const char *expected;
  17. result = SDL_strlcpy(text, "foo", sizeof(text));
  18. expected = "foo";
  19. SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
  20. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  21. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), (int)result);
  22. result = SDL_strlcpy(text, "foo", 2);
  23. expected = "f";
  24. SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
  25. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  26. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int)result);
  27. return TEST_COMPLETED;
  28. }
  29. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  30. #pragma GCC diagnostic push
  31. #if defined(HAVE_WFORMAT)
  32. #pragma GCC diagnostic ignored "-Wformat"
  33. #endif
  34. #if defined(HAVE_WFORMAT_EXTRA_ARGS)
  35. #pragma GCC diagnostic ignored "-Wformat-extra-args"
  36. #endif
  37. #endif
  38. /**
  39. * @brief Call to SDL_snprintf
  40. */
  41. #undef SDL_snprintf
  42. int stdlib_snprintf(void *arg)
  43. {
  44. int result;
  45. int predicted;
  46. char text[1024];
  47. const char *expected;
  48. size_t size;
  49. result = SDL_snprintf(text, sizeof(text), "%s", "foo");
  50. expected = "foo";
  51. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
  52. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  53. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  54. result = SDL_snprintf(text, sizeof(text), "%S", L"foo");
  55. expected = "foo";
  56. SDLTest_AssertPass("Call to SDL_snprintf(\"%%S\", \"foo\")");
  57. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  58. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  59. result = SDL_snprintf(text, sizeof(text), "%ls", L"foo");
  60. expected = "foo";
  61. SDLTest_AssertPass("Call to SDL_snprintf(\"%%ls\", \"foo\")");
  62. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  63. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  64. result = SDL_snprintf(text, 2, "%s", "foo");
  65. expected = "f";
  66. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
  67. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  68. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  69. result = SDL_snprintf(NULL, 0, "%s", "foo");
  70. SDLTest_AssertPass("Call to SDL_snprintf(NULL, 0, \"%%s\", \"foo\")");
  71. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  72. result = SDL_snprintf(text, 2, "%s\n", "foo");
  73. expected = "f";
  74. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\\n\", \"foo\") with buffer size 2");
  75. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  76. SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", result);
  77. result = SDL_snprintf(text, sizeof(text), "%f", 0.0);
  78. predicted = SDL_snprintf(NULL, 0, "%f", 0.0);
  79. expected = "0.000000";
  80. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 0.0)");
  81. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  82. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  83. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  84. result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
  85. predicted = SDL_snprintf(NULL, 0, "%f", 1.0);
  86. expected = "1.000000";
  87. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
  88. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  89. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  90. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  91. result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
  92. predicted = SDL_snprintf(NULL, 0, "%.f", 1.0);
  93. expected = "1";
  94. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
  95. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  96. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  97. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  98. result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
  99. predicted = SDL_snprintf(NULL, 0, "%#.f", 1.0);
  100. expected = "1.";
  101. SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
  102. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  103. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  104. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  105. result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
  106. predicted = SDL_snprintf(NULL, 0, "%f", 1.0 + 1.0 / 3.0);
  107. expected = "1.333333";
  108. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
  109. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  110. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  111. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  112. result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
  113. predicted = SDL_snprintf(NULL, 0, "%+f", 1.0 + 1.0 / 3.0);
  114. expected = "+1.333333";
  115. SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
  116. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  117. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  118. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  119. result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
  120. predicted = SDL_snprintf(NULL, 0, "%.2f", 1.0 + 1.0 / 3.0);
  121. expected = "1.33";
  122. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
  123. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  124. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  125. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  126. result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
  127. predicted = SDL_snprintf(NULL, 0, "%6.2f", 1.0 + 1.0 / 3.0);
  128. expected = " 1.33";
  129. SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
  130. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  131. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  132. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  133. result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
  134. predicted = SDL_snprintf(NULL, 0, "%06.2f", 1.0 + 1.0 / 3.0);
  135. expected = "001.33";
  136. SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.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. result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
  141. expected = "001.";
  142. SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
  143. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  144. SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
  145. size = 64;
  146. result = SDL_snprintf(text, sizeof(text), "%zu %s", size, "test");
  147. expected = "64 test";
  148. SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%zu %%s\", size, \"test\")");
  149. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  150. SDLTest_AssertCheck(result == 7, "Check result value, expected: 7, got: %d", result);
  151. return TEST_COMPLETED;
  152. }
  153. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  154. #pragma GCC diagnostic pop
  155. #endif
  156. /**
  157. * @brief Call to SDL_getenv and SDL_setenv
  158. */
  159. int stdlib_getsetenv(void *arg)
  160. {
  161. const int nameLen = 16;
  162. char name[17];
  163. int counter;
  164. int result;
  165. char *value1;
  166. char *value2;
  167. char *expected;
  168. int overwrite;
  169. char *text;
  170. /* Create a random name. This tests SDL_getenv, since we need to */
  171. /* make sure the variable is not set yet (it shouldn't). */
  172. do {
  173. for (counter = 0; counter < nameLen; counter++) {
  174. name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
  175. }
  176. name[nameLen] = '\0';
  177. text = SDL_getenv(name);
  178. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  179. if (text != NULL) {
  180. SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, (int)SDL_strlen(text));
  181. }
  182. } while (text != NULL);
  183. /* Create random values to set */
  184. value1 = SDLTest_RandomAsciiStringOfSize(10);
  185. value2 = SDLTest_RandomAsciiStringOfSize(10);
  186. /* Set value 1 without overwrite */
  187. overwrite = 0;
  188. expected = value1;
  189. result = SDL_setenv(name, value1, overwrite);
  190. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  191. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  192. /* Check value */
  193. text = SDL_getenv(name);
  194. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  195. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  196. if (text != NULL) {
  197. SDLTest_AssertCheck(
  198. SDL_strcmp(text, expected) == 0,
  199. "Verify returned text, expected: %s, got: %s",
  200. expected,
  201. text);
  202. }
  203. /* Set value 2 with overwrite */
  204. overwrite = 1;
  205. expected = value2;
  206. result = SDL_setenv(name, value2, overwrite);
  207. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite);
  208. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  209. /* Check value */
  210. text = SDL_getenv(name);
  211. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  212. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  213. if (text != NULL) {
  214. SDLTest_AssertCheck(
  215. SDL_strcmp(text, expected) == 0,
  216. "Verify returned text, expected: %s, got: %s",
  217. expected,
  218. text);
  219. }
  220. /* Set value 1 without overwrite */
  221. overwrite = 0;
  222. expected = value2;
  223. result = SDL_setenv(name, value1, overwrite);
  224. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  225. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  226. /* Check value */
  227. text = SDL_getenv(name);
  228. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  229. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  230. if (text != NULL) {
  231. SDLTest_AssertCheck(
  232. SDL_strcmp(text, expected) == 0,
  233. "Verify returned text, expected: %s, got: %s",
  234. expected,
  235. text);
  236. }
  237. /* Set value 1 without overwrite */
  238. overwrite = 1;
  239. expected = value1;
  240. result = SDL_setenv(name, value1, overwrite);
  241. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  242. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  243. /* Check value */
  244. text = SDL_getenv(name);
  245. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  246. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  247. if (text != NULL) {
  248. SDLTest_AssertCheck(
  249. SDL_strcmp(text, expected) == 0,
  250. "Verify returned text, expected: %s, got: %s",
  251. expected,
  252. text);
  253. }
  254. /* Negative cases */
  255. for (overwrite = 0; overwrite <= 1; overwrite++) {
  256. result = SDL_setenv(NULL, value1, overwrite);
  257. SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite);
  258. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  259. result = SDL_setenv("", value1, overwrite);
  260. SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite);
  261. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  262. result = SDL_setenv("=", value1, overwrite);
  263. SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite);
  264. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  265. result = SDL_setenv(name, NULL, overwrite);
  266. SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite);
  267. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  268. }
  269. /* Clean up */
  270. SDL_free(value1);
  271. SDL_free(value2);
  272. return TEST_COMPLETED;
  273. }
  274. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  275. #pragma GCC diagnostic push
  276. #if defined(HAVE_WFORMAT)
  277. #pragma GCC diagnostic ignored "-Wformat"
  278. #endif
  279. #if defined(HAVE_WFORMAT_EXTRA_ARGS)
  280. #pragma GCC diagnostic ignored "-Wformat-extra-args"
  281. #endif
  282. #endif
  283. /**
  284. * @brief Call to SDL_sscanf
  285. */
  286. #undef SDL_sscanf
  287. int stdlib_sscanf(void *arg)
  288. {
  289. int output;
  290. int result;
  291. int expected_output;
  292. int expected_result;
  293. short short_output, expected_short_output;
  294. long long_output, expected_long_output;
  295. long long long_long_output, expected_long_long_output;
  296. size_t size_output, expected_size_output;
  297. char text[128];
  298. expected_output = output = 123;
  299. expected_result = -1;
  300. result = SDL_sscanf("", "%i", &output);
  301. SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)");
  302. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  303. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  304. expected_output = output = 123;
  305. expected_result = 0;
  306. result = SDL_sscanf("a", "%i", &output);
  307. SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)");
  308. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  309. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  310. output = 123;
  311. expected_output = 2;
  312. expected_result = 1;
  313. result = SDL_sscanf("2", "%i", &output);
  314. SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i\", &output)");
  315. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  316. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  317. output = 123;
  318. expected_output = 0xa;
  319. expected_result = 1;
  320. result = SDL_sscanf("aa", "%1x", &output);
  321. SDLTest_AssertPass("Call to SDL_sscanf(\"aa\", \"%%1x\", &output)");
  322. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  323. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  324. #define SIZED_TEST_CASE(type, var, format_specifier) \
  325. var##_output = 123; \
  326. expected_##var##_output = (type)(((unsigned type)(~0)) >> 1); \
  327. expected_result = 1; \
  328. result = SDL_snprintf(text, sizeof(text), format_specifier, expected_##var##_output); \
  329. result = SDL_sscanf(text, format_specifier, &var##_output); \
  330. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%s\", &output)", text, #format_specifier); \
  331. SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " format_specifier ", got: " format_specifier, expected_##var##_output, var##_output); \
  332. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \
  333. \
  334. var##_output = 123; \
  335. expected_##var##_output = ~(type)(((unsigned type)(~0)) >> 1); \
  336. expected_result = 1; \
  337. result = SDL_snprintf(text, sizeof(text), format_specifier, expected_##var##_output); \
  338. result = SDL_sscanf(text, format_specifier, &var##_output); \
  339. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%s\", &output)", text, #format_specifier); \
  340. SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " format_specifier ", got: " format_specifier, expected_##var##_output, var##_output); \
  341. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  342. SIZED_TEST_CASE(short, short, "%hd")
  343. SIZED_TEST_CASE(long, long, "%ld")
  344. SIZED_TEST_CASE(long long, long_long, "%lld")
  345. size_output = 123;
  346. expected_size_output = ~((size_t)0);
  347. expected_result = 1;
  348. result = SDL_snprintf(text, sizeof(text), "%zu", expected_size_output);
  349. result = SDL_sscanf(text, "%zu", &size_output);
  350. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%zu\", &output)", text);
  351. SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output);
  352. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  353. return TEST_COMPLETED;
  354. }
  355. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  356. #pragma GCC diagnostic pop
  357. #endif
  358. #if defined(_WIN64)
  359. #define SIZE_FORMAT "I64u"
  360. #elif defined(__WIN32__)
  361. #define SIZE_FORMAT "I32u"
  362. #else
  363. #define SIZE_FORMAT "zu"
  364. #endif
  365. typedef struct
  366. {
  367. size_t a;
  368. size_t b;
  369. size_t result;
  370. int status;
  371. } overflow_test;
  372. static const overflow_test multiplications[] = {
  373. { 1, 1, 1, 0 },
  374. { 0, 0, 0, 0 },
  375. { SDL_SIZE_MAX, 0, 0, 0 },
  376. { SDL_SIZE_MAX, 1, SDL_SIZE_MAX, 0 },
  377. { SDL_SIZE_MAX / 2, 2, SDL_SIZE_MAX - (SDL_SIZE_MAX % 2), 0 },
  378. { SDL_SIZE_MAX / 23, 23, SDL_SIZE_MAX - (SDL_SIZE_MAX % 23), 0 },
  379. { (SDL_SIZE_MAX / 2) + 1, 2, 0, -1 },
  380. { (SDL_SIZE_MAX / 23) + 42, 23, 0, -1 },
  381. { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, -1 },
  382. };
  383. static const overflow_test additions[] = {
  384. { 1, 1, 2, 0 },
  385. { 0, 0, 0, 0 },
  386. { SDL_SIZE_MAX, 0, SDL_SIZE_MAX, 0 },
  387. { SDL_SIZE_MAX - 1, 1, SDL_SIZE_MAX, 0 },
  388. { SDL_SIZE_MAX - 42, 23, SDL_SIZE_MAX - (42 - 23), 0 },
  389. { SDL_SIZE_MAX, 1, 0, -1 },
  390. { SDL_SIZE_MAX, 23, 0, -1 },
  391. { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, -1 },
  392. };
  393. static int
  394. stdlib_overflow(void *arg)
  395. {
  396. size_t i;
  397. size_t useBuiltin;
  398. for (useBuiltin = 0; useBuiltin < 2; useBuiltin++) {
  399. if (useBuiltin) {
  400. SDLTest_Log("Using gcc/clang builtins if possible");
  401. } else {
  402. SDLTest_Log("Not using gcc/clang builtins");
  403. }
  404. for (i = 0; i < SDL_arraysize(multiplications); i++) {
  405. const overflow_test *t = &multiplications[i];
  406. int status;
  407. size_t result = ~t->result;
  408. if (useBuiltin) {
  409. status = SDL_size_mul_overflow(t->a, t->b, &result);
  410. } else {
  411. /* This disables the macro that tries to use a gcc/clang
  412. * builtin, so we test the fallback implementation instead. */
  413. status = (SDL_size_mul_overflow)(t->a, t->b, &result);
  414. }
  415. if (t->status == 0) {
  416. SDLTest_AssertCheck(status == 0,
  417. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
  418. t->a, t->b);
  419. SDLTest_AssertCheck(result == t->result,
  420. "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  421. t->a, t->b, t->result, result);
  422. } else {
  423. SDLTest_AssertCheck(status == -1,
  424. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
  425. t->a, t->b);
  426. }
  427. if (t->a == t->b) {
  428. continue;
  429. }
  430. result = ~t->result;
  431. if (useBuiltin) {
  432. status = SDL_size_mul_overflow(t->b, t->a, &result);
  433. } else {
  434. status = (SDL_size_mul_overflow)(t->b, t->a, &result);
  435. }
  436. if (t->status == 0) {
  437. SDLTest_AssertCheck(status == 0,
  438. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
  439. t->b, t->a);
  440. SDLTest_AssertCheck(result == t->result,
  441. "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  442. t->b, t->a, t->result, result);
  443. } else {
  444. SDLTest_AssertCheck(status == -1,
  445. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
  446. t->b, t->a);
  447. }
  448. }
  449. for (i = 0; i < SDL_arraysize(additions); i++) {
  450. const overflow_test *t = &additions[i];
  451. int status;
  452. size_t result = ~t->result;
  453. if (useBuiltin) {
  454. status = SDL_size_add_overflow(t->a, t->b, &result);
  455. } else {
  456. status = (SDL_size_add_overflow)(t->a, t->b, &result);
  457. }
  458. if (t->status == 0) {
  459. SDLTest_AssertCheck(status == 0,
  460. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
  461. t->a, t->b);
  462. SDLTest_AssertCheck(result == t->result,
  463. "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  464. t->a, t->b, t->result, result);
  465. } else {
  466. SDLTest_AssertCheck(status == -1,
  467. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
  468. t->a, t->b);
  469. }
  470. if (t->a == t->b) {
  471. continue;
  472. }
  473. result = ~t->result;
  474. if (useBuiltin) {
  475. status = SDL_size_add_overflow(t->b, t->a, &result);
  476. } else {
  477. status = (SDL_size_add_overflow)(t->b, t->a, &result);
  478. }
  479. if (t->status == 0) {
  480. SDLTest_AssertCheck(status == 0,
  481. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
  482. t->b, t->a);
  483. SDLTest_AssertCheck(result == t->result,
  484. "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  485. t->b, t->a, t->result, result);
  486. } else {
  487. SDLTest_AssertCheck(status == -1,
  488. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
  489. t->b, t->a);
  490. }
  491. }
  492. }
  493. return TEST_COMPLETED;
  494. }
  495. /* ================= Test References ================== */
  496. /* Standard C routine test cases */
  497. static const SDLTest_TestCaseReference stdlibTest1 = {
  498. (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED
  499. };
  500. static const SDLTest_TestCaseReference stdlibTest2 = {
  501. (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED
  502. };
  503. static const SDLTest_TestCaseReference stdlibTest3 = {
  504. (SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED
  505. };
  506. static const SDLTest_TestCaseReference stdlibTest4 = {
  507. (SDLTest_TestCaseFp)stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED
  508. };
  509. static const SDLTest_TestCaseReference stdlibTestOverflow = {
  510. stdlib_overflow, "stdlib_overflow", "Overflow detection", TEST_ENABLED
  511. };
  512. /* Sequence of Standard C routine test cases */
  513. static const SDLTest_TestCaseReference *stdlibTests[] = {
  514. &stdlibTest1,
  515. &stdlibTest2,
  516. &stdlibTest3,
  517. &stdlibTest4,
  518. &stdlibTestOverflow,
  519. NULL
  520. };
  521. /* Standard C routine test suite (global) */
  522. SDLTest_TestSuiteReference stdlibTestSuite = {
  523. "Stdlib",
  524. NULL,
  525. stdlibTests,
  526. NULL
  527. };