-
Notifications
You must be signed in to change notification settings - Fork 3
/
RankedPairsCalculatorTest.php
331 lines (280 loc) · 12 KB
/
RankedPairsCalculatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace PivotLibre\Tideman;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use PivotLibre\Tideman\CandidateList;
use PivotLibre\Tideman\RankedPairsCalculator;
class RankedPairsCalculatorTest extends TestCase
{
private const ALICE_ID = "A";
private const BOB_ID = "B";
private const CLAIRE_ID = "C";
private const DAVE_ID = "D";
private $alice;
private $bob;
private $claire;
private $dave;
private $tieBreakingBallot;
private $parser;
protected function setUp()
{
$this->parser = new CandidateRankingParser();
$this->alice = new Candidate(self::ALICE_ID);
$this->bob = new Candidate(self::BOB_ID);
$this->claire = new Candidate(self::CLAIRE_ID);
$this->dave = new Candidate(self::DAVE_ID);
//construct a ballot that has no ties and that will break ties in alphabetical order
$this->tieBreakingBallot = new Ballot(
...$this->parser->parse("A>B>C>D")
);
}
protected function assertOnlyContainsCandidates(CandidateList $actualCandidates, array $expectedCandidates)
{
$actualCandidates = $actualCandidates->toArray();
$this->assertEquals(sizeof($expectedCandidates), sizeof($actualCandidates));
foreach ($expectedCandidates as $expectedCandidate) {
$this->assertContains($expectedCandidate, $actualCandidates);
}
}
protected function cloneBallot(NBallot $ballot, int $numBallots) : array
{
$ballots = array();
for ($i = 0; $i < $numBallots; $i++) {
$ballots[] = clone $ballot;
}
return $ballots;
}
public function testConstructionSucceedsWhenTieBreakingBallotContainsTies() : void
{
$ballotWithTies = new Ballot(
...$this->parser->parse("A>B>C=D")
);
$this->assertNotNull(new RankedPairsCalculator($ballotWithTies));
}
public function testConstructionSucceedsWhenTieBreakingBallotContainsNoTies() : void
{
$instance = new RankedPairsCalculator($this->tieBreakingBallot);
$this->assertNotNull($instance);
}
public function testOneBallotWithoutTies() : void
{
$ballot = new NBallot(
1,
...$this->parser->parse(
"A>B>C"
)
);
$ballots = [$ballot];
$instance = new RankedPairsCalculator($this->tieBreakingBallot);
$actualWinners = $instance->calculate(-1, null, ...$ballots);
$expectedWinners = $this->parser->parse("A>B>C");
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
}
public function testOneNBallotWithTenCountWithoutTies() : void
{
$ballot = new NBallot(10, ...$this->parser->parse(
"A>B>C"
));
$ballots = [$ballot];
$instance = new RankedPairsCalculator($this->tieBreakingBallot);
$actualWinners = $instance->calculate(-1, null, ...$ballots);
$expectedWinners = $this->parser->parse("A>B>C");
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
}
public function testTenBallotsWithoutTies() : void
{
$ballot = new NBallot(1, ...$this->parser->parse(
"A>B>C"
));
$ballots = $this->cloneBallot($ballot, 10);
$instance = new RankedPairsCalculator($this->tieBreakingBallot);
$actualWinners = $instance->calculate(-1, null, ...$ballots);
$expectedWinners = $this->parser->parse("A>B>C");
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
}
public function testOneBallotWithTies() : void
{
$ballot = new NBallot(1, ...$this->parser->parse("A>B=C"));
$ballots = [$ballot];
$instance = new RankedPairsCalculator($this->tieBreakingBallot);
$actualWinners = $instance->calculate(3, null, ...$ballots);
$expectedWinners = $this->parser->parse("A>B=C");
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
}
public function testTenBallotsWithTies() : void
{
$ballot = new NBallot(1, ...$this->parser->parse(
"A>B=C"
));
$ballots = $this->cloneBallot($ballot, 10);
$instance = new RankedPairsCalculator($this->tieBreakingBallot);
$actualWinners = $instance->calculate(3, null, ...$ballots);
$expectedWinners = $this->parser->parse("A>B=C");
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
}
public function testOneNBallotsWithTenCountAndWithTies() : void
{
$candidateListsForBallot = [
new CandidateList($this->alice),
new CandidateList($this->bob, $this->claire)
];
$ballot = new NBallot(10, ...$candidateListsForBallot);
$ballots = [$ballot];
$instance = new RankedPairsCalculator($this->tieBreakingBallot);
$actualWinners = $instance->calculate(3, null, ...$ballots);
$expectedWinners = $this->parser->parse("A>B=C");
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
}
public function testPairTieBreakingWithNonTiedBallot() : void
{
$candidateListsForBallot = [
new CandidateList($this->alice),
new CandidateList($this->bob),
new CandidateList($this->claire)
];
$ballot = new NBallot(1, ...$candidateListsForBallot);
$candidateListsForOppositeBallot = [
new CandidateList($this->claire),
new CandidateList($this->bob),
new CandidateList($this->alice)
];
$oppositeBallot = new NBallot(1, ...$candidateListsForBallot);
$instance = new RankedPairsCalculator($this->tieBreakingBallot);
$actualWinners = $instance->calculate(3, null, $ballot, $oppositeBallot);
$expectedWinners = $this->parser->parse("A>B>C");
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
}
public function testPairTieBreakingWitTiedBallot() : void
{
$this->markTestSkipped('This test should be restored once candidate ties are surfaced');
$candidateListsForBallot = [
new CandidateList($this->alice),
new CandidateList($this->bob),
new CandidateList($this->claire)
];
$ballot = new NBallot(1, ...$candidateListsForBallot);
$candidateListsForOppositeBallot = [
new CandidateList($this->claire),
new CandidateList($this->bob),
new CandidateList($this->alice)
];
$oppositeBallot = new NBallot(1, ...$candidateListsForOppositeBallot);
$tiedTieBreakingCandidateList = new CandidateList(
$this->claire,
$this->bob,
$this->alice
);
$tiedTieBreakingBallot = new Ballot($tiedTieBreakingCandidateList);
//seed the random number generator so that results are consistent across tests
try {
mt_srand(485);
$instance = new RankedPairsCalculator($tiedTieBreakingBallot);
$actualWinners = $instance->calculate(3, null, $ballot, $oppositeBallot);
} finally {
mt_srand();
}
$expectedWinners = new CandidateList($this->alice, $this->claire, $this->bob);
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
//ensure that randomness is affecting the result by re-calculating with a different
//seed on the random number generator, and asserting different results
try {
mt_srand(95);
$instance = new RankedPairsCalculator($tiedTieBreakingBallot);
$actualWinners = $instance->calculate(3, null, $ballot, $oppositeBallot);
} finally {
mt_srand();
}
$expectedWinners = new CandidateList($this->claire, $this->bob, $this->alice);
$this->assertEquals($expectedWinners, $actualWinners->getRanking());
}
public function testCustomAgendaRestrictsResultsToSubsetOfCandidates()
{
//ballot contains A, B, and C...
$ballots = [
(new NBallotParser())->parse('A>C>B')
];
//but we set the agenda to only include two of them
$agenda = new Agenda();
$agenda->addCandidates($this->alice);
$agenda->addCandidates($this->bob);
//and we expect the result to only contain two of them
$expectedRanking = (new CandidateRankingParser())->parse('A>B');
$tieBreakingBallot = new Ballot(...$ballots[0]);
$instance = new RankedPairsCalculator($tieBreakingBallot);
$results = $instance->calculate(-1, $agenda, ...$ballots);
$this->assertEquals($expectedRanking, $results->getRanking());
}
public function testTideman1987Example5()
{
$expectedRanking = (new CandidateRankingParser())->parse("V>W>X>Y>Z");
$ballots = (new TestScenarioTideman1987Example2())->getBallots();
$tieBreakingBallot = new Ballot(...$ballots[0]);
$instance = new RankedPairsCalculator($tieBreakingBallot);
$results = $instance->calculate(-1, null, ...$ballots);
$actualRanking = $results->getRanking();
$this->assertEquals($expectedRanking, $actualRanking);
}
/**
* Scenario 1 from the test spreadsheet
* https://docs.google.com/spreadsheets/d/1634wP6-N8GG2Fig-yjIOk7vPBn4AijXOrjq6Z2T1K8M/edit?usp=sharing
* Drawing of graph:
* https://docs.google.com/drawings/d/1mtGlWgqr_h85qdvqSjC9eK0bRzbGYas-sLhuzAiDd3I/edit?usp=sharing
*/
public function testScenario1()
{
$expectedRanking = (new CandidateRankingParser())->parse("MM>SY=DD>YW>RR");
$ballots = (new TestScenario1())->getBallots();
$tieBreakingBallot = new Ballot(...$ballots[0]);
$instance = new RankedPairsCalculator($tieBreakingBallot);
$results = $instance->calculate(-1, null, ...$ballots);
$actualRanking = $results->getRanking();
$this->assertEquals($expectedRanking, $actualRanking);
}
/**
* Scenario 2 from the test spreadsheet
* https://docs.google.com/spreadsheets/d/1634wP6-N8GG2Fig-yjIOk7vPBn4AijXOrjq6Z2T1K8M/edit?usp=sharing
*/
public function testScenario2()
{
$expectedRanking = (new CandidateRankingParser())->parse("MM>BT>FE=CS>RR");
$ballots = (new TestScenario2())->getBallots();
$tieBreakingBallot = new Ballot(...$ballots[0]);
$instance = new RankedPairsCalculator($tieBreakingBallot);
$results = $instance->calculate(-1, null, ...$ballots);
$actualRanking = $results->getRanking();
$this->assertEquals($expectedRanking, $actualRanking);
}
/**
* Scenario 3 from the test spreadsheet
* https://docs.google.com/spreadsheets/d/1634wP6-N8GG2Fig-yjIOk7vPBn4AijXOrjq6Z2T1K8M/edit?usp=sharing
*/
public function testScenario3()
{
$expectedRanking = (new CandidateRankingParser())->parse("MN>MC>BT>FE=CS>RR");
$ballots = (new TestScenario3())->getBallots();
$tieBreakingBallot = new Ballot(...$ballots[0]);
$instance = new RankedPairsCalculator($tieBreakingBallot);
$results = $instance->calculate(-1, null, ...$ballots);
$actualRanking = $results->getRanking();
$this->assertEquals($expectedRanking, $actualRanking);
}
/**
* Scenario 4 from the test spreadsheet
* https://docs.google.com/spreadsheets/d/1634wP6-N8GG2Fig-yjIOk7vPBn4AijXOrjq6Z2T1K8M/edit?usp=sharing
*/
public function testScenario4()
{
$expectedRanking = (new CandidateRankingParser())->parse("CW>BB>CS>BT=SY");
$ballots = (new TestScenario4())->getBallots();
$tieBreakingBallot = new Ballot(...$ballots[0]);
$instance = new RankedPairsCalculator($tieBreakingBallot);
$results = $instance->calculate(-1, null, ...$ballots);
$actualRanking = $results->getRanking();
$this->assertEquals($expectedRanking, $actualRanking);
}
public function testBadUnknownCandidateBehavior()
{
$this->expectException(InvalidArgumentException::class);
new RankedPairsCalculator(new Ballot(), 'FOOBAR');
}
}