Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 12 | * are also available at https://curl.se/docs/copyright.html. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | ***************************************************************************/ |
| 22 | #include "curlcheck.h" |
| 23 | |
| 24 | #include "curl/mprintf.h" |
| 25 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 26 | static CURLcode unit_setup(void) {return CURLE_OK;} |
| 27 | static void unit_stop(void) {} |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 28 | |
| 29 | UNITTEST_START |
| 30 | |
| 31 | int rc; |
| 32 | char buf[3] = {'b', 'u', 'g'}; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 33 | const char *str = "bug"; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 34 | int width = 3; |
| 35 | char output[24]; |
| 36 | |
| 37 | /*#define curl_msnprintf snprintf */ |
| 38 | |
| 39 | /* without a trailing zero */ |
| 40 | rc = curl_msnprintf(output, 4, "%.*s", width, buf); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 41 | fail_unless(rc == 3, "return code should be 3"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 42 | fail_unless(!strcmp(output, "bug"), "wrong output"); |
| 43 | |
| 44 | /* with a trailing zero */ |
| 45 | rc = curl_msnprintf(output, 4, "%.*s", width, str); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 46 | fail_unless(rc == 3, "return code should be 3"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 47 | fail_unless(!strcmp(output, "bug"), "wrong output"); |
| 48 | |
| 49 | width = 2; |
| 50 | /* one byte less */ |
| 51 | rc = curl_msnprintf(output, 4, "%.*s", width, buf); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 52 | fail_unless(rc == 2, "return code should be 2"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 53 | fail_unless(!strcmp(output, "bu"), "wrong output"); |
| 54 | |
| 55 | /* string with larger precision */ |
| 56 | rc = curl_msnprintf(output, 8, "%.8s", str); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 57 | fail_unless(rc == 3, "return code should be 3"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 58 | fail_unless(!strcmp(output, "bug"), "wrong output"); |
| 59 | |
| 60 | /* longer string with precision */ |
| 61 | rc = curl_msnprintf(output, 8, "%.3s", "0123456789"); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 62 | fail_unless(rc == 3, "return code should be 3"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 63 | fail_unless(!strcmp(output, "012"), "wrong output"); |
| 64 | |
| 65 | /* negative width */ |
| 66 | rc = curl_msnprintf(output, 8, "%-8s", str); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 67 | fail_unless(rc == 7, "return code should be 7"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 68 | fail_unless(!strcmp(output, "bug "), "wrong output"); |
| 69 | |
| 70 | /* larger width that string length */ |
| 71 | rc = curl_msnprintf(output, 8, "%8s", str); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 72 | fail_unless(rc == 7, "return code should be 7"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 73 | fail_unless(!strcmp(output, " bu"), "wrong output"); |
| 74 | |
| 75 | /* output a number in a limited output */ |
| 76 | rc = curl_msnprintf(output, 4, "%d", 10240); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 77 | fail_unless(rc == 3, "return code should be 3"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 78 | fail_unless(!strcmp(output, "102"), "wrong output"); |
| 79 | |
| 80 | /* padded strings */ |
| 81 | rc = curl_msnprintf(output, 16, "%8s%8s", str, str); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 82 | fail_unless(rc == 15, "return code should be 15"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 83 | fail_unless(!strcmp(output, " bug bu"), "wrong output"); |
| 84 | |
| 85 | /* padded numbers */ |
| 86 | rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678); |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 87 | fail_unless(rc == 15, "return code should be 15"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 88 | fail_unless(!strcmp(output, " 1234 567"), "wrong output"); |
| 89 | |
| 90 | UNITTEST_STOP |