lib557.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /*
  25. * The purpose of this test is to minimally exercise libcurl's internal
  26. * curl_m*printf formatting capabilities and handling of some data types.
  27. */
  28. #include "test.h"
  29. #include <limits.h>
  30. #ifdef HAVE_LOCALE_H
  31. # include <locale.h> /* for setlocale() */
  32. #endif
  33. #include "memdebug.h"
  34. #if (SIZEOF_CURL_OFF_T > SIZEOF_LONG)
  35. # define MPRNT_SUFFIX_CURL_OFF_T LL
  36. #else
  37. # define MPRNT_SUFFIX_CURL_OFF_T L
  38. #endif
  39. #ifdef CURL_ISOCPP
  40. # define MPRNT_OFF_T_C_HELPER2(Val,Suffix) Val ## Suffix
  41. #else
  42. # define MPRNT_OFF_T_C_HELPER2(Val,Suffix) Val/**/Suffix
  43. #endif
  44. #define MPRNT_OFF_T_C_HELPER1(Val,Suffix) MPRNT_OFF_T_C_HELPER2(Val,Suffix)
  45. #define MPRNT_OFF_T_C(Val) MPRNT_OFF_T_C_HELPER1(Val,MPRNT_SUFFIX_CURL_OFF_T)
  46. #define BUFSZ 256
  47. #define USHORT_TESTS_ARRSZ 1 + 100
  48. #define SSHORT_TESTS_ARRSZ 1 + 100
  49. #define UINT_TESTS_ARRSZ 1 + 100
  50. #define SINT_TESTS_ARRSZ 1 + 100
  51. #define ULONG_TESTS_ARRSZ 1 + 100
  52. #define SLONG_TESTS_ARRSZ 1 + 100
  53. #define COFFT_TESTS_ARRSZ 1 + 100
  54. struct unsshort_st {
  55. unsigned short num; /* unsigned short */
  56. const char *expected; /* expected string */
  57. char result[BUFSZ]; /* result string */
  58. };
  59. struct sigshort_st {
  60. short num; /* signed short */
  61. const char *expected; /* expected string */
  62. char result[BUFSZ]; /* result string */
  63. };
  64. struct unsint_st {
  65. unsigned int num; /* unsigned int */
  66. const char *expected; /* expected string */
  67. char result[BUFSZ]; /* result string */
  68. };
  69. struct sigint_st {
  70. int num; /* signed int */
  71. const char *expected; /* expected string */
  72. char result[BUFSZ]; /* result string */
  73. };
  74. struct unslong_st {
  75. unsigned long num; /* unsigned long */
  76. const char *expected; /* expected string */
  77. char result[BUFSZ]; /* result string */
  78. };
  79. struct siglong_st {
  80. long num; /* signed long */
  81. const char *expected; /* expected string */
  82. char result[BUFSZ]; /* result string */
  83. };
  84. struct curloff_st {
  85. curl_off_t num; /* curl_off_t */
  86. const char *expected; /* expected string */
  87. char result[BUFSZ]; /* result string */
  88. };
  89. static struct unsshort_st us_test[USHORT_TESTS_ARRSZ];
  90. static struct sigshort_st ss_test[SSHORT_TESTS_ARRSZ];
  91. static struct unsint_st ui_test[UINT_TESTS_ARRSZ];
  92. static struct sigint_st si_test[SINT_TESTS_ARRSZ];
  93. static struct unslong_st ul_test[ULONG_TESTS_ARRSZ];
  94. static struct siglong_st sl_test[SLONG_TESTS_ARRSZ];
  95. static struct curloff_st co_test[COFFT_TESTS_ARRSZ];
  96. static int test_unsigned_short_formatting(void)
  97. {
  98. int i, j;
  99. int num_ushort_tests = 0;
  100. int failed = 0;
  101. i = 1; us_test[i].num = 0xFFFFU; us_test[i].expected = "65535";
  102. i++; us_test[i].num = 0xFF00U; us_test[i].expected = "65280";
  103. i++; us_test[i].num = 0x00FFU; us_test[i].expected = "255";
  104. i++; us_test[i].num = 0xF000U; us_test[i].expected = "61440";
  105. i++; us_test[i].num = 0x0F00U; us_test[i].expected = "3840";
  106. i++; us_test[i].num = 0x00F0U; us_test[i].expected = "240";
  107. i++; us_test[i].num = 0x000FU; us_test[i].expected = "15";
  108. i++; us_test[i].num = 0xC000U; us_test[i].expected = "49152";
  109. i++; us_test[i].num = 0x0C00U; us_test[i].expected = "3072";
  110. i++; us_test[i].num = 0x00C0U; us_test[i].expected = "192";
  111. i++; us_test[i].num = 0x000CU; us_test[i].expected = "12";
  112. i++; us_test[i].num = 0x0001U; us_test[i].expected = "1";
  113. i++; us_test[i].num = 0x0000U; us_test[i].expected = "0";
  114. num_ushort_tests = i;
  115. for(i = 1; i <= num_ushort_tests; i++) {
  116. for(j = 0; j<BUFSZ; j++)
  117. us_test[i].result[j] = 'X';
  118. us_test[i].result[BUFSZ-1] = '\0';
  119. (void)curl_msprintf(us_test[i].result, "%hu", us_test[i].num);
  120. if(memcmp(us_test[i].result,
  121. us_test[i].expected,
  122. strlen(us_test[i].expected))) {
  123. printf("unsigned short test #%.2d: Failed (Expected: %s Got: %s)\n",
  124. i, us_test[i].expected, us_test[i].result);
  125. failed++;
  126. }
  127. }
  128. if(!failed)
  129. printf("All curl_mprintf() unsigned short tests OK!\n");
  130. else
  131. printf("Some curl_mprintf() unsigned short tests Failed!\n");
  132. return failed;
  133. }
  134. static int test_signed_short_formatting(void)
  135. {
  136. int i, j;
  137. int num_sshort_tests = 0;
  138. int failed = 0;
  139. i = 1; ss_test[i].num = 0x7FFF; ss_test[i].expected = "32767";
  140. i++; ss_test[i].num = 0x7FFE; ss_test[i].expected = "32766";
  141. i++; ss_test[i].num = 0x7FFD; ss_test[i].expected = "32765";
  142. i++; ss_test[i].num = 0x7F00; ss_test[i].expected = "32512";
  143. i++; ss_test[i].num = 0x07F0; ss_test[i].expected = "2032";
  144. i++; ss_test[i].num = 0x007F; ss_test[i].expected = "127";
  145. i++; ss_test[i].num = 0x7000; ss_test[i].expected = "28672";
  146. i++; ss_test[i].num = 0x0700; ss_test[i].expected = "1792";
  147. i++; ss_test[i].num = 0x0070; ss_test[i].expected = "112";
  148. i++; ss_test[i].num = 0x0007; ss_test[i].expected = "7";
  149. i++; ss_test[i].num = 0x5000; ss_test[i].expected = "20480";
  150. i++; ss_test[i].num = 0x0500; ss_test[i].expected = "1280";
  151. i++; ss_test[i].num = 0x0050; ss_test[i].expected = "80";
  152. i++; ss_test[i].num = 0x0005; ss_test[i].expected = "5";
  153. i++; ss_test[i].num = 0x0001; ss_test[i].expected = "1";
  154. i++; ss_test[i].num = 0x0000; ss_test[i].expected = "0";
  155. i++; ss_test[i].num = -0x7FFF -1; ss_test[i].expected = "-32768";
  156. i++; ss_test[i].num = -0x7FFE -1; ss_test[i].expected = "-32767";
  157. i++; ss_test[i].num = -0x7FFD -1; ss_test[i].expected = "-32766";
  158. i++; ss_test[i].num = -0x7F00 -1; ss_test[i].expected = "-32513";
  159. i++; ss_test[i].num = -0x07F0 -1; ss_test[i].expected = "-2033";
  160. i++; ss_test[i].num = -0x007F -1; ss_test[i].expected = "-128";
  161. i++; ss_test[i].num = -0x7000 -1; ss_test[i].expected = "-28673";
  162. i++; ss_test[i].num = -0x0700 -1; ss_test[i].expected = "-1793";
  163. i++; ss_test[i].num = -0x0070 -1; ss_test[i].expected = "-113";
  164. i++; ss_test[i].num = -0x0007 -1; ss_test[i].expected = "-8";
  165. i++; ss_test[i].num = -0x5000 -1; ss_test[i].expected = "-20481";
  166. i++; ss_test[i].num = -0x0500 -1; ss_test[i].expected = "-1281";
  167. i++; ss_test[i].num = -0x0050 -1; ss_test[i].expected = "-81";
  168. i++; ss_test[i].num = -0x0005 -1; ss_test[i].expected = "-6";
  169. i++; ss_test[i].num = 0x0000 -1; ss_test[i].expected = "-1";
  170. num_sshort_tests = i;
  171. for(i = 1; i <= num_sshort_tests; i++) {
  172. for(j = 0; j<BUFSZ; j++)
  173. ss_test[i].result[j] = 'X';
  174. ss_test[i].result[BUFSZ-1] = '\0';
  175. (void)curl_msprintf(ss_test[i].result, "%hd", ss_test[i].num);
  176. if(memcmp(ss_test[i].result,
  177. ss_test[i].expected,
  178. strlen(ss_test[i].expected))) {
  179. printf("signed short test #%.2d: Failed (Expected: %s Got: %s)\n",
  180. i, ss_test[i].expected, ss_test[i].result);
  181. failed++;
  182. }
  183. }
  184. if(!failed)
  185. printf("All curl_mprintf() signed short tests OK!\n");
  186. else
  187. printf("Some curl_mprintf() signed short tests Failed!\n");
  188. return failed;
  189. }
  190. static int test_unsigned_int_formatting(void)
  191. {
  192. int i, j;
  193. int num_uint_tests = 0;
  194. int failed = 0;
  195. #if (SIZEOF_INT == 2)
  196. i = 1; ui_test[i].num = 0xFFFFU; ui_test[i].expected = "65535";
  197. i++; ui_test[i].num = 0xFF00U; ui_test[i].expected = "65280";
  198. i++; ui_test[i].num = 0x00FFU; ui_test[i].expected = "255";
  199. i++; ui_test[i].num = 0xF000U; ui_test[i].expected = "61440";
  200. i++; ui_test[i].num = 0x0F00U; ui_test[i].expected = "3840";
  201. i++; ui_test[i].num = 0x00F0U; ui_test[i].expected = "240";
  202. i++; ui_test[i].num = 0x000FU; ui_test[i].expected = "15";
  203. i++; ui_test[i].num = 0xC000U; ui_test[i].expected = "49152";
  204. i++; ui_test[i].num = 0x0C00U; ui_test[i].expected = "3072";
  205. i++; ui_test[i].num = 0x00C0U; ui_test[i].expected = "192";
  206. i++; ui_test[i].num = 0x000CU; ui_test[i].expected = "12";
  207. i++; ui_test[i].num = 0x0001U; ui_test[i].expected = "1";
  208. i++; ui_test[i].num = 0x0000U; ui_test[i].expected = "0";
  209. num_uint_tests = i;
  210. #elif (SIZEOF_INT == 4)
  211. i = 1; ui_test[i].num = 0xFFFFFFFFU; ui_test[i].expected = "4294967295";
  212. i++; ui_test[i].num = 0xFFFF0000U; ui_test[i].expected = "4294901760";
  213. i++; ui_test[i].num = 0x0000FFFFU; ui_test[i].expected = "65535";
  214. i++; ui_test[i].num = 0xFF000000U; ui_test[i].expected = "4278190080";
  215. i++; ui_test[i].num = 0x00FF0000U; ui_test[i].expected = "16711680";
  216. i++; ui_test[i].num = 0x0000FF00U; ui_test[i].expected = "65280";
  217. i++; ui_test[i].num = 0x000000FFU; ui_test[i].expected = "255";
  218. i++; ui_test[i].num = 0xF0000000U; ui_test[i].expected = "4026531840";
  219. i++; ui_test[i].num = 0x0F000000U; ui_test[i].expected = "251658240";
  220. i++; ui_test[i].num = 0x00F00000U; ui_test[i].expected = "15728640";
  221. i++; ui_test[i].num = 0x000F0000U; ui_test[i].expected = "983040";
  222. i++; ui_test[i].num = 0x0000F000U; ui_test[i].expected = "61440";
  223. i++; ui_test[i].num = 0x00000F00U; ui_test[i].expected = "3840";
  224. i++; ui_test[i].num = 0x000000F0U; ui_test[i].expected = "240";
  225. i++; ui_test[i].num = 0x0000000FU; ui_test[i].expected = "15";
  226. i++; ui_test[i].num = 0xC0000000U; ui_test[i].expected = "3221225472";
  227. i++; ui_test[i].num = 0x0C000000U; ui_test[i].expected = "201326592";
  228. i++; ui_test[i].num = 0x00C00000U; ui_test[i].expected = "12582912";
  229. i++; ui_test[i].num = 0x000C0000U; ui_test[i].expected = "786432";
  230. i++; ui_test[i].num = 0x0000C000U; ui_test[i].expected = "49152";
  231. i++; ui_test[i].num = 0x00000C00U; ui_test[i].expected = "3072";
  232. i++; ui_test[i].num = 0x000000C0U; ui_test[i].expected = "192";
  233. i++; ui_test[i].num = 0x0000000CU; ui_test[i].expected = "12";
  234. i++; ui_test[i].num = 0x00000001U; ui_test[i].expected = "1";
  235. i++; ui_test[i].num = 0x00000000U; ui_test[i].expected = "0";
  236. num_uint_tests = i;
  237. #elif (SIZEOF_INT == 8)
  238. /* !checksrc! disable LONGLINE all */
  239. i = 1; ui_test[i].num = 0xFFFFFFFFFFFFFFFFU; ui_test[i].expected = "18446744073709551615";
  240. i++; ui_test[i].num = 0xFFFFFFFF00000000U; ui_test[i].expected = "18446744069414584320";
  241. i++; ui_test[i].num = 0x00000000FFFFFFFFU; ui_test[i].expected = "4294967295";
  242. i++; ui_test[i].num = 0xFFFF000000000000U; ui_test[i].expected = "18446462598732840960";
  243. i++; ui_test[i].num = 0x0000FFFF00000000U; ui_test[i].expected = "281470681743360";
  244. i++; ui_test[i].num = 0x00000000FFFF0000U; ui_test[i].expected = "4294901760";
  245. i++; ui_test[i].num = 0x000000000000FFFFU; ui_test[i].expected = "65535";
  246. i++; ui_test[i].num = 0xFF00000000000000U; ui_test[i].expected = "18374686479671623680";
  247. i++; ui_test[i].num = 0x00FF000000000000U; ui_test[i].expected = "71776119061217280";
  248. i++; ui_test[i].num = 0x0000FF0000000000U; ui_test[i].expected = "280375465082880";
  249. i++; ui_test[i].num = 0x000000FF00000000U; ui_test[i].expected = "1095216660480";
  250. i++; ui_test[i].num = 0x00000000FF000000U; ui_test[i].expected = "4278190080";
  251. i++; ui_test[i].num = 0x0000000000FF0000U; ui_test[i].expected = "16711680";
  252. i++; ui_test[i].num = 0x000000000000FF00U; ui_test[i].expected = "65280";
  253. i++; ui_test[i].num = 0x00000000000000FFU; ui_test[i].expected = "255";
  254. i++; ui_test[i].num = 0xF000000000000000U; ui_test[i].expected = "17293822569102704640";
  255. i++; ui_test[i].num = 0x0F00000000000000U; ui_test[i].expected = "1080863910568919040";
  256. i++; ui_test[i].num = 0x00F0000000000000U; ui_test[i].expected = "67553994410557440";
  257. i++; ui_test[i].num = 0x000F000000000000U; ui_test[i].expected = "4222124650659840";
  258. i++; ui_test[i].num = 0x0000F00000000000U; ui_test[i].expected = "263882790666240";
  259. i++; ui_test[i].num = 0x00000F0000000000U; ui_test[i].expected = "16492674416640";
  260. i++; ui_test[i].num = 0x000000F000000000U; ui_test[i].expected = "1030792151040";
  261. i++; ui_test[i].num = 0x0000000F00000000U; ui_test[i].expected = "64424509440";
  262. i++; ui_test[i].num = 0x00000000F0000000U; ui_test[i].expected = "4026531840";
  263. i++; ui_test[i].num = 0x000000000F000000U; ui_test[i].expected = "251658240";
  264. i++; ui_test[i].num = 0x0000000000F00000U; ui_test[i].expected = "15728640";
  265. i++; ui_test[i].num = 0x00000000000F0000U; ui_test[i].expected = "983040";
  266. i++; ui_test[i].num = 0x000000000000F000U; ui_test[i].expected = "61440";
  267. i++; ui_test[i].num = 0x0000000000000F00U; ui_test[i].expected = "3840";
  268. i++; ui_test[i].num = 0x00000000000000F0U; ui_test[i].expected = "240";
  269. i++; ui_test[i].num = 0x000000000000000FU; ui_test[i].expected = "15";
  270. i++; ui_test[i].num = 0xC000000000000000U; ui_test[i].expected = "13835058055282163712";
  271. i++; ui_test[i].num = 0x0C00000000000000U; ui_test[i].expected = "864691128455135232";
  272. i++; ui_test[i].num = 0x00C0000000000000U; ui_test[i].expected = "54043195528445952";
  273. i++; ui_test[i].num = 0x000C000000000000U; ui_test[i].expected = "3377699720527872";
  274. i++; ui_test[i].num = 0x0000C00000000000U; ui_test[i].expected = "211106232532992";
  275. i++; ui_test[i].num = 0x00000C0000000000U; ui_test[i].expected = "13194139533312";
  276. i++; ui_test[i].num = 0x000000C000000000U; ui_test[i].expected = "824633720832";
  277. i++; ui_test[i].num = 0x0000000C00000000U; ui_test[i].expected = "51539607552";
  278. i++; ui_test[i].num = 0x00000000C0000000U; ui_test[i].expected = "3221225472";
  279. i++; ui_test[i].num = 0x000000000C000000U; ui_test[i].expected = "201326592";
  280. i++; ui_test[i].num = 0x0000000000C00000U; ui_test[i].expected = "12582912";
  281. i++; ui_test[i].num = 0x00000000000C0000U; ui_test[i].expected = "786432";
  282. i++; ui_test[i].num = 0x000000000000C000U; ui_test[i].expected = "49152";
  283. i++; ui_test[i].num = 0x0000000000000C00U; ui_test[i].expected = "3072";
  284. i++; ui_test[i].num = 0x00000000000000C0U; ui_test[i].expected = "192";
  285. i++; ui_test[i].num = 0x000000000000000CU; ui_test[i].expected = "12";
  286. i++; ui_test[i].num = 0x00000001U; ui_test[i].expected = "1";
  287. i++; ui_test[i].num = 0x00000000U; ui_test[i].expected = "0";
  288. num_uint_tests = i;
  289. #endif
  290. for(i = 1; i <= num_uint_tests; i++) {
  291. for(j = 0; j<BUFSZ; j++)
  292. ui_test[i].result[j] = 'X';
  293. ui_test[i].result[BUFSZ-1] = '\0';
  294. (void)curl_msprintf(ui_test[i].result, "%u", ui_test[i].num);
  295. if(memcmp(ui_test[i].result,
  296. ui_test[i].expected,
  297. strlen(ui_test[i].expected))) {
  298. printf("unsigned int test #%.2d: Failed (Expected: %s Got: %s)\n",
  299. i, ui_test[i].expected, ui_test[i].result);
  300. failed++;
  301. }
  302. }
  303. if(!failed)
  304. printf("All curl_mprintf() unsigned int tests OK!\n");
  305. else
  306. printf("Some curl_mprintf() unsigned int tests Failed!\n");
  307. return failed;
  308. }
  309. static int test_signed_int_formatting(void)
  310. {
  311. int i, j;
  312. int num_sint_tests = 0;
  313. int failed = 0;
  314. #if (SIZEOF_INT == 2)
  315. i = 1; si_test[i].num = 0x7FFF; si_test[i].expected = "32767";
  316. i++; si_test[i].num = 0x7FFE; si_test[i].expected = "32766";
  317. i++; si_test[i].num = 0x7FFD; si_test[i].expected = "32765";
  318. i++; si_test[i].num = 0x7F00; si_test[i].expected = "32512";
  319. i++; si_test[i].num = 0x07F0; si_test[i].expected = "2032";
  320. i++; si_test[i].num = 0x007F; si_test[i].expected = "127";
  321. i++; si_test[i].num = 0x7000; si_test[i].expected = "28672";
  322. i++; si_test[i].num = 0x0700; si_test[i].expected = "1792";
  323. i++; si_test[i].num = 0x0070; si_test[i].expected = "112";
  324. i++; si_test[i].num = 0x0007; si_test[i].expected = "7";
  325. i++; si_test[i].num = 0x5000; si_test[i].expected = "20480";
  326. i++; si_test[i].num = 0x0500; si_test[i].expected = "1280";
  327. i++; si_test[i].num = 0x0050; si_test[i].expected = "80";
  328. i++; si_test[i].num = 0x0005; si_test[i].expected = "5";
  329. i++; si_test[i].num = 0x0001; si_test[i].expected = "1";
  330. i++; si_test[i].num = 0x0000; si_test[i].expected = "0";
  331. i++; si_test[i].num = -0x7FFF -1; si_test[i].expected = "-32768";
  332. i++; si_test[i].num = -0x7FFE -1; si_test[i].expected = "-32767";
  333. i++; si_test[i].num = -0x7FFD -1; si_test[i].expected = "-32766";
  334. i++; si_test[i].num = -0x7F00 -1; si_test[i].expected = "-32513";
  335. i++; si_test[i].num = -0x07F0 -1; si_test[i].expected = "-2033";
  336. i++; si_test[i].num = -0x007F -1; si_test[i].expected = "-128";
  337. i++; si_test[i].num = -0x7000 -1; si_test[i].expected = "-28673";
  338. i++; si_test[i].num = -0x0700 -1; si_test[i].expected = "-1793";
  339. i++; si_test[i].num = -0x0070 -1; si_test[i].expected = "-113";
  340. i++; si_test[i].num = -0x0007 -1; si_test[i].expected = "-8";
  341. i++; si_test[i].num = -0x5000 -1; si_test[i].expected = "-20481";
  342. i++; si_test[i].num = -0x0500 -1; si_test[i].expected = "-1281";
  343. i++; si_test[i].num = -0x0050 -1; si_test[i].expected = "-81";
  344. i++; si_test[i].num = -0x0005 -1; si_test[i].expected = "-6";
  345. i++; si_test[i].num = 0x0000 -1; si_test[i].expected = "-1";
  346. num_sint_tests = i;
  347. #elif (SIZEOF_INT == 4)
  348. i = 1; si_test[i].num = 0x7FFFFFFF; si_test[i].expected = "2147483647";
  349. i++; si_test[i].num = 0x7FFFFFFE; si_test[i].expected = "2147483646";
  350. i++; si_test[i].num = 0x7FFFFFFD; si_test[i].expected = "2147483645";
  351. i++; si_test[i].num = 0x7FFF0000; si_test[i].expected = "2147418112";
  352. i++; si_test[i].num = 0x00007FFF; si_test[i].expected = "32767";
  353. i++; si_test[i].num = 0x7F000000; si_test[i].expected = "2130706432";
  354. i++; si_test[i].num = 0x007F0000; si_test[i].expected = "8323072";
  355. i++; si_test[i].num = 0x00007F00; si_test[i].expected = "32512";
  356. i++; si_test[i].num = 0x0000007F; si_test[i].expected = "127";
  357. i++; si_test[i].num = 0x70000000; si_test[i].expected = "1879048192";
  358. i++; si_test[i].num = 0x07000000; si_test[i].expected = "117440512";
  359. i++; si_test[i].num = 0x00700000; si_test[i].expected = "7340032";
  360. i++; si_test[i].num = 0x00070000; si_test[i].expected = "458752";
  361. i++; si_test[i].num = 0x00007000; si_test[i].expected = "28672";
  362. i++; si_test[i].num = 0x00000700; si_test[i].expected = "1792";
  363. i++; si_test[i].num = 0x00000070; si_test[i].expected = "112";
  364. i++; si_test[i].num = 0x00000007; si_test[i].expected = "7";
  365. i++; si_test[i].num = 0x50000000; si_test[i].expected = "1342177280";
  366. i++; si_test[i].num = 0x05000000; si_test[i].expected = "83886080";
  367. i++; si_test[i].num = 0x00500000; si_test[i].expected = "5242880";
  368. i++; si_test[i].num = 0x00050000; si_test[i].expected = "327680";
  369. i++; si_test[i].num = 0x00005000; si_test[i].expected = "20480";
  370. i++; si_test[i].num = 0x00000500; si_test[i].expected = "1280";
  371. i++; si_test[i].num = 0x00000050; si_test[i].expected = "80";
  372. i++; si_test[i].num = 0x00000005; si_test[i].expected = "5";
  373. i++; si_test[i].num = 0x00000001; si_test[i].expected = "1";
  374. i++; si_test[i].num = 0x00000000; si_test[i].expected = "0";
  375. i++; si_test[i].num = -0x7FFFFFFF -1; si_test[i].expected = "-2147483648";
  376. i++; si_test[i].num = -0x7FFFFFFE -1; si_test[i].expected = "-2147483647";
  377. i++; si_test[i].num = -0x7FFFFFFD -1; si_test[i].expected = "-2147483646";
  378. i++; si_test[i].num = -0x7FFF0000 -1; si_test[i].expected = "-2147418113";
  379. i++; si_test[i].num = -0x00007FFF -1; si_test[i].expected = "-32768";
  380. i++; si_test[i].num = -0x7F000000 -1; si_test[i].expected = "-2130706433";
  381. i++; si_test[i].num = -0x007F0000 -1; si_test[i].expected = "-8323073";
  382. i++; si_test[i].num = -0x00007F00 -1; si_test[i].expected = "-32513";
  383. i++; si_test[i].num = -0x0000007F -1; si_test[i].expected = "-128";
  384. i++; si_test[i].num = -0x70000000 -1; si_test[i].expected = "-1879048193";
  385. i++; si_test[i].num = -0x07000000 -1; si_test[i].expected = "-117440513";
  386. i++; si_test[i].num = -0x00700000 -1; si_test[i].expected = "-7340033";
  387. i++; si_test[i].num = -0x00070000 -1; si_test[i].expected = "-458753";
  388. i++; si_test[i].num = -0x00007000 -1; si_test[i].expected = "-28673";
  389. i++; si_test[i].num = -0x00000700 -1; si_test[i].expected = "-1793";
  390. i++; si_test[i].num = -0x00000070 -1; si_test[i].expected = "-113";
  391. i++; si_test[i].num = -0x00000007 -1; si_test[i].expected = "-8";
  392. i++; si_test[i].num = -0x50000000 -1; si_test[i].expected = "-1342177281";
  393. i++; si_test[i].num = -0x05000000 -1; si_test[i].expected = "-83886081";
  394. i++; si_test[i].num = -0x00500000 -1; si_test[i].expected = "-5242881";
  395. i++; si_test[i].num = -0x00050000 -1; si_test[i].expected = "-327681";
  396. i++; si_test[i].num = -0x00005000 -1; si_test[i].expected = "-20481";
  397. i++; si_test[i].num = -0x00000500 -1; si_test[i].expected = "-1281";
  398. i++; si_test[i].num = -0x00000050 -1; si_test[i].expected = "-81";
  399. i++; si_test[i].num = -0x00000005 -1; si_test[i].expected = "-6";
  400. i++; si_test[i].num = 0x00000000 -1; si_test[i].expected = "-1";
  401. num_sint_tests = i;
  402. #elif (SIZEOF_INT == 8)
  403. i = 1; si_test[i].num = 0x7FFFFFFFFFFFFFFF; si_test[i].expected = "9223372036854775807";
  404. i++; si_test[i].num = 0x7FFFFFFFFFFFFFFE; si_test[i].expected = "9223372036854775806";
  405. i++; si_test[i].num = 0x7FFFFFFFFFFFFFFD; si_test[i].expected = "9223372036854775805";
  406. i++; si_test[i].num = 0x7FFFFFFF00000000; si_test[i].expected = "9223372032559808512";
  407. i++; si_test[i].num = 0x000000007FFFFFFF; si_test[i].expected = "2147483647";
  408. i++; si_test[i].num = 0x7FFF000000000000; si_test[i].expected = "9223090561878065152";
  409. i++; si_test[i].num = 0x00007FFF00000000; si_test[i].expected = "140733193388032";
  410. i++; si_test[i].num = 0x000000007FFF0000; si_test[i].expected = "2147418112";
  411. i++; si_test[i].num = 0x0000000000007FFF; si_test[i].expected = "32767";
  412. i++; si_test[i].num = 0x7F00000000000000; si_test[i].expected = "9151314442816847872";
  413. i++; si_test[i].num = 0x007F000000000000; si_test[i].expected = "35747322042253312";
  414. i++; si_test[i].num = 0x00007F0000000000; si_test[i].expected = "139637976727552";
  415. i++; si_test[i].num = 0x0000007F00000000; si_test[i].expected = "545460846592";
  416. i++; si_test[i].num = 0x000000007F000000; si_test[i].expected = "2130706432";
  417. i++; si_test[i].num = 0x00000000007F0000; si_test[i].expected = "8323072";
  418. i++; si_test[i].num = 0x0000000000007F00; si_test[i].expected = "32512";
  419. i++; si_test[i].num = 0x000000000000007F; si_test[i].expected = "127";
  420. i++; si_test[i].num = 0x7000000000000000; si_test[i].expected = "8070450532247928832";
  421. i++; si_test[i].num = 0x0700000000000000; si_test[i].expected = "504403158265495552";
  422. i++; si_test[i].num = 0x0070000000000000; si_test[i].expected = "31525197391593472";
  423. i++; si_test[i].num = 0x0007000000000000; si_test[i].expected = "1970324836974592";
  424. i++; si_test[i].num = 0x0000700000000000; si_test[i].expected = "123145302310912";
  425. i++; si_test[i].num = 0x0000070000000000; si_test[i].expected = "7696581394432";
  426. i++; si_test[i].num = 0x0000007000000000; si_test[i].expected = "481036337152";
  427. i++; si_test[i].num = 0x0000000700000000; si_test[i].expected = "30064771072";
  428. i++; si_test[i].num = 0x0000000070000000; si_test[i].expected = "1879048192";
  429. i++; si_test[i].num = 0x0000000007000000; si_test[i].expected = "117440512";
  430. i++; si_test[i].num = 0x0000000000700000; si_test[i].expected = "7340032";
  431. i++; si_test[i].num = 0x0000000000070000; si_test[i].expected = "458752";
  432. i++; si_test[i].num = 0x0000000000007000; si_test[i].expected = "28672";
  433. i++; si_test[i].num = 0x0000000000000700; si_test[i].expected = "1792";
  434. i++; si_test[i].num = 0x0000000000000070; si_test[i].expected = "112";
  435. i++; si_test[i].num = 0x0000000000000007; si_test[i].expected = "7";
  436. i++; si_test[i].num = 0x0000000000000001; si_test[i].expected = "1";
  437. i++; si_test[i].num = 0x0000000000000000; si_test[i].expected = "0";
  438. i++; si_test[i].num = -0x7FFFFFFFFFFFFFFF -1; si_test[i].expected = "-9223372036854775808";
  439. i++; si_test[i].num = -0x7FFFFFFFFFFFFFFE -1; si_test[i].expected = "-9223372036854775807";
  440. i++; si_test[i].num = -0x7FFFFFFFFFFFFFFD -1; si_test[i].expected = "-9223372036854775806";
  441. i++; si_test[i].num = -0x7FFFFFFF00000000 -1; si_test[i].expected = "-9223372032559808513";
  442. i++; si_test[i].num = -0x000000007FFFFFFF -1; si_test[i].expected = "-2147483648";
  443. i++; si_test[i].num = -0x7FFF000000000000 -1; si_test[i].expected = "-9223090561878065153";
  444. i++; si_test[i].num = -0x00007FFF00000000 -1; si_test[i].expected = "-140733193388033";
  445. i++; si_test[i].num = -0x000000007FFF0000 -1; si_test[i].expected = "-2147418113";
  446. i++; si_test[i].num = -0x0000000000007FFF -1; si_test[i].expected = "-32768";
  447. i++; si_test[i].num = -0x7F00000000000000 -1; si_test[i].expected = "-9151314442816847873";
  448. i++; si_test[i].num = -0x007F000000000000 -1; si_test[i].expected = "-35747322042253313";
  449. i++; si_test[i].num = -0x00007F0000000000 -1; si_test[i].expected = "-139637976727553";
  450. i++; si_test[i].num = -0x0000007F00000000 -1; si_test[i].expected = "-545460846593";
  451. i++; si_test[i].num = -0x000000007F000000 -1; si_test[i].expected = "-2130706433";
  452. i++; si_test[i].num = -0x00000000007F0000 -1; si_test[i].expected = "-8323073";
  453. i++; si_test[i].num = -0x0000000000007F00 -1; si_test[i].expected = "-32513";
  454. i++; si_test[i].num = -0x000000000000007F -1; si_test[i].expected = "-128";
  455. i++; si_test[i].num = -0x7000000000000000 -1; si_test[i].expected = "-8070450532247928833";
  456. i++; si_test[i].num = -0x0700000000000000 -1; si_test[i].expected = "-504403158265495553";
  457. i++; si_test[i].num = -0x0070000000000000 -1; si_test[i].expected = "-31525197391593473";
  458. i++; si_test[i].num = -0x0007000000000000 -1; si_test[i].expected = "-1970324836974593";
  459. i++; si_test[i].num = -0x0000700000000000 -1; si_test[i].expected = "-123145302310913";
  460. i++; si_test[i].num = -0x0000070000000000 -1; si_test[i].expected = "-7696581394433";
  461. i++; si_test[i].num = -0x0000007000000000 -1; si_test[i].expected = "-481036337153";
  462. i++; si_test[i].num = -0x0000000700000000 -1; si_test[i].expected = "-30064771073";
  463. i++; si_test[i].num = -0x0000000070000000 -1; si_test[i].expected = "-1879048193";
  464. i++; si_test[i].num = -0x0000000007000000 -1; si_test[i].expected = "-117440513";
  465. i++; si_test[i].num = -0x0000000000700000 -1; si_test[i].expected = "-7340033";
  466. i++; si_test[i].num = -0x0000000000070000 -1; si_test[i].expected = "-458753";
  467. i++; si_test[i].num = -0x0000000000007000 -1; si_test[i].expected = "-28673";
  468. i++; si_test[i].num = -0x0000000000000700 -1; si_test[i].expected = "-1793";
  469. i++; si_test[i].num = -0x0000000000000070 -1; si_test[i].expected = "-113";
  470. i++; si_test[i].num = -0x0000000000000007 -1; si_test[i].expected = "-8";
  471. i++; si_test[i].num = 0x0000000000000000 -1; si_test[i].expected = "-1";
  472. num_sint_tests = i;
  473. #endif
  474. for(i = 1; i <= num_sint_tests; i++) {
  475. for(j = 0; j<BUFSZ; j++)
  476. si_test[i].result[j] = 'X';
  477. si_test[i].result[BUFSZ-1] = '\0';
  478. (void)curl_msprintf(si_test[i].result, "%d", si_test[i].num);
  479. if(memcmp(si_test[i].result,
  480. si_test[i].expected,
  481. strlen(si_test[i].expected))) {
  482. printf("signed int test #%.2d: Failed (Expected: %s Got: %s)\n",
  483. i, si_test[i].expected, si_test[i].result);
  484. failed++;
  485. }
  486. }
  487. if(!failed)
  488. printf("All curl_mprintf() signed int tests OK!\n");
  489. else
  490. printf("Some curl_mprintf() signed int tests Failed!\n");
  491. return failed;
  492. }
  493. static int test_unsigned_long_formatting(void)
  494. {
  495. int i, j;
  496. int num_ulong_tests = 0;
  497. int failed = 0;
  498. #if (SIZEOF_LONG == 2)
  499. i = 1; ul_test[i].num = 0xFFFFUL; ul_test[i].expected = "65535";
  500. i++; ul_test[i].num = 0xFF00UL; ul_test[i].expected = "65280";
  501. i++; ul_test[i].num = 0x00FFUL; ul_test[i].expected = "255";
  502. i++; ul_test[i].num = 0xF000UL; ul_test[i].expected = "61440";
  503. i++; ul_test[i].num = 0x0F00UL; ul_test[i].expected = "3840";
  504. i++; ul_test[i].num = 0x00F0UL; ul_test[i].expected = "240";
  505. i++; ul_test[i].num = 0x000FUL; ul_test[i].expected = "15";
  506. i++; ul_test[i].num = 0xC000UL; ul_test[i].expected = "49152";
  507. i++; ul_test[i].num = 0x0C00UL; ul_test[i].expected = "3072";
  508. i++; ul_test[i].num = 0x00C0UL; ul_test[i].expected = "192";
  509. i++; ul_test[i].num = 0x000CUL; ul_test[i].expected = "12";
  510. i++; ul_test[i].num = 0x0001UL; ul_test[i].expected = "1";
  511. i++; ul_test[i].num = 0x0000UL; ul_test[i].expected = "0";
  512. num_ulong_tests = i;
  513. #elif (SIZEOF_LONG == 4)
  514. i = 1; ul_test[i].num = 0xFFFFFFFFUL; ul_test[i].expected = "4294967295";
  515. i++; ul_test[i].num = 0xFFFF0000UL; ul_test[i].expected = "4294901760";
  516. i++; ul_test[i].num = 0x0000FFFFUL; ul_test[i].expected = "65535";
  517. i++; ul_test[i].num = 0xFF000000UL; ul_test[i].expected = "4278190080";
  518. i++; ul_test[i].num = 0x00FF0000UL; ul_test[i].expected = "16711680";
  519. i++; ul_test[i].num = 0x0000FF00UL; ul_test[i].expected = "65280";
  520. i++; ul_test[i].num = 0x000000FFUL; ul_test[i].expected = "255";
  521. i++; ul_test[i].num = 0xF0000000UL; ul_test[i].expected = "4026531840";
  522. i++; ul_test[i].num = 0x0F000000UL; ul_test[i].expected = "251658240";
  523. i++; ul_test[i].num = 0x00F00000UL; ul_test[i].expected = "15728640";
  524. i++; ul_test[i].num = 0x000F0000UL; ul_test[i].expected = "983040";
  525. i++; ul_test[i].num = 0x0000F000UL; ul_test[i].expected = "61440";
  526. i++; ul_test[i].num = 0x00000F00UL; ul_test[i].expected = "3840";
  527. i++; ul_test[i].num = 0x000000F0UL; ul_test[i].expected = "240";
  528. i++; ul_test[i].num = 0x0000000FUL; ul_test[i].expected = "15";
  529. i++; ul_test[i].num = 0xC0000000UL; ul_test[i].expected = "3221225472";
  530. i++; ul_test[i].num = 0x0C000000UL; ul_test[i].expected = "201326592";
  531. i++; ul_test[i].num = 0x00C00000UL; ul_test[i].expected = "12582912";
  532. i++; ul_test[i].num = 0x000C0000UL; ul_test[i].expected = "786432";
  533. i++; ul_test[i].num = 0x0000C000UL; ul_test[i].expected = "49152";
  534. i++; ul_test[i].num = 0x00000C00UL; ul_test[i].expected = "3072";
  535. i++; ul_test[i].num = 0x000000C0UL; ul_test[i].expected = "192";
  536. i++; ul_test[i].num = 0x0000000CUL; ul_test[i].expected = "12";
  537. i++; ul_test[i].num = 0x00000001UL; ul_test[i].expected = "1";
  538. i++; ul_test[i].num = 0x00000000UL; ul_test[i].expected = "0";
  539. num_ulong_tests = i;
  540. #elif (SIZEOF_LONG == 8)
  541. i = 1; ul_test[i].num = 0xFFFFFFFFFFFFFFFFUL; ul_test[i].expected = "18446744073709551615";
  542. i++; ul_test[i].num = 0xFFFFFFFF00000000UL; ul_test[i].expected = "18446744069414584320";
  543. i++; ul_test[i].num = 0x00000000FFFFFFFFUL; ul_test[i].expected = "4294967295";
  544. i++; ul_test[i].num = 0xFFFF000000000000UL; ul_test[i].expected = "18446462598732840960";
  545. i++; ul_test[i].num = 0x0000FFFF00000000UL; ul_test[i].expected = "281470681743360";
  546. i++; ul_test[i].num = 0x00000000FFFF0000UL; ul_test[i].expected = "4294901760";
  547. i++; ul_test[i].num = 0x000000000000FFFFUL; ul_test[i].expected = "65535";
  548. i++; ul_test[i].num = 0xFF00000000000000UL; ul_test[i].expected = "18374686479671623680";
  549. i++; ul_test[i].num = 0x00FF000000000000UL; ul_test[i].expected = "71776119061217280";
  550. i++; ul_test[i].num = 0x0000FF0000000000UL; ul_test[i].expected = "280375465082880";
  551. i++; ul_test[i].num = 0x000000FF00000000UL; ul_test[i].expected = "1095216660480";
  552. i++; ul_test[i].num = 0x00000000FF000000UL; ul_test[i].expected = "4278190080";
  553. i++; ul_test[i].num = 0x0000000000FF0000UL; ul_test[i].expected = "16711680";
  554. i++; ul_test[i].num = 0x000000000000FF00UL; ul_test[i].expected = "65280";
  555. i++; ul_test[i].num = 0x00000000000000FFUL; ul_test[i].expected = "255";
  556. i++; ul_test[i].num = 0xF000000000000000UL; ul_test[i].expected = "17293822569102704640";
  557. i++; ul_test[i].num = 0x0F00000000000000UL; ul_test[i].expected = "1080863910568919040";
  558. i++; ul_test[i].num = 0x00F0000000000000UL; ul_test[i].expected = "67553994410557440";
  559. i++; ul_test[i].num = 0x000F000000000000UL; ul_test[i].expected = "4222124650659840";
  560. i++; ul_test[i].num = 0x0000F00000000000UL; ul_test[i].expected = "263882790666240";
  561. i++; ul_test[i].num = 0x00000F0000000000UL; ul_test[i].expected = "16492674416640";
  562. i++; ul_test[i].num = 0x000000F000000000UL; ul_test[i].expected = "1030792151040";
  563. i++; ul_test[i].num = 0x0000000F00000000UL; ul_test[i].expected = "64424509440";
  564. i++; ul_test[i].num = 0x00000000F0000000UL; ul_test[i].expected = "4026531840";
  565. i++; ul_test[i].num = 0x000000000F000000UL; ul_test[i].expected = "251658240";
  566. i++; ul_test[i].num = 0x0000000000F00000UL; ul_test[i].expected = "15728640";
  567. i++; ul_test[i].num = 0x00000000000F0000UL; ul_test[i].expected = "983040";
  568. i++; ul_test[i].num = 0x000000000000F000UL; ul_test[i].expected = "61440";
  569. i++; ul_test[i].num = 0x0000000000000F00UL; ul_test[i].expected = "3840";
  570. i++; ul_test[i].num = 0x00000000000000F0UL; ul_test[i].expected = "240";
  571. i++; ul_test[i].num = 0x000000000000000FUL; ul_test[i].expected = "15";
  572. i++; ul_test[i].num = 0xC000000000000000UL; ul_test[i].expected = "13835058055282163712";
  573. i++; ul_test[i].num = 0x0C00000000000000UL; ul_test[i].expected = "864691128455135232";
  574. i++; ul_test[i].num = 0x00C0000000000000UL; ul_test[i].expected = "54043195528445952";
  575. i++; ul_test[i].num = 0x000C000000000000UL; ul_test[i].expected = "3377699720527872";
  576. i++; ul_test[i].num = 0x0000C00000000000UL; ul_test[i].expected = "211106232532992";
  577. i++; ul_test[i].num = 0x00000C0000000000UL; ul_test[i].expected = "13194139533312";
  578. i++; ul_test[i].num = 0x000000C000000000UL; ul_test[i].expected = "824633720832";
  579. i++; ul_test[i].num = 0x0000000C00000000UL; ul_test[i].expected = "51539607552";
  580. i++; ul_test[i].num = 0x00000000C0000000UL; ul_test[i].expected = "3221225472";
  581. i++; ul_test[i].num = 0x000000000C000000UL; ul_test[i].expected = "201326592";
  582. i++; ul_test[i].num = 0x0000000000C00000UL; ul_test[i].expected = "12582912";
  583. i++; ul_test[i].num = 0x00000000000C0000UL; ul_test[i].expected = "786432";
  584. i++; ul_test[i].num = 0x000000000000C000UL; ul_test[i].expected = "49152";
  585. i++; ul_test[i].num = 0x0000000000000C00UL; ul_test[i].expected = "3072";
  586. i++; ul_test[i].num = 0x00000000000000C0UL; ul_test[i].expected = "192";
  587. i++; ul_test[i].num = 0x000000000000000CUL; ul_test[i].expected = "12";
  588. i++; ul_test[i].num = 0x00000001UL; ul_test[i].expected = "1";
  589. i++; ul_test[i].num = 0x00000000UL; ul_test[i].expected = "0";
  590. num_ulong_tests = i;
  591. #endif
  592. for(i = 1; i <= num_ulong_tests; i++) {
  593. for(j = 0; j<BUFSZ; j++)
  594. ul_test[i].result[j] = 'X';
  595. ul_test[i].result[BUFSZ-1] = '\0';
  596. (void)curl_msprintf(ul_test[i].result, "%lu", ul_test[i].num);
  597. if(memcmp(ul_test[i].result,
  598. ul_test[i].expected,
  599. strlen(ul_test[i].expected))) {
  600. printf("unsigned long test #%.2d: Failed (Expected: %s Got: %s)\n",
  601. i, ul_test[i].expected, ul_test[i].result);
  602. failed++;
  603. }
  604. }
  605. if(!failed)
  606. printf("All curl_mprintf() unsigned long tests OK!\n");
  607. else
  608. printf("Some curl_mprintf() unsigned long tests Failed!\n");
  609. return failed;
  610. }
  611. static int test_signed_long_formatting(void)
  612. {
  613. int i, j;
  614. int num_slong_tests = 0;
  615. int failed = 0;
  616. #if (SIZEOF_LONG == 2)
  617. i = 1; sl_test[i].num = 0x7FFFL; sl_test[i].expected = "32767";
  618. i++; sl_test[i].num = 0x7FFEL; sl_test[i].expected = "32766";
  619. i++; sl_test[i].num = 0x7FFDL; sl_test[i].expected = "32765";
  620. i++; sl_test[i].num = 0x7F00L; sl_test[i].expected = "32512";
  621. i++; sl_test[i].num = 0x07F0L; sl_test[i].expected = "2032";
  622. i++; sl_test[i].num = 0x007FL; sl_test[i].expected = "127";
  623. i++; sl_test[i].num = 0x7000L; sl_test[i].expected = "28672";
  624. i++; sl_test[i].num = 0x0700L; sl_test[i].expected = "1792";
  625. i++; sl_test[i].num = 0x0070L; sl_test[i].expected = "112";
  626. i++; sl_test[i].num = 0x0007L; sl_test[i].expected = "7";
  627. i++; sl_test[i].num = 0x5000L; sl_test[i].expected = "20480";
  628. i++; sl_test[i].num = 0x0500L; sl_test[i].expected = "1280";
  629. i++; sl_test[i].num = 0x0050L; sl_test[i].expected = "80";
  630. i++; sl_test[i].num = 0x0005L; sl_test[i].expected = "5";
  631. i++; sl_test[i].num = 0x0001L; sl_test[i].expected = "1";
  632. i++; sl_test[i].num = 0x0000L; sl_test[i].expected = "0";
  633. i++; sl_test[i].num = -0x7FFFL -1L; sl_test[i].expected = "-32768";
  634. i++; sl_test[i].num = -0x7FFEL -1L; sl_test[i].expected = "-32767";
  635. i++; sl_test[i].num = -0x7FFDL -1L; sl_test[i].expected = "-32766";
  636. i++; sl_test[i].num = -0x7F00L -1L; sl_test[i].expected = "-32513";
  637. i++; sl_test[i].num = -0x07F0L -1L; sl_test[i].expected = "-2033";
  638. i++; sl_test[i].num = -0x007FL -1L; sl_test[i].expected = "-128";
  639. i++; sl_test[i].num = -0x7000L -1L; sl_test[i].expected = "-28673";
  640. i++; sl_test[i].num = -0x0700L -1L; sl_test[i].expected = "-1793";
  641. i++; sl_test[i].num = -0x0070L -1L; sl_test[i].expected = "-113";
  642. i++; sl_test[i].num = -0x0007L -1L; sl_test[i].expected = "-8";
  643. i++; sl_test[i].num = -0x5000L -1L; sl_test[i].expected = "-20481";
  644. i++; sl_test[i].num = -0x0500L -1L; sl_test[i].expected = "-1281";
  645. i++; sl_test[i].num = -0x0050L -1L; sl_test[i].expected = "-81";
  646. i++; sl_test[i].num = -0x0005L -1L; sl_test[i].expected = "-6";
  647. i++; sl_test[i].num = 0x0000L -1L; sl_test[i].expected = "-1";
  648. num_slong_tests = i;
  649. #elif (SIZEOF_LONG == 4)
  650. i = 1; sl_test[i].num = 0x7FFFFFFFL; sl_test[i].expected = "2147483647";
  651. i++; sl_test[i].num = 0x7FFFFFFEL; sl_test[i].expected = "2147483646";
  652. i++; sl_test[i].num = 0x7FFFFFFDL; sl_test[i].expected = "2147483645";
  653. i++; sl_test[i].num = 0x7FFF0000L; sl_test[i].expected = "2147418112";
  654. i++; sl_test[i].num = 0x00007FFFL; sl_test[i].expected = "32767";
  655. i++; sl_test[i].num = 0x7F000000L; sl_test[i].expected = "2130706432";
  656. i++; sl_test[i].num = 0x007F0000L; sl_test[i].expected = "8323072";
  657. i++; sl_test[i].num = 0x00007F00L; sl_test[i].expected = "32512";
  658. i++; sl_test[i].num = 0x0000007FL; sl_test[i].expected = "127";
  659. i++; sl_test[i].num = 0x70000000L; sl_test[i].expected = "1879048192";
  660. i++; sl_test[i].num = 0x07000000L; sl_test[i].expected = "117440512";
  661. i++; sl_test[i].num = 0x00700000L; sl_test[i].expected = "7340032";
  662. i++; sl_test[i].num = 0x00070000L; sl_test[i].expected = "458752";
  663. i++; sl_test[i].num = 0x00007000L; sl_test[i].expected = "28672";
  664. i++; sl_test[i].num = 0x00000700L; sl_test[i].expected = "1792";
  665. i++; sl_test[i].num = 0x00000070L; sl_test[i].expected = "112";
  666. i++; sl_test[i].num = 0x00000007L; sl_test[i].expected = "7";
  667. i++; sl_test[i].num = 0x50000000L; sl_test[i].expected = "1342177280";
  668. i++; sl_test[i].num = 0x05000000L; sl_test[i].expected = "83886080";
  669. i++; sl_test[i].num = 0x00500000L; sl_test[i].expected = "5242880";
  670. i++; sl_test[i].num = 0x00050000L; sl_test[i].expected = "327680";
  671. i++; sl_test[i].num = 0x00005000L; sl_test[i].expected = "20480";
  672. i++; sl_test[i].num = 0x00000500L; sl_test[i].expected = "1280";
  673. i++; sl_test[i].num = 0x00000050L; sl_test[i].expected = "80";
  674. i++; sl_test[i].num = 0x00000005L; sl_test[i].expected = "5";
  675. i++; sl_test[i].num = 0x00000001L; sl_test[i].expected = "1";
  676. i++; sl_test[i].num = 0x00000000L; sl_test[i].expected = "0";
  677. i++; sl_test[i].num = -0x7FFFFFFFL -1L; sl_test[i].expected = "-2147483648";
  678. i++; sl_test[i].num = -0x7FFFFFFEL -1L; sl_test[i].expected = "-2147483647";
  679. i++; sl_test[i].num = -0x7FFFFFFDL -1L; sl_test[i].expected = "-2147483646";
  680. i++; sl_test[i].num = -0x7FFF0000L -1L; sl_test[i].expected = "-2147418113";
  681. i++; sl_test[i].num = -0x00007FFFL -1L; sl_test[i].expected = "-32768";
  682. i++; sl_test[i].num = -0x7F000000L -1L; sl_test[i].expected = "-2130706433";
  683. i++; sl_test[i].num = -0x007F0000L -1L; sl_test[i].expected = "-8323073";
  684. i++; sl_test[i].num = -0x00007F00L -1L; sl_test[i].expected = "-32513";
  685. i++; sl_test[i].num = -0x0000007FL -1L; sl_test[i].expected = "-128";
  686. i++; sl_test[i].num = -0x70000000L -1L; sl_test[i].expected = "-1879048193";
  687. i++; sl_test[i].num = -0x07000000L -1L; sl_test[i].expected = "-117440513";
  688. i++; sl_test[i].num = -0x00700000L -1L; sl_test[i].expected = "-7340033";
  689. i++; sl_test[i].num = -0x00070000L -1L; sl_test[i].expected = "-458753";
  690. i++; sl_test[i].num = -0x00007000L -1L; sl_test[i].expected = "-28673";
  691. i++; sl_test[i].num = -0x00000700L -1L; sl_test[i].expected = "-1793";
  692. i++; sl_test[i].num = -0x00000070L -1L; sl_test[i].expected = "-113";
  693. i++; sl_test[i].num = -0x00000007L -1L; sl_test[i].expected = "-8";
  694. i++; sl_test[i].num = -0x50000000L -1L; sl_test[i].expected = "-1342177281";
  695. i++; sl_test[i].num = -0x05000000L -1L; sl_test[i].expected = "-83886081";
  696. i++; sl_test[i].num = -0x00500000L -1L; sl_test[i].expected = "-5242881";
  697. i++; sl_test[i].num = -0x00050000L -1L; sl_test[i].expected = "-327681";
  698. i++; sl_test[i].num = -0x00005000L -1L; sl_test[i].expected = "-20481";
  699. i++; sl_test[i].num = -0x00000500L -1L; sl_test[i].expected = "-1281";
  700. i++; sl_test[i].num = -0x00000050L -1L; sl_test[i].expected = "-81";
  701. i++; sl_test[i].num = -0x00000005L -1L; sl_test[i].expected = "-6";
  702. i++; sl_test[i].num = 0x00000000L -1L; sl_test[i].expected = "-1";
  703. num_slong_tests = i;
  704. #elif (SIZEOF_LONG == 8)
  705. i = 1; sl_test[i].num = 0x7FFFFFFFFFFFFFFFL; sl_test[i].expected = "9223372036854775807";
  706. i++; sl_test[i].num = 0x7FFFFFFFFFFFFFFEL; sl_test[i].expected = "9223372036854775806";
  707. i++; sl_test[i].num = 0x7FFFFFFFFFFFFFFDL; sl_test[i].expected = "9223372036854775805";
  708. i++; sl_test[i].num = 0x7FFFFFFF00000000L; sl_test[i].expected = "9223372032559808512";
  709. i++; sl_test[i].num = 0x000000007FFFFFFFL; sl_test[i].expected = "2147483647";
  710. i++; sl_test[i].num = 0x7FFF000000000000L; sl_test[i].expected = "9223090561878065152";
  711. i++; sl_test[i].num = 0x00007FFF00000000L; sl_test[i].expected = "140733193388032";
  712. i++; sl_test[i].num = 0x000000007FFF0000L; sl_test[i].expected = "2147418112";
  713. i++; sl_test[i].num = 0x0000000000007FFFL; sl_test[i].expected = "32767";
  714. i++; sl_test[i].num = 0x7F00000000000000L; sl_test[i].expected = "9151314442816847872";
  715. i++; sl_test[i].num = 0x007F000000000000L; sl_test[i].expected = "35747322042253312";
  716. i++; sl_test[i].num = 0x00007F0000000000L; sl_test[i].expected = "139637976727552";
  717. i++; sl_test[i].num = 0x0000007F00000000L; sl_test[i].expected = "545460846592";
  718. i++; sl_test[i].num = 0x000000007F000000L; sl_test[i].expected = "2130706432";
  719. i++; sl_test[i].num = 0x00000000007F0000L; sl_test[i].expected = "8323072";
  720. i++; sl_test[i].num = 0x0000000000007F00L; sl_test[i].expected = "32512";
  721. i++; sl_test[i].num = 0x000000000000007FL; sl_test[i].expected = "127";
  722. i++; sl_test[i].num = 0x7000000000000000L; sl_test[i].expected = "8070450532247928832";
  723. i++; sl_test[i].num = 0x0700000000000000L; sl_test[i].expected = "504403158265495552";
  724. i++; sl_test[i].num = 0x0070000000000000L; sl_test[i].expected = "31525197391593472";
  725. i++; sl_test[i].num = 0x0007000000000000L; sl_test[i].expected = "1970324836974592";
  726. i++; sl_test[i].num = 0x0000700000000000L; sl_test[i].expected = "123145302310912";
  727. i++; sl_test[i].num = 0x0000070000000000L; sl_test[i].expected = "7696581394432";
  728. i++; sl_test[i].num = 0x0000007000000000L; sl_test[i].expected = "481036337152";
  729. i++; sl_test[i].num = 0x0000000700000000L; sl_test[i].expected = "30064771072";
  730. i++; sl_test[i].num = 0x0000000070000000L; sl_test[i].expected = "1879048192";
  731. i++; sl_test[i].num = 0x0000000007000000L; sl_test[i].expected = "117440512";
  732. i++; sl_test[i].num = 0x0000000000700000L; sl_test[i].expected = "7340032";
  733. i++; sl_test[i].num = 0x0000000000070000L; sl_test[i].expected = "458752";
  734. i++; sl_test[i].num = 0x0000000000007000L; sl_test[i].expected = "28672";
  735. i++; sl_test[i].num = 0x0000000000000700L; sl_test[i].expected = "1792";
  736. i++; sl_test[i].num = 0x0000000000000070L; sl_test[i].expected = "112";
  737. i++; sl_test[i].num = 0x0000000000000007L; sl_test[i].expected = "7";
  738. i++; sl_test[i].num = 0x0000000000000001L; sl_test[i].expected = "1";
  739. i++; sl_test[i].num = 0x0000000000000000L; sl_test[i].expected = "0";
  740. i++; sl_test[i].num = -0x7FFFFFFFFFFFFFFFL -1L; sl_test[i].expected = "-9223372036854775808";
  741. i++; sl_test[i].num = -0x7FFFFFFFFFFFFFFEL -1L; sl_test[i].expected = "-9223372036854775807";
  742. i++; sl_test[i].num = -0x7FFFFFFFFFFFFFFDL -1L; sl_test[i].expected = "-9223372036854775806";
  743. i++; sl_test[i].num = -0x7FFFFFFF00000000L -1L; sl_test[i].expected = "-9223372032559808513";
  744. i++; sl_test[i].num = -0x000000007FFFFFFFL -1L; sl_test[i].expected = "-2147483648";
  745. i++; sl_test[i].num = -0x7FFF000000000000L -1L; sl_test[i].expected = "-9223090561878065153";
  746. i++; sl_test[i].num = -0x00007FFF00000000L -1L; sl_test[i].expected = "-140733193388033";
  747. i++; sl_test[i].num = -0x000000007FFF0000L -1L; sl_test[i].expected = "-2147418113";
  748. i++; sl_test[i].num = -0x0000000000007FFFL -1L; sl_test[i].expected = "-32768";
  749. i++; sl_test[i].num = -0x7F00000000000000L -1L; sl_test[i].expected = "-9151314442816847873";
  750. i++; sl_test[i].num = -0x007F000000000000L -1L; sl_test[i].expected = "-35747322042253313";
  751. i++; sl_test[i].num = -0x00007F0000000000L -1L; sl_test[i].expected = "-139637976727553";
  752. i++; sl_test[i].num = -0x0000007F00000000L -1L; sl_test[i].expected = "-545460846593";
  753. i++; sl_test[i].num = -0x000000007F000000L -1L; sl_test[i].expected = "-2130706433";
  754. i++; sl_test[i].num = -0x00000000007F0000L -1L; sl_test[i].expected = "-8323073";
  755. i++; sl_test[i].num = -0x0000000000007F00L -1L; sl_test[i].expected = "-32513";
  756. i++; sl_test[i].num = -0x000000000000007FL -1L; sl_test[i].expected = "-128";
  757. i++; sl_test[i].num = -0x7000000000000000L -1L; sl_test[i].expected = "-8070450532247928833";
  758. i++; sl_test[i].num = -0x0700000000000000L -1L; sl_test[i].expected = "-504403158265495553";
  759. i++; sl_test[i].num = -0x0070000000000000L -1L; sl_test[i].expected = "-31525197391593473";
  760. i++; sl_test[i].num = -0x0007000000000000L -1L; sl_test[i].expected = "-1970324836974593";
  761. i++; sl_test[i].num = -0x0000700000000000L -1L; sl_test[i].expected = "-123145302310913";
  762. i++; sl_test[i].num = -0x0000070000000000L -1L; sl_test[i].expected = "-7696581394433";
  763. i++; sl_test[i].num = -0x0000007000000000L -1L; sl_test[i].expected = "-481036337153";
  764. i++; sl_test[i].num = -0x0000000700000000L -1L; sl_test[i].expected = "-30064771073";
  765. i++; sl_test[i].num = -0x0000000070000000L -1L; sl_test[i].expected = "-1879048193";
  766. i++; sl_test[i].num = -0x0000000007000000L -1L; sl_test[i].expected = "-117440513";
  767. i++; sl_test[i].num = -0x0000000000700000L -1L; sl_test[i].expected = "-7340033";
  768. i++; sl_test[i].num = -0x0000000000070000L -1L; sl_test[i].expected = "-458753";
  769. i++; sl_test[i].num = -0x0000000000007000L -1L; sl_test[i].expected = "-28673";
  770. i++; sl_test[i].num = -0x0000000000000700L -1L; sl_test[i].expected = "-1793";
  771. i++; sl_test[i].num = -0x0000000000000070L -1L; sl_test[i].expected = "-113";
  772. i++; sl_test[i].num = -0x0000000000000007L -1L; sl_test[i].expected = "-8";
  773. i++; sl_test[i].num = 0x0000000000000000L -1L; sl_test[i].expected = "-1";
  774. num_slong_tests = i;
  775. #endif
  776. for(i = 1; i <= num_slong_tests; i++) {
  777. for(j = 0; j<BUFSZ; j++)
  778. sl_test[i].result[j] = 'X';
  779. sl_test[i].result[BUFSZ-1] = '\0';
  780. (void)curl_msprintf(sl_test[i].result, "%ld", sl_test[i].num);
  781. if(memcmp(sl_test[i].result,
  782. sl_test[i].expected,
  783. strlen(sl_test[i].expected))) {
  784. printf("signed long test #%.2d: Failed (Expected: %s Got: %s)\n",
  785. i, sl_test[i].expected, sl_test[i].result);
  786. failed++;
  787. }
  788. }
  789. if(!failed)
  790. printf("All curl_mprintf() signed long tests OK!\n");
  791. else
  792. printf("Some curl_mprintf() signed long tests Failed!\n");
  793. return failed;
  794. }
  795. static int test_curl_off_t_formatting(void)
  796. {
  797. int i, j;
  798. int num_cofft_tests = 0;
  799. int failed = 0;
  800. #if (SIZEOF_CURL_OFF_T == 2)
  801. i = 1; co_test[i].num = MPRNT_OFF_T_C(0x7FFF); co_test[i].expected = "32767";
  802. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFE); co_test[i].expected = "32766";
  803. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFD); co_test[i].expected = "32765";
  804. i++; co_test[i].num = MPRNT_OFF_T_C(0x7F00); co_test[i].expected = "32512";
  805. i++; co_test[i].num = MPRNT_OFF_T_C(0x07F0); co_test[i].expected = "2032";
  806. i++; co_test[i].num = MPRNT_OFF_T_C(0x007F); co_test[i].expected = "127";
  807. i++; co_test[i].num = MPRNT_OFF_T_C(0x7000); co_test[i].expected = "28672";
  808. i++; co_test[i].num = MPRNT_OFF_T_C(0x0700); co_test[i].expected = "1792";
  809. i++; co_test[i].num = MPRNT_OFF_T_C(0x0070); co_test[i].expected = "112";
  810. i++; co_test[i].num = MPRNT_OFF_T_C(0x0007); co_test[i].expected = "7";
  811. i++; co_test[i].num = MPRNT_OFF_T_C(0x5000); co_test[i].expected = "20480";
  812. i++; co_test[i].num = MPRNT_OFF_T_C(0x0500); co_test[i].expected = "1280";
  813. i++; co_test[i].num = MPRNT_OFF_T_C(0x0050); co_test[i].expected = "80";
  814. i++; co_test[i].num = MPRNT_OFF_T_C(0x0005); co_test[i].expected = "5";
  815. i++; co_test[i].num = MPRNT_OFF_T_C(0x0001); co_test[i].expected = "1";
  816. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000); co_test[i].expected = "0";
  817. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32768";
  818. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFE) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32767";
  819. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFD) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32766";
  820. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7F00) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32513";
  821. i++; co_test[i].num = -MPRNT_OFF_T_C(0x07F0) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2033";
  822. i++; co_test[i].num = -MPRNT_OFF_T_C(0x007F) -MPRNT_OFF_T_C(1); co_test[i].expected = "-128";
  823. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-28673";
  824. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0700) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1793";
  825. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0070) -MPRNT_OFF_T_C(1); co_test[i].expected = "-113";
  826. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0007) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8";
  827. i++; co_test[i].num = -MPRNT_OFF_T_C(0x5000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-20481";
  828. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0500) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1281";
  829. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0050) -MPRNT_OFF_T_C(1); co_test[i].expected = "-81";
  830. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0005) -MPRNT_OFF_T_C(1); co_test[i].expected = "-6";
  831. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1";
  832. num_cofft_tests = i;
  833. #elif (SIZEOF_CURL_OFF_T == 4)
  834. i = 1; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFF); co_test[i].expected = "2147483647";
  835. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFE); co_test[i].expected = "2147483646";
  836. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFD); co_test[i].expected = "2147483645";
  837. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFF0000); co_test[i].expected = "2147418112";
  838. i++; co_test[i].num = MPRNT_OFF_T_C(0x00007FFF); co_test[i].expected = "32767";
  839. i++; co_test[i].num = MPRNT_OFF_T_C(0x7F000000); co_test[i].expected = "2130706432";
  840. i++; co_test[i].num = MPRNT_OFF_T_C(0x007F0000); co_test[i].expected = "8323072";
  841. i++; co_test[i].num = MPRNT_OFF_T_C(0x00007F00); co_test[i].expected = "32512";
  842. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000007F); co_test[i].expected = "127";
  843. i++; co_test[i].num = MPRNT_OFF_T_C(0x70000000); co_test[i].expected = "1879048192";
  844. i++; co_test[i].num = MPRNT_OFF_T_C(0x07000000); co_test[i].expected = "117440512";
  845. i++; co_test[i].num = MPRNT_OFF_T_C(0x00700000); co_test[i].expected = "7340032";
  846. i++; co_test[i].num = MPRNT_OFF_T_C(0x00070000); co_test[i].expected = "458752";
  847. i++; co_test[i].num = MPRNT_OFF_T_C(0x00007000); co_test[i].expected = "28672";
  848. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000700); co_test[i].expected = "1792";
  849. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000070); co_test[i].expected = "112";
  850. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000007); co_test[i].expected = "7";
  851. i++; co_test[i].num = MPRNT_OFF_T_C(0x50000000); co_test[i].expected = "1342177280";
  852. i++; co_test[i].num = MPRNT_OFF_T_C(0x05000000); co_test[i].expected = "83886080";
  853. i++; co_test[i].num = MPRNT_OFF_T_C(0x00500000); co_test[i].expected = "5242880";
  854. i++; co_test[i].num = MPRNT_OFF_T_C(0x00050000); co_test[i].expected = "327680";
  855. i++; co_test[i].num = MPRNT_OFF_T_C(0x00005000); co_test[i].expected = "20480";
  856. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000500); co_test[i].expected = "1280";
  857. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000050); co_test[i].expected = "80";
  858. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000005); co_test[i].expected = "5";
  859. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000001); co_test[i].expected = "1";
  860. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000000); co_test[i].expected = "0";
  861. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2147483648";
  862. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFE) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2147483647";
  863. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFD) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2147483646";
  864. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFF0000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2147418113";
  865. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00007FFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32768";
  866. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7F000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2130706433";
  867. i++; co_test[i].num = -MPRNT_OFF_T_C(0x007F0000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8323073";
  868. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00007F00) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32513";
  869. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000007F) -MPRNT_OFF_T_C(1); co_test[i].expected = "-128";
  870. i++; co_test[i].num = -MPRNT_OFF_T_C(0x70000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1879048193";
  871. i++; co_test[i].num = -MPRNT_OFF_T_C(0x07000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-117440513";
  872. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00700000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-7340033";
  873. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00070000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-458753";
  874. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00007000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-28673";
  875. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00000700) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1793";
  876. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00000070) -MPRNT_OFF_T_C(1); co_test[i].expected = "-113";
  877. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00000007) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8";
  878. i++; co_test[i].num = -MPRNT_OFF_T_C(0x50000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1342177281";
  879. i++; co_test[i].num = -MPRNT_OFF_T_C(0x05000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-83886081";
  880. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00500000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-5242881";
  881. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00050000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-327681";
  882. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00005000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-20481";
  883. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00000500) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1281";
  884. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00000050) -MPRNT_OFF_T_C(1); co_test[i].expected = "-81";
  885. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00000005) -MPRNT_OFF_T_C(1); co_test[i].expected = "-6";
  886. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1";
  887. num_cofft_tests = i;
  888. #elif (SIZEOF_CURL_OFF_T == 8)
  889. i = 1; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFF); co_test[i].expected = "9223372036854775807";
  890. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFE); co_test[i].expected = "9223372036854775806";
  891. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFD); co_test[i].expected = "9223372036854775805";
  892. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFF00000000); co_test[i].expected = "9223372032559808512";
  893. i++; co_test[i].num = MPRNT_OFF_T_C(0x000000007FFFFFFF); co_test[i].expected = "2147483647";
  894. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFF000000000000); co_test[i].expected = "9223090561878065152";
  895. i++; co_test[i].num = MPRNT_OFF_T_C(0x00007FFF00000000); co_test[i].expected = "140733193388032";
  896. i++; co_test[i].num = MPRNT_OFF_T_C(0x000000007FFF0000); co_test[i].expected = "2147418112";
  897. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000007FFF); co_test[i].expected = "32767";
  898. i++; co_test[i].num = MPRNT_OFF_T_C(0x7F00000000000000); co_test[i].expected = "9151314442816847872";
  899. i++; co_test[i].num = MPRNT_OFF_T_C(0x007F000000000000); co_test[i].expected = "35747322042253312";
  900. i++; co_test[i].num = MPRNT_OFF_T_C(0x00007F0000000000); co_test[i].expected = "139637976727552";
  901. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000007F00000000); co_test[i].expected = "545460846592";
  902. i++; co_test[i].num = MPRNT_OFF_T_C(0x000000007F000000); co_test[i].expected = "2130706432";
  903. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000000007F0000); co_test[i].expected = "8323072";
  904. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000007F00); co_test[i].expected = "32512";
  905. i++; co_test[i].num = MPRNT_OFF_T_C(0x000000000000007F); co_test[i].expected = "127";
  906. i++; co_test[i].num = MPRNT_OFF_T_C(0x7000000000000000); co_test[i].expected = "8070450532247928832";
  907. i++; co_test[i].num = MPRNT_OFF_T_C(0x0700000000000000); co_test[i].expected = "504403158265495552";
  908. i++; co_test[i].num = MPRNT_OFF_T_C(0x0070000000000000); co_test[i].expected = "31525197391593472";
  909. i++; co_test[i].num = MPRNT_OFF_T_C(0x0007000000000000); co_test[i].expected = "1970324836974592";
  910. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000700000000000); co_test[i].expected = "123145302310912";
  911. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000070000000000); co_test[i].expected = "7696581394432";
  912. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000007000000000); co_test[i].expected = "481036337152";
  913. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000700000000); co_test[i].expected = "30064771072";
  914. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000070000000); co_test[i].expected = "1879048192";
  915. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000007000000); co_test[i].expected = "117440512";
  916. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000700000); co_test[i].expected = "7340032";
  917. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000070000); co_test[i].expected = "458752";
  918. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000007000); co_test[i].expected = "28672";
  919. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000700); co_test[i].expected = "1792";
  920. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000070); co_test[i].expected = "112";
  921. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000007); co_test[i].expected = "7";
  922. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000001); co_test[i].expected = "1";
  923. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000000); co_test[i].expected = "0";
  924. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223372036854775808";
  925. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFE) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223372036854775807";
  926. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFD) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223372036854775806";
  927. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFF00000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223372032559808513";
  928. i++; co_test[i].num = -MPRNT_OFF_T_C(0x000000007FFFFFFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2147483648";
  929. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFF000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223090561878065153";
  930. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00007FFF00000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-140733193388033";
  931. i++; co_test[i].num = -MPRNT_OFF_T_C(0x000000007FFF0000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2147418113";
  932. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000007FFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32768";
  933. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7F00000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9151314442816847873";
  934. i++; co_test[i].num = -MPRNT_OFF_T_C(0x007F000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-35747322042253313";
  935. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00007F0000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-139637976727553";
  936. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000007F00000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-545460846593";
  937. i++; co_test[i].num = -MPRNT_OFF_T_C(0x000000007F000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2130706433";
  938. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00000000007F0000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8323073";
  939. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000007F00) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32513";
  940. i++; co_test[i].num = -MPRNT_OFF_T_C(0x000000000000007F) -MPRNT_OFF_T_C(1); co_test[i].expected = "-128";
  941. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7000000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8070450532247928833";
  942. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0700000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-504403158265495553";
  943. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0070000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-31525197391593473";
  944. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0007000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1970324836974593";
  945. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000700000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-123145302310913";
  946. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000070000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-7696581394433";
  947. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000007000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-481036337153";
  948. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000700000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-30064771073";
  949. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000070000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1879048193";
  950. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000007000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-117440513";
  951. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000700000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-7340033";
  952. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000070000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-458753";
  953. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000007000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-28673";
  954. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000000700) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1793";
  955. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000000070) -MPRNT_OFF_T_C(1); co_test[i].expected = "-113";
  956. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000000007) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8";
  957. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1";
  958. num_cofft_tests = i;
  959. #endif
  960. for(i = 1; i <= num_cofft_tests; i++) {
  961. for(j = 0; j<BUFSZ; j++)
  962. co_test[i].result[j] = 'X';
  963. co_test[i].result[BUFSZ-1] = '\0';
  964. (void)curl_msprintf(co_test[i].result, "%" CURL_FORMAT_CURL_OFF_T,
  965. co_test[i].num);
  966. if(memcmp(co_test[i].result,
  967. co_test[i].expected,
  968. strlen(co_test[i].expected))) {
  969. printf("curl_off_t test #%.2d: Failed (Expected: %s Got: %s)\n",
  970. i, co_test[i].expected, co_test[i].result);
  971. failed++;
  972. }
  973. }
  974. if(!failed)
  975. printf("All curl_mprintf() curl_off_t tests OK!\n");
  976. else
  977. printf("Some curl_mprintf() curl_off_t tests Failed!\n");
  978. return failed;
  979. }
  980. static int _string_check(int linenumber, char *buf, const char *buf2)
  981. {
  982. if(strcmp(buf, buf2)) {
  983. /* they shouldn't differ */
  984. printf("sprintf line %d failed:\nwe '%s'\nsystem: '%s'\n",
  985. linenumber, buf, buf2);
  986. return 1;
  987. }
  988. return 0;
  989. }
  990. #define string_check(x,y) _string_check(__LINE__, x, y)
  991. static int _strlen_check(int linenumber, char *buf, size_t len)
  992. {
  993. size_t buflen = strlen(buf);
  994. if(len != buflen) {
  995. /* they shouldn't differ */
  996. printf("sprintf strlen:%d failed:\nwe '%zu'\nsystem: '%zu'\n",
  997. linenumber, buflen, len);
  998. return 1;
  999. }
  1000. return 0;
  1001. }
  1002. #define strlen_check(x,y) _strlen_check(__LINE__, x, y)
  1003. /*
  1004. * The output strings in this test need to have been verified with a system
  1005. * sprintf() before used here.
  1006. */
  1007. static int test_string_formatting(void)
  1008. {
  1009. int errors = 0;
  1010. char buf[256];
  1011. curl_msnprintf(buf, sizeof(buf), "%0*d%s", 2, 9, "foo");
  1012. errors += string_check(buf, "09foo");
  1013. curl_msnprintf(buf, sizeof(buf), "%*.*s", 5, 2, "foo");
  1014. errors += string_check(buf, " fo");
  1015. curl_msnprintf(buf, sizeof(buf), "%*.*s", 2, 5, "foo");
  1016. errors += string_check(buf, "foo");
  1017. curl_msnprintf(buf, sizeof(buf), "%*.*s", 0, 10, "foo");
  1018. errors += string_check(buf, "foo");
  1019. curl_msnprintf(buf, sizeof(buf), "%-10s", "foo");
  1020. errors += string_check(buf, "foo ");
  1021. curl_msnprintf(buf, sizeof(buf), "%10s", "foo");
  1022. errors += string_check(buf, " foo");
  1023. curl_msnprintf(buf, sizeof(buf), "%*.*s", -10, -10, "foo");
  1024. errors += string_check(buf, "foo ");
  1025. if(!errors)
  1026. printf("All curl_mprintf() strings tests OK!\n");
  1027. else
  1028. printf("Some curl_mprintf() string tests Failed!\n");
  1029. return errors;
  1030. }
  1031. static int test_weird_arguments(void)
  1032. {
  1033. int errors = 0;
  1034. char buf[256];
  1035. int rc;
  1036. /* MAX_PARAMETERS is 128, try exact 128! */
  1037. rc = curl_msnprintf(buf, sizeof(buf),
  1038. "%d%d%d%d%d%d%d%d%d%d" /* 10 */
  1039. "%d%d%d%d%d%d%d%d%d%d" /* 10 1 */
  1040. "%d%d%d%d%d%d%d%d%d%d" /* 10 2 */
  1041. "%d%d%d%d%d%d%d%d%d%d" /* 10 3 */
  1042. "%d%d%d%d%d%d%d%d%d%d" /* 10 4 */
  1043. "%d%d%d%d%d%d%d%d%d%d" /* 10 5 */
  1044. "%d%d%d%d%d%d%d%d%d%d" /* 10 6 */
  1045. "%d%d%d%d%d%d%d%d%d%d" /* 10 7 */
  1046. "%d%d%d%d%d%d%d%d%d%d" /* 10 8 */
  1047. "%d%d%d%d%d%d%d%d%d%d" /* 10 9 */
  1048. "%d%d%d%d%d%d%d%d%d%d" /* 10 10 */
  1049. "%d%d%d%d%d%d%d%d%d%d" /* 10 11 */
  1050. "%d%d%d%d%d%d%d%d" /* 8 */
  1051. ,
  1052. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 */
  1053. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 1 */
  1054. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 2 */
  1055. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 3 */
  1056. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 4 */
  1057. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 5 */
  1058. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 6 */
  1059. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 7 */
  1060. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 8 */
  1061. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 9 */
  1062. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 10 */
  1063. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 11 */
  1064. 0, 1, 2, 3, 4, 5, 6, 7); /* 8 */
  1065. if(rc != 128) {
  1066. printf("curl_mprintf() returned %d and not 128!\n", rc);
  1067. errors++;
  1068. }
  1069. errors += string_check(buf,
  1070. "0123456789" /* 10 */
  1071. "0123456789" /* 10 1 */
  1072. "0123456789" /* 10 2 */
  1073. "0123456789" /* 10 3 */
  1074. "0123456789" /* 10 4 */
  1075. "0123456789" /* 10 5 */
  1076. "0123456789" /* 10 6 */
  1077. "0123456789" /* 10 7 */
  1078. "0123456789" /* 10 8 */
  1079. "0123456789" /* 10 9 */
  1080. "0123456789" /* 10 10*/
  1081. "0123456789" /* 10 11 */
  1082. "01234567" /* 8 */
  1083. );
  1084. /* MAX_PARAMETERS is 128, try more! */
  1085. buf[0] = 0;
  1086. rc = curl_msnprintf(buf, sizeof(buf),
  1087. "%d%d%d%d%d%d%d%d%d%d" /* 10 */
  1088. "%d%d%d%d%d%d%d%d%d%d" /* 10 1 */
  1089. "%d%d%d%d%d%d%d%d%d%d" /* 10 2 */
  1090. "%d%d%d%d%d%d%d%d%d%d" /* 10 3 */
  1091. "%d%d%d%d%d%d%d%d%d%d" /* 10 4 */
  1092. "%d%d%d%d%d%d%d%d%d%d" /* 10 5 */
  1093. "%d%d%d%d%d%d%d%d%d%d" /* 10 6 */
  1094. "%d%d%d%d%d%d%d%d%d%d" /* 10 7 */
  1095. "%d%d%d%d%d%d%d%d%d%d" /* 10 8 */
  1096. "%d%d%d%d%d%d%d%d%d%d" /* 10 9 */
  1097. "%d%d%d%d%d%d%d%d%d%d" /* 10 10 */
  1098. "%d%d%d%d%d%d%d%d%d%d" /* 10 11 */
  1099. "%d%d%d%d%d%d%d%d%d" /* 9 */
  1100. ,
  1101. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 */
  1102. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 1 */
  1103. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 2 */
  1104. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 3 */
  1105. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 4 */
  1106. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 5 */
  1107. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 6 */
  1108. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 7 */
  1109. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 8 */
  1110. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 9 */
  1111. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 10 */
  1112. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 11 */
  1113. 0, 1, 2, 3, 4, 5, 6, 7, 8); /* 9 */
  1114. if(rc) {
  1115. printf("curl_mprintf() returned %d and not 0\n", rc);
  1116. errors++;
  1117. }
  1118. errors += string_check(buf, "");
  1119. /* Do not skip sanity checks with parameters! */
  1120. buf[0] = 0;
  1121. rc = curl_msnprintf(buf, sizeof(buf), "%d, %.*1$d", 500, 1);
  1122. if(rc != sizeof(buf) - 1) {
  1123. printf("curl_mprintf() returned %d and not %d!\n", rc,
  1124. sizeof(buf) - 1);
  1125. errors++;
  1126. }
  1127. errors += strlen_check(buf, 255);
  1128. if(errors)
  1129. printf("Some curl_mprintf() weird arguments tests failed!\n");
  1130. return errors;
  1131. }
  1132. /* DBL_MAX value from Linux */
  1133. #define MAXIMIZE -1.7976931348623157081452E+308
  1134. static int test_float_formatting(void)
  1135. {
  1136. int errors = 0;
  1137. char buf[512]; /* larger than max float size */
  1138. curl_msnprintf(buf, sizeof(buf), "%f", 9.0);
  1139. errors += string_check(buf, "9.000000");
  1140. curl_msnprintf(buf, sizeof(buf), "%.1f", 9.1);
  1141. errors += string_check(buf, "9.1");
  1142. curl_msnprintf(buf, sizeof(buf), "%.2f", 9.1);
  1143. errors += string_check(buf, "9.10");
  1144. curl_msnprintf(buf, sizeof(buf), "%.0f", 9.1);
  1145. errors += string_check(buf, "9");
  1146. curl_msnprintf(buf, sizeof(buf), "%0f", 9.1);
  1147. errors += string_check(buf, "9.100000");
  1148. curl_msnprintf(buf, sizeof(buf), "%10f", 9.1);
  1149. errors += string_check(buf, " 9.100000");
  1150. curl_msnprintf(buf, sizeof(buf), "%10.3f", 9.1);
  1151. errors += string_check(buf, " 9.100");
  1152. curl_msnprintf(buf, sizeof(buf), "%-10.3f", 9.1);
  1153. errors += string_check(buf, "9.100 ");
  1154. curl_msnprintf(buf, sizeof(buf), "%-10.3f", 9.123456);
  1155. errors += string_check(buf, "9.123 ");
  1156. curl_msnprintf(buf, sizeof(buf), "%.-2f", 9.1);
  1157. errors += string_check(buf, "9.100000");
  1158. curl_msnprintf(buf, sizeof(buf), "%*f", 10, 9.1);
  1159. errors += string_check(buf, " 9.100000");
  1160. curl_msnprintf(buf, sizeof(buf), "%*f", 3, 9.1);
  1161. errors += string_check(buf, "9.100000");
  1162. curl_msnprintf(buf, sizeof(buf), "%*f", 6, 9.2987654);
  1163. errors += string_check(buf, "9.298765");
  1164. curl_msnprintf(buf, sizeof(buf), "%*f", 6, 9.298765);
  1165. errors += string_check(buf, "9.298765");
  1166. curl_msnprintf(buf, sizeof(buf), "%*f", 6, 9.29876);
  1167. errors += string_check(buf, "9.298760");
  1168. curl_msnprintf(buf, sizeof(buf), "%.*f", 6, 9.2987654);
  1169. errors += string_check(buf, "9.298765");
  1170. curl_msnprintf(buf, sizeof(buf), "%.*f", 5, 9.2987654);
  1171. errors += string_check(buf, "9.29877");
  1172. curl_msnprintf(buf, sizeof(buf), "%.*f", 4, 9.2987654);
  1173. errors += string_check(buf, "9.2988");
  1174. curl_msnprintf(buf, sizeof(buf), "%.*f", 3, 9.2987654);
  1175. errors += string_check(buf, "9.299");
  1176. curl_msnprintf(buf, sizeof(buf), "%.*f", 2, 9.2987654);
  1177. errors += string_check(buf, "9.30");
  1178. curl_msnprintf(buf, sizeof(buf), "%.*f", 1, 9.2987654);
  1179. errors += string_check(buf, "9.3");
  1180. curl_msnprintf(buf, sizeof(buf), "%.*f", 0, 9.2987654);
  1181. errors += string_check(buf, "9");
  1182. /* very large precisions easily turn into system specific outputs so we only
  1183. check the output buffer length here as we know the internal limit */
  1184. curl_msnprintf(buf, sizeof(buf), "%.*f", (1<<30), 9.2987654);
  1185. errors += strlen_check(buf, 325);
  1186. curl_msnprintf(buf, sizeof(buf), "%10000.10000f", 9.2987654);
  1187. errors += strlen_check(buf, 325);
  1188. curl_msnprintf(buf, sizeof(buf), "%240.10000f",
  1189. 123456789123456789123456789.2987654);
  1190. errors += strlen_check(buf, 325);
  1191. /* check negative when used signed */
  1192. curl_msnprintf(buf, sizeof(buf), "%*f", INT_MIN, 9.1);
  1193. errors += string_check(buf, "9.100000");
  1194. /* curl_msnprintf() limits a single float output to 325 bytes maximum
  1195. width */
  1196. curl_msnprintf(buf, sizeof(buf), "%*f", (1<<30), 9.1);
  1197. errors += string_check(buf, " 9.100000");
  1198. curl_msnprintf(buf, sizeof(buf), "%100000f", 9.1);
  1199. errors += string_check(buf, " 9.100000");
  1200. curl_msnprintf(buf, sizeof(buf), "%f", MAXIMIZE);
  1201. errors += strlen_check(buf, 317);
  1202. curl_msnprintf(buf, 2, "%f", MAXIMIZE);
  1203. errors += strlen_check(buf, 1);
  1204. curl_msnprintf(buf, 3, "%f", MAXIMIZE);
  1205. errors += strlen_check(buf, 2);
  1206. curl_msnprintf(buf, 4, "%f", MAXIMIZE);
  1207. errors += strlen_check(buf, 3);
  1208. curl_msnprintf(buf, 5, "%f", MAXIMIZE);
  1209. errors += strlen_check(buf, 4);
  1210. curl_msnprintf(buf, 6, "%f", MAXIMIZE);
  1211. errors += strlen_check(buf, 5);
  1212. if(!errors)
  1213. printf("All float strings tests OK!\n");
  1214. else
  1215. printf("test_float_formatting Failed!\n");
  1216. return errors;
  1217. }
  1218. /* !checksrc! enable LONGLINE */
  1219. static int test_return_codes(void)
  1220. {
  1221. char buf[128];
  1222. int rc;
  1223. rc = curl_msnprintf(buf, 100, "%d", 9999);
  1224. if(rc != 4)
  1225. return 1;
  1226. rc = curl_msnprintf(buf, 100, "%d", 99999);
  1227. if(rc != 5)
  1228. return 1;
  1229. /* returns the length excluding the nul byte */
  1230. rc = curl_msnprintf(buf, 5, "%d", 99999);
  1231. if(rc != 4)
  1232. return 1;
  1233. /* returns the length excluding the nul byte */
  1234. rc = curl_msnprintf(buf, 5, "%s", "helloooooooo");
  1235. if(rc != 4)
  1236. return 1;
  1237. /* returns the length excluding the nul byte */
  1238. rc = curl_msnprintf(buf, 6, "%s", "helloooooooo");
  1239. if(rc != 5)
  1240. return 1;
  1241. return 0;
  1242. }
  1243. int test(char *URL)
  1244. {
  1245. int errors = 0;
  1246. (void)URL; /* not used */
  1247. #ifdef HAVE_SETLOCALE
  1248. /*
  1249. * The test makes assumptions about the numeric locale (specifically,
  1250. * RADIXCHAR) so set it to a known working (and portable) one.
  1251. */
  1252. setlocale(LC_NUMERIC, "C");
  1253. #endif
  1254. errors += test_weird_arguments();
  1255. errors += test_unsigned_short_formatting();
  1256. errors += test_signed_short_formatting();
  1257. errors += test_unsigned_int_formatting();
  1258. errors += test_signed_int_formatting();
  1259. errors += test_unsigned_long_formatting();
  1260. errors += test_signed_long_formatting();
  1261. errors += test_curl_off_t_formatting();
  1262. errors += test_string_formatting();
  1263. errors += test_float_formatting();
  1264. errors += test_return_codes();
  1265. if(errors)
  1266. return TEST_ERR_MAJOR_BAD;
  1267. else
  1268. return 0;
  1269. }