mfbt/tests/TestIntegerPrintfMacros.cpp

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial