1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 * This is a test program for the PR_snprintf() functions defined
10 * in prprf.c. This test program is based on ns/nspr/tests/sprintf.c,
12 * Modification History:
13 * 20-May-1997 AGarcia replaced printf statment to return PASS\n. This is to be used by the
14 * regress tool parsing routine.
15 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
16 * recognize the return code from tha main program.
27 static char sbuf
[20000];
31 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
32 ** Make sure the results are identical
34 static void test_i(char *pattern
, int i
)
40 /* try all three routines */
41 s
= PR_smprintf(pattern
, i
);
43 n
= PR_snprintf(buf
, sizeof(buf
), pattern
, i
);
44 PR_ASSERT(n
<= sizeof(buf
));
45 sprintf(sbuf
, pattern
, i
);
48 if ((strncmp(s
, buf
, sizeof(buf
)) != 0) ||
49 (strncmp(s
, sbuf
, sizeof(sbuf
)) != 0)) {
51 "pattern='%s' i=%d\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
52 pattern
, i
, s
, buf
, sbuf
);
59 static void TestI(void)
65 static char *signs
[] = {
68 "0-", "0+", "0 ", "-0", "-+", "- ",
69 "+0", "+-", "+ ", " 0", " -", " +",
70 "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
71 "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
72 "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
73 " 0-", " 0+", " -0", " -+", " +0", " +-",
74 "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
75 "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
76 "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
77 " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
79 static char *precs
[] = {
81 "7.3", "7.5", "7.11", "7.43",
83 static char *formats
[] = {
85 "hd", "ho", "hx", "hu"
90 for (f
= 0; f
< PR_ARRAY_SIZE(formats
); f
++) {
91 for (s
= 0; s
< PR_ARRAY_SIZE(signs
); s
++) {
92 for (p
= 0; p
< PR_ARRAY_SIZE(precs
); p
++) {
96 strcat(fmt
, signs
[s
]);
99 strcat(fmt
, precs
[p
]);
102 strcat(fmt
, formats
[f
]);
104 for (n
= 0; n
< PR_ARRAY_SIZE(nums
); n
++) {
105 test_i(fmt
, nums
[n
]);
112 /************************************************************************/
115 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
116 ** Make sure the results are identical
118 static void test_l(char *pattern
, char *spattern
, PRInt32 l
)
124 /* try all three routines */
125 s
= PR_smprintf(pattern
, l
);
127 n
= PR_snprintf(buf
, sizeof(buf
), pattern
, l
);
128 PR_ASSERT(n
<= sizeof(buf
));
129 sprintf(sbuf
, spattern
, l
);
131 /* compare results */
132 if ((strncmp(s
, buf
, sizeof(buf
)) != 0) ||
133 (strncmp(s
, sbuf
, sizeof(sbuf
)) != 0)) {
135 "pattern='%s' l=%ld\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
136 pattern
, l
, s
, buf
, sbuf
);
143 static void TestL(void)
145 static PRInt32 nums
[] = {
153 PR_INT32(0x7fffffff), /* 2147483647L */
154 -1 - PR_INT32(0x7fffffff) /* -2147483648L */
156 static char *signs
[] = {
159 "0-", "0+", "0 ", "-0", "-+", "- ",
160 "+0", "+-", "+ ", " 0", " -", " +",
161 "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
162 "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
163 "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
164 " 0-", " 0+", " -0", " -+", " +0", " +-",
165 "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
166 "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
167 "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
168 " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
170 static char *precs
[] = {
173 "7.3", "7.5", "7.11", "7.43",
175 static char *formats
[] = { "ld", "lo", "lx", "lu" };
177 #if PR_BYTES_PER_INT == 4
178 static char *sformats
[] = { "d", "o", "x", "u" };
179 #elif PR_BYTES_PER_LONG == 4
180 static char *sformats
[] = { "ld", "lo", "lx", "lu" };
182 #error Neither int nor long is 4 bytes on this platform
186 char fmt
[40], sfmt
[40];
188 for (f
= 0; f
< PR_ARRAY_SIZE(formats
); f
++) {
189 for (s
= 0; s
< PR_ARRAY_SIZE(signs
); s
++) {
190 for (p
= 0; p
< PR_ARRAY_SIZE(precs
); p
++) {
194 strcat(fmt
, signs
[s
]);
197 strcat(fmt
, precs
[p
]);
201 strcat(fmt
, formats
[f
]);
204 strcat(sfmt
, sformats
[f
]);
206 for (n
= 0; n
< PR_ARRAY_SIZE(nums
); n
++) {
207 test_l(fmt
, sfmt
, nums
[n
]);
214 /************************************************************************/
217 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
218 ** Make sure the results are identical
220 static void test_ll(char *pattern
, char *spattern
, PRInt64 l
)
226 /* try all three routines */
227 s
= PR_smprintf(pattern
, l
);
229 n
= PR_snprintf(buf
, sizeof(buf
), pattern
, l
);
230 PR_ASSERT(n
<= sizeof(buf
));
231 #if defined(HAVE_LONG_LONG)
232 sprintf(sbuf
, spattern
, l
);
234 /* compare results */
235 if ((strncmp(s
, buf
, sizeof(buf
)) != 0) ||
236 (strncmp(s
, sbuf
, sizeof(sbuf
)) != 0)) {
237 #if PR_BYTES_PER_LONG == 8
238 #define FORMAT_SPEC "%ld"
240 #define FORMAT_SPEC "%Ld"
242 #define FORMAT_SPEC "%I64d"
244 #define FORMAT_SPEC "%lld"
247 "pattern='%s' ll=" FORMAT_SPEC
"\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
248 pattern
, l
, s
, buf
, sbuf
);
255 /* compare results */
256 if ((strncmp(s
, buf
, sizeof(buf
)) != 0)) {
258 "pattern='%s'\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
259 pattern
, s
, buf
, sbuf
);
268 static void TestLL(void)
270 static PRInt64 nums
[] = {
273 LL_INIT(0xffffffff, 0xffffffff), /* -1 */
275 LL_INIT(0xffffffff, 0xfffffff6), /* -10 */
277 LL_INIT(0xffffffff, 0xffff8000), /* -32768 */
278 LL_INIT(0, 0x7fffffff), /* 2147483647 */
279 LL_INIT(0xffffffff, 0x80000000), /* -2147483648 */
280 LL_INIT(0x7fffffff, 0xffffffff), /* 9223372036854775807 */
281 LL_INIT(0x80000000, 0), /* -9223372036854775808 */
289 PR_INT64(2147483647),
290 PR_INT64(-2147483648),
291 PR_INT64(9223372036854775807),
292 PR_INT64(-9223372036854775808)
295 static char *signs
[] = {
298 "0-", "0+", "0 ", "-0", "-+", "- ",
299 "+0", "+-", "+ ", " 0", " -", " +",
300 "0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
301 "-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
302 "+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
303 " 0-", " 0+", " -0", " -+", " +0", " +-",
304 "0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
305 "-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
306 "+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
307 " 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
309 static char *precs
[] = {
312 "7.3", "7.5", "7.11", "7.43",
314 static char *formats
[] = { "lld", "llo", "llx", "llu" };
316 #if PR_BYTES_PER_LONG == 8
317 static char *sformats
[] = { "ld", "lo", "lx", "lu" };
319 /* Watcom uses the format string "%Ld" instead of "%lld". */
320 static char *sformats
[] = { "Ld", "Lo", "Lx", "Lu" };
322 static char *sformats
[] = { "I64d", "I64o", "I64x", "I64u" };
324 static char *sformats
[] = { "lld", "llo", "llx", "llu" };
328 char fmt
[40], sfmt
[40];
330 for (f
= 0; f
< PR_ARRAY_SIZE(formats
); f
++) {
331 for (s
= 0; s
< PR_ARRAY_SIZE(signs
); s
++) {
332 for (p
= 0; p
< PR_ARRAY_SIZE(precs
); p
++) {
336 strcat(fmt
, signs
[s
]);
339 strcat(fmt
, precs
[p
]);
343 strcat(fmt
, formats
[f
]);
346 strcat(sfmt
, sformats
[f
]);
348 for (n
= 0; n
< PR_ARRAY_SIZE(nums
); n
++) {
349 test_ll(fmt
, sfmt
, nums
[n
]);
356 /************************************************************************/
359 ** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
360 ** Make sure the results are identical
362 static void test_s(char *pattern
, char *ss
)
365 unsigned char before
[8];
367 unsigned char after
[8];
370 memset(before
, 0xBB, 8);
371 memset(after
, 0xAA, 8);
373 /* try all three routines */
374 s
= PR_smprintf(pattern
, ss
);
376 n
= PR_snprintf(buf
, sizeof(buf
), pattern
, ss
);
377 PR_ASSERT(n
<= sizeof(buf
));
378 sprintf(sbuf
, pattern
, ss
);
380 for (n
= 0; n
< 8; n
++) {
381 PR_ASSERT(before
[n
] == 0xBB);
382 PR_ASSERT(after
[n
] == 0xAA);
385 /* compare results */
386 if ((strncmp(s
, buf
, sizeof(buf
)) != 0) ||
387 (strncmp(s
, sbuf
, sizeof(sbuf
)) != 0)) {
389 "pattern='%s' ss=%.20s\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
390 pattern
, ss
, s
, buf
, sbuf
);
398 static void TestS(void)
400 static char *strs
[] = {
406 "abcdefghijklmnopqrstuvwxyz0123456789!@#$"
407 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$"
408 "abcdefghijklmnopqrstuvwxyz0123456789!@#$",
410 /* '0' is not relevant to printing strings */
411 static char *signs
[] = {
414 "-+", "- ", "+-", "+ ", " -", " +",
415 "-+ ", "- +", "+- ", "+ -", " -+", " +-",
417 static char *precs
[] = {
420 "7.3", "7.5", "7.11", "7.43",
422 static char *formats
[] = { "s" };
426 for (f
= 0; f
< PR_ARRAY_SIZE(formats
); f
++) {
427 for (s
= 0; s
< PR_ARRAY_SIZE(signs
); s
++) {
428 for (p
= 0; p
< PR_ARRAY_SIZE(precs
); p
++) {
432 strcat(fmt
+strlen(fmt
), signs
[s
]);
435 strcat(fmt
+strlen(fmt
), precs
[p
]);
438 strcat(fmt
+strlen(fmt
), formats
[f
]);
440 for (n
= 0; n
< PR_ARRAY_SIZE(strs
); n
++) {
441 test_s(fmt
, strs
[n
]);
448 /************************************************************************/
450 int main(int argc
, char **argv
)