1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "curlcheck.h"
24 #include "curl/mprintf.h"
26 static CURLcode unit_setup( void ) {return CURLE_OK;}
27 static void unit_stop( void ) {}
32 char buf[3] = {'b', 'u', 'g'};
33 const char *str="bug";
37 /*#define curl_msnprintf snprintf */
39 /* without a trailing zero */
40 rc = curl_msnprintf(output, 4, "%.*s", width, buf);
41 fail_unless( rc == 3 , "return code should be 3" );
42 fail_unless(!strcmp(output, "bug"), "wrong output");
44 /* with a trailing zero */
45 rc = curl_msnprintf(output, 4, "%.*s", width, str);
46 fail_unless( rc == 3 , "return code should be 3" );
47 fail_unless(!strcmp(output, "bug"), "wrong output");
51 rc = curl_msnprintf(output, 4, "%.*s", width, buf);
52 fail_unless( rc == 2 , "return code should be 2" );
53 fail_unless(!strcmp(output, "bu"), "wrong output");
55 /* string with larger precision */
56 rc = curl_msnprintf(output, 8, "%.8s", str);
57 fail_unless( rc == 3 , "return code should be 3" );
58 fail_unless(!strcmp(output, "bug"), "wrong output");
60 /* longer string with precision */
61 rc = curl_msnprintf(output, 8, "%.3s", "0123456789");
62 fail_unless( rc == 3 , "return code should be 3" );
63 fail_unless(!strcmp(output, "012"), "wrong output");
66 rc = curl_msnprintf(output, 8, "%-8s", str);
67 fail_unless( rc == 8 , "return code should be 8" );
68 fail_unless(!strcmp(output, "bug "), "wrong output");
70 /* larger width that string length */
71 rc = curl_msnprintf(output, 8, "%8s", str);
72 fail_unless( rc == 8 , "return code should be 8" );
73 fail_unless(!strcmp(output, " bu"), "wrong output");
75 /* output a number in a limited output */
76 rc = curl_msnprintf(output, 4, "%d", 10240);
77 /* TODO: this should return 5 to be POSIX/snprintf compliant! */
78 fail_unless( rc == 4 , "return code should be 4" );
79 fail_unless(!strcmp(output, "102"), "wrong output");
82 rc = curl_msnprintf(output, 16, "%8s%8s", str, str);
83 fail_unless( rc == 16 , "return code should be 16" );
84 fail_unless(!strcmp(output, " bug bu"), "wrong output");
87 rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678);
88 fail_unless( rc == 16 , "return code should be 16" );
89 fail_unless(!strcmp(output, " 1234 567"), "wrong output");