1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
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.
18 int curl_msprintf(char *buffer, const char *format, ...);
21 #if (CURL_SIZEOF_CURL_OFF_T > 4)
22 # if (CURL_SIZEOF_LONG > 4)
23 # define MPRNT_SUFFIX_CURL_OFF_T L
25 # define MPRNT_SUFFIX_CURL_OFF_T LL
28 # if (CURL_SIZEOF_LONG > 2)
29 # define MPRNT_SUFFIX_CURL_OFF_T L
31 # define MPRNT_SUFFIX_CURL_OFF_T LL
36 # define MPRNT_OFF_T_C_HELPER2(Val,Suffix) Val ## Suffix
38 # define MPRNT_OFF_T_C_HELPER2(Val,Suffix) Val/**/Suffix
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)
45 #define NUM_ULONG_TESTS 4
46 #define NUM_SLONG_TESTS 7
47 #define NUM_COFFT_TESTS 7
51 unsigned long num; /* unsigned long */
52 const char *expected; /* expected string */
53 char result[BUFSZ]; /* result string */
58 long num; /* signed long */
59 const char *expected; /* expected string */
60 char result[BUFSZ]; /* result string */
65 curl_off_t num; /* curl_off_t */
66 const char *expected; /* expected string */
67 char result[BUFSZ]; /* result string */
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];
76 static int test_unsigned_long_formatting(void)
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";
102 for(i=0; i<NUM_ULONG_TESTS; i++) {
104 for(j=0; j<BUFSZ; j++)
105 ul_test[i].result[j] = 'X';
106 ul_test[i].result[BUFSZ-1] = '\0';
108 (void)curl_msprintf(ul_test[i].result, "%lu", ul_test[i].num);
110 if(!memcmp(ul_test[i].result,
112 strlen(ul_test[i].expected)))
113 printf("unsigned long test #%d: OK\n", i+1);
115 printf("unsigned long test #%d: Failed (Expected: %s Got: %s)\n",
116 i+1, ul_test[i].expected, ul_test[i].result);
126 static int test_signed_long_formatting(void)
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";
166 for(i=0; i<NUM_SLONG_TESTS; i++) {
168 for(j=0; j<BUFSZ; j++)
169 sl_test[i].result[j] = 'X';
170 sl_test[i].result[BUFSZ-1] = '\0';
172 (void)curl_msprintf(sl_test[i].result, "%ld", sl_test[i].num);
174 if(!memcmp(sl_test[i].result,
176 strlen(sl_test[i].expected)))
177 printf("signed long test #%d: OK\n", i+1);
179 printf("signed long test #%d: Failed (Expected: %s Got: %s)\n",
180 i+1, sl_test[i].expected, sl_test[i].result);
190 static int test_curl_off_t_formatting(void)
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";
230 for(i=0; i<NUM_COFFT_TESTS; i++) {
232 for(j=0; j<BUFSZ; j++)
233 co_test[i].result[j] = 'X';
234 co_test[i].result[BUFSZ-1] = '\0';
236 (void)curl_msprintf(co_test[i].result, "%" FORMAT_OFF_T, co_test[i].num);
238 if(!memcmp(co_test[i].result,
240 strlen(co_test[i].expected)))
241 printf("curl_off_t test #%d: OK\n", i+1);
243 printf("curl_off_t test #%d: Failed (Expected: %s Got: %s)\n",
244 i+1, co_test[i].expected, co_test[i].result);
257 (void)URL; /* not used */
259 errors += test_unsigned_long_formatting();
261 errors += test_signed_long_formatting();
263 errors += test_curl_off_t_formatting();
266 return TEST_ERR_MAJOR_BAD;