Skip to content

Commit 7cc74ff

Browse files
committed
Add shift-checker test
1 parent d8dacfb commit 7cc74ff

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

test/shift-checker.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { beforeEach, describe, it, TestContext } from 'node:test';
2+
import { Builder, ShiftChecker } from '../src/builder';
3+
4+
describe ('LLParse/ShiftChecker', () => {
5+
let b: Builder;
6+
let sc: ShiftChecker;
7+
8+
beforeEach(() => {
9+
b = new Builder();
10+
});
11+
12+
it('should prohibit undefined properties', (t: TestContext) => {
13+
const lshift = b.lshift("undefined", 1);
14+
const start = b.node('start');
15+
16+
start
17+
.otherwise(lshift);
18+
lshift.skipTo(start);
19+
sc = new ShiftChecker(b.properties);
20+
21+
t.assert.throws(() => {
22+
sc.check(start);
23+
}, /has not been defined for.*undefined/)
24+
});
25+
26+
it('should detect overflowing bits', (t: TestContext) => {
27+
const rshift = b.rshift("defined", 8);
28+
const start = b.node('start');
29+
30+
b.property('i8', "defined");
31+
32+
start
33+
.otherwise(rshift);
34+
rshift.skipTo(start);
35+
36+
37+
sc = new ShiftChecker(b.properties);
38+
t.assert.throws(() => {
39+
sc.check(start);
40+
}, /and will cause node .* to overflow./);
41+
42+
});
43+
44+
it('should detect inapproperate types', (t: TestContext) => {
45+
const rshift = b.rshift("defined", 8);
46+
const start = b.node('start');
47+
48+
b.property('ptr', "defined");
49+
50+
start
51+
.otherwise(rshift);
52+
rshift.skipTo(start);
53+
54+
55+
sc = new ShiftChecker(b.properties);
56+
t.assert.throws(() => {
57+
sc.check(start);
58+
}, /defined cannot be provided to ".*" because field was defined as a "ptr"/);
59+
});
60+
61+
it('should allow types that are smaller than itself', (t: TestContext) => {
62+
const rshift = b.rshift("defined", 2);
63+
const start = b.node('start');
64+
65+
/* hypothetically let's say we have an uint32_t in C but we only
66+
need to pack a i16 bit integer, this is valid use-case because we can safely
67+
back it even if the property is bigger than itself. */
68+
b.property('i32', "defined");
69+
70+
start
71+
.otherwise(rshift);
72+
rshift.skipTo(start);
73+
74+
75+
sc = new ShiftChecker(b.properties);
76+
t.assert.doesNotThrow(() => sc.check(start));
77+
});
78+
79+
80+
81+
})

0 commit comments

Comments
 (0)