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