~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/sprintf.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
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/
 
7
 * 
 
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.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
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
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
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
 
32
 * GPL.
 
33
 */
 
34
 
 
35
/*
 
36
 * File:        sprintf.c
 
37
 * Description:
 
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,
 
40
 *     revision 1.10.
 
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.
 
46
 */
 
47
 
 
48
#include "prinit.h"
 
49
#include "prprf.h"
 
50
#include "prlog.h"
 
51
#include "prlong.h"
 
52
#include <string.h>
 
53
#include <stdio.h>
 
54
#include <stdlib.h>
 
55
 
 
56
#define countof(a) (sizeof(a)/sizeof(a[0]))
 
57
 
 
58
static char sbuf[20000];
 
59
 
 
60
 
 
61
/*
 
62
** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
 
63
** Make sure the results are identical
 
64
*/
 
65
static void test_i(char *pattern, int i)
 
66
{
 
67
    char *s;
 
68
    char buf[200];
 
69
    int n;
 
70
 
 
71
    /* try all three routines */
 
72
    s = PR_smprintf(pattern, i);
 
73
    PR_ASSERT(s != 0);
 
74
    n = PR_snprintf(buf, sizeof(buf), pattern, i);
 
75
    PR_ASSERT(n <= sizeof(buf));
 
76
    sprintf(sbuf, pattern, i);
 
77
 
 
78
    /* compare results */
 
79
    if ((strncmp(s, buf, sizeof(buf)) != 0) ||
 
80
        (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
 
81
        fprintf(stderr,
 
82
           "pattern='%s' i=%d\nPR_smprintf='%s'\nPR_snprintf='%s'\n    sprintf='%s'\n",
 
83
           pattern, i, s, buf, sbuf);
 
84
           PR_smprintf_free(s);
 
85
        exit(-1);
 
86
    }
 
87
        PR_smprintf_free(s);
 
88
}
 
89
 
 
90
static void TestI(void)
 
91
{
 
92
    static int nums[] = {
 
93
        0, 1, -1, 10, -10,
 
94
        32767, -32768,
 
95
    };
 
96
    static char *signs[] = {
 
97
        "",
 
98
        "0",    "-",    "+", " ",
 
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",
 
109
    };
 
110
    static char *precs[] = {
 
111
        "", "3", "5", "43",
 
112
        "7.3", "7.5", "7.11", "7.43",
 
113
    };
 
114
    static char *formats[] = {
 
115
        "d", "o", "x", "u",
 
116
        "hd", "ho", "hx", "hu"
 
117
    };
 
118
    int f, s, n, p;
 
119
    char fmt[20];
 
120
 
 
121
    for (f = 0; f < countof(formats); f++) {
 
122
        for (s = 0; s < countof(signs); s++) {
 
123
            for (p = 0; p < countof(precs); p++) {
 
124
                fmt[0] = '%';
 
125
                fmt[1] = 0;
 
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]);
 
131
                }
 
132
            }
 
133
        }
 
134
    }
 
135
}
 
136
 
 
137
/************************************************************************/
 
138
 
 
139
/*
 
140
** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
 
141
** Make sure the results are identical
 
142
*/
 
143
static void test_l(char *pattern, char *spattern, PRInt32 l)
 
144
{
 
145
    char *s;
 
146
    char buf[200];
 
147
    int n;
 
148
 
 
149
    /* try all three routines */
 
150
    s = PR_smprintf(pattern, l);
 
151
    PR_ASSERT(s != 0);
 
152
    n = PR_snprintf(buf, sizeof(buf), pattern, l);
 
153
    PR_ASSERT(n <= sizeof(buf));
 
154
    sprintf(sbuf, spattern, l);
 
155
 
 
156
    /* compare results */
 
157
    if ((strncmp(s, buf, sizeof(buf)) != 0) ||
 
158
        (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
 
159
        fprintf(stderr,
 
160
           "pattern='%s' l=%ld\nPR_smprintf='%s'\nPR_snprintf='%s'\n    sprintf='%s'\n",
 
161
           pattern, l, s, buf, sbuf);
 
162
        PR_smprintf_free(s);
 
163
        exit(-1);
 
164
    }
 
165
        PR_smprintf_free(s);
 
166
}
 
167
 
 
168
static void TestL(void)
 
169
{
 
170
    static PRInt32 nums[] = {
 
171
        0,
 
172
        1,
 
173
        -1,
 
174
        10,
 
175
        -10,
 
176
        32767,
 
177
        -32768,
 
178
        PR_INT32(0x7fffffff), /* 2147483647L */
 
179
        -1 - PR_INT32(0x7fffffff)  /* -2147483648L */
 
180
    };
 
181
    static char *signs[] = {
 
182
        "",
 
183
        "0",    "-",    "+", " ",
 
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",
 
194
    };
 
195
    static char *precs[] = {
 
196
        "", "3", "5", "43",
 
197
        ".3", ".43",
 
198
        "7.3", "7.5", "7.11", "7.43",
 
199
    };
 
200
    static char *formats[] = { "ld", "lo", "lx", "lu" };
 
201
 
 
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" };
 
206
#else
 
207
#error Neither int nor long is 4 bytes on this platform
 
208
#endif
 
209
 
 
210
    int f, s, n, p;
 
211
    char fmt[40], sfmt[40];
 
212
 
 
213
    for (f = 0; f < countof(formats); f++) {
 
214
        for (s = 0; s < countof(signs); s++) {
 
215
            for (p = 0; p < countof(precs); p++) {
 
216
                fmt[0] = '%';
 
217
                fmt[1] = 0;
 
218
                if (signs[s]) strcat(fmt, signs[s]);
 
219
                if (precs[p]) strcat(fmt, precs[p]);
 
220
                strcpy(sfmt, fmt);
 
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]);
 
225
                }
 
226
            }
 
227
        }
 
