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