1
0

random-tests.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * This script stress tests calculators with random large numbers and ensures that all implementations return the same
  4. * results. It is designed to run in an infinite loop unless a bug is found.
  5. */
  6. declare(strict_types=1);
  7. require __DIR__ . '/vendor/autoload.php';
  8. use Brick\Math\Internal\Calculator;
  9. (new class(30) { // max digits
  10. private $gmp;
  11. private $bcmath;
  12. private $native;
  13. private $maxDigits;
  14. public function __construct(int $maxDigits)
  15. {
  16. $this->gmp = new Calculator\GmpCalculator();
  17. $this->bcmath = new Calculator\BcMathCalculator();
  18. $this->native = new Calculator\NativeCalculator();
  19. $this->maxDigits = $maxDigits;
  20. }
  21. public function __invoke() : void
  22. {
  23. for (;;) {
  24. $a = $this->generateRandomNumber();
  25. $b = $this->generateRandomNumber();
  26. $c = $this->generateRandomNumber();
  27. $this->runTests($a, $b);
  28. $this->runTests($b, $a);
  29. if ($a !== '0') {
  30. $this->runTests("-$a", $b);
  31. $this->runTests($b, "-$a");
  32. }
  33. if ($b !== '0') {
  34. $this->runTests($a, "-$b");
  35. $this->runTests("-$b", $a);
  36. }
  37. if ($a !== '0' && $b !== '0') {
  38. $this->runTests("-$a", "-$b");
  39. $this->runTests("-$b", "-$a");
  40. }
  41. if ($c !== '0') {
  42. $this->test("$a POW $b MOD $c", function(Calculator $calc) use($a, $b, $c) {
  43. return $calc->modPow($a, $b, $c);
  44. });
  45. }
  46. }
  47. }
  48. /**
  49. * @param string $a The left operand.
  50. * @param string $b The right operand.
  51. */
  52. function runTests(string $a, string $b) : void
  53. {
  54. $this->test("$a + $b", function(Calculator $c) use($a, $b) {
  55. return $c->add($a, $b);
  56. });
  57. $this->test("$a - $b", function(Calculator $c) use($a, $b) {
  58. return $c->sub($a, $b);
  59. });
  60. $this->test("$a * $b", function(Calculator $c) use($a, $b) {
  61. return $c->mul($a, $b);
  62. });
  63. if ($b !== '0') {
  64. $this->test("$a / $b", function(Calculator $c) use($a, $b) {
  65. return $c->divQR($a, $b);
  66. });
  67. $this->test("$a MOD $b", function(Calculator $c) use($a, $b) {
  68. return $c->mod($a, $b);
  69. });
  70. }
  71. if ($b !== '0' && $b[0] !== '-') {
  72. $this->test("INV $a MOD $b", function(Calculator $c) use($a, $b) {
  73. return $c->modInverse($a, $b);
  74. });
  75. }
  76. $this->test("GCD $a, $b", function(Calculator $c) use($a, $b) {
  77. return $c->gcd($a, $b);
  78. });
  79. if ($a[0] !== '-') {
  80. $this->test("SQRT $a", function(Calculator $c) use($a, $b) {
  81. return $c->sqrt($a);
  82. });
  83. }
  84. $this->test("$a AND $b", function(Calculator $c) use($a, $b) {
  85. return $c->and($a, $b);
  86. });
  87. $this->test("$a OR $b", function(Calculator $c) use($a, $b) {
  88. return $c->or($a, $b);
  89. });
  90. $this->test("$a XOR $b", function(Calculator $c) use($a, $b) {
  91. return $c->xor($a, $b);
  92. });
  93. }
  94. /**
  95. * @param string $test A string representing the test being executed.
  96. * @param Closure $callback A callback function accepting a Calculator instance and returning a calculation result.
  97. */
  98. private function test(string $test, Closure $callback) : void
  99. {
  100. static $counter = 0;
  101. static $lastOutputTime = null;
  102. $gmpResult = $callback($this->gmp);
  103. $bcmathResult = $callback($this->bcmath);
  104. $nativeResult = $callback($this->native);
  105. if ($gmpResult !== $bcmathResult) {
  106. self::failure('GMP', 'BCMath', $test);
  107. }
  108. if ($gmpResult !== $nativeResult) {
  109. self::failure('GMP', 'Native', $test);
  110. }
  111. $counter++;
  112. $time = microtime(true);
  113. if ($lastOutputTime === null) {
  114. $lastOutputTime = $time;
  115. } elseif ($time - $lastOutputTime >= 0.1) {
  116. echo "\r", number_format($counter);
  117. $lastOutputTime = $time;
  118. }
  119. }
  120. /**
  121. * @param string $c1 The name of the first calculator.
  122. * @param string $c2 The name of the second calculator.
  123. * @param string $test A string representing the test being executed.
  124. */
  125. private static function failure(string $c1, string $c2, string $test) : void
  126. {
  127. echo PHP_EOL;
  128. echo 'FAILURE!', PHP_EOL;
  129. echo $c1, ' vs ', $c2, PHP_EOL;
  130. echo $test, PHP_EOL;
  131. die;
  132. }
  133. private function generateRandomNumber() : string
  134. {
  135. $length = random_int(1, $this->maxDigits);
  136. $number = '';
  137. for ($i = 0; $i < $length; $i++) {
  138. $number .= random_int(0, 9);
  139. }
  140. $number = ltrim($number, '0');
  141. if ($number === '') {
  142. return '0';
  143. }
  144. return $number;
  145. }
  146. })();