You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.3 KiB

3 years ago
  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. private 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 $testCounter = 0;
  101. static $lastOutputTime = 0.0;
  102. static $currentSecond = 0;
  103. static $currentSecondTestCounter = 0;
  104. static $testsPerSecond = 0;
  105. $gmpResult = $callback($this->gmp);
  106. $bcmathResult = $callback($this->bcmath);
  107. $nativeResult = $callback($this->native);
  108. if ($gmpResult !== $bcmathResult) {
  109. self::failure('GMP', 'BCMath', $test);
  110. }
  111. if ($gmpResult !== $nativeResult) {
  112. self::failure('GMP', 'Native', $test);
  113. }
  114. $testCounter++;
  115. $currentSecondTestCounter++;
  116. $time = microtime(true);
  117. $second = (int) $time;
  118. if ($second !== $currentSecond) {
  119. $currentSecond = $second;
  120. $testsPerSecond = $currentSecondTestCounter;
  121. $currentSecondTestCounter = 0;
  122. }
  123. if ($time - $lastOutputTime >= 0.1) {
  124. echo "\r", number_format($testCounter), ' (', number_format($testsPerSecond) . ' / s)';
  125. $lastOutputTime = $time;
  126. }
  127. }
  128. /**
  129. * @param string $c1 The name of the first calculator.
  130. * @param string $c2 The name of the second calculator.
  131. * @param string $test A string representing the test being executed.
  132. */
  133. private static function failure(string $c1, string $c2, string $test) : void
  134. {
  135. echo PHP_EOL;
  136. echo 'FAILURE!', PHP_EOL;
  137. echo $c1, ' vs ', $c2, PHP_EOL;
  138. echo $test, PHP_EOL;
  139. die;
  140. }
  141. private function generateRandomNumber() : string
  142. {
  143. $length = random_int(1, $this->maxDigits);
  144. $number = '';
  145. for ($i = 0; $i < $length; $i++) {
  146. $number .= random_int(0, 9);
  147. }
  148. $number = ltrim($number, '0');
  149. if ($number === '') {
  150. return '0';
  151. }
  152. return $number;
  153. }
  154. })();