nsprpub/pr/tests/sprintf.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * File: sprintf.c
michael@0 8 * Description:
michael@0 9 * This is a test program for the PR_snprintf() functions defined
michael@0 10 * in prprf.c. This test program is based on ns/nspr/tests/sprintf.c,
michael@0 11 * revision 1.10.
michael@0 12 * Modification History:
michael@0 13 * 20-May-1997 AGarcia replaced printf statment to return PASS\n. This is to be used by the
michael@0 14 * regress tool parsing routine.
michael@0 15 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
michael@0 16 * recognize the return code from tha main program.
michael@0 17 */
michael@0 18
michael@0 19 #include "prinit.h"
michael@0 20 #include "prprf.h"
michael@0 21 #include "prlog.h"
michael@0 22 #include "prlong.h"
michael@0 23 #include <string.h>
michael@0 24 #include <stdio.h>
michael@0 25 #include <stdlib.h>
michael@0 26
michael@0 27 static char sbuf[20000];
michael@0 28
michael@0 29
michael@0 30 /*
michael@0 31 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
michael@0 32 ** Make sure the results are identical
michael@0 33 */
michael@0 34 static void test_i(char *pattern, int i)
michael@0 35 {
michael@0 36 char *s;
michael@0 37 char buf[200];
michael@0 38 int n;
michael@0 39
michael@0 40 /* try all three routines */
michael@0 41 s = PR_smprintf(pattern, i);
michael@0 42 PR_ASSERT(s != 0);
michael@0 43 n = PR_snprintf(buf, sizeof(buf), pattern, i);
michael@0 44 PR_ASSERT(n <= sizeof(buf));
michael@0 45 sprintf(sbuf, pattern, i);
michael@0 46
michael@0 47 /* compare results */
michael@0 48 if ((strncmp(s, buf, sizeof(buf)) != 0) ||
michael@0 49 (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
michael@0 50 fprintf(stderr,
michael@0 51 "pattern='%s' i=%d\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
michael@0 52 pattern, i, s, buf, sbuf);
michael@0 53 PR_smprintf_free(s);
michael@0 54 exit(-1);
michael@0 55 }
michael@0 56 PR_smprintf_free(s);
michael@0 57 }
michael@0 58
michael@0 59 static void TestI(void)
michael@0 60 {
michael@0 61 static int nums[] = {
michael@0 62 0, 1, -1, 10, -10,
michael@0 63 32767, -32768,
michael@0 64 };
michael@0 65 static char *signs[] = {
michael@0 66 "",
michael@0 67 "0", "-", "+", " ",
michael@0 68 "0-", "0+", "0 ", "-0", "-+", "- ",
michael@0 69 "+0", "+-", "+ ", " 0", " -", " +",
michael@0 70 "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
michael@0 71 "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
michael@0 72 "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
michael@0 73 " 0-", " 0+", " -0", " -+", " +0", " +-",
michael@0 74 "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
michael@0 75 "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
michael@0 76 "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
michael@0 77 " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
michael@0 78 };
michael@0 79 static char *precs[] = {
michael@0 80 "", "3", "5", "43",
michael@0 81 "7.3", "7.5", "7.11", "7.43",
michael@0 82 };
michael@0 83 static char *formats[] = {
michael@0 84 "d", "o", "x", "u",
michael@0 85 "hd", "ho", "hx", "hu"
michael@0 86 };
michael@0 87 int f, s, n, p;
michael@0 88 char fmt[20];
michael@0 89
michael@0 90 for (f = 0; f < PR_ARRAY_SIZE(formats); f++) {
michael@0 91 for (s = 0; s < PR_ARRAY_SIZE(signs); s++) {
michael@0 92 for (p = 0; p < PR_ARRAY_SIZE(precs); p++) {
michael@0 93 fmt[0] = '%';
michael@0 94 fmt[1] = 0;
michael@0 95 if (signs[s]) strcat(fmt, signs[s]);
michael@0 96 if (precs[p]) strcat(fmt, precs[p]);
michael@0 97 if (formats[f]) strcat(fmt, formats[f]);
michael@0 98 for (n = 0; n < PR_ARRAY_SIZE(nums); n++) {
michael@0 99 test_i(fmt, nums[n]);
michael@0 100 }
michael@0 101 }
michael@0 102 }
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 /************************************************************************/
michael@0 107
michael@0 108 /*
michael@0 109 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
michael@0 110 ** Make sure the results are identical
michael@0 111 */
michael@0 112 static void test_l(char *pattern, char *spattern, PRInt32 l)
michael@0 113 {
michael@0 114 char *s;
michael@0 115 char buf[200];
michael@0 116 int n;
michael@0 117
michael@0 118 /* try all three routines */
michael@0 119 s = PR_smprintf(pattern, l);
michael@0 120 PR_ASSERT(s != 0);
michael@0 121 n = PR_snprintf(buf, sizeof(buf), pattern, l);
michael@0 122 PR_ASSERT(n <= sizeof(buf));
michael@0 123 sprintf(sbuf, spattern, l);
michael@0 124
michael@0 125 /* compare results */
michael@0 126 if ((strncmp(s, buf, sizeof(buf)) != 0) ||
michael@0 127 (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
michael@0 128 fprintf(stderr,
michael@0 129 "pattern='%s' l=%ld\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
michael@0 130 pattern, l, s, buf, sbuf);
michael@0 131 PR_smprintf_free(s);
michael@0 132 exit(-1);
michael@0 133 }
michael@0 134 PR_smprintf_free(s);
michael@0 135 }
michael@0 136
michael@0 137 static void TestL(void)
michael@0 138 {
michael@0 139 static PRInt32 nums[] = {
michael@0 140 0,
michael@0 141 1,
michael@0 142 -1,
michael@0 143 10,
michael@0 144 -10,
michael@0 145 32767,
michael@0 146 -32768,
michael@0 147 PR_INT32(0x7fffffff), /* 2147483647L */
michael@0 148 -1 - PR_INT32(0x7fffffff) /* -2147483648L */
michael@0 149 };
michael@0 150 static char *signs[] = {
michael@0 151 "",
michael@0 152 "0", "-", "+", " ",
michael@0 153 "0-", "0+", "0 ", "-0", "-+", "- ",
michael@0 154 "+0", "+-", "+ ", " 0", " -", " +",
michael@0 155 "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
michael@0 156 "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
michael@0 157 "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
michael@0 158 " 0-", " 0+", " -0", " -+", " +0", " +-",
michael@0 159 "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
michael@0 160 "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
michael@0 161 "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
michael@0 162 " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
michael@0 163 };
michael@0 164 static char *precs[] = {
michael@0 165 "", "3", "5", "43",
michael@0 166 ".3", ".43",
michael@0 167 "7.3", "7.5", "7.11", "7.43",
michael@0 168 };
michael@0 169 static char *formats[] = { "ld", "lo", "lx", "lu" };
michael@0 170
michael@0 171 #if PR_BYTES_PER_INT == 4
michael@0 172 static char *sformats[] = { "d", "o", "x", "u" };
michael@0 173 #elif PR_BYTES_PER_LONG == 4
michael@0 174 static char *sformats[] = { "ld", "lo", "lx", "lu" };
michael@0 175 #else
michael@0 176 #error Neither int nor long is 4 bytes on this platform
michael@0 177 #endif
michael@0 178
michael@0 179 int f, s, n, p;
michael@0 180 char fmt[40], sfmt[40];
michael@0 181
michael@0 182 for (f = 0; f < PR_ARRAY_SIZE(formats); f++) {
michael@0 183 for (s = 0; s < PR_ARRAY_SIZE(signs); s++) {
michael@0 184 for (p = 0; p < PR_ARRAY_SIZE(precs); p++) {
michael@0 185 fmt[0] = '%';
michael@0 186 fmt[1] = 0;
michael@0 187 if (signs[s]) strcat(fmt, signs[s]);
michael@0 188 if (precs[p]) strcat(fmt, precs[p]);
michael@0 189 strcpy(sfmt, fmt);
michael@0 190 if (formats[f]) strcat(fmt, formats[f]);
michael@0 191 if (sformats[f]) strcat(sfmt, sformats[f]);
michael@0 192 for (n = 0; n < PR_ARRAY_SIZE(nums); n++) {
michael@0 193 test_l(fmt, sfmt, nums[n]);
michael@0 194 }
michael@0 195 }
michael@0 196 }
michael@0 197 }
michael@0 198 }
michael@0 199
michael@0 200 /************************************************************************/
michael@0 201
michael@0 202 /*
michael@0 203 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
michael@0 204 ** Make sure the results are identical
michael@0 205 */
michael@0 206 static void test_ll(char *pattern, char *spattern, PRInt64 l)
michael@0 207 {
michael@0 208 char *s;
michael@0 209 char buf[200];
michael@0 210 int n;
michael@0 211
michael@0 212 /* try all three routines */
michael@0 213 s = PR_smprintf(pattern, l);
michael@0 214 PR_ASSERT(s != 0);
michael@0 215 n = PR_snprintf(buf, sizeof(buf), pattern, l);
michael@0 216 PR_ASSERT(n <= sizeof(buf));
michael@0 217 #if defined(HAVE_LONG_LONG)
michael@0 218 sprintf(sbuf, spattern, l);
michael@0 219
michael@0 220 /* compare results */
michael@0 221 if ((strncmp(s, buf, sizeof(buf)) != 0) ||
michael@0 222 (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
michael@0 223 #if PR_BYTES_PER_LONG == 8
michael@0 224 #define FORMAT_SPEC "%ld"
michael@0 225 #elif defined(WIN16)
michael@0 226 #define FORMAT_SPEC "%Ld"
michael@0 227 #elif defined(WIN32)
michael@0 228 #define FORMAT_SPEC "%I64d"
michael@0 229 #else
michael@0 230 #define FORMAT_SPEC "%lld"
michael@0 231 #endif
michael@0 232 fprintf(stderr,
michael@0 233 "pattern='%s' ll=" FORMAT_SPEC "\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
michael@0 234 pattern, l, s, buf, sbuf);
michael@0 235 printf("FAIL\n");
michael@0 236 PR_smprintf_free(s);
michael@0 237 exit(-1);
michael@0 238 }
michael@0 239 PR_smprintf_free(s);
michael@0 240 #else
michael@0 241 /* compare results */
michael@0 242 if ((strncmp(s, buf, sizeof(buf)) != 0)) {
michael@0 243 fprintf(stderr,
michael@0 244 "pattern='%s'\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
michael@0 245 pattern, s, buf, sbuf);
michael@0 246 printf("FAIL\n");
michael@0 247 PR_smprintf_free(s);
michael@0 248 exit(-1);
michael@0 249 }
michael@0 250 PR_smprintf_free(s);
michael@0 251 #endif
michael@0 252 }
michael@0 253
michael@0 254 static void TestLL(void)
michael@0 255 {
michael@0 256 static PRInt64 nums[] = {
michael@0 257 LL_INIT(0, 0),
michael@0 258 LL_INIT(0, 1),
michael@0 259 LL_INIT(0xffffffff, 0xffffffff), /* -1 */
michael@0 260 LL_INIT(0, 10),
michael@0 261 LL_INIT(0xffffffff, 0xfffffff6), /* -10 */
michael@0 262 LL_INIT(0, 32767),
michael@0 263 LL_INIT(0xffffffff, 0xffff8000), /* -32768 */
michael@0 264 LL_INIT(0, 0x7fffffff), /* 2147483647 */
michael@0 265 LL_INIT(0xffffffff, 0x80000000), /* -2147483648 */
michael@0 266 LL_INIT(0x7fffffff, 0xffffffff), /* 9223372036854775807 */
michael@0 267 LL_INIT(0x80000000, 0), /* -9223372036854775808 */
michael@0 268 PR_INT64(0),
michael@0 269 PR_INT64(1),
michael@0 270 PR_INT64(-1),
michael@0 271 PR_INT64(10),
michael@0 272 PR_INT64(-10),
michael@0 273 PR_INT64(32767),
michael@0 274 PR_INT64(-32768),
michael@0 275 PR_INT64(2147483647),
michael@0 276 PR_INT64(-2147483648),
michael@0 277 PR_INT64(9223372036854775807),
michael@0 278 PR_INT64(-9223372036854775808)
michael@0 279 };
michael@0 280
michael@0 281 static char *signs[] = {
michael@0 282 "",
michael@0 283 "0", "-", "+", " ",
michael@0 284 "0-", "0+", "0 ", "-0", "-+", "- ",
michael@0 285 "+0", "+-", "+ ", " 0", " -", " +",
michael@0 286 "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
michael@0 287 "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
michael@0 288 "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
michael@0 289 " 0-", " 0+", " -0", " -+", " +0", " +-",
michael@0 290 "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
michael@0 291 "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
michael@0 292 "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
michael@0 293 " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
michael@0 294 };
michael@0 295 static char *precs[] = {
michael@0 296 "", "3", "5", "43",
michael@0 297 ".3", ".43",
michael@0 298 "7.3", "7.5", "7.11", "7.43",
michael@0 299 };
michael@0 300 static char *formats[] = { "lld", "llo", "llx", "llu" };
michael@0 301
michael@0 302 #if PR_BYTES_PER_LONG == 8
michael@0 303 static char *sformats[] = { "ld", "lo", "lx", "lu" };
michael@0 304 #elif defined(WIN16)
michael@0 305 /* Watcom uses the format string "%Ld" instead of "%lld". */
michael@0 306 static char *sformats[] = { "Ld", "Lo", "Lx", "Lu" };
michael@0 307 #elif defined(WIN32)
michael@0 308 static char *sformats[] = { "I64d", "I64o", "I64x", "I64u" };
michael@0 309 #else
michael@0 310 static char *sformats[] = { "lld", "llo", "llx", "llu" };
michael@0 311 #endif
michael@0 312
michael@0 313 int f, s, n, p;
michael@0 314 char fmt[40], sfmt[40];
michael@0 315
michael@0 316 for (f = 0; f < PR_ARRAY_SIZE(formats); f++) {
michael@0 317 for (s = 0; s < PR_ARRAY_SIZE(signs); s++) {
michael@0 318 for (p = 0; p < PR_ARRAY_SIZE(precs); p++) {
michael@0 319 fmt[0] = '%';
michael@0 320 fmt[1] = 0;
michael@0 321 if (signs[s]) strcat(fmt, signs[s]);
michael@0 322 if (precs[p]) strcat(fmt, precs[p]);
michael@0 323 strcpy(sfmt, fmt);
michael@0 324 if (formats[f]) strcat(fmt, formats[f]);
michael@0 325 if (sformats[f]) strcat(sfmt, sformats[f]);
michael@0 326 for (n = 0; n < PR_ARRAY_SIZE(nums); n++) {
michael@0 327 test_ll(fmt, sfmt, nums[n]);
michael@0 328 }
michael@0 329 }
michael@0 330 }
michael@0 331 }
michael@0 332 }
michael@0 333
michael@0 334 /************************************************************************/
michael@0 335
michael@0 336 /*
michael@0 337 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
michael@0 338 ** Make sure the results are identical
michael@0 339 */
michael@0 340 static void test_s(char *pattern, char *ss)
michael@0 341 {
michael@0 342 char *s;
michael@0 343 unsigned char before[8];
michael@0 344 char buf[200];
michael@0 345 unsigned char after[8];
michael@0 346 int n;
michael@0 347
michael@0 348 memset(before, 0xBB, 8);
michael@0 349 memset(after, 0xAA, 8);
michael@0 350
michael@0 351 /* try all three routines */
michael@0 352 s = PR_smprintf(pattern, ss);
michael@0 353 PR_ASSERT(s != 0);
michael@0 354 n = PR_snprintf(buf, sizeof(buf), pattern, ss);
michael@0 355 PR_ASSERT(n <= sizeof(buf));
michael@0 356 sprintf(sbuf, pattern, ss);
michael@0 357
michael@0 358 for (n = 0; n < 8; n++) {
michael@0 359 PR_ASSERT(before[n] == 0xBB);
michael@0 360 PR_ASSERT(after[n] == 0xAA);
michael@0 361 }
michael@0 362
michael@0 363 /* compare results */
michael@0 364 if ((strncmp(s, buf, sizeof(buf)) != 0) ||
michael@0 365 (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
michael@0 366 fprintf(stderr,
michael@0 367 "pattern='%s' ss=%.20s\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
michael@0 368 pattern, ss, s, buf, sbuf);
michael@0 369 printf("FAIL\n");
michael@0 370 PR_smprintf_free(s);
michael@0 371 exit(-1);
michael@0 372 }
michael@0 373 PR_smprintf_free(s);
michael@0 374 }
michael@0 375
michael@0 376 static void TestS(void)
michael@0 377 {
michael@0 378 static char *strs[] = {
michael@0 379 "",
michael@0 380 "a",
michael@0 381 "abc",
michael@0 382 "abcde",
michael@0 383 "abcdefABCDEF",
michael@0 384 "abcdefghijklmnopqrstuvwxyz0123456789!@#$"
michael@0 385 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$"
michael@0 386 "abcdefghijklmnopqrstuvwxyz0123456789!@#$",
michael@0 387 };
michael@0 388 /* '0' is not relevant to printing strings */
michael@0 389 static char *signs[] = {
michael@0 390 "",
michael@0 391 "-", "+", " ",
michael@0 392 "-+", "- ", "+-", "+ ", " -", " +",
michael@0 393 "-+ ", "- +", "+- ", "+ -", " -+", " +-",
michael@0 394 };
michael@0 395 static char *precs[] = {
michael@0 396 "", "3", "5", "43",
michael@0 397 ".3", ".43",
michael@0 398 "7.3", "7.5", "7.11", "7.43",
michael@0 399 };
michael@0 400 static char *formats[] = { "s" };
michael@0 401 int f, s, n, p;
michael@0 402 char fmt[40];
michael@0 403
michael@0 404 for (f = 0; f < PR_ARRAY_SIZE(formats); f++) {
michael@0 405 for (s = 0; s < PR_ARRAY_SIZE(signs); s++) {
michael@0 406 for (p = 0; p < PR_ARRAY_SIZE(precs); p++) {
michael@0 407 fmt[0] = '%';
michael@0 408 fmt[1] = 0;
michael@0 409 if (signs[s]) strcat(fmt+strlen(fmt), signs[s]);
michael@0 410 if (precs[p]) strcat(fmt+strlen(fmt), precs[p]);
michael@0 411 if (formats[f]) strcat(fmt+strlen(fmt), formats[f]);
michael@0 412 for (n = 0; n < PR_ARRAY_SIZE(strs); n++) {
michael@0 413 test_s(fmt, strs[n]);
michael@0 414 }
michael@0 415 }
michael@0 416 }
michael@0 417 }
michael@0 418 }
michael@0 419
michael@0 420 /************************************************************************/
michael@0 421
michael@0 422 int main(int argc, char **argv)
michael@0 423 {
michael@0 424 PR_STDIO_INIT();
michael@0 425 TestI();
michael@0 426 TestL();
michael@0 427 TestLL();
michael@0 428 TestS();
michael@0 429 printf("PASS\n");
michael@0 430 return 0;
michael@0 431 }

mercurial