/
test.js
139 lines (101 loc) · 2.77 KB
/
test.js
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
(function() {
"use strict";
try {
/**
* create struct
*
* arg1 = data object
* arg2 = default value(optional, default 0)
* arg3 = endian(optional, default true)
*/
var struct = new Struct({
foo: 'uint8',
bar: ['int8', 127],
baz: {
qux: ['uint16', 65535]
}
}, 0, true);
if (!struct instanceof Struct) {
//throw new Error("returned wrong data type");
}
if (struct.byteLength !== 4 || !struct instanceof Object) {
throw new Error("wrong property of instance of struct");
}
/**
* write arraybuffer from javascript object
*
* arg1 = object(optional)
*/
var ab = struct.write();
// => ArrayBuffer {byteLength: 4, slice: function}
if (!ab instanceof ArrayBuffer) {
throw new Error("returned wrong data type");
}
if (ab.byteLength !== 4) {
throw new Error("wrong byteLength of arrayBuffer");
}
/**
* read data object from arraybuffer
*
* arg1 = arraybuffer
* arg2 = offset(optinal, default 0)
*/
var obj = struct.read(ab);
// => Obejct {foo: 0, bar: 127, baz: {qux: 65535}}
if (!obj instanceof Object) {
throw new Error("returned wrong data type");
}
if (obj.foo !== 0 || obj.bar !== 127 || obj.baz.qux !== 65535) {
console.log(obj);
throw new Error("wrong data object value");
}
/**
* update data of arrayBuffer with write and read
*/
ab = struct.write({foo: 255, baz: {qux: 0}});
// => ArrayBuffer {byteLength: 4, slice: function}
if (!ab instanceof ArrayBuffer) {
throw new Error("returned wrong data type");
}
if (ab.byteLength !== 4) {
throw new Error("wrong byteLength of arrayBuffer");
}
obj = struct.read(ab);
// => Obejct {foo: 255, bar: 127, baz: {qux: 0}}
if (!obj instanceof Object) {
throw new Error("returned wrong data type");
}
if (obj.foo !== 255 || obj.bar !== 127 || obj.baz.qux !== 0) {
console.log(obj);
throw new Error("wrong data object value");
}
/**
* create struct with multi-byte value
*/
struct = new Struct({
foo: ['uint16', [0xffff, 4095]],
bar: ['uint8', 'firejune', 8]
});
ab = struct.write();
// => ArrayBuffer {byteLength: 12, slice: function}
if (!ab instanceof ArrayBuffer) {
throw new Error("returned wrong data type");
}
if (ab.byteLength !== 12) {
throw new Error("wrong byteLength of arrayBuffer");
}
obj = struct.read(ab);
// => Object {bar: [65535, 4095], qux: "firejune"}
if (!obj instanceof Object) {
throw new Error("returned wrong data type");
}
if (obj.foo[0] !== 65535 || obj.foo[1] !== 4095 || obj.bar !== 'firejune') {
console.log(obj);
throw new Error("wrong data object value");
}
console.log("All tests OK!");
}
catch (e) {
console.error("Test failed: " + e.message);
}
})();