-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
53 lines (42 loc) · 1.39 KB
/
test.js
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
import test from 'ava'
import { WrightSTV, Candidate, Vote } from './index'
const Horse = new Candidate('Horse')
const Turtle = new Candidate('Turtle')
const Lion = new Candidate('Lion')
const Monkey = new Candidate('Monkey')
const Snake = new Candidate('Snake')
const CANDIDATES = [Horse, Turtle, Lion, Monkey, Snake]
function constuctVotes(mapping) {
const votes = []
for (let [prefs, count] of mapping.entries()) {
for (let i = 0; i < count; i++) {
votes.push(new Vote(prefs))
}
}
return votes
}
test('simple case', async t => {
const conf = new Map()
conf.set([Horse, Turtle, Lion, Monkey, Snake], 12)
const votes = constuctVotes(conf)
const results = WrightSTV(3, votes, CANDIDATES)
t.deepEqual(results, [Horse, Turtle, Lion])
})
test('a slightly less simple case', async t => {
const conf = new Map()
conf.set([Horse], 6)
conf.set([Lion], 6)
const votes = constuctVotes(conf)
const results = WrightSTV(2, votes, CANDIDATES)
t.deepEqual(results, [Horse, Lion])
})
test('now we\'re really testing the alogirthm', async t => {
const conf = new Map()
conf.set([Horse, Turtle, Lion, Monkey, Snake], 20)
conf.set([Turtle, Horse, Lion, Monkey, Snake], 25)
conf.set([Lion, Monkey, Snake, Turtle, Horse], 16)
conf.set([Snake, Lion, Turtle, Monkey, Horse], 40)
const votes = constuctVotes(conf)
const results = WrightSTV(3, votes, CANDIDATES)
t.deepEqual(results, [Snake, Lion, Turtle])
})