|
1 /* -*- Mode: C++; tab-width: 2; 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 file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "mozilla/Assertions.h" |
|
7 #include "mozilla/IntegerPrintfMacros.h" // this must pick up <stdint.h> |
|
8 |
|
9 #include <stddef.h> |
|
10 #include <stdio.h> |
|
11 #include <string.h> |
|
12 |
|
13 /* Output array and poisoning method shared by all tests. */ |
|
14 static char output[32]; |
|
15 |
|
16 static void |
|
17 PoisonOutput() |
|
18 { |
|
19 memset(output, 0xDA, sizeof(output)); |
|
20 } |
|
21 |
|
22 /* |
|
23 * The fprintf macros for signed integers are: |
|
24 * |
|
25 * PRIdN PRIdLEASTN PRIdFASTN PRIdMAX PRIdPTR |
|
26 * PRIiN PRIiLEASTN PRIiFASTN PRIiMAX PRIiPTR |
|
27 * |
|
28 * In these names N is the width of the type as described in C99 7.18.1. |
|
29 */ |
|
30 |
|
31 static void |
|
32 TestPrintSigned8() |
|
33 { |
|
34 PoisonOutput(); |
|
35 sprintf(output, "%" PRId8, int8_t(-17)); |
|
36 MOZ_RELEASE_ASSERT(!strcmp(output, "-17")); |
|
37 |
|
38 PoisonOutput(); |
|
39 sprintf(output, "%" PRIi8, int8_t(42)); |
|
40 MOZ_RELEASE_ASSERT(!strcmp(output, "42")); |
|
41 } |
|
42 |
|
43 static void |
|
44 TestPrintSigned16() |
|
45 { |
|
46 PoisonOutput(); |
|
47 sprintf(output, "%" PRId16, int16_t(-289)); |
|
48 MOZ_RELEASE_ASSERT(!strcmp(output, "-289")); |
|
49 |
|
50 PoisonOutput(); |
|
51 sprintf(output, "%" PRIi16, int16_t(728)); |
|
52 MOZ_RELEASE_ASSERT(!strcmp(output, "728")); |
|
53 } |
|
54 |
|
55 static void |
|
56 TestPrintSigned32() |
|
57 { |
|
58 PoisonOutput(); |
|
59 sprintf(output, "%" PRId32, int32_t(-342178)); |
|
60 MOZ_RELEASE_ASSERT(!strcmp(output, "-342178")); |
|
61 |
|
62 PoisonOutput(); |
|
63 sprintf(output, "%" PRIi32, int32_t(5719283)); |
|
64 MOZ_RELEASE_ASSERT(!strcmp(output, "5719283")); |
|
65 } |
|
66 |
|
67 static void |
|
68 TestPrintSigned64() |
|
69 { |
|
70 PoisonOutput(); |
|
71 sprintf(output, "%" PRId64, int64_t(-INT64_C(432157943248732))); |
|
72 MOZ_RELEASE_ASSERT(!strcmp(output, "-432157943248732")); |
|
73 |
|
74 PoisonOutput(); |
|
75 sprintf(output, "%" PRIi64, int64_t(INT64_C(325719232983))); |
|
76 MOZ_RELEASE_ASSERT(!strcmp(output, "325719232983")); |
|
77 } |
|
78 |
|
79 static void |
|
80 TestPrintSignedN() |
|
81 { |
|
82 TestPrintSigned8(); |
|
83 TestPrintSigned16(); |
|
84 TestPrintSigned32(); |
|
85 TestPrintSigned64(); |
|
86 } |
|
87 |
|
88 static void |
|
89 TestPrintSignedLeast8() |
|
90 { |
|
91 PoisonOutput(); |
|
92 sprintf(output, "%" PRIdLEAST8, int_least8_t(-17)); |
|
93 MOZ_RELEASE_ASSERT(!strcmp(output, "-17")); |
|
94 |
|
95 PoisonOutput(); |
|
96 sprintf(output, "%" PRIiLEAST8, int_least8_t(42)); |
|
97 MOZ_RELEASE_ASSERT(!strcmp(output, "42")); |
|
98 } |
|
99 |
|
100 static void |
|
101 TestPrintSignedLeast16() |
|
102 { |
|
103 PoisonOutput(); |
|
104 sprintf(output, "%" PRIdLEAST16, int_least16_t(-289)); |
|
105 MOZ_RELEASE_ASSERT(!strcmp(output, "-289")); |
|
106 |
|
107 PoisonOutput(); |
|
108 sprintf(output, "%" PRIiLEAST16, int_least16_t(728)); |
|
109 MOZ_RELEASE_ASSERT(!strcmp(output, "728")); |
|
110 } |
|
111 |
|
112 static void |
|
113 TestPrintSignedLeast32() |
|
114 { |
|
115 PoisonOutput(); |
|
116 sprintf(output, "%" PRIdLEAST32, int_least32_t(-342178)); |
|
117 MOZ_RELEASE_ASSERT(!strcmp(output, "-342178")); |
|
118 |
|
119 PoisonOutput(); |
|
120 sprintf(output, "%" PRIiLEAST32, int_least32_t(5719283)); |
|
121 MOZ_RELEASE_ASSERT(!strcmp(output, "5719283")); |
|
122 } |
|
123 |
|
124 static void |
|
125 TestPrintSignedLeast64() |
|
126 { |
|
127 PoisonOutput(); |
|
128 sprintf(output, "%" PRIdLEAST64, int_least64_t(-INT64_C(432157943248732))); |
|
129 MOZ_RELEASE_ASSERT(!strcmp(output, "-432157943248732")); |
|
130 |
|
131 PoisonOutput(); |
|
132 sprintf(output, "%" PRIiLEAST64, int_least64_t(INT64_C(325719232983))); |
|
133 MOZ_RELEASE_ASSERT(!strcmp(output, "325719232983")); |
|
134 } |
|
135 |
|
136 static void |
|
137 TestPrintSignedLeastN() |
|
138 { |
|
139 TestPrintSignedLeast8(); |
|
140 TestPrintSignedLeast16(); |
|
141 TestPrintSignedLeast32(); |
|
142 TestPrintSignedLeast64(); |
|
143 } |
|
144 |
|
145 static void |
|
146 TestPrintSignedFast8() |
|
147 { |
|
148 PoisonOutput(); |
|
149 sprintf(output, "%" PRIdFAST8, int_fast8_t(-17)); |
|
150 MOZ_RELEASE_ASSERT(!strcmp(output, "-17")); |
|
151 |
|
152 PoisonOutput(); |
|
153 sprintf(output, "%" PRIiFAST8, int_fast8_t(42)); |
|
154 MOZ_RELEASE_ASSERT(!strcmp(output, "42")); |
|
155 } |
|
156 |
|
157 static void |
|
158 TestPrintSignedFast16() |
|
159 { |
|
160 PoisonOutput(); |
|
161 sprintf(output, "%" PRIdFAST16, int_fast16_t(-289)); |
|
162 MOZ_RELEASE_ASSERT(!strcmp(output, "-289")); |
|
163 |
|
164 PoisonOutput(); |
|
165 sprintf(output, "%" PRIiFAST16, int_fast16_t(728)); |
|
166 MOZ_RELEASE_ASSERT(!strcmp(output, "728")); |
|
167 } |
|
168 |
|
169 static void |
|
170 TestPrintSignedFast32() |
|
171 { |
|
172 PoisonOutput(); |
|
173 sprintf(output, "%" PRIdFAST32, int_fast32_t(-342178)); |
|
174 MOZ_RELEASE_ASSERT(!strcmp(output, "-342178")); |
|
175 |
|
176 PoisonOutput(); |
|
177 sprintf(output, "%" PRIiFAST32, int_fast32_t(5719283)); |
|
178 MOZ_RELEASE_ASSERT(!strcmp(output, "5719283")); |
|
179 } |
|
180 |
|
181 static void |
|
182 TestPrintSignedFast64() |
|
183 { |
|
184 PoisonOutput(); |
|
185 sprintf(output, "%" PRIdFAST64, int_fast64_t(-INT64_C(432157943248732))); |
|
186 MOZ_RELEASE_ASSERT(!strcmp(output, "-432157943248732")); |
|
187 |
|
188 PoisonOutput(); |
|
189 sprintf(output, "%" PRIiFAST64, int_fast64_t(INT64_C(325719232983))); |
|
190 MOZ_RELEASE_ASSERT(!strcmp(output, "325719232983")); |
|
191 } |
|
192 |
|
193 static void |
|
194 TestPrintSignedFastN() |
|
195 { |
|
196 TestPrintSignedFast8(); |
|
197 TestPrintSignedFast16(); |
|
198 TestPrintSignedFast32(); |
|
199 TestPrintSignedFast64(); |
|
200 } |
|
201 |
|
202 static void |
|
203 TestPrintSignedMax() |
|
204 { |
|
205 PoisonOutput(); |
|
206 sprintf(output, "%" PRIdMAX, intmax_t(-INTMAX_C(432157943248732))); |
|
207 MOZ_RELEASE_ASSERT(!strcmp(output, "-432157943248732")); |
|
208 |
|
209 PoisonOutput(); |
|
210 sprintf(output, "%" PRIiMAX, intmax_t(INTMAX_C(325719232983))); |
|
211 MOZ_RELEASE_ASSERT(!strcmp(output, "325719232983")); |
|
212 } |
|
213 |
|
214 static void |
|
215 TestPrintSignedPtr() |
|
216 { |
|
217 PoisonOutput(); |
|
218 sprintf(output, "%" PRIdPTR, intptr_t(reinterpret_cast<void*>(12345678))); |
|
219 MOZ_RELEASE_ASSERT(!strcmp(output, "12345678")); |
|
220 |
|
221 PoisonOutput(); |
|
222 sprintf(output, "%" PRIiPTR, intptr_t(reinterpret_cast<void*>(87654321))); |
|
223 MOZ_RELEASE_ASSERT(!strcmp(output, "87654321")); |
|
224 } |
|
225 |
|
226 static void |
|
227 TestPrintSigned() |
|
228 { |
|
229 TestPrintSignedN(); |
|
230 TestPrintSignedLeastN(); |
|
231 TestPrintSignedFastN(); |
|
232 TestPrintSignedMax(); |
|
233 TestPrintSignedPtr(); |
|
234 } |
|
235 |
|
236 /* |
|
237 * The fprintf macros for unsigned integers are: |
|
238 * |
|
239 * PRIoN PRIoLEASTN PRIoFASTN PRIoMAX PRIoPTR |
|
240 * PRIuN PRIuLEASTN PRIuFASTN PRIuMAX PRIuPTR |
|
241 * PRIxN PRIxLEASTN PRIxFASTN PRIxMAX PRIxPTR |
|
242 * PRIXN PRIXLEASTN PRIXFASTN PRIXMAX PRIXPTR |
|
243 * |
|
244 * In these names N is the width of the type as described in C99 7.18.1. |
|
245 */ |
|
246 |
|
247 static void |
|
248 TestPrintUnsigned8() |
|
249 { |
|
250 PoisonOutput(); |
|
251 sprintf(output, "%" PRIo8, uint8_t(042)); |
|
252 MOZ_RELEASE_ASSERT(!strcmp(output, "42")); |
|
253 |
|
254 PoisonOutput(); |
|
255 sprintf(output, "%" PRIu8, uint8_t(17)); |
|
256 MOZ_RELEASE_ASSERT(!strcmp(output, "17")); |
|
257 |
|
258 PoisonOutput(); |
|
259 sprintf(output, "%" PRIx8, uint8_t(0x2a)); |
|
260 MOZ_RELEASE_ASSERT(!strcmp(output, "2a")); |
|
261 |
|
262 PoisonOutput(); |
|
263 sprintf(output, "%" PRIX8, uint8_t(0xCD)); |
|
264 MOZ_RELEASE_ASSERT(!strcmp(output, "CD")); |
|
265 } |
|
266 |
|
267 static void |
|
268 TestPrintUnsigned16() |
|
269 { |
|
270 PoisonOutput(); |
|
271 sprintf(output, "%" PRIo16, uint16_t(04242)); |
|
272 MOZ_RELEASE_ASSERT(!strcmp(output, "4242")); |
|
273 |
|
274 PoisonOutput(); |
|
275 sprintf(output, "%" PRIu16, uint16_t(1717)); |
|
276 MOZ_RELEASE_ASSERT(!strcmp(output, "1717")); |
|
277 |
|
278 PoisonOutput(); |
|
279 sprintf(output, "%" PRIx16, uint16_t(0x2a2a)); |
|
280 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a")); |
|
281 |
|
282 PoisonOutput(); |
|
283 sprintf(output, "%" PRIX16, uint16_t(0xCDCD)); |
|
284 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCD")); |
|
285 } |
|
286 |
|
287 static void |
|
288 TestPrintUnsigned32() |
|
289 { |
|
290 PoisonOutput(); |
|
291 sprintf(output, "%" PRIo32, uint32_t(0424242)); |
|
292 MOZ_RELEASE_ASSERT(!strcmp(output, "424242")); |
|
293 |
|
294 PoisonOutput(); |
|
295 sprintf(output, "%" PRIu32, uint32_t(171717)); |
|
296 MOZ_RELEASE_ASSERT(!strcmp(output, "171717")); |
|
297 |
|
298 PoisonOutput(); |
|
299 sprintf(output, "%" PRIx32, uint32_t(0x2a2a2a)); |
|
300 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a2a")); |
|
301 |
|
302 PoisonOutput(); |
|
303 sprintf(output, "%" PRIX32, uint32_t(0xCDCDCD)); |
|
304 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCDCD")); |
|
305 } |
|
306 |
|
307 static void |
|
308 TestPrintUnsigned64() |
|
309 { |
|
310 PoisonOutput(); |
|
311 sprintf(output, "%" PRIo64, uint64_t(UINT64_C(0424242424242))); |
|
312 MOZ_RELEASE_ASSERT(!strcmp(output, "424242424242")); |
|
313 |
|
314 PoisonOutput(); |
|
315 sprintf(output, "%" PRIu64, uint64_t(UINT64_C(17171717171717171717))); |
|
316 MOZ_RELEASE_ASSERT(!strcmp(output, "17171717171717171717")); |
|
317 |
|
318 PoisonOutput(); |
|
319 sprintf(output, "%" PRIx64, uint64_t(UINT64_C(0x2a2a2a2a2a2a2a))); |
|
320 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a2a2a2a2a2a")); |
|
321 |
|
322 PoisonOutput(); |
|
323 sprintf(output, "%" PRIX64, uint64_t(UINT64_C(0xCDCDCDCDCDCD))); |
|
324 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCDCDCDCDCD")); |
|
325 } |
|
326 |
|
327 static void |
|
328 TestPrintUnsignedN() |
|
329 { |
|
330 TestPrintUnsigned8(); |
|
331 TestPrintUnsigned16(); |
|
332 TestPrintUnsigned32(); |
|
333 TestPrintUnsigned64(); |
|
334 } |
|
335 |
|
336 static void |
|
337 TestPrintUnsignedLeast8() |
|
338 { |
|
339 PoisonOutput(); |
|
340 sprintf(output, "%" PRIoLEAST8, uint_least8_t(042)); |
|
341 MOZ_RELEASE_ASSERT(!strcmp(output, "42")); |
|
342 |
|
343 PoisonOutput(); |
|
344 sprintf(output, "%" PRIuLEAST8, uint_least8_t(17)); |
|
345 MOZ_RELEASE_ASSERT(!strcmp(output, "17")); |
|
346 |
|
347 PoisonOutput(); |
|
348 sprintf(output, "%" PRIxLEAST8, uint_least8_t(0x2a)); |
|
349 MOZ_RELEASE_ASSERT(!strcmp(output, "2a")); |
|
350 |
|
351 PoisonOutput(); |
|
352 sprintf(output, "%" PRIXLEAST8, uint_least8_t(0xCD)); |
|
353 MOZ_RELEASE_ASSERT(!strcmp(output, "CD")); |
|
354 } |
|
355 |
|
356 static void |
|
357 TestPrintUnsignedLeast16() |
|
358 { |
|
359 PoisonOutput(); |
|
360 sprintf(output, "%" PRIoLEAST16, uint_least16_t(04242)); |
|
361 MOZ_RELEASE_ASSERT(!strcmp(output, "4242")); |
|
362 |
|
363 PoisonOutput(); |
|
364 sprintf(output, "%" PRIuLEAST16, uint_least16_t(1717)); |
|
365 MOZ_RELEASE_ASSERT(!strcmp(output, "1717")); |
|
366 |
|
367 PoisonOutput(); |
|
368 sprintf(output, "%" PRIxLEAST16, uint_least16_t(0x2a2a)); |
|
369 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a")); |
|
370 |
|
371 PoisonOutput(); |
|
372 sprintf(output, "%" PRIXLEAST16, uint_least16_t(0xCDCD)); |
|
373 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCD")); |
|
374 } |
|
375 |
|
376 static void |
|
377 TestPrintUnsignedLeast32() |
|
378 { |
|
379 PoisonOutput(); |
|
380 sprintf(output, "%" PRIoLEAST32, uint_least32_t(0424242)); |
|
381 MOZ_RELEASE_ASSERT(!strcmp(output, "424242")); |
|
382 |
|
383 PoisonOutput(); |
|
384 sprintf(output, "%" PRIuLEAST32, uint_least32_t(171717)); |
|
385 MOZ_RELEASE_ASSERT(!strcmp(output, "171717")); |
|
386 |
|
387 PoisonOutput(); |
|
388 sprintf(output, "%" PRIxLEAST32, uint_least32_t(0x2a2a2a)); |
|
389 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a2a")); |
|
390 |
|
391 PoisonOutput(); |
|
392 sprintf(output, "%" PRIXLEAST32, uint_least32_t(0xCDCDCD)); |
|
393 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCDCD")); |
|
394 } |
|
395 |
|
396 static void |
|
397 TestPrintUnsignedLeast64() |
|
398 { |
|
399 PoisonOutput(); |
|
400 sprintf(output, "%" PRIoLEAST64, uint_least64_t(UINT64_C(0424242424242))); |
|
401 MOZ_RELEASE_ASSERT(!strcmp(output, "424242424242")); |
|
402 |
|
403 PoisonOutput(); |
|
404 sprintf(output, "%" PRIuLEAST64, uint_least64_t(UINT64_C(17171717171717171717))); |
|
405 MOZ_RELEASE_ASSERT(!strcmp(output, "17171717171717171717")); |
|
406 |
|
407 PoisonOutput(); |
|
408 sprintf(output, "%" PRIxLEAST64, uint_least64_t(UINT64_C(0x2a2a2a2a2a2a2a))); |
|
409 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a2a2a2a2a2a")); |
|
410 |
|
411 PoisonOutput(); |
|
412 sprintf(output, "%" PRIXLEAST64, uint_least64_t(UINT64_C(0xCDCDCDCDCDCD))); |
|
413 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCDCDCDCDCD")); |
|
414 } |
|
415 |
|
416 static void |
|
417 TestPrintUnsignedLeastN() |
|
418 { |
|
419 TestPrintUnsignedLeast8(); |
|
420 TestPrintUnsignedLeast16(); |
|
421 TestPrintUnsignedLeast32(); |
|
422 TestPrintUnsignedLeast64(); |
|
423 } |
|
424 |
|
425 static void |
|
426 TestPrintUnsignedFast8() |
|
427 { |
|
428 PoisonOutput(); |
|
429 sprintf(output, "%" PRIoFAST8, uint_fast8_t(042)); |
|
430 MOZ_RELEASE_ASSERT(!strcmp(output, "42")); |
|
431 |
|
432 PoisonOutput(); |
|
433 sprintf(output, "%" PRIuFAST8, uint_fast8_t(17)); |
|
434 MOZ_RELEASE_ASSERT(!strcmp(output, "17")); |
|
435 |
|
436 PoisonOutput(); |
|
437 sprintf(output, "%" PRIxFAST8, uint_fast8_t(0x2a)); |
|
438 MOZ_RELEASE_ASSERT(!strcmp(output, "2a")); |
|
439 |
|
440 PoisonOutput(); |
|
441 sprintf(output, "%" PRIXFAST8, uint_fast8_t(0xCD)); |
|
442 MOZ_RELEASE_ASSERT(!strcmp(output, "CD")); |
|
443 } |
|
444 |
|
445 static void |
|
446 TestPrintUnsignedFast16() |
|
447 { |
|
448 PoisonOutput(); |
|
449 sprintf(output, "%" PRIoFAST16, uint_fast16_t(04242)); |
|
450 MOZ_RELEASE_ASSERT(!strcmp(output, "4242")); |
|
451 |
|
452 PoisonOutput(); |
|
453 sprintf(output, "%" PRIuFAST16, uint_fast16_t(1717)); |
|
454 MOZ_RELEASE_ASSERT(!strcmp(output, "1717")); |
|
455 |
|
456 PoisonOutput(); |
|
457 sprintf(output, "%" PRIxFAST16, uint_fast16_t(0x2a2a)); |
|
458 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a")); |
|
459 |
|
460 PoisonOutput(); |
|
461 sprintf(output, "%" PRIXFAST16, uint_fast16_t(0xCDCD)); |
|
462 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCD")); |
|
463 } |
|
464 |
|
465 static void |
|
466 TestPrintUnsignedFast32() |
|
467 { |
|
468 PoisonOutput(); |
|
469 sprintf(output, "%" PRIoFAST32, uint_fast32_t(0424242)); |
|
470 MOZ_RELEASE_ASSERT(!strcmp(output, "424242")); |
|
471 |
|
472 PoisonOutput(); |
|
473 sprintf(output, "%" PRIuFAST32, uint_fast32_t(171717)); |
|
474 MOZ_RELEASE_ASSERT(!strcmp(output, "171717")); |
|
475 |
|
476 PoisonOutput(); |
|
477 sprintf(output, "%" PRIxFAST32, uint_fast32_t(0x2a2a2a)); |
|
478 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a2a")); |
|
479 |
|
480 PoisonOutput(); |
|
481 sprintf(output, "%" PRIXFAST32, uint_fast32_t(0xCDCDCD)); |
|
482 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCDCD")); |
|
483 } |
|
484 |
|
485 static void |
|
486 TestPrintUnsignedFast64() |
|
487 { |
|
488 PoisonOutput(); |
|
489 sprintf(output, "%" PRIoFAST64, uint_fast64_t(UINT64_C(0424242424242))); |
|
490 MOZ_RELEASE_ASSERT(!strcmp(output, "424242424242")); |
|
491 |
|
492 PoisonOutput(); |
|
493 sprintf(output, "%" PRIuFAST64, uint_fast64_t(UINT64_C(17171717171717171717))); |
|
494 MOZ_RELEASE_ASSERT(!strcmp(output, "17171717171717171717")); |
|
495 |
|
496 PoisonOutput(); |
|
497 sprintf(output, "%" PRIxFAST64, uint_fast64_t(UINT64_C(0x2a2a2a2a2a2a2a))); |
|
498 MOZ_RELEASE_ASSERT(!strcmp(output, "2a2a2a2a2a2a2a")); |
|
499 |
|
500 PoisonOutput(); |
|
501 sprintf(output, "%" PRIXFAST64, uint_fast64_t(UINT64_C(0xCDCDCDCDCDCD))); |
|
502 MOZ_RELEASE_ASSERT(!strcmp(output, "CDCDCDCDCDCD")); |
|
503 } |
|
504 |
|
505 static void |
|
506 TestPrintUnsignedFastN() |
|
507 { |
|
508 TestPrintUnsignedFast8(); |
|
509 TestPrintUnsignedFast16(); |
|
510 TestPrintUnsignedFast32(); |
|
511 TestPrintUnsignedFast64(); |
|
512 } |
|
513 |
|
514 static void |
|
515 TestPrintUnsignedMax() |
|
516 { |
|
517 PoisonOutput(); |
|
518 sprintf(output, "%" PRIoMAX, uintmax_t(UINTMAX_C(432157943248732))); |
|
519 MOZ_RELEASE_ASSERT(!strcmp(output, "14220563454333534")); |
|
520 |
|
521 PoisonOutput(); |
|
522 sprintf(output, "%" PRIuMAX, uintmax_t(UINTMAX_C(325719232983))); |
|
523 MOZ_RELEASE_ASSERT(!strcmp(output, "325719232983")); |
|
524 |
|
525 PoisonOutput(); |
|
526 sprintf(output, "%" PRIxMAX, uintmax_t(UINTMAX_C(327281321873))); |
|
527 MOZ_RELEASE_ASSERT(!strcmp(output, "4c337ca791")); |
|
528 |
|
529 PoisonOutput(); |
|
530 sprintf(output, "%" PRIXMAX, uintmax_t(UINTMAX_C(912389523743523))); |
|
531 MOZ_RELEASE_ASSERT(!strcmp(output, "33DD03D75A323")); |
|
532 } |
|
533 |
|
534 static void |
|
535 TestPrintUnsignedPtr() |
|
536 { |
|
537 PoisonOutput(); |
|
538 sprintf(output, "%" PRIoPTR, uintptr_t(reinterpret_cast<void*>(12345678))); |
|
539 MOZ_RELEASE_ASSERT(!strcmp(output, "57060516")); |
|
540 |
|
541 PoisonOutput(); |
|
542 sprintf(output, "%" PRIuPTR, uintptr_t(reinterpret_cast<void*>(87654321))); |
|
543 MOZ_RELEASE_ASSERT(!strcmp(output, "87654321")); |
|
544 |
|
545 PoisonOutput(); |
|
546 sprintf(output, "%" PRIxPTR, uintptr_t(reinterpret_cast<void*>(0x4c3a791))); |
|
547 MOZ_RELEASE_ASSERT(!strcmp(output, "4c3a791")); |
|
548 |
|
549 PoisonOutput(); |
|
550 sprintf(output, "%" PRIXPTR, uintptr_t(reinterpret_cast<void*>(0xF328DB))); |
|
551 MOZ_RELEASE_ASSERT(!strcmp(output, "F328DB")); |
|
552 } |
|
553 |
|
554 static void |
|
555 TestPrintUnsigned() |
|
556 { |
|
557 TestPrintUnsignedN(); |
|
558 TestPrintUnsignedLeastN(); |
|
559 TestPrintUnsignedFastN(); |
|
560 TestPrintUnsignedMax(); |
|
561 TestPrintUnsignedPtr(); |
|
562 } |
|
563 |
|
564 static void |
|
565 TestPrint() |
|
566 { |
|
567 TestPrintSigned(); |
|
568 TestPrintUnsigned(); |
|
569 } |
|
570 |
|
571 /* |
|
572 * The fscanf macros for signed integers are: |
|
573 * |
|
574 * SCNdN SCNdLEASTN SCNdFASTN SCNdMAX SCNdPTR |
|
575 * SCNiN SCNiLEASTN SCNiFASTN SCNiMAX SCNiPTR |
|
576 * |
|
577 * In these names N is the width of the type as described in C99 7.18.1. |
|
578 */ |
|
579 |
|
580 /* |
|
581 * MSVC's scanf is insufficiently powerful to implement all the SCN* macros. |
|
582 * Rather than support some subset of them, we instead support none of them. |
|
583 * See the comment at the top of IntegerPrintfMacros.h. But in case we ever do |
|
584 * support them, the following tests should adequately test implementation |
|
585 * correctness. (Indeed, these tests *revealed* MSVC's limitations.) |
|
586 * |
|
587 * That said, even if MSVC ever picks up complete support, we still probably |
|
588 * don't want to support these, because of the undefined-behavior issue noted |
|
589 * further down in the comment atop IntegerPrintfMacros.h. |
|
590 */ |
|
591 #define SHOULD_TEST_SCANF_MACROS 0 |
|
592 |
|
593 #if SHOULD_TEST_SCANF_MACROS |
|
594 |
|
595 /* |
|
596 * glibc header definitions for SCN{d,i,o,u,x}{,LEAST,FAST}8 use the "hh" length |
|
597 * modifier, which is new in C99 (and C++11, by reference). We compile this |
|
598 * file as C++11, so if "hh" is used in these macros, it's standard. But some |
|
599 * versions of gcc wrongly think it isn't and warn about a "non-standard" |
|
600 * modifier. And since these tests mostly exist to verify format-macro/type |
|
601 * consistency (particularly through compiler warnings about incorrect formats), |
|
602 * these warnings are unacceptable. So for now, compile tests for those macros |
|
603 * only if we aren't compiling with gcc. |
|
604 */ |
|
605 #define SHOULD_TEST_8BIT_FORMAT_MACROS (!(MOZ_IS_GCC)) |
|
606 |
|
607 template<typename T> |
|
608 union Input |
|
609 { |
|
610 T i; |
|
611 unsigned char pun[16]; |
|
612 }; |
|
613 |
|
614 template<typename T> |
|
615 static void |
|
616 PoisonInput(Input<T>& input) |
|
617 { |
|
618 memset(input.pun, 0xDA, sizeof(input.pun)); |
|
619 } |
|
620 |
|
621 template<typename T> |
|
622 static bool |
|
623 ExtraBitsUntouched(const Input<T>& input) |
|
624 { |
|
625 for (size_t i = sizeof(input.i); i < sizeof(input); i++) { |
|
626 if (input.pun[i] != 0xDA) |
|
627 return false; |
|
628 } |
|
629 |
|
630 return true; |
|
631 } |
|
632 |
|
633 static void |
|
634 TestScanSigned8() |
|
635 { |
|
636 #if SHOULD_TEST_8BIT_FORMAT_MACROS |
|
637 Input<int8_t> u; |
|
638 |
|
639 PoisonInput(u); |
|
640 sscanf("-17", "%" SCNd8, &u.i); |
|
641 MOZ_RELEASE_ASSERT(u.i == -17); |
|
642 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
643 |
|
644 PoisonInput(u); |
|
645 sscanf("042", "%" SCNi8, &u.i); |
|
646 MOZ_RELEASE_ASSERT(u.i == 042); |
|
647 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
648 #endif |
|
649 } |
|
650 |
|
651 static void |
|
652 TestScanSigned16() |
|
653 { |
|
654 Input<int16_t> u; |
|
655 |
|
656 PoisonInput(u); |
|
657 sscanf("-1742", "%" SCNd16, &u.i); |
|
658 MOZ_RELEASE_ASSERT(u.i == -1742); |
|
659 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
660 |
|
661 PoisonInput(u); |
|
662 sscanf("04217", "%" SCNi16, &u.i); |
|
663 MOZ_RELEASE_ASSERT(u.i == 04217); |
|
664 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
665 } |
|
666 |
|
667 static void |
|
668 TestScanSigned32() |
|
669 { |
|
670 Input<int32_t> u; |
|
671 |
|
672 PoisonInput(u); |
|
673 sscanf("-174257", "%" SCNd32, &u.i); |
|
674 MOZ_RELEASE_ASSERT(u.i == -174257); |
|
675 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
676 |
|
677 PoisonInput(u); |
|
678 sscanf("0423571", "%" SCNi32, &u.i); |
|
679 MOZ_RELEASE_ASSERT(u.i == 0423571); |
|
680 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
681 } |
|
682 |
|
683 static void |
|
684 TestScanSigned64() |
|
685 { |
|
686 Input<int64_t> u; |
|
687 |
|
688 PoisonInput(u); |
|
689 sscanf("-17425238927232", "%" SCNd64, &u.i); |
|
690 MOZ_RELEASE_ASSERT(u.i == -INT64_C(17425238927232)); |
|
691 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
692 |
|
693 PoisonInput(u); |
|
694 sscanf("042333576571", "%" SCNi64, &u.i); |
|
695 MOZ_RELEASE_ASSERT(u.i == INT64_C(042333576571)); |
|
696 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
697 } |
|
698 |
|
699 static void |
|
700 TestScanSignedN() |
|
701 { |
|
702 TestScanSigned8(); |
|
703 TestScanSigned16(); |
|
704 TestScanSigned32(); |
|
705 TestScanSigned64(); |
|
706 } |
|
707 |
|
708 static void |
|
709 TestScanSignedLeast8() |
|
710 { |
|
711 #if SHOULD_TEST_8BIT_FORMAT_MACROS |
|
712 Input<int_least8_t> u; |
|
713 |
|
714 PoisonInput(u); |
|
715 sscanf("-17", "%" SCNdLEAST8, &u.i); |
|
716 MOZ_RELEASE_ASSERT(u.i == -17); |
|
717 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
718 |
|
719 PoisonInput(u); |
|
720 sscanf("042", "%" SCNiLEAST8, &u.i); |
|
721 MOZ_RELEASE_ASSERT(u.i == 042); |
|
722 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
723 #endif |
|
724 } |
|
725 |
|
726 static void |
|
727 TestScanSignedLeast16() |
|
728 { |
|
729 Input<int_least16_t> u; |
|
730 |
|
731 PoisonInput(u); |
|
732 sscanf("-1742", "%" SCNdLEAST16, &u.i); |
|
733 MOZ_RELEASE_ASSERT(u.i == -1742); |
|
734 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
735 |
|
736 PoisonInput(u); |
|
737 sscanf("04217", "%" SCNiLEAST16, &u.i); |
|
738 MOZ_RELEASE_ASSERT(u.i == 04217); |
|
739 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
740 } |
|
741 |
|
742 static void |
|
743 TestScanSignedLeast32() |
|
744 { |
|
745 Input<int_least32_t> u; |
|
746 |
|
747 PoisonInput(u); |
|
748 sscanf("-174257", "%" SCNdLEAST32, &u.i); |
|
749 MOZ_RELEASE_ASSERT(u.i == -174257); |
|
750 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
751 |
|
752 PoisonInput(u); |
|
753 sscanf("0423571", "%" SCNiLEAST32, &u.i); |
|
754 MOZ_RELEASE_ASSERT(u.i == 0423571); |
|
755 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
756 } |
|
757 |
|
758 static void |
|
759 TestScanSignedLeast64() |
|
760 { |
|
761 Input<int_least64_t> u; |
|
762 |
|
763 PoisonInput(u); |
|
764 sscanf("-17425238927232", "%" SCNdLEAST64, &u.i); |
|
765 MOZ_RELEASE_ASSERT(u.i == -INT64_C(17425238927232)); |
|
766 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
767 |
|
768 PoisonInput(u); |
|
769 sscanf("042333576571", "%" SCNiLEAST64, &u.i); |
|
770 MOZ_RELEASE_ASSERT(u.i == INT64_C(042333576571)); |
|
771 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
772 } |
|
773 |
|
774 static void |
|
775 TestScanSignedLeastN() |
|
776 { |
|
777 TestScanSignedLeast8(); |
|
778 TestScanSignedLeast16(); |
|
779 TestScanSignedLeast32(); |
|
780 TestScanSignedLeast64(); |
|
781 } |
|
782 |
|
783 static void |
|
784 TestScanSignedFast8() |
|
785 { |
|
786 #if SHOULD_TEST_8BIT_FORMAT_MACROS |
|
787 Input<int_fast8_t> u; |
|
788 |
|
789 PoisonInput(u); |
|
790 sscanf("-17", "%" SCNdFAST8, &u.i); |
|
791 MOZ_RELEASE_ASSERT(u.i == -17); |
|
792 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
793 |
|
794 PoisonInput(u); |
|
795 sscanf("042", "%" SCNiFAST8, &u.i); |
|
796 MOZ_RELEASE_ASSERT(u.i == 042); |
|
797 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
798 #endif |
|
799 } |
|
800 |
|
801 static void |
|
802 TestScanSignedFast16() |
|
803 { |
|
804 Input<int_fast16_t> u; |
|
805 |
|
806 PoisonInput(u); |
|
807 sscanf("-1742", "%" SCNdFAST16, &u.i); |
|
808 MOZ_RELEASE_ASSERT(u.i == -1742); |
|
809 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
810 |
|
811 PoisonInput(u); |
|
812 sscanf("04217", "%" SCNiFAST16, &u.i); |
|
813 MOZ_RELEASE_ASSERT(u.i == 04217); |
|
814 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
815 } |
|
816 |
|
817 static void |
|
818 TestScanSignedFast32() |
|
819 { |
|
820 Input<int_fast32_t> u; |
|
821 |
|
822 PoisonInput(u); |
|
823 sscanf("-174257", "%" SCNdFAST32, &u.i); |
|
824 MOZ_RELEASE_ASSERT(u.i == -174257); |
|
825 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
826 |
|
827 PoisonInput(u); |
|
828 sscanf("0423571", "%" SCNiFAST32, &u.i); |
|
829 MOZ_RELEASE_ASSERT(u.i == 0423571); |
|
830 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
831 } |
|
832 |
|
833 static void |
|
834 TestScanSignedFast64() |
|
835 { |
|
836 Input<int_fast64_t> u; |
|
837 |
|
838 PoisonInput(u); |
|
839 sscanf("-17425238927232", "%" SCNdFAST64, &u.i); |
|
840 MOZ_RELEASE_ASSERT(u.i == -INT64_C(17425238927232)); |
|
841 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
842 |
|
843 PoisonInput(u); |
|
844 sscanf("042333576571", "%" SCNiFAST64, &u.i); |
|
845 MOZ_RELEASE_ASSERT(u.i == INT64_C(042333576571)); |
|
846 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
847 } |
|
848 |
|
849 static void |
|
850 TestScanSignedFastN() |
|
851 { |
|
852 TestScanSignedFast8(); |
|
853 TestScanSignedFast16(); |
|
854 TestScanSignedFast32(); |
|
855 TestScanSignedFast64(); |
|
856 } |
|
857 |
|
858 static void |
|
859 TestScanSignedMax() |
|
860 { |
|
861 Input<intmax_t> u; |
|
862 |
|
863 PoisonInput(u); |
|
864 sscanf("-432157943248732", "%" SCNdMAX, &u.i); |
|
865 MOZ_RELEASE_ASSERT(u.i == -INTMAX_C(432157943248732)); |
|
866 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
867 |
|
868 PoisonInput(u); |
|
869 sscanf("04233357236571", "%" SCNiMAX, &u.i); |
|
870 MOZ_RELEASE_ASSERT(u.i == INTMAX_C(04233357236571)); |
|
871 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
872 } |
|
873 |
|
874 static void |
|
875 TestScanSignedPtr() |
|
876 { |
|
877 Input<intptr_t> u; |
|
878 |
|
879 PoisonInput(u); |
|
880 sscanf("12345678", "%" SCNdPTR, &u.i); |
|
881 MOZ_RELEASE_ASSERT(u.i == intptr_t(reinterpret_cast<void*>(12345678))); |
|
882 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
883 |
|
884 PoisonInput(u); |
|
885 sscanf("04233357236", "%" SCNiPTR, &u.i); |
|
886 MOZ_RELEASE_ASSERT(u.i == intptr_t(reinterpret_cast<void*>(04233357236))); |
|
887 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
888 } |
|
889 |
|
890 static void |
|
891 TestScanSigned() |
|
892 { |
|
893 TestScanSignedN(); |
|
894 TestScanSignedLeastN(); |
|
895 TestScanSignedFastN(); |
|
896 TestScanSignedMax(); |
|
897 TestScanSignedPtr(); |
|
898 } |
|
899 |
|
900 /* |
|
901 * The fscanf macros for unsigned integers are: |
|
902 * |
|
903 * SCNoN SCNoLEASTN SCNoFASTN SCNoMAX SCNoPTR |
|
904 * SCNuN SCNuLEASTN SCNuFASTN SCNuMAX SCNuPTR |
|
905 * SCNxN SCNxLEASTN SCNxFASTN SCNxMAX SCNxPTR |
|
906 * |
|
907 * In these names N is the width of the type as described in C99 7.18.1. |
|
908 */ |
|
909 |
|
910 static void |
|
911 TestScanUnsigned8() |
|
912 { |
|
913 #if SHOULD_TEST_8BIT_FORMAT_MACROS |
|
914 Input<uint8_t> u; |
|
915 |
|
916 PoisonInput(u); |
|
917 sscanf("17", "%" SCNo8, &u.i); |
|
918 MOZ_RELEASE_ASSERT(u.i == 017); |
|
919 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
920 |
|
921 PoisonInput(u); |
|
922 sscanf("42", "%" SCNu8, &u.i); |
|
923 MOZ_RELEASE_ASSERT(u.i == 42); |
|
924 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
925 |
|
926 PoisonInput(u); |
|
927 sscanf("2A", "%" SCNx8, &u.i); |
|
928 MOZ_RELEASE_ASSERT(u.i == 0x2A); |
|
929 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
930 #endif |
|
931 } |
|
932 |
|
933 static void |
|
934 TestScanUnsigned16() |
|
935 { |
|
936 Input<uint16_t> u; |
|
937 |
|
938 PoisonInput(u); |
|
939 sscanf("1742", "%" SCNo16, &u.i); |
|
940 MOZ_RELEASE_ASSERT(u.i == 01742); |
|
941 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
942 |
|
943 PoisonInput(u); |
|
944 sscanf("4217", "%" SCNu16, &u.i); |
|
945 MOZ_RELEASE_ASSERT(u.i == 4217); |
|
946 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
947 |
|
948 PoisonInput(u); |
|
949 sscanf("2ABC", "%" SCNx16, &u.i); |
|
950 MOZ_RELEASE_ASSERT(u.i == 0x2ABC); |
|
951 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
952 } |
|
953 |
|
954 static void |
|
955 TestScanUnsigned32() |
|
956 { |
|
957 Input<uint32_t> u; |
|
958 |
|
959 PoisonInput(u); |
|
960 sscanf("17421742", "%" SCNo32, &u.i); |
|
961 MOZ_RELEASE_ASSERT(u.i == 017421742); |
|
962 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
963 |
|
964 PoisonInput(u); |
|
965 sscanf("4217867", "%" SCNu32, &u.i); |
|
966 MOZ_RELEASE_ASSERT(u.i == 4217867); |
|
967 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
968 |
|
969 PoisonInput(u); |
|
970 sscanf("2ABCBEEF", "%" SCNx32, &u.i); |
|
971 MOZ_RELEASE_ASSERT(u.i == 0x2ABCBEEF); |
|
972 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
973 } |
|
974 |
|
975 static void |
|
976 TestScanUnsigned64() |
|
977 { |
|
978 Input<uint64_t> u; |
|
979 |
|
980 PoisonInput(u); |
|
981 sscanf("17421742173", "%" SCNo64, &u.i); |
|
982 MOZ_RELEASE_ASSERT(u.i == UINT64_C(017421742173)); |
|
983 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
984 |
|
985 PoisonInput(u); |
|
986 sscanf("421786713579", "%" SCNu64, &u.i); |
|
987 MOZ_RELEASE_ASSERT(u.i == UINT64_C(421786713579)); |
|
988 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
989 |
|
990 PoisonInput(u); |
|
991 sscanf("DEADBEEF7457E", "%" SCNx64, &u.i); |
|
992 MOZ_RELEASE_ASSERT(u.i == UINT64_C(0xDEADBEEF7457E)); |
|
993 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
994 } |
|
995 |
|
996 static void |
|
997 TestScanUnsignedN() |
|
998 { |
|
999 TestScanUnsigned8(); |
|
1000 TestScanUnsigned16(); |
|
1001 TestScanUnsigned32(); |
|
1002 TestScanUnsigned64(); |
|
1003 } |
|
1004 |
|
1005 static void |
|
1006 TestScanUnsignedLeast8() |
|
1007 { |
|
1008 #if SHOULD_TEST_8BIT_FORMAT_MACROS |
|
1009 Input<uint_least8_t> u; |
|
1010 |
|
1011 PoisonInput(u); |
|
1012 sscanf("17", "%" SCNoLEAST8, &u.i); |
|
1013 MOZ_RELEASE_ASSERT(u.i == 017); |
|
1014 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1015 |
|
1016 PoisonInput(u); |
|
1017 sscanf("42", "%" SCNuLEAST8, &u.i); |
|
1018 MOZ_RELEASE_ASSERT(u.i == 42); |
|
1019 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1020 |
|
1021 PoisonInput(u); |
|
1022 sscanf("2A", "%" SCNxLEAST8, &u.i); |
|
1023 MOZ_RELEASE_ASSERT(u.i == 0x2A); |
|
1024 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1025 #endif |
|
1026 } |
|
1027 |
|
1028 static void |
|
1029 TestScanUnsignedLeast16() |
|
1030 { |
|
1031 Input<uint_least16_t> u; |
|
1032 |
|
1033 PoisonInput(u); |
|
1034 sscanf("1742", "%" SCNoLEAST16, &u.i); |
|
1035 MOZ_RELEASE_ASSERT(u.i == 01742); |
|
1036 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1037 |
|
1038 PoisonInput(u); |
|
1039 sscanf("4217", "%" SCNuLEAST16, &u.i); |
|
1040 MOZ_RELEASE_ASSERT(u.i == 4217); |
|
1041 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1042 |
|
1043 PoisonInput(u); |
|
1044 sscanf("2ABC", "%" SCNxLEAST16, &u.i); |
|
1045 MOZ_RELEASE_ASSERT(u.i == 0x2ABC); |
|
1046 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1047 } |
|
1048 |
|
1049 static void |
|
1050 TestScanUnsignedLeast32() |
|
1051 { |
|
1052 Input<uint_least32_t> u; |
|
1053 |
|
1054 PoisonInput(u); |
|
1055 sscanf("17421742", "%" SCNoLEAST32, &u.i); |
|
1056 MOZ_RELEASE_ASSERT(u.i == 017421742); |
|
1057 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1058 |
|
1059 PoisonInput(u); |
|
1060 sscanf("4217867", "%" SCNuLEAST32, &u.i); |
|
1061 MOZ_RELEASE_ASSERT(u.i == 4217867); |
|
1062 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1063 |
|
1064 PoisonInput(u); |
|
1065 sscanf("2ABCBEEF", "%" SCNxLEAST32, &u.i); |
|
1066 MOZ_RELEASE_ASSERT(u.i == 0x2ABCBEEF); |
|
1067 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1068 } |
|
1069 |
|
1070 static void |
|
1071 TestScanUnsignedLeast64() |
|
1072 { |
|
1073 Input<uint_least64_t> u; |
|
1074 |
|
1075 PoisonInput(u); |
|
1076 sscanf("17421742173", "%" SCNoLEAST64, &u.i); |
|
1077 MOZ_RELEASE_ASSERT(u.i == UINT64_C(017421742173)); |
|
1078 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1079 |
|
1080 PoisonInput(u); |
|
1081 sscanf("421786713579", "%" SCNuLEAST64, &u.i); |
|
1082 MOZ_RELEASE_ASSERT(u.i == UINT64_C(421786713579)); |
|
1083 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1084 |
|
1085 PoisonInput(u); |
|
1086 sscanf("DEADBEEF7457E", "%" SCNxLEAST64, &u.i); |
|
1087 MOZ_RELEASE_ASSERT(u.i == UINT64_C(0xDEADBEEF7457E)); |
|
1088 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1089 } |
|
1090 |
|
1091 static void |
|
1092 TestScanUnsignedLeastN() |
|
1093 { |
|
1094 TestScanUnsignedLeast8(); |
|
1095 TestScanUnsignedLeast16(); |
|
1096 TestScanUnsignedLeast32(); |
|
1097 TestScanUnsignedLeast64(); |
|
1098 } |
|
1099 |
|
1100 static void |
|
1101 TestScanUnsignedFast8() |
|
1102 { |
|
1103 #if SHOULD_TEST_8BIT_FORMAT_MACROS |
|
1104 Input<uint_fast8_t> u; |
|
1105 |
|
1106 PoisonInput(u); |
|
1107 sscanf("17", "%" SCNoFAST8, &u.i); |
|
1108 MOZ_RELEASE_ASSERT(u.i == 017); |
|
1109 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1110 |
|
1111 PoisonInput(u); |
|
1112 sscanf("42", "%" SCNuFAST8, &u.i); |
|
1113 MOZ_RELEASE_ASSERT(u.i == 42); |
|
1114 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1115 |
|
1116 PoisonInput(u); |
|
1117 sscanf("2A", "%" SCNxFAST8, &u.i); |
|
1118 MOZ_RELEASE_ASSERT(u.i == 0x2A); |
|
1119 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1120 #endif |
|
1121 } |
|
1122 |
|
1123 static void |
|
1124 TestScanUnsignedFast16() |
|
1125 { |
|
1126 Input<uint_fast16_t> u; |
|
1127 |
|
1128 PoisonInput(u); |
|
1129 sscanf("1742", "%" SCNoFAST16, &u.i); |
|
1130 MOZ_RELEASE_ASSERT(u.i == 01742); |
|
1131 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1132 |
|
1133 PoisonInput(u); |
|
1134 sscanf("4217", "%" SCNuFAST16, &u.i); |
|
1135 MOZ_RELEASE_ASSERT(u.i == 4217); |
|
1136 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1137 |
|
1138 PoisonInput(u); |
|
1139 sscanf("2ABC", "%" SCNxFAST16, &u.i); |
|
1140 MOZ_RELEASE_ASSERT(u.i == 0x2ABC); |
|
1141 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1142 } |
|
1143 |
|
1144 static void |
|
1145 TestScanUnsignedFast32() |
|
1146 { |
|
1147 Input<uint_fast32_t> u; |
|
1148 |
|
1149 PoisonInput(u); |
|
1150 sscanf("17421742", "%" SCNoFAST32, &u.i); |
|
1151 MOZ_RELEASE_ASSERT(u.i == 017421742); |
|
1152 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1153 |
|
1154 PoisonInput(u); |
|
1155 sscanf("4217867", "%" SCNuFAST32, &u.i); |
|
1156 MOZ_RELEASE_ASSERT(u.i == 4217867); |
|
1157 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1158 |
|
1159 PoisonInput(u); |
|
1160 sscanf("2ABCBEEF", "%" SCNxFAST32, &u.i); |
|
1161 MOZ_RELEASE_ASSERT(u.i == 0x2ABCBEEF); |
|
1162 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1163 } |
|
1164 |
|
1165 static void |
|
1166 TestScanUnsignedFast64() |
|
1167 { |
|
1168 Input<uint_fast64_t> u; |
|
1169 |
|
1170 PoisonInput(u); |
|
1171 sscanf("17421742173", "%" SCNoFAST64, &u.i); |
|
1172 MOZ_RELEASE_ASSERT(u.i == UINT64_C(017421742173)); |
|
1173 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1174 |
|
1175 PoisonInput(u); |
|
1176 sscanf("421786713579", "%" SCNuFAST64, &u.i); |
|
1177 MOZ_RELEASE_ASSERT(u.i == UINT64_C(421786713579)); |
|
1178 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1179 |
|
1180 PoisonInput(u); |
|
1181 sscanf("DEADBEEF7457E", "%" SCNxFAST64, &u.i); |
|
1182 MOZ_RELEASE_ASSERT(u.i == UINT64_C(0xDEADBEEF7457E)); |
|
1183 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1184 } |
|
1185 |
|
1186 static void |
|
1187 TestScanUnsignedFastN() |
|
1188 { |
|
1189 TestScanUnsignedFast8(); |
|
1190 TestScanUnsignedFast16(); |
|
1191 TestScanUnsignedFast32(); |
|
1192 TestScanUnsignedFast64(); |
|
1193 } |
|
1194 |
|
1195 static void |
|
1196 TestScanUnsignedMax() |
|
1197 { |
|
1198 Input<uintmax_t> u; |
|
1199 |
|
1200 PoisonInput(u); |
|
1201 sscanf("14220563454333534", "%" SCNoMAX, &u.i); |
|
1202 MOZ_RELEASE_ASSERT(u.i == UINTMAX_C(432157943248732)); |
|
1203 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1204 |
|
1205 PoisonInput(u); |
|
1206 sscanf("432157943248732", "%" SCNuMAX, &u.i); |
|
1207 MOZ_RELEASE_ASSERT(u.i == UINTMAX_C(432157943248732)); |
|
1208 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1209 |
|
1210 PoisonInput(u); |
|
1211 sscanf("4c337ca791", "%" SCNxMAX, &u.i); |
|
1212 MOZ_RELEASE_ASSERT(u.i == UINTMAX_C(327281321873)); |
|
1213 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1214 } |
|
1215 |
|
1216 static void |
|
1217 TestScanUnsignedPtr() |
|
1218 { |
|
1219 Input<uintptr_t> u; |
|
1220 |
|
1221 PoisonInput(u); |
|
1222 sscanf("57060516", "%" SCNoPTR, &u.i); |
|
1223 MOZ_RELEASE_ASSERT(u.i == uintptr_t(reinterpret_cast<void*>(12345678))); |
|
1224 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1225 |
|
1226 PoisonInput(u); |
|
1227 sscanf("87654321", "%" SCNuPTR, &u.i); |
|
1228 MOZ_RELEASE_ASSERT(u.i == uintptr_t(reinterpret_cast<void*>(87654321))); |
|
1229 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1230 |
|
1231 PoisonInput(u); |
|
1232 sscanf("4c3a791", "%" SCNxPTR, &u.i); |
|
1233 MOZ_RELEASE_ASSERT(u.i == uintptr_t(reinterpret_cast<void*>(0x4c3a791))); |
|
1234 MOZ_RELEASE_ASSERT(ExtraBitsUntouched(u)); |
|
1235 } |
|
1236 |
|
1237 static void |
|
1238 TestScanUnsigned() |
|
1239 { |
|
1240 TestScanUnsignedN(); |
|
1241 TestScanUnsignedLeastN(); |
|
1242 TestScanUnsignedFastN(); |
|
1243 TestScanUnsignedMax(); |
|
1244 TestScanUnsignedPtr(); |
|
1245 } |
|
1246 |
|
1247 static void |
|
1248 TestScan() |
|
1249 { |
|
1250 TestScanSigned(); |
|
1251 TestScanUnsigned(); |
|
1252 } |
|
1253 |
|
1254 #endif /* SHOULD_TEST_SCANF_MACROS */ |
|
1255 |
|
1256 int |
|
1257 main() |
|
1258 { |
|
1259 TestPrint(); |
|
1260 #if SHOULD_TEST_SCANF_MACROS |
|
1261 TestScan(); |
|
1262 #endif |
|
1263 return 0; |
|
1264 } |