lib557.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. i = 1; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFF); co_test[i].expected = "9223372036854775807";
  801. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFE); co_test[i].expected = "9223372036854775806";
  802. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFD); co_test[i].expected = "9223372036854775805";
  803. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFFFFFF00000000); co_test[i].expected = "9223372032559808512";
  804. i++; co_test[i].num = MPRNT_OFF_T_C(0x000000007FFFFFFF); co_test[i].expected = "2147483647";
  805. i++; co_test[i].num = MPRNT_OFF_T_C(0x7FFF000000000000); co_test[i].expected = "9223090561878065152";
  806. i++; co_test[i].num = MPRNT_OFF_T_C(0x00007FFF00000000); co_test[i].expected = "140733193388032";
  807. i++; co_test[i].num = MPRNT_OFF_T_C(0x000000007FFF0000); co_test[i].expected = "2147418112";
  808. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000007FFF); co_test[i].expected = "32767";
  809. i++; co_test[i].num = MPRNT_OFF_T_C(0x7F00000000000000); co_test[i].expected = "9151314442816847872";
  810. i++; co_test[i].num = MPRNT_OFF_T_C(0x007F000000000000); co_test[i].expected = "35747322042253312";
  811. i++; co_test[i].num = MPRNT_OFF_T_C(0x00007F0000000000); co_test[i].expected = "139637976727552";
  812. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000007F00000000); co_test[i].expected = "545460846592";
  813. i++; co_test[i].num = MPRNT_OFF_T_C(0x000000007F000000); co_test[i].expected = "2130706432";
  814. i++; co_test[i].num = MPRNT_OFF_T_C(0x00000000007F0000); co_test[i].expected = "8323072";
  815. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000007F00); co_test[i].expected = "32512";
  816. i++; co_test[i].num = MPRNT_OFF_T_C(0x000000000000007F); co_test[i].expected = "127";
  817. i++; co_test[i].num = MPRNT_OFF_T_C(0x7000000000000000); co_test[i].expected = "8070450532247928832";
  818. i++; co_test[i].num = MPRNT_OFF_T_C(0x0700000000000000); co_test[i].expected = "504403158265495552";
  819. i++; co_test[i].num = MPRNT_OFF_T_C(0x0070000000000000); co_test[i].expected = "31525197391593472";
  820. i++; co_test[i].num = MPRNT_OFF_T_C(0x0007000000000000); co_test[i].expected = "1970324836974592";
  821. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000700000000000); co_test[i].expected = "123145302310912";
  822. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000070000000000); co_test[i].expected = "7696581394432";
  823. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000007000000000); co_test[i].expected = "481036337152";
  824. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000700000000); co_test[i].expected = "30064771072";
  825. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000070000000); co_test[i].expected = "1879048192";
  826. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000007000000); co_test[i].expected = "117440512";
  827. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000700000); co_test[i].expected = "7340032";
  828. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000070000); co_test[i].expected = "458752";
  829. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000007000); co_test[i].expected = "28672";
  830. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000700); co_test[i].expected = "1792";
  831. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000070); co_test[i].expected = "112";
  832. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000007); co_test[i].expected = "7";
  833. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000001); co_test[i].expected = "1";
  834. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000000); co_test[i].expected = "0";
  835. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223372036854775808";
  836. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFE) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223372036854775807";
  837. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFD) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223372036854775806";
  838. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFFFFFF00000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223372032559808513";
  839. i++; co_test[i].num = -MPRNT_OFF_T_C(0x000000007FFFFFFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2147483648";
  840. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7FFF000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9223090561878065153";
  841. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00007FFF00000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-140733193388033";
  842. i++; co_test[i].num = -MPRNT_OFF_T_C(0x000000007FFF0000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2147418113";
  843. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000007FFF) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32768";
  844. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7F00000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-9151314442816847873";
  845. i++; co_test[i].num = -MPRNT_OFF_T_C(0x007F000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-35747322042253313";
  846. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00007F0000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-139637976727553";
  847. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000007F00000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-545460846593";
  848. i++; co_test[i].num = -MPRNT_OFF_T_C(0x000000007F000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-2130706433";
  849. i++; co_test[i].num = -MPRNT_OFF_T_C(0x00000000007F0000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8323073";
  850. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000007F00) -MPRNT_OFF_T_C(1); co_test[i].expected = "-32513";
  851. i++; co_test[i].num = -MPRNT_OFF_T_C(0x000000000000007F) -MPRNT_OFF_T_C(1); co_test[i].expected = "-128";
  852. i++; co_test[i].num = -MPRNT_OFF_T_C(0x7000000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8070450532247928833";
  853. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0700000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-504403158265495553";
  854. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0070000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-31525197391593473";
  855. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0007000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1970324836974593";
  856. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000700000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-123145302310913";
  857. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000070000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-7696581394433";
  858. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000007000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-481036337153";
  859. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000700000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-30064771073";
  860. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000070000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1879048193";
  861. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000007000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-117440513";
  862. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000700000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-7340033";
  863. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000070000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-458753";
  864. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000007000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-28673";
  865. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000000700) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1793";
  866. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000000070) -MPRNT_OFF_T_C(1); co_test[i].expected = "-113";
  867. i++; co_test[i].num = -MPRNT_OFF_T_C(0x0000000000000007) -MPRNT_OFF_T_C(1); co_test[i].expected = "-8";
  868. i++; co_test[i].num = MPRNT_OFF_T_C(0x0000000000000000) -MPRNT_OFF_T_C(1); co_test[i].expected = "-1";
  869. num_cofft_tests = i;
  870. for(i = 1; i <= num_cofft_tests; i++) {
  871. for(j = 0; j<BUFSZ; j++)
  872. co_test[i].result[j] = 'X';
  873. co_test[i].result[BUFSZ-1] = '\0';
  874. (void)curl_msprintf(co_test[i].result, "%" CURL_FORMAT_CURL_OFF_T,
  875. co_test[i].num);
  876. if(memcmp(co_test[i].result,
  877. co_test[i].expected,
  878. strlen(co_test[i].expected))) {
  879. printf("curl_off_t test #%.2d: Failed (Expected: %s Got: %s)\n",
  880. i, co_test[i].expected, co_test[i].result);
  881. failed++;
  882. }
  883. }
  884. if(!failed)
  885. printf("All curl_mprintf() curl_off_t tests OK!\n");
  886. else
  887. printf("Some curl_mprintf() curl_off_t tests Failed!\n");
  888. return failed;
  889. }
  890. static int _string_check(int linenumber, char *buf, const char *buf2)
  891. {
  892. if(strcmp(buf, buf2)) {
  893. /* they shouldn't differ */
  894. printf("sprintf line %d failed:\nwe '%s'\nsystem: '%s'\n",
  895. linenumber, buf, buf2);
  896. return 1;
  897. }
  898. return 0;
  899. }
  900. #define string_check(x,y) _string_check(__LINE__, x, y)
  901. static int _strlen_check(int linenumber, char *buf, size_t len)
  902. {
  903. size_t buflen = strlen(buf);
  904. if(len != buflen) {
  905. /* they shouldn't differ */
  906. printf("sprintf strlen:%d failed:\nwe '%zu'\nsystem: '%zu'\n",
  907. linenumber, buflen, len);
  908. return 1;
  909. }
  910. return 0;
  911. }
  912. #define strlen_check(x,y) _strlen_check(__LINE__, x, y)
  913. /*
  914. * The output strings in this test need to have been verified with a system
  915. * sprintf() before used here.
  916. */
  917. static int test_string_formatting(void)
  918. {
  919. int errors = 0;
  920. char buf[256];
  921. curl_msnprintf(buf, sizeof(buf), "%0*d%s", 2, 9, "foo");
  922. errors += string_check(buf, "09foo");
  923. curl_msnprintf(buf, sizeof(buf), "%*.*s", 5, 2, "foo");
  924. errors += string_check(buf, " fo");
  925. curl_msnprintf(buf, sizeof(buf), "%*.*s", 2, 5, "foo");
  926. errors += string_check(buf, "foo");
  927. curl_msnprintf(buf, sizeof(buf), "%*.*s", 0, 10, "foo");
  928. errors += string_check(buf, "foo");
  929. curl_msnprintf(buf, sizeof(buf), "%-10s", "foo");
  930. errors += string_check(buf, "foo ");
  931. curl_msnprintf(buf, sizeof(buf), "%10s", "foo");
  932. errors += string_check(buf, " foo");
  933. curl_msnprintf(buf, sizeof(buf), "%*.*s", -10, -10, "foo");
  934. errors += string_check(buf, "foo ");
  935. if(!errors)
  936. printf("All curl_mprintf() strings tests OK!\n");
  937. else
  938. printf("Some curl_mprintf() string tests Failed!\n");
  939. return errors;
  940. }
  941. static int test_weird_arguments(void)
  942. {
  943. int errors = 0;
  944. char buf[256];
  945. int rc;
  946. /* MAX_PARAMETERS is 128, try exact 128! */
  947. rc = curl_msnprintf(buf, sizeof(buf),
  948. "%d%d%d%d%d%d%d%d%d%d" /* 10 */
  949. "%d%d%d%d%d%d%d%d%d%d" /* 10 1 */
  950. "%d%d%d%d%d%d%d%d%d%d" /* 10 2 */
  951. "%d%d%d%d%d%d%d%d%d%d" /* 10 3 */
  952. "%d%d%d%d%d%d%d%d%d%d" /* 10 4 */
  953. "%d%d%d%d%d%d%d%d%d%d" /* 10 5 */
  954. "%d%d%d%d%d%d%d%d%d%d" /* 10 6 */
  955. "%d%d%d%d%d%d%d%d%d%d" /* 10 7 */
  956. "%d%d%d%d%d%d%d%d%d%d" /* 10 8 */
  957. "%d%d%d%d%d%d%d%d%d%d" /* 10 9 */
  958. "%d%d%d%d%d%d%d%d%d%d" /* 10 10 */
  959. "%d%d%d%d%d%d%d%d%d%d" /* 10 11 */
  960. "%d%d%d%d%d%d%d%d" /* 8 */
  961. ,
  962. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 */
  963. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 1 */
  964. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 2 */
  965. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 3 */
  966. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 4 */
  967. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 5 */
  968. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 6 */
  969. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 7 */
  970. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 8 */
  971. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 9 */
  972. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 10 */
  973. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 11 */
  974. 0, 1, 2, 3, 4, 5, 6, 7); /* 8 */
  975. if(rc != 128) {
  976. printf("curl_mprintf() returned %d and not 128!\n", rc);
  977. errors++;
  978. }
  979. errors += string_check(buf,
  980. "0123456789" /* 10 */
  981. "0123456789" /* 10 1 */
  982. "0123456789" /* 10 2 */
  983. "0123456789" /* 10 3 */
  984. "0123456789" /* 10 4 */
  985. "0123456789" /* 10 5 */
  986. "0123456789" /* 10 6 */
  987. "0123456789" /* 10 7 */
  988. "0123456789" /* 10 8 */
  989. "0123456789" /* 10 9 */
  990. "0123456789" /* 10 10 */
  991. "0123456789" /* 10 11 */
  992. "01234567" /* 8 */
  993. );
  994. /* MAX_PARAMETERS is 128, try more! */
  995. buf[0] = 0;
  996. rc = curl_msnprintf(buf, sizeof(buf),
  997. "%d%d%d%d%d%d%d%d%d%d" /* 10 */
  998. "%d%d%d%d%d%d%d%d%d%d" /* 10 1 */
  999. "%d%d%d%d%d%d%d%d%d%d" /* 10 2 */
  1000. "%d%d%d%d%d%d%d%d%d%d" /* 10 3 */
  1001. "%d%d%d%d%d%d%d%d%d%d" /* 10 4 */
  1002. "%d%d%d%d%d%d%d%d%d%d" /* 10 5 */
  1003. "%d%d%d%d%d%d%d%d%d%d" /* 10 6 */
  1004. "%d%d%d%d%d%d%d%d%d%d" /* 10 7 */
  1005. "%d%d%d%d%d%d%d%d%d%d" /* 10 8 */
  1006. "%d%d%d%d%d%d%d%d%d%d" /* 10 9 */
  1007. "%d%d%d%d%d%d%d%d%d%d" /* 10 10 */
  1008. "%d%d%d%d%d%d%d%d%d%d" /* 10 11 */
  1009. "%d%d%d%d%d%d%d%d%d" /* 9 */
  1010. ,
  1011. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 */
  1012. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 1 */
  1013. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 2 */
  1014. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 3 */
  1015. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 4 */
  1016. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 5 */
  1017. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 6 */
  1018. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 7 */
  1019. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 8 */
  1020. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 9 */
  1021. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 10 */
  1022. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 10 11 */
  1023. 0, 1, 2, 3, 4, 5, 6, 7, 8); /* 9 */
  1024. if(rc) {
  1025. printf("curl_mprintf() returned %d and not 0\n", rc);
  1026. errors++;
  1027. }
  1028. errors += string_check(buf, "");
  1029. /* Do not skip sanity checks with parameters! */
  1030. buf[0] = 0;
  1031. rc = curl_msnprintf(buf, sizeof(buf), "%d, %.*1$d", 500, 1);
  1032. if(rc != sizeof(buf) - 1) {
  1033. printf("curl_mprintf() returned %d and not %d!\n", rc,
  1034. sizeof(buf) - 1);
  1035. errors++;
  1036. }
  1037. errors += strlen_check(buf, 255);
  1038. if(errors)
  1039. printf("Some curl_mprintf() weird arguments tests failed!\n");
  1040. return errors;
  1041. }
  1042. /* DBL_MAX value from Linux */
  1043. #define MAXIMIZE -1.7976931348623157081452E+308
  1044. static int test_float_formatting(void)
  1045. {
  1046. int errors = 0;
  1047. char buf[512]; /* larger than max float size */
  1048. curl_msnprintf(buf, sizeof(buf), "%f", 9.0);
  1049. errors += string_check(buf, "9.000000");
  1050. curl_msnprintf(buf, sizeof(buf), "%.1f", 9.1);
  1051. errors += string_check(buf, "9.1");
  1052. curl_msnprintf(buf, sizeof(buf), "%.2f", 9.1);
  1053. errors += string_check(buf, "9.10");
  1054. curl_msnprintf(buf, sizeof(buf), "%.0f", 9.1);
  1055. errors += string_check(buf, "9");
  1056. curl_msnprintf(buf, sizeof(buf), "%0f", 9.1);
  1057. errors += string_check(buf, "9.100000");
  1058. curl_msnprintf(buf, sizeof(buf), "%10f", 9.1);
  1059. errors += string_check(buf, " 9.100000");
  1060. curl_msnprintf(buf, sizeof(buf), "%10.3f", 9.1);
  1061. errors += string_check(buf, " 9.100");
  1062. curl_msnprintf(buf, sizeof(buf), "%-10.3f", 9.1);
  1063. errors += string_check(buf, "9.100 ");
  1064. curl_msnprintf(buf, sizeof(buf), "%-10.3f", 9.123456);
  1065. errors += string_check(buf, "9.123 ");
  1066. curl_msnprintf(buf, sizeof(buf), "%.-2f", 9.1);
  1067. errors += string_check(buf, "9.100000");
  1068. curl_msnprintf(buf, sizeof(buf), "%*f", 10, 9.1);
  1069. errors += string_check(buf, " 9.100000");
  1070. curl_msnprintf(buf, sizeof(buf), "%*f", 3, 9.1);
  1071. errors += string_check(buf, "9.100000");
  1072. curl_msnprintf(buf, sizeof(buf), "%*f", 6, 9.2987654);
  1073. errors += string_check(buf, "9.298765");
  1074. curl_msnprintf(buf, sizeof(buf), "%*f", 6, 9.298765);
  1075. errors += string_check(buf, "9.298765");
  1076. curl_msnprintf(buf, sizeof(buf), "%*f", 6, 9.29876);
  1077. errors += string_check(buf, "9.298760");
  1078. curl_msnprintf(buf, sizeof(buf), "%.*f", 6, 9.2987654);
  1079. errors += string_check(buf, "9.298765");
  1080. curl_msnprintf(buf, sizeof(buf), "%.*f", 5, 9.2987654);
  1081. errors += string_check(buf, "9.29877");
  1082. curl_msnprintf(buf, sizeof(buf), "%.*f", 4, 9.2987654);
  1083. errors += string_check(buf, "9.2988");
  1084. curl_msnprintf(buf, sizeof(buf), "%.*f", 3, 9.2987654);
  1085. errors += string_check(buf, "9.299");
  1086. curl_msnprintf(buf, sizeof(buf), "%.*f", 2, 9.2987654);
  1087. errors += string_check(buf, "9.30");
  1088. curl_msnprintf(buf, sizeof(buf), "%.*f", 1, 9.2987654);
  1089. errors += string_check(buf, "9.3");
  1090. curl_msnprintf(buf, sizeof(buf), "%.*f", 0, 9.2987654);
  1091. errors += string_check(buf, "9");
  1092. /* very large precisions easily turn into system specific outputs so we only
  1093. check the output buffer length here as we know the internal limit */
  1094. curl_msnprintf(buf, sizeof(buf), "%.*f", (1<<30), 9.2987654);
  1095. errors += strlen_check(buf, 325);
  1096. curl_msnprintf(buf, sizeof(buf), "%10000.10000f", 9.2987654);
  1097. errors += strlen_check(buf, 325);
  1098. curl_msnprintf(buf, sizeof(buf), "%240.10000f",
  1099. 123456789123456789123456789.2987654);
  1100. errors += strlen_check(buf, 325);
  1101. /* check negative when used signed */
  1102. curl_msnprintf(buf, sizeof(buf), "%*f", INT_MIN, 9.1);
  1103. errors += string_check(buf, "9.100000");
  1104. /* curl_msnprintf() limits a single float output to 325 bytes maximum
  1105. width */
  1106. curl_msnprintf(buf, sizeof(buf), "%*f", (1<<30), 9.1);
  1107. errors += string_check(buf, " 9.100000");
  1108. curl_msnprintf(buf, sizeof(buf), "%100000f", 9.1);
  1109. errors += string_check(buf, " 9.100000");
  1110. curl_msnprintf(buf, sizeof(buf), "%f", MAXIMIZE);
  1111. errors += strlen_check(buf, 317);
  1112. curl_msnprintf(buf, 2, "%f", MAXIMIZE);
  1113. errors += strlen_check(buf, 1);
  1114. curl_msnprintf(buf, 3, "%f", MAXIMIZE);
  1115. errors += strlen_check(buf, 2);
  1116. curl_msnprintf(buf, 4, "%f", MAXIMIZE);
  1117. errors += strlen_check(buf, 3);
  1118. curl_msnprintf(buf, 5, "%f", MAXIMIZE);
  1119. errors += strlen_check(buf, 4);
  1120. curl_msnprintf(buf, 6, "%f", MAXIMIZE);
  1121. errors += strlen_check(buf, 5);
  1122. if(!errors)
  1123. printf("All float strings tests OK!\n");
  1124. else
  1125. printf("test_float_formatting Failed!\n");
  1126. return errors;
  1127. }
  1128. /* !checksrc! enable LONGLINE */
  1129. static int test_return_codes(void)
  1130. {
  1131. char buf[128];
  1132. int rc;
  1133. rc = curl_msnprintf(buf, 100, "%d", 9999);
  1134. if(rc != 4)
  1135. return 1;
  1136. rc = curl_msnprintf(buf, 100, "%d", 99999);
  1137. if(rc != 5)
  1138. return 1;
  1139. /* returns the length excluding the nul byte */
  1140. rc = curl_msnprintf(buf, 5, "%d", 99999);
  1141. if(rc != 4)
  1142. return 1;
  1143. /* returns the length excluding the nul byte */
  1144. rc = curl_msnprintf(buf, 5, "%s", "helloooooooo");
  1145. if(rc != 4)
  1146. return 1;
  1147. /* returns the length excluding the nul byte */
  1148. rc = curl_msnprintf(buf, 6, "%s", "helloooooooo");
  1149. if(rc != 5)
  1150. return 1;
  1151. return 0;
  1152. }
  1153. int test(char *URL)
  1154. {
  1155. int errors = 0;
  1156. (void)URL; /* not used */
  1157. #ifdef HAVE_SETLOCALE
  1158. /*
  1159. * The test makes assumptions about the numeric locale (specifically,
  1160. * RADIXCHAR) so set it to a known working (and portable) one.
  1161. */
  1162. setlocale(LC_NUMERIC, "C");
  1163. #endif
  1164. errors += test_weird_arguments();
  1165. errors += test_unsigned_short_formatting();
  1166. errors += test_signed_short_formatting();
  1167. errors += test_unsigned_int_formatting();
  1168. errors += test_signed_int_formatting();
  1169. errors += test_unsigned_long_formatting();
  1170. errors += test_signed_long_formatting();
  1171. errors += test_curl_off_t_formatting();
  1172. errors += test_string_formatting();
  1173. errors += test_float_formatting();
  1174. errors += test_return_codes();
  1175. if(errors)
  1176. return TEST_ERR_MAJOR_BAD;
  1177. else
  1178. return 0;
  1179. }