1 |
3a515b92
|
cagy
|
var assert = require('assert');
|
2 |
|
|
var WriteBuffer = require('../');
|
3 |
|
|
|
4 |
|
|
describe('WriteBuffer', function() {
|
5 |
|
|
var w;
|
6 |
|
|
beforeEach(function() {
|
7 |
|
|
w = new WriteBuffer();
|
8 |
|
|
});
|
9 |
|
|
|
10 |
|
|
function join(arr) {
|
11 |
|
|
return arr.map(function(buf) {
|
12 |
|
|
return buf.toString('hex');
|
13 |
|
|
}).join('');
|
14 |
|
|
}
|
15 |
|
|
|
16 |
|
|
describe('.writeUInt8', function() {
|
17 |
|
|
it('should write bytes', function() {
|
18 |
|
|
w.writeUInt8(1);
|
19 |
|
|
w.writeUInt8(2);
|
20 |
|
|
w.writeUInt8(3);
|
21 |
|
|
w.writeUInt8(4);
|
22 |
|
|
assert.equal(join(w.render()), '01020304');
|
23 |
|
|
});
|
24 |
|
|
|
25 |
|
|
it('should correctly handle overflow', function() {
|
26 |
|
|
w.reserve(3);
|
27 |
|
|
w.writeUInt8(1);
|
28 |
|
|
w.writeUInt8(2);
|
29 |
|
|
w.writeUInt8(3);
|
30 |
|
|
w.writeUInt8(4);
|
31 |
|
|
assert.equal(join(w.render()), '01020304');
|
32 |
|
|
});
|
33 |
|
|
});
|
34 |
|
|
|
35 |
|
|
describe('.writeInt8', function() {
|
36 |
|
|
it('should write bytes', function() {
|
37 |
|
|
w.writeInt8(-1);
|
38 |
|
|
w.writeInt8(2);
|
39 |
|
|
assert.equal(join(w.render()), 'ff02');
|
40 |
|
|
});
|
41 |
|
|
});
|
42 |
|
|
|
43 |
|
|
describe('.writeUInt16BE', function() {
|
44 |
|
|
it('should write bytes', function() {
|
45 |
|
|
w.writeUInt16BE(0x0102);
|
46 |
|
|
w.writeUInt16BE(0x0304);
|
47 |
|
|
assert.equal(join(w.render()), '01020304');
|
48 |
|
|
});
|
49 |
|
|
|
50 |
|
|
it('should correctly handle overflow', function() {
|
51 |
|
|
w.reserve(2);
|
52 |
|
|
w.reserve(3);
|
53 |
|
|
w.writeUInt16BE(0x0102);
|
54 |
|
|
w.writeUInt16BE(0x0304);
|
55 |
|
|
w.writeUInt16BE(0x0506);
|
56 |
|
|
assert.equal(join(w.render()), '010203040506');
|
57 |
|
|
});
|
58 |
|
|
});
|
59 |
|
|
|
60 |
|
|
describe('.writeInt16BE', function() {
|
61 |
|
|
it('should write bytes', function() {
|
62 |
|
|
w.writeInt16BE(-0x0102);
|
63 |
|
|
w.writeInt16BE(0x0304);
|
64 |
|
|
assert.equal(join(w.render()), 'fefe0304');
|
65 |
|
|
});
|
66 |
|
|
});
|
67 |
|
|
|
68 |
|
|
describe('.writeUInt16LE', function() {
|
69 |
|
|
it('should write bytes', function() {
|
70 |
|
|
w.writeUInt16LE(0x0102);
|
71 |
|
|
w.writeUInt16LE(0x0304);
|
72 |
|
|
assert.equal(join(w.render()), '02010403');
|
73 |
|
|
});
|
74 |
|
|
|
75 |
|
|
it('should correctly handle overflow', function() {
|
76 |
|
|
w.reserve(2);
|
77 |
|
|
w.reserve(3);
|
78 |
|
|
w.writeUInt16LE(0x0102);
|
79 |
|
|
w.writeUInt16LE(0x0304);
|
80 |
|
|
w.writeUInt16LE(0x0506);
|
81 |
|
|
assert.equal(join(w.render()), '020104030605');
|
82 |
|
|
});
|
83 |
|
|
});
|
84 |
|
|
|
85 |
|
|
describe('.writeInt16LE', function() {
|
86 |
|
|
it('should write bytes', function() {
|
87 |
|
|
w.writeInt16LE(-0x0201);
|
88 |
|
|
w.writeInt16LE(0x0304);
|
89 |
|
|
assert.equal(join(w.render()), 'fffd0403');
|
90 |
|
|
});
|
91 |
|
|
});
|
92 |
|
|
|
93 |
|
|
describe('.writeUInt24BE', function() {
|
94 |
|
|
it('should write bytes', function() {
|
95 |
|
|
w.writeUInt24BE(0x010203);
|
96 |
|
|
w.writeUInt24BE(0x040506);
|
97 |
|
|
assert.equal(join(w.render()), '010203040506');
|
98 |
|
|
});
|
99 |
|
|
|
100 |
|
|
it('should correctly set avail on boundary', function() {
|
101 |
|
|
w = new WriteBuffer();
|
102 |
|
|
w.reserveRate = 4;
|
103 |
|
|
w.writeUInt16BE(1);
|
104 |
|
|
w.writeUInt24BE(1);
|
105 |
|
|
assert.equal(w.avail, 3);
|
106 |
|
|
});
|
107 |
|
|
});
|
108 |
|
|
|
109 |
|
|
describe('.writeInt24BE', function() {
|
110 |
|
|
it('should write bytes', function() {
|
111 |
|
|
w.writeInt24BE(-0x010203);
|
112 |
|
|
w.writeInt24BE(0x040506);
|
113 |
|
|
assert.equal(join(w.render()), 'fefdfd040506');
|
114 |
|
|
});
|
115 |
|
|
});
|
116 |
|
|
|
117 |
|
|
describe('.writeUInt24LE', function() {
|
118 |
|
|
it('should write bytes', function() {
|
119 |
|
|
w.writeUInt24LE(0x010203);
|
120 |
|
|
w.writeUInt24LE(0x040506);
|
121 |
|
|
assert.equal(join(w.render()), '030201060504');
|
122 |
|
|
});
|
123 |
|
|
});
|
124 |
|
|
|
125 |
|
|
describe('.writeInt24LE', function() {
|
126 |
|
|
it('should write bytes', function() {
|
127 |
|
|
w.writeInt24LE(-0x010203);
|
128 |
|
|
w.writeInt24LE(0x040506);
|
129 |
|
|
assert.equal(join(w.render()), 'fdfdfe060504');
|
130 |
|
|
});
|
131 |
|
|
});
|
132 |
|
|
|
133 |
|
|
describe('.writeUInt32BE', function() {
|
134 |
|
|
it('should write bytes', function() {
|
135 |
|
|
w.writeUInt32BE(0x01020304);
|
136 |
|
|
w.writeUInt32BE(0x05060708);
|
137 |
|
|
assert.equal(join(w.render()), '0102030405060708');
|
138 |
|
|
});
|
139 |
|
|
|
140 |
|
|
it('should write bytes on the boundary', function() {
|
141 |
|
|
w.reserve(4);
|
142 |
|
|
w.writeUInt8(0x00);
|
143 |
|
|
w.writeUInt32BE(0x01020304);
|
144 |
|
|
assert.equal(join(w.render()), '0001020304');
|
145 |
|
|
});
|
146 |
|
|
});
|
147 |
|
|
|
148 |
|
|
describe('.writeInt32BE', function() {
|
149 |
|
|
it('should write bytes', function() {
|
150 |
|
|
w.writeInt32BE(-0x01020304);
|
151 |
|
|
w.writeInt32BE(0x05060708);
|
152 |
|
|
assert.equal(join(w.render()), 'fefdfcfc05060708');
|
153 |
|
|
});
|
154 |
|
|
});
|
155 |
|
|
|
156 |
|
|
describe('.writeUInt32LE', function() {
|
157 |
|
|
it('should write bytes', function() {
|
158 |
|
|
w.writeUInt32LE(0x01020304);
|
159 |
|
|
w.writeUInt32LE(0x05060708);
|
160 |
|
|
assert.equal(join(w.render()), '0403020108070605');
|
161 |
|
|
});
|
162 |
|
|
|
163 |
|
|
it('should write max uint32 value', function() {
|
164 |
|
|
w.writeUInt32LE(0xffffffff);
|
165 |
|
|
assert.equal(join(w.render()), 'ffffffff');
|
166 |
|
|
});
|
167 |
|
|
});
|
168 |
|
|
|
169 |
|
|
describe('.combWrite', function() {
|
170 |
|
|
it('should write bytes', function() {
|
171 |
|
|
w.writeComb(1, 'le', 0x01);
|
172 |
|
|
w.writeComb(1, 'be', 0x02);
|
173 |
|
|
w.writeComb(2, 'le', 0x0102);
|
174 |
|
|
w.writeComb(2, 'be', 0x0304);
|
175 |
|
|
w.writeComb(3, 'le', 0x010203);
|
176 |
|
|
w.writeComb(3, 'be', 0x040506);
|
177 |
|
|
w.writeComb(4, 'le', 0x01020304);
|
178 |
|
|
w.writeComb(4, 'be', 0x05060708);
|
179 |
|
|
assert.equal(join(w.render()),
|
180 |
|
|
'0102020103040302010405060403020105060708');
|
181 |
|
|
});
|
182 |
|
|
|
183 |
|
|
it('should write max uint32 value', function() {
|
184 |
|
|
w.writeUInt32LE(0xffffffff);
|
185 |
|
|
assert.equal(join(w.render()), 'ffffffff');
|
186 |
|
|
});
|
187 |
|
|
});
|
188 |
|
|
|
189 |
|
|
describe('.writeInt32LE', function() {
|
190 |
|
|
it('should write bytes', function() {
|
191 |
|
|
w.writeInt32LE(-0x01020304);
|
192 |
|
|
w.writeInt32LE(0x05060708);
|
193 |
|
|
assert.equal(join(w.render()), 'fcfcfdfe08070605');
|
194 |
|
|
});
|
195 |
|
|
});
|
196 |
|
|
|
197 |
|
|
describe('.skip', function() {
|
198 |
|
|
it('should skip bytes', function() {
|
199 |
|
|
w.skip(4);
|
200 |
|
|
w.writeUInt32BE(0xdeadbeef);
|
201 |
|
|
assert(/^.{8}deadbeef$/.test(join(w.render())));
|
202 |
|
|
});
|
203 |
|
|
|
204 |
|
|
it('should skip 0 bytes', function() {
|
205 |
|
|
var skip = w.skip(0);
|
206 |
|
|
assert.equal(skip.size, 0);
|
207 |
|
|
w.writeUInt32BE(0xdeadbeef);
|
208 |
|
|
assert(/^deadbeef$/.test(join(w.render())));
|
209 |
|
|
});
|
210 |
|
|
|
211 |
|
|
it('should skip bytes on the boundary', function() {
|
212 |
|
|
w.reserve(4);
|
213 |
|
|
w.writeUInt8(0x01);
|
214 |
|
|
var skip = w.skip(4);
|
215 |
|
|
w.writeUInt32BE(0xdeadbeef);
|
216 |
|
|
skip.writeUInt32BE(0xabbabaab);
|
217 |
|
|
assert(/^01abbabaabdeadbeef$/.test(join(w.render())));
|
218 |
|
|
});
|
219 |
|
|
|
220 |
|
|
it('should skip bytes on the boundary in two chunks', function() {
|
221 |
|
|
w.reserve(4);
|
222 |
|
|
var skip1 = w.skip(2);
|
223 |
|
|
var skip2 = w.skip(2);
|
224 |
|
|
w.writeUInt32BE(0xdeadbeef);
|
225 |
|
|
skip1.writeUInt16BE(0xabba);
|
226 |
|
|
skip2.writeUInt16BE(0xbaba);
|
227 |
|
|
assert(/^abbababadeadbeef$/.test(join(w.render())));
|
228 |
|
|
});
|
229 |
|
|
});
|
230 |
|
|
|
231 |
|
|
describe('.slice', function() {
|
232 |
|
|
it('should return empty slice', function() {
|
233 |
|
|
w.writeUInt32BE(0xabbadead);
|
234 |
|
|
assert.equal(join(w.slice(4, 4).render()), '');
|
235 |
|
|
assert.equal(join(w.render()), 'abbadead');
|
236 |
|
|
});
|
237 |
|
|
|
238 |
|
|
it('should return full slice', function() {
|
239 |
|
|
w.writeUInt32BE(0xabbadead);
|
240 |
|
|
var slice = w.slice(0, 4);
|
241 |
|
|
slice.writeUInt32BE(0xdeadbeef);
|
242 |
|
|
assert.equal(join(slice.render()), 'deadbeef');
|
243 |
|
|
assert.equal(join(w.render()), 'deadbeef');
|
244 |
|
|
});
|
245 |
|
|
|
246 |
|
|
it('should return partial slice', function() {
|
247 |
|
|
w.writeUInt32BE(0xabbadead);
|
248 |
|
|
var slice = w.slice(0, 3);
|
249 |
|
|
slice.writeUInt24BE(0xdeadbe);
|
250 |
|
|
assert.equal(join(slice.render()), 'deadbe');
|
251 |
|
|
assert.equal(join(w.render()), 'deadbead');
|
252 |
|
|
});
|
253 |
|
|
|
254 |
|
|
it('should return over-the-boundary slice', function() {
|
255 |
|
|
for (var i = 0; i < 16; i++) {
|
256 |
|
|
w.reserve(3);
|
257 |
|
|
w.writeUInt24BE(i);
|
258 |
|
|
}
|
259 |
|
|
assert.equal(join(w.render()),
|
260 |
|
|
'000000000001000002000003000004000005000006000007' +
|
261 |
|
|
'00000800000900000a00000b00000c00000d00000e00000f');
|
262 |
|
|
|
263 |
|
|
var slice = w.slice(5, 12);
|
264 |
|
|
slice.writeUInt24BE(0xaaabac);
|
265 |
|
|
slice.writeUInt24BE(0xbabbbc);
|
266 |
|
|
slice.writeUInt8(0xcc);
|
267 |
|
|
|
268 |
|
|
assert.equal(join(slice.render()), 'aaabacbabbbccc');
|
269 |
|
|
assert.equal(join(w.render()),
|
270 |
|
|
'0000000000aaabacbabbbccc000004000005000006000007' +
|
271 |
|
|
'00000800000900000a00000b00000c00000d00000e00000f');
|
272 |
|
|
});
|
273 |
|
|
});
|
274 |
|
|
|
275 |
|
|
describe('.copyFrom', function() {
|
276 |
|
|
it('should copy bytes', function() {
|
277 |
|
|
var tmp = new Buffer(128);
|
278 |
|
|
for (var i = 0; i < tmp.length; i++)
|
279 |
|
|
tmp[i] = i;
|
280 |
|
|
w.writeUInt32BE(0xdeadbeef);
|
281 |
|
|
w.copyFrom(tmp);
|
282 |
|
|
w.writeUInt32BE(0xabbadead);
|
283 |
|
|
|
284 |
|
|
assert.equal(
|
285 |
|
|
join(w.render()),
|
286 |
|
|
'deadbeef000102030405060708090a0b0c0d0e0f101112131415161718191a1b' +
|
287 |
|
|
'1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b' +
|
288 |
|
|
'3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b' +
|
289 |
|
|
'5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b' +
|
290 |
|
|
'7c7d7e7fabbadead');
|
291 |
|
|
});
|
292 |
|
|
|
293 |
|
|
it('should copy bytes using offset', function() {
|
294 |
|
|
var tmp = new Buffer(128);
|
295 |
|
|
for (var i = 0; i < tmp.length; i++)
|
296 |
|
|
tmp[i] = i;
|
297 |
|
|
w.writeUInt32BE(0xdeadbeef);
|
298 |
|
|
w.copyFrom(tmp, 10, 12);
|
299 |
|
|
w.writeUInt32BE(0xabbadead);
|
300 |
|
|
|
301 |
|
|
assert.equal(
|
302 |
|
|
join(w.render()),
|
303 |
|
|
'deadbeef0a0babbadead');
|
304 |
|
|
});
|
305 |
|
|
});
|
306 |
|
|
|
307 |
|
|
describe('.write', function() {
|
308 |
|
|
it('should write utf8 string', function() {
|
309 |
|
|
w.writeUInt32BE(0xdeadbeef);
|
310 |
|
|
w.write('ohai\u1023');
|
311 |
|
|
w.writeUInt32BE(0xabbadead);
|
312 |
|
|
|
313 |
|
|
assert.equal(
|
314 |
|
|
join(w.render()),
|
315 |
|
|
'deadbeef' +
|
316 |
|
|
'6f6861691023' +
|
317 |
|
|
'abbadead');
|
318 |
|
|
});
|
319 |
|
|
|
320 |
|
|
it('should copy bytes using offset', function() {
|
321 |
|
|
var tmp = new Buffer(128);
|
322 |
|
|
for (var i = 0; i < tmp.length; i++)
|
323 |
|
|
tmp[i] = i;
|
324 |
|
|
w.writeUInt32BE(0xdeadbeef);
|
325 |
|
|
w.copyFrom(tmp, 10, 12);
|
326 |
|
|
w.writeUInt32BE(0xabbadead);
|
327 |
|
|
|
328 |
|
|
assert.equal(
|
329 |
|
|
join(w.render()),
|
330 |
|
|
'deadbeef0a0babbadead');
|
331 |
|
|
});
|
332 |
|
|
});
|
333 |
|
|
|
334 |
|
|
describe('.skip', function() {
|
335 |
|
|
it('should copy bytes', function() {
|
336 |
|
|
w.reserve(5);
|
337 |
|
|
var h = w.skip(4);
|
338 |
|
|
w.writeUInt32BE(0xabbadead);
|
339 |
|
|
h.writeUInt32BE(0xdeadbeef);
|
340 |
|
|
|
341 |
|
|
assert.equal(
|
342 |
|
|
join(w.render()),
|
343 |
|
|
'deadbeefabbadead');
|
344 |
|
|
});
|
345 |
|
|
});
|
346 |
|
|
|
347 |
|
|
describe('.forceReserve = true', function() {
|
348 |
|
|
it('should allocate more bytes', function() {
|
349 |
|
|
w.forceReserve = true;
|
350 |
|
|
w.reserve(4);
|
351 |
|
|
w.writeUInt32BE(0xabbadead);
|
352 |
|
|
w.writeUInt32BE(0xabbadead);
|
353 |
|
|
|
354 |
|
|
assert.equal(w.render().length, 1);
|
355 |
|
|
});
|
356 |
|
|
});
|
357 |
|
|
});
|