From 3f315171ee2d6417768c66153ba15fffbcf64584 Mon Sep 17 00:00:00 2001 From: Rama Olivera Fedi Date: Thu, 28 Jul 2016 19:09:14 -0300 Subject: [PATCH] Added tests for Product class --- src/Slang.js | 15 ++++-- test/Program.spec.js | 116 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 test/Program.spec.js diff --git a/src/Slang.js b/src/Slang.js index e3a1989..c2a9bbb 100644 --- a/src/Slang.js +++ b/src/Slang.js @@ -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; @@ -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; } @@ -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) => { @@ -181,7 +183,7 @@ class Program { } // Empty instruction patch - if (code === 0) { + if (code === 1) { return new Program([Instruction.fromCode(0)]); } @@ -220,6 +222,7 @@ class Program { case Program.codeModes.GODEL: code = '[ '; this.instructions.forEach((instruction) => code += `${instruction.getCode()} `); + code += '] '; break; case Program.codeModes.NUMBER: @@ -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 }; \ No newline at end of file diff --git a/test/Program.spec.js b/test/Program.spec.js new file mode 100644 index 0000000..faae54a --- /dev/null +++ b/test/Program.spec.js @@ -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; + }); + }); +});