From 452965b3d8df794a1fff7f0cd0f6b133df113e7f Mon Sep 17 00:00:00 2001 From: Julien Boudry Date: Fri, 24 May 2024 18:23:34 +0200 Subject: [PATCH] Fully convert Randomizers tests to Pest --- tests/Pest.php | 2 + .../Tools/Randomizers/ArrayRandomizerTest.php | 248 ++++++++---------- .../Tools/Randomizers/RandomizerTestCase.php | 24 ++ .../Tools/Randomizers/VoteRandomizerTest.php | 5 +- 4 files changed, 134 insertions(+), 145 deletions(-) create mode 100644 tests/src/Tools/Randomizers/RandomizerTestCase.php diff --git a/tests/Pest.php b/tests/Pest.php index 203a795e..875a3cbc 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -17,6 +17,8 @@ uses(Tests\CondorcetTestCase::class); uses(Tests\AlgoTestCase::class)->in('src/Algo'); +uses(Tests\src\Tools\Randomizers\RandomizerTestCase::class)->in('src/Tools/Randomizers'); + /* |-------------------------------------------------------------------------- diff --git a/tests/src/Tools/Randomizers/ArrayRandomizerTest.php b/tests/src/Tools/Randomizers/ArrayRandomizerTest.php index 2243bcac..8f6949a1 100644 --- a/tests/src/Tools/Randomizers/ArrayRandomizerTest.php +++ b/tests/src/Tools/Randomizers/ArrayRandomizerTest.php @@ -2,169 +2,133 @@ declare(strict_types=1); -namespace CondorcetPHP\Condorcet\Tests\src\Tools\Randomizers; - use CondorcetPHP\Condorcet\Tools\Randomizers\ArrayRandomizer; -use Tests\CondorcetTestCase; - -class ArrayRandomizerTest extends CondorcetTestCase -{ - public const SEED = 'CondorcetSeed'; - - public const CANDIDATE_SET_1 = [ - 'Candidate1', 'Candidate2', 'Candidate3', 'Candidate4', 'Candidate5', 'Candidate6', 'Candidate7', 'Candidate8', 'Candidate9', - ]; - - public const CANDIDATE_SET_2 = [ - 'Candidate1', 'Candidate2', 'Candidate3', - ]; - - public const CANDIDATE_SET_3 = [ - 'Candidate1', 'Candidate2', 'Candidate3', ['Candidate4'], - ]; - - - public function testDefaultRandomVotes(): void - { - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); - - for ($i = 0; $i < 10; $i++) { - $nv = $votesRandomizer->shuffle(); - - expect($nv)->not()->toEqual(self::CANDIDATE_SET_1); - expect($nv)->toHaveCount(\count(self::CANDIDATE_SET_1)); - } - } - - public function testMaxCandidatesRanked(): void - { - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); - $votesRandomizer->maxCandidatesRanked = 3; - - $original = \array_slice(self::CANDIDATE_SET_1, 0, 3); - - for ($i = 0; $i < 10; $i++) { - $nv = $votesRandomizer->shuffle(); - - expect($nv)->not()->toEqual($original); - expect($nv)->toHaveCount($votesRandomizer->maxCandidatesRanked); - } - } - - public function testMinCandidatesRanked(): void - { - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); - $votesRandomizer->minCandidatesRanked = 3; - $variations = []; +test('default random votes', function () { + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); - for ($i = 0; $i < 10; $i++) { - $nv = $votesRandomizer->shuffle(); - - expect($nv)->not()->toEqual(self::CANDIDATE_SET_1); - - $countNv = \count($nv); - $variations[] = $countNv; - - expect($countNv)->toBeGreaterThanOrEqual($votesRandomizer->minCandidatesRanked); - } + for ($i = 0; $i < 10; $i++) { + $nv = $votesRandomizer->shuffle(); - $variationsCount = \count(array_unique($variations)); - expect($variationsCount)->toBe(\count(self::CANDIDATE_SET_1) - $votesRandomizer->minCandidatesRanked + 1); + expect($nv)->not()->toEqual(self::CANDIDATE_SET_1); + expect($nv)->toHaveCount(\count(self::CANDIDATE_SET_1)); } +}); +test('max candidates ranked', function () { + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); + $votesRandomizer->maxCandidatesRanked = 3; - public function testMinAndMaxCandidatesRanked(): void - { - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); - $votesRandomizer->minCandidatesRanked = 3; - $votesRandomizer->maxCandidatesRanked = 6; + $original = \array_slice(self::CANDIDATE_SET_1, 0, 3); - $variations = []; - - for ($i = 0; $i < 10; $i++) { - $nv = $votesRandomizer->shuffle(); - - $countNv = \count($nv); - $variations[] = $countNv; - - expect($countNv)->toBeGreaterThanOrEqual($votesRandomizer->minCandidatesRanked); - expect($countNv)->toBeLessThanOrEqual($votesRandomizer->maxCandidatesRanked); - } + for ($i = 0; $i < 10; $i++) { + $nv = $votesRandomizer->shuffle(); - $variationsCount = \count(array_unique($variations)); - expect($variationsCount)->toBe($votesRandomizer->maxCandidatesRanked - $votesRandomizer->minCandidatesRanked + 1); + expect($nv)->not()->toEqual($original); + expect($nv)->toHaveCount($votesRandomizer->maxCandidatesRanked); } +}); +test('min candidates ranked', function () { + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); + $votesRandomizer->minCandidatesRanked = 3; - public function testAddedTies(): void - { - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_2, self::SEED); + $variations = []; - $votesRandomizer->tiesProbability = 100; + for ($i = 0; $i < 10; $i++) { $nv = $votesRandomizer->shuffle(); - expect($nv)->toHaveCount(2); - expect($nv[0])->toBeString(); - expect($nv[1])->toHaveCount(2); - $votesRandomizer->tiesProbability = 90; - $nv = $votesRandomizer->shuffle(); - expect($nv)->toHaveCount(2); - expect($nv[0])->toBeString(); - expect($nv[1])->toHaveCount(2); + expect($nv)->not()->toEqual(self::CANDIDATE_SET_1); - $votesRandomizer->tiesProbability = 70; - $nv = $votesRandomizer->shuffle(); - expect($nv)->toHaveCount(3); + $countNv = \count($nv); + $variations[] = $countNv; - $votesRandomizer->tiesProbability = 500; - $nv = $votesRandomizer->shuffle(); - expect($nv)->toHaveCount(1); - expect($nv[0])->toHaveCount(3); + expect($countNv)->toBeGreaterThanOrEqual($votesRandomizer->minCandidatesRanked); } - public function testAddedTiesWithArray1(): void - { - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_3, self::SEED); - - $votesRandomizer->tiesProbability = 100; - $nv = $votesRandomizer->shuffle(); - - expect($nv)->toHaveCount(3); - expect($nv[2])->toHaveCount(2); - } + $variationsCount = \count(array_unique($variations)); + expect($variationsCount)->toBe(\count(self::CANDIDATE_SET_1) - $votesRandomizer->minCandidatesRanked + 1); +}); +test('min and max candidates ranked', function () { + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); + $votesRandomizer->minCandidatesRanked = 3; + $votesRandomizer->maxCandidatesRanked = 6; - public function testAddedTiesWithArray2(): void - { - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); + $variations = []; - $votesRandomizer->tiesProbability = 500; + for ($i = 0; $i < 10; $i++) { $nv = $votesRandomizer->shuffle(); - expect($nv)->toHaveCount(4); - expect($nv[2])->toHaveCount(6); + $countNv = \count($nv); + $variations[] = $countNv; + + expect($countNv)->toBeGreaterThanOrEqual($votesRandomizer->minCandidatesRanked); + expect($countNv)->toBeLessThanOrEqual($votesRandomizer->maxCandidatesRanked); } - public function testSeeds(): void - { - // Test low seed - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, '42'); - expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[5]); - - // Test 32 bytes seed - $s = 'abcdefghijklmnopqrstuvwxyz123456'; - expect(\strlen($s))->toBe(32); - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, $s); - expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[6]); - - // Test custom Randomizer - $r = new \Random\Randomizer(new \Random\Engine\PcgOneseq128XslRr64('abcdefghijklmnop')); - $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, $r); - expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[4]); - - // Test secure engine - $votesRandomizer = new ArrayRandomizer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); - - for ($i = 0; $i < 3; $i++) { - expect($votesRandomizer->shuffle())->not()->toBe(self::CANDIDATE_SET_1); - } + $variationsCount = \count(array_unique($variations)); + expect($variationsCount)->toBe($votesRandomizer->maxCandidatesRanked - $votesRandomizer->minCandidatesRanked + 1); +}); +test('added ties', function () { + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_2, self::SEED); + + $votesRandomizer->tiesProbability = 100; + $nv = $votesRandomizer->shuffle(); + expect($nv)->toHaveCount(2); + expect($nv[0])->toBeString(); + expect($nv[1])->toHaveCount(2); + + $votesRandomizer->tiesProbability = 90; + $nv = $votesRandomizer->shuffle(); + expect($nv)->toHaveCount(2); + expect($nv[0])->toBeString(); + expect($nv[1])->toHaveCount(2); + + $votesRandomizer->tiesProbability = 70; + $nv = $votesRandomizer->shuffle(); + expect($nv)->toHaveCount(3); + + $votesRandomizer->tiesProbability = 500; + $nv = $votesRandomizer->shuffle(); + expect($nv)->toHaveCount(1); + expect($nv[0])->toHaveCount(3); +}); +test('added ties with array1', function () { + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_3, self::SEED); + + $votesRandomizer->tiesProbability = 100; + $nv = $votesRandomizer->shuffle(); + + expect($nv)->toHaveCount(3); + expect($nv[2])->toHaveCount(2); +}); +test('added ties with array2', function () { + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED); + + $votesRandomizer->tiesProbability = 500; + $nv = $votesRandomizer->shuffle(); + + expect($nv)->toHaveCount(4); + expect($nv[2])->toHaveCount(6); +}); +test('seeds', function () { + // Test low seed + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, '42'); + expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[5]); + + // Test 32 bytes seed + $s = 'abcdefghijklmnopqrstuvwxyz123456'; + expect(\strlen($s))->toBe(32); + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, $s); + expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[6]); + + // Test custom Randomizer + $r = new \Random\Randomizer(new \Random\Engine\PcgOneseq128XslRr64('abcdefghijklmnop')); + $votesRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, $r); + expect($votesRandomizer->shuffle()[0])->toBe(self::CANDIDATE_SET_1[4]); + + // Test secure engine + $votesRandomizer = new ArrayRandomizer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); + + for ($i = 0; $i < 3; $i++) { + expect($votesRandomizer->shuffle())->not()->toBe(self::CANDIDATE_SET_1); } -} +}); diff --git a/tests/src/Tools/Randomizers/RandomizerTestCase.php b/tests/src/Tools/Randomizers/RandomizerTestCase.php new file mode 100644 index 00000000..f51a3276 --- /dev/null +++ b/tests/src/Tools/Randomizers/RandomizerTestCase.php @@ -0,0 +1,24 @@ +