1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/sprintf.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,431 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * File: sprintf.c 1.11 + * Description: 1.12 + * This is a test program for the PR_snprintf() functions defined 1.13 + * in prprf.c. This test program is based on ns/nspr/tests/sprintf.c, 1.14 + * revision 1.10. 1.15 + * Modification History: 1.16 + * 20-May-1997 AGarcia replaced printf statment to return PASS\n. This is to be used by the 1.17 + * regress tool parsing routine. 1.18 + ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to 1.19 + * recognize the return code from tha main program. 1.20 + */ 1.21 + 1.22 +#include "prinit.h" 1.23 +#include "prprf.h" 1.24 +#include "prlog.h" 1.25 +#include "prlong.h" 1.26 +#include <string.h> 1.27 +#include <stdio.h> 1.28 +#include <stdlib.h> 1.29 + 1.30 +static char sbuf[20000]; 1.31 + 1.32 + 1.33 +/* 1.34 +** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf. 1.35 +** Make sure the results are identical 1.36 +*/ 1.37 +static void test_i(char *pattern, int i) 1.38 +{ 1.39 + char *s; 1.40 + char buf[200]; 1.41 + int n; 1.42 + 1.43 + /* try all three routines */ 1.44 + s = PR_smprintf(pattern, i); 1.45 + PR_ASSERT(s != 0); 1.46 + n = PR_snprintf(buf, sizeof(buf), pattern, i); 1.47 + PR_ASSERT(n <= sizeof(buf)); 1.48 + sprintf(sbuf, pattern, i); 1.49 + 1.50 + /* compare results */ 1.51 + if ((strncmp(s, buf, sizeof(buf)) != 0) || 1.52 + (strncmp(s, sbuf, sizeof(sbuf)) != 0)) { 1.53 + fprintf(stderr, 1.54 + "pattern='%s' i=%d\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", 1.55 + pattern, i, s, buf, sbuf); 1.56 + PR_smprintf_free(s); 1.57 + exit(-1); 1.58 + } 1.59 + PR_smprintf_free(s); 1.60 +} 1.61 + 1.62 +static void TestI(void) 1.63 +{ 1.64 + static int nums[] = { 1.65 + 0, 1, -1, 10, -10, 1.66 + 32767, -32768, 1.67 + }; 1.68 + static char *signs[] = { 1.69 + "", 1.70 + "0", "-", "+", " ", 1.71 + "0-", "0+", "0 ", "-0", "-+", "- ", 1.72 + "+0", "+-", "+ ", " 0", " -", " +", 1.73 + "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +", 1.74 + "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +", 1.75 + "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -", 1.76 + " 0-", " 0+", " -0", " -+", " +0", " +-", 1.77 + "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-", 1.78 + "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", 1.79 + "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", 1.80 + " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0", 1.81 + }; 1.82 + static char *precs[] = { 1.83 + "", "3", "5", "43", 1.84 + "7.3", "7.5", "7.11", "7.43", 1.85 + }; 1.86 + static char *formats[] = { 1.87 + "d", "o", "x", "u", 1.88 + "hd", "ho", "hx", "hu" 1.89 + }; 1.90 + int f, s, n, p; 1.91 + char fmt[20]; 1.92 + 1.93 + for (f = 0; f < PR_ARRAY_SIZE(formats); f++) { 1.94 + for (s = 0; s < PR_ARRAY_SIZE(signs); s++) { 1.95 + for (p = 0; p < PR_ARRAY_SIZE(precs); p++) { 1.96 + fmt[0] = '%'; 1.97 + fmt[1] = 0; 1.98 + if (signs[s]) strcat(fmt, signs[s]); 1.99 + if (precs[p]) strcat(fmt, precs[p]); 1.100 + if (formats[f]) strcat(fmt, formats[f]); 1.101 + for (n = 0; n < PR_ARRAY_SIZE(nums); n++) { 1.102 + test_i(fmt, nums[n]); 1.103 + } 1.104 + } 1.105 + } 1.106 + } 1.107 +} 1.108 + 1.109 +/************************************************************************/ 1.110 + 1.111 +/* 1.112 +** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf. 1.113 +** Make sure the results are identical 1.114 +*/ 1.115 +static void test_l(char *pattern, char *spattern, PRInt32 l) 1.116 +{ 1.117 + char *s; 1.118 + char buf[200]; 1.119 + int n; 1.120 + 1.121 + /* try all three routines */ 1.122 + s = PR_smprintf(pattern, l); 1.123 + PR_ASSERT(s != 0); 1.124 + n = PR_snprintf(buf, sizeof(buf), pattern, l); 1.125 + PR_ASSERT(n <= sizeof(buf)); 1.126 + sprintf(sbuf, spattern, l); 1.127 + 1.128 + /* compare results */ 1.129 + if ((strncmp(s, buf, sizeof(buf)) != 0) || 1.130 + (strncmp(s, sbuf, sizeof(sbuf)) != 0)) { 1.131 + fprintf(stderr, 1.132 + "pattern='%s' l=%ld\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", 1.133 + pattern, l, s, buf, sbuf); 1.134 + PR_smprintf_free(s); 1.135 + exit(-1); 1.136 + } 1.137 + PR_smprintf_free(s); 1.138 +} 1.139 + 1.140 +static void TestL(void) 1.141 +{ 1.142 + static PRInt32 nums[] = { 1.143 + 0, 1.144 + 1, 1.145 + -1, 1.146 + 10, 1.147 + -10, 1.148 + 32767, 1.149 + -32768, 1.150 + PR_INT32(0x7fffffff), /* 2147483647L */ 1.151 + -1 - PR_INT32(0x7fffffff) /* -2147483648L */ 1.152 + }; 1.153 + static char *signs[] = { 1.154 + "", 1.155 + "0", "-", "+", " ", 1.156 + "0-", "0+", "0 ", "-0", "-+", "- ", 1.157 + "+0", "+-", "+ ", " 0", " -", " +", 1.158 + "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +", 1.159 + "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +", 1.160 + "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -", 1.161 + " 0-", " 0+", " -0", " -+", " +0", " +-", 1.162 + "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-", 1.163 + "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", 1.164 + "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", 1.165 + " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0", 1.166 + }; 1.167 + static char *precs[] = { 1.168 + "", "3", "5", "43", 1.169 + ".3", ".43", 1.170 + "7.3", "7.5", "7.11", "7.43", 1.171 + }; 1.172 + static char *formats[] = { "ld", "lo", "lx", "lu" }; 1.173 + 1.174 +#if PR_BYTES_PER_INT == 4 1.175 + static char *sformats[] = { "d", "o", "x", "u" }; 1.176 +#elif PR_BYTES_PER_LONG == 4 1.177 + static char *sformats[] = { "ld", "lo", "lx", "lu" }; 1.178 +#else 1.179 +#error Neither int nor long is 4 bytes on this platform 1.180 +#endif 1.181 + 1.182 + int f, s, n, p; 1.183 + char fmt[40], sfmt[40]; 1.184 + 1.185 + for (f = 0; f < PR_ARRAY_SIZE(formats); f++) { 1.186 + for (s = 0; s < PR_ARRAY_SIZE(signs); s++) { 1.187 + for (p = 0; p < PR_ARRAY_SIZE(precs); p++) { 1.188 + fmt[0] = '%'; 1.189 + fmt[1] = 0; 1.190 + if (signs[s]) strcat(fmt, signs[s]); 1.191 + if (precs[p]) strcat(fmt, precs[p]); 1.192 + strcpy(sfmt, fmt); 1.193 + if (formats[f]) strcat(fmt, formats[f]); 1.194 + if (sformats[f]) strcat(sfmt, sformats[f]); 1.195 + for (n = 0; n < PR_ARRAY_SIZE(nums); n++) { 1.196 + test_l(fmt, sfmt, nums[n]); 1.197 + } 1.198 + } 1.199 + } 1.200 + } 1.201 +} 1.202 + 1.203 +/************************************************************************/ 1.204 + 1.205 +/* 1.206 +** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf. 1.207 +** Make sure the results are identical 1.208 +*/ 1.209 +static void test_ll(char *pattern, char *spattern, PRInt64 l) 1.210 +{ 1.211 + char *s; 1.212 + char buf[200]; 1.213 + int n; 1.214 + 1.215 + /* try all three routines */ 1.216 + s = PR_smprintf(pattern, l); 1.217 + PR_ASSERT(s != 0); 1.218 + n = PR_snprintf(buf, sizeof(buf), pattern, l); 1.219 + PR_ASSERT(n <= sizeof(buf)); 1.220 +#if defined(HAVE_LONG_LONG) 1.221 + sprintf(sbuf, spattern, l); 1.222 + 1.223 + /* compare results */ 1.224 + if ((strncmp(s, buf, sizeof(buf)) != 0) || 1.225 + (strncmp(s, sbuf, sizeof(sbuf)) != 0)) { 1.226 +#if PR_BYTES_PER_LONG == 8 1.227 +#define FORMAT_SPEC "%ld" 1.228 +#elif defined(WIN16) 1.229 +#define FORMAT_SPEC "%Ld" 1.230 +#elif defined(WIN32) 1.231 +#define FORMAT_SPEC "%I64d" 1.232 +#else 1.233 +#define FORMAT_SPEC "%lld" 1.234 +#endif 1.235 + fprintf(stderr, 1.236 + "pattern='%s' ll=" FORMAT_SPEC "\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", 1.237 + pattern, l, s, buf, sbuf); 1.238 + printf("FAIL\n"); 1.239 + PR_smprintf_free(s); 1.240 + exit(-1); 1.241 + } 1.242 + PR_smprintf_free(s); 1.243 +#else 1.244 + /* compare results */ 1.245 + if ((strncmp(s, buf, sizeof(buf)) != 0)) { 1.246 + fprintf(stderr, 1.247 + "pattern='%s'\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", 1.248 + pattern, s, buf, sbuf); 1.249 + printf("FAIL\n"); 1.250 + PR_smprintf_free(s); 1.251 + exit(-1); 1.252 + } 1.253 + PR_smprintf_free(s); 1.254 +#endif 1.255 +} 1.256 + 1.257 +static void TestLL(void) 1.258 +{ 1.259 + static PRInt64 nums[] = { 1.260 + LL_INIT(0, 0), 1.261 + LL_INIT(0, 1), 1.262 + LL_INIT(0xffffffff, 0xffffffff), /* -1 */ 1.263 + LL_INIT(0, 10), 1.264 + LL_INIT(0xffffffff, 0xfffffff6), /* -10 */ 1.265 + LL_INIT(0, 32767), 1.266 + LL_INIT(0xffffffff, 0xffff8000), /* -32768 */ 1.267 + LL_INIT(0, 0x7fffffff), /* 2147483647 */ 1.268 + LL_INIT(0xffffffff, 0x80000000), /* -2147483648 */ 1.269 + LL_INIT(0x7fffffff, 0xffffffff), /* 9223372036854775807 */ 1.270 + LL_INIT(0x80000000, 0), /* -9223372036854775808 */ 1.271 + PR_INT64(0), 1.272 + PR_INT64(1), 1.273 + PR_INT64(-1), 1.274 + PR_INT64(10), 1.275 + PR_INT64(-10), 1.276 + PR_INT64(32767), 1.277 + PR_INT64(-32768), 1.278 + PR_INT64(2147483647), 1.279 + PR_INT64(-2147483648), 1.280 + PR_INT64(9223372036854775807), 1.281 + PR_INT64(-9223372036854775808) 1.282 + }; 1.283 + 1.284 + static char *signs[] = { 1.285 + "", 1.286 + "0", "-", "+", " ", 1.287 + "0-", "0+", "0 ", "-0", "-+", "- ", 1.288 + "+0", "+-", "+ ", " 0", " -", " +", 1.289 + "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +", 1.290 + "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +", 1.291 + "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -", 1.292 + " 0-", " 0+", " -0", " -+", " +0", " +-", 1.293 + "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-", 1.294 + "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", 1.295 + "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", 1.296 + " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0", 1.297 + }; 1.298 + static char *precs[] = { 1.299 + "", "3", "5", "43", 1.300 + ".3", ".43", 1.301 + "7.3", "7.5", "7.11", "7.43", 1.302 + }; 1.303 + static char *formats[] = { "lld", "llo", "llx", "llu" }; 1.304 + 1.305 +#if PR_BYTES_PER_LONG == 8 1.306 + static char *sformats[] = { "ld", "lo", "lx", "lu" }; 1.307 +#elif defined(WIN16) 1.308 + /* Watcom uses the format string "%Ld" instead of "%lld". */ 1.309 + static char *sformats[] = { "Ld", "Lo", "Lx", "Lu" }; 1.310 +#elif defined(WIN32) 1.311 + static char *sformats[] = { "I64d", "I64o", "I64x", "I64u" }; 1.312 +#else 1.313 + static char *sformats[] = { "lld", "llo", "llx", "llu" }; 1.314 +#endif 1.315 + 1.316 + int f, s, n, p; 1.317 + char fmt[40], sfmt[40]; 1.318 + 1.319 + for (f = 0; f < PR_ARRAY_SIZE(formats); f++) { 1.320 + for (s = 0; s < PR_ARRAY_SIZE(signs); s++) { 1.321 + for (p = 0; p < PR_ARRAY_SIZE(precs); p++) { 1.322 + fmt[0] = '%'; 1.323 + fmt[1] = 0; 1.324 + if (signs[s]) strcat(fmt, signs[s]); 1.325 + if (precs[p]) strcat(fmt, precs[p]); 1.326 + strcpy(sfmt, fmt); 1.327 + if (formats[f]) strcat(fmt, formats[f]); 1.328 + if (sformats[f]) strcat(sfmt, sformats[f]); 1.329 + for (n = 0; n < PR_ARRAY_SIZE(nums); n++) { 1.330 + test_ll(fmt, sfmt, nums[n]); 1.331 + } 1.332 + } 1.333 + } 1.334 + } 1.335 +} 1.336 + 1.337 +/************************************************************************/ 1.338 + 1.339 +/* 1.340 +** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf. 1.341 +** Make sure the results are identical 1.342 +*/ 1.343 +static void test_s(char *pattern, char *ss) 1.344 +{ 1.345 + char *s; 1.346 + unsigned char before[8]; 1.347 + char buf[200]; 1.348 + unsigned char after[8]; 1.349 + int n; 1.350 + 1.351 + memset(before, 0xBB, 8); 1.352 + memset(after, 0xAA, 8); 1.353 + 1.354 + /* try all three routines */ 1.355 + s = PR_smprintf(pattern, ss); 1.356 + PR_ASSERT(s != 0); 1.357 + n = PR_snprintf(buf, sizeof(buf), pattern, ss); 1.358 + PR_ASSERT(n <= sizeof(buf)); 1.359 + sprintf(sbuf, pattern, ss); 1.360 + 1.361 + for (n = 0; n < 8; n++) { 1.362 + PR_ASSERT(before[n] == 0xBB); 1.363 + PR_ASSERT(after[n] == 0xAA); 1.364 + } 1.365 + 1.366 + /* compare results */ 1.367 + if ((strncmp(s, buf, sizeof(buf)) != 0) || 1.368 + (strncmp(s, sbuf, sizeof(sbuf)) != 0)) { 1.369 + fprintf(stderr, 1.370 + "pattern='%s' ss=%.20s\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", 1.371 + pattern, ss, s, buf, sbuf); 1.372 + printf("FAIL\n"); 1.373 + PR_smprintf_free(s); 1.374 + exit(-1); 1.375 + } 1.376 + PR_smprintf_free(s); 1.377 +} 1.378 + 1.379 +static void TestS(void) 1.380 +{ 1.381 + static char *strs[] = { 1.382 + "", 1.383 + "a", 1.384 + "abc", 1.385 + "abcde", 1.386 + "abcdefABCDEF", 1.387 + "abcdefghijklmnopqrstuvwxyz0123456789!@#$" 1.388 + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$" 1.389 + "abcdefghijklmnopqrstuvwxyz0123456789!@#$", 1.390 + }; 1.391 + /* '0' is not relevant to printing strings */ 1.392 + static char *signs[] = { 1.393 + "", 1.394 + "-", "+", " ", 1.395 + "-+", "- ", "+-", "+ ", " -", " +", 1.396 + "-+ ", "- +", "+- ", "+ -", " -+", " +-", 1.397 + }; 1.398 + static char *precs[] = { 1.399 + "", "3", "5", "43", 1.400 + ".3", ".43", 1.401 + "7.3", "7.5", "7.11", "7.43", 1.402 + }; 1.403 + static char *formats[] = { "s" }; 1.404 + int f, s, n, p; 1.405 + char fmt[40]; 1.406 + 1.407 + for (f = 0; f < PR_ARRAY_SIZE(formats); f++) { 1.408 + for (s = 0; s < PR_ARRAY_SIZE(signs); s++) { 1.409 + for (p = 0; p < PR_ARRAY_SIZE(precs); p++) { 1.410 + fmt[0] = '%'; 1.411 + fmt[1] = 0; 1.412 + if (signs[s]) strcat(fmt+strlen(fmt), signs[s]); 1.413 + if (precs[p]) strcat(fmt+strlen(fmt), precs[p]); 1.414 + if (formats[f]) strcat(fmt+strlen(fmt), formats[f]); 1.415 + for (n = 0; n < PR_ARRAY_SIZE(strs); n++) { 1.416 + test_s(fmt, strs[n]); 1.417 + } 1.418 + } 1.419 + } 1.420 + } 1.421 +} 1.422 + 1.423 +/************************************************************************/ 1.424 + 1.425 +int main(int argc, char **argv) 1.426 +{ 1.427 + PR_STDIO_INIT(); 1.428 + TestI(); 1.429 + TestL(); 1.430 + TestLL(); 1.431 + TestS(); 1.432 + printf("PASS\n"); 1.433 + return 0; 1.434 +}