RelativePlacementSpec.js 1.48 KB
/**
 * Created by Techniv on 02/12/2016.
 */
var assert = require('assert');
/** @type RelativePlacement.relativePlacement */
var relativePlacement = require('../../lib/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;
}