testautomation_stdlib.c 12 KB

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