-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.test.ts
227 lines (179 loc) · 5.96 KB
/
struct.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { assertEquals, assertStrictEquals, assertThrows } from '@std/assert';
import { LITTLE_ENDIAN } from './endian.ts';
import { int8 } from './int/8.ts';
import { member, pad } from './member.ts';
import { Struct } from './struct.ts';
import { getByteOffset } from './util.ts';
Deno.test('Struct: buffer', () => {
const buffer = new ArrayBuffer(0);
assertStrictEquals(new Struct(buffer).buffer, buffer);
// Non-ArrayBuffer throws immediately.
assertThrows(
() => new Struct(new Uint8Array() as ArrayBufferLike),
TypeError,
);
});
Deno.test('Struct: byteLength', () => {
class Test extends Struct {
public static override readonly BYTE_LENGTH: number = 8;
}
const data = new Uint8Array(16);
assertEquals(new Test(data.buffer, 4).byteLength, 8);
});
Deno.test('Struct: byteOffset', () => {
const MAX = Number.MAX_SAFE_INTEGER;
class Test extends Struct {
declare public alpha: number;
public static override readonly BYTE_LENGTH: number = ((o) => {
o += int8(this, 'alpha', o);
return o;
})(super.BYTE_LENGTH);
}
const buffer = new ArrayBuffer(32);
assertEquals(new Test(buffer, 4).byteOffset, 4);
assertEquals(new Test(buffer, 3.14).byteOffset, 3);
assertEquals(new Test(buffer, 3.99).byteOffset, 3);
assertEquals(new Test(buffer, Number.EPSILON).byteOffset, 0);
assertEquals(new Test(buffer, 1 - Number.EPSILON).byteOffset, 0);
assertEquals(new Test(buffer, NaN).byteOffset, 0);
assertEquals(new Test(buffer, MAX).byteOffset, MAX);
assertEquals(new Test(buffer, 32).byteOffset, 32);
assertEquals(new Test(buffer, -1).byteOffset, -1);
// Impossible offset throws immediately.
assertThrows(() => new Test(buffer, MAX + 1), RangeError);
assertThrows(() => new Test(buffer, MAX + .5), RangeError);
assertThrows(() => new Test(buffer, Infinity), RangeError);
assertThrows(() => new Test(buffer, -Infinity), RangeError);
// Offset over buffer size throws lazy.
assertThrows(() => new Test(buffer, 32).alpha, RangeError);
assertThrows(() => new Test(buffer, 33).alpha, RangeError);
});
Deno.test('Struct: littleEndian', () => {
const buffer = new ArrayBuffer(32);
assertEquals(new Struct(buffer).littleEndian, LITTLE_ENDIAN);
assertEquals(new Struct(buffer, 0, true).littleEndian, true);
assertEquals(new Struct(buffer, 0, false).littleEndian, false);
});
Deno.test('Struct: BYTE_LENGTH', () => {
assertEquals(Struct.BYTE_LENGTH, 0);
});
Deno.test('Struct: MEMBERS', () => {
class A extends Struct {}
class B extends A {}
class C extends B {}
assertStrictEquals(Object.getPrototypeOf(C).MEMBERS, B.MEMBERS);
assertStrictEquals(Object.getPrototypeOf(B).MEMBERS, A.MEMBERS);
assertStrictEquals(Object.getPrototypeOf(A).MEMBERS, Struct.MEMBERS);
});
Deno.test('Struct: protected properties', () => {
class Test extends Struct {
declare protected alpha: number;
declare public unrelated: number;
public getAlpha(): number {
return this.alpha;
}
public static override readonly BYTE_LENGTH: number = ((o) => {
o += int8(this, 'alpha' as never, o);
return o;
})(super.BYTE_LENGTH);
}
const test = new Test(new Uint8Array([42]).buffer);
assertEquals(test.getAlpha(), 42);
});
Deno.test('Struct: private properties', () => {
class Test extends Struct {
declare private alpha: number;
declare public unrelated: number;
public getAlpha(): number {
return this.alpha;
}
public static override readonly BYTE_LENGTH: number = ((o) => {
o += int8(this, 'alpha' as never, o);
return o;
})(super.BYTE_LENGTH);
}
const test = new Test(new Uint8Array([42]).buffer);
assertEquals(test.getAlpha(), 42);
});
Deno.test('Struct: extends', () => {
class Var extends Struct {
declare public type: number;
public static override readonly BYTE_LENGTH: number = ((o) => {
o += int8(this, 'type', o);
return o;
})(super.BYTE_LENGTH);
}
class Int8 extends Var {
declare public value: number;
public static override readonly BYTE_LENGTH: number = ((o) => {
o += int8(this, 'value', o);
return o;
})(super.BYTE_LENGTH);
}
const data = new Uint8Array(Int8.BYTE_LENGTH);
const varFloat = new Int8(data.buffer, 0, true);
varFloat.type = 8;
varFloat.value = 123;
assertEquals(data, new Uint8Array([8, 123]));
});
Deno.test('Struct: abstract', () => {
abstract class Member extends Struct {
declare public value: number;
public abstract method(): number;
public static override readonly BYTE_LENGTH: number = ((o) => {
o += int8(this, 'value', o);
return o;
})(super.BYTE_LENGTH);
}
class MemberImp extends Member {
declare public value: number;
public method(): number {
return 42;
}
}
class Test extends Struct {
declare public child: MemberImp;
public static override readonly BYTE_LENGTH: number = ((o) => {
o += member(MemberImp, this, 'child', o);
return o;
})(super.BYTE_LENGTH);
}
const data = new Uint8Array(Test.BYTE_LENGTH);
const test = new Test(data.buffer, 0, true);
test.child.value = 123;
assertEquals(data, new Uint8Array([123]));
});
Deno.test('Struct: abstract placeholder', () => {
abstract class Member extends Struct {
declare public value: number;
public abstract method(): number;
public static override readonly BYTE_LENGTH: number = ((o) => {
o += int8(this, 'value', o);
return o;
})(super.BYTE_LENGTH);
}
class MemberImp extends Member {
declare public value: number;
public method(): number {
return 42;
}
}
abstract class Test extends Struct {
declare public child: Member;
public static override readonly BYTE_LENGTH: number = ((o) => {
o += pad(Member.BYTE_LENGTH, this, 'child' as never, o);
return o;
})(super.BYTE_LENGTH);
}
class TestImp extends Test {
declare public child: Member;
public static override readonly BYTE_LENGTH: number = ((o) => {
o += member(MemberImp, this, 'child', getByteOffset(this, 'child'));
return o;
})(super.BYTE_LENGTH);
}
const data = new Uint8Array(Test.BYTE_LENGTH);
const test = new TestImp(data.buffer, 0, true);
test.child.value = 123;
assertEquals(data, new Uint8Array([123]));
});