testautomation_stdlib.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. unsigned int r = 0, g = 0, b = 0;
  366. expected_output = output = 123;
  367. expected_result = -1;
  368. result = SDL_sscanf("", "%i", &output);
  369. SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)");
  370. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  371. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  372. expected_output = output = 123;
  373. expected_result = 0;
  374. result = SDL_sscanf("a", "%i", &output);
  375. SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)");
  376. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  377. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  378. output = 123;
  379. length = 0;
  380. expected_output = 2;
  381. expected_result = 1;
  382. result = SDL_sscanf("2", "%i%n", &output, &length);
  383. SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i%%n\", &output, &length)");
  384. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  385. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  386. SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length);
  387. output = 123;
  388. length = 0;
  389. expected_output = 0xa;
  390. expected_result = 1;
  391. result = SDL_sscanf("aa", "%1x%n", &output, &length);
  392. SDLTest_AssertPass("Call to SDL_sscanf(\"aa\", \"%%1x%%n\", &output, &length)");
  393. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  394. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  395. SDLTest_AssertCheck(length == 1, "Check length, expected: 1, got: %i", length);
  396. expected_result = 3;
  397. result = SDL_sscanf("#026", "#%1x%1x%1x", &r, &g, &b);
  398. SDLTest_AssertPass("Call to SDL_sscanf(\"#026\", \"#%%1x%%1x%%1x\", &r, &g, &b)");
  399. expected_output = 0;
  400. SDLTest_AssertCheck(r == expected_output, "Check output for r, expected: %i, got: %i", expected_output, r);
  401. expected_output = 2;
  402. SDLTest_AssertCheck(g == expected_output, "Check output for g, expected: %i, got: %i", expected_output, g);
  403. expected_output = 6;
  404. SDLTest_AssertCheck(b == expected_output, "Check output for b, expected: %i, got: %i", expected_output, b);
  405. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  406. #define SIZED_TEST_CASE(type, var, printf_specifier, scanf_specifier) \
  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. \
  418. var##_output = 123; \
  419. var##_length = 0; \
  420. expected_##var##_output = ~(type)(((unsigned type)(~0)) >> 1); \
  421. expected_result = 1; \
  422. result = SDL_snprintf(text, sizeof(text), printf_specifier, expected_##var##_output); \
  423. result = SDL_sscanf(text, scanf_specifier, &var##_output, &var##_length); \
  424. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", %s, &output, &length)", text, #scanf_specifier); \
  425. SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " printf_specifier ", got: " printf_specifier, expected_##var##_output, var##_output); \
  426. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \
  427. SDLTest_AssertCheck(var##_length == (type)SDL_strlen(text), "Check length, expected: %i, got: %i", (int)SDL_strlen(text), (int)var##_length); \
  428. SIZED_TEST_CASE(short, short, "%hd", "%hd%hn")
  429. SIZED_TEST_CASE(long, long, "%ld", "%ld%ln")
  430. SIZED_TEST_CASE(long long, long_long, FMT_PRILLd, FMT_PRILLdn)
  431. size_output = 123;
  432. expected_size_output = ~((size_t)0);
  433. expected_result = 1;
  434. result = SDL_snprintf(text, sizeof(text), "%zu", expected_size_output);
  435. result = SDL_sscanf(text, "%zu", &size_output);
  436. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%zu\", &output)", text);
  437. SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output);
  438. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  439. expected_result = 1;
  440. text[0] = '\0';
  441. result = SDL_sscanf("abc def", "%s", text);
  442. SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%s\", text)");
  443. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  444. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  445. expected_result = 1;
  446. text[0] = '\0';
  447. result = SDL_sscanf("abc,def", "%s", text);
  448. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%s\", text)");
  449. SDLTest_AssertCheck(SDL_strcmp(text, "abc,def") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  450. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  451. expected_result = 1;
  452. text[0] = '\0';
  453. result = SDL_sscanf("abc,def", "%[cba]", text);
  454. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[cba]\", text)");
  455. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  456. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  457. expected_result = 1;
  458. text[0] = '\0';
  459. result = SDL_sscanf("abc,def", "%[a-z]", text);
  460. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[z-a]\", text)");
  461. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  462. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  463. expected_result = 1;
  464. text[0] = '\0';
  465. result = SDL_sscanf("abc,def", "%[^,]", text);
  466. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[^,]\", text)");
  467. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  468. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  469. expected_result = 0;
  470. text[0] = '\0';
  471. result = SDL_sscanf("abc,def", "%[A-Z]", text);
  472. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[A-Z]\", text)");
  473. SDLTest_AssertCheck(SDL_strcmp(text, "") == 0, "Check output, expected: \"\", got: \"%s\"", text);
  474. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  475. expected_result = 2;
  476. text[0] = '\0';
  477. text2[0] = '\0';
  478. result = SDL_sscanf("abc,def", "%[abc],%[def]", text, text2);
  479. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc],%%[def]\", text)");
  480. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  481. SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
  482. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  483. expected_result = 2;
  484. text[0] = '\0';
  485. text2[0] = '\0';
  486. result = SDL_sscanf("abc,def", "%[abc]%*[,]%[def]", text, text2);
  487. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc]%%*[,]%%[def]\", text)");
  488. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  489. SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
  490. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  491. expected_result = 2;
  492. text[0] = '\0';
  493. text2[0] = '\0';
  494. result = SDL_sscanf("abc def", "%[abc] %[def]", text, text2);
  495. SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%[abc] %%[def]\", text)");
  496. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  497. SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
  498. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  499. expected_result = 1;
  500. text[0] = '\0';
  501. result = SDL_sscanf("abc123XYZ", "%[a-zA-Z0-9]", text);
  502. SDLTest_AssertPass("Call to SDL_sscanf(\"abc123XYZ\", \"%%[a-zA-Z0-9]\", text)");
  503. SDLTest_AssertCheck(SDL_strcmp(text, "abc123XYZ") == 0, "Check output, expected: \"abc123XYZ\", got: \"%s\"", text);
  504. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  505. return TEST_COMPLETED;
  506. }
  507. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  508. #pragma GCC diagnostic pop
  509. #endif
  510. #if defined(_WIN64)
  511. #define SIZE_FORMAT "I64u"
  512. #elif defined(__WIN32__)
  513. #define SIZE_FORMAT "I32u"
  514. #else
  515. #define SIZE_FORMAT "zu"
  516. #endif
  517. typedef struct
  518. {
  519. size_t a;
  520. size_t b;
  521. size_t result;
  522. int status;
  523. } overflow_test;
  524. static const overflow_test multiplications[] = {
  525. { 1, 1, 1, 0 },
  526. { 0, 0, 0, 0 },
  527. { SDL_SIZE_MAX, 0, 0, 0 },
  528. { SDL_SIZE_MAX, 1, SDL_SIZE_MAX, 0 },
  529. { SDL_SIZE_MAX / 2, 2, SDL_SIZE_MAX - (SDL_SIZE_MAX % 2), 0 },
  530. { SDL_SIZE_MAX / 23, 23, SDL_SIZE_MAX - (SDL_SIZE_MAX % 23), 0 },
  531. { (SDL_SIZE_MAX / 2) + 1, 2, 0, -1 },
  532. { (SDL_SIZE_MAX / 23) + 42, 23, 0, -1 },
  533. { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, -1 },
  534. };
  535. static const overflow_test additions[] = {
  536. { 1, 1, 2, 0 },
  537. { 0, 0, 0, 0 },
  538. { SDL_SIZE_MAX, 0, SDL_SIZE_MAX, 0 },
  539. { SDL_SIZE_MAX - 1, 1, SDL_SIZE_MAX, 0 },
  540. { SDL_SIZE_MAX - 42, 23, SDL_SIZE_MAX - (42 - 23), 0 },
  541. { SDL_SIZE_MAX, 1, 0, -1 },
  542. { SDL_SIZE_MAX, 23, 0, -1 },
  543. { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, -1 },
  544. };
  545. static int
  546. stdlib_overflow(void *arg)
  547. {
  548. size_t i;
  549. size_t useBuiltin;
  550. for (useBuiltin = 0; useBuiltin < 2; useBuiltin++) {
  551. if (useBuiltin) {
  552. SDLTest_Log("Using gcc/clang builtins if possible");
  553. } else {
  554. SDLTest_Log("Not using gcc/clang builtins");
  555. }
  556. for (i = 0; i < SDL_arraysize(multiplications); i++) {
  557. const overflow_test *t = &multiplications[i];
  558. int status;
  559. size_t result = ~t->result;
  560. if (useBuiltin) {
  561. status = SDL_size_mul_overflow(t->a, t->b, &result);
  562. } else {
  563. /* This disables the macro that tries to use a gcc/clang
  564. * builtin, so we test the fallback implementation instead. */
  565. status = (SDL_size_mul_overflow)(t->a, t->b, &result);
  566. }
  567. if (t->status == 0) {
  568. SDLTest_AssertCheck(status == 0,
  569. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
  570. t->a, t->b);
  571. SDLTest_AssertCheck(result == t->result,
  572. "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  573. t->a, t->b, t->result, result);
  574. } else {
  575. SDLTest_AssertCheck(status == -1,
  576. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
  577. t->a, t->b);
  578. }
  579. if (t->a == t->b) {
  580. continue;
  581. }
  582. result = ~t->result;
  583. if (useBuiltin) {
  584. status = SDL_size_mul_overflow(t->b, t->a, &result);
  585. } else {
  586. status = (SDL_size_mul_overflow)(t->b, t->a, &result);
  587. }
  588. if (t->status == 0) {
  589. SDLTest_AssertCheck(status == 0,
  590. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
  591. t->b, t->a);
  592. SDLTest_AssertCheck(result == t->result,
  593. "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  594. t->b, t->a, t->result, result);
  595. } else {
  596. SDLTest_AssertCheck(status == -1,
  597. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
  598. t->b, t->a);
  599. }
  600. }
  601. for (i = 0; i < SDL_arraysize(additions); i++) {
  602. const overflow_test *t = &additions[i];
  603. int status;
  604. size_t result = ~t->result;
  605. if (useBuiltin) {
  606. status = SDL_size_add_overflow(t->a, t->b, &result);
  607. } else {
  608. status = (SDL_size_add_overflow)(t->a, t->b, &result);
  609. }
  610. if (t->status == 0) {
  611. SDLTest_AssertCheck(status == 0,
  612. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
  613. t->a, t->b);
  614. SDLTest_AssertCheck(result == t->result,
  615. "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  616. t->a, t->b, t->result, result);
  617. } else {
  618. SDLTest_AssertCheck(status == -1,
  619. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
  620. t->a, t->b);
  621. }
  622. if (t->a == t->b) {
  623. continue;
  624. }
  625. result = ~t->result;
  626. if (useBuiltin) {
  627. status = SDL_size_add_overflow(t->b, t->a, &result);
  628. } else {
  629. status = (SDL_size_add_overflow)(t->b, t->a, &result);
  630. }
  631. if (t->status == 0) {
  632. SDLTest_AssertCheck(status == 0,
  633. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
  634. t->b, t->a);
  635. SDLTest_AssertCheck(result == t->result,
  636. "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  637. t->b, t->a, t->result, result);
  638. } else {
  639. SDLTest_AssertCheck(status == -1,
  640. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
  641. t->b, t->a);
  642. }
  643. }
  644. }
  645. return TEST_COMPLETED;
  646. }
  647. static int
  648. stdlib_strtox(void *arg)
  649. {
  650. const unsigned long long ullong_max = ~0ULL;
  651. #define STRTOX_TEST_CASE(func_name, type, format_spec, str, base, expected_result, expected_endp_offset) do { \
  652. const char *s = str; \
  653. type r, expected_r = expected_result; \
  654. char *ep, *expected_ep = (char *)s + expected_endp_offset; \
  655. r = func_name(s, &ep, base); \
  656. SDLTest_AssertPass("Call to " #func_name "(" #str ", &endp, " #base ")"); \
  657. SDLTest_AssertCheck(r == expected_r, "Check result value, expected: " format_spec ", got: " format_spec, expected_r, r); \
  658. SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \
  659. } while (0)
  660. // infer decimal
  661. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "\t 123abcxyz", 0, 123, 6); // skip leading space
  662. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "+123abcxyz", 0, 123, 4);
  663. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "+123abcxyz", 0, 123, 4);
  664. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-123abcxyz", 0, -123, 4);
  665. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "9999999999999999999999999999999999999999abcxyz", 0, ullong_max, 40);
  666. // infer hexadecimal
  667. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0x123abcxyz", 0, 0x123abc, 8);
  668. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0X123ABCXYZ", 0, 0x123abc, 8); // uppercase X
  669. // infer octal
  670. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0123abcxyz", 0, 0123, 4);
  671. // arbitrary bases
  672. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "00110011", 2, 51, 8);
  673. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-uvwxyz", 32, -991, 3);
  674. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "ZzZzZzZzZzZzZzZzZzZzZzZzZ", 36, ullong_max, 25);
  675. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0", 0, 0, 1);
  676. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "0", 10, 0, 1);
  677. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-0", 0, 0, 2);
  678. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, "-0", 10, 0, 2);
  679. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLu, " - 1", 0, 0, 0); // invalid input
  680. // We know that SDL_strtol, SDL_strtoul and SDL_strtoll share the same code path as SDL_strtoull under the hood,
  681. // so the most interesting test cases are those close to the bounds of the integer type.
  682. // For simplicity, we only run long/long long tests when they are 32-bit/64-bit, respectively.
  683. // Suppressing warnings would be difficult otherwise.
  684. // Since the CI runs the tests against a variety of targets, this should be fine in practice.
  685. if (sizeof(long) == 4) {
  686. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "0", 0, 0, 1);
  687. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "0", 10, 0, 1);
  688. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-0", 0, 0, 2);
  689. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-0", 10, 0, 2);
  690. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "2147483647", 10, 2147483647, 10);
  691. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "2147483648", 10, 2147483647, 10);
  692. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-2147483648", 10, -2147483647L - 1, 11);
  693. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-2147483649", 10, -2147483647L - 1, 11);
  694. STRTOX_TEST_CASE(SDL_strtol, long, "%ld", "-9999999999999999999999999999999999999999", 10, -2147483647L - 1, 41);
  695. STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "4294967295", 10, 4294967295UL, 10);
  696. STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "4294967296", 10, 4294967295UL, 10);
  697. STRTOX_TEST_CASE(SDL_strtoul, unsigned long, "%lu", "-4294967295", 10, 1, 11);
  698. }
  699. if (sizeof(long long) == 8) {
  700. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "0", 0, 0LL, 1);
  701. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "0", 10, 0LL, 1);
  702. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-0", 0, 0LL, 2);
  703. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-0", 10, 0LL, 2);
  704. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "9223372036854775807", 10, 9223372036854775807LL, 19);
  705. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "9223372036854775808", 10, 9223372036854775807LL, 19);
  706. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9223372036854775808", 10, -9223372036854775807LL - 1, 20);
  707. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9223372036854775809", 10, -9223372036854775807LL - 1, 20);
  708. STRTOX_TEST_CASE(SDL_strtoll, long long, FMT_PRILLd, "-9999999999999999999999999999999999999999", 10, -9223372036854775807LL - 1, 41);
  709. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "18446744073709551615", 10, 18446744073709551615ULL, 20);
  710. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "18446744073709551616", 10, 18446744073709551615ULL, 20);
  711. STRTOX_TEST_CASE(SDL_strtoull, unsigned long long, FMT_PRILLd, "-18446744073709551615", 10, 1, 21);
  712. }
  713. #undef STRTOX_TEST_CASE
  714. return TEST_COMPLETED;
  715. }
  716. static int
  717. stdlib_strtod(void *arg)
  718. {
  719. #define STRTOD_TEST_CASE(str, expected_result, expected_endp_offset) do { \
  720. const char *s = str; \
  721. double r, expected_r = expected_result; \
  722. char *ep, *expected_ep = (char *)s + expected_endp_offset; \
  723. r = SDL_strtod(s, &ep); \
  724. SDLTest_AssertPass("Call to SDL_strtod(" #str ", &endp)"); \
  725. SDLTest_AssertCheck(r == expected_r, "Check result value, expected: %f, got: %f", expected_r, r); \
  726. SDLTest_AssertCheck(ep == expected_ep, "Check endp value, expected: %p, got: %p", expected_ep, ep); \
  727. } while (0)
  728. STRTOD_TEST_CASE("\t 123.75abcxyz", 123.75, 9); // skip leading space
  729. STRTOD_TEST_CASE("+999.555", 999.555, 8);
  730. STRTOD_TEST_CASE("-999.555", -999.555, 8);
  731. #undef STRTOD_TEST_CASE
  732. return TEST_COMPLETED;
  733. }
  734. /* ================= Test References ================== */
  735. /* Standard C routine test cases */
  736. static const SDLTest_TestCaseReference stdlibTest1 = {
  737. (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED
  738. };
  739. static const SDLTest_TestCaseReference stdlibTest2 = {
  740. (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED
  741. };
  742. static const SDLTest_TestCaseReference stdlibTest3 = {
  743. (SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED
  744. };
  745. static const SDLTest_TestCaseReference stdlibTest4 = {
  746. (SDLTest_TestCaseFp)stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED
  747. };
  748. static const SDLTest_TestCaseReference stdlibTestOverflow = {
  749. stdlib_overflow, "stdlib_overflow", "Overflow detection", TEST_ENABLED
  750. };
  751. static const SDLTest_TestCaseReference stdlibTest_strtox = {
  752. stdlib_strtox, "stdlib_strtox", "Calls to SDL_strtol, SDL_strtoul, SDL_strtoll and SDL_strtoull", TEST_ENABLED
  753. };
  754. static const SDLTest_TestCaseReference stdlibTest_strtod = {
  755. stdlib_strtod, "stdlib_strtod", "Calls to SDL_strtod", TEST_ENABLED
  756. };
  757. /* Sequence of Standard C routine test cases */
  758. static const SDLTest_TestCaseReference *stdlibTests[] = {
  759. &stdlibTest1,
  760. &stdlibTest2,
  761. &stdlibTest3,
  762. &stdlibTest4,
  763. &stdlibTestOverflow,
  764. &stdlibTest_strtox,
  765. &stdlibTest_strtod,
  766. NULL
  767. };
  768. /* Standard C routine test suite (global) */
  769. SDLTest_TestSuiteReference stdlibTestSuite = {
  770. "Stdlib",
  771. NULL,
  772. stdlibTests,
  773. NULL
  774. };