lltest.c (23562B)
1 /* -*- Mode: C++; tab-width: 4; 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 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* 7 ** testll.c -- test suite for 64bit integer (longlong) operations 8 ** 9 ** Summary: testll [-d] | [-h] 10 ** 11 ** Where: 12 ** -d set debug mode on; displays individual test failures 13 ** -v verbose mode; displays progress in test, plus -d 14 ** -h gives usage message. 15 ** 16 ** Description: 17 ** lltest.c tests the functions defined in NSPR 2.0's prlong.h. 18 ** 19 ** Successive tests begin to depend on other LL functions working 20 ** correctly. So, ... Do not change the order of the tests as run 21 ** from main(). 22 ** 23 ** Caveats: 24 ** Do not even begin to think that this is an exhaustive test! 25 ** 26 ** These tests try a little of everything, but not all boundary 27 ** conditions and limits are tested. 28 ** You want better coverage? ... Add it. 29 ** 30 ** --- 31 ** Author: Lawrence Hardiman <larryh@netscape.com>. 32 ** --- 33 ** Revision History: 34 ** 01-Oct-1997. Original implementation. 35 ** 36 */ 37 38 #include "nspr.h" 39 #include "plgetopt.h" 40 41 /* --- Local Definitions --- */ 42 #define ReportProgress(m) \ 43 if (verboseMode) PR_fprintf(output, (m)); 44 45 /* --- Global variables --- */ 46 static PRIntn failedAlready = 0; 47 static PRFileDesc* output = NULL; 48 static PRBool debugMode = PR_FALSE; 49 static PRBool verboseMode = PR_FALSE; 50 51 /* 52 ** Constants used in tests. 53 */ 54 const PRInt64 bigZero = LL_INIT(0, 0); 55 const PRInt64 bigOne = LL_INIT(0, 1); 56 const PRInt64 bigTwo = LL_INIT(0, 2); 57 const PRInt64 bigSixTeen = LL_INIT(0, 16); 58 const PRInt64 bigThirtyTwo = LL_INIT(0, 32); 59 const PRInt64 bigMinusOne = LL_INIT(0xffffffff, 0xffffffff); 60 const PRInt64 bigMinusTwo = LL_INIT(0xffffffff, 0xfffffffe); 61 const PRInt64 bigNumber = LL_INIT(0x7fffffff, 0xffffffff); 62 const PRInt64 bigMinusNumber = LL_INIT(0x80000000, 0x00000001); 63 const PRInt64 bigMaxInt32 = LL_INIT(0x00000000, 0x7fffffff); 64 const PRInt64 big2To31 = LL_INIT(0x00000000, 0x80000000); 65 const PRUint64 bigZeroFox = LL_INIT(0x00000000, 0xffffffff); 66 const PRUint64 bigFoxFox = LL_INIT(0xffffffff, 0xffffffff); 67 const PRUint64 bigFoxZero = LL_INIT(0xffffffff, 0x00000000); 68 const PRUint64 bigEightZero = LL_INIT(0x80000000, 0x00000000); 69 const PRUint64 big64K = LL_INIT(0x00000000, 0x00010000); 70 const PRInt64 bigInt0 = LL_INIT(0x01a00000, 0x00001000); 71 const PRInt64 bigInt1 = LL_INIT(0x01a00000, 0x00001100); 72 const PRInt64 bigInt2 = LL_INIT(0x01a00000, 0x00000100); 73 const PRInt64 bigInt3 = LL_INIT(0x01a00001, 0x00001000); 74 const PRInt64 bigInt4 = LL_INIT(0x01a00001, 0x00001100); 75 const PRInt64 bigInt5 = LL_INIT(0x01a00001, 0x00000100); 76 const PRInt64 bigInt6 = LL_INIT(0xb1a00000, 0x00001000); 77 const PRInt64 bigInt7 = LL_INIT(0xb1a00000, 0x00001100); 78 const PRInt64 bigInt8 = LL_INIT(0xb1a00000, 0x00000100); 79 const PRInt64 bigInt9 = LL_INIT(0xb1a00001, 0x00001000); 80 const PRInt64 bigInt10 = LL_INIT(0xb1a00001, 0x00001100); 81 const PRInt64 bigInt11 = LL_INIT(0xb1a00001, 0x00000100); 82 const PRInt32 one = 1l; 83 const PRInt32 minusOne = -1l; 84 const PRInt32 sixteen = 16l; 85 const PRInt32 thirtyTwo = 32l; 86 const PRInt32 sixtyThree = 63l; 87 88 /* 89 ** SetFailed() -- Report individual test failure 90 ** 91 */ 92 static void SetFailed(char* what, char* how) { 93 failedAlready = 1; 94 if (debugMode) { 95 PR_fprintf(output, "%s: failed: %s\n", what, how); 96 } 97 return; 98 } 99 100 static void ResultFailed(char* what, char* how, PRInt64 expected, PRInt64 got) { 101 if (debugMode) { 102 SetFailed(what, how); 103 PR_fprintf(output, "Expected: 0x%llx Got: 0x%llx\n", expected, got); 104 } 105 return; 106 } 107 108 /* 109 ** TestAssignment() -- Test the assignment 110 */ 111 static void TestAssignment(void) { 112 PRInt64 zero = LL_Zero(); 113 PRInt64 min = LL_MinInt(); 114 PRInt64 max = LL_MaxInt(); 115 if (!LL_EQ(zero, bigZero)) { 116 SetFailed("LL_EQ(zero, bigZero)", "!="); 117 } 118 if (!LL_CMP(max, >, min)) { 119 SetFailed("LL_CMP(max, >, min)", "!>"); 120 } 121 } 122 123 /* 124 ** TestComparisons() -- Test the longlong comparison operations 125 */ 126 static void TestComparisons(void) { 127 ReportProgress("Testing Comparisons Operations\n"); 128 129 /* test for zero */ 130 if (!LL_IS_ZERO(bigZero)) { 131 SetFailed("LL_IS_ZERO", "Zero is not zero"); 132 } 133 134 if (LL_IS_ZERO(bigOne)) { 135 SetFailed("LL_IS_ZERO", "One tests as zero"); 136 } 137 138 if (LL_IS_ZERO(bigMinusOne)) { 139 SetFailed("LL_IS_ZERO", "Minus One tests as zero"); 140 } 141 142 /* test equal */ 143 if (!LL_EQ(bigZero, bigZero)) { 144 SetFailed("LL_EQ", "zero EQ zero"); 145 } 146 147 if (!LL_EQ(bigOne, bigOne)) { 148 SetFailed("LL_EQ", "one EQ one"); 149 } 150 151 if (!LL_EQ(bigNumber, bigNumber)) { 152 SetFailed("LL_EQ", "bigNumber EQ bigNumber"); 153 } 154 155 if (!LL_EQ(bigMinusOne, bigMinusOne)) { 156 SetFailed("LL_EQ", "minus one EQ minus one"); 157 } 158 159 if (LL_EQ(bigZero, bigOne)) { 160 SetFailed("LL_EQ", "zero EQ one"); 161 } 162 163 if (LL_EQ(bigOne, bigZero)) { 164 SetFailed("LL_EQ", "one EQ zero"); 165 } 166 167 if (LL_EQ(bigMinusOne, bigOne)) { 168 SetFailed("LL_EQ", "minus one EQ one"); 169 } 170 171 if (LL_EQ(bigNumber, bigOne)) { 172 SetFailed("LL_EQ", "bigNumber EQ one"); 173 } 174 175 /* test not equal */ 176 if (LL_NE(bigZero, bigZero)) { 177 SetFailed("LL_NE", "0 NE 0"); 178 } 179 180 if (LL_NE(bigOne, bigOne)) { 181 SetFailed("LL_NE", "1 NE 1"); 182 } 183 184 if (LL_NE(bigMinusOne, bigMinusOne)) { 185 SetFailed("LL_NE", "-1 NE -1"); 186 } 187 188 if (LL_NE(bigNumber, bigNumber)) { 189 SetFailed("LL_NE", "n NE n"); 190 } 191 192 if (LL_NE(bigMinusNumber, bigMinusNumber)) { 193 SetFailed("LL_NE", "-n NE -n"); 194 } 195 196 if (!LL_NE(bigZero, bigOne)) { 197 SetFailed("LL_NE", "0 NE 1"); 198 } 199 200 if (!LL_NE(bigOne, bigMinusNumber)) { 201 SetFailed("LL_NE", "1 NE -n"); 202 } 203 204 /* Greater than or equal to zero */ 205 if (!LL_GE_ZERO(bigZero)) { 206 SetFailed("LL_GE_ZERO", "0"); 207 } 208 209 if (!LL_GE_ZERO(bigOne)) { 210 SetFailed("LL_GE_ZERO", "1"); 211 } 212 213 if (!LL_GE_ZERO(bigNumber)) { 214 SetFailed("LL_GE_ZERO", "n"); 215 } 216 217 if (LL_GE_ZERO(bigMinusOne)) { 218 SetFailed("LL_GE_ZERO", "-1"); 219 } 220 221 if (LL_GE_ZERO(bigMinusNumber)) { 222 SetFailed("LL_GE_ZERO", "-n"); 223 } 224 225 /* Algebraic Compare two values */ 226 if (!LL_CMP(bigZero, ==, bigZero)) { 227 SetFailed("LL_CMP", "0 == 0"); 228 } 229 230 if (LL_CMP(bigZero, >, bigZero)) { 231 SetFailed("LL_CMP", "0 > 0"); 232 } 233 234 if (LL_CMP(bigZero, <, bigZero)) { 235 SetFailed("LL_CMP", "0 < 0"); 236 } 237 238 if (LL_CMP(bigNumber, <, bigOne)) { 239 SetFailed("LL_CMP", "n < 1"); 240 } 241 242 if (!LL_CMP(bigNumber, >, bigOne)) { 243 SetFailed("LL_CMP", "n <= 1"); 244 } 245 246 if (LL_CMP(bigOne, >, bigNumber)) { 247 SetFailed("LL_CMP", "1 > n"); 248 } 249 250 if (LL_CMP(bigMinusNumber, >, bigNumber)) { 251 SetFailed("LL_CMP", "-n > n"); 252 } 253 254 if (LL_CMP(bigNumber, !=, bigNumber)) { 255 SetFailed("LL_CMP", "n != n"); 256 } 257 258 if (!LL_CMP(bigMinusOne, >, bigMinusTwo)) { 259 SetFailed("LL_CMP", "-1 <= -2"); 260 } 261 262 if (!LL_CMP(bigMaxInt32, <, big2To31)) { 263 SetFailed("LL_CMP", "Max 32-bit signed int >= 2^31"); 264 } 265 266 /* Two positive numbers */ 267 if (!LL_CMP(bigInt0, <=, bigInt0)) { 268 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 269 } 270 271 if (!LL_CMP(bigInt0, <=, bigInt1)) { 272 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 273 } 274 275 if (LL_CMP(bigInt0, <=, bigInt2)) { 276 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 277 } 278 279 if (!LL_CMP(bigInt0, <=, bigInt3)) { 280 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 281 } 282 283 if (!LL_CMP(bigInt0, <=, bigInt4)) { 284 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 285 } 286 287 if (!LL_CMP(bigInt0, <=, bigInt5)) { 288 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 289 } 290 291 /* Two negative numbers */ 292 if (!LL_CMP(bigInt6, <=, bigInt6)) { 293 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 294 } 295 296 if (!LL_CMP(bigInt6, <=, bigInt7)) { 297 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 298 } 299 300 if (LL_CMP(bigInt6, <=, bigInt8)) { 301 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 302 } 303 304 if (!LL_CMP(bigInt6, <=, bigInt9)) { 305 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 306 } 307 308 if (!LL_CMP(bigInt6, <=, bigInt10)) { 309 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 310 } 311 312 if (!LL_CMP(bigInt6, <=, bigInt11)) { 313 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 314 } 315 316 /* One positive, one negative */ 317 if (LL_CMP(bigInt0, <=, bigInt6)) { 318 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 319 } 320 321 if (LL_CMP(bigInt0, <=, bigInt7)) { 322 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 323 } 324 325 if (LL_CMP(bigInt0, <=, bigInt8)) { 326 SetFailed("LL_CMP", "LL_CMP(<=) failed"); 327 } 328 329 /* Bitwise Compare two numbers */ 330 if (!LL_UCMP(bigZero, ==, bigZero)) { 331 SetFailed("LL_UCMP", "0 == 0"); 332 } 333 334 if (LL_UCMP(bigZero, >, bigZero)) { 335 SetFailed("LL_UCMP", "0 > 0"); 336 } 337 338 if (LL_UCMP(bigZero, <, bigZero)) { 339 SetFailed("LL_UCMP", "0 < 0"); 340 } 341 342 if (LL_UCMP(bigNumber, <, bigOne)) { 343 SetFailed("LL_UCMP", "n < 1"); 344 } 345 346 if (!LL_UCMP(bigNumber, >, bigOne)) { 347 SetFailed("LL_UCMP", "n < 1"); 348 } 349 350 if (LL_UCMP(bigOne, >, bigNumber)) { 351 SetFailed("LL_UCMP", "1 > n"); 352 } 353 354 if (LL_UCMP(bigMinusNumber, <, bigNumber)) { 355 SetFailed("LL_UCMP", "-n < n"); 356 } 357 358 /* Two positive numbers */ 359 if (!LL_UCMP(bigInt0, <=, bigInt0)) { 360 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 361 } 362 363 if (!LL_UCMP(bigInt0, <=, bigInt1)) { 364 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 365 } 366 367 if (LL_UCMP(bigInt0, <=, bigInt2)) { 368 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 369 } 370 371 if (!LL_UCMP(bigInt0, <=, bigInt3)) { 372 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 373 } 374 375 if (!LL_UCMP(bigInt0, <=, bigInt4)) { 376 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 377 } 378 379 if (!LL_UCMP(bigInt0, <=, bigInt5)) { 380 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 381 } 382 383 /* Two negative numbers */ 384 if (!LL_UCMP(bigInt6, <=, bigInt6)) { 385 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 386 } 387 388 if (!LL_UCMP(bigInt6, <=, bigInt7)) { 389 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 390 } 391 392 if (LL_UCMP(bigInt6, <=, bigInt8)) { 393 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 394 } 395 396 if (!LL_UCMP(bigInt6, <=, bigInt9)) { 397 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 398 } 399 400 if (!LL_UCMP(bigInt6, <=, bigInt10)) { 401 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 402 } 403 404 if (!LL_UCMP(bigInt6, <=, bigInt11)) { 405 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 406 } 407 408 /* One positive, one negative */ 409 if (!LL_UCMP(bigInt0, <=, bigInt6)) { 410 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 411 } 412 413 if (!LL_UCMP(bigInt0, <=, bigInt7)) { 414 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 415 } 416 417 if (!LL_UCMP(bigInt0, <=, bigInt8)) { 418 SetFailed("LL_UCMP", "LL_UCMP(<=) failed"); 419 } 420 421 return; 422 } 423 424 /* 425 ** TestLogicalOperations() -- Tests for AND, OR, ... 426 ** 427 */ 428 static void TestLogicalOperations(void) { 429 PRUint64 result, result2; 430 431 ReportProgress("Testing Logical Operations\n"); 432 433 /* Test AND */ 434 LL_AND(result, bigZero, bigZero); 435 if (!LL_IS_ZERO(result)) { 436 ResultFailed("LL_AND", "0 & 0", bigZero, result); 437 } 438 439 LL_AND(result, bigOne, bigOne); 440 if (LL_IS_ZERO(result)) { 441 ResultFailed("LL_AND", "1 & 1", bigOne, result); 442 } 443 444 LL_AND(result, bigZero, bigOne); 445 if (!LL_IS_ZERO(result)) { 446 ResultFailed("LL_AND", "1 & 1", bigZero, result); 447 } 448 449 LL_AND(result, bigMinusOne, bigMinusOne); 450 if (!LL_UCMP(result, ==, bigMinusOne)) { 451 ResultFailed("LL_AND", "-1 & -1", bigMinusOne, result); 452 } 453 454 /* test OR */ 455 LL_OR(result, bigZero, bigZero); 456 if (!LL_IS_ZERO(result)) { 457 ResultFailed("LL_OR", "0 | 1", bigZero, result); 458 } 459 460 LL_OR(result, bigZero, bigOne); 461 if (LL_IS_ZERO(result)) { 462 ResultFailed("LL_OR", "0 | 1", bigOne, result); 463 } 464 465 LL_OR(result, bigZero, bigMinusNumber); 466 if (!LL_UCMP(result, ==, bigMinusNumber)) { 467 ResultFailed("LL_OR", "0 | -n", bigMinusNumber, result); 468 } 469 470 LL_OR(result, bigMinusNumber, bigZero); 471 if (!LL_UCMP(result, ==, bigMinusNumber)) { 472 ResultFailed("LL_OR", "-n | 0", bigMinusNumber, result); 473 } 474 475 /* test XOR */ 476 LL_XOR(result, bigZero, bigZero); 477 if (LL_UCMP(result, !=, bigZero)) { 478 ResultFailed("LL_XOR", "0 ^ 0", bigZero, result); 479 } 480 481 LL_XOR(result, bigOne, bigZero); 482 if (LL_UCMP(result, !=, bigOne)) { 483 ResultFailed("LL_XOR", "1 ^ 0", bigZero, result); 484 } 485 486 LL_XOR(result, bigMinusNumber, bigZero); 487 if (LL_UCMP(result, !=, bigMinusNumber)) { 488 ResultFailed("LL_XOR", "-n ^ 0", bigMinusNumber, result); 489 } 490 491 LL_XOR(result, bigMinusNumber, bigMinusNumber); 492 if (LL_UCMP(result, !=, bigZero)) { 493 ResultFailed("LL_XOR", "-n ^ -n", bigMinusNumber, result); 494 } 495 496 /* test OR2. */ 497 result = bigZero; 498 LL_OR2(result, bigOne); 499 if (LL_UCMP(result, !=, bigOne)) { 500 ResultFailed("LL_OR2", "(r=0) |= 1", bigOne, result); 501 } 502 503 result = bigOne; 504 LL_OR2(result, bigNumber); 505 if (LL_UCMP(result, !=, bigNumber)) { 506 ResultFailed("LL_OR2", "(r=1) |= n", bigNumber, result); 507 } 508 509 result = bigMinusNumber; 510 LL_OR2(result, bigMinusNumber); 511 if (LL_UCMP(result, !=, bigMinusNumber)) { 512 ResultFailed("LL_OR2", "(r=-n) |= -n", bigMinusNumber, result); 513 } 514 515 /* test NOT */ 516 LL_NOT(result, bigMinusNumber); 517 LL_NOT(result2, result); 518 if (LL_UCMP(result2, !=, bigMinusNumber)) { 519 ResultFailed("LL_NOT", "r != ~(~-n)", bigMinusNumber, result); 520 } 521 522 /* test Negation */ 523 LL_NEG(result, bigMinusNumber); 524 LL_NEG(result2, result); 525 if (LL_CMP(result2, !=, bigMinusNumber)) { 526 ResultFailed("LL_NEG", "r != -(-(-n))", bigMinusNumber, result); 527 } 528 529 return; 530 } 531 532 /* 533 ** TestConversion() -- Test Conversion Operations 534 ** 535 */ 536 static void TestConversion(void) { 537 PRInt64 result; 538 PRInt64 resultU; 539 PRInt32 result32; 540 PRUint32 resultU32; 541 float resultF; 542 PRFloat64 resultD; 543 544 ReportProgress("Testing Conversion Operations\n"); 545 546 /* LL_L2I -- Convert to signed 32bit */ 547 LL_L2I(result32, bigOne); 548 if (result32 != one) { 549 SetFailed("LL_L2I", "r != 1"); 550 } 551 552 LL_L2I(result32, bigMinusOne); 553 if (result32 != minusOne) { 554 SetFailed("LL_L2I", "r != -1"); 555 } 556 557 /* LL_L2UI -- Convert 64bit to unsigned 32bit */ 558 LL_L2UI(resultU32, bigMinusOne); 559 if (resultU32 != (PRUint32)minusOne) { 560 SetFailed("LL_L2UI", "r != -1"); 561 } 562 563 LL_L2UI(resultU32, bigOne); 564 if (resultU32 != (PRUint32)one) { 565 SetFailed("LL_L2UI", "r != 1"); 566 } 567 568 /* LL_L2F -- Convert to 32bit floating point */ 569 LL_L2F(resultF, bigOne); 570 if (resultF != 1.0) { 571 SetFailed("LL_L2F", "r != 1.0"); 572 } 573 574 LL_L2F(resultF, bigMinusOne); 575 if (resultF != -1.0) { 576 SetFailed("LL_L2F", "r != 1.0"); 577 } 578 579 /* LL_L2D -- Convert to 64bit floating point */ 580 LL_L2D(resultD, bigOne); 581 if (resultD != 1.0L) { 582 SetFailed("LL_L2D", "r != 1.0"); 583 } 584 585 LL_L2D(resultD, bigMinusOne); 586 if (resultD != -1.0L) { 587 SetFailed("LL_L2D", "r != -1.0"); 588 } 589 590 /* LL_I2L -- Convert 32bit signed to 64bit signed */ 591 LL_I2L(result, one); 592 if (LL_CMP(result, !=, bigOne)) { 593 SetFailed("LL_I2L", "r != 1"); 594 } 595 596 LL_I2L(result, minusOne); 597 if (LL_CMP(result, !=, bigMinusOne)) { 598 SetFailed("LL_I2L", "r != -1"); 599 } 600 601 /* LL_UI2L -- Convert 32bit unsigned to 64bit unsigned */ 602 LL_UI2L(resultU, (PRUint32)one); 603 if (LL_CMP(resultU, !=, bigOne)) { 604 SetFailed("LL_UI2L", "r != 1"); 605 } 606 607 /* [lth.] This did not behave as expected, but it is correct 608 */ 609 LL_UI2L(resultU, (PRUint32)minusOne); 610 if (LL_CMP(resultU, !=, bigZeroFox)) { 611 ResultFailed("LL_UI2L", "r != -1", bigZeroFox, resultU); 612 } 613 614 /* LL_F2L -- Convert 32bit float to 64bit signed */ 615 LL_F2L(result, 1.0); 616 if (LL_CMP(result, !=, bigOne)) { 617 SetFailed("LL_F2L", "r != 1"); 618 } 619 620 LL_F2L(result, -1.0); 621 if (LL_CMP(result, !=, bigMinusOne)) { 622 SetFailed("LL_F2L", "r != -1"); 623 } 624 625 /* LL_D2L -- Convert 64bit Float to 64bit signed */ 626 LL_D2L(result, 1.0L); 627 if (LL_CMP(result, !=, bigOne)) { 628 SetFailed("LL_D2L", "r != 1"); 629 } 630 631 LL_D2L(result, -1.0L); 632 if (LL_CMP(result, !=, bigMinusOne)) { 633 SetFailed("LL_D2L", "r != -1"); 634 } 635 636 return; 637 } 638 639 static void ShiftCompileOnly() { 640 /* 641 ** This function is only compiled, never called. 642 ** The real test is to see if it compiles w/o 643 ** warnings. This is no small feat, by the way. 644 */ 645 PRInt64 ia, ib; 646 PRUint64 ua, ub; 647 LL_SHR(ia, ib, 32); 648 LL_SHL(ia, ib, 32); 649 650 LL_USHR(ua, ub, 32); 651 LL_ISHL(ia, 49, 32); 652 653 } /* ShiftCompileOnly */ 654 655 /* 656 ** TestShift() -- Test Shifting Operations 657 ** 658 */ 659 static void TestShift(void) { 660 static const PRInt64 largeTwoZero = LL_INIT(0x00000002, 0x00000000); 661 PRInt64 result; 662 PRUint64 resultU; 663 664 ReportProgress("Testing Shifting Operations\n"); 665 666 /* LL_SHL -- Shift left algebraic */ 667 LL_SHL(result, bigOne, one); 668 if (LL_CMP(result, !=, bigTwo)) { 669 ResultFailed("LL_SHL", "r != 2", bigOne, result); 670 } 671 672 LL_SHL(result, bigTwo, thirtyTwo); 673 if (LL_CMP(result, !=, largeTwoZero)) { 674 ResultFailed("LL_SHL", "r != twoZero", largeTwoZero, result); 675 } 676 677 /* LL_SHR -- Shift right algebraic */ 678 LL_SHR(result, bigFoxZero, thirtyTwo); 679 if (LL_CMP(result, !=, bigMinusOne)) { 680 ResultFailed("LL_SHR", "r != -1", bigMinusOne, result); 681 } 682 683 LL_SHR(result, bigTwo, one); 684 if (LL_CMP(result, !=, bigOne)) { 685 ResultFailed("LL_SHR", "r != 1", bigOne, result); 686 } 687 688 LL_SHR(result, bigFoxFox, thirtyTwo); 689 if (LL_CMP(result, !=, bigMinusOne)) { 690 ResultFailed("LL_SHR", "r != -1 (was ff,ff)", bigMinusOne, result); 691 } 692 693 /* LL_USHR -- Logical shift right */ 694 LL_USHR(resultU, bigZeroFox, thirtyTwo); 695 if (LL_UCMP(resultU, !=, bigZero)) { 696 ResultFailed("LL_USHR", "r != 0 ", bigZero, result); 697 } 698 699 LL_USHR(resultU, bigFoxFox, thirtyTwo); 700 if (LL_UCMP(resultU, !=, bigZeroFox)) { 701 ResultFailed("LL_USHR", "r != 0 ", bigZeroFox, result); 702 } 703 704 /* LL_ISHL -- Shift a 32bit integer into a 64bit result */ 705 LL_ISHL(resultU, minusOne, thirtyTwo); 706 if (LL_UCMP(resultU, !=, bigFoxZero)) { 707 ResultFailed("LL_ISHL", "r != ff,00 ", bigFoxZero, result); 708 } 709 710 LL_ISHL(resultU, one, sixtyThree); 711 if (LL_UCMP(resultU, !=, bigEightZero)) { 712 ResultFailed("LL_ISHL", "r != 80,00 ", bigEightZero, result); 713 } 714 715 LL_ISHL(resultU, one, sixteen); 716 if (LL_UCMP(resultU, !=, big64K)) { 717 ResultFailed("LL_ISHL", "r != 64K ", big64K, resultU); 718 } 719 720 return; 721 } 722 723 /* 724 ** TestArithmetic() -- Test arithmetic operations. 725 ** 726 */ 727 static void TestArithmetic(void) { 728 PRInt64 largeVal = LL_INIT(0x00000001, 0xffffffff); 729 PRInt64 largeValPlusOne = LL_INIT(0x00000002, 0x00000000); 730 PRInt64 largeValTimesTwo = LL_INIT(0x00000003, 0xfffffffe); 731 PRInt64 largeMultCand = LL_INIT(0x00000000, 0x7fffffff); 732 PRInt64 largeMinusMultCand = LL_INIT(0xffffffff, 0x10000001); 733 PRInt64 largeMultCandx64K = LL_INIT(0x00007fff, 0xffff0000); 734 PRInt64 largeNumSHL5 = LL_INIT(0x0000001f, 0xffffffe0); 735 PRInt64 result, result2; 736 737 /* Addition */ 738 LL_ADD(result, bigOne, bigOne); 739 if (LL_CMP(result, !=, bigTwo)) { 740 ResultFailed("LL_ADD", "r != 1 + 1", bigTwo, result); 741 } 742 743 LL_ADD(result, bigMinusOne, bigOne); 744 if (LL_CMP(result, !=, bigZero)) { 745 ResultFailed("LL_ADD", "r != -1 + 1", bigOne, result); 746 } 747 748 LL_ADD(result, largeVal, bigOne); 749 if (LL_CMP(result, !=, largeValPlusOne)) { 750 ResultFailed("LL_ADD", "lVP1 != lV + 1", largeValPlusOne, result); 751 } 752 753 /* Subtraction */ 754 LL_SUB(result, bigOne, bigOne); 755 if (LL_CMP(result, !=, bigZero)) { 756 ResultFailed("LL_SUB", "r != 1 - 1", bigZero, result); 757 } 758 759 LL_SUB(result, bigTwo, bigOne); 760 if (LL_CMP(result, !=, bigOne)) { 761 ResultFailed("LL_SUB", "r != 2 - 1", bigOne, result); 762 } 763 764 LL_SUB(result, largeValPlusOne, bigOne); 765 if (LL_CMP(result, !=, largeVal)) { 766 ResultFailed("LL_SUB", "r != lVP1 - 1", largeVal, result); 767 } 768 769 /* Multiply */ 770 LL_MUL(result, largeVal, bigTwo); 771 if (LL_CMP(result, !=, largeValTimesTwo)) { 772 ResultFailed("LL_MUL", "r != lV*2", largeValTimesTwo, result); 773 } 774 775 LL_MUL(result, largeMultCand, big64K); 776 if (LL_CMP(result, !=, largeMultCandx64K)) { 777 ResultFailed("LL_MUL", "r != lV*64K", largeMultCandx64K, result); 778 } 779 780 LL_NEG(result2, largeMultCand); 781 LL_MUL(result, largeMultCand, bigMinusOne); 782 if (LL_CMP(result, !=, result2)) { 783 ResultFailed("LL_MUL", "r != -lMC", result2, result); 784 } 785 786 LL_SHL(result2, bigZeroFox, 5); 787 LL_MUL(result, bigZeroFox, bigThirtyTwo); 788 if (LL_CMP(result, !=, largeNumSHL5)) { 789 ResultFailed("LL_MUL", "r != 0f<<5", largeNumSHL5, result); 790 } 791 792 /* LL_DIV() Division */ 793 LL_DIV(result, bigOne, bigOne); 794 if (LL_CMP(result, !=, bigOne)) { 795 ResultFailed("LL_DIV", "1 != 1", bigOne, result); 796 } 797 798 LL_DIV(result, bigNumber, bigOne); 799 if (LL_CMP(result, !=, bigNumber)) { 800 ResultFailed("LL_DIV", "r != n / 1", bigNumber, result); 801 } 802 803 LL_DIV(result, bigNumber, bigMinusOne); 804 if (LL_CMP(result, !=, bigMinusNumber)) { 805 ResultFailed("LL_DIV", "r != n / -1", bigMinusNumber, result); 806 } 807 808 LL_DIV(result, bigMinusNumber, bigMinusOne); 809 if (LL_CMP(result, !=, bigNumber)) { 810 ResultFailed("LL_DIV", "r != -n / -1", bigNumber, result); 811 } 812 813 LL_SHL(result2, bigZeroFox, 5); 814 LL_DIV(result, result2, bigOne); 815 if (LL_CMP(result, !=, result2)) { 816 ResultFailed("LL_DIV", "0f<<5 != 0f<<5", result2, result); 817 } 818 819 LL_SHL(result2, bigZeroFox, 5); 820 LL_NEG(result2, result2); 821 LL_DIV(result, result2, bigOne); 822 if (LL_CMP(result, !=, result2)) { 823 ResultFailed("LL_DIV", "-0f<<5 != -0f<<5", result2, result); 824 } 825 826 LL_SHL(result2, bigZeroFox, 17); 827 LL_DIV(result, result2, bigMinusOne); 828 LL_NEG(result2, result2); 829 if (LL_CMP(result, !=, result2)) { 830 ResultFailed("LL_DIV", "-0f<<17 != -0f<<17", result2, result); 831 } 832 833 /* LL_MOD() Modulo Division */ 834 LL_ADD(result2, bigThirtyTwo, bigOne); 835 LL_MOD(result, result2, bigSixTeen); 836 if (LL_CMP(result, !=, bigOne)) { 837 ResultFailed("LL_MOD", "r != 1", bigSixTeen, result); 838 } 839 840 LL_MUL(result2, bigZeroFox, bigThirtyTwo); 841 LL_ADD(result2, result2, bigSixTeen); 842 LL_MOD(result, result2, bigThirtyTwo); 843 if (LL_CMP(result, !=, bigSixTeen)) { 844 ResultFailed("LL_MOD", "r != 16", bigSixTeen, result); 845 } 846 847 /* LL_UDIVMOD */ 848 LL_DIV(result, bigOne, bigOne); 849 if (LL_CMP(result, !=, bigOne)) { 850 ResultFailed("LL_DIV", "r != 16", bigSixTeen, result); 851 } 852 853 return; 854 } 855 856 static void TestWellknowns(void) { 857 PRInt64 max = LL_MAXINT, min = LL_MININT, zero = LL_ZERO; 858 PRInt64 mmax = LL_MaxInt(), mmin = LL_MinInt(), mzero = LL_Zero(); 859 if (LL_NE(max, mmax)) { 860 ResultFailed("max, mmax", "max != mmax", max, mmax); 861 } 862 if (LL_NE(min, mmin)) { 863 ResultFailed("min, mmin", "min != mmin", max, mmin); 864 } 865 if (LL_NE(zero, mzero)) { 866 ResultFailed("zero, mzero", "zero != mzero", zero, mzero); 867 } 868 } /* TestWellknowns */ 869 870 /* 871 ** Initialize() -- Initialize the test case 872 ** 873 ** Parse command line options 874 ** 875 */ 876 static PRIntn Initialize(PRIntn argc, char** argv) { 877 PLOptState* opt = PL_CreateOptState(argc, argv, "dvh"); 878 PLOptStatus os; 879 880 /* 881 ** Parse command line options 882 */ 883 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { 884 if (PL_OPT_BAD == os) { 885 continue; 886 } 887 switch (opt->option) { 888 case 'd': /* set debug mode */ 889 debugMode = PR_TRUE; 890 break; 891 892 case 'v': /* set verbose mode */ 893 verboseMode = PR_TRUE; 894 debugMode = PR_TRUE; 895 break; 896 897 case 'h': /* user wants some guidance */ 898 default: 899 PR_fprintf(output, "You get help.\n"); 900 return (1); 901 } 902 } 903 PL_DestroyOptState(opt); 904 return (0); 905 } 906 907 int main(int argc, char** argv) { 908 output = PR_GetSpecialFD(PR_StandardError); 909 910 if (Initialize(argc, argv)) { 911 return (1); 912 } 913 914 TestAssignment(); 915 TestComparisons(); 916 TestLogicalOperations(); 917 TestConversion(); 918 TestShift(); 919 TestArithmetic(); 920 TestWellknowns(); 921 922 /* 923 ** That's all folks! 924 */ 925 if (failedAlready) { 926 PR_fprintf(output, "FAIL\n"); 927 } else { 928 PR_fprintf(output, "PASS\n"); 929 } 930 return failedAlready; 931 } /* end main() */