RelativePlacementSpec.js
1.48 KB
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
/**
* Created by Techniv on 02/12/2016.
*/
var assert = require('assert');
/** @type RelativePlacement.relativePlacement */
var relativePlacement = require('../../relative-placement').relativePlacement;
/** @var {TestData[]} */
var testDataList = require('./results_data.json');
describe('RelativePlacement algo', () => {
describe('Process result data',() => {
testDataList.forEach(
/**
* @param {TestData} testData
* @param {Number} id
*/
(testData,id) => {
testData.id = id;
it('#'+id+': '+(testData.comment||''), ()=>{
var votes = compileVotes(testData);
var result = relativePlacement(votes);
assert.deepEqual(result, testData.result);
});
}
);
});
});
/**
* @name TestData
* @typedef Object
* @property {Number} id
* @property {String} comment
* @property {Array[]}votes
* @property {String[]} result
*/
/**
* @param {TestData} testData
* @return {String[][]}
*/
function compileVotes(testData){
var votes = testData.votes;
var compileVotes = [];
var candidates = Object.keys(votes);
var voteNumber = votes[candidates[0]].length;
for(let i = 0; i < voteNumber; i++){
let compileVote = [];
for(let name in votes){
compileVote[votes[name][i]-1] = name;
}
compileVotes[i] = compileVote;
}
return compileVotes;
}