displayResults.js 2.24 KB
/**
 * Created by Techniv on 07/12/2016.
 */
var RelativePlacement = require('../../lib/relative-placement');
var data = require('./results_data.json');
var util = require('util');

console.log('############################');
console.log('# Details of result matrix #');
console.log('############################');
console.log('');
console.log('The cadidate are display in order of placement');
console.log('');
console.log('Column:');
console.log('Expected | (*error)Candidate\t[Votes]\t[Sum matrix (Sum score)]');
console.log('');
console.log('');
console.log('');
console.log('');

data.forEach((testCase, num) => {
    console.log("#"+ num + ": "+testCase.comment);

    var election = new RelativePlacement();
    var votes = compileVotes(testCase);

    election.addCandidates(votes[0]);
    election.addVotes(votes);

    var result = election.getResult();
    var expected = testCase.result;
    var candidates = election.candidateList;

    result.forEach((candidateName, place) => {
        var log = [];
        var candidate = candidates[candidateName];

        log.push(expected[place]);
        log.push('\t|\t');
        (candidateName != expected[place]) ? log.push('*') : log.push(' ');
        log.push(candidateName);
        log.push('\t');
        log.push(util.format(candidate.votes));
        log.push('\t[');
        for (var i = 0; i < candidate.placements.length; i++){
            log.push(i==0 ? ' ' : ', ');
            log.push(candidate.placements[i]);
            log.push('(');
            log.push(candidate.cumulativePlacement[i]);
            log.push(') ');
        }
        log.push(' ]');

        console.log(log.join(''));
    });

    console.log('-------------------------------------------------------------------');
    console.log('');
    console.log('');
});

/**
 * @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;
}