2
0

lib557.c 68 KB

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