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