228
    }
 
229
}
 
230
 
 
231
/************************************************************************/
 
232
 
 
233
/*
 
234
** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
 
235
** Make sure the results are identical
 
236
*/
 
237
static void test_ll(char *pattern, char *spattern, PRInt64 l)
 
238
{
 
239
    char *s;
 
240
    char buf[200];
 
241
    int n;
 
242
 
 
243
    /* try all three routines */
 
244
    s = PR_smprintf(pattern, l);
 
245
    PR_ASSERT(s != 0);
 
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);
 
250
 
 
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"
 
256
#elif defined(WIN16)
 
257
#define FORMAT_SPEC "%Ld"
 
258
#elif defined(WIN32)
 
259
#define FORMAT_SPEC "%I64d"
 
260
#else
 
261
#define FORMAT_SPEC "%lld"
 
262
#endif
 
263
        fprintf(stderr,
 
264
            "pattern='%s' ll=" FORMAT_SPEC "\nPR_smprintf='%s'\nPR_snprintf='%s'\n    sprintf='%s'\n",
 
265
            pattern, l, s, buf, sbuf);
 
266
        printf("FAIL\n");
 
267
        PR_smprintf_free(s);
 
268
        exit(-1);
 
269
    }
 
270
        PR_smprintf_free(s);
 
271
#else
 
272
    /* compare results */
 
273
    if ((strncmp(s, buf, sizeof(buf)) != 0)) {
 
274
        fprintf(stderr,
 
275
            "pattern='%s'\nPR_smprintf='%s'\nPR_snprintf='%s'\n    sprintf='%s'\n",
 
276
            pattern, s, buf, sbuf);
 
277
        printf("FAIL\n");
 
278
        PR_smprintf_free(s);
 
279
        exit(-1);
 
280
    }
 
281
        PR_smprintf_free(s);
 
282
#endif
 
283
}
 
284
 
 
285
static void TestLL(void)
 
286
{
 
287
    static PRInt64 nums[] = {
 
288
        LL_INIT(0, 0),
 
289
        LL_INIT(0, 1),
 
290
        LL_INIT(0xffffffff, 0xffffffff),  /* -1 */
 
291
        LL_INIT(0, 10),
 
292
        LL_INIT(0xffffffff, 0xfffffff6),  /* -10 */
 
293
        LL_INIT(0, 32767),
 
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 */
 
299
    };
 
300
 
 
301
    static char *signs[] = {
 
302
        "",
 
303
        "0",    "-",    "+", " ",
 
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",
 
314
    };
 
315
    static char *precs[] = {
 
316
        "", "3", "5", "43",
 
317
        ".3", ".43",
 
318
        "7.3", "7.5", "7.11", "7.43",
 
319
    };
 
320
    static char *formats[] = { "lld", "llo", "llx", "llu" };
 
321
 
 
322
#if PR_BYTES_PER_LONG == 8
 
323
    static char *sformats[] = { "ld", "lo", "lx", "lu" };
 
324
#elif defined(WIN16)
 
325
    /* Watcom uses the format string "%Ld" instead of "%lld". */
 
326
    static char *sformats[] = { "Ld", "Lo", "Lx", "Lu" };
 
327
#elif defined(WIN32)
 
328
    static char *sformats[] = { "I64d", "I64o", "I64x", "I64u" };
 
329
#else
 
330
    static char *sformats[] = { "lld", "llo", "llx", "llu" };
 
331
#endif
 
332
 
 
333
    int f, s, n, p;
 
334
    char fmt[40], sfmt[40];
 
335
 
 
336
    for (f = 0; f < countof(formats); f++) {
 
337
        for (s = 0; s < countof(signs); s++) {
 
338
            for (p = 0; p < countof(precs); p++) {
 
339
                fmt[0] = '%';
 
340
                fmt[1] = 0;
 
341
                if (signs[s]) strcat(fmt, signs[s]);
 
342
                if (precs[p]) strcat(fmt, precs[p]);
 
343
                strcpy(sfmt, fmt);
 
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]);
 
348
                }
 
349
            }
 
