Skip to content

Commit

Permalink
Added tests for Product class
Browse files Browse the repository at this point in the history
  • Loading branch information
ramadis committed Jul 28, 2016
1 parent 0eba4d9 commit 3f31517
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Slang.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Instruction {
}

static fromString (str) {
if (!str || typeof str != 'string') throw new Error(ERROR.INVALID_STR.msg);
if (!str) return;
if (typeof str != 'string') throw new Error(ERROR.INVALID_STR.msg);

let [label, operation] = str.toLowerCase().split(' ');
operation = operation ? operation : label;
Expand Down Expand Up @@ -141,7 +142,7 @@ class Pairing {
// Represents the program. Its the main class.
// Holds an array of instructions.
class Program {
constructor (instructions) {
constructor (instructions = []) {
this.instructions = instructions;
}

Expand Down Expand Up @@ -172,6 +173,7 @@ class Program {
}

static fromCode (code = 0) {
if (!Number.isInteger(code) || code < 0) throw new Error(ERROR.INVALID_NUM.msg);
code += 1; let primeIdx = 0; const instructions = [];

const primeValue = (num, prime) => {
Expand All @@ -181,7 +183,7 @@ class Program {
}

// Empty instruction patch
if (code === 0) {
if (code === 1) {
return new Program([Instruction.fromCode(0)]);
}

Expand Down Expand Up @@ -220,6 +222,7 @@ class Program {
case Program.codeModes.GODEL:
code = '[ ';
this.instructions.forEach((instruction) => code += `${instruction.getCode()} `);
code += '] ';
break;

case Program.codeModes.NUMBER:
Expand All @@ -236,9 +239,13 @@ class Program {

equals (program) {
if (program && program instanceof Program) {
return program.getCode(Program.codeModes.NUMBER) === this.getCode(Program.codeModes.NUMBER);
return program.getCode() === this.getCode();
}
}

toString () {
return `${ this.getCode(Program.codeModes.NUMBER) }`;
}
};

module.exports = { Program, Instruction, Pairing, ERROR };
116 changes: 116 additions & 0 deletions test/Program.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Program } from './all';

describe('Program: getString()', () => {
const instructions = [];

// 15 randomly generated instructions
for (let j = 0; j < 15; j++) instructions.push(Instruction.fromCode(Math.round(Math.random() * 100)));

for (let i = 0; i < 7; i++) {
const shuffledInsts = instructions.sort(() => 0.5 - Math.random());
const program = new Program(shuffledInsts);
it(`Should shout the same string`, () => {
expect(program.getString()).to.equal(shuffledInsts.map((inst) => inst.getString()).join('\n'));
});
}
});

describe('Program: fromString()', () => {
const instructions = [];

// 15 randomly generated instructions
for (let j = 0; j < 15; j++) instructions.push(Instruction.fromCode(Math.round(Math.random() * 100)));

for (let i = 0; i < 7; i++) {
it(`Should shout the same program`, () => {
const shuffledInsts = instructions.sort(() => 0.5 - Math.random());
const programString = shuffledInsts.map((inst) => inst.getString()).join('\n');

expect(Program.fromString(programString)).to.deep.equal(new Program(shuffledInsts));
});
}
});

describe('Program: getCode(Program.codeModes.PRIMES)', () => {
const primes = Program.primes.slice(0, 15);
const instructions = [];

// 15 randomly generated instructions
for (let j = 0; j < 15; j++) instructions.push(Instruction.fromCode(Math.round(Math.random() * 100)));

for (let i = 0; i < 7; i++) {
it(`Should shout the same code`, () => {
const shuffledInsts = instructions.sort(() => 0.5 - Math.random());
const program = new Program(shuffledInsts);
const response = primes.map((prime, idx) => `${prime}^${shuffledInsts[idx].getCode()}`).join(' ') + " - 1";

expect(program.getCode(Program.codeModes.PRIMES)).to.equal(response);
});
}
});

describe('Program: getCode(Program.codeModes.GODEL)', () => {
const primes = Program.primes.slice(0, 15);
const instructions = [];

// 15 randomly generated instructions
for (let j = 0; j < 15; j++) instructions.push(Instruction.fromCode(Math.round(Math.random() * 100)));

for (let i = 0; i < 7; i++) {
it(`Should shout the same code`, () => {
const shuffledInsts = instructions.sort(() => 0.5 - Math.random());
const program = new Program(shuffledInsts);
const response = "[ " + primes.map((prime, idx) => `${shuffledInsts[idx].getCode()}`).join(' ') + " ] - 1";

expect(program.getCode(Program.codeModes.GODEL)).to.equal(response);
});
}
});

describe('Program: getCode(Program.codeModes.NUMBER)', () => {
const primes = Program.primes.slice(0, 15);
const instructions = [];

// 15 randomly generated instructions
for (let j = 0; j < 15; j++) instructions.push(Instruction.fromCode(Math.round(Math.random() * 100)));

for (let i = 0; i < 7; i++) {
it(`Should shout the same code`, () => {
const shuffledInsts = instructions.sort(() => 0.5 - Math.random());
const program = new Program(shuffledInsts);
let code = 0;
primes.forEach((prime, idx) => code += Math.pow(prime, shuffledInsts[idx].getCode()));

expect(program.getCode(Program.codeModes.NUMBER)).to.equal(code - 1);
});
}
});

describe('Program: equals()', () => {
const primes = Program.primes.slice(0, 15);
const instructions = [];

// 15 randomly generated instructions
for (let j = 0; j < 15; j++) instructions.push(Instruction.fromCode(Math.round(Math.random() * 100)));

for (let i = 0; i < 7; i++) {
it(`Should prove programs are equal`, () => {
const shuffledInsts = instructions.sort(() => 0.5 - Math.random());
expect(new Program(shuffledInsts)).to.deep.equal(new Program(shuffledInsts));
});
}

const invalidExpects = [
{ input: null },
{ input: 1 },
{ input: new Instruction(0, 0, 0) },
{ input: new Program([new Instruction(0, 0, 0)]) }
];

const program = new Program(instructions);
invalidExpects.forEach((invalidExpect) => {
it(`${invalidExpect.input} =/> ${program}`, () => {
expect(program.equals(invalidExpect.input)).to.not.be.ok;
});
});
});

0 comments on commit 3f31517

Please sign in to comment.