-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
205 lines (140 loc) · 4.02 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
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
'use strict';
/*global describe, it*/
var assert = require('assert');
var _ = require('lodash');
var MicroBuffer = require('./');
var mb;
function cmpBuf(a, b) {
if (a.length !== b.length) {
throw new assert.AssertionError({
actual: a,
expected: b,
operator: 'compare'
});
}
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
throw new assert.AssertionError({
actual: a,
expected: b,
operator: 'compare'
});
}
}
}
describe('MicroBuffer', function () {
it('create by size', function () {
mb = new MicroBuffer(5);
assert.equal(mb.length, 5);
assert.ok(_.isTypedArray(mb.buffer));
});
it('wrap array', function () {
mb = new MicroBuffer([ 1, 2, 3, 4 ]);
cmpBuf(mb.toArray(), [ 1, 2, 3, 4 ]);
mb = new MicroBuffer([ 1, 2, 3, 4 ], 1, 2);
cmpBuf(mb.toArray(), [ 2, 3 ]);
});
it('wrap typed array', function () {
mb = new MicroBuffer(new Uint8Array([ 1, 2, 3, 4 ]));
cmpBuf(mb.toArray(), [ 1, 2, 3, 4 ]);
mb = new MicroBuffer(new Uint8Array([ 1, 2, 3, 4 ]), 1, 2);
cmpBuf(mb.toArray(), [ 2, 3 ]);
});
it('wrap MicroBuffer', function () {
mb = new MicroBuffer(new MicroBuffer([ 1, 2, 3, 4 ]));
cmpBuf(mb.toArray(), [ 1, 2, 3, 4 ]);
mb = new MicroBuffer(new MicroBuffer([ 1, 2, 3, 4 ]), 1, 2);
cmpBuf(mb.toArray(), [ 2, 3 ]);
});
it('get/set numbers', function () {
mb = new MicroBuffer(4);
mb.setUint8(0, 0xAA);
mb.setUint8(1, 0x55);
mb.setUint16(2, 0x88EE);
assert.equal(mb.getUint8(0), 0xAA);
assert.equal(mb.getUint8(1), 0x55);
assert.equal(mb.getUint8(2), 0x88);
assert.equal(mb.getUint8(3), 0xEE);
assert.equal(mb.getUint16(0), 0xAA55);
assert.equal(mb.getUint16(2), 0x88EE);
assert.equal(mb.getUint32(0), 0xAA5588EE);
mb = new MicroBuffer(4);
mb.setUint32(0, 0xAA5588EE);
assert.equal(mb.getUint32(0), 0xAA5588EE);
});
it('get/set numbers LE', function () {
mb = new MicroBuffer(4);
mb.setUint16(0, 0x88EE, true);
assert.equal(mb.getUint16(0), 0xEE88);
mb = new MicroBuffer(4);
mb.setUint32(0, 0xAA5588EE, true);
assert.equal(mb.getUint32(0), 0xEE8855AA);
});
it('write numbers', function () {
mb = new MicroBuffer(14);
mb.writeUint8(1);
assert.equal(mb.tell(), 1);
mb.writeInt8(-1);
assert.equal(mb.tell(), 2);
mb.writeUint16(0xAA55);
assert.equal(mb.tell(), 4);
mb.writeInt16(-2);
assert.equal(mb.tell(), 6);
mb.writeUint32(0xEE33AA55);
assert.equal(mb.tell(), 10);
mb.writeInt32(-3);
assert.equal(mb.tell(), 14);
cmpBuf(mb.toArray(), [
1,
0xFF,
0xAA, 0x55,
0xFF, 0xFE,
0xEE, 0x33, 0xAA, 0x55,
0xFF, 0xFF, 0xFF, 0xFD
]);
});
it('write numbers LE', function () {
mb = new MicroBuffer(4);
mb.writeUint16(0xAA55, true);
mb.writeInt16(-2, true);
cmpBuf(mb.toArray(), [
0x55, 0xAA,
0xFE, 0xFF
]);
mb = new MicroBuffer(8);
mb.writeUint32(0xEE33AA55, true);
mb.writeInt32(-3, true);
cmpBuf(mb.toArray(), [
0x55, 0xAA, 0x33, 0xEE,
0xFD, 0xFF, 0xFF, 0xFF
]);
});
it('write Uint64', function () {
mb = new MicroBuffer(8);
mb.writeUint64(0x112233445566);
cmpBuf(mb.toArray(), [
0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66
]);
});
it('seek/fill', function () {
mb = new MicroBuffer(4);
mb.fill(0x99);
mb.seek(2);
mb.writeUint16(0xAA55);
assert.equal(mb.getUint32(0), 0x9999AA55);
});
it('writeBytes', function () {
mb = new MicroBuffer(4);
mb.writeBytes([ 0x00, 0xFF ]);
mb.writeBytes(new Uint8Array([ 0xAA, 0x55 ]));
assert.equal(mb.getUint32(0), 0x00FFAA55);
});
it('toString', function () {
mb = new MicroBuffer([ 0xAA, 0x55, 0x00, 0xFF ]);
var str = mb.toString();
assert.equal(str.charCodeAt(0), 0xAA);
assert.equal(str.charCodeAt(1), 0x55);
assert.equal(str.charCodeAt(2), 0x00);
assert.equal(str.charCodeAt(3), 0xFF);
});
});