350
        }
 
351
    }
 
352
}
 
353
 
 
354
/************************************************************************/
 
355
 
 
356
/*
 
357
** Perform a three way test against PR_smprintf, PR_snprintf, and sprintf.
 
358
** Make sure the results are identical
 
359
*/
 
360
static void test_s(char *pattern, char *ss)
 
361
{
 
362
    char *s;
 
363
    unsigned char before[8];
 
364
    char buf[200];
 
365
    unsigned char after[8];
 
366
    int n;
 
367
 
 
368
    memset(before, 0xBB, 8);
 
369
    memset(after, 0xAA, 8);
 
370
 
 
371
    /* try all three routines */
 
372
    s = PR_smprintf(pattern, ss);
 
373
    PR_ASSERT(s != 0);
 
374
    n = PR_snprintf(buf, sizeof(buf), pattern, ss);
 
375
    PR_ASSERT(n <= sizeof(buf));
 
376
    sprintf(sbuf, pattern, ss);
 
377
 
 
378
    for (n = 0; n < 8; n++) {
 
379
        PR_ASSERT(before[n] == 0xBB);
 
380
        PR_ASSERT(after[n] == 0xAA);
 
381
    }
 
382
 
 
383
    /* compare results */
 
384
    if ((strncmp(s, buf, sizeof(buf)) != 0) ||
 
385
        (strncmp(s, sbuf, sizeof(sbuf)) != 0)) {
 
386
        fprintf(stderr,
 
387
           "pattern='%s' ss=%.20s\nPR_smprintf='%s'\nPR_snprintf='%s'\n    sprintf='%s'\n",
 
388
           pattern, ss, s, buf, sbuf);
 
389
        printf("FAIL\n");
 
390
        PR_smprintf_free(s);
 
391
        exit(-1);
 
392
    }
 
393
        PR_smprintf_free(s);
 
394
}
 
395
 
 
396
static void TestS(void)
 
397
{
 
398
    static char *strs[] = {
 
399
        "",
 
400
        "a",
 
401
        "abc",
 
402
        "abcde",
 
403
        "abcdefABCDEF",
 
404
        "abcdefghijklmnopqrstuvwxyz0123456789!@#$"
 
405
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$"
 
406
            "abcdefghijklmnopqrstuvwxyz0123456789!@#$",
 
407
    };
 
408
    /* '0' is not relevant to printing strings */
 
409
    static char *signs[] = {
 
410
        "",
 
411
        "-",    "+",    " ",
 
412
        "-+",   "- ",   "+-",   "+ ",   " -",   " +",
 
413
        "-+ ",  "- +",  "+- ",  "+ -",  " -+",  " +-",
 
414
    };
 
415
    static char *precs[] = {
 
416
        "", "3", "5", "43",
 
417
        ".3", ".43",
 
418
        "7.3", "7.5", "7.11", "7.43",
 
419
    };
 
420
    static char *formats[] = { "s" };
 
421
    int f, s, n, p;
 
422
    char fmt[40];
 
423
 
 
424
    for (f = 0; f < countof(formats); f++) {
 
425
        for (s = 0; s < countof(signs); s++) {
 
426
            for (p = 0; p < countof(precs); p++) {
 
427
                fmt[0] = '%';
 
428
                fmt[1] = 0;
 
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]);
 
434
                }
 
435
            }
 
436
        }
 
437
    }
 
438
}
 
439
 
 
440
/************************************************************************/
 
441
 
 
442
int main(int argc, char **argv)
 
443
{
 
444
    PR_STDIO_INIT();
 
445
    TestI();
 
446
    TestL();
 
447
    TestLL();
 
448
    TestS();
 
449
    printf("PASS\n");
 
450
    return 0;
 
451
}