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