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