2014-05-05 05:37:24 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2014-05-05 05:37:24 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2014-05-05 05:37:24 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2014-05-05 05:37:24 +08:00
|
|
|
***************************************************************************/
|
2023-08-07 04:29:40 +08:00
|
|
|
#define CURL_NO_FMT_CHECKS
|
|
|
|
|
2014-05-05 05:37:24 +08:00
|
|
|
#include "curlcheck.h"
|
|
|
|
|
|
|
|
#include "curl/mprintf.h"
|
|
|
|
|
2016-04-04 02:28:34 +08:00
|
|
|
static CURLcode unit_setup(void) {return CURLE_OK;}
|
|
|
|
static void unit_stop(void) {}
|
2014-05-05 05:37:24 +08:00
|
|
|
|
|
|
|
UNITTEST_START
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
char buf[3] = {'b', 'u', 'g'};
|
2017-09-10 05:09:06 +08:00
|
|
|
const char *str = "bug";
|
2014-05-05 05:37:24 +08:00
|
|
|
int width = 3;
|
|
|
|
char output[24];
|
|
|
|
|
|
|
|
/*#define curl_msnprintf snprintf */
|
|
|
|
|
|
|
|
/* without a trailing zero */
|
|
|
|
rc = curl_msnprintf(output, 4, "%.*s", width, buf);
|
2016-04-04 02:28:34 +08:00
|
|
|
fail_unless(rc == 3, "return code should be 3");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, "bug"), "wrong output");
|
|
|
|
|
|
|
|
/* with a trailing zero */
|
|
|
|
rc = curl_msnprintf(output, 4, "%.*s", width, str);
|
2016-04-04 02:28:34 +08:00
|
|
|
fail_unless(rc == 3, "return code should be 3");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, "bug"), "wrong output");
|
|
|
|
|
|
|
|
width = 2;
|
|
|
|
/* one byte less */
|
|
|
|
rc = curl_msnprintf(output, 4, "%.*s", width, buf);
|
2016-04-04 02:28:34 +08:00
|
|
|
fail_unless(rc == 2, "return code should be 2");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, "bu"), "wrong output");
|
|
|
|
|
|
|
|
/* string with larger precision */
|
|
|
|
rc = curl_msnprintf(output, 8, "%.8s", str);
|
2016-04-04 02:28:34 +08:00
|
|
|
fail_unless(rc == 3, "return code should be 3");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, "bug"), "wrong output");
|
|
|
|
|
|
|
|
/* longer string with precision */
|
|
|
|
rc = curl_msnprintf(output, 8, "%.3s", "0123456789");
|
2016-04-04 02:28:34 +08:00
|
|
|
fail_unless(rc == 3, "return code should be 3");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, "012"), "wrong output");
|
|
|
|
|
|
|
|
/* negative width */
|
|
|
|
rc = curl_msnprintf(output, 8, "%-8s", str);
|
2021-07-07 20:51:17 +08:00
|
|
|
fail_unless(rc == 7, "return code should be 7");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, "bug "), "wrong output");
|
|
|
|
|
|
|
|
/* larger width that string length */
|
|
|
|
rc = curl_msnprintf(output, 8, "%8s", str);
|
2021-07-07 20:51:17 +08:00
|
|
|
fail_unless(rc == 7, "return code should be 7");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, " bu"), "wrong output");
|
|
|
|
|
|
|
|
/* output a number in a limited output */
|
|
|
|
rc = curl_msnprintf(output, 4, "%d", 10240);
|
2021-07-07 20:51:17 +08:00
|
|
|
fail_unless(rc == 3, "return code should be 3");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, "102"), "wrong output");
|
|
|
|
|
|
|
|
/* padded strings */
|
|
|
|
rc = curl_msnprintf(output, 16, "%8s%8s", str, str);
|
2021-07-07 20:51:17 +08:00
|
|
|
fail_unless(rc == 15, "return code should be 15");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, " bug bu"), "wrong output");
|
|
|
|
|
|
|
|
/* padded numbers */
|
|
|
|
rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678);
|
2021-07-07 20:51:17 +08:00
|
|
|
fail_unless(rc == 15, "return code should be 15");
|
2014-05-05 05:37:24 +08:00
|
|
|
fail_unless(!strcmp(output, " 1234 567"), "wrong output");
|
|
|
|
|
2022-10-17 23:56:26 +08:00
|
|
|
/* double precision */
|
|
|
|
rc = curl_msnprintf(output, 24, "%.*1$.99d", 3, 5678);
|
|
|
|
fail_unless(rc == 0, "return code should be 0");
|
|
|
|
|
2014-05-05 05:37:24 +08:00
|
|
|
UNITTEST_STOP
|