test.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. use BI\BigInteger;
  3. function test($a, $b, $message = "") {
  4. error_log(($a === $b ? "PASS" : "FAIL get: " . $a . ", expected: " . $b) . " " . $message);
  5. }
  6. function testB($a, $b, $message = "") {
  7. error_log(($a->toString() === $b ? "PASS" : "FAIL get: " . $a . ", expected: " . $b) . " " . $message);
  8. }
  9. function testSerialization($b, $msg = "") {
  10. test($b->toBits(), "1010000", $msg . " toBits");
  11. test($b->toBytes(), hex2bin("50"), $msg . " toBytes");
  12. test($b->toHex(), "50", $msg . " toHex");
  13. test($b->toDec(), "80", $msg . " toDec");
  14. test($b->toNumber(), 80, $msg . " toNumber");
  15. test($b->toBase(58), "1M", $msg . " to58");
  16. }
  17. function testCreate() {
  18. error_log("=============\nTest serialization\n=============");
  19. testSerialization(new BigInteger("1010000", 2), "bits");
  20. testSerialization(new BigInteger(hex2bin("50"), 256), "bytes");
  21. testSerialization(new BigInteger("50", 16), "hex");
  22. testSerialization(new BigInteger("80", 10), "dec");
  23. testSerialization(new BigInteger("80"), "dec2");
  24. testSerialization(new BigInteger(80), "number");
  25. }
  26. function testCreateSafeSingle($value, $base, $msg) {
  27. try {
  28. $z = new BigInteger($value, $base);
  29. error_log("FAIL exception during create " . $msg);
  30. }
  31. catch (\Exception $e) {
  32. error_log("PASS exception during create " . $msg);
  33. }
  34. test(BigInteger::createSafe($value, $base), false, "createSafe " . $msg);
  35. }
  36. function testCreateSafe() {
  37. error_log("=============\nTest create safe\n=============");
  38. testCreateSafeSingle("zz", 2, "bin");
  39. testCreateSafeSingle("zz", 10, "dec");
  40. testCreateSafeSingle("zz", 16, "hex");
  41. }
  42. function testSpaces() {
  43. error_log("=============\nTest spaces\n=============");
  44. test((new BigInteger("11 0 1", 2))->toBits(), "1101", "bin");
  45. test((new BigInteger("6 2 0 6", 10))->toDec(), "6206", "dec");
  46. test((new BigInteger("f3 5 12 ac 0", 16))->toHex(), "f3512ac0", "hex");
  47. }
  48. function testOp() {
  49. error_log("=============\nTest op\n=============");
  50. testB((new BigInteger(20))->add(34), "54", "add");
  51. testB((new BigInteger(20))->sub(14), "6", "sub");
  52. testB((new BigInteger(20))->mul(12), "240", "mul");
  53. testB((new BigInteger(20))->div(4), "5", "div");
  54. testB((new BigInteger(20))->divR(7), "6", "divR");
  55. $qr = (new BigInteger(20))->divQR(6);
  56. testB($qr[0], "3", "divQR[0]");
  57. testB($qr[1], "2", "divQR[1]");
  58. testB((new BigInteger(20))->mod(3), "2", "mod");
  59. testB((new BigInteger(54))->gcd(81), "27", "gcd");
  60. testB((new BigInteger(3))->modInverse(10), "7", "modInverse");
  61. testB((new BigInteger(3))->pow(4), "81", "pow");
  62. testB((new BigInteger(3))->powMod(4, 10), "1", "powMod");
  63. testB((new BigInteger(20))->abs(), "20", "abs");
  64. testB((new BigInteger(20))->neg(), "-20", "neg");
  65. testB((new BigInteger(20))->binaryAnd(18), "16", "binaryAnd");
  66. testB((new BigInteger(20))->binaryOr(18), "22", "binaryOr");
  67. testB((new BigInteger(20))->binaryXor(18), "6", "binaryXor");
  68. testB((new BigInteger(20))->setbit(3), "28", "setbit");
  69. test((new BigInteger(20))->testbit(4), true, "testbit true");
  70. test((new BigInteger(20))->testbit(3), false, "testbit false");
  71. test((new BigInteger(5))->testbit(0), true, "testbit 0 true");
  72. test((new BigInteger(6))->testbit(0), false, "testbit 0 false");
  73. test((new BigInteger(6))->testbit(1), true, "testbit 1 true");
  74. test((new BigInteger(5))->testbit(1), false, "testbit 1 false");
  75. test((new BigInteger(132))->testbit(7), true, "testbit 7 true");
  76. test((new BigInteger(81))->testbit(7), false, "testbit 7 false");
  77. test((new BigInteger(258))->testbit(8), true, "testbit 8 true");
  78. test((new BigInteger(253))->testbit(8), false, "testbit 8 false");
  79. test((new BigInteger(20))->scan0(2), 3, "scan0");
  80. test((new BigInteger(20))->scan1(3), 4, "scan1");
  81. test((new BigInteger(20))->cmp(22), -1, "cmp -1");
  82. test((new BigInteger(20))->cmp(20), 0, "cmp 0");
  83. test((new BigInteger(20))->cmp(18), 1, "cmp 1");
  84. test((new BigInteger(20))->equals(20), true, "equals true");
  85. test((new BigInteger(20))->equals(21), false, "equals false");
  86. test((new BigInteger(-20))->sign(), -1, "sign -1");
  87. test((new BigInteger(0))->sign(), 0, "sign 0");
  88. test((new BigInteger(20))->sign(), 1, "sign 1");
  89. testB(new BigInteger("-20"), "-20", "minus");
  90. testB(new BigInteger("-14", 16), "-20", "minus");
  91. testB(new BigInteger("-10100", 2), "-20", "minus");
  92. }
  93. function testBig() {
  94. error_log("=============\nTest big\n=============");
  95. $bits = "1001010111010010100001000101110110100001000101101000110101010101001";
  96. $hex = "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3";
  97. $dec = "436529472098746319073192837123683467019263172846";
  98. $bytes = hex2bin($hex);
  99. test((new BigInteger($bits, 2))->toBits(), $bits, "init big from binary");
  100. test((new BigInteger($dec, 10))->toDec(), $dec, "init big from dec");
  101. test((new BigInteger($hex, 16))->toHex(), $hex, "init big from hex");
  102. test((new BigInteger($bytes, 256))->toBytes(), $bytes, "init big from buffer");
  103. }
  104. testCreate();
  105. testCreateSafe();
  106. testSpaces();
  107. testOp();
  108. testBig();