testautomation_stdlib.c 44 KB

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