michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * File: sprintf.c michael@0: * Description: michael@0: * This is a test program for the PR_snprintf() functions defined michael@0: * in prprf.c. This test program is based on ns/nspr/tests/sprintf.c, michael@0: * revision 1.10. michael@0: * Modification History: michael@0: * 20-May-1997 AGarcia replaced printf statment to return PASS\n. This is to be used by the michael@0: * regress tool parsing routine. michael@0: ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to michael@0: * recognize the return code from tha main program. michael@0: */ michael@0: michael@0: #include "prinit.h" michael@0: #include "prprf.h" michael@0: #include "prlog.h" michael@0: #include "prlong.h" michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: static char sbuf[20000]; michael@0: michael@0: michael@0: /* michael@0: ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf. michael@0: ** Make sure the results are identical michael@0: */ michael@0: static void test_i(char *pattern, int i) michael@0: { michael@0: char *s; michael@0: char buf[200]; michael@0: int n; michael@0: michael@0: /* try all three routines */ michael@0: s = PR_smprintf(pattern, i); michael@0: PR_ASSERT(s != 0); michael@0: n = PR_snprintf(buf, sizeof(buf), pattern, i); michael@0: PR_ASSERT(n <= sizeof(buf)); michael@0: sprintf(sbuf, pattern, i); michael@0: michael@0: /* compare results */ michael@0: if ((strncmp(s, buf, sizeof(buf)) != 0) || michael@0: (strncmp(s, sbuf, sizeof(sbuf)) != 0)) { michael@0: fprintf(stderr, michael@0: "pattern='%s' i=%d\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", michael@0: pattern, i, s, buf, sbuf); michael@0: PR_smprintf_free(s); michael@0: exit(-1); michael@0: } michael@0: PR_smprintf_free(s); michael@0: } michael@0: michael@0: static void TestI(void) michael@0: { michael@0: static int nums[] = { michael@0: 0, 1, -1, 10, -10, michael@0: 32767, -32768, michael@0: }; michael@0: static char *signs[] = { michael@0: "", michael@0: "0", "-", "+", " ", michael@0: "0-", "0+", "0 ", "-0", "-+", "- ", michael@0: "+0", "+-", "+ ", " 0", " -", " +", michael@0: "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +", michael@0: "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +", michael@0: "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -", michael@0: " 0-", " 0+", " -0", " -+", " +0", " +-", michael@0: "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-", michael@0: "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", michael@0: "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", michael@0: " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0", michael@0: }; michael@0: static char *precs[] = { michael@0: "", "3", "5", "43", michael@0: "7.3", "7.5", "7.11", "7.43", michael@0: }; michael@0: static char *formats[] = { michael@0: "d", "o", "x", "u", michael@0: "hd", "ho", "hx", "hu" michael@0: }; michael@0: int f, s, n, p; michael@0: char fmt[20]; michael@0: michael@0: for (f = 0; f < PR_ARRAY_SIZE(formats); f++) { michael@0: for (s = 0; s < PR_ARRAY_SIZE(signs); s++) { michael@0: for (p = 0; p < PR_ARRAY_SIZE(precs); p++) { michael@0: fmt[0] = '%'; michael@0: fmt[1] = 0; michael@0: if (signs[s]) strcat(fmt, signs[s]); michael@0: if (precs[p]) strcat(fmt, precs[p]); michael@0: if (formats[f]) strcat(fmt, formats[f]); michael@0: for (n = 0; n < PR_ARRAY_SIZE(nums); n++) { michael@0: test_i(fmt, nums[n]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: /* michael@0: ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf. michael@0: ** Make sure the results are identical michael@0: */ michael@0: static void test_l(char *pattern, char *spattern, PRInt32 l) michael@0: { michael@0: char *s; michael@0: char buf[200]; michael@0: int n; michael@0: michael@0: /* try all three routines */ michael@0: s = PR_smprintf(pattern, l); michael@0: PR_ASSERT(s != 0); michael@0: n = PR_snprintf(buf, sizeof(buf), pattern, l); michael@0: PR_ASSERT(n <= sizeof(buf)); michael@0: sprintf(sbuf, spattern, l); michael@0: michael@0: /* compare results */ michael@0: if ((strncmp(s, buf, sizeof(buf)) != 0) || michael@0: (strncmp(s, sbuf, sizeof(sbuf)) != 0)) { michael@0: fprintf(stderr, michael@0: "pattern='%s' l=%ld\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", michael@0: pattern, l, s, buf, sbuf); michael@0: PR_smprintf_free(s); michael@0: exit(-1); michael@0: } michael@0: PR_smprintf_free(s); michael@0: } michael@0: michael@0: static void TestL(void) michael@0: { michael@0: static PRInt32 nums[] = { michael@0: 0, michael@0: 1, michael@0: -1, michael@0: 10, michael@0: -10, michael@0: 32767, michael@0: -32768, michael@0: PR_INT32(0x7fffffff), /* 2147483647L */ michael@0: -1 - PR_INT32(0x7fffffff) /* -2147483648L */ michael@0: }; michael@0: static char *signs[] = { michael@0: "", michael@0: "0", "-", "+", " ", michael@0: "0-", "0+", "0 ", "-0", "-+", "- ", michael@0: "+0", "+-", "+ ", " 0", " -", " +", michael@0: "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +", michael@0: "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +", michael@0: "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -", michael@0: " 0-", " 0+", " -0", " -+", " +0", " +-", michael@0: "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-", michael@0: "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", michael@0: "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", michael@0: " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0", michael@0: }; michael@0: static char *precs[] = { michael@0: "", "3", "5", "43", michael@0: ".3", ".43", michael@0: "7.3", "7.5", "7.11", "7.43", michael@0: }; michael@0: static char *formats[] = { "ld", "lo", "lx", "lu" }; michael@0: michael@0: #if PR_BYTES_PER_INT == 4 michael@0: static char *sformats[] = { "d", "o", "x", "u" }; michael@0: #elif PR_BYTES_PER_LONG == 4 michael@0: static char *sformats[] = { "ld", "lo", "lx", "lu" }; michael@0: #else michael@0: #error Neither int nor long is 4 bytes on this platform michael@0: #endif michael@0: michael@0: int f, s, n, p; michael@0: char fmt[40], sfmt[40]; michael@0: michael@0: for (f = 0; f < PR_ARRAY_SIZE(formats); f++) { michael@0: for (s = 0; s < PR_ARRAY_SIZE(signs); s++) { michael@0: for (p = 0; p < PR_ARRAY_SIZE(precs); p++) { michael@0: fmt[0] = '%'; michael@0: fmt[1] = 0; michael@0: if (signs[s]) strcat(fmt, signs[s]); michael@0: if (precs[p]) strcat(fmt, precs[p]); michael@0: strcpy(sfmt, fmt); michael@0: if (formats[f]) strcat(fmt, formats[f]); michael@0: if (sformats[f]) strcat(sfmt, sformats[f]); michael@0: for (n = 0; n < PR_ARRAY_SIZE(nums); n++) { michael@0: test_l(fmt, sfmt, nums[n]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: /* michael@0: ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf. michael@0: ** Make sure the results are identical michael@0: */ michael@0: static void test_ll(char *pattern, char *spattern, PRInt64 l) michael@0: { michael@0: char *s; michael@0: char buf[200]; michael@0: int n; michael@0: michael@0: /* try all three routines */ michael@0: s = PR_smprintf(pattern, l); michael@0: PR_ASSERT(s != 0); michael@0: n = PR_snprintf(buf, sizeof(buf), pattern, l); michael@0: PR_ASSERT(n <= sizeof(buf)); michael@0: #if defined(HAVE_LONG_LONG) michael@0: sprintf(sbuf, spattern, l); michael@0: michael@0: /* compare results */ michael@0: if ((strncmp(s, buf, sizeof(buf)) != 0) || michael@0: (strncmp(s, sbuf, sizeof(sbuf)) != 0)) { michael@0: #if PR_BYTES_PER_LONG == 8 michael@0: #define FORMAT_SPEC "%ld" michael@0: #elif defined(WIN16) michael@0: #define FORMAT_SPEC "%Ld" michael@0: #elif defined(WIN32) michael@0: #define FORMAT_SPEC "%I64d" michael@0: #else michael@0: #define FORMAT_SPEC "%lld" michael@0: #endif michael@0: fprintf(stderr, michael@0: "pattern='%s' ll=" FORMAT_SPEC "\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", michael@0: pattern, l, s, buf, sbuf); michael@0: printf("FAIL\n"); michael@0: PR_smprintf_free(s); michael@0: exit(-1); michael@0: } michael@0: PR_smprintf_free(s); michael@0: #else michael@0: /* compare results */ michael@0: if ((strncmp(s, buf, sizeof(buf)) != 0)) { michael@0: fprintf(stderr, michael@0: "pattern='%s'\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", michael@0: pattern, s, buf, sbuf); michael@0: printf("FAIL\n"); michael@0: PR_smprintf_free(s); michael@0: exit(-1); michael@0: } michael@0: PR_smprintf_free(s); michael@0: #endif michael@0: } michael@0: michael@0: static void TestLL(void) michael@0: { michael@0: static PRInt64 nums[] = { michael@0: LL_INIT(0, 0), michael@0: LL_INIT(0, 1), michael@0: LL_INIT(0xffffffff, 0xffffffff), /* -1 */ michael@0: LL_INIT(0, 10), michael@0: LL_INIT(0xffffffff, 0xfffffff6), /* -10 */ michael@0: LL_INIT(0, 32767), michael@0: LL_INIT(0xffffffff, 0xffff8000), /* -32768 */ michael@0: LL_INIT(0, 0x7fffffff), /* 2147483647 */ michael@0: LL_INIT(0xffffffff, 0x80000000), /* -2147483648 */ michael@0: LL_INIT(0x7fffffff, 0xffffffff), /* 9223372036854775807 */ michael@0: LL_INIT(0x80000000, 0), /* -9223372036854775808 */ michael@0: PR_INT64(0), michael@0: PR_INT64(1), michael@0: PR_INT64(-1), michael@0: PR_INT64(10), michael@0: PR_INT64(-10), michael@0: PR_INT64(32767), michael@0: PR_INT64(-32768), michael@0: PR_INT64(2147483647), michael@0: PR_INT64(-2147483648), michael@0: PR_INT64(9223372036854775807), michael@0: PR_INT64(-9223372036854775808) michael@0: }; michael@0: michael@0: static char *signs[] = { michael@0: "", michael@0: "0", "-", "+", " ", michael@0: "0-", "0+", "0 ", "-0", "-+", "- ", michael@0: "+0", "+-", "+ ", " 0", " -", " +", michael@0: "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +", michael@0: "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +", michael@0: "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -", michael@0: " 0-", " 0+", " -0", " -+", " +0", " +-", michael@0: "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-", michael@0: "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0", michael@0: "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0", michael@0: " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0", michael@0: }; michael@0: static char *precs[] = { michael@0: "", "3", "5", "43", michael@0: ".3", ".43", michael@0: "7.3", "7.5", "7.11", "7.43", michael@0: }; michael@0: static char *formats[] = { "lld", "llo", "llx", "llu" }; michael@0: michael@0: #if PR_BYTES_PER_LONG == 8 michael@0: static char *sformats[] = { "ld", "lo", "lx", "lu" }; michael@0: #elif defined(WIN16) michael@0: /* Watcom uses the format string "%Ld" instead of "%lld". */ michael@0: static char *sformats[] = { "Ld", "Lo", "Lx", "Lu" }; michael@0: #elif defined(WIN32) michael@0: static char *sformats[] = { "I64d", "I64o", "I64x", "I64u" }; michael@0: #else michael@0: static char *sformats[] = { "lld", "llo", "llx", "llu" }; michael@0: #endif michael@0: michael@0: int f, s, n, p; michael@0: char fmt[40], sfmt[40]; michael@0: michael@0: for (f = 0; f < PR_ARRAY_SIZE(formats); f++) { michael@0: for (s = 0; s < PR_ARRAY_SIZE(signs); s++) { michael@0: for (p = 0; p < PR_ARRAY_SIZE(precs); p++) { michael@0: fmt[0] = '%'; michael@0: fmt[1] = 0; michael@0: if (signs[s]) strcat(fmt, signs[s]); michael@0: if (precs[p]) strcat(fmt, precs[p]); michael@0: strcpy(sfmt, fmt); michael@0: if (formats[f]) strcat(fmt, formats[f]); michael@0: if (sformats[f]) strcat(sfmt, sformats[f]); michael@0: for (n = 0; n < PR_ARRAY_SIZE(nums); n++) { michael@0: test_ll(fmt, sfmt, nums[n]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: /* michael@0: ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf. michael@0: ** Make sure the results are identical michael@0: */ michael@0: static void test_s(char *pattern, char *ss) michael@0: { michael@0: char *s; michael@0: unsigned char before[8]; michael@0: char buf[200]; michael@0: unsigned char after[8]; michael@0: int n; michael@0: michael@0: memset(before, 0xBB, 8); michael@0: memset(after, 0xAA, 8); michael@0: michael@0: /* try all three routines */ michael@0: s = PR_smprintf(pattern, ss); michael@0: PR_ASSERT(s != 0); michael@0: n = PR_snprintf(buf, sizeof(buf), pattern, ss); michael@0: PR_ASSERT(n <= sizeof(buf)); michael@0: sprintf(sbuf, pattern, ss); michael@0: michael@0: for (n = 0; n < 8; n++) { michael@0: PR_ASSERT(before[n] == 0xBB); michael@0: PR_ASSERT(after[n] == 0xAA); michael@0: } michael@0: michael@0: /* compare results */ michael@0: if ((strncmp(s, buf, sizeof(buf)) != 0) || michael@0: (strncmp(s, sbuf, sizeof(sbuf)) != 0)) { michael@0: fprintf(stderr, michael@0: "pattern='%s' ss=%.20s\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n", michael@0: pattern, ss, s, buf, sbuf); michael@0: printf("FAIL\n"); michael@0: PR_smprintf_free(s); michael@0: exit(-1); michael@0: } michael@0: PR_smprintf_free(s); michael@0: } michael@0: michael@0: static void TestS(void) michael@0: { michael@0: static char *strs[] = { michael@0: "", michael@0: "a", michael@0: "abc", michael@0: "abcde", michael@0: "abcdefABCDEF", michael@0: "abcdefghijklmnopqrstuvwxyz0123456789!@#$" michael@0: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$" michael@0: "abcdefghijklmnopqrstuvwxyz0123456789!@#$", michael@0: }; michael@0: /* '0' is not relevant to printing strings */ michael@0: static char *signs[] = { michael@0: "", michael@0: "-", "+", " ", michael@0: "-+", "- ", "+-", "+ ", " -", " +", michael@0: "-+ ", "- +", "+- ", "+ -", " -+", " +-", michael@0: }; michael@0: static char *precs[] = { michael@0: "", "3", "5", "43", michael@0: ".3", ".43", michael@0: "7.3", "7.5", "7.11", "7.43", michael@0: }; michael@0: static char *formats[] = { "s" }; michael@0: int f, s, n, p; michael@0: char fmt[40]; michael@0: michael@0: for (f = 0; f < PR_ARRAY_SIZE(formats); f++) { michael@0: for (s = 0; s < PR_ARRAY_SIZE(signs); s++) { michael@0: for (p = 0; p < PR_ARRAY_SIZE(precs); p++) { michael@0: fmt[0] = '%'; michael@0: fmt[1] = 0; michael@0: if (signs[s]) strcat(fmt+strlen(fmt), signs[s]); michael@0: if (precs[p]) strcat(fmt+strlen(fmt), precs[p]); michael@0: if (formats[f]) strcat(fmt+strlen(fmt), formats[f]); michael@0: for (n = 0; n < PR_ARRAY_SIZE(strs); n++) { michael@0: test_s(fmt, strs[n]); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PR_STDIO_INIT(); michael@0: TestI(); michael@0: TestL(); michael@0: TestLL(); michael@0: TestS(); michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }