Skip to content

Commit

Permalink
Fixed Program's fromCode() and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ramadis committed Jul 28, 2016
1 parent 3f31517 commit 81bfaf7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/Errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const ERROR = {
INVALID_STR: { code: 4, msg: 'THE STRING PROVIDED IS NOT ACTUALLY A STRING' },
INVALID_OPER: { code: 5, msg: 'THERE IS NO COMMAND IN THE STRING' },
INVALID_INSTRUCTION: { code: 6, msg: 'PARAMETERS PASSED ARE NOT AN INSTRUCTION' },
INVALID_CODE: { code: 7, msg: 'THE CODE PROVIDED IS NOT A WELL FORMED CODE' }
};
44 changes: 20 additions & 24 deletions src/Slang.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,32 +172,28 @@ class Program {
1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907];
}

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) => {
let acc = 0;
while (num && num % prime === 0) { acc++; num /= prime; }
return acc;
}

// Empty instruction patch
if (code === 1) {
return new Program([Instruction.fromCode(0)]);
}
static fromCode (code) {
if (!code || typeof code !== "string") throw new Error(ERROR.INVALID_STR.msg);
code = code.toLowerCase().trim();

while (code !== 1) {
if (code % Program.primes[primeIdx] === 0) {
const prime = Program.primes[primeIdx];
const exp = primeValue(code, prime);
instructions.push(Instruction.fromCode(exp));
code /= Math.pow(prime, exp);
}
primeIdx++;
}
const codeModes = {
PRIMES: /^(\d+\^\d+ )+- ?1$/,
GODEL: /^\[ (\d+ )+\] ?- ?1/
};

return new Program(instructions);
if (code.match(codeModes.PRIMES)) {
code = code.split(' ')
.map((pairs) => pairs.split('^')[1])
.filter((exp) => exp);
} else if (code.match(codeModes.GODEL)) {
code = code.replace('[', '')
.replace(']', '')
.split(' ')
.filter((exp) => exp);
code.pop(); code.pop();
} else throw new Error(ERROR.INVALID_CODE.msg);

return new Program(code.map((codeInst) => Instruction.fromCode(codeInst)));
}

static fromString (str) {
Expand Down
53 changes: 52 additions & 1 deletion test/Program.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ describe('Program: getCode(Program.codeModes.NUMBER)', () => {
});

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

// 15 randomly generated instructions
Expand All @@ -114,3 +113,55 @@ describe('Program: equals()', () => {
});
});
});

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

const invalidExpects = [
{ input: '()adsf' },
{ input: 1 },
{ input: new Instruction(0, 0, 0) },
{ input: new Program([new Instruction(0, 0, 0)]) },
{ input: '[a a]' },
{ input: '[]' },
{ input: '^^^' },
{ input: 'a^' },
{ input: 'a^ -1' }
];

invalidExpects.forEach((invalidExpect) => {
it(`${invalidExpect.input} => ERROR`, () => {
expect(() => Program.fromCode(invalidExpect.input)).to.throw(Error);
});
});

});


describe('Program: fromCode(Program.codeModes.PRIME)', () => {
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 get correct Program from code`, () => {
const shuffledInsts = instructions.sort(() => 0.5 - Math.random());
expect(Program.fromCode(new Program(shuffledInsts).getCode())).to.deep.equal(new Program(shuffledInsts));
});
}
});

describe('Program: fromCode(Program.codeModes.GODEL)', () => {
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 get correct Program from code`, () => {
const shuffledInsts = instructions.sort(() => 0.5 - Math.random());
expect(Program.fromCode(new Program(shuffledInsts).getCode())).to.deep.equal(new Program(shuffledInsts));
});
}
});

0 comments on commit 81bfaf7

Please sign in to comment.