�PNG  IHDR��;���IDATx��ܻn�0���K�� �)(�pA��� ���7�LeG{�� �§㻢|��ذaÆ 6lذaÆ 6lذaÆ 6lom��$^�y���ذag�5bÆ 6lذaÆ 6lذa{���� 6lذaÆ �`����}H�Fkm�,�m����Ӫ���ô�ô!� �x�|'ܢ˟;�E:���9�&ᶒ�}�{�v]�n&�6� �h��_��t�ڠ͵-ҫ���Z;��Z$�.�P���k�ž)�!��o���>}l�eQfJ�T��u і���چ��\��X=8��Rن4`Vw�l�>����n�G�^��i�s��"ms�$�u��i��?w�bs[m�6�K4���O���.�4��%����/����b�C%��t ��M�ז� �-l�G6�mrz2���s�%�9��s@���-�k�9�=���)������k�B5����\��+͂�Zsٲ ��Rn��~G���R���C����� �wIcI��n7jJ���hۛNCS|���j0��8y�iHKֶۛ�k�Ɉ+;Sz������L/��F�*\��Ԕ�#"5��m�2��[S��������=�g��n�a�P�e�ғ�L�� lذaÆ 6l�^k��̱aÆ 6lذaÆ 6lذa;���� �_��ذaÆ 6lذaÆ 6lذaÆ ���R���IEND�B` gmp = new Calculator\GmpCalculator(); $this->bcmath = new Calculator\BcMathCalculator(); $this->native = new Calculator\NativeCalculator(); $this->maxDigits = $maxDigits; } public function __invoke() : void { for (;;) { $a = $this->generateRandomNumber(); $b = $this->generateRandomNumber(); $c = $this->generateRandomNumber(); $this->runTests($a, $b); $this->runTests($b, $a); if ($a !== '0') { $this->runTests("-$a", $b); $this->runTests($b, "-$a"); } if ($b !== '0') { $this->runTests($a, "-$b"); $this->runTests("-$b", $a); } if ($a !== '0' && $b !== '0') { $this->runTests("-$a", "-$b"); $this->runTests("-$b", "-$a"); } if ($c !== '0') { $this->test("$a POW $b MOD $c", function(Calculator $calc) use($a, $b, $c) { return $calc->modPow($a, $b, $c); }); } } } /** * @param string $a The left operand. * @param string $b The right operand. */ private function runTests(string $a, string $b) : void { $this->test("$a + $b", function(Calculator $c) use($a, $b) { return $c->add($a, $b); }); $this->test("$a - $b", function(Calculator $c) use($a, $b) { return $c->sub($a, $b); }); $this->test("$a * $b", function(Calculator $c) use($a, $b) { return $c->mul($a, $b); }); if ($b !== '0') { $this->test("$a / $b", function(Calculator $c) use($a, $b) { return $c->divQR($a, $b); }); $this->test("$a MOD $b", function(Calculator $c) use($a, $b) { return $c->mod($a, $b); }); } if ($b !== '0' && $b[0] !== '-') { $this->test("INV $a MOD $b", function(Calculator $c) use($a, $b) { return $c->modInverse($a, $b); }); } $this->test("GCD $a, $b", function(Calculator $c) use($a, $b) { return $c->gcd($a, $b); }); if ($a[0] !== '-') { $this->test("SQRT $a", function(Calculator $c) use($a, $b) { return $c->sqrt($a); }); } $this->test("$a AND $b", function(Calculator $c) use($a, $b) { return $c->and($a, $b); }); $this->test("$a OR $b", function(Calculator $c) use($a, $b) { return $c->or($a, $b); }); $this->test("$a XOR $b", function(Calculator $c) use($a, $b) { return $c->xor($a, $b); }); } /** * @param string $test A string representing the test being executed. * @param Closure $callback A callback function accepting a Calculator instance and returning a calculation result. */ private function test(string $test, Closure $callback) : void { static $testCounter = 0; static $lastOutputTime = 0.0; static $currentSecond = 0; static $currentSecondTestCounter = 0; static $testsPerSecond = 0; $gmpResult = $callback($this->gmp); $bcmathResult = $callback($this->bcmath); $nativeResult = $callback($this->native); if ($gmpResult !== $bcmathResult) { self::failure('GMP', 'BCMath', $test); } if ($gmpResult !== $nativeResult) { self::failure('GMP', 'Native', $test); } $testCounter++; $currentSecondTestCounter++; $time = microtime(true); $second = (int) $time; if ($second !== $currentSecond) { $currentSecond = $second; $testsPerSecond = $currentSecondTestCounter; $currentSecondTestCounter = 0; } if ($time - $lastOutputTime >= 0.1) { echo "\r", number_format($testCounter), ' (', number_format($testsPerSecond) . ' / s)'; $lastOutputTime = $time; } } /** * @param string $c1 The name of the first calculator. * @param string $c2 The name of the second calculator. * @param string $test A string representing the test being executed. */ private static function failure(string $c1, string $c2, string $test) : void { echo PHP_EOL; echo 'FAILURE!', PHP_EOL; echo $c1, ' vs ', $c2, PHP_EOL; echo $test, PHP_EOL; die; } private function generateRandomNumber() : string { $length = random_int(1, $this->maxDigits); $number = ''; for ($i = 0; $i < $length; $i++) { $number .= random_int(0, 9); } $number = ltrim($number, '0'); if ($number === '') { return '0'; } return $number; } })();