1 | 'use strict';
|
2 |
|
3 | var Pbf = require('../'),
|
4 | fs = require('fs'),
|
5 | path = require('path'),
|
6 | test = require('tap').test;
|
7 |
|
8 |
|
9 |
|
10 | function toArray(buf) {
|
11 | var arr = [];
|
12 | for (var i = 0; i < buf.length; i++) {
|
13 | arr.push(buf[i]);
|
14 | }
|
15 | return arr;
|
16 | }
|
17 |
|
18 | test('initialization', function(t) {
|
19 | var buf = new Pbf(new Buffer([]));
|
20 | buf.destroy();
|
21 | t.end();
|
22 | });
|
23 |
|
24 | test('realloc', function(t) {
|
25 | var buf = new Pbf(new Buffer([]));
|
26 | buf.realloc(5);
|
27 | t.ok(buf.length >= 5);
|
28 | buf.realloc(25);
|
29 | t.ok(buf.length >= 30);
|
30 | t.end();
|
31 | });
|
32 |
|
33 | var testNumbers = [1,0,0,4,14,23,40,86,127,141,113,925,258,1105,1291,6872,12545,16256,65521,126522,133028,444205,
|
34 | 846327,1883372, 2080768, 266338304, 34091302912,
|
35 | 3716678,674158,15203102,27135056,42501689,110263473,6449928,65474499,943840723,1552431153,407193337,2193544970,
|
36 | 8167778088,5502125480,14014009728,56371207648,9459068416,410595966336,673736830976,502662539776,2654996269056,
|
37 | 5508583663616,6862782705664,34717688324096,1074895093760,95806297440256,130518477701120,197679237955584,
|
38 | 301300890730496,1310140661760000,2883205519638528,2690669862715392,3319292539961344];
|
39 |
|
40 | test('readVarint & writeVarint', function(t) {
|
41 | var buf = new Pbf(new Buffer(0));
|
42 |
|
43 | for (var i = 0; i < testNumbers.length; i++) {
|
44 | buf.writeVarint(testNumbers[i]);
|
45 | }
|
46 | var len = buf.finish().length;
|
47 | t.equal(len, 244);
|
48 | buf.finish();
|
49 |
|
50 | i = 0;
|
51 | while (buf.pos < len) {
|
52 | t.equal(buf.readVarint(), testNumbers[i++]);
|
53 | }
|
54 |
|
55 | t.end();
|
56 | });
|
57 |
|
58 | test('readVarint64', function(t) {
|
59 | var bytes = [0xc8,0xe8,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01];
|
60 | var buf = new Pbf(new Buffer(bytes));
|
61 | t.equal(buf.readVarint64(), -3000);
|
62 |
|
63 | bytes = [0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01];
|
64 | buf = new Pbf(new Buffer(bytes));
|
65 | t.equal(buf.readVarint64(), -1);
|
66 |
|
67 | bytes = [0xc8,0x01];
|
68 | buf = new Pbf(new Buffer(bytes));
|
69 | t.equal(buf.readVarint64(), 200);
|
70 |
|
71 | t.end();
|
72 | });
|
73 |
|
74 | test('readVarint & writeVarint handle really big numbers', function(t) {
|
75 | var buf = new Pbf();
|
76 | var bigNum1 = Math.pow(2, 60);
|
77 | var bigNum2 = Math.pow(2, 69);
|
78 | buf.writeVarint(bigNum1);
|
79 | buf.writeVarint(bigNum2);
|
80 | buf.finish();
|
81 | t.equal(buf.readVarint(), bigNum1);
|
82 | t.equal(buf.readVarint(), bigNum2);
|
83 | t.end();
|
84 | });
|
85 |
|
86 | var testSigned = [0,1,2,0,2,-1,11,18,-17,145,369,891,-1859,-798,2780,-13107,12589,-16433,21140,148023,221062,-985141,
|
87 | 494812,-2121059,-2078871,82483,19219191,29094607,35779553,-215357075,-334572816,-991453240,-1677041436,-3781260558,
|
88 | -6633052788,1049995056,-22854591776,37921771616,-136983944384,187687841024,107420097536,1069000079360,1234936065024,
|
89 | -2861223108608,-492686688256,-6740322942976,-7061359607808,24638679941120,19583051038720,83969719009280,
|
90 | 52578722775040,416482297118720,1981092523409408,-389256637841408];
|
91 |
|
92 | test('readSVarint & writeSVarint', function(t) {
|
93 | var buf = new Pbf(new Buffer(0));
|
94 |
|
95 | for (var i = 0; i < testSigned.length; i++) {
|
96 | buf.writeSVarint(testSigned[i]);
|
97 | }
|
98 | var len = buf.finish().length;
|
99 | t.equal(len, 224);
|
100 | buf.finish();
|
101 |
|
102 | i = 0;
|
103 | while (buf.pos < len) {
|
104 | t.equal(buf.readSVarint(), testSigned[i++]);
|
105 | }
|
106 |
|
107 | t.end();
|
108 | });
|
109 |
|
110 | test('writeVarint throws error on a number that is too big', function(t) {
|
111 | var buf = new Pbf(new Buffer(0));
|
112 | t.throws(function() {
|
113 | buf.writeVarint(29234322996241367000012);
|
114 | });
|
115 | t.end();
|
116 | });
|
117 |
|
118 | test('readVarint throws error on a number that is longer than 10 bytes', function(t) {
|
119 | var buf = new Pbf(new Buffer([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]));
|
120 | t.throws(function() {
|
121 | buf.readVarint();
|
122 | });
|
123 | t.end();
|
124 | });
|
125 |
|
126 | test('readBoolean & writeBoolean', function(t) {
|
127 | var buf = new Pbf();
|
128 | buf.writeBoolean(true);
|
129 | buf.writeBoolean(false);
|
130 | buf.finish();
|
131 | t.equal(buf.readBoolean(), true);
|
132 | t.equal(buf.readBoolean(), false);
|
133 | t.end();
|
134 | });
|
135 |
|
136 | test('readBytes', function(t) {
|
137 | var buf = new Pbf([8, 1, 2, 3, 4, 5, 6, 7, 8]);
|
138 | t.same(toArray(buf.readBytes()), [1, 2, 3, 4, 5, 6, 7, 8]);
|
139 | t.end();
|
140 | });
|
141 |
|
142 | test('writeBytes', function(t) {
|
143 | var buf = new Pbf();
|
144 | buf.writeBytes([1, 2, 3, 4, 5, 6, 7, 8]);
|
145 | var bytes = buf.finish();
|
146 | t.same(toArray(bytes), [8, 1, 2, 3, 4, 5, 6, 7, 8]);
|
147 | t.end();
|
148 | });
|
149 |
|
150 | test('readDouble', function(t) {
|
151 | var buffer = new Buffer(8);
|
152 | buffer.writeDoubleLE(12345.6789012345, 0);
|
153 | var buf = new Pbf(buffer);
|
154 | t.equal(Math.round(buf.readDouble() * 1e10) / 1e10, 12345.6789012345);
|
155 | t.end();
|
156 | });
|
157 |
|
158 | test('readPacked and writePacked', function(t) {
|
159 | var testNumbers2 = testNumbers.slice(0, 10);
|
160 |
|
161 | ['Varint', 'SVarint', 'Float', 'Double', 'Fixed32', 'SFixed32', 'Fixed64', 'SFixed64'].forEach(function(type) {
|
162 | var buf = new Pbf();
|
163 | buf['writePacked' + type](1, testNumbers2);
|
164 | buf.finish();
|
165 | buf.readFields(function readField(tag) {
|
166 | if (tag === 1) t.same(buf['readPacked' + type](), testNumbers2, 'packed ' + type);
|
167 | else t.fail('wrong tag encountered: ' + tag);
|
168 | });
|
169 | });
|
170 |
|
171 | var buf = new Pbf();
|
172 | buf.writePackedBoolean(1, testNumbers2);
|
173 | buf.finish();
|
174 | buf.readFields(function readField(tag) {
|
175 | if (tag === 1) t.same(buf.readPackedBoolean(),
|
176 | [true, false, false, true, true, true, true, true, true, true], 'packed Boolean');
|
177 | else t.fail('wrong tag encountered: ' + tag);
|
178 | });
|
179 |
|
180 | t.end();
|
181 | });
|
182 |
|
183 | test('writeDouble', function(t) {
|
184 | var buf = new Pbf(new Buffer(8));
|
185 | buf.writeDouble(12345.6789012345);
|
186 | buf.finish();
|
187 | t.equal(Math.round(buf.readDouble() * 1e10) / 1e10, 12345.6789012345);
|
188 | t.end();
|
189 | });
|
190 |
|
191 | test('readFloat', function(t) {
|
192 | var buffer = new Buffer(4);
|
193 | buffer.writeFloatLE(123.456, 0);
|
194 | var buf = new Pbf(buffer);
|
195 | t.equal(Math.round(1000 * buf.readFloat()) / 1000, 123.456);
|
196 | t.end();
|
197 | });
|
198 |
|
199 | test('writeFloat', function(t) {
|
200 | var buf = new Pbf(new Buffer(4));
|
201 | buf.writeFloat(123.456);
|
202 | buf.finish();
|
203 | t.equal(Math.round(1000 * buf.readFloat()) / 1000, 123.456);
|
204 | t.end();
|
205 | });
|
206 |
|
207 | test('readFixed32', function(t) {
|
208 | var buffer = new Buffer(16);
|
209 | buffer.writeUInt32LE(42, 0);
|
210 | buffer.writeUInt32LE(24, 4);
|
211 | var buf = new Pbf(buffer);
|
212 | t.equal(buf.readFixed32(), 42);
|
213 | t.equal(buf.readFixed32(), 24);
|
214 | t.end();
|
215 | });
|
216 |
|
217 | test('writeFixed32', function(t) {
|
218 | var buf = new Pbf(new Buffer(16));
|
219 | buf.writeFixed32(42);
|
220 | buf.writeFixed32(24);
|
221 | buf.finish();
|
222 | t.equal(buf.readFixed32(), 42);
|
223 | t.equal(buf.readFixed32(), 24);
|
224 | t.end();
|
225 | });
|
226 |
|
227 | test('readFixed64', function(t) {
|
228 | var buf = new Pbf(new Buffer(8));
|
229 | buf.writeFixed64(102451124123);
|
230 | buf.finish();
|
231 | t.same(buf.readFixed64(), 102451124123);
|
232 | t.end();
|
233 | });
|
234 |
|
235 | test('writeFixed64', function(t) {
|
236 | var buf = new Pbf(new Buffer(8));
|
237 | buf.writeFixed64(102451124123);
|
238 | t.same(toArray(buf.buf), [155,23,144,218,23,0,0,0]);
|
239 | t.end();
|
240 | });
|
241 |
|
242 | test('readSFixed32', function(t) {
|
243 | var buffer = new Buffer(16);
|
244 | buffer.writeInt32LE(4223, 0);
|
245 | buffer.writeInt32LE(-1231, 4);
|
246 | var buf = new Pbf(buffer);
|
247 | t.equal(buf.readSFixed32(), 4223);
|
248 | t.equal(buf.readSFixed32(), -1231);
|
249 | t.end();
|
250 | });
|
251 |
|
252 | test('writeSFixed32', function(t) {
|
253 | var buf = new Pbf(new Buffer(16));
|
254 | buf.writeSFixed32(4223);
|
255 | buf.writeSFixed32(-1231);
|
256 | buf.finish();
|
257 | t.equal(buf.readSFixed32(), 4223);
|
258 | t.equal(buf.readSFixed32(), -1231);
|
259 | t.end();
|
260 | });
|
261 |
|
262 | test('readSFixed64', function(t) {
|
263 | var buf = new Pbf(new Buffer(8));
|
264 | buf.writeSFixed64(-102451124123);
|
265 | buf.finish();
|
266 | t.same(buf.readSFixed64(), -102451124123);
|
267 | t.end();
|
268 | });
|
269 |
|
270 | test('writeSFixed64', function(t) {
|
271 | var buf = new Pbf(new Buffer(8));
|
272 | buf.writeSFixed64(-102451124123);
|
273 | t.same(toArray(buf.buf), [101,232,111,37,232,255,255,255]);
|
274 | t.end();
|
275 | });
|
276 |
|
277 | test('writeString', function(t) {
|
278 | var buffer = new Buffer(32);
|
279 | var buf = new Pbf(buffer);
|
280 | var len = Buffer.byteLength('Привет');
|
281 | buf.writeVarint(len);
|
282 | buffer.write('Привет', buf.pos);
|
283 | buf.pos += len;
|
284 | buf.finish();
|
285 | t.equal(buf.readString(), 'Привет');
|
286 | t.end();
|
287 | });
|
288 |
|
289 | test('readString', function(t) {
|
290 | var buf = new Pbf(new Buffer(0));
|
291 | buf.writeString('Привет');
|
292 | buf.finish();
|
293 | t.equal(buf.readString(), 'Привет');
|
294 | t.end();
|
295 | });
|
296 |
|
297 | test('readFields', function(t) {
|
298 | var buf = new Pbf(fs.readFileSync(path.join(__dirname, '/fixtures/12665.vector.pbf'))),
|
299 | layerOffsets = [],
|
300 | foo = {}, res, res2, buf2;
|
301 |
|
302 | res2 = buf.readFields(function(tag, result, buf) {
|
303 | if (tag === 3) layerOffsets.push(buf.pos);
|
304 | res = result;
|
305 | buf2 = buf;
|
306 | }, foo);
|
307 |
|
308 | t.equal(res, foo);
|
309 | t.equal(res2, foo);
|
310 | t.equal(buf2, buf);
|
311 |
|
312 | t.ok(buf.pos >= buf.length);
|
313 | t.same(layerOffsets, [1,2490,2581,2819,47298,47626,55732,56022,56456,88178,112554]);
|
314 |
|
315 | t.end();
|
316 | });
|
317 |
|
318 | test('readMessage', function(t) {
|
319 | var buf = new Pbf(fs.readFileSync(path.join(__dirname, '/fixtures/12665.vector.pbf'))),
|
320 | layerNames = [],
|
321 | foo = {};
|
322 |
|
323 | buf.readFields(function(tag) {
|
324 | if (tag === 3) buf.readMessage(readLayer, foo);
|
325 | }, foo);
|
326 |
|
327 | function readLayer(tag) {
|
328 | if (tag === 1) layerNames.push(buf.readString());
|
329 | }
|
330 |
|
331 | t.same(layerNames, ['landuse','water','barrier_line','building','tunnel','road',
|
332 | 'place_label','water_label','poi_label','road_label','housenum_label']);
|
333 |
|
334 | t.end();
|
335 | });
|
336 |
|
337 | test('field writing methods', function(t) {
|
338 | var buf = new Pbf();
|
339 | buf.writeFixed32Field(1, 100);
|
340 | buf.writeFixed64Field(2, 200);
|
341 | buf.writeVarintField(3, 1234);
|
342 | buf.writeSVarintField(4, -599);
|
343 | buf.writeStringField(5, 'Hello world');
|
344 | buf.writeFloatField(6, 123);
|
345 | buf.writeDoubleField(7, 123);
|
346 | buf.writeBooleanField(8, true);
|
347 | buf.writeBytesField(9, [1, 2, 3]);
|
348 | buf.writeMessage(10, function() {
|
349 | buf.writeBooleanField(1, true);
|
350 | buf.writePackedVarint(2, testNumbers);
|
351 | });
|
352 |
|
353 | buf.writeSFixed32Field(11, -123);
|
354 | buf.writeSFixed64Field(12, -256);
|
355 |
|
356 | buf.finish();
|
357 |
|
358 | buf.readFields(function(tag) {
|
359 | if (tag === 1) buf.readFixed32();
|
360 | else if (tag === 2) buf.readFixed64();
|
361 | else if (tag === 3) buf.readVarint();
|
362 | else if (tag === 4) buf.readSVarint();
|
363 | else if (tag === 5) buf.readString();
|
364 | else if (tag === 6) buf.readFloat();
|
365 | else if (tag === 7) buf.readDouble();
|
366 | else if (tag === 8) buf.readBoolean();
|
367 | else if (tag === 9) buf.readBytes();
|
368 | else if (tag === 10) buf.readMessage(function() { });
|
369 | else if (tag === 11) buf.readSFixed32();
|
370 | else if (tag === 12) buf.readSFixed64();
|
371 | else t.fail('unknown tag');
|
372 | });
|
373 | t.end();
|
374 | });
|
375 |
|
376 | test('skip', function(t) {
|
377 | var buf = new Pbf();
|
378 | buf.writeFixed32Field(1, 100);
|
379 | buf.writeFixed64Field(2, 200);
|
380 | buf.writeVarintField(3, 1234);
|
381 | buf.writeStringField(4, 'Hello world');
|
382 | buf.finish();
|
383 |
|
384 | buf.readFields(function() { });
|
385 |
|
386 | t.equal(buf.pos, buf.length);
|
387 |
|
388 | t.throws(function() {
|
389 | buf.skip(6);
|
390 | });
|
391 | t.end();
|
392 | });
|