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