| xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** | 
|  | 2 | *                                  _   _ ____  _ | 
|  | 3 | *  Project                     ___| | | |  _ \| | | 
|  | 4 | *                             / __| | | | |_) | | | 
|  | 5 | *                            | (__| |_| |  _ <| |___ | 
|  | 6 | *                             \___|\___/|_| \_\_____| | 
|  | 7 | * | 
|  | 8 | * Copyright (C) 1998 - 2022, 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 | #include "curlcheck.h" | 
|  | 25 |  | 
|  | 26 | #include "curl/mprintf.h" | 
|  | 27 |  | 
|  | 28 | static CURLcode unit_setup(void) {return CURLE_OK;} | 
|  | 29 | static void unit_stop(void) {} | 
|  | 30 |  | 
|  | 31 | UNITTEST_START | 
|  | 32 |  | 
|  | 33 | int rc; | 
|  | 34 | char buf[3] = {'b', 'u', 'g'}; | 
|  | 35 | const char *str = "bug"; | 
|  | 36 | int width = 3; | 
|  | 37 | char output[24]; | 
|  | 38 |  | 
|  | 39 | /*#define curl_msnprintf snprintf */ | 
|  | 40 |  | 
|  | 41 | /* without a trailing zero */ | 
|  | 42 | rc = curl_msnprintf(output, 4, "%.*s", width, buf); | 
|  | 43 | fail_unless(rc == 3, "return code should be 3"); | 
|  | 44 | fail_unless(!strcmp(output, "bug"), "wrong output"); | 
|  | 45 |  | 
|  | 46 | /* with a trailing zero */ | 
|  | 47 | rc = curl_msnprintf(output, 4, "%.*s", width, str); | 
|  | 48 | fail_unless(rc == 3, "return code should be 3"); | 
|  | 49 | fail_unless(!strcmp(output, "bug"), "wrong output"); | 
|  | 50 |  | 
|  | 51 | width = 2; | 
|  | 52 | /* one byte less */ | 
|  | 53 | rc = curl_msnprintf(output, 4, "%.*s", width, buf); | 
|  | 54 | fail_unless(rc == 2, "return code should be 2"); | 
|  | 55 | fail_unless(!strcmp(output, "bu"), "wrong output"); | 
|  | 56 |  | 
|  | 57 | /* string with larger precision */ | 
|  | 58 | rc = curl_msnprintf(output, 8, "%.8s", str); | 
|  | 59 | fail_unless(rc == 3, "return code should be 3"); | 
|  | 60 | fail_unless(!strcmp(output, "bug"), "wrong output"); | 
|  | 61 |  | 
|  | 62 | /* longer string with precision */ | 
|  | 63 | rc = curl_msnprintf(output, 8, "%.3s", "0123456789"); | 
|  | 64 | fail_unless(rc == 3, "return code should be 3"); | 
|  | 65 | fail_unless(!strcmp(output, "012"), "wrong output"); | 
|  | 66 |  | 
|  | 67 | /* negative width */ | 
|  | 68 | rc = curl_msnprintf(output, 8, "%-8s", str); | 
|  | 69 | fail_unless(rc == 7, "return code should be 7"); | 
|  | 70 | fail_unless(!strcmp(output, "bug    "), "wrong output"); | 
|  | 71 |  | 
|  | 72 | /* larger width that string length */ | 
|  | 73 | rc = curl_msnprintf(output, 8, "%8s", str); | 
|  | 74 | fail_unless(rc == 7, "return code should be 7"); | 
|  | 75 | fail_unless(!strcmp(output, "     bu"), "wrong output"); | 
|  | 76 |  | 
|  | 77 | /* output a number in a limited output */ | 
|  | 78 | rc = curl_msnprintf(output, 4, "%d", 10240); | 
|  | 79 | fail_unless(rc == 3, "return code should be 3"); | 
|  | 80 | fail_unless(!strcmp(output, "102"), "wrong output"); | 
|  | 81 |  | 
|  | 82 | /* padded strings */ | 
|  | 83 | rc = curl_msnprintf(output, 16, "%8s%8s", str, str); | 
|  | 84 | fail_unless(rc == 15, "return code should be 15"); | 
|  | 85 | fail_unless(!strcmp(output, "     bug     bu"), "wrong output"); | 
|  | 86 |  | 
|  | 87 | /* padded numbers */ | 
|  | 88 | rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678); | 
|  | 89 | fail_unless(rc == 15, "return code should be 15"); | 
|  | 90 | fail_unless(!strcmp(output, "    1234    567"), "wrong output"); | 
|  | 91 |  | 
|  | 92 | /* double precision */ | 
|  | 93 | rc = curl_msnprintf(output, 24, "%.*1$.99d", 3, 5678); | 
|  | 94 | fail_unless(rc == 0, "return code should be 0"); | 
|  | 95 |  | 
|  | 96 | UNITTEST_STOP |