1 | var assert = require('assert');
|
---|
2 | var OffsetBuffer = require('../');
|
---|
3 |
|
---|
4 | describe('OffsetBuffer', function() {
|
---|
5 | var o;
|
---|
6 | beforeEach(function() {
|
---|
7 | o = new OffsetBuffer();
|
---|
8 | });
|
---|
9 |
|
---|
10 | describe('.take()', function() {
|
---|
11 | it('should return empty buffer', function() {
|
---|
12 | var b = new Buffer('hello world');
|
---|
13 | o.push(b);
|
---|
14 | var r = o.take(0);
|
---|
15 | assert.equal(r.length, 0);
|
---|
16 | assert.equal(o.size, b.length);
|
---|
17 | });
|
---|
18 |
|
---|
19 | it('should return the first buffer itself', function() {
|
---|
20 | var b = new Buffer('hello world');
|
---|
21 | o.push(b);
|
---|
22 | var r = o.take(b.length);
|
---|
23 | assert(r === b);
|
---|
24 | assert(o.isEmpty());
|
---|
25 | });
|
---|
26 |
|
---|
27 | it('should return the slice of the buffer ', function() {
|
---|
28 | var b = new Buffer('hello world');
|
---|
29 | o.push(b);
|
---|
30 | assert.equal(o.take(5).toString(), 'hello');
|
---|
31 | assert.equal(o.take(1).toString(), ' ');
|
---|
32 | assert.equal(o.take(5).toString(), 'world');
|
---|
33 | assert(o.isEmpty());
|
---|
34 | });
|
---|
35 |
|
---|
36 | it('should concat buffers', function() {
|
---|
37 | o.push(new Buffer('hello'));
|
---|
38 | o.push(new Buffer(' '));
|
---|
39 | o.push(new Buffer('world!'));
|
---|
40 | assert.equal(o.take(11).toString(), 'hello world');
|
---|
41 | assert.equal(o.take(1).toString(), '!');
|
---|
42 | assert(o.isEmpty());
|
---|
43 | });
|
---|
44 | });
|
---|
45 |
|
---|
46 | describe('.skip', function() {
|
---|
47 | it('should skip bytes', function() {
|
---|
48 | o.push(new Buffer('hello '));
|
---|
49 | o.push(new Buffer('world'));
|
---|
50 | o.push(new Buffer(' oh gosh'));
|
---|
51 |
|
---|
52 | assert.equal(o.take(2).toString(), 'he');
|
---|
53 | o.skip(1);
|
---|
54 | assert.equal(o.take(2).toString(), 'lo');
|
---|
55 | o.skip(1);
|
---|
56 | assert.equal(o.take(2).toString(), 'wo');
|
---|
57 | o.skip(4);
|
---|
58 | assert.equal(o.take(7).toString(), 'oh gosh');
|
---|
59 |
|
---|
60 | assert(o.isEmpty());
|
---|
61 | });
|
---|
62 | });
|
---|
63 |
|
---|
64 | describe('.peekUInt8', function() {
|
---|
65 | it('should return and not move by one byte', function() {
|
---|
66 | o.push(new Buffer([ 0x1, 0x2 ]));
|
---|
67 | assert.equal(o.peekUInt8(), 1);
|
---|
68 | assert.equal(o.readUInt8(), 1);
|
---|
69 | assert.equal(o.peekUInt8(), 2);
|
---|
70 | assert.equal(o.readUInt8(), 2);
|
---|
71 | assert(o.isEmpty());
|
---|
72 | });
|
---|
73 | });
|
---|
74 |
|
---|
75 | describe('.peekInt8', function() {
|
---|
76 | it('should return signed number', function() {
|
---|
77 | o.push(new Buffer([ 0x80 ]));
|
---|
78 | assert.equal(o.peekInt8(), -128);
|
---|
79 | assert.equal(o.readInt8(), -128);
|
---|
80 | assert(o.isEmpty());
|
---|
81 | });
|
---|
82 | });
|
---|
83 |
|
---|
84 | describe('.readUInt8', function() {
|
---|
85 | it('should return and move by one byte', function() {
|
---|
86 | o.push(new Buffer([ 0x1, 0x2 ]));
|
---|
87 | o.push(new Buffer([ 0x3, 0x4 ]));
|
---|
88 | assert.equal(o.readUInt8(), 1);
|
---|
89 | assert.equal(o.readUInt8(), 2);
|
---|
90 | assert.equal(o.readUInt8(), 3);
|
---|
91 | assert.equal(o.readUInt8(), 4);
|
---|
92 | assert(o.isEmpty());
|
---|
93 | });
|
---|
94 | });
|
---|
95 |
|
---|
96 | describe('.readInt8', function() {
|
---|
97 | it('should return signed number', function() {
|
---|
98 | o.push(new Buffer([ 0x8f, 0x7f ]));
|
---|
99 | assert.equal(o.readInt8(), -113);
|
---|
100 | assert.equal(o.readInt8(), 127);
|
---|
101 | assert(o.isEmpty());
|
---|
102 | });
|
---|
103 | });
|
---|
104 |
|
---|
105 | describe('.readUInt16LE', function() {
|
---|
106 | it('should return and move by two bytes', function() {
|
---|
107 | o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
|
---|
108 | o.push(new Buffer([ 0x4, 0x5, 0x6 ]));
|
---|
109 | assert.equal(o.readUInt16LE(), 0x0201);
|
---|
110 | assert.equal(o.readUInt16LE(), 0x0403);
|
---|
111 | assert.equal(o.readUInt16LE(), 0x0605);
|
---|
112 | assert(o.isEmpty());
|
---|
113 | });
|
---|
114 |
|
---|
115 | it('should return and move by two bytes (regression #1)', function() {
|
---|
116 | o.push(new Buffer([ 0x1 ]));
|
---|
117 | o.push(new Buffer([ 0x2, 0x3, 0x4 ]));
|
---|
118 | assert.equal(o.readUInt16LE(), 0x0201);
|
---|
119 | assert.equal(o.readUInt16LE(), 0x0403);
|
---|
120 | assert(o.isEmpty());
|
---|
121 | });
|
---|
122 | });
|
---|
123 |
|
---|
124 | describe('.readInt16LE', function() {
|
---|
125 | it('should return signed number', function() {
|
---|
126 | o.push(new Buffer([ 0x23, 0x81 ]));
|
---|
127 | assert.equal(o.readInt16LE(), -32477);
|
---|
128 | assert(o.isEmpty());
|
---|
129 | });
|
---|
130 | });
|
---|
131 |
|
---|
132 | describe('.readUInt24LE', function() {
|
---|
133 | it('should return and move by three bytes', function() {
|
---|
134 | o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5 ]));
|
---|
135 | o.push(new Buffer([ 0x6, 0x7 ]));
|
---|
136 | o.push(new Buffer([ 0x8, 0x9 ]));
|
---|
137 | assert.equal(o.readUInt24LE(), 0x030201);
|
---|
138 | assert.equal(o.readUInt24LE(), 0x060504);
|
---|
139 | assert.equal(o.readUInt24LE(), 0x090807);
|
---|
140 | assert(o.isEmpty());
|
---|
141 | });
|
---|
142 |
|
---|
143 | it('should return and move by three bytes (regression #1)', function() {
|
---|
144 | o.push(new Buffer([ 0x1, 0x2 ]));
|
---|
145 | o.push(new Buffer([ 0x3 ]));
|
---|
146 | assert.equal(o.readUInt24LE(), 0x030201);
|
---|
147 | assert.equal(o.buffers.length, 0);
|
---|
148 | assert(o.isEmpty());
|
---|
149 | });
|
---|
150 | });
|
---|
151 |
|
---|
152 | describe('.readInt24LE', function() {
|
---|
153 | it('should return signed number', function() {
|
---|
154 | o.push(new Buffer([ 0x23, 0x45, 0x81 ]));
|
---|
155 | assert.equal(o.readInt24LE(), -8305373);
|
---|
156 | assert(o.isEmpty());
|
---|
157 | });
|
---|
158 | });
|
---|
159 |
|
---|
160 | describe('.readUInt32LE', function() {
|
---|
161 | it('should return and move by four bytes', function() {
|
---|
162 | o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 ]));
|
---|
163 | o.push(new Buffer([ 0x8, 0x9, 0xa ]));
|
---|
164 | o.push(new Buffer([ 0xb, 0xc, 0xd ]));
|
---|
165 | o.push(new Buffer([ 0xe, 0xf, 0x10 ]));
|
---|
166 | assert.equal(o.readUInt32LE(), 0x04030201);
|
---|
167 | assert.equal(o.readUInt32LE(), 0x08070605);
|
---|
168 | assert.equal(o.readUInt32LE(), 0x0c0b0a09);
|
---|
169 | assert.equal(o.readUInt32LE(), 0x100f0e0d);
|
---|
170 | assert(o.isEmpty());
|
---|
171 | });
|
---|
172 |
|
---|
173 | it('should return and move by four bytes (regression #1)', function() {
|
---|
174 | o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
|
---|
175 | o.push(new Buffer([ 0x4 ]));
|
---|
176 | assert.equal(o.readUInt32LE(), 0x04030201);
|
---|
177 | assert.equal(o.buffers.length, 0);
|
---|
178 | assert(o.isEmpty());
|
---|
179 | });
|
---|
180 | });
|
---|
181 |
|
---|
182 | describe('.readInt32LE', function() {
|
---|
183 | it('should return signed number', function() {
|
---|
184 | o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
|
---|
185 | assert.equal(o.readInt32LE(), -1);
|
---|
186 | assert(o.isEmpty());
|
---|
187 | });
|
---|
188 | });
|
---|
189 |
|
---|
190 | describe('.readUInt16BE', function() {
|
---|
191 | it('should return and move by two bytes', function() {
|
---|
192 | o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
|
---|
193 | o.push(new Buffer([ 0x4, 0x5, 0x6 ]));
|
---|
194 | assert.equal(o.readUInt16BE(), 0x0102);
|
---|
195 | assert.equal(o.readUInt16BE(), 0x0304);
|
---|
196 | assert.equal(o.readUInt16BE(), 0x0506);
|
---|
197 | assert(o.isEmpty());
|
---|
198 | });
|
---|
199 | });
|
---|
200 |
|
---|
201 | describe('.readInt16BE', function() {
|
---|
202 | it('should return signed number', function() {
|
---|
203 | o.push(new Buffer([ 0x81, 0x23 ]));
|
---|
204 | assert.equal(o.readInt16BE(), -32477);
|
---|
205 | assert(o.isEmpty());
|
---|
206 | });
|
---|
207 | });
|
---|
208 |
|
---|
209 | describe('.readUInt24BE', function() {
|
---|
210 | it('should return and move by three bytes', function() {
|
---|
211 | o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5 ]));
|
---|
212 | o.push(new Buffer([ 0x6, 0x7 ]));
|
---|
213 | o.push(new Buffer([ 0x8, 0x9 ]));
|
---|
214 | assert.equal(o.readUInt24BE(), 0x010203);
|
---|
215 | assert.equal(o.readUInt24BE(), 0x040506);
|
---|
216 | assert.equal(o.readUInt24BE(), 0x070809);
|
---|
217 | assert(o.isEmpty());
|
---|
218 | });
|
---|
219 | });
|
---|
220 |
|
---|
221 | describe('.readInt24BE', function() {
|
---|
222 | it('should return signed number', function() {
|
---|
223 | o.push(new Buffer([ 0x81, 0x45, 0x23 ]));
|
---|
224 | assert.equal(o.readInt24BE(), -8305373);
|
---|
225 | assert(o.isEmpty());
|
---|
226 | });
|
---|
227 | });
|
---|
228 |
|
---|
229 | describe('.readUInt32BE', function() {
|
---|
230 | it('should return and move by four bytes', function() {
|
---|
231 | o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 ]));
|
---|
232 | o.push(new Buffer([ 0x8, 0x9, 0xa ]));
|
---|
233 | o.push(new Buffer([ 0xb, 0xc, 0xd ]));
|
---|
234 | o.push(new Buffer([ 0xe, 0xf, 0x10 ]));
|
---|
235 | assert.equal(o.readUInt32BE(), 0x01020304);
|
---|
236 | assert.equal(o.readUInt32BE(), 0x05060708);
|
---|
237 | assert.equal(o.readUInt32BE(), 0x090a0b0c);
|
---|
238 | assert.equal(o.readUInt32BE(), 0x0d0e0f10);
|
---|
239 | assert(o.isEmpty());
|
---|
240 | });
|
---|
241 |
|
---|
242 | it('should return positive values', function() {
|
---|
243 | o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
|
---|
244 | assert.equal(o.readUInt32BE(), 0xffffffff);
|
---|
245 | assert(o.isEmpty());
|
---|
246 | });
|
---|
247 | });
|
---|
248 |
|
---|
249 | describe('.readInt32BE', function() {
|
---|
250 | it('should return signed number', function() {
|
---|
251 | o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
|
---|
252 | assert.equal(o.readInt32BE(), -1);
|
---|
253 | assert(o.isEmpty());
|
---|
254 | });
|
---|
255 | });
|
---|
256 |
|
---|
257 | describe('.has', function() {
|
---|
258 | it('should properly check the amount of the remaining bytes', function() {
|
---|
259 | o.push(new Buffer([ 1, 2, 3 ]));
|
---|
260 | assert(o.has(3));
|
---|
261 | assert.equal(o.readUInt8(), 0x01);
|
---|
262 | assert(!o.has(3));
|
---|
263 | assert(o.has(2));
|
---|
264 | assert.equal(o.readUInt16BE(), 0x0203);
|
---|
265 | assert(!o.has(1));
|
---|
266 | });
|
---|
267 | });
|
---|
268 | });
|
---|