Added test case 557 to verify libcurl's internal curl_m*printf() functions
[platform/upstream/curl.git] / tests / libtest / lib557.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  */
10
11 /*
12  * The purpose of this test is to minimally exercise libcurl's internal
13  * curl_m*printf formatting capabilities and handling of some data types.
14  */
15
16 #include "test.h"
17
18 int curl_msprintf(char *buffer, const char *format, ...);
19
20
21 #if (CURL_SIZEOF_CURL_OFF_T > 4)
22 #  if (CURL_SIZEOF_LONG > 4)
23 #    define MPRNT_SUFFIX_CURL_OFF_T  L
24 #  else
25 #    define MPRNT_SUFFIX_CURL_OFF_T  LL
26 #  endif
27 #else
28 #  if (CURL_SIZEOF_LONG > 2)
29 #    define MPRNT_SUFFIX_CURL_OFF_T  L
30 #  else
31 #    define MPRNT_SUFFIX_CURL_OFF_T  LL
32 #  endif
33 #endif
34
35 #ifdef CURL_ISOCPP
36 #  define MPRNT_OFF_T_C_HELPER2(Val,Suffix) Val ## Suffix
37 #else
38 #  define MPRNT_OFF_T_C_HELPER2(Val,Suffix) Val/**/Suffix
39 #endif
40 #define MPRNT_OFF_T_C_HELPER1(Val,Suffix) MPRNT_OFF_T_C_HELPER2(Val,Suffix)
41 #define MPRNT_OFF_T_C(Val)  MPRNT_OFF_T_C_HELPER1(Val,MPRNT_SUFFIX_CURL_OFF_T)
42
43
44 #define BUFSZ    256
45 #define NUM_ULONG_TESTS  4
46 #define NUM_SLONG_TESTS  7
47 #define NUM_COFFT_TESTS  7
48
49
50 struct unslong_st {
51   unsigned long num;    /* unsigned long   */
52   const char *expected; /* expected string */
53   char result[BUFSZ];   /* result string   */
54 };
55
56
57 struct siglong_st {
58   long num;             /* signed long     */
59   const char *expected; /* expected string */
60   char result[BUFSZ];   /* result string   */
61 };
62
63
64 struct curloff_st {
65   curl_off_t num;       /* curl_off_t      */
66   const char *expected; /* expected string */
67   char result[BUFSZ];   /* result string   */
68 };
69
70
71 static struct unslong_st ul_test[NUM_ULONG_TESTS];
72 static struct siglong_st sl_test[NUM_SLONG_TESTS];
73 static struct curloff_st co_test[NUM_COFFT_TESTS];
74
75
76 static int test_unsigned_long_formatting(void)
77 {
78   int i, j;
79   int failed = 0;
80
81   ul_test[0].num = 0x0L;
82   ul_test[0].expected = "0";
83   ul_test[1].num = 0x1L;
84   ul_test[1].expected = "1";
85 #if (CURL_SIZEOF_LONG == 2)
86   ul_test[2].num = 0xFFL;
87   ul_test[2].expected = "255";
88   ul_test[3].num = 0xFFFFL;
89   ul_test[3].expected = "65535";
90 #elif (CURL_SIZEOF_LONG == 4)
91   ul_test[2].num = 0xFFFFL;
92   ul_test[2].expected = "65535";
93   ul_test[3].num = 0xFFFFFFFFL;
94   ul_test[3].expected = "4294967295";
95 #elif (CURL_SIZEOF_LONG == 8)
96   ul_test[2].num = 0xFFFFFFFFL;
97   ul_test[2].expected = "4294967295";
98   ul_test[3].num = 0xFFFFFFFFFFFFFFFFL;
99   ul_test[3].expected = "18446744073709551615";
100 #endif
101
102   for(i=0; i<NUM_ULONG_TESTS; i++) {
103
104     for(j=0; j<BUFSZ; j++)
105       ul_test[i].result[j] = 'X';
106     ul_test[i].result[BUFSZ-1] = '\0';
107
108     (void)curl_msprintf(ul_test[i].result, "%lu", ul_test[i].num);
109
110     if(!memcmp(ul_test[i].result,
111                ul_test[i].expected,
112                strlen(ul_test[i].expected)))
113       printf("unsigned long test #%d: OK\n", i+1);
114     else {
115       printf("unsigned long test #%d: Failed (Expected: %s Got: %s)\n",
116              i+1, ul_test[i].expected, ul_test[i].result);
117       failed++;
118     }
119
120   }
121
122   return failed;
123 }
124
125
126 static int test_signed_long_formatting(void)
127 {
128   int i, j;
129   int failed = 0;
130
131   sl_test[0].num = 0x0L;
132   sl_test[0].expected = "0";
133   sl_test[1].num = 0x1L;
134   sl_test[1].expected = "1";
135   sl_test[2].num = -0x1L;
136   sl_test[2].expected = "-1";
137 #if (CURL_SIZEOF_LONG == 2)
138   sl_test[3].num = 0x7FL;
139   sl_test[3].expected = "127";
140   sl_test[4].num = 0x7FFFL;
141   sl_test[4].expected = "32767";
142   sl_test[5].num = -0x7FL -1L;
143   sl_test[5].expected = "-128";
144   sl_test[6].num = -0x7FFFL -1L;
145   sl_test[6].expected = "-32768";
146 #elif (CURL_SIZEOF_LONG == 4)
147   sl_test[3].num = 0x7FFFL;
148   sl_test[3].expected = "32767";
149   sl_test[4].num = 0x7FFFFFFFL;
150   sl_test[4].expected = "2147483647";
151   sl_test[5].num = -0x7FFFL -1L;
152   sl_test[5].expected = "-32768";
153   sl_test[6].num = -0x7FFFFFFFL -1L;
154   sl_test[6].expected = "-2147483648";
155 #elif (CURL_SIZEOF_LONG == 8)
156   sl_test[3].num = 0x7FFFFFFFL;
157   sl_test[3].expected = "2147483647";
158   sl_test[4].num = 0x7FFFFFFFFFFFFFFFL;
159   sl_test[4].expected = "9223372036854775807";
160   sl_test[5].num = -0x7FFFFFFFL -1L;
161   sl_test[5].expected = "-2147483648";
162   sl_test[6].num = -0x7FFFFFFFFFFFFFFFL -1L;
163   sl_test[6].expected = "-9223372036854775808";
164 #endif
165
166   for(i=0; i<NUM_SLONG_TESTS; i++) {
167
168     for(j=0; j<BUFSZ; j++)
169       sl_test[i].result[j] = 'X';
170     sl_test[i].result[BUFSZ-1] = '\0';
171
172     (void)curl_msprintf(sl_test[i].result, "%ld", sl_test[i].num);
173
174     if(!memcmp(sl_test[i].result,
175                sl_test[i].expected,
176                strlen(sl_test[i].expected)))
177       printf("signed long test #%d: OK\n", i+1);
178     else {
179       printf("signed long test #%d: Failed (Expected: %s Got: %s)\n",
180              i+1, sl_test[i].expected, sl_test[i].result);
181       failed++;
182     }
183
184   }
185
186   return failed;
187 }
188
189
190 static int test_curl_off_t_formatting(void)
191 {
192   int i, j;
193   int failed = 0;
194
195   co_test[0].num = MPRNT_OFF_T_C(0x0);
196   co_test[0].expected = "0";
197   co_test[1].num = MPRNT_OFF_T_C(0x1);
198   co_test[1].expected = "1";
199   co_test[2].num = -MPRNT_OFF_T_C(0x1);
200   co_test[2].expected = "-1";
201 #if (CURL_SIZEOF_CURL_OFF_T == 2)
202   co_test[3].num = MPRNT_OFF_T_C(0x7F);
203   co_test[3].expected = "127";
204   co_test[4].num = MPRNT_OFF_T_C(0x7FFF);
205   co_test[4].expected = "32767";
206   co_test[5].num = -MPRNT_OFF_T_C(0x7F) -MPRNT_OFF_T_C(1);
207   co_test[5].expected = "-128";
208   co_test[6].num = -MPRNT_OFF_T_C(0x7FFF) -MPRNT_OFF_T_C(1);
209   co_test[6].expected = "-32768";
210 #elif (CURL_SIZEOF_CURL_OFF_T == 4)
211   co_test[3].num = MPRNT_OFF_T_C(0x7FFF);
212   co_test[3].expected = "32767";
213   co_test[4].num = MPRNT_OFF_T_C(0x7FFFFFFF);
214   co_test[4].expected = "2147483647";
215   co_test[5].num = -MPRNT_OFF_T_C(0x7FFF) -MPRNT_OFF_T_C(1);
216   co_test[5].expected = "-32768";
217   co_test[6].num = -MPRNT_OFF_T_C(0x7FFFFFFF) -MPRNT_OFF_T_C(1);
218   co_test[6].expected = "-2147483648";
219 #elif (CURL_SIZEOF_CURL_OFF_T == 8)
220   co_test[3].num = MPRNT_OFF_T_C(0x7FFFFFFF);
221   co_test[3].expected = "2147483647";
222   co_test[4].num = MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFF);
223   co_test[4].expected = "9223372036854775807";
224   co_test[5].num = -MPRNT_OFF_T_C(0x7FFFFFFF) -MPRNT_OFF_T_C(1);
225   co_test[5].expected = "-2147483648";
226   co_test[6].num = -MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFF) -MPRNT_OFF_T_C(1);
227   co_test[6].expected = "-9223372036854775808";
228 #endif
229
230   for(i=0; i<NUM_COFFT_TESTS; i++) {
231
232     for(j=0; j<BUFSZ; j++)
233       co_test[i].result[j] = 'X';
234     co_test[i].result[BUFSZ-1] = '\0';
235
236     (void)curl_msprintf(co_test[i].result, "%" FORMAT_OFF_T, co_test[i].num);
237
238     if(!memcmp(co_test[i].result,
239                co_test[i].expected,
240                strlen(co_test[i].expected)))
241       printf("curl_off_t test #%d: OK\n", i+1);
242     else {
243       printf("curl_off_t test #%d: Failed (Expected: %s Got: %s)\n",
244              i+1, co_test[i].expected, co_test[i].result);
245       failed++;
246     }
247
248   }
249
250   return failed;
251 }
252
253
254 int test(char *URL)
255 {
256   int errors = 0;
257   (void)URL; /* not used */
258
259   errors += test_unsigned_long_formatting();
260
261   errors += test_signed_long_formatting();
262
263   errors += test_curl_off_t_formatting();
264
265   if(errors)
266     return TEST_ERR_MAJOR_BAD;
267   else
268     return 0;
269 }