#2 Write Algo test
Showing
2 changed files
with
68 additions
and
0 deletions
tests/node/RelativePlacementSpec.js
0 → 100644
1 | /** | ||
2 | * Created by Techniv on 02/12/2016. | ||
3 | */ | ||
4 | var assert = require('assert'); | ||
5 | var RelativePlacement = require('../../relative-placement'); | ||
6 | /** @var {TestData[]} */ | ||
7 | var testDataList = require('./results_data.json'); | ||
8 | describe('RelativePlacement algo', () => { | ||
9 | |||
10 | var election; | ||
11 | |||
12 | beforeEach(()=>{ | ||
13 | election = new RelativePlacement(); | ||
14 | }); | ||
15 | |||
16 | describe('Process result data',() => { | ||
17 | |||
18 | testDataList.forEach( | ||
19 | /** | ||
20 | * @param {TestData} testData | ||
21 | * @param {Number} id | ||
22 | */ | ||
23 | (testData,id) => { | ||
24 | it('#'+id+': '+(testData.comment||''), ()=>{ | ||
25 | election.addCandidates(testData.result.concat().sort(()=>Math.random()-Math.random())); | ||
26 | var votes = compileVotes(testData); | ||
27 | |||
28 | votes.forEach((vote) => { | ||
29 | election.addVote(vote); | ||
30 | }); | ||
31 | |||
32 | assert.deepEqual(election.getResult(), testData.result); | ||
33 | }); | ||
34 | } | ||
35 | ); | ||
36 | }); | ||
37 | |||
38 | }); | ||
39 | |||
40 | /** | ||
41 | * @name TestData | ||
42 | * @typedef Object | ||
43 | * @property {String} comment | ||
44 | * @property {Array[]}votes | ||
45 | * @property {String[]} result | ||
46 | */ | ||
47 | |||
48 | /** | ||
49 | * @param {TestData} testData | ||
50 | * @return {String[][]} | ||
51 | */ | ||
52 | function compileVotes(testData){ | ||
53 | var votes = testData.votes; | ||
54 | var compileVotes = []; | ||
55 | var candidates = Object.keys(votes); | ||
56 | var voteNumber = votes[candidates[0]].length; | ||
57 | |||
58 | for(let i = 0; i < voteNumber; i++){ | ||
59 | let compileVote = []; | ||
60 | for(let name in votes){ | ||
61 | compileVote[votes[name][i]-1] = name; | ||
62 | } | ||
63 | compileVotes[i] = compileVote; | ||
64 | } | ||
65 | |||
66 | return compileVotes; | ||
67 | } | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment