-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
105 lines (96 loc) · 3.62 KB
/
test.html
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
<!DOCTYPE html>
<body>
<script type="module">
import Packet from "./Packet.js"
let p = new Packet();
p.add(3, 2);
p.add(7, 3);
p.add(2, 2);
p.add(3.14);
p.add("123");
p.add(123, 8);
p.add(12345, 16);
p.add(1233456, 32);
p.add([1, 2, 3], 8);
p.add([1, 2, 3], 16);
p.add(new Uint8Array([1, 2, 3]));
let out = new Packet(p.buffer);
console.log(out.parse("b2,b3,b2,f,'3',b8,b16,b32,[3],[3]:16,r3"));
//console.log(out.parse("b2,b3"));
//console.log(out.parse("b2,f,'3'"));
//console.log(out.parse("b8,b16,b32,[3],[3]:16,r3"));
function test() {
let markup = "";
let res = [];
let p = new Packet();
function random(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
for (let i = 0; i < 10; i++) {
switch (random(0, 6)) {
case 0: {
let bits = random(1, 9);
let val = random(0, 1 << bits);
p.add(val, bits);
res.push(val);
markup += 'b' + bits + ',';
} break;
case 1: {
let f = new Float32Array([random(1, 100) / random(1, 100) + 0.001])[0];
p.add(f);
res.push(f);
markup += 'f,';
} break;
case 2: {
let bytes = 2 ** random(0, 3);
let val = random(1 << ((bytes - 1) * 8), 1 << (bytes * 8));
p.add(val, bytes * 8);
res.push(val);
markup += 'b' + bytes * 8 + ',';
} break;
case 3: {
let bytes = 2 ** random(0, 3);
let len = random(1, 5);
let arr = [];
for (let i = 0; i < len; i++) {
arr.push(random(1 << ((bytes - 1) * 8), 1 << (bytes * 8)));
}
p.add(arr, bytes * 8);
res.push(arr);
markup += '[' + len + ']:' + bytes * 8 + ',';
} break;
case 4: {
let len = random(1, 5);
let arr = [];
for (let i = 0; i < len; i++) {
arr.push(random(0, 256));
}
p.add(arr, 8);
res.push(arr);
markup += '[' + len + '],';
} break;
case 5: {
let len = random(3, 10);
let str = '';
for (let i = 0; i < len; i++) str += 'a';
p.add(str);
res.push(str);
markup += '"' + len + '",';
} break;
}
}
markup = markup.slice(0, -1);
let pout = new Packet(p.buffer);
let out = pout.parse(markup);
let ok = JSON.stringify(res) == JSON.stringify(out);
if (!ok) {
console.log("ERROR!");
console.log(markup);
console.log(JSON.stringify(res), JSON.stringify(out));
}
}
for (let i = 0; i < 30; i++) test();
console.log("done!");
</script>
</body>
</html>