🛠️🐜 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.

351 lines
15 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
  13. stdlib_strlcpy(void *arg)
  14. {
  15. size_t result;
  16. char text[1024];
  17. const char *expected;
  18. result = SDL_strlcpy(text, "foo", sizeof(text));
  19. expected = "foo";
  20. SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
  21. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  22. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), (int) result);
  23. result = SDL_strlcpy(text, "foo", 2);
  24. expected = "f";
  25. SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
  26. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  27. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int) result);
  28. return TEST_COMPLETED;
  29. }
  30. /**
  31. * @brief Call to SDL_snprintf
  32. */
  33. #undef SDL_snprintf
  34. int
  35. stdlib_snprintf(void *arg)
  36. {
  37. int result;
  38. int predicted;
  39. char text[1024];
  40. const char *expected;
  41. result = SDL_snprintf(text, sizeof(text), "%s", "foo");
  42. expected = "foo";
  43. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
  44. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  45. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
  46. result = SDL_snprintf(text, 2, "%s", "foo");
  47. expected = "f";
  48. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
  49. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  50. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  51. result = SDL_snprintf(NULL, 0, "%s", "foo");
  52. SDLTest_AssertPass("Call to SDL_snprintf(NULL, 0, \"%%s\", \"foo\")");
  53. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  54. result = SDL_snprintf(text, 2, "%s\n", "foo");
  55. expected = "f";
  56. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\\n\", \"foo\") with buffer size 2");
  57. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  58. SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", result);
  59. result = SDL_snprintf(text, sizeof(text), "%f", 0.0);
  60. predicted = SDL_snprintf(NULL, 0, "%f", 0.0);
  61. expected = "0.000000";
  62. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 0.0)");
  63. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  64. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
  65. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  66. result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
  67. predicted = SDL_snprintf(NULL, 0, "%f", 1.0);
  68. expected = "1.000000";
  69. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
  70. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  71. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
  72. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  73. result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
  74. predicted = SDL_snprintf(NULL, 0, "%.f", 1.0);
  75. expected = "1";
  76. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
  77. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  78. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
  79. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  80. result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
  81. predicted = SDL_snprintf(NULL, 0, "%#.f", 1.0);
  82. expected = "1.";
  83. SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
  84. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  85. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
  86. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  87. result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
  88. predicted = SDL_snprintf(NULL, 0, "%f", 1.0 + 1.0 / 3.0);
  89. expected = "1.333333";
  90. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.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 + 1.0 / 3.0);
  95. predicted = SDL_snprintf(NULL, 0, "%+f", 1.0 + 1.0 / 3.0);
  96. expected = "+1.333333";
  97. SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.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), "%.2f", 1.0 + 1.0 / 3.0);
  102. predicted = SDL_snprintf(NULL, 0, "%.2f", 1.0 + 1.0 / 3.0);
  103. expected = "1.33";
  104. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.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), "%6.2f", 1.0 + 1.0 / 3.0);
  109. predicted = SDL_snprintf(NULL, 0, "%6.2f", 1.0 + 1.0 / 3.0);
  110. expected = " 1.33";
  111. SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.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), "%06.2f", 1.0 + 1.0 / 3.0);
  116. predicted = SDL_snprintf(NULL, 0, "%06.2f", 1.0 + 1.0 / 3.0);
  117. expected = "001.33";
  118. SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 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, 5, "%06.2f", 1.0 + 1.0 / 3.0);
  123. expected = "001.";
  124. SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
  125. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  126. SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
  127. return TEST_COMPLETED;
  128. }
  129. /**
  130. * @brief Call to SDL_getenv and SDL_setenv
  131. */
  132. int
  133. stdlib_getsetenv(void *arg)
  134. {
  135. const int nameLen = 16;
  136. char name[17];
  137. int counter;
  138. int result;
  139. char * value1;
  140. char * value2;
  141. char * expected;
  142. int overwrite;
  143. char * text;
  144. /* Create a random name. This tests SDL_getenv, since we need to */
  145. /* make sure the variable is not set yet (it shouldn't). */
  146. do {
  147. for(counter = 0; counter < nameLen; counter++) {
  148. name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
  149. }
  150. name[nameLen] = '\0';
  151. text = SDL_getenv(name);
  152. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  153. if (text != NULL) {
  154. SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, (int) SDL_strlen(text));
  155. }
  156. } while (text != NULL);
  157. /* Create random values to set */
  158. value1 = SDLTest_RandomAsciiStringOfSize(10);
  159. value2 = SDLTest_RandomAsciiStringOfSize(10);
  160. /* Set value 1 without overwrite */
  161. overwrite = 0;
  162. expected = value1;
  163. result = SDL_setenv(name, value1, overwrite);
  164. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  165. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  166. /* Check value */
  167. text = SDL_getenv(name);
  168. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  169. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  170. if (text != NULL) {
  171. SDLTest_AssertCheck(
  172. SDL_strcmp(text, expected) == 0,
  173. "Verify returned text, expected: %s, got: %s",
  174. expected,
  175. text);
  176. }
  177. /* Set value 2 with overwrite */
  178. overwrite = 1;
  179. expected = value2;
  180. result = SDL_setenv(name, value2, overwrite);
  181. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite);
  182. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  183. /* Check value */
  184. text = SDL_getenv(name);
  185. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  186. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  187. if (text != NULL) {
  188. SDLTest_AssertCheck(
  189. SDL_strcmp(text, expected) == 0,
  190. "Verify returned text, expected: %s, got: %s",
  191. expected,
  192. text);
  193. }
  194. /* Set value 1 without overwrite */
  195. overwrite = 0;
  196. expected = value2;
  197. result = SDL_setenv(name, value1, overwrite);
  198. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  199. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  200. /* Check value */
  201. text = SDL_getenv(name);
  202. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  203. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  204. if (text != NULL) {
  205. SDLTest_AssertCheck(
  206. SDL_strcmp(text, expected) == 0,
  207. "Verify returned text, expected: %s, got: %s",
  208. expected,
  209. text);
  210. }
  211. /* Set value 1 without overwrite */
  212. overwrite = 1;
  213. expected = value1;
  214. result = SDL_setenv(name, value1, overwrite);
  215. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  216. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  217. /* Check value */
  218. text = SDL_getenv(name);
  219. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  220. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  221. if (text != NULL) {
  222. SDLTest_AssertCheck(
  223. SDL_strcmp(text, expected) == 0,
  224. "Verify returned text, expected: %s, got: %s",
  225. expected,
  226. text);
  227. }
  228. /* Negative cases */
  229. for (overwrite=0; overwrite <= 1; overwrite++) {
  230. result = SDL_setenv(NULL, value1, overwrite);
  231. SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite);
  232. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  233. result = SDL_setenv("", value1, overwrite);
  234. SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite);
  235. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  236. result = SDL_setenv("=", value1, overwrite);
  237. SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite);
  238. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  239. result = SDL_setenv(name, NULL, overwrite);
  240. SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite);
  241. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  242. }
  243. /* Clean up */
  244. SDL_free(value1);
  245. SDL_free(value2);
  246. return TEST_COMPLETED;
  247. }
  248. /**
  249. * @brief Call to SDL_sscanf
  250. */
  251. #undef SDL_sscanf
  252. int
  253. stdlib_sscanf(void *arg)
  254. {
  255. int output;
  256. int result;
  257. int expected_output;
  258. int expected_result;
  259. expected_output = output = 123;
  260. expected_result = -1;
  261. result = SDL_sscanf("", "%i", &output);
  262. SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)");
  263. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  264. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  265. expected_output = output = 123;
  266. expected_result = 0;
  267. result = SDL_sscanf("a", "%i", &output);
  268. SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)");
  269. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  270. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  271. output = 123;
  272. expected_output = 2;
  273. expected_result = 1;
  274. result = SDL_sscanf("2", "%i", &output);
  275. SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i\", &output)");
  276. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  277. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  278. return TEST_COMPLETED;
  279. }
  280. /* ================= Test References ================== */
  281. /* Standard C routine test cases */
  282. static const SDLTest_TestCaseReference stdlibTest1 =
  283. { (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED };
  284. static const SDLTest_TestCaseReference stdlibTest2 =
  285. { (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED };
  286. static const SDLTest_TestCaseReference stdlibTest3 =
  287. { (SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED };
  288. static const SDLTest_TestCaseReference stdlibTest4 =
  289. { (SDLTest_TestCaseFp)stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED };
  290. /* Sequence of Standard C routine test cases */
  291. static const SDLTest_TestCaseReference *stdlibTests[] = {
  292. &stdlibTest1, &stdlibTest2, &stdlibTest3, &stdlibTest4, NULL
  293. };
  294. /* Standard C routine test suite (global) */
  295. SDLTest_TestSuiteReference stdlibTestSuite = {
  296. "Stdlib",
  297. NULL,
  298. stdlibTests,
  299. NULL
  300. };