1 | /*************************************************************************** |
2 | * _ _ ____ _ |
3 | * Project ___| | | | _ \| | |
4 | * / __| | | | |_) | | |
5 | * | (__| |_| | _ <| |___ |
6 | * \___|\___/|_| \_\_____| |
7 | * |
8 | * Copyright (C) 1998 - 2019, 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.haxx.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 | ***************************************************************************/ |
22 | #include "curlcheck.h" |
23 | |
24 | #include "curl/mprintf.h" |
25 | |
26 | static CURLcode unit_setup(void) {return CURLE_OK;} |
27 | static void unit_stop(void) {} |
28 | |
29 | UNITTEST_START |
30 | |
31 | int rc; |
32 | char buf[3] = {'b', 'u', 'g'}; |
33 | const char *str = "bug" ; |
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); |
41 | fail_unless(rc == 3, "return code should be 3" ); |
42 | fail_unless(!strcmp(output, "bug" ), "wrong output" ); |
43 | |
44 | /* with a trailing zero */ |
45 | rc = curl_msnprintf(output, 4, "%.*s" , width, str); |
46 | fail_unless(rc == 3, "return code should be 3" ); |
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); |
52 | fail_unless(rc == 2, "return code should be 2" ); |
53 | fail_unless(!strcmp(output, "bu" ), "wrong output" ); |
54 | |
55 | /* string with larger precision */ |
56 | rc = curl_msnprintf(output, 8, "%.8s" , str); |
57 | fail_unless(rc == 3, "return code should be 3" ); |
58 | fail_unless(!strcmp(output, "bug" ), "wrong output" ); |
59 | |
60 | /* longer string with precision */ |
61 | rc = curl_msnprintf(output, 8, "%.3s" , "0123456789" ); |
62 | fail_unless(rc == 3, "return code should be 3" ); |
63 | fail_unless(!strcmp(output, "012" ), "wrong output" ); |
64 | |
65 | /* negative width */ |
66 | rc = curl_msnprintf(output, 8, "%-8s" , str); |
67 | fail_unless(rc == 8, "return code should be 8" ); |
68 | fail_unless(!strcmp(output, "bug " ), "wrong output" ); |
69 | |
70 | /* larger width that string length */ |
71 | rc = curl_msnprintf(output, 8, "%8s" , str); |
72 | fail_unless(rc == 8, "return code should be 8" ); |
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); |
77 | fail_unless(rc == 4, "return code should be 4" ); |
78 | fail_unless(!strcmp(output, "102" ), "wrong output" ); |
79 | |
80 | /* padded strings */ |
81 | rc = curl_msnprintf(output, 16, "%8s%8s" , str, str); |
82 | fail_unless(rc == 16, "return code should be 16" ); |
83 | fail_unless(!strcmp(output, " bug bu" ), "wrong output" ); |
84 | |
85 | /* padded numbers */ |
86 | rc = curl_msnprintf(output, 16, "%8d%8d" , 1234, 5678); |
87 | fail_unless(rc == 16, "return code should be 16" ); |
88 | fail_unless(!strcmp(output, " 1234 567" ), "wrong output" ); |
89 | |
90 | UNITTEST_STOP |
91 | |