blob: e73ce24b0cf851b420ba55a27a261c79f4c8e9ab [file] [log] [blame]
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Elliott Hughes34dd5f42021-08-10 13:01:18 -07008 * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Elliott Hughes34dd5f42021-08-10 13:01:18 -070012 * are also available at https://curl.se/docs/copyright.html.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070013 *
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
Alex Deymo8f1a2142016-06-28 14:49:26 -070026static CURLcode unit_setup(void) {return CURLE_OK;}
27static void unit_stop(void) {}
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070028
29UNITTEST_START
30
31int rc;
32char buf[3] = {'b', 'u', 'g'};
Alex Deymo486467e2017-12-19 19:04:07 +010033const char *str = "bug";
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070034int 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);
Alex Deymo8f1a2142016-06-28 14:49:26 -070041fail_unless(rc == 3, "return code should be 3");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070042fail_unless(!strcmp(output, "bug"), "wrong output");
43
44/* with a trailing zero */
45rc = curl_msnprintf(output, 4, "%.*s", width, str);
Alex Deymo8f1a2142016-06-28 14:49:26 -070046fail_unless(rc == 3, "return code should be 3");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070047fail_unless(!strcmp(output, "bug"), "wrong output");
48
49width = 2;
50/* one byte less */
51rc = curl_msnprintf(output, 4, "%.*s", width, buf);
Alex Deymo8f1a2142016-06-28 14:49:26 -070052fail_unless(rc == 2, "return code should be 2");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070053fail_unless(!strcmp(output, "bu"), "wrong output");
54
55/* string with larger precision */
56rc = curl_msnprintf(output, 8, "%.8s", str);
Alex Deymo8f1a2142016-06-28 14:49:26 -070057fail_unless(rc == 3, "return code should be 3");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070058fail_unless(!strcmp(output, "bug"), "wrong output");
59
60/* longer string with precision */
61rc = curl_msnprintf(output, 8, "%.3s", "0123456789");
Alex Deymo8f1a2142016-06-28 14:49:26 -070062fail_unless(rc == 3, "return code should be 3");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070063fail_unless(!strcmp(output, "012"), "wrong output");
64
65/* negative width */
66rc = curl_msnprintf(output, 8, "%-8s", str);
Elliott Hughes34dd5f42021-08-10 13:01:18 -070067fail_unless(rc == 7, "return code should be 7");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070068fail_unless(!strcmp(output, "bug "), "wrong output");
69
70/* larger width that string length */
71rc = curl_msnprintf(output, 8, "%8s", str);
Elliott Hughes34dd5f42021-08-10 13:01:18 -070072fail_unless(rc == 7, "return code should be 7");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070073fail_unless(!strcmp(output, " bu"), "wrong output");
74
75/* output a number in a limited output */
76rc = curl_msnprintf(output, 4, "%d", 10240);
Elliott Hughes34dd5f42021-08-10 13:01:18 -070077fail_unless(rc == 3, "return code should be 3");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070078fail_unless(!strcmp(output, "102"), "wrong output");
79
80/* padded strings */
81rc = curl_msnprintf(output, 16, "%8s%8s", str, str);
Elliott Hughes34dd5f42021-08-10 13:01:18 -070082fail_unless(rc == 15, "return code should be 15");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070083fail_unless(!strcmp(output, " bug bu"), "wrong output");
84
85/* padded numbers */
86rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678);
Elliott Hughes34dd5f42021-08-10 13:01:18 -070087fail_unless(rc == 15, "return code should be 15");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070088fail_unless(!strcmp(output, " 1234 567"), "wrong output");
89
90UNITTEST_STOP