unit1398.c (8373B)
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 #include "unitcheck.h" 25 26 #if defined(CURL_GNUC_DIAG) || defined(__clang__) 27 #pragma GCC diagnostic push 28 #pragma GCC diagnostic ignored "-Wformat" 29 #endif 30 31 static CURLcode test_unit1398(char *arg) 32 { 33 UNITTEST_BEGIN_SIMPLE 34 35 int rc; 36 char buf[3] = {'b', 'u', 'g'}; 37 static const char *str = "bug"; 38 int width = 3; 39 char output[130]; 40 41 /*#define curl_msnprintf snprintf */ 42 43 /* without a trailing zero */ 44 rc = curl_msnprintf(output, 4, "%.*s", width, buf); 45 fail_unless(rc == 3, "return code should be 3"); 46 fail_unless(!strcmp(output, "bug"), "wrong output"); 47 48 /* with a trailing zero */ 49 rc = curl_msnprintf(output, 4, "%.*s", width, str); 50 fail_unless(rc == 3, "return code should be 3"); 51 fail_unless(!strcmp(output, "bug"), "wrong output"); 52 53 width = 2; 54 /* one byte less */ 55 rc = curl_msnprintf(output, 4, "%.*s", width, buf); 56 fail_unless(rc == 2, "return code should be 2"); 57 fail_unless(!strcmp(output, "bu"), "wrong output"); 58 59 /* string with larger precision */ 60 rc = curl_msnprintf(output, 8, "%.8s", str); 61 fail_unless(rc == 3, "return code should be 3"); 62 fail_unless(!strcmp(output, "bug"), "wrong output"); 63 64 /* longer string with precision */ 65 rc = curl_msnprintf(output, 8, "%.3s", "0123456789"); 66 fail_unless(rc == 3, "return code should be 3"); 67 fail_unless(!strcmp(output, "012"), "wrong output"); 68 69 /* negative width */ 70 rc = curl_msnprintf(output, 8, "%-8s", str); 71 fail_unless(rc == 7, "return code should be 7"); 72 fail_unless(!strcmp(output, "bug "), "wrong output"); 73 74 /* larger width that string length */ 75 rc = curl_msnprintf(output, 8, "%8s", str); 76 fail_unless(rc == 7, "return code should be 7"); 77 fail_unless(!strcmp(output, " bu"), "wrong output"); 78 79 /* output a number in a limited output */ 80 rc = curl_msnprintf(output, 4, "%d", 10240); 81 fail_unless(rc == 3, "return code should be 3"); 82 fail_unless(!strcmp(output, "102"), "wrong output"); 83 84 /* padded strings */ 85 rc = curl_msnprintf(output, 16, "%8s%8s", str, str); 86 fail_unless(rc == 15, "return code should be 15"); 87 fail_unless(!strcmp(output, " bug bu"), "wrong output"); 88 89 /* padded numbers */ 90 rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678); 91 fail_unless(rc == 15, "return code should be 15"); 92 fail_unless(!strcmp(output, " 1234 567"), "wrong output"); 93 94 /* double precision */ 95 rc = curl_msnprintf(output, 24, "%2$.*1$.99d", 3, 5678); 96 fail_unless(rc == 0, "return code should be 0"); 97 98 /* 129 input % flags */ 99 rc = curl_msnprintf(output, 130, 100 "%s%s%s%s%s%s%s%s%s%s" /* 10 */ 101 "%s%s%s%s%s%s%s%s%s%s" /* 20 */ 102 "%s%s%s%s%s%s%s%s%s%s" /* 30 */ 103 "%s%s%s%s%s%s%s%s%s%s" /* 40 */ 104 "%s%s%s%s%s%s%s%s%s%s" /* 50 */ 105 "%s%s%s%s%s%s%s%s%s%s" /* 60 */ 106 "%s%s%s%s%s%s%s%s%s%s" /* 70 */ 107 "%s%s%s%s%s%s%s%s%s%s" /* 80 */ 108 "%s%s%s%s%s%s%s%s%s%s" /* 90 */ 109 "%s%s%s%s%s%s%s%s%s%s" /* 100 */ 110 "%s%s%s%s%s%s%s%s%s%s" /* 110 */ 111 "%s%s%s%s%s%s%s%s%s%s" /* 120 */ 112 "%s%s%s%s%s%s%s%s%s", /* 129 */ 113 114 "a", "", "", "", "", "", "", "", "", "", /* 10 */ 115 "b", "", "", "", "", "", "", "", "", "", /* 20 */ 116 "c", "", "", "", "", "", "", "", "", "", /* 30 */ 117 "d", "", "", "", "", "", "", "", "", "", /* 40 */ 118 "e", "", "", "", "", "", "", "", "", "", /* 50 */ 119 "f", "", "", "", "", "", "", "", "", "", /* 60 */ 120 "g", "", "", "", "", "", "", "", "", "", /* 70 */ 121 "h", "", "", "", "", "", "", "", "", "", /* 80 */ 122 "i", "", "", "", "", "", "", "", "", "", /* 90 */ 123 "j", "", "", "", "", "", "", "", "", "", /* 100 */ 124 "k", "", "", "", "", "", "", "", "", "", /* 110 */ 125 "l", "", "", "", "", "", "", "", "", "", /* 120 */ 126 "m", "", "", "", "", "", "", "", "" /* 129 */ 127 ); 128 fail_unless(rc == 0, "return code should be 0"); 129 130 /* 128 input % flags */ 131 rc = curl_msnprintf(output, 130, 132 "%s%s%s%s%s%s%s%s%s%s" /* 10 */ 133 "%s%s%s%s%s%s%s%s%s%s" /* 20 */ 134 "%s%s%s%s%s%s%s%s%s%s" /* 30 */ 135 "%s%s%s%s%s%s%s%s%s%s" /* 40 */ 136 "%s%s%s%s%s%s%s%s%s%s" /* 50 */ 137 "%s%s%s%s%s%s%s%s%s%s" /* 60 */ 138 "%s%s%s%s%s%s%s%s%s%s" /* 70 */ 139 "%s%s%s%s%s%s%s%s%s%s" /* 80 */ 140 "%s%s%s%s%s%s%s%s%s%s" /* 90 */ 141 "%s%s%s%s%s%s%s%s%s%s" /* 100 */ 142 "%s%s%s%s%s%s%s%s%s%s" /* 110 */ 143 "%s%s%s%s%s%s%s%s%s%s" /* 120 */ 144 "%s%s%s%s%s%s%s%s", /* 128 */ 145 146 "a", "", "", "", "", "", "", "", "", "", /* 10 */ 147 "b", "", "", "", "", "", "", "", "", "", /* 20 */ 148 "c", "", "", "", "", "", "", "", "", "", /* 30 */ 149 "d", "", "", "", "", "", "", "", "", "", /* 40 */ 150 "e", "", "", "", "", "", "", "", "", "", /* 50 */ 151 "f", "", "", "", "", "", "", "", "", "", /* 60 */ 152 "g", "", "", "", "", "", "", "", "", "", /* 70 */ 153 "h", "", "", "", "", "", "", "", "", "", /* 80 */ 154 "i", "", "", "", "", "", "", "", "", "", /* 90 */ 155 "j", "", "", "", "", "", "", "", "", "", /* 100 */ 156 "k", "", "", "", "", "", "", "", "", "", /* 110 */ 157 "l", "", "", "", "", "", "", "", "", "", /* 120 */ 158 "m", "", "", "", "", "", "", "" /* 128 */ 159 ); 160 fail_unless(rc == 13, "return code should be 13"); 161 162 /* 129 output segments */ 163 rc = curl_msnprintf(output, 130, 164 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 20 */ 165 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 40 */ 166 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 60 */ 167 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 80 */ 168 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 100 */ 169 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 120 */ 170 "%%%%%%%%%%%%%%%%%%" /* 129 */ 171 ); 172 fail_unless(rc == 0, "return code should be 0"); 173 174 /* 128 output segments */ 175 rc = curl_msnprintf(output, 129, 176 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 20 */ 177 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 40 */ 178 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 60 */ 179 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 80 */ 180 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 100 */ 181 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 120 */ 182 "%%%%%%%%%%%%%%%%" /* 128 */ 183 ); 184 fail_unless(rc == 128, "return code should be 128"); 185 186 UNITTEST_END_SIMPLE 187 } 188 189 #if defined(CURL_GNUC_DIAG) || defined(__clang__) 190 #pragma GCC diagnostic pop 191 #endif