1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
38
* This is a test program for the PR_snprintf() functions defined
39
* in prprf.c. This test program is based on ns/nspr/tests/sprintf.c,
41
* Modification History:
42
* 20-May-1997 AGarcia replaced printf statment to return PASS\n. This is to be used by the
43
* regress tool parsing routine.
44
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
45
* recognize the return code from tha main program.
56
#define countof(a) (sizeof(a)/sizeof(a[0]))
58
static char sbuf[20000];
62
** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
63
** Make sure the results are identical
65
static void test_i(char *pattern, int i)
71
/* try all three routines */
72
s = PR_smprintf(pattern, i);
74
n = PR_snprintf(buf, sizeof(buf), pattern, i);
75
PR_ASSERT(n <= sizeof(buf));
76
sprintf(sbuf, pattern, i);
79
if ((strncmp(s, buf, sizeof(buf)) != 0) ||
80
(strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
82
"pattern='%s' i=%d\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
83
pattern, i, s, buf, sbuf);
90
static void TestI(void)
96
static char *signs[] = {
99
"0-", "0+", "0 ", "-0", "-+", "- ",
100
"+0", "+-", "+ ", " 0", " -", " +",
101
"0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
102
"-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
103
"+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
104
" 0-", " 0+", " -0", " -+", " +0", " +-",
105
"0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
106
"-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
107
"+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
108
" 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
110
static char *precs[] = {
112
"7.3", "7.5", "7.11", "7.43",
114
static char *formats[] = {
116
"hd", "ho", "hx", "hu"
121
for (f = 0; f < countof(formats); f++) {
122
for (s = 0; s < countof(signs); s++) {
123
for (p = 0; p < countof(precs); p++) {
126
if (signs[s]) strcat(fmt, signs[s]);
127
if (precs[p]) strcat(fmt, precs[p]);
128
if (formats[f]) strcat(fmt, formats[f]);
129
for (n = 0; n < countof(nums); n++) {
130
test_i(fmt, nums[n]);
137
/************************************************************************/
140
** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
141
** Make sure the results are identical
143
static void test_l(char *pattern, char *spattern, PRInt32 l)
149
/* try all three routines */
150
s = PR_smprintf(pattern, l);
152
n = PR_snprintf(buf, sizeof(buf), pattern, l);
153
PR_ASSERT(n <= sizeof(buf));
154
sprintf(sbuf, spattern, l);
156
/* compare results */
157
if ((strncmp(s, buf, sizeof(buf)) != 0) ||
158
(strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
160
"pattern='%s' l=%ld\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
161
pattern, l, s, buf, sbuf);
168
static void TestL(void)
170
static PRInt32 nums[] = {
178
PR_INT32(0x7fffffff), /* 2147483647L */
179
-1 - PR_INT32(0x7fffffff) /* -2147483648L */
181
static char *signs[] = {
184
"0-", "0+", "0 ", "-0", "-+", "- ",
185
"+0", "+-", "+ ", " 0", " -", " +",
186
"0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
187
"-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
188
"+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
189
" 0-", " 0+", " -0", " -+", " +0", " +-",
190
"0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
191
"-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
192
"+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
193
" 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
195
static char *precs[] = {
198
"7.3", "7.5", "7.11", "7.43",
200
static char *formats[] = { "ld", "lo", "lx", "lu" };
202
#if PR_BYTES_PER_INT == 4
203
static char *sformats[] = { "d", "o", "x", "u" };
204
#elif PR_BYTES_PER_LONG == 4
205
static char *sformats[] = { "ld", "lo", "lx", "lu" };
207
#error Neither int nor long is 4 bytes on this platform
211
char fmt[40], sfmt[40];
213
for (f = 0; f < countof(formats); f++) {
214
for (s = 0; s < countof(signs); s++) {
215
for (p = 0; p < countof(precs); p++) {
218
if (signs[s]) strcat(fmt, signs[s]);
219
if (precs[p]) strcat(fmt, precs[p]);
221
if (formats[f]) strcat(fmt, formats[f]);
222
if (sformats[f]) strcat(sfmt, sformats[f]);
223
for (n = 0; n < countof(nums); n++) {
224
test_l(fmt, sfmt, nums[n]);
231
/************************************************************************/
234
** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
235
** Make sure the results are identical
237
static void test_ll(char *pattern, char *spattern, PRInt64 l)
243
/* try all three routines */
244
s = PR_smprintf(pattern, l);
246
n = PR_snprintf(buf, sizeof(buf), pattern, l);
247
PR_ASSERT(n <= sizeof(buf));
248
#if defined(HAVE_LONG_LONG)
249
sprintf(sbuf, spattern, l);
251
/* compare results */
252
if ((strncmp(s, buf, sizeof(buf)) != 0) ||
253
(strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
254
#if PR_BYTES_PER_LONG == 8
255
#define FORMAT_SPEC "%ld"
257
#define FORMAT_SPEC "%Ld"
259
#define FORMAT_SPEC "%I64d"
261
#define FORMAT_SPEC "%lld"
264
"pattern='%s' ll=" FORMAT_SPEC "\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
265
pattern, l, s, buf, sbuf);
272
/* compare results */
273
if ((strncmp(s, buf, sizeof(buf)) != 0)) {
275
"pattern='%s'\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
276
pattern, s, buf, sbuf);
285
static void TestLL(void)
287
static PRInt64 nums[] = {
290
LL_INIT(0xffffffff, 0xffffffff), /* -1 */
292
LL_INIT(0xffffffff, 0xfffffff6), /* -10 */
294
LL_INIT(0xffffffff, 0xffff8000), /* -32768 */
295
LL_INIT(0, 0x7fffffff), /* 2147483647 */
296
LL_INIT(0xffffffff, 0x80000000), /* -2147483648 */
297
LL_INIT(0x7fffffff, 0xffffffff), /* 9223372036854775807 */
298
LL_INIT(0x80000000, 0) /* -9223372036854775808 */
301
static char *signs[] = {
304
"0-", "0+", "0 ", "-0", "-+", "- ",
305
"+0", "+-", "+ ", " 0", " -", " +",
306
"0-+", "0- ", "0+-", "0+ ", "0 -", "0 +",
307
"-0+", "-0 ", "-+0", "-+ ", "- 0", "- +",
308
"+0-", "+0 ", "+-0", "+- ", "+ 0", "+ -",
309
" 0-", " 0+", " -0", " -+", " +0", " +-",
310
"0-+ ", "0- +", "0+- ", "0+ -", "0 -+", "0 +-",
311
"-0+ ", "-0 +", "-+0 ", "-+ 0", "- 0+", "- +0",
312
"+0- ", "+0 -", "+-0 ", "+- 0", "+ 0-", "+ -0",
313
" 0-+", " 0+-", " -0+", " -+0", " +0-", " +-0",
315
static char *precs[] = {
318
"7.3", "7.5", "7.11", "7.43",
320
static char *formats[] = { "lld", "llo", "llx", "llu" };
322
#if PR_BYTES_PER_LONG == 8
323
static char *sformats[] = { "ld", "lo", "lx", "lu" };
325
/* Watcom uses the format string "%Ld" instead of "%lld". */
326
static char *sformats[] = { "Ld", "Lo", "Lx", "Lu" };
328
static char *sformats[] = { "I64d", "I64o", "I64x", "I64u" };
330
static char *sformats[] = { "lld", "llo", "llx", "llu" };
334
char fmt[40], sfmt[40];
336
for (f = 0; f < countof(formats); f++) {
337
for (s = 0; s < countof(signs); s++) {
338
for (p = 0; p < countof(precs); p++) {
341
if (signs[s]) strcat(fmt, signs[s]);
342
if (precs[p]) strcat(fmt, precs[p]);
344
if (formats[f]) strcat(fmt, formats[f]);
345
if (sformats[f]) strcat(sfmt, sformats[f]);
346
for (n = 0; n < countof(nums); n++) {
347
test_ll(fmt, sfmt, nums[n]);
354
/************************************************************************/
357
** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
358
** Make sure the results are identical
360
static void test_s(char *pattern, char *ss)
363
unsigned char before[8];
365
unsigned char after[8];
368
memset(before, 0xBB, 8);
369
memset(after, 0xAA, 8);
371
/* try all three routines */
372
s = PR_smprintf(pattern, ss);
374
n = PR_snprintf(buf, sizeof(buf), pattern, ss);
375
PR_ASSERT(n <= sizeof(buf));
376
sprintf(sbuf, pattern, ss);
378
for (n = 0; n < 8; n++) {
379
PR_ASSERT(before[n] == 0xBB);
380
PR_ASSERT(after[n] == 0xAA);
383
/* compare results */
384
if ((strncmp(s, buf, sizeof(buf)) != 0) ||
385
(strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
387
"pattern='%s' ss=%.20s\nPR_smprintf='%s'\nPR_snprintf='%s'\n sprintf='%s'\n",
388
pattern, ss, s, buf, sbuf);
396
static void TestS(void)
398
static char *strs[] = {
404
"abcdefghijklmnopqrstuvwxyz0123456789!@#$"
405
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$"
406
"abcdefghijklmnopqrstuvwxyz0123456789!@#$",
408
/* '0' is not relevant to printing strings */
409
static char *signs[] = {
412
"-+", "- ", "+-", "+ ", " -", " +",
413
"-+ ", "- +", "+- ", "+ -", " -+", " +-",
415
static char *precs[] = {
418
"7.3", "7.5", "7.11", "7.43",
420
static char *formats[] = { "s" };
424
for (f = 0; f < countof(formats); f++) {
425
for (s = 0; s < countof(signs); s++) {
426
for (p = 0; p < countof(precs); p++) {
429
if (signs[s]) strcat(fmt+strlen(fmt), signs[s]);
430
if (precs[p]) strcat(fmt+strlen(fmt), precs[p]);
431
if (formats[f]) strcat(fmt+strlen(fmt), formats[f]);
432
for (n = 0; n < countof(strs); n++) {
433
test_s(fmt, strs[n]);
440
/************************************************************************/
442
int main(int argc, char **argv)