This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
320 changed files
with
29,088 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"cuyz\/global-ray","description":"Use Ray in any PHP script without requiring it in your projects.","authors":[{"name":"Romain Canon","email":"romain.hydrocanon@gmail.com"}],"require":{"spatie\/ray":"^1.0"},"scripts":{"refresh":["rm -rf build","bin\/php-scoper.phar add-prefix --force --output-dir=build_tmp","composer dump-autoload --optimize --no-scripts --classmap-authoritative --working-dir=build_tmp","php tests\/test.php","mv build_tmp build"]},"config":{"autoloader-suffix":"RayGlobalScoped"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
// autoload.php @generated by Composer | ||
|
||
require_once __DIR__ . '/composer/autoload_real.php'; | ||
|
||
return ComposerAutoloaderInitRayGlobalScoped::getLoader(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013-present Benjamin Morel | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
/** | ||
* This script stress tests calculators with random large numbers and ensures that all implementations return the same | ||
* results. It is designed to run in an infinite loop unless a bug is found. | ||
*/ | ||
declare (strict_types=1); | ||
namespace RayGlobalScoped; | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
use RayGlobalScoped\Brick\Math\Internal\Calculator; | ||
(new class(30) | ||
{ | ||
// max digits | ||
private $gmp; | ||
private $bcmath; | ||
private $native; | ||
private $maxDigits; | ||
public function __construct(int $maxDigits) | ||
{ | ||
$this->gmp = new \RayGlobalScoped\Brick\Math\Internal\Calculator\GmpCalculator(); | ||
$this->bcmath = new \RayGlobalScoped\Brick\Math\Internal\Calculator\BcMathCalculator(); | ||
$this->native = new \RayGlobalScoped\Brick\Math\Internal\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 (\RayGlobalScoped\Brick\Math\Internal\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 (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->add($a, $b); | ||
}); | ||
$this->test("{$a} - {$b}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->sub($a, $b); | ||
}); | ||
$this->test("{$a} * {$b}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->mul($a, $b); | ||
}); | ||
if ($b !== '0') { | ||
$this->test("{$a} / {$b}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->divQR($a, $b); | ||
}); | ||
$this->test("{$a} MOD {$b}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->mod($a, $b); | ||
}); | ||
} | ||
if ($b !== '0' && $b[0] !== '-') { | ||
$this->test("INV {$a} MOD {$b}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->modInverse($a, $b); | ||
}); | ||
} | ||
$this->test("GCD {$a}, {$b}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->gcd($a, $b); | ||
}); | ||
if ($a[0] !== '-') { | ||
$this->test("SQRT {$a}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->sqrt($a); | ||
}); | ||
} | ||
$this->test("{$a} AND {$b}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->and($a, $b); | ||
}); | ||
$this->test("{$a} OR {$b}", function (\RayGlobalScoped\Brick\Math\Internal\Calculator $c) use($a, $b) { | ||
return $c->or($a, $b); | ||
}); | ||
$this->test("{$a} XOR {$b}", function (\RayGlobalScoped\Brick\Math\Internal\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; | ||
} | ||
})(); |
Oops, something went wrong.