Skip to content

Commit

Permalink
Fully convert Randomizers tests to Pest
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed May 24, 2024
1 parent c1f5df9 commit 452965b
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 145 deletions.
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');


/*
|--------------------------------------------------------------------------
Expand Down
248 changes: 106 additions & 142 deletions tests/src/Tools/Randomizers/ArrayRandomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
24 changes: 24 additions & 0 deletions tests/src/Tools/Randomizers/RandomizerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tests\src\Tools\Randomizers;

use Tests\CondorcetTestCase;

class RandomizerTestCase 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'],
];
}
5 changes: 2 additions & 3 deletions tests/src/Tools/Randomizers/VoteRandomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

declare(strict_types=1);

use CondorcetPHP\Condorcet\Tests\src\Tools\Randomizers\ArrayRandomizerTest;
use CondorcetPHP\Condorcet\Tools\Randomizers\{ArrayRandomizer, VoteRandomizer};
use CondorcetPHP\Condorcet\Utils\CondorcetUtil;

test('equivalence', function (): void {
$arrayRandomizer = new ArrayRandomizer(ArrayRandomizerTest::CANDIDATE_SET_1, ArrayRandomizerTest::SEED);
$votesRandomizer = new VoteRandomizer(ArrayRandomizerTest::CANDIDATE_SET_1, ArrayRandomizerTest::SEED);
$arrayRandomizer = new ArrayRandomizer(self::CANDIDATE_SET_1, self::SEED);
$votesRandomizer = new VoteRandomizer(self::CANDIDATE_SET_1, self::SEED);

for ($i = 0; $i < 5; $i++) {
expect(
Expand Down

0 comments on commit 452965b

Please sign in to comment.