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