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