Skip to content

Commit

Permalink
added tests for loops #145
Browse files Browse the repository at this point in the history
  • Loading branch information
tmptrash committed May 6, 2018
1 parent 6624153 commit 27b3ca9
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 58 deletions.
4 changes: 2 additions & 2 deletions client/src/vm/Operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Operators {
* depends on configuration. '...' means, that all other bits are
* ignored. Example:
*
* bits : 6 xx xx 2
* bits : 6 xx xx 4
* number: 100011 00 01 00...
* string: while (v0 < v1) {
*/
Expand Down Expand Up @@ -346,7 +346,7 @@ class Operators {

eval(`Operators.global.fn = function bracket(line, num, org, lines) {
const startLine = this.offs[line];
const operator = (lines[startLine] & ${opMask}) >>> Num.VAR_BITS_OFFS;
const operator = typeof startLine === 'undefined' ? -1 : (lines[startLine] & ${opMask}) >>> Num.VAR_BITS_OFFS;
if (operator === 0x3) {return this.offs[line]} // loop
if (operator === 0x5) { // func
const stack = this.stack;
Expand Down
236 changes: 180 additions & 56 deletions client/src/vm/OperatorsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("client/src/vm/Operators", () => {
vars = null;
});

describe('Creation and destroy', () => {
xdescribe('Creation and destroy', () => {
it('Checks creation', () => {
expect(ops.offs).toEqual(offs);
expect(ops.vars).toEqual(vars);
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("client/src/vm/Operators", () => {
});
});

describe('vars 2bits per var', () => {
xdescribe('vars 2bits per var', () => {
it('Checks v0=v1', () => {
expect(ops.operators[h('100000 00 01')].call(ops, 0)).toEqual(1);
expect(ops.vars).toEqual([1,1,2,3]);
Expand Down Expand Up @@ -145,7 +145,7 @@ describe("client/src/vm/Operators", () => {
});
});

describe('consts 2bits per var', () => {
xdescribe('consts 2bits per var', () => {
it('Checks v0=1', () => {
expect(ops.operators[h('100001 00')].call(ops, 0, h('100001 00 001 000000000000000000000'))).toEqual(1);
expect(ops.vars).toEqual([1,1,2,3]);
Expand Down Expand Up @@ -228,7 +228,7 @@ describe("client/src/vm/Operators", () => {
});
});

describe('conditions 2bits per var', () => {
xdescribe('ifs 2bits per var', () => {
it('if(v3!==v3) should be false', () => {
const code = [
h('100010 11 11 1110 000000000000000000'), // if (v3!==v3) {
Expand Down Expand Up @@ -256,58 +256,182 @@ describe("client/src/vm/Operators", () => {
ops.updateIndexes(code);
expect(ops.operators[h('100010 00 00 1111')].call(ops, 0)).toEqual(1);
});
it('Garbage in a tail should not affect if', () => {
const code = [
h('100010 00 01 0000 111111111111111111'), // if (v0+v1) {
h('100000 00 01 1111111111111111111111'), // v0 = v1
h('101000 11111111111111111111111111') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 11 11 0000')].call(ops, 0)).toEqual(1);
});
it('Test of one if and two closed brackets', () => {
const code = [
h('100010 11 11 1110 000000000000000000'), // if (v3!==v3) {
h('100000 00 01 1111111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000'), // }
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 11 11 1110')].call(ops, 0)).toEqual(3);
});
it('Test of one if and two closed brackets 2', () => {
const code = [
h('101000 00000000000000000000000000'), // }
h('100010 11 11 1110 000000000000000000'), // if (v3!==v3) {
h('100000 00 01 1111111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 11 11 1110')].call(ops, 1)).toEqual(4);
});
it('if(false) without closed bracket should go to the next line', () => {
const code = [
h('100010 11 11 1110 000000000000000000'), // if (v3!==v3) {
h('100000 00 01 1111111111111111111111') // v0 = v1
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 11 11 1110')].call(ops, 0)).toEqual(1);
});
it('if(true) without closed bracket should go to the next line', () => {
const code = [
h('100010 11 11 1101 000000000000000000'), // if (v3===v3) {
h('100000 00 01 1111111111111111111111') // v0 = v1
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 11 11 1101')].call(ops, 0)).toEqual(1);
});

describe('ifs 3bits per var', () => {
let bpv;
let ops;
let vars;
let offs;
beforeAll (() => {
bpv = OConfig.codeBitsPerVar;
OConfig.codeBitsPerVar = 3;
Operators.compile();
});
afterAll (() => Operators.compile());
beforeEach(() => {
vars = [0,1,2,3,4,5,6,7];
offs = new Array(10);
ops = new Operators(offs, vars);
});
afterEach (() => {
ops.destroy();
ops = null;
offs = null;
vars = null;
OConfig.codeBitsPerVar = bpv;
});

// describe('ifs 3bits per var', () => {
// let bpv;
// let ops;
// let vars;
// let offs;
// beforeAll (() => {
// bpv = OConfig.codeBitsPerVar;
// OConfig.codeBitsPerVar = 3;
// Operators.compile();
// });
// afterAll (() => Operators.compile());
// beforeEach(() => {
// vars = [0,1,2,3,4,5,6,7];
// offs = new Array(10);
// ops = new Operators(offs, vars);
// });
// afterEach (() => {
// ops.destroy();
// ops = null;
// offs = null;
// vars = null;
// OConfig.codeBitsPerVar = bpv;
// });
//
// it('Checks v0=1', () => {
// expect(ops.operators[h('100001 000')].call(ops, 0, h('100001 000 001 00000000000000000000'))).toEqual(1);
// expect(ops.vars).toEqual([1,1,2,3,4,5,6,7]);
// });
// it('Checks v0=0', () => {
// expect(ops.operators[h('100001 000')].call(ops, 0, h('100001 000 000 00000000000000000000'))).toEqual(1);
// expect(ops.vars).toEqual([0,1,2,3,4,5,6,7]);
// });
// it('Checks v0=3', () => {
// expect(ops.operators[h('100001 000')].call(ops, 0, h('100001 000 011 00000000000000000000'))).toEqual(1);
// expect(ops.vars).toEqual([3,1,2,3,4,5,6,7]);
// });
// it('Checks v7=7', () => {
// expect(ops.operators[h('100001 111')].call(ops, 0, h('100001 000 111 00000000000000000000'))).toEqual(1);
// expect(ops.vars).toEqual([0,1,2,3,4,5,6,7]);
// });
// it('Checks correct line return', () => {
// expect(ops.operators[h('100001 011')].call(ops, 0, h('100001 011 011 00000000000000000000'))).toEqual(1);
// expect(ops.operators[h('100001 011')].call(ops, 1, h('100001 011 001 00000000000000000000'))).toEqual(2);
// expect(ops.operators[h('100001 001')].call(ops, 100, h('100001 001 011 00000000000000000000'))).toEqual(101);
// });
// it('Garbage in a tail should not affect vars', () => {
// expect(ops.operators[h('100001 000')].call(ops, 0, h('100001 000 001 11111111111111111111'))).toEqual(1);
// expect(ops.vars).toEqual([1,1,2,3,4,5,6,7]);
// expect(ops.operators[h('100001 000')].call(ops, 1, h('100001 000 000 11111111111111111111'))).toEqual(2);
// expect(ops.vars).toEqual([0,1,2,3,4,5,6,7]);
// });
// });
it('if(v3!==v3) should be false', () => {
const code = [
h('100010 011 011 1110 0000000000000000'), // if (v3!==v3) {
h('100000 000 001 11111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 011 011 1110')].call(ops, 0)).toEqual(3);
});
it('if(v0+v1) should be true', () => {
const code = [
h('100010 000 001 0000 0000000000000000'), // if (v0+v1) {
h('100000 000 001 11111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 011 011 0000')].call(ops, 0)).toEqual(1);
});
it('if(v0<=v0) should be false', () => {
const code = [
h('100010 000 000 1111 0000000000000000'), // if (v0<=v0) {
h('100000 000 001 11111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 000 000 1111')].call(ops, 0)).toEqual(1);
});
it('Garbage in a tail should not affect if', () => {
const code = [
h('100010 000 001 0000 1111111111111111'), // if (v0+v1) {
h('100000 000 001 11111111111111111111'), // v0 = v1
h('101000 11111111111111111111111111') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 011 011 0000')].call(ops, 0)).toEqual(1);
});
it('Test of one if and two closed brackets', () => {
const code = [
h('100010 011 011 1110 0000000000000000'), // if (v3!==v3) {
h('100000 000 001 11111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000'), // }
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 011 011 1110')].call(ops, 0)).toEqual(3);
});
it('Test of one if and two closed brackets 2', () => {
const code = [
h('101000 00000000000000000000000000'), // }
h('100010 011 011 1110 0000000000000000'), // if (v3!==v3) {
h('100000 000 001 11111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('101000')].call(ops, 0, code[0])).toEqual(1);
expect(ops.operators[h('100010 011 011 1110')].call(ops, 1)).toEqual(4);
});
it('if(false) without closed bracket should go to the next line', () => {
const code = [
h('100010 011 011 1110 0000000000000000'), // if (v3!==v3) {
h('100000 000 001 11111111111111111111') // v0 = v1
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 011 011 1110')].call(ops, 0)).toEqual(1);
});
it('if(true) without closed bracket should go to the next line', () => {
const code = [
h('100010 011 011 1101 0000000000000000'), // if (v3===v3) {
h('100000 000 001 11111111111111111111') // v0 = v1
];
ops.updateIndexes(code);
expect(ops.operators[h('100010 011 011 1101')].call(ops, 0)).toEqual(1);
});
});
});

describe('loops 2bits per var', () => {
it('while() with false condition should go outside the closed bracket', () => {
const code = [
h('100011 11 11 1110 000000000000000000'), // while (v3!==v3) {
h('100000 00 01 1111111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100011 11 11 1110')].call(ops, 0)).toEqual(3);
});
it('while() with true condition should go to the next line', () => {
const code = [
h('100011 11 11 1101 000000000000000000'), // while (v3===v3) {
h('100000 00 01 1111111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100011 11 11 1101')].call(ops, 0)).toEqual(1);
});
it('while() with true at the beginning and false after', () => {
const code = [
h('100011 00 01 1110 000000000000000000'), // while (v0!==v1) {
h('100000 00 01 1111111111111111111111'), // v0 = v1
h('101000 00000000000000000000000000') // }
];
ops.updateIndexes(code);
expect(ops.operators[h('100011 00 01 1110')].call(ops, 0)).toEqual(1);
expect(ops.operators[h('100000 00 01')].call(ops, 1)).toEqual(2);
expect(ops.operators[h('101000')].call(ops, 2, code[2], {}, code)).toEqual(0);
expect(ops.operators[h('100011 00 01 1110')].call(ops, 0)).toEqual(3);
});
});
});

0 comments on commit 27b3ca9

Please sign in to comment.