RelativePlacementSpec.js 1.62 KB
/**
 * Created by Techniv on 02/12/2016.
 */
var assert = require('assert');
var RelativePlacement = require('../../relative-placement');
/** @var {TestData[]} */
var testDataList = require('./results_data.json');
describe('RelativePlacement algo', () => {

    var election;

    beforeEach(()=>{
        election = new RelativePlacement();
    });

    describe('Process result data',() => {

        testDataList.forEach(
            /**
             * @param {TestData} testData
             * @param {Number} id
             */
            (testData,id) => {
                it('#'+id+': '+(testData.comment||''), ()=>{
                    election.addCandidates(testData.result.concat().sort(()=>Math.random()-Math.random()));
                    var votes = compileVotes(testData);

                    votes.forEach((vote) => {
                        election.addVote(vote);
                    });

                    assert.deepEqual(election.getResult(), testData.result);
                });
            }
        );
    });

});

/**
 * @name TestData
 * @typedef Object
 * @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;
}