1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Assertions.h"
8 #include "mozilla/Sprintf.h"
13 /* Output array and poisoning method shared by all tests. */
14 static char gOutput
[32];
16 static void PoisonOutput() { memset(gOutput
, 0xDA, sizeof(gOutput
)); }
19 * The fprintf macros for signed integers are:
21 * PRIdN PRIdLEASTN PRIdFASTN PRIdMAX PRIdPTR
22 * PRIiN PRIiLEASTN PRIiFASTN PRIiMAX PRIiPTR
24 * In these names N is the width of the type as described in C99 7.18.1.
27 static void TestPrintSigned8() {
29 SprintfLiteral(gOutput
, "%" PRId8
, int8_t(-17));
30 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-17"));
33 SprintfLiteral(gOutput
, "%" PRIi8
, int8_t(42));
34 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "42"));
37 static void TestPrintSigned16() {
39 SprintfLiteral(gOutput
, "%" PRId16
, int16_t(-289));
40 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-289"));
43 SprintfLiteral(gOutput
, "%" PRIi16
, int16_t(728));
44 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "728"));
47 static void TestPrintSigned32() {
49 SprintfLiteral(gOutput
, "%" PRId32
, int32_t(-342178));
50 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-342178"));
53 SprintfLiteral(gOutput
, "%" PRIi32
, int32_t(5719283));
54 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "5719283"));
57 static void TestPrintSigned64() {
59 SprintfLiteral(gOutput
, "%" PRId64
, int64_t(-INT64_C(432157943248732)));
60 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-432157943248732"));
63 SprintfLiteral(gOutput
, "%" PRIi64
, int64_t(INT64_C(325719232983)));
64 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "325719232983"));
67 static void TestPrintSignedN() {
74 static void TestPrintSignedLeast8() {
76 SprintfLiteral(gOutput
, "%" PRIdLEAST8
, int_least8_t(-17));
77 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-17"));
80 SprintfLiteral(gOutput
, "%" PRIiLEAST8
, int_least8_t(42));
81 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "42"));
84 static void TestPrintSignedLeast16() {
86 SprintfLiteral(gOutput
, "%" PRIdLEAST16
, int_least16_t(-289));
87 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-289"));
90 SprintfLiteral(gOutput
, "%" PRIiLEAST16
, int_least16_t(728));
91 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "728"));
94 static void TestPrintSignedLeast32() {
96 SprintfLiteral(gOutput
, "%" PRIdLEAST32
, int_least32_t(-342178));
97 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-342178"));
100 SprintfLiteral(gOutput
, "%" PRIiLEAST32
, int_least32_t(5719283));
101 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "5719283"));
104 static void TestPrintSignedLeast64() {
106 SprintfLiteral(gOutput
, "%" PRIdLEAST64
,
107 int_least64_t(-INT64_C(432157943248732)));
108 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-432157943248732"));
111 SprintfLiteral(gOutput
, "%" PRIiLEAST64
,
112 int_least64_t(INT64_C(325719232983)));
113 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "325719232983"));
116 static void TestPrintSignedLeastN() {
117 TestPrintSignedLeast8();
118 TestPrintSignedLeast16();
119 TestPrintSignedLeast32();
120 TestPrintSignedLeast64();
123 static void TestPrintSignedFast8() {
125 SprintfLiteral(gOutput
, "%" PRIdFAST8
, int_fast8_t(-17));
126 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-17"));
129 SprintfLiteral(gOutput
, "%" PRIiFAST8
, int_fast8_t(42));
130 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "42"));
133 static void TestPrintSignedFast16() {
135 SprintfLiteral(gOutput
, "%" PRIdFAST16
, int_fast16_t(-289));
136 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-289"));
139 SprintfLiteral(gOutput
, "%" PRIiFAST16
, int_fast16_t(728));
140 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "728"));
143 static void TestPrintSignedFast32() {
145 SprintfLiteral(gOutput
, "%" PRIdFAST32
, int_fast32_t(-342178));
146 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-342178"));
149 SprintfLiteral(gOutput
, "%" PRIiFAST32
, int_fast32_t(5719283));
150 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "5719283"));
153 static void TestPrintSignedFast64() {
155 SprintfLiteral(gOutput
, "%" PRIdFAST64
,
156 int_fast64_t(-INT64_C(432157943248732)));
157 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-432157943248732"));
160 SprintfLiteral(gOutput
, "%" PRIiFAST64
, int_fast64_t(INT64_C(325719232983)));
161 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "325719232983"));
164 static void TestPrintSignedFastN() {
165 TestPrintSignedFast8();
166 TestPrintSignedFast16();
167 TestPrintSignedFast32();
168 TestPrintSignedFast64();
171 static void TestPrintSignedMax() {
173 SprintfLiteral(gOutput
, "%" PRIdMAX
, intmax_t(-INTMAX_C(432157943248732)));
174 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "-432157943248732"));
177 SprintfLiteral(gOutput
, "%" PRIiMAX
, intmax_t(INTMAX_C(325719232983)));
178 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "325719232983"));
181 static void TestPrintSignedPtr() {
183 SprintfLiteral(gOutput
, "%" PRIdPTR
,
184 intptr_t(reinterpret_cast<void*>(12345678)));
185 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "12345678"));
188 SprintfLiteral(gOutput
, "%" PRIiPTR
,
189 intptr_t(reinterpret_cast<void*>(87654321)));
190 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "87654321"));
193 static void TestPrintSigned() {
195 TestPrintSignedLeastN();
196 TestPrintSignedFastN();
197 TestPrintSignedMax();
198 TestPrintSignedPtr();
202 * The fprintf macros for unsigned integers are:
204 * PRIoN PRIoLEASTN PRIoFASTN PRIoMAX PRIoPTR
205 * PRIuN PRIuLEASTN PRIuFASTN PRIuMAX PRIuPTR
206 * PRIxN PRIxLEASTN PRIxFASTN PRIxMAX PRIxPTR
207 * PRIXN PRIXLEASTN PRIXFASTN PRIXMAX PRIXPTR
209 * In these names N is the width of the type as described in C99 7.18.1.
212 static void TestPrintUnsigned8() {
214 SprintfLiteral(gOutput
, "%" PRIo8
, uint8_t(042));
215 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "42"));
218 SprintfLiteral(gOutput
, "%" PRIu8
, uint8_t(17));
219 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "17"));
222 SprintfLiteral(gOutput
, "%" PRIx8
, uint8_t(0x2a));
223 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a"));
226 SprintfLiteral(gOutput
, "%" PRIX8
, uint8_t(0xCD));
227 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CD"));
230 static void TestPrintUnsigned16() {
232 SprintfLiteral(gOutput
, "%" PRIo16
, uint16_t(04242));
233 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "4242"));
236 SprintfLiteral(gOutput
, "%" PRIu16
, uint16_t(1717));
237 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "1717"));
240 SprintfLiteral(gOutput
, "%" PRIx16
, uint16_t(0x2a2a));
241 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a"));
244 SprintfLiteral(gOutput
, "%" PRIX16
, uint16_t(0xCDCD));
245 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCD"));
248 static void TestPrintUnsigned32() {
250 SprintfLiteral(gOutput
, "%" PRIo32
, uint32_t(0424242));
251 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "424242"));
254 SprintfLiteral(gOutput
, "%" PRIu32
, uint32_t(171717));
255 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "171717"));
258 SprintfLiteral(gOutput
, "%" PRIx32
, uint32_t(0x2a2a2a));
259 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a2a"));
262 SprintfLiteral(gOutput
, "%" PRIX32
, uint32_t(0xCDCDCD));
263 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCDCD"));
266 static void TestPrintUnsigned64() {
268 SprintfLiteral(gOutput
, "%" PRIo64
, uint64_t(UINT64_C(0424242424242)));
269 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "424242424242"));
272 SprintfLiteral(gOutput
, "%" PRIu64
, uint64_t(UINT64_C(17171717171717171717)));
273 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "17171717171717171717"));
276 SprintfLiteral(gOutput
, "%" PRIx64
, uint64_t(UINT64_C(0x2a2a2a2a2a2a2a)));
277 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a2a2a2a2a2a"));
280 SprintfLiteral(gOutput
, "%" PRIX64
, uint64_t(UINT64_C(0xCDCDCDCDCDCD)));
281 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCDCDCDCDCD"));
284 static void TestPrintUnsignedN() {
285 TestPrintUnsigned8();
286 TestPrintUnsigned16();
287 TestPrintUnsigned32();
288 TestPrintUnsigned64();
291 static void TestPrintUnsignedLeast8() {
293 SprintfLiteral(gOutput
, "%" PRIoLEAST8
, uint_least8_t(042));
294 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "42"));
297 SprintfLiteral(gOutput
, "%" PRIuLEAST8
, uint_least8_t(17));
298 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "17"));
301 SprintfLiteral(gOutput
, "%" PRIxLEAST8
, uint_least8_t(0x2a));
302 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a"));
305 SprintfLiteral(gOutput
, "%" PRIXLEAST8
, uint_least8_t(0xCD));
306 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CD"));
309 static void TestPrintUnsignedLeast16() {
311 SprintfLiteral(gOutput
, "%" PRIoLEAST16
, uint_least16_t(04242));
312 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "4242"));
315 SprintfLiteral(gOutput
, "%" PRIuLEAST16
, uint_least16_t(1717));
316 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "1717"));
319 SprintfLiteral(gOutput
, "%" PRIxLEAST16
, uint_least16_t(0x2a2a));
320 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a"));
323 SprintfLiteral(gOutput
, "%" PRIXLEAST16
, uint_least16_t(0xCDCD));
324 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCD"));
327 static void TestPrintUnsignedLeast32() {
329 SprintfLiteral(gOutput
, "%" PRIoLEAST32
, uint_least32_t(0424242));
330 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "424242"));
333 SprintfLiteral(gOutput
, "%" PRIuLEAST32
, uint_least32_t(171717));
334 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "171717"));
337 SprintfLiteral(gOutput
, "%" PRIxLEAST32
, uint_least32_t(0x2a2a2a));
338 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a2a"));
341 SprintfLiteral(gOutput
, "%" PRIXLEAST32
, uint_least32_t(0xCDCDCD));
342 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCDCD"));
345 static void TestPrintUnsignedLeast64() {
347 SprintfLiteral(gOutput
, "%" PRIoLEAST64
,
348 uint_least64_t(UINT64_C(0424242424242)));
349 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "424242424242"));
352 SprintfLiteral(gOutput
, "%" PRIuLEAST64
,
353 uint_least64_t(UINT64_C(17171717171717171717)));
354 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "17171717171717171717"));
357 SprintfLiteral(gOutput
, "%" PRIxLEAST64
,
358 uint_least64_t(UINT64_C(0x2a2a2a2a2a2a2a)));
359 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a2a2a2a2a2a"));
362 SprintfLiteral(gOutput
, "%" PRIXLEAST64
,
363 uint_least64_t(UINT64_C(0xCDCDCDCDCDCD)));
364 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCDCDCDCDCD"));
367 static void TestPrintUnsignedLeastN() {
368 TestPrintUnsignedLeast8();
369 TestPrintUnsignedLeast16();
370 TestPrintUnsignedLeast32();
371 TestPrintUnsignedLeast64();
374 static void TestPrintUnsignedFast8() {
376 SprintfLiteral(gOutput
, "%" PRIoFAST8
, uint_fast8_t(042));
377 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "42"));
380 SprintfLiteral(gOutput
, "%" PRIuFAST8
, uint_fast8_t(17));
381 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "17"));
384 SprintfLiteral(gOutput
, "%" PRIxFAST8
, uint_fast8_t(0x2a));
385 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a"));
388 SprintfLiteral(gOutput
, "%" PRIXFAST8
, uint_fast8_t(0xCD));
389 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CD"));
392 static void TestPrintUnsignedFast16() {
394 SprintfLiteral(gOutput
, "%" PRIoFAST16
, uint_fast16_t(04242));
395 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "4242"));
398 SprintfLiteral(gOutput
, "%" PRIuFAST16
, uint_fast16_t(1717));
399 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "1717"));
402 SprintfLiteral(gOutput
, "%" PRIxFAST16
, uint_fast16_t(0x2a2a));
403 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a"));
406 SprintfLiteral(gOutput
, "%" PRIXFAST16
, uint_fast16_t(0xCDCD));
407 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCD"));
410 static void TestPrintUnsignedFast32() {
412 SprintfLiteral(gOutput
, "%" PRIoFAST32
, uint_fast32_t(0424242));
413 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "424242"));
416 SprintfLiteral(gOutput
, "%" PRIuFAST32
, uint_fast32_t(171717));
417 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "171717"));
420 SprintfLiteral(gOutput
, "%" PRIxFAST32
, uint_fast32_t(0x2a2a2a));
421 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a2a"));
424 SprintfLiteral(gOutput
, "%" PRIXFAST32
, uint_fast32_t(0xCDCDCD));
425 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCDCD"));
428 static void TestPrintUnsignedFast64() {
430 SprintfLiteral(gOutput
, "%" PRIoFAST64
,
431 uint_fast64_t(UINT64_C(0424242424242)));
432 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "424242424242"));
435 SprintfLiteral(gOutput
, "%" PRIuFAST64
,
436 uint_fast64_t(UINT64_C(17171717171717171717)));
437 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "17171717171717171717"));
440 SprintfLiteral(gOutput
, "%" PRIxFAST64
,
441 uint_fast64_t(UINT64_C(0x2a2a2a2a2a2a2a)));
442 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "2a2a2a2a2a2a2a"));
445 SprintfLiteral(gOutput
, "%" PRIXFAST64
,
446 uint_fast64_t(UINT64_C(0xCDCDCDCDCDCD)));
447 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "CDCDCDCDCDCD"));
450 static void TestPrintUnsignedFastN() {
451 TestPrintUnsignedFast8();
452 TestPrintUnsignedFast16();
453 TestPrintUnsignedFast32();
454 TestPrintUnsignedFast64();
457 static void TestPrintUnsignedMax() {
459 SprintfLiteral(gOutput
, "%" PRIoMAX
, uintmax_t(UINTMAX_C(432157943248732)));
460 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "14220563454333534"));
463 SprintfLiteral(gOutput
, "%" PRIuMAX
, uintmax_t(UINTMAX_C(325719232983)));
464 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "325719232983"));
467 SprintfLiteral(gOutput
, "%" PRIxMAX
, uintmax_t(UINTMAX_C(327281321873)));
468 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "4c337ca791"));
471 SprintfLiteral(gOutput
, "%" PRIXMAX
, uintmax_t(UINTMAX_C(912389523743523)));
472 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "33DD03D75A323"));
475 static void TestPrintUnsignedPtr() {
477 SprintfLiteral(gOutput
, "%" PRIoPTR
,
478 uintptr_t(reinterpret_cast<void*>(12345678)));
479 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "57060516"));
482 SprintfLiteral(gOutput
, "%" PRIuPTR
,
483 uintptr_t(reinterpret_cast<void*>(87654321)));
484 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "87654321"));
487 SprintfLiteral(gOutput
, "%" PRIxPTR
,
488 uintptr_t(reinterpret_cast<void*>(0x4c3a791)));
489 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "4c3a791"));
492 SprintfLiteral(gOutput
, "%" PRIXPTR
,
493 uintptr_t(reinterpret_cast<void*>(0xF328DB)));
494 MOZ_RELEASE_ASSERT(!strcmp(gOutput
, "F328DB"));
497 static void TestPrintUnsigned() {
498 TestPrintUnsignedN();
499 TestPrintUnsignedLeastN();
500 TestPrintUnsignedFastN();
501 TestPrintUnsignedMax();
502 TestPrintUnsignedPtr();
505 static void TestPrint() {
511 * The fscanf macros for signed integers are:
513 * SCNdN SCNdLEASTN SCNdFASTN SCNdMAX SCNdPTR
514 * SCNiN SCNiLEASTN SCNiFASTN SCNiMAX SCNiPTR
516 * In these names N is the width of the type as described in C99 7.18.1.
520 * MSVC's scanf is insufficiently powerful to implement all the SCN* macros.
521 * Rather than support some subset of them, we instead support none of them.
522 * See the comment at the top of IntegerPrintfMacros.h. But in case we ever do
523 * support them, the following tests should adequately test implementation
524 * correctness. (Indeed, these tests *revealed* MSVC's limitations.)
526 * That said, even if MSVC ever picks up complete support, we still probably
527 * don't want to support these, because of the undefined-behavior issue noted
528 * further down in the comment atop IntegerPrintfMacros.h.
530 #define SHOULD_TEST_SCANF_MACROS 0
532 #if SHOULD_TEST_SCANF_MACROS
535 * glibc header definitions for SCN{d,i,o,u,x}{,LEAST,FAST}8 use the "hh" length
536 * modifier, which is new in C99 (and C++11, by reference). We compile this
537 * file as C++11, so if "hh" is used in these macros, it's standard. But some
538 * versions of gcc wrongly think it isn't and warn about a "non-standard"
539 * modifier. And since these tests mostly exist to verify format-macro/type
540 * consistency (particularly through compiler warnings about incorrect formats),
541 * these warnings are unacceptable. So for now, compile tests for those macros
542 * only if we aren't compiling with gcc.
544 # define SHOULD_TEST_8BIT_FORMAT_MACROS (!(MOZ_IS_GCC))
546 template <typename T
>
549 unsigned char mPun
[16];
552 template <typename T
>
553 static void PoisonInput(Input
<T
>& aInput
) {
554 memset(aInput
.mPun
, 0xDA, sizeof(aInput
.mPun
));
557 template <typename T
>
558 static bool ExtraBitsUntouched(const Input
<T
>& aInput
) {
559 for (size_t i
= sizeof(aInput
.mI
); i
< sizeof(aInput
); i
++) {
560 if (aInput
.mPun
[i
] != 0xDA) {
568 static void TestScanSigned8() {
569 # if SHOULD_TEST_8BIT_FORMAT_MACROS
573 sscanf("-17", "%" SCNd8
, &u
.mI
);
574 MOZ_RELEASE_ASSERT(u
.mI
== -17);
575 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
578 sscanf("042", "%" SCNi8
, &u
.mI
);
579 MOZ_RELEASE_ASSERT(u
.mI
== 042);
580 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
584 static void TestScanSigned16() {
588 sscanf("-1742", "%" SCNd16
, &u
.mI
);
589 MOZ_RELEASE_ASSERT(u
.mI
== -1742);
590 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
593 sscanf("04217", "%" SCNi16
, &u
.mI
);
594 MOZ_RELEASE_ASSERT(u
.mI
== 04217);
595 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
598 static void TestScanSigned32() {
602 sscanf("-174257", "%" SCNd32
, &u
.mI
);
603 MOZ_RELEASE_ASSERT(u
.mI
== -174257);
604 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
607 sscanf("0423571", "%" SCNi32
, &u
.mI
);
608 MOZ_RELEASE_ASSERT(u
.mI
== 0423571);
609 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
612 static void TestScanSigned64() {
616 sscanf("-17425238927232", "%" SCNd64
, &u
.mI
);
617 MOZ_RELEASE_ASSERT(u
.mI
== -INT64_C(17425238927232));
618 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
621 sscanf("042333576571", "%" SCNi64
, &u
.mI
);
622 MOZ_RELEASE_ASSERT(u
.mI
== INT64_C(042333576571));
623 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
626 static void TestScanSignedN() {
633 static void TestScanSignedLeast8() {
634 # if SHOULD_TEST_8BIT_FORMAT_MACROS
635 Input
<int_least8_t> u
;
638 sscanf("-17", "%" SCNdLEAST8
, &u
.mI
);
639 MOZ_RELEASE_ASSERT(u
.mI
== -17);
640 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
643 sscanf("042", "%" SCNiLEAST8
, &u
.mI
);
644 MOZ_RELEASE_ASSERT(u
.mI
== 042);
645 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
649 static void TestScanSignedLeast16() {
650 Input
<int_least16_t> u
;
653 sscanf("-1742", "%" SCNdLEAST16
, &u
.mI
);
654 MOZ_RELEASE_ASSERT(u
.mI
== -1742);
655 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
658 sscanf("04217", "%" SCNiLEAST16
, &u
.mI
);
659 MOZ_RELEASE_ASSERT(u
.mI
== 04217);
660 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
663 static void TestScanSignedLeast32() {
664 Input
<int_least32_t> u
;
667 sscanf("-174257", "%" SCNdLEAST32
, &u
.mI
);
668 MOZ_RELEASE_ASSERT(u
.mI
== -174257);
669 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
672 sscanf("0423571", "%" SCNiLEAST32
, &u
.mI
);
673 MOZ_RELEASE_ASSERT(u
.mI
== 0423571);
674 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
677 static void TestScanSignedLeast64() {
678 Input
<int_least64_t> u
;
681 sscanf("-17425238927232", "%" SCNdLEAST64
, &u
.mI
);
682 MOZ_RELEASE_ASSERT(u
.mI
== -INT64_C(17425238927232));
683 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
686 sscanf("042333576571", "%" SCNiLEAST64
, &u
.mI
);
687 MOZ_RELEASE_ASSERT(u
.mI
== INT64_C(042333576571));
688 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
691 static void TestScanSignedLeastN() {
692 TestScanSignedLeast8();
693 TestScanSignedLeast16();
694 TestScanSignedLeast32();
695 TestScanSignedLeast64();
698 static void TestScanSignedFast8() {
699 # if SHOULD_TEST_8BIT_FORMAT_MACROS
700 Input
<int_fast8_t> u
;
703 sscanf("-17", "%" SCNdFAST8
, &u
.mI
);
704 MOZ_RELEASE_ASSERT(u
.mI
== -17);
705 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
708 sscanf("042", "%" SCNiFAST8
, &u
.mI
);
709 MOZ_RELEASE_ASSERT(u
.mI
== 042);
710 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
714 static void TestScanSignedFast16() {
715 Input
<int_fast16_t> u
;
718 sscanf("-1742", "%" SCNdFAST16
, &u
.mI
);
719 MOZ_RELEASE_ASSERT(u
.mI
== -1742);
720 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
723 sscanf("04217", "%" SCNiFAST16
, &u
.mI
);
724 MOZ_RELEASE_ASSERT(u
.mI
== 04217);
725 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
728 static void TestScanSignedFast32() {
729 Input
<int_fast32_t> u
;
732 sscanf("-174257", "%" SCNdFAST32
, &u
.mI
);
733 MOZ_RELEASE_ASSERT(u
.mI
== -174257);
734 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
737 sscanf("0423571", "%" SCNiFAST32
, &u
.mI
);
738 MOZ_RELEASE_ASSERT(u
.mI
== 0423571);
739 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
742 static void TestScanSignedFast64() {
743 Input
<int_fast64_t> u
;
746 sscanf("-17425238927232", "%" SCNdFAST64
, &u
.mI
);
747 MOZ_RELEASE_ASSERT(u
.mI
== -INT64_C(17425238927232));
748 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
751 sscanf("042333576571", "%" SCNiFAST64
, &u
.mI
);
752 MOZ_RELEASE_ASSERT(u
.mI
== INT64_C(042333576571));
753 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
756 static void TestScanSignedFastN() {
757 TestScanSignedFast8();
758 TestScanSignedFast16();
759 TestScanSignedFast32();
760 TestScanSignedFast64();
763 static void TestScanSignedMax() {
767 sscanf("-432157943248732", "%" SCNdMAX
, &u
.mI
);
768 MOZ_RELEASE_ASSERT(u
.mI
== -INTMAX_C(432157943248732));
769 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
772 sscanf("04233357236571", "%" SCNiMAX
, &u
.mI
);
773 MOZ_RELEASE_ASSERT(u
.mI
== INTMAX_C(04233357236571));
774 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
777 static void TestScanSignedPtr() {
781 sscanf("12345678", "%" SCNdPTR
, &u
.mI
);
782 MOZ_RELEASE_ASSERT(u
.mI
== intptr_t(reinterpret_cast<void*>(12345678)));
783 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
786 sscanf("04233357236", "%" SCNiPTR
, &u
.mI
);
787 MOZ_RELEASE_ASSERT(u
.mI
== intptr_t(reinterpret_cast<void*>(04233357236)));
788 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
791 static void TestScanSigned() {
793 TestScanSignedLeastN();
794 TestScanSignedFastN();
800 * The fscanf macros for unsigned integers are:
802 * SCNoN SCNoLEASTN SCNoFASTN SCNoMAX SCNoPTR
803 * SCNuN SCNuLEASTN SCNuFASTN SCNuMAX SCNuPTR
804 * SCNxN SCNxLEASTN SCNxFASTN SCNxMAX SCNxPTR
806 * In these names N is the width of the type as described in C99 7.18.1.
809 static void TestScanUnsigned8() {
810 # if SHOULD_TEST_8BIT_FORMAT_MACROS
814 sscanf("17", "%" SCNo8
, &u
.mI
);
815 MOZ_RELEASE_ASSERT(u
.mI
== 017);
816 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
819 sscanf("42", "%" SCNu8
, &u
.mI
);
820 MOZ_RELEASE_ASSERT(u
.mI
== 42);
821 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
824 sscanf("2A", "%" SCNx8
, &u
.mI
);
825 MOZ_RELEASE_ASSERT(u
.mI
== 0x2A);
826 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
830 static void TestScanUnsigned16() {
834 sscanf("1742", "%" SCNo16
, &u
.mI
);
835 MOZ_RELEASE_ASSERT(u
.mI
== 01742);
836 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
839 sscanf("4217", "%" SCNu16
, &u
.mI
);
840 MOZ_RELEASE_ASSERT(u
.mI
== 4217);
841 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
844 sscanf("2ABC", "%" SCNx16
, &u
.mI
);
845 MOZ_RELEASE_ASSERT(u
.mI
== 0x2ABC);
846 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
849 static void TestScanUnsigned32() {
853 sscanf("17421742", "%" SCNo32
, &u
.mI
);
854 MOZ_RELEASE_ASSERT(u
.mI
== 017421742);
855 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
858 sscanf("4217867", "%" SCNu32
, &u
.mI
);
859 MOZ_RELEASE_ASSERT(u
.mI
== 4217867);
860 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
863 sscanf("2ABCBEEF", "%" SCNx32
, &u
.mI
);
864 MOZ_RELEASE_ASSERT(u
.mI
== 0x2ABCBEEF);
865 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
868 static void TestScanUnsigned64() {
872 sscanf("17421742173", "%" SCNo64
, &u
.mI
);
873 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(017421742173));
874 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
877 sscanf("421786713579", "%" SCNu64
, &u
.mI
);
878 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(421786713579));
879 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
882 sscanf("DEADBEEF7457E", "%" SCNx64
, &u
.mI
);
883 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(0xDEADBEEF7457E));
884 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
887 static void TestScanUnsignedN() {
889 TestScanUnsigned16();
890 TestScanUnsigned32();
891 TestScanUnsigned64();
894 static void TestScanUnsignedLeast8() {
895 # if SHOULD_TEST_8BIT_FORMAT_MACROS
896 Input
<uint_least8_t> u
;
899 sscanf("17", "%" SCNoLEAST8
, &u
.mI
);
900 MOZ_RELEASE_ASSERT(u
.mI
== 017);
901 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
904 sscanf("42", "%" SCNuLEAST8
, &u
.mI
);
905 MOZ_RELEASE_ASSERT(u
.mI
== 42);
906 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
909 sscanf("2A", "%" SCNxLEAST8
, &u
.mI
);
910 MOZ_RELEASE_ASSERT(u
.mI
== 0x2A);
911 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
915 static void TestScanUnsignedLeast16() {
916 Input
<uint_least16_t> u
;
919 sscanf("1742", "%" SCNoLEAST16
, &u
.mI
);
920 MOZ_RELEASE_ASSERT(u
.mI
== 01742);
921 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
924 sscanf("4217", "%" SCNuLEAST16
, &u
.mI
);
925 MOZ_RELEASE_ASSERT(u
.mI
== 4217);
926 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
929 sscanf("2ABC", "%" SCNxLEAST16
, &u
.mI
);
930 MOZ_RELEASE_ASSERT(u
.mI
== 0x2ABC);
931 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
934 static void TestScanUnsignedLeast32() {
935 Input
<uint_least32_t> u
;
938 sscanf("17421742", "%" SCNoLEAST32
, &u
.mI
);
939 MOZ_RELEASE_ASSERT(u
.mI
== 017421742);
940 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
943 sscanf("4217867", "%" SCNuLEAST32
, &u
.mI
);
944 MOZ_RELEASE_ASSERT(u
.mI
== 4217867);
945 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
948 sscanf("2ABCBEEF", "%" SCNxLEAST32
, &u
.mI
);
949 MOZ_RELEASE_ASSERT(u
.mI
== 0x2ABCBEEF);
950 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
953 static void TestScanUnsignedLeast64() {
954 Input
<uint_least64_t> u
;
957 sscanf("17421742173", "%" SCNoLEAST64
, &u
.mI
);
958 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(017421742173));
959 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
962 sscanf("421786713579", "%" SCNuLEAST64
, &u
.mI
);
963 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(421786713579));
964 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
967 sscanf("DEADBEEF7457E", "%" SCNxLEAST64
, &u
.mI
);
968 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(0xDEADBEEF7457E));
969 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
972 static void TestScanUnsignedLeastN() {
973 TestScanUnsignedLeast8();
974 TestScanUnsignedLeast16();
975 TestScanUnsignedLeast32();
976 TestScanUnsignedLeast64();
979 static void TestScanUnsignedFast8() {
980 # if SHOULD_TEST_8BIT_FORMAT_MACROS
981 Input
<uint_fast8_t> u
;
984 sscanf("17", "%" SCNoFAST8
, &u
.mI
);
985 MOZ_RELEASE_ASSERT(u
.mI
== 017);
986 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
989 sscanf("42", "%" SCNuFAST8
, &u
.mI
);
990 MOZ_RELEASE_ASSERT(u
.mI
== 42);
991 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
994 sscanf("2A", "%" SCNxFAST8
, &u
.mI
);
995 MOZ_RELEASE_ASSERT(u
.mI
== 0x2A);
996 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1000 static void TestScanUnsignedFast16() {
1001 Input
<uint_fast16_t> u
;
1004 sscanf("1742", "%" SCNoFAST16
, &u
.mI
);
1005 MOZ_RELEASE_ASSERT(u
.mI
== 01742);
1006 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1009 sscanf("4217", "%" SCNuFAST16
, &u
.mI
);
1010 MOZ_RELEASE_ASSERT(u
.mI
== 4217);
1011 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1014 sscanf("2ABC", "%" SCNxFAST16
, &u
.mI
);
1015 MOZ_RELEASE_ASSERT(u
.mI
== 0x2ABC);
1016 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1019 static void TestScanUnsignedFast32() {
1020 Input
<uint_fast32_t> u
;
1023 sscanf("17421742", "%" SCNoFAST32
, &u
.mI
);
1024 MOZ_RELEASE_ASSERT(u
.mI
== 017421742);
1025 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1028 sscanf("4217867", "%" SCNuFAST32
, &u
.mI
);
1029 MOZ_RELEASE_ASSERT(u
.mI
== 4217867);
1030 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1033 sscanf("2ABCBEEF", "%" SCNxFAST32
, &u
.mI
);
1034 MOZ_RELEASE_ASSERT(u
.mI
== 0x2ABCBEEF);
1035 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1038 static void TestScanUnsignedFast64() {
1039 Input
<uint_fast64_t> u
;
1042 sscanf("17421742173", "%" SCNoFAST64
, &u
.mI
);
1043 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(017421742173));
1044 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1047 sscanf("421786713579", "%" SCNuFAST64
, &u
.mI
);
1048 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(421786713579));
1049 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1052 sscanf("DEADBEEF7457E", "%" SCNxFAST64
, &u
.mI
);
1053 MOZ_RELEASE_ASSERT(u
.mI
== UINT64_C(0xDEADBEEF7457E));
1054 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1057 static void TestScanUnsignedFastN() {
1058 TestScanUnsignedFast8();
1059 TestScanUnsignedFast16();
1060 TestScanUnsignedFast32();
1061 TestScanUnsignedFast64();
1064 static void TestScanUnsignedMax() {
1068 sscanf("14220563454333534", "%" SCNoMAX
, &u
.mI
);
1069 MOZ_RELEASE_ASSERT(u
.mI
== UINTMAX_C(432157943248732));
1070 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1073 sscanf("432157943248732", "%" SCNuMAX
, &u
.mI
);
1074 MOZ_RELEASE_ASSERT(u
.mI
== UINTMAX_C(432157943248732));
1075 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1078 sscanf("4c337ca791", "%" SCNxMAX
, &u
.mI
);
1079 MOZ_RELEASE_ASSERT(u
.mI
== UINTMAX_C(327281321873));
1080 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1083 static void TestScanUnsignedPtr() {
1087 sscanf("57060516", "%" SCNoPTR
, &u
.mI
);
1088 MOZ_RELEASE_ASSERT(u
.mI
== uintptr_t(reinterpret_cast<void*>(12345678)));
1089 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1092 sscanf("87654321", "%" SCNuPTR
, &u
.mI
);
1093 MOZ_RELEASE_ASSERT(u
.mI
== uintptr_t(reinterpret_cast<void*>(87654321)));
1094 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1097 sscanf("4c3a791", "%" SCNxPTR
, &u
.mI
);
1098 MOZ_RELEASE_ASSERT(u
.mI
== uintptr_t(reinterpret_cast<void*>(0x4c3a791)));
1099 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u
));
1102 static void TestScanUnsigned() {
1103 TestScanUnsignedN();
1104 TestScanUnsignedLeastN();
1105 TestScanUnsignedFastN();
1106 TestScanUnsignedMax();
1107 TestScanUnsignedPtr();
1110 static void TestScan() {
1115 #endif /* SHOULD_TEST_SCANF_MACROS */
1121 #endif // defined(XP_WIN)
1124 #if SHOULD_TEST_SCANF_MACROS