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+ } , / h a s n o t b e e n d e f i n e d f o r .* u n d e f i n e d / )
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+ } , / a n d w i l l c a u s e n o d e .* t o o v e r f l o w ./ ) ;
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+ } , / d e f i n e d c a n n o t b e p r o v i d e d t o " .* " b e c a u s e f i e l d w a s d e f i n e d a s a " p t r " / ) ;
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