testautomation_stdlib.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. /**
  2. * Standard C library routine test suite
  3. */
  4. #include <SDL3/SDL.h>
  5. #include <SDL3/SDL_test.h>
  6. #include "testautomation_suites.h"
  7. /* Test case functions */
  8. /**
  9. * Call to SDL_strnlen
  10. */
  11. static int SDLCALL stdlib_strnlen(void *arg)
  12. {
  13. size_t result;
  14. char *text_result;
  15. const char *text = "food";
  16. const char *expected;
  17. result = SDL_strnlen(text, 6);
  18. SDLTest_AssertPass("Call to SDL_strndup(\"food\", 6)");
  19. SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", (int)result);
  20. result = SDL_strnlen(text, 3);
  21. SDLTest_AssertPass("Call to SDL_strndup(\"food\", 3)");
  22. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int)result);
  23. text_result = SDL_strndup(text, 3);
  24. expected = "foo";
  25. SDLTest_AssertPass("Call to SDL_strndup(\"food\", 3)");
  26. SDLTest_AssertCheck(SDL_strcmp(text_result, expected) == 0, "Check text, expected: %s, got: %s", expected, text_result);
  27. SDL_free(text_result);
  28. return TEST_COMPLETED;
  29. }
  30. /**
  31. * Call to SDL_strlcpy
  32. */
  33. static int SDLCALL stdlib_strlcpy(void *arg)
  34. {
  35. size_t result;
  36. char text[1024];
  37. const char *expected;
  38. result = SDL_strlcpy(text, "foo", sizeof(text));
  39. expected = "foo";
  40. SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
  41. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  42. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), (int)result);
  43. result = SDL_strlcpy(text, "foo", 2);
  44. expected = "f";
  45. SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
  46. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  47. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int)result);
  48. return TEST_COMPLETED;
  49. }
  50. /**
  51. * Call to SDL_strstr
  52. */
  53. static int SDLCALL stdlib_strstr(void *arg)
  54. {
  55. char *result;
  56. const char *text = "abcdef";
  57. const char *expected;
  58. result = SDL_strstr(text, "");
  59. expected = text;
  60. SDLTest_AssertPass("Call to SDL_strstr(text, \"\")");
  61. SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
  62. result = SDL_strstr(text, "abc");
  63. expected = text;
  64. SDLTest_AssertPass("Call to SDL_strstr(text, \"abc\")");
  65. SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
  66. result = SDL_strstr(text, "bcd");
  67. expected = text+1;
  68. SDLTest_AssertPass("Call to SDL_strstr(text, \"bcd\")");
  69. SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
  70. result = SDL_strstr(text, "xyz");
  71. expected = NULL;
  72. SDLTest_AssertPass("Call to SDL_strstr(text, \"xyz\")");
  73. SDLTest_AssertCheck(result == expected, "Check result, expected: (null), got: %s", result);
  74. result = SDL_strnstr(text, "", SDL_strlen(text));
  75. expected = text;
  76. SDLTest_AssertPass("Call to SDL_strnstr(text, \"\", SDL_strlen(text))");
  77. SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
  78. result = SDL_strnstr(text, "abc", SDL_strlen(text));
  79. expected = text;
  80. SDLTest_AssertPass("Call to SDL_strnstr(text, \"abc\", SDL_strlen(text))");
  81. SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
  82. result = SDL_strnstr(text, "bcd", SDL_strlen(text));
  83. expected = text+1;
  84. SDLTest_AssertPass("Call to SDL_strnstr(text, \"bcd\", SDL_strlen(text))");
  85. SDLTest_AssertCheck(result == expected, "Check result, expected: %s, got: %s", expected, result);
  86. result = SDL_strnstr(text, "bcd", 3);
  87. expected = NULL;
  88. SDLTest_AssertPass("Call to SDL_strnstr(text, \"bcd\", 3)");
  89. SDLTest_AssertCheck(result == expected, "Check result, expected: (null), got: %s", result);
  90. result = SDL_strnstr(text, "xyz", 3);
  91. expected = NULL;
  92. SDLTest_AssertPass("Call to SDL_strnstr(text, \"xyz\", 3)");
  93. SDLTest_AssertCheck(result == expected, "Check result, expected: (null), got: %s", result);
  94. result = SDL_strnstr(text, "xyz", SDL_strlen(text)*100000);
  95. expected = NULL;
  96. SDLTest_AssertPass("Call to SDL_strnstr(text, \"xyz\", SDL_strlen(text)*100000)");
  97. SDLTest_AssertCheck(result == expected, "Check result, expected: (null), got: %s", result);
  98. return TEST_COMPLETED;
  99. }
  100. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  101. #pragma GCC diagnostic push
  102. #ifdef HAVE_WFORMAT
  103. #pragma GCC diagnostic ignored "-Wformat"
  104. #endif
  105. #ifdef HAVE_WFORMAT_EXTRA_ARGS
  106. #pragma GCC diagnostic ignored "-Wformat-extra-args"
  107. #endif
  108. #endif
  109. /**
  110. * Call to SDL_snprintf
  111. */
  112. static int SDLCALL stdlib_snprintf(void *arg)
  113. {
  114. int result;
  115. int predicted;
  116. char text[1024];
  117. const char *expected, *expected2, *expected3, *expected4, *expected5;
  118. size_t size;
  119. result = SDL_snprintf(text, sizeof(text), "%s", "foo");
  120. expected = "foo";
  121. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
  122. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  123. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  124. result = SDL_snprintf(text, sizeof(text), "%10sA", "foo");
  125. expected = " fooA";
  126. SDLTest_AssertPass("Call to SDL_snprintf(\"%%10sA\", \"foo\")");
  127. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  128. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  129. result = SDL_snprintf(text, sizeof(text), "%-10sA", "foo");
  130. expected = "foo A";
  131. SDLTest_AssertPass("Call to SDL_snprintf(\"%%-10sA\", \"foo\")");
  132. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  133. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  134. result = SDL_snprintf(text, sizeof(text), "%S", L"foo");
  135. expected = "foo";
  136. SDLTest_AssertPass("Call to SDL_snprintf(\"%%S\", \"foo\")");
  137. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  138. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  139. result = SDL_snprintf(text, sizeof(text), "%ls", L"foo");
  140. expected = "foo";
  141. SDLTest_AssertPass("Call to SDL_snprintf(\"%%ls\", \"foo\")");
  142. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  143. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  144. result = SDL_snprintf(text, 2, "%s", "foo");
  145. expected = "f";
  146. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
  147. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  148. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  149. result = SDL_snprintf(NULL, 0, "%s", "foo");
  150. SDLTest_AssertPass("Call to SDL_snprintf(NULL, 0, \"%%s\", \"foo\")");
  151. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  152. result = SDL_snprintf(text, 2, "%s\n", "foo");
  153. expected = "f";
  154. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\\n\", \"foo\") with buffer size 2");
  155. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  156. SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", result);
  157. result = SDL_snprintf(text, sizeof(text), "%f", 0.0);
  158. predicted = SDL_snprintf(NULL, 0, "%f", 0.0);
  159. expected = "0.000000";
  160. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 0.0)");
  161. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  162. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  163. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  164. result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
  165. predicted = SDL_snprintf(NULL, 0, "%f", 1.0);
  166. expected = "1.000000";
  167. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
  168. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  169. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  170. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  171. result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
  172. predicted = SDL_snprintf(NULL, 0, "%.f", 1.0);
  173. expected = "1";
  174. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
  175. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  176. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  177. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  178. result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
  179. predicted = SDL_snprintf(NULL, 0, "%#.f", 1.0);
  180. expected = "1.";
  181. SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
  182. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  183. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  184. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  185. result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
  186. predicted = SDL_snprintf(NULL, 0, "%f", 1.0 + 1.0 / 3.0);
  187. expected = "1.333333";
  188. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
  189. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  190. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  191. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  192. result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
  193. predicted = SDL_snprintf(NULL, 0, "%+f", 1.0 + 1.0 / 3.0);
  194. expected = "+1.333333";
  195. SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
  196. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  197. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  198. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  199. result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
  200. predicted = SDL_snprintf(NULL, 0, "%.2f", 1.0 + 1.0 / 3.0);
  201. expected = "1.33";
  202. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
  203. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  204. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  205. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  206. result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
  207. predicted = SDL_snprintf(NULL, 0, "%6.2f", 1.0 + 1.0 / 3.0);
  208. expected = " 1.33";
  209. SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
  210. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  211. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  212. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  213. result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
  214. predicted = SDL_snprintf(NULL, 0, "%06.2f", 1.0 + 1.0 / 3.0);
  215. expected = "001.33";
  216. SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
  217. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  218. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int)SDL_strlen(text), result);
  219. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  220. result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
  221. expected = "001.";
  222. SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
  223. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  224. SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
  225. {
  226. static struct
  227. {
  228. int precision;
  229. float value;
  230. const char *expected_f;
  231. const char *expected_g;
  232. } f_and_g_test_cases[] = {
  233. { 6, 100.0f, "100.000000", "100" },
  234. { 6, -100.0f, "-100.000000", "-100" },
  235. { 6, 100.75f, "100.750000", "100.75" },
  236. { 6, -100.75f, "-100.750000", "-100.75" },
  237. { 6, ((100 * 60 * 1000) / 1001) / 100.0f, "59.939999", "59.94" },
  238. { 6, -((100 * 60 * 1000) / 1001) / 100.0f, "-59.939999", "-59.94" },
  239. { 6, ((100 * 120 * 1000) / 1001) / 100.0f, "119.879997", "119.88" },
  240. { 6, -((100 * 120 * 1000) / 1001) / 100.0f, "-119.879997", "-119.88" },
  241. { 6, 0.9999999f, "1.000000", "1" },
  242. { 6, -0.9999999f, "-1.000000", "-1" },
  243. { 5, 9.999999f, "10.00000", "10" },
  244. { 5, -9.999999f, "-10.00000", "-10" },
  245. };
  246. int i;
  247. for (i = 0; i < SDL_arraysize(f_and_g_test_cases); ++i) {
  248. float value = f_and_g_test_cases[i].value;
  249. int prec = f_and_g_test_cases[i].precision;
  250. result = SDL_snprintf(text, sizeof(text), "%.*f", prec, value);
  251. predicted = SDL_snprintf(NULL, 0, "%.*f", prec, value);
  252. expected = f_and_g_test_cases[i].expected_f;
  253. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.5f\", %g)", value);
  254. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  255. SDLTest_AssertCheck(result == SDL_strlen(expected), "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
  256. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  257. result = SDL_snprintf(text, sizeof(text), "%g", value);
  258. predicted = SDL_snprintf(NULL, 0, "%g", value);
  259. expected = f_and_g_test_cases[i].expected_g;
  260. SDLTest_AssertPass("Call to SDL_snprintf(\"%%g\", %g)", value);
  261. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  262. SDLTest_AssertCheck(result == SDL_strlen(expected), "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
  263. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  264. }
  265. }
  266. size = 64;
  267. result = SDL_snprintf(text, sizeof(text), "%zu %s", size, "test");
  268. expected = "64 test";
  269. SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%zu %%s\", size, \"test\")");
  270. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  271. SDLTest_AssertCheck(result == 7, "Check result value, expected: 7, got: %d", result);
  272. result = SDL_snprintf(text, sizeof(text), "%p", (void *)0x1234abcd);
  273. expected = "0x1234abcd";
  274. expected2 = "1234ABCD";
  275. expected3 = "000000001234ABCD";
  276. expected4 = "1234abcd";
  277. expected5 = "000000001234abcd";
  278. SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%p\", 0x1234abcd)");
  279. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 ||
  280. SDL_strcmp(text, expected2) == 0 ||
  281. SDL_strcmp(text, expected3) == 0 ||
  282. SDL_strcmp(text, expected4) == 0 ||
  283. SDL_strcmp(text, expected5) == 0,
  284. "Check text, expected: '%s', got: '%s'", expected, text);
  285. SDLTest_AssertCheck(result == SDL_strlen(expected) ||
  286. result == SDL_strlen(expected2) ||
  287. result == SDL_strlen(expected3) ||
  288. result == SDL_strlen(expected4) ||
  289. result == SDL_strlen(expected5),
  290. "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
  291. result = SDL_snprintf(text, sizeof(text), "A %p B", (void *)0x1234abcd);
  292. expected = "A 0x1234abcd B";
  293. expected2 = "A 1234ABCD B";
  294. expected3 = "A 000000001234ABCD B";
  295. expected4 = "A 1234abcd B";
  296. expected5 = "A 000000001234abcd B";
  297. SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"A %%p B\", 0x1234abcd)");
  298. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 ||
  299. SDL_strcmp(text, expected2) == 0 ||
  300. SDL_strcmp(text, expected3) == 0 ||
  301. SDL_strcmp(text, expected4) == 0 ||
  302. SDL_strcmp(text, expected5) == 0,
  303. "Check text, expected: '%s', got: '%s'", expected, text);
  304. SDLTest_AssertCheck(result == SDL_strlen(expected) ||
  305. result == SDL_strlen(expected2) ||
  306. result == SDL_strlen(expected3) ||
  307. result == SDL_strlen(expected4) ||
  308. result == SDL_strlen(expected5),
  309. "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
  310. if (sizeof(void *) >= 8) {
  311. result = SDL_snprintf(text, sizeof(text), "%p", (void *)0x1ba07bddf60L);
  312. expected = "0x1ba07bddf60";
  313. expected2 = "000001BA07BDDF60";
  314. expected3 = "000001ba07bddf60";
  315. SDLTest_AssertPass("Call to SDL_snprintf(text, sizeof(text), \"%%p\", 0x1ba07bddf60)");
  316. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0 ||
  317. SDL_strcmp(text, expected2) == 0 ||
  318. SDL_strcmp(text, expected3) == 0,
  319. "Check text, expected: '%s', got: '%s'", expected, text);
  320. SDLTest_AssertCheck(result == SDL_strlen(expected) ||
  321. result == SDL_strlen(expected2) ||
  322. result == SDL_strlen(expected3),
  323. "Check result value, expected: %d, got: %d", (int)SDL_strlen(expected), result);
  324. }
  325. return TEST_COMPLETED;
  326. }
  327. /**
  328. * Call to SDL_swprintf
  329. */
  330. static int SDLCALL stdlib_swprintf(void *arg)
  331. {
  332. int result;
  333. int predicted;
  334. wchar_t text[1024];
  335. const wchar_t *expected;
  336. size_t size;
  337. result = SDL_swprintf(text, SDL_arraysize(text), L"%s", "foo");
  338. expected = L"foo";
  339. SDLTest_AssertPass("Call to SDL_swprintf(\"%%s\", \"foo\")");
  340. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  341. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  342. result = SDL_swprintf(text, 2, L"%s", "foo");
  343. expected = L"f";
  344. SDLTest_AssertPass("Call to SDL_swprintf(\"%%s\", \"foo\") with buffer size 2");
  345. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  346. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  347. result = SDL_swprintf(NULL, 0, L"%s", "foo");
  348. SDLTest_AssertPass("Call to SDL_swprintf(NULL, 0, \"%%s\", \"foo\")");
  349. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  350. result = SDL_swprintf(text, 2, L"%s\n", "foo");
  351. expected = L"f";
  352. SDLTest_AssertPass("Call to SDL_swprintf(\"%%s\\n\", \"foo\") with buffer size 2");
  353. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  354. SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", result);
  355. result = SDL_swprintf(text, sizeof(text), L"%f", 0.0);
  356. predicted = SDL_swprintf(NULL, 0, L"%f", 0.0);
  357. expected = L"0.000000";
  358. SDLTest_AssertPass("Call to SDL_swprintf(\"%%f\", 0.0)");
  359. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  360. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  361. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  362. result = SDL_swprintf(text, sizeof(text), L"%f", 1.0);
  363. predicted = SDL_swprintf(NULL, 0, L"%f", 1.0);
  364. expected = L"1.000000";
  365. SDLTest_AssertPass("Call to SDL_swprintf(\"%%f\", 1.0)");
  366. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  367. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  368. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  369. result = SDL_swprintf(text, sizeof(text), L"%.f", 1.0);
  370. predicted = SDL_swprintf(NULL, 0, L"%.f", 1.0);
  371. expected = L"1";
  372. SDLTest_AssertPass("Call to SDL_swprintf(\"%%.f\", 1.0)");
  373. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  374. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  375. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  376. result = SDL_swprintf(text, sizeof(text), L"%#.f", 1.0);
  377. predicted = SDL_swprintf(NULL, 0, L"%#.f", 1.0);
  378. expected = L"1.";
  379. SDLTest_AssertPass("Call to SDL_swprintf(\"%%#.f\", 1.0)");
  380. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  381. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  382. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  383. result = SDL_swprintf(text, sizeof(text), L"%f", 1.0 + 1.0 / 3.0);
  384. predicted = SDL_swprintf(NULL, 0, L"%f", 1.0 + 1.0 / 3.0);
  385. expected = L"1.333333";
  386. SDLTest_AssertPass("Call to SDL_swprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
  387. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  388. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  389. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  390. result = SDL_swprintf(text, sizeof(text), L"%+f", 1.0 + 1.0 / 3.0);
  391. predicted = SDL_swprintf(NULL, 0, L"%+f", 1.0 + 1.0 / 3.0);
  392. expected = L"+1.333333";
  393. SDLTest_AssertPass("Call to SDL_swprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
  394. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  395. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  396. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  397. result = SDL_swprintf(text, sizeof(text), L"%.2f", 1.0 + 1.0 / 3.0);
  398. predicted = SDL_swprintf(NULL, 0, L"%.2f", 1.0 + 1.0 / 3.0);
  399. expected = L"1.33";
  400. SDLTest_AssertPass("Call to SDL_swprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
  401. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: %S, got: %S", expected, text);
  402. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  403. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  404. result = SDL_swprintf(text, sizeof(text), L"%6.2f", 1.0 + 1.0 / 3.0);
  405. predicted = SDL_swprintf(NULL, 0, L"%6.2f", 1.0 + 1.0 / 3.0);
  406. expected = L" 1.33";
  407. SDLTest_AssertPass("Call to SDL_swprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
  408. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
  409. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  410. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  411. result = SDL_swprintf(text, sizeof(text), L"%06.2f", 1.0 + 1.0 / 3.0);
  412. predicted = SDL_swprintf(NULL, 0, L"%06.2f", 1.0 + 1.0 / 3.0);
  413. expected = L"001.33";
  414. SDLTest_AssertPass("Call to SDL_swprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
  415. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
  416. SDLTest_AssertCheck(result == SDL_wcslen(text), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(text), result);
  417. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  418. result = SDL_swprintf(text, 5, L"%06.2f", 1.0 + 1.0 / 3.0);
  419. expected = L"001.";
  420. SDLTest_AssertPass("Call to SDL_swprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
  421. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
  422. SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
  423. {
  424. static struct
  425. {
  426. float value;
  427. const wchar_t *expected_f;
  428. const wchar_t *expected_g;
  429. } f_and_g_test_cases[] = {
  430. { 100.0f, L"100.000000", L"100" },
  431. { -100.0f, L"-100.000000", L"-100" },
  432. { 100.75f, L"100.750000", L"100.75" },
  433. { -100.75f, L"-100.750000", L"-100.75" },
  434. { ((100 * 60 * 1000) / 1001) / 100.0f, L"59.939999", L"59.94" },
  435. { -((100 * 60 * 1000) / 1001) / 100.0f, L"-59.939999", L"-59.94" },
  436. { ((100 * 120 * 1000) / 1001) / 100.0f, L"119.879997", L"119.88" },
  437. { -((100 * 120 * 1000) / 1001) / 100.0f, L"-119.879997", L"-119.88" },
  438. { 9.9999999f, L"10.000000", L"10" },
  439. { -9.9999999f, L"-10.000000", L"-10" },
  440. };
  441. int i;
  442. for (i = 0; i < SDL_arraysize(f_and_g_test_cases); ++i) {
  443. float value = f_and_g_test_cases[i].value;
  444. result = SDL_swprintf(text, sizeof(text), L"%f", value);
  445. predicted = SDL_swprintf(NULL, 0, L"%f", value);
  446. expected = f_and_g_test_cases[i].expected_f;
  447. SDLTest_AssertPass("Call to SDL_swprintf(\"%%f\", %g)", value);
  448. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
  449. SDLTest_AssertCheck(result == SDL_wcslen(expected), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(expected), result);
  450. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  451. result = SDL_swprintf(text, sizeof(text), L"%g", value);
  452. predicted = SDL_swprintf(NULL, 0, L"%g", value);
  453. expected = f_and_g_test_cases[i].expected_g;
  454. SDLTest_AssertPass("Call to SDL_swprintf(\"%%g\", %g)", value);
  455. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
  456. SDLTest_AssertCheck(result == SDL_wcslen(expected), "Check result value, expected: %d, got: %d", (int)SDL_wcslen(expected), result);
  457. SDLTest_AssertCheck(predicted == result, "Check predicted value, expected: %d, got: %d", result, predicted);
  458. }
  459. }
  460. size = 64;
  461. result = SDL_swprintf(text, sizeof(text), L"%zu %s", size, "test");
  462. expected = L"64 test";
  463. SDLTest_AssertPass("Call to SDL_swprintf(text, sizeof(text), \"%%zu %%s\", size, \"test\")");
  464. SDLTest_AssertCheck(SDL_wcscmp(text, expected) == 0, "Check text, expected: '%S', got: '%S'", expected, text);
  465. SDLTest_AssertCheck(result == 7, "Check result value, expected: 7, got: %d", result);
  466. return TEST_COMPLETED;
  467. }
  468. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  469. #pragma GCC diagnostic pop
  470. #endif
  471. /**
  472. * Call to SDL_getenv and SDL_setenv
  473. */
  474. static int SDLCALL stdlib_getsetenv(void *arg)
  475. {
  476. const int nameLen = 16;
  477. char name[17];
  478. int counter;
  479. int result;
  480. char *value1;
  481. char *value2;
  482. char *expected;
  483. int overwrite;
  484. const char *text;
  485. /* Create a random name. This tests SDL_getenv, since we need to */
  486. /* make sure the variable is not set yet (it shouldn't). */
  487. do {
  488. for (counter = 0; counter < nameLen; counter++) {
  489. name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
  490. }
  491. name[nameLen] = '\0';
  492. text = SDL_getenv(name);
  493. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  494. if (text) {
  495. SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, (int)SDL_strlen(text));
  496. }
  497. } while (text);
  498. /* Create random values to set */
  499. value1 = SDLTest_RandomAsciiStringOfSize(10);
  500. value2 = SDLTest_RandomAsciiStringOfSize(10);
  501. /* Set value 1 without overwrite */
  502. overwrite = 0;
  503. expected = value1;
  504. result = SDL_setenv(name, value1, overwrite);
  505. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  506. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  507. /* Check value */
  508. text = SDL_getenv(name);
  509. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  510. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  511. if (text != NULL) {
  512. SDLTest_AssertCheck(
  513. SDL_strcmp(text, expected) == 0,
  514. "Verify returned text, expected: %s, got: %s",
  515. expected,
  516. text);
  517. }
  518. /* Set value 2 with overwrite */
  519. overwrite = 1;
  520. expected = value2;
  521. result = SDL_setenv(name, value2, overwrite);
  522. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite);
  523. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  524. /* Check value */
  525. text = SDL_getenv(name);
  526. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  527. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  528. if (text != NULL) {
  529. SDLTest_AssertCheck(
  530. SDL_strcmp(text, expected) == 0,
  531. "Verify returned text, expected: %s, got: %s",
  532. expected,
  533. text);
  534. }
  535. /* Set value 1 without overwrite */
  536. overwrite = 0;
  537. expected = value2;
  538. result = SDL_setenv(name, value1, overwrite);
  539. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  540. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  541. /* Check value */
  542. text = SDL_getenv(name);
  543. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  544. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  545. if (text != NULL) {
  546. SDLTest_AssertCheck(
  547. SDL_strcmp(text, expected) == 0,
  548. "Verify returned text, expected: %s, got: %s",
  549. expected,
  550. text);
  551. }
  552. /* Set value 1 with overwrite */
  553. overwrite = 1;
  554. expected = value1;
  555. result = SDL_setenv(name, value1, overwrite);
  556. SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
  557. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  558. /* Check value */
  559. text = SDL_getenv(name);
  560. SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
  561. SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
  562. if (text != NULL) {
  563. SDLTest_AssertCheck(
  564. SDL_strcmp(text, expected) == 0,
  565. "Verify returned text, expected: %s, got: %s",
  566. expected,
  567. text);
  568. }
  569. /* Verify setenv() with empty string vs unsetenv() */
  570. result = SDL_setenv("FOO", "1", 1);
  571. SDLTest_AssertPass("Call to SDL_setenv('FOO','1', 1)");
  572. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  573. expected = "1";
  574. text = SDL_getenv("FOO");
  575. SDLTest_AssertPass("Call to SDL_getenv('FOO')");
  576. SDLTest_AssertCheck(text && SDL_strcmp(text, expected) == 0, "Verify returned text, expected: %s, got: %s", expected, text);
  577. result = SDL_setenv("FOO", "", 1);
  578. SDLTest_AssertPass("Call to SDL_setenv('FOO','', 1)");
  579. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  580. expected = "";
  581. text = SDL_getenv("FOO");
  582. SDLTest_AssertPass("Call to SDL_getenv('FOO')");
  583. SDLTest_AssertCheck(text && SDL_strcmp(text, expected) == 0, "Verify returned text, expected: '%s', got: '%s'", expected, text);
  584. result = SDL_unsetenv("FOO");
  585. SDLTest_AssertPass("Call to SDL_unsetenv('FOO')");
  586. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  587. text = SDL_getenv("FOO");
  588. SDLTest_AssertPass("Call to SDL_getenv('FOO')");
  589. SDLTest_AssertCheck(text == NULL, "Verify returned text, expected: (null), got: %s", text);
  590. result = SDL_setenv("FOO", "0", 0);
  591. SDLTest_AssertPass("Call to SDL_setenv('FOO','0', 0)");
  592. SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
  593. expected = "0";
  594. text = SDL_getenv("FOO");
  595. SDLTest_AssertPass("Call to SDL_getenv('FOO')");
  596. SDLTest_AssertCheck(text && SDL_strcmp(text, expected) == 0, "Verify returned text, expected: %s, got: %s", expected, text);
  597. /* Negative cases */
  598. for (overwrite = 0; overwrite <= 1; overwrite++) {
  599. result = SDL_setenv(NULL, value1, overwrite);
  600. SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite);
  601. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  602. result = SDL_setenv("", value1, overwrite);
  603. SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite);
  604. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  605. result = SDL_setenv("=", value1, overwrite);
  606. SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite);
  607. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  608. result = SDL_setenv(name, NULL, overwrite);
  609. SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite);
  610. SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
  611. }
  612. /* Clean up */
  613. SDL_free(value1);
  614. SDL_free(value2);
  615. return TEST_COMPLETED;
  616. }
  617. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  618. #pragma GCC diagnostic push
  619. #ifdef HAVE_WFORMAT
  620. #pragma GCC diagnostic ignored "-Wformat"
  621. #endif
  622. #ifdef HAVE_WFORMAT_EXTRA_ARGS
  623. #pragma GCC diagnostic ignored "-Wformat-extra-args"
  624. #endif
  625. #endif
  626. /**
  627. * Call to SDL_sscanf
  628. */
  629. static int SDLCALL stdlib_sscanf(void *arg)
  630. {
  631. int output;
  632. int result;
  633. int expected_output;
  634. int expected_result;
  635. short short_output, expected_short_output;
  636. long long_output, expected_long_output;
  637. long long long_long_output, expected_long_long_output;
  638. size_t size_output, expected_size_output;
  639. uintptr_t uintptr_output, expected_uintptr_output;
  640. char text[128], text2[128];
  641. expected_output = output = 123;
  642. expected_result = -1;
  643. result = SDL_sscanf("", "%i", &output);
  644. SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)");
  645. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  646. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  647. expected_output = output = 123;
  648. expected_result = 0;
  649. result = SDL_sscanf("a", "%i", &output);
  650. SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)");
  651. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  652. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  653. output = 123;
  654. expected_output = 2;
  655. expected_result = 1;
  656. result = SDL_sscanf("2", "%i", &output);
  657. SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i\", &output)");
  658. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  659. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  660. output = 123;
  661. expected_output = 0xa;
  662. expected_result = 1;
  663. result = SDL_sscanf("aa", "%1x", &output);
  664. SDLTest_AssertPass("Call to SDL_sscanf(\"aa\", \"%%1x\", &output)");
  665. SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
  666. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  667. #define SIZED_TEST_CASE(type, var, format_specifier) \
  668. var##_output = 123; \
  669. expected_##var##_output = (type)(((unsigned type)(~0)) >> 1); \
  670. expected_result = 1; \
  671. result = SDL_snprintf(text, sizeof(text), format_specifier, expected_##var##_output); \
  672. result = SDL_sscanf(text, format_specifier, &var##_output); \
  673. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%s\", &output)", text, #format_specifier); \
  674. SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " format_specifier ", got: " format_specifier, expected_##var##_output, var##_output); \
  675. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result); \
  676. \
  677. var##_output = 123; \
  678. expected_##var##_output = ~(type)(((unsigned type)(~0)) >> 1); \
  679. expected_result = 1; \
  680. result = SDL_snprintf(text, sizeof(text), format_specifier, expected_##var##_output); \
  681. result = SDL_sscanf(text, format_specifier, &var##_output); \
  682. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%s\", &output)", text, #format_specifier); \
  683. SDLTest_AssertCheck(expected_##var##_output == var##_output, "Check output, expected: " format_specifier ", got: " format_specifier, expected_##var##_output, var##_output); \
  684. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  685. SIZED_TEST_CASE(short, short, "%hd")
  686. SIZED_TEST_CASE(long, long, "%ld")
  687. SIZED_TEST_CASE(long long, long_long, "%lld")
  688. size_output = 123;
  689. expected_size_output = ~((size_t)0);
  690. expected_result = 1;
  691. result = SDL_snprintf(text, sizeof(text), "%zu", expected_size_output);
  692. result = SDL_sscanf(text, "%zu", &size_output);
  693. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%zu\", &output)", text);
  694. SDLTest_AssertCheck(expected_size_output == size_output, "Check output, expected: %zu, got: %zu", expected_size_output, size_output);
  695. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  696. uintptr_output = 123;
  697. expected_uintptr_output = 0x1234567;
  698. expected_result = 1;
  699. result = SDL_snprintf(text, sizeof(text), "%p", expected_uintptr_output);
  700. result = SDL_sscanf(text, "%p", &uintptr_output);
  701. SDLTest_AssertPass("Call to SDL_sscanf(\"%s\", \"%%p\", &output)", text);
  702. SDLTest_AssertCheck(expected_uintptr_output == uintptr_output, "Check output, expected: %p, got: %p", expected_uintptr_output, uintptr_output);
  703. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  704. expected_result = 1;
  705. text[0] = '\0';
  706. result = SDL_sscanf("abc def", "%s", text);
  707. SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%s\", text)");
  708. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  709. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  710. expected_result = 1;
  711. text[0] = '\0';
  712. result = SDL_sscanf("abc,def", "%s", text);
  713. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%s\", text)");
  714. SDLTest_AssertCheck(SDL_strcmp(text, "abc,def") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  715. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  716. expected_result = 1;
  717. text[0] = '\0';
  718. result = SDL_sscanf("abc,def", "%[cba]", text);
  719. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[cba]\", text)");
  720. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  721. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  722. expected_result = 1;
  723. text[0] = '\0';
  724. result = SDL_sscanf("abc,def", "%[a-z]", text);
  725. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[z-a]\", text)");
  726. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  727. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  728. expected_result = 1;
  729. text[0] = '\0';
  730. result = SDL_sscanf("abc,def", "%[^,]", text);
  731. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[^,]\", text)");
  732. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  733. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  734. expected_result = 0;
  735. text[0] = '\0';
  736. result = SDL_sscanf("abc,def", "%[A-Z]", text);
  737. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[A-Z]\", text)");
  738. SDLTest_AssertCheck(SDL_strcmp(text, "") == 0, "Check output, expected: \"\", got: \"%s\"", text);
  739. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  740. expected_result = 2;
  741. text[0] = '\0';
  742. text2[0] = '\0';
  743. result = SDL_sscanf("abc,def", "%[abc],%[def]", text, text2);
  744. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc],%%[def]\", text)");
  745. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  746. SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
  747. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  748. expected_result = 2;
  749. text[0] = '\0';
  750. text2[0] = '\0';
  751. result = SDL_sscanf("abc,def", "%[abc]%*[,]%[def]", text, text2);
  752. SDLTest_AssertPass("Call to SDL_sscanf(\"abc,def\", \"%%[abc]%%*[,]%%[def]\", text)");
  753. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  754. SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
  755. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  756. expected_result = 2;
  757. text[0] = '\0';
  758. text2[0] = '\0';
  759. result = SDL_sscanf("abc def", "%[abc] %[def]", text, text2);
  760. SDLTest_AssertPass("Call to SDL_sscanf(\"abc def\", \"%%[abc] %%[def]\", text)");
  761. SDLTest_AssertCheck(SDL_strcmp(text, "abc") == 0, "Check output, expected: \"abc\", got: \"%s\"", text);
  762. SDLTest_AssertCheck(SDL_strcmp(text2, "def") == 0, "Check output, expected: \"def\", got: \"%s\"", text2);
  763. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  764. expected_result = 1;
  765. text[0] = '\0';
  766. result = SDL_sscanf("abc123XYZ", "%[a-zA-Z0-9]", text);
  767. SDLTest_AssertPass("Call to SDL_sscanf(\"abc123XYZ\", \"%%[a-zA-Z0-9]\", text)");
  768. SDLTest_AssertCheck(SDL_strcmp(text, "abc123XYZ") == 0, "Check output, expected: \"abc123XYZ\", got: \"%s\"", text);
  769. SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
  770. return TEST_COMPLETED;
  771. }
  772. #if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
  773. #pragma GCC diagnostic pop
  774. #endif
  775. #ifdef _WIN64
  776. #define SIZE_FORMAT "I64u"
  777. #elif defined(SDL_PLATFORM_WIN32)
  778. #define SIZE_FORMAT "I32u"
  779. #else
  780. #define SIZE_FORMAT "zu"
  781. #endif
  782. /**
  783. * Call to SDL_aligned_alloc
  784. */
  785. static int SDLCALL stdlib_aligned_alloc(void *arg)
  786. {
  787. size_t i, alignment;
  788. void *ptr;
  789. for (i = 0; i < 2*sizeof(void *); ++i) {
  790. SDLTest_AssertPass("Call to SDL_aligned_alloc(%"SIZE_FORMAT")", i);
  791. ptr = SDL_aligned_alloc(i, 1);
  792. if (i < sizeof(void *)) {
  793. alignment = sizeof(void *);
  794. } else {
  795. alignment = i;
  796. }
  797. SDLTest_AssertCheck(ptr != NULL, "Check output, expected non-NULL, got: %p", ptr);
  798. SDLTest_AssertCheck((((size_t)ptr) % alignment) == 0, "Check output, expected aligned pointer, actual offset: %"SIZE_FORMAT, (((size_t)ptr) % alignment));
  799. SDLTest_AssertPass("Filling memory to alignment value");
  800. SDL_memset(ptr, 0xAA, alignment);
  801. SDL_aligned_free(ptr);
  802. }
  803. return TEST_COMPLETED;
  804. }
  805. typedef struct
  806. {
  807. size_t a;
  808. size_t b;
  809. size_t result;
  810. SDL_bool status;
  811. } overflow_test;
  812. static const overflow_test multiplications[] = {
  813. { 1, 1, 1, SDL_TRUE },
  814. { 0, 0, 0, SDL_TRUE },
  815. { SDL_SIZE_MAX, 0, 0, SDL_TRUE },
  816. { SDL_SIZE_MAX, 1, SDL_SIZE_MAX, SDL_TRUE },
  817. { SDL_SIZE_MAX / 2, 2, SDL_SIZE_MAX - (SDL_SIZE_MAX % 2), SDL_TRUE },
  818. { SDL_SIZE_MAX / 23, 23, SDL_SIZE_MAX - (SDL_SIZE_MAX % 23), SDL_TRUE },
  819. { (SDL_SIZE_MAX / 2) + 1, 2, 0, SDL_FALSE },
  820. { (SDL_SIZE_MAX / 23) + 42, 23, 0, SDL_FALSE },
  821. { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, SDL_FALSE },
  822. };
  823. static const overflow_test additions[] = {
  824. { 1, 1, 2, SDL_TRUE },
  825. { 0, 0, 0, SDL_TRUE },
  826. { SDL_SIZE_MAX, 0, SDL_SIZE_MAX, SDL_TRUE },
  827. { SDL_SIZE_MAX - 1, 1, SDL_SIZE_MAX, SDL_TRUE },
  828. { SDL_SIZE_MAX - 42, 23, SDL_SIZE_MAX - (42 - 23), SDL_TRUE },
  829. { SDL_SIZE_MAX, 1, 0, SDL_FALSE },
  830. { SDL_SIZE_MAX, 23, 0, SDL_FALSE },
  831. { SDL_SIZE_MAX, SDL_SIZE_MAX, 0, SDL_FALSE },
  832. };
  833. static int SDLCALL
  834. stdlib_overflow(void *arg)
  835. {
  836. size_t i;
  837. size_t useBuiltin;
  838. for (useBuiltin = 0; useBuiltin < 2; useBuiltin++) {
  839. if (useBuiltin) {
  840. SDLTest_Log("Using gcc/clang builtins if possible");
  841. } else {
  842. SDLTest_Log("Not using gcc/clang builtins");
  843. }
  844. for (i = 0; i < SDL_arraysize(multiplications); i++) {
  845. const overflow_test *t = &multiplications[i];
  846. int status;
  847. size_t result = ~t->result;
  848. if (useBuiltin) {
  849. status = SDL_size_mul_check_overflow(t->a, t->b, &result);
  850. } else {
  851. /* This disables the macro that tries to use a gcc/clang
  852. * builtin, so we test the fallback implementation instead. */
  853. status = (SDL_size_mul_check_overflow)(t->a, t->b, &result);
  854. }
  855. if (t->status) {
  856. SDLTest_AssertCheck(status,
  857. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
  858. t->a, t->b);
  859. SDLTest_AssertCheck(result == t->result,
  860. "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  861. t->a, t->b, t->result, result);
  862. } else {
  863. SDLTest_AssertCheck(!status,
  864. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
  865. t->a, t->b);
  866. }
  867. if (t->a == t->b) {
  868. continue;
  869. }
  870. result = ~t->result;
  871. if (useBuiltin) {
  872. status = SDL_size_mul_check_overflow(t->b, t->a, &result);
  873. } else {
  874. status = (SDL_size_mul_check_overflow)(t->b, t->a, &result);
  875. }
  876. if (t->status) {
  877. SDLTest_AssertCheck(status,
  878. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should succeed",
  879. t->b, t->a);
  880. SDLTest_AssertCheck(result == t->result,
  881. "(%" SIZE_FORMAT " * %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  882. t->b, t->a, t->result, result);
  883. } else {
  884. SDLTest_AssertCheck(!status,
  885. "(%" SIZE_FORMAT " * %" SIZE_FORMAT ") should fail",
  886. t->b, t->a);
  887. }
  888. }
  889. for (i = 0; i < SDL_arraysize(additions); i++) {
  890. const overflow_test *t = &additions[i];
  891. SDL_bool status;
  892. size_t result = ~t->result;
  893. if (useBuiltin) {
  894. status = SDL_size_add_check_overflow(t->a, t->b, &result);
  895. } else {
  896. status = (SDL_size_add_check_overflow)(t->a, t->b, &result);
  897. }
  898. if (t->status) {
  899. SDLTest_AssertCheck(status,
  900. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
  901. t->a, t->b);
  902. SDLTest_AssertCheck(result == t->result,
  903. "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  904. t->a, t->b, t->result, result);
  905. } else {
  906. SDLTest_AssertCheck(!status,
  907. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
  908. t->a, t->b);
  909. }
  910. if (t->a == t->b) {
  911. continue;
  912. }
  913. result = ~t->result;
  914. if (useBuiltin) {
  915. status = SDL_size_add_check_overflow(t->b, t->a, &result);
  916. } else {
  917. status = (SDL_size_add_check_overflow)(t->b, t->a, &result);
  918. }
  919. if (t->status) {
  920. SDLTest_AssertCheck(status,
  921. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should succeed",
  922. t->b, t->a);
  923. SDLTest_AssertCheck(result == t->result,
  924. "(%" SIZE_FORMAT " + %" SIZE_FORMAT "): expected %" SIZE_FORMAT ", got %" SIZE_FORMAT,
  925. t->b, t->a, t->result, result);
  926. } else {
  927. SDLTest_AssertCheck(!status,
  928. "(%" SIZE_FORMAT " + %" SIZE_FORMAT ") should fail",
  929. t->b, t->a);
  930. }
  931. }
  932. }
  933. return TEST_COMPLETED;
  934. }
  935. static void format_for_description(char *buffer, size_t buflen, const char *text) {
  936. if (text == NULL) {
  937. SDL_strlcpy(buffer, "NULL", buflen);
  938. } else {
  939. SDL_snprintf(buffer, buflen, "\"%s\"", text);
  940. }
  941. }
  942. static int SDLCALL
  943. stdlib_iconv(void *arg)
  944. {
  945. struct {
  946. SDL_bool expect_success;
  947. const char *from_encoding;
  948. const char *text;
  949. const char *to_encoding;
  950. const char *expected;
  951. } inputs[] = {
  952. { SDL_FALSE, "bogus-from-encoding", NULL, "bogus-to-encoding", NULL },
  953. { SDL_FALSE, "bogus-from-encoding", "hello world", "bogus-to-encoding", NULL },
  954. { SDL_FALSE, "bogus-from-encoding", "hello world", "ascii", NULL },
  955. { SDL_TRUE, "utf-8", NULL, "ascii", "" },
  956. { SDL_TRUE, "utf-8", "hello world", "ascii", "hello world" },
  957. { SDL_TRUE, "utf-8", "\xe2\x8c\xa8\xf0\x9f\x92\xbb", "utf-16le", "\x28\x23\x3d\xd8\xbb\xdc\x00" },
  958. };
  959. SDL_iconv_t cd;
  960. size_t i;
  961. for (i = 0; i < SDL_arraysize(inputs); i++) {
  962. char to_encoding_str[32];
  963. char from_encoding_str[32];
  964. char text_str[32];
  965. size_t len_text = 0;
  966. int r;
  967. char out_buffer[6];
  968. const char *in_ptr;
  969. size_t in_pos;
  970. char *out_ptr;
  971. char *output;
  972. size_t iconv_result;
  973. size_t out_len;
  974. SDL_bool is_error;
  975. size_t out_pos;
  976. SDLTest_AssertPass("case %d", (int)i);
  977. format_for_description(to_encoding_str, SDL_arraysize(to_encoding_str), inputs[i].to_encoding);
  978. format_for_description(from_encoding_str, SDL_arraysize(from_encoding_str), inputs[i].from_encoding);
  979. format_for_description(text_str, SDL_arraysize(text_str), inputs[i].text);
  980. if (inputs[i].text) {
  981. len_text = SDL_strlen(inputs[i].text) + 1;
  982. }
  983. SDLTest_AssertPass("About to call SDL_iconv_open(%s, %s)", to_encoding_str, from_encoding_str);
  984. cd = SDL_iconv_open(inputs[i].to_encoding, inputs[i].from_encoding);
  985. if (inputs[i].expect_success) {
  986. SDLTest_AssertCheck(cd != (SDL_iconv_t)SDL_ICONV_ERROR, "result must NOT be SDL_ICONV_ERROR");
  987. } else {
  988. SDLTest_AssertCheck(cd == (SDL_iconv_t)SDL_ICONV_ERROR, "result must be SDL_ICONV_ERROR");
  989. }
  990. in_ptr = inputs[i].text;
  991. in_pos = 0;
  992. out_pos = 0;
  993. do {
  994. size_t in_left;
  995. size_t count_written;
  996. size_t count_read;
  997. in_left = len_text - in_pos;
  998. out_ptr = out_buffer;
  999. out_len = SDL_arraysize(out_buffer);
  1000. SDLTest_AssertPass("About to call SDL_iconv(cd, %s+%d, .., dest, ..)", text_str, (int)in_pos);
  1001. iconv_result = SDL_iconv(cd, &in_ptr, &in_left, &out_ptr, &out_len);
  1002. count_written = SDL_arraysize(out_buffer) - out_len;
  1003. count_read = in_ptr - inputs[i].text - in_pos;
  1004. in_pos += count_read;
  1005. is_error = iconv_result == SDL_ICONV_ERROR
  1006. || iconv_result == SDL_ICONV_EILSEQ
  1007. || iconv_result == SDL_ICONV_EINVAL;
  1008. if (inputs[i].expect_success) {
  1009. SDLTest_AssertCheck(!is_error, "result must NOT be an error code");
  1010. SDLTest_AssertCheck(count_written > 0 || inputs[i].expected[out_pos] == '\0', "%" SDL_PRIu64 " bytes have been written", (Uint64)count_written);
  1011. SDLTest_AssertCheck(out_pos <= SDL_strlen(inputs[i].expected), "Data written by SDL_iconv cannot be longer then reference output");
  1012. SDLTest_CompareMemory(out_buffer, count_written, inputs[i].expected + out_pos, count_written);
  1013. } else {
  1014. SDLTest_AssertCheck(is_error, "result must be an error code");
  1015. break;
  1016. }
  1017. out_pos += count_written;
  1018. if (count_written == 0) {
  1019. break;
  1020. }
  1021. if (count_read == 0) {
  1022. SDLTest_AssertCheck(SDL_FALSE, "SDL_iconv wrote data, but read no data");
  1023. break;
  1024. }
  1025. } while (!is_error && in_pos < len_text);
  1026. SDLTest_AssertPass("About to call SDL_iconv_close(cd)");
  1027. r = SDL_iconv_close(cd);
  1028. if (inputs[i].expect_success) {
  1029. SDLTest_AssertCheck(r == 0, "result must be 0");
  1030. } else {
  1031. SDLTest_AssertCheck(r == -1, "result must be -1");
  1032. }
  1033. SDLTest_AssertPass("About to call SDL_iconv_string(%s, %s, %s, %" SDL_PRIu64 ")",
  1034. to_encoding_str, from_encoding_str, text_str, (Uint64)len_text);
  1035. output = SDL_iconv_string(inputs[i].to_encoding, inputs[i].from_encoding, inputs[i].text, len_text);
  1036. if (inputs[i].expect_success) {
  1037. SDLTest_AssertCheck(output != NULL, "result must NOT be NULL");
  1038. SDLTest_AssertCheck(SDL_strncmp(inputs[i].expected, output, SDL_strlen(inputs[i].expected)) == 0,
  1039. "converted string should be correct");
  1040. } else {
  1041. SDLTest_AssertCheck(output == NULL, "result must be NULL");
  1042. }
  1043. SDL_free(output);
  1044. }
  1045. return TEST_COMPLETED;
  1046. }
  1047. static int SDLCALL
  1048. stdlib_strpbrk(void *arg)
  1049. {
  1050. struct {
  1051. const char *input;
  1052. const char *accept;
  1053. int expected[3]; /* negative if NULL */
  1054. } test_cases[] = {
  1055. { "", "", { -1, -1, -1 } },
  1056. { "abc", "", { -1, -1, -1 } },
  1057. { "Abc", "a", { -1, -1, -1 } },
  1058. { "abc", "a", { 0, -1, -1 } },
  1059. { "abcbd", "bbbb", { 1, 3, -1 } },
  1060. { "a;b;c", ";", { 1, 3, -1 } },
  1061. { "a;b;c", ",", { -1, -1, -1 } },
  1062. { "a:bbbb;c", ";:", { 1, 6, -1 } },
  1063. { "Hello\tS DL\n", " \t\r\n", { 5, 7, 10 } },
  1064. };
  1065. int i;
  1066. for (i = 0; i < SDL_arraysize(test_cases); i++) {
  1067. int j;
  1068. const char *input = test_cases[i].input;
  1069. for (j = 0; j < SDL_arraysize(test_cases[i].expected); j++) {
  1070. char *result;
  1071. SDLTest_AssertPass("About to call SDL_strpbrk(\"%s\", \"%s\")", input, test_cases[i].accept);
  1072. result = SDL_strpbrk(input, test_cases[i].accept);
  1073. if (test_cases[i].expected[j] < 0) {
  1074. SDLTest_AssertCheck(result == NULL, "Expected NULL, got %p", result);
  1075. } else {
  1076. SDLTest_AssertCheck(result == test_cases[i].input + test_cases[i].expected[j], "Expected %p, got %p", test_cases[i].input + test_cases[i].expected[j], result);
  1077. input = test_cases[i].input + test_cases[i].expected[j] + 1;
  1078. }
  1079. }
  1080. }
  1081. return TEST_COMPLETED;
  1082. }
  1083. static int SDLCALL stdlib_wcstol(void *arg)
  1084. {
  1085. const wchar_t *text;
  1086. long result;
  1087. wchar_t *endp;
  1088. long expected_result;
  1089. wchar_t *expected_endp;
  1090. // infer decimal
  1091. text = L"\t 123abcxyz"; // skip leading space
  1092. expected_result = 123;
  1093. expected_endp = (wchar_t *)text + 6;
  1094. result = SDL_wcstol(text, &endp, 0);
  1095. SDLTest_AssertPass("Call to SDL_wcstol(L\"\\t 123abcxyz\", &endp, 0)");
  1096. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1097. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1098. text = L"+123abcxyz";
  1099. expected_result = 123;
  1100. expected_endp = (wchar_t *)text + 4;
  1101. result = SDL_wcstol(text, &endp, 0);
  1102. SDLTest_AssertPass("Call to SDL_wcstol(L\"+123abcxyz\", &endp, 0)");
  1103. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1104. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1105. text = L"-123abcxyz";
  1106. expected_result = -123;
  1107. expected_endp = (wchar_t *)text + 4;
  1108. result = SDL_wcstol(text, &endp, 0);
  1109. SDLTest_AssertPass("Call to SDL_wcstol(L\"-123abcxyz\", &endp, 0)");
  1110. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1111. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1112. text = L"99999999999999999999abcxyz";
  1113. expected_result = (~0UL) >> 1; // LONG_MAX
  1114. expected_endp = (wchar_t *)text + 20;
  1115. result = SDL_wcstol(text, &endp, 0);
  1116. SDLTest_AssertPass("Call to SDL_wcstol(L\"99999999999999999999abcxyz\", &endp, 0)");
  1117. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1118. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1119. text = L"-99999999999999999999abcxyz";
  1120. expected_result = ((~0UL) >> 1) + 1UL; // LONG_MIN
  1121. expected_endp = (wchar_t *)text + 21;
  1122. result = SDL_wcstol(text, &endp, 0);
  1123. SDLTest_AssertPass("Call to SDL_wcstol(L\"-99999999999999999999abcxyz\", &endp, 0)");
  1124. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1125. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1126. // infer hexadecimal
  1127. text = L"0x123abcxyz";
  1128. expected_result = 0x123abc;
  1129. expected_endp = (wchar_t *)text + 8;
  1130. result = SDL_wcstol(text, &endp, 0);
  1131. SDLTest_AssertPass("Call to SDL_wcstol(L\"0x123abcxyz\", &endp, 0)");
  1132. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1133. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1134. text = L"0X123ABCXYZ"; // uppercase X
  1135. expected_result = 0x123abc;
  1136. expected_endp = (wchar_t *)text + 8;
  1137. result = SDL_wcstol(text, &endp, 0);
  1138. SDLTest_AssertPass("Call to SDL_wcstol(L\"0X123ABCXYZ\", &endp, 0)");
  1139. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1140. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1141. // infer octal
  1142. text = L"0123abcxyz";
  1143. expected_result = 0123;
  1144. expected_endp = (wchar_t *)text + 4;
  1145. result = SDL_wcstol(text, &endp, 0);
  1146. SDLTest_AssertPass("Call to SDL_wcstol(L\"0123abcxyz\", &endp, 0)");
  1147. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1148. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1149. // arbitrary bases
  1150. text = L"00110011";
  1151. expected_result = 51;
  1152. expected_endp = (wchar_t *)text + 8;
  1153. result = SDL_wcstol(text, &endp, 2);
  1154. SDLTest_AssertPass("Call to SDL_wcstol(L\"00110011\", &endp, 2)");
  1155. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1156. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1157. text = L"-uvwxyz";
  1158. expected_result = -991;
  1159. expected_endp = (wchar_t *)text + 3;
  1160. result = SDL_wcstol(text, &endp, 32);
  1161. SDLTest_AssertPass("Call to SDL_wcstol(L\"-uvwxyz\", &endp, 32)");
  1162. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1163. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1164. text = L"ZzZzZzZzZzZzZ";
  1165. expected_result = (~0UL) >> 1; // LONG_MAX;
  1166. expected_endp = (wchar_t *)text + 13;
  1167. result = SDL_wcstol(text, &endp, 36);
  1168. SDLTest_AssertPass("Call to SDL_wcstol(L\"ZzZzZzZzZzZzZ\", &endp, 36)");
  1169. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1170. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1171. text = L"-0";
  1172. expected_result = 0;
  1173. expected_endp = (wchar_t *)text + 2;
  1174. result = SDL_wcstol(text, &endp, 10);
  1175. SDLTest_AssertPass("Call to SDL_wcstol(L\"-0\", &endp, 10)");
  1176. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1177. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1178. text = L" +0x"; // invalid hexadecimal
  1179. expected_result = 0;
  1180. expected_endp = (wchar_t *)text;
  1181. result = SDL_wcstol(text, &endp, 0);
  1182. SDLTest_AssertPass("Call to SDL_wcstol(L\" +0x\", &endp, 0)");
  1183. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %ld, got: %ld", expected_result, result);
  1184. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1185. return TEST_COMPLETED;
  1186. }
  1187. static int SDLCALL stdlib_strtoull(void *arg)
  1188. {
  1189. const char *text;
  1190. unsigned long long result;
  1191. char *endp;
  1192. unsigned long long expected_result;
  1193. char *expected_endp;
  1194. // infer decimal
  1195. text = "\t 123abcxyz"; // skip leading space
  1196. expected_result = 123;
  1197. expected_endp = (char *)text + 6;
  1198. result = SDL_strtoull(text, &endp, 0);
  1199. SDLTest_AssertPass("Call to SDL_strtoull(\"\\t 123abcxyz\", &endp, 0)");
  1200. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1201. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1202. text = "+123abcxyz";
  1203. expected_result = 123;
  1204. expected_endp = (char *)text + 4;
  1205. result = SDL_strtoull(text, &endp, 0);
  1206. SDLTest_AssertPass("Call to SDL_strtoull(\"+123abcxyz\", &endp, 0)");
  1207. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1208. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1209. text = "-123abcxyz";
  1210. expected_result = 0ULL - 123;
  1211. expected_endp = (char *)text + 4;
  1212. result = SDL_strtoull(text, &endp, 0);
  1213. SDLTest_AssertPass("Call to SDL_strtoull(\"-123abcxyz\", &endp, 0)");
  1214. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1215. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1216. text = "9999999999999999999999999999999999999999abcxyz";
  1217. expected_result = ~0ULL; // ULLONG_MAX
  1218. expected_endp = (char *)text + 40;
  1219. result = SDL_strtoull(text, &endp, 0);
  1220. SDLTest_AssertPass("Call to SDL_strtoull(\"9999999999999999999999999999999999999999abcxyz\", &endp, 0)");
  1221. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1222. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1223. text = "-9999999999999999999999999999999999999999abcxyz";
  1224. expected_result = ~0ULL; // ULLONG_MAX
  1225. expected_endp = (char *)text + 41;
  1226. result = SDL_strtoull(text, &endp, 0);
  1227. SDLTest_AssertPass("Call to SDL_strtoull(\"-9999999999999999999999999999999999999999abcxyz\", &endp, 0)");
  1228. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1229. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1230. // infer hexadecimal
  1231. text = "0x123abcxyz";
  1232. expected_result = 0x123abc;
  1233. expected_endp = (char *)text + 8;
  1234. result = SDL_strtoull(text, &endp, 0);
  1235. SDLTest_AssertPass("Call to SDL_strtoull(\"0x123abcxyz\", &endp, 0)");
  1236. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1237. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1238. text = "0X123ABCXYZ"; // uppercase X
  1239. expected_result = 0x123abc;
  1240. expected_endp = (char *)text + 8;
  1241. result = SDL_strtoull(text, &endp, 0);
  1242. SDLTest_AssertPass("Call to SDL_strtoull(\"0X123ABCXYZ\", &endp, 0)");
  1243. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1244. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1245. // infer octal
  1246. text = "0123abcxyz";
  1247. expected_result = 0123;
  1248. expected_endp = (char *)text + 4;
  1249. result = SDL_strtoull(text, &endp, 0);
  1250. SDLTest_AssertPass("Call to SDL_strtoull(\"0123abcxyz\", &endp, 0)");
  1251. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1252. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1253. // arbitrary bases
  1254. text = "00110011";
  1255. expected_result = 51;
  1256. expected_endp = (char *)text + 8;
  1257. result = SDL_strtoull(text, &endp, 2);
  1258. SDLTest_AssertPass("Call to SDL_strtoull(\"00110011\", &endp, 2)");
  1259. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1260. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1261. text = "-uvwxyz";
  1262. expected_result = -991;
  1263. expected_endp = (char *)text + 3;
  1264. result = SDL_strtoull(text, &endp, 32);
  1265. SDLTest_AssertPass("Call to SDL_strtoull(\"-uvwxyz\", &endp, 32)");
  1266. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1267. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1268. text = "ZzZzZzZzZzZzZzZzZzZzZzZzZ";
  1269. expected_result = ~0ULL; // ULLONG_MAX
  1270. expected_endp = (char *)text + 25;
  1271. result = SDL_strtoull(text, &endp, 36);
  1272. SDLTest_AssertPass("Call to SDL_strtoull(\"ZzZzZzZzZzZzZzZzZzZzZzZzZ\", &endp, 36)");
  1273. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1274. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1275. text = "-0";
  1276. expected_result = 0;
  1277. expected_endp = (char *)text + 2;
  1278. result = SDL_strtoull(text, &endp, 10);
  1279. SDLTest_AssertPass("Call to SDL_strtoull(\"-0\", &endp, 10)");
  1280. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1281. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1282. text = " +0x"; // invalid hexadecimal
  1283. expected_result = 0;
  1284. expected_endp = (char *)text;
  1285. result = SDL_strtoull(text, &endp, 0);
  1286. SDLTest_AssertPass("Call to SDL_strtoull(\" +0x\", &endp, 0)");
  1287. SDLTest_AssertCheck(result == expected_result, "Check result value, expected: %llu, got: %llu", expected_result, result);
  1288. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1289. // We know that SDL_strtol, SDL_strtoul and SDL_strtoll share the same code path as SDL_strtoull under the hood,
  1290. // so the most interesting test cases are those close to the bounds of the integer type.
  1291. // For simplicity, we only run long/long long tests when they are 32-bit/64-bit respectively.
  1292. // Since the tests are run against a variety of targets, this should be fine in practice.
  1293. // SDL_strtol (32-bit)
  1294. if (sizeof(long) == 4) {
  1295. long lresult;
  1296. long expected_lresult;
  1297. text = "2147483647";
  1298. expected_lresult = 2147483647;
  1299. expected_endp = (char *)text + 10;
  1300. lresult = SDL_strtol(text, &endp, 0);
  1301. SDLTest_AssertPass("Call to SDL_strtol(\"2147483647\", &endp, 0)");
  1302. SDLTest_AssertCheck(lresult == expected_lresult, "Check result value, expected: %ld, got: %ld", expected_lresult, lresult);
  1303. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1304. text = "2147483648";
  1305. expected_lresult = 2147483647;
  1306. expected_endp = (char *)text + 10;
  1307. lresult = SDL_strtol(text, &endp, 0);
  1308. SDLTest_AssertPass("Call to SDL_strtol(\"2147483648\", &endp, 0)");
  1309. SDLTest_AssertCheck(lresult == expected_lresult, "Check result value, expected: %ld, got: %ld", expected_lresult, lresult);
  1310. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1311. text = "-2147483648";
  1312. expected_lresult = -2147483648;
  1313. expected_endp = (char *)text + 11;
  1314. lresult = SDL_strtol(text, &endp, 0);
  1315. SDLTest_AssertPass("Call to SDL_strtol(\"-2147483648\", &endp, 0)");
  1316. SDLTest_AssertCheck(lresult == expected_lresult, "Check result value, expected: %ld, got: %ld", expected_lresult, lresult);
  1317. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1318. text = "-2147483649";
  1319. expected_lresult = -2147483648;
  1320. expected_endp = (char *)text + 11;
  1321. lresult = SDL_strtol(text, &endp, 0);
  1322. SDLTest_AssertPass("Call to SDL_strtol(\"-2147483649\", &endp, 0)");
  1323. SDLTest_AssertCheck(lresult == expected_lresult, "Check result value, expected: %ld, got: %ld", expected_lresult, lresult);
  1324. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1325. text = "-9999999999999999999999999999999999999999";
  1326. expected_lresult = -2147483648;
  1327. expected_endp = (char *)text + 41;
  1328. lresult = SDL_strtol(text, &endp, 0);
  1329. SDLTest_AssertPass("Call to SDL_strtol(\"-9999999999999999999999999999999999999999\", &endp, 0)");
  1330. SDLTest_AssertCheck(lresult == expected_lresult, "Check result value, expected: %ld, got: %ld", expected_lresult, lresult);
  1331. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1332. }
  1333. // SDL_strtoul (32-bit)
  1334. if (sizeof(unsigned long) == 4) {
  1335. unsigned long ulresult;
  1336. unsigned long expected_ulresult;
  1337. text = "4294967295";
  1338. expected_ulresult = 4294967295;
  1339. expected_endp = (char *)text + 10;
  1340. ulresult = SDL_strtoul(text, &endp, 0);
  1341. SDLTest_AssertPass("Call to SDL_strtoul(\"4294967295\", &endp, 0)");
  1342. SDLTest_AssertCheck(ulresult == expected_ulresult, "Check result value, expected: %lu, got: %lu", expected_ulresult, ulresult);
  1343. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1344. text = "4294967296";
  1345. expected_ulresult = 4294967295;
  1346. expected_endp = (char *)text + 10;
  1347. ulresult = SDL_strtoul(text, &endp, 0);
  1348. SDLTest_AssertPass("Call to SDL_strtoul(\"4294967296\", &endp, 0)");
  1349. SDLTest_AssertCheck(ulresult == expected_ulresult, "Check result value, expected: %lu, got: %lu", expected_ulresult, ulresult);
  1350. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1351. text = "-4294967295";
  1352. expected_ulresult = 1;
  1353. expected_endp = (char *)text + 11;
  1354. ulresult = SDL_strtoul(text, &endp, 0);
  1355. SDLTest_AssertPass("Call to SDL_strtoul(\"-4294967295\", &endp, 0)");
  1356. SDLTest_AssertCheck(ulresult == expected_ulresult, "Check result value, expected: %lu, got: %lu", expected_ulresult, ulresult);
  1357. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1358. text = "-4294967296";
  1359. expected_ulresult = 4294967295;
  1360. expected_endp = (char *)text + 11;
  1361. ulresult = SDL_strtoul(text, &endp, 0);
  1362. SDLTest_AssertPass("Call to SDL_strtoul(\"-4294967296\", &endp, 0)");
  1363. SDLTest_AssertCheck(ulresult == expected_ulresult, "Check result value, expected: %lu, got: %lu", expected_ulresult, ulresult);
  1364. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1365. text = "-9999999999999999999999999999999999999999";
  1366. expected_ulresult = 4294967295;
  1367. expected_endp = (char *)text + 41;
  1368. ulresult = SDL_strtoul(text, &endp, 0);
  1369. SDLTest_AssertPass("Call to SDL_strtoul(\"-9999999999999999999999999999999999999999\", &endp, 0)");
  1370. SDLTest_AssertCheck(ulresult == expected_ulresult, "Check result value, expected: %lu, got: %lu", expected_ulresult, ulresult);
  1371. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1372. }
  1373. // SDL_strtoll (64-bit)
  1374. if (sizeof(long long) == 8) {
  1375. long long llresult;
  1376. long long expected_llresult;
  1377. text = "9223372036854775807";
  1378. expected_llresult = 9223372036854775807;
  1379. expected_endp = (char *)text + 19;
  1380. llresult = SDL_strtoll(text, &endp, 0);
  1381. SDLTest_AssertPass("Call to SDL_strtoll(\"9223372036854775807\", &endp, 0)");
  1382. SDLTest_AssertCheck(llresult == expected_llresult, "Check result value, expected: %lld, got: %lld", expected_llresult, llresult);
  1383. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1384. text = "9223372036854775808";
  1385. expected_llresult = 9223372036854775807;
  1386. expected_endp = (char *)text + 19;
  1387. llresult = SDL_strtoll(text, &endp, 0);
  1388. SDLTest_AssertPass("Call to SDL_strtoll(\"9223372036854775808\", &endp, 0)");
  1389. SDLTest_AssertCheck(llresult == expected_llresult, "Check result value, expected: %lld, got: %lld", expected_llresult, llresult);
  1390. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1391. text = "-9223372036854775808";
  1392. expected_llresult = -9223372036854775807 - 1;
  1393. expected_endp = (char *)text + 20;
  1394. llresult = SDL_strtoll(text, &endp, 0);
  1395. SDLTest_AssertPass("Call to SDL_strtoll(\"-9223372036854775808\", &endp, 0)");
  1396. SDLTest_AssertCheck(llresult == expected_llresult, "Check result value, expected: %lld, got: %lld", expected_llresult, llresult);
  1397. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1398. text = "-9223372036854775809";
  1399. expected_llresult = -9223372036854775807 - 1;
  1400. expected_endp = (char *)text + 20;
  1401. llresult = SDL_strtoll(text, &endp, 0);
  1402. SDLTest_AssertPass("Call to SDL_strtoll(\"-9223372036854775809\", &endp, 0)");
  1403. SDLTest_AssertCheck(llresult == expected_llresult, "Check result value, expected: %lld, got: %lld", expected_llresult, llresult);
  1404. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1405. text = "-9999999999999999999999999999999999999999";
  1406. expected_llresult = -9223372036854775807 - 1;
  1407. expected_endp = (char *)text + 41;
  1408. llresult = SDL_strtoll(text, &endp, 0);
  1409. SDLTest_AssertPass("Call to SDL_strtoll(\"-9999999999999999999999999999999999999999\", &endp, 0)");
  1410. SDLTest_AssertCheck(llresult == expected_llresult, "Check result value, expected: %lld, got: %lld", expected_llresult, llresult);
  1411. SDLTest_AssertCheck(endp == expected_endp, "Check endp value, expected: %p, got: %p", expected_endp, endp);
  1412. }
  1413. return TEST_COMPLETED;
  1414. }
  1415. /* ================= Test References ================== */
  1416. /* Standard C routine test cases */
  1417. static const SDLTest_TestCaseReference stdlibTest_strnlen = {
  1418. stdlib_strnlen, "stdlib_strnlen", "Call to SDL_strnlen", TEST_ENABLED
  1419. };
  1420. static const SDLTest_TestCaseReference stdlibTest_strlcpy = {
  1421. stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED
  1422. };
  1423. static const SDLTest_TestCaseReference stdlibTest_strstr = {
  1424. stdlib_strstr, "stdlib_strstr", "Call to SDL_strstr", TEST_ENABLED
  1425. };
  1426. static const SDLTest_TestCaseReference stdlibTest_snprintf = {
  1427. stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED
  1428. };
  1429. static const SDLTest_TestCaseReference stdlibTest_swprintf = {
  1430. stdlib_swprintf, "stdlib_swprintf", "Call to SDL_swprintf", TEST_ENABLED
  1431. };
  1432. static const SDLTest_TestCaseReference stdlibTest_getsetenv = {
  1433. stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED
  1434. };
  1435. static const SDLTest_TestCaseReference stdlibTest_sscanf = {
  1436. stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED
  1437. };
  1438. static const SDLTest_TestCaseReference stdlibTest_aligned_alloc = {
  1439. stdlib_aligned_alloc, "stdlib_aligned_alloc", "Call to SDL_aligned_alloc", TEST_ENABLED
  1440. };
  1441. static const SDLTest_TestCaseReference stdlibTestOverflow = {
  1442. stdlib_overflow, "stdlib_overflow", "Overflow detection", TEST_ENABLED
  1443. };
  1444. static const SDLTest_TestCaseReference stdlibTest_iconv = {
  1445. stdlib_iconv, "stdlib_iconv", "Calls to SDL_iconv", TEST_ENABLED
  1446. };
  1447. static const SDLTest_TestCaseReference stdlibTest_strpbrk = {
  1448. stdlib_strpbrk, "stdlib_strpbrk", "Calls to SDL_strpbrk", TEST_ENABLED
  1449. };
  1450. static const SDLTest_TestCaseReference stdlibTest_wcstol = {
  1451. stdlib_wcstol, "stdlib_wcstol", "Calls to SDL_wcstol", TEST_ENABLED
  1452. };
  1453. static const SDLTest_TestCaseReference stdlibTest_strtoull = {
  1454. stdlib_strtoull, "stdlib_strtoull", "Calls to SDL_strtoull, SDL_strtol, SDL_strtoul and SDL_strtoll", TEST_ENABLED
  1455. };
  1456. /* Sequence of Standard C routine test cases */
  1457. static const SDLTest_TestCaseReference *stdlibTests[] = {
  1458. &stdlibTest_strnlen,
  1459. &stdlibTest_strlcpy,
  1460. &stdlibTest_strstr,
  1461. &stdlibTest_snprintf,
  1462. &stdlibTest_swprintf,
  1463. &stdlibTest_getsetenv,
  1464. &stdlibTest_sscanf,
  1465. &stdlibTest_aligned_alloc,
  1466. &stdlibTestOverflow,
  1467. &stdlibTest_iconv,
  1468. &stdlibTest_strpbrk,
  1469. &stdlibTest_wcstol,
  1470. &stdlibTest_strtoull,
  1471. NULL
  1472. };
  1473. /* Standard C routine test suite (global) */
  1474. SDLTest_TestSuiteReference stdlibTestSuite = {
  1475. "Stdlib",
  1476. NULL,
  1477. stdlibTests,
  1478. NULL
  1479. };