OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 goog.require('jspb.BinaryConstants'); | 45 goog.require('jspb.BinaryConstants'); |
46 goog.require('jspb.BinaryDecoder'); | 46 goog.require('jspb.BinaryDecoder'); |
47 goog.require('jspb.BinaryReader'); | 47 goog.require('jspb.BinaryReader'); |
48 goog.require('jspb.BinaryWriter'); | 48 goog.require('jspb.BinaryWriter'); |
49 | 49 |
50 | 50 |
51 | 51 |
52 describe('binaryReaderTest', function() { | 52 describe('binaryReaderTest', function() { |
53 /** | 53 /** |
54 * Tests the reader instance cache. | 54 * Tests the reader instance cache. |
| 55 * @suppress {visibility} |
55 */ | 56 */ |
56 it('testInstanceCaches', /** @suppress {visibility} */ function() { | 57 it('testInstanceCaches', function() { |
57 var writer = new jspb.BinaryWriter(); | 58 var writer = new jspb.BinaryWriter(); |
58 var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); | 59 var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); |
59 writer.writeMessage(1, dummyMessage, goog.nullFunction); | 60 writer.writeMessage(1, dummyMessage, goog.nullFunction); |
60 writer.writeMessage(2, dummyMessage, goog.nullFunction); | 61 writer.writeMessage(2, dummyMessage, goog.nullFunction); |
61 | 62 |
62 var buffer = writer.getResultBuffer(); | 63 var buffer = writer.getResultBuffer(); |
63 | 64 |
64 // Empty the instance caches. | 65 // Empty the instance caches. |
65 jspb.BinaryReader.instanceCache_ = []; | 66 jspb.BinaryReader.instanceCache_ = []; |
66 | 67 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 */ | 124 */ |
124 function truncate(x) { | 125 function truncate(x) { |
125 var temp = new Float32Array(1); | 126 var temp = new Float32Array(1); |
126 temp[0] = x; | 127 temp[0] = x; |
127 return temp[0]; | 128 return temp[0]; |
128 } | 129 } |
129 | 130 |
130 | 131 |
131 /** | 132 /** |
132 * Verifies that misuse of the reader class triggers assertions. | 133 * Verifies that misuse of the reader class triggers assertions. |
| 134 * @suppress {checkTypes|visibility} |
133 */ | 135 */ |
134 it('testReadErrors', /** @suppress {checkTypes|visibility} */ function() { | 136 it('testReadErrors', function() { |
135 // Calling readMessage on a non-delimited field should trigger an | 137 // Calling readMessage on a non-delimited field should trigger an |
136 // assertion. | 138 // assertion. |
137 var reader = jspb.BinaryReader.alloc([8, 1]); | 139 var reader = jspb.BinaryReader.alloc([8, 1]); |
138 var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); | 140 var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); |
139 reader.nextField(); | 141 reader.nextField(); |
140 assertThrows(function() { | 142 assertThrows(function() { |
141 reader.readMessage(dummyMessage, goog.nullFunction); | 143 reader.readMessage(dummyMessage, goog.nullFunction); |
142 }); | 144 }); |
143 | 145 |
144 // Reading past the end of the stream should trigger an assertion. | 146 // Reading past the end of the stream should trigger an assertion. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 /** | 193 /** |
192 * Tests encoding and decoding of unsigned field types. | 194 * Tests encoding and decoding of unsigned field types. |
193 * @param {Function} readField | 195 * @param {Function} readField |
194 * @param {Function} writeField | 196 * @param {Function} writeField |
195 * @param {number} epsilon | 197 * @param {number} epsilon |
196 * @param {number} upperLimit | 198 * @param {number} upperLimit |
197 * @param {Function} filter | 199 * @param {Function} filter |
198 * @private | 200 * @private |
199 * @suppress {missingProperties} | 201 * @suppress {missingProperties} |
200 */ | 202 */ |
201 var doTestUnsignedField_ = function(readField, | 203 function doTestUnsignedField_(readField, |
202 writeField, epsilon, upperLimit, filter) { | 204 writeField, epsilon, upperLimit, filter) { |
203 assertNotNull(readField); | 205 assertNotNull(readField); |
204 assertNotNull(writeField); | 206 assertNotNull(writeField); |
205 | 207 |
206 var writer = new jspb.BinaryWriter(); | 208 var writer = new jspb.BinaryWriter(); |
207 | 209 |
208 // Encode zero and limits. | 210 // Encode zero and limits. |
209 writeField.call(writer, 1, filter(0)); | 211 writeField.call(writer, 1, filter(0)); |
210 writeField.call(writer, 2, filter(epsilon)); | 212 writeField.call(writer, 2, filter(epsilon)); |
211 writeField.call(writer, 3, filter(upperLimit)); | 213 writeField.call(writer, 3, filter(upperLimit)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 * Tests encoding and decoding of signed field types. | 245 * Tests encoding and decoding of signed field types. |
244 * @param {Function} readField | 246 * @param {Function} readField |
245 * @param {Function} writeField | 247 * @param {Function} writeField |
246 * @param {number} epsilon | 248 * @param {number} epsilon |
247 * @param {number} lowerLimit | 249 * @param {number} lowerLimit |
248 * @param {number} upperLimit | 250 * @param {number} upperLimit |
249 * @param {Function} filter | 251 * @param {Function} filter |
250 * @private | 252 * @private |
251 * @suppress {missingProperties} | 253 * @suppress {missingProperties} |
252 */ | 254 */ |
253 var doTestSignedField_ = function(readField, | 255 function doTestSignedField_(readField, |
254 writeField, epsilon, lowerLimit, upperLimit, filter) { | 256 writeField, epsilon, lowerLimit, upperLimit, filter) { |
255 var writer = new jspb.BinaryWriter(); | 257 var writer = new jspb.BinaryWriter(); |
256 | 258 |
257 // Encode zero and limits. | 259 // Encode zero and limits. |
258 writeField.call(writer, 1, filter(lowerLimit)); | 260 writeField.call(writer, 1, filter(lowerLimit)); |
259 writeField.call(writer, 2, filter(-epsilon)); | 261 writeField.call(writer, 2, filter(-epsilon)); |
260 writeField.call(writer, 3, filter(0)); | 262 writeField.call(writer, 3, filter(0)); |
261 writeField.call(writer, 4, filter(epsilon)); | 263 writeField.call(writer, 4, filter(epsilon)); |
262 writeField.call(writer, 5, filter(upperLimit)); | 264 writeField.call(writer, 5, filter(upperLimit)); |
263 | 265 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 assertEquals(expected.fieldNumber, reader.getFieldNumber()); | 314 assertEquals(expected.fieldNumber, reader.getFieldNumber()); |
313 assertEquals(expected.value, readField.call(reader)); | 315 assertEquals(expected.value, readField.call(reader)); |
314 } | 316 } |
315 }; | 317 }; |
316 | 318 |
317 | 319 |
318 /** | 320 /** |
319 * Tests fields that use varint encoding. | 321 * Tests fields that use varint encoding. |
320 */ | 322 */ |
321 it('testVarintFields', function() { | 323 it('testVarintFields', function() { |
322 assertNotUndefined(jspb.BinaryReader.prototype.readUint32); | 324 assertNotNull(jspb.BinaryReader.prototype.readUint32); |
323 assertNotUndefined(jspb.BinaryWriter.prototype.writeUint32); | 325 assertNotNull(jspb.BinaryReader.prototype.writeUint32); |
324 assertNotUndefined(jspb.BinaryReader.prototype.readUint64); | 326 assertNotNull(jspb.BinaryReader.prototype.readUint64); |
325 assertNotUndefined(jspb.BinaryWriter.prototype.writeUint64); | 327 assertNotNull(jspb.BinaryReader.prototype.writeUint64); |
326 assertNotUndefined(jspb.BinaryReader.prototype.readBool); | 328 assertNotNull(jspb.BinaryReader.prototype.readBool); |
327 assertNotUndefined(jspb.BinaryWriter.prototype.writeBool); | 329 assertNotNull(jspb.BinaryReader.prototype.writeBool); |
328 doTestUnsignedField_( | 330 doTestUnsignedField_( |
329 jspb.BinaryReader.prototype.readUint32, | 331 jspb.BinaryReader.prototype.readUint32, |
330 jspb.BinaryWriter.prototype.writeUint32, | 332 jspb.BinaryWriter.prototype.writeUint32, |
331 1, Math.pow(2, 32) - 1, Math.round); | 333 1, Math.pow(2, 32) - 1, Math.round); |
332 | 334 |
333 doTestUnsignedField_( | 335 doTestUnsignedField_( |
334 jspb.BinaryReader.prototype.readUint64, | 336 jspb.BinaryReader.prototype.readUint64, |
335 jspb.BinaryWriter.prototype.writeUint64, | 337 jspb.BinaryWriter.prototype.writeUint64, |
336 1, Math.pow(2, 64) - 1025, Math.round); | 338 1, Math.pow(2, 64) - 1025, Math.round); |
337 | 339 |
(...skipping 13 matching lines...) Expand all Loading... |
351 1, -Math.pow(2, 31), Math.pow(2, 31) - 1, Math.round); | 353 1, -Math.pow(2, 31), Math.pow(2, 31) - 1, Math.round); |
352 | 354 |
353 doTestUnsignedField_( | 355 doTestUnsignedField_( |
354 jspb.BinaryReader.prototype.readBool, | 356 jspb.BinaryReader.prototype.readBool, |
355 jspb.BinaryWriter.prototype.writeBool, | 357 jspb.BinaryWriter.prototype.writeBool, |
356 1, 1, function(x) { return !!x; }); | 358 1, 1, function(x) { return !!x; }); |
357 }); | 359 }); |
358 | 360 |
359 | 361 |
360 /** | 362 /** |
361 * Tests reading a field from hexadecimal string (format: '08 BE EF'). | |
362 * @param {Function} readField | |
363 * @param {number} expected | |
364 * @param {string} hexString | |
365 */ | |
366 function doTestHexStringVarint_(readField, expected, hexString) { | |
367 var bytesCount = (hexString.length + 1) / 3; | |
368 var bytes = new Uint8Array(bytesCount); | |
369 for (var i = 0; i < bytesCount; i++) { | |
370 bytes[i] = parseInt(hexString.substring(i * 3, i * 3 + 2), 16); | |
371 } | |
372 var reader = jspb.BinaryReader.alloc(bytes); | |
373 reader.nextField(); | |
374 assertEquals(expected, readField.call(reader)); | |
375 } | |
376 | |
377 | |
378 /** | |
379 * Tests non-canonical redundant varint decoding. | |
380 */ | |
381 it('testRedundantVarintFields', function() { | |
382 assertNotNull(jspb.BinaryReader.prototype.readUint32); | |
383 assertNotNull(jspb.BinaryReader.prototype.readUint64); | |
384 assertNotNull(jspb.BinaryReader.prototype.readSint32); | |
385 assertNotNull(jspb.BinaryReader.prototype.readSint64); | |
386 | |
387 // uint32 and sint32 take no more than 5 bytes | |
388 // 08 - field prefix (type = 0 means varint) | |
389 doTestHexStringVarint_( | |
390 jspb.BinaryReader.prototype.readUint32, | |
391 12, '08 8C 80 80 80 00'); | |
392 | |
393 // 11 stands for -6 in zigzag encoding | |
394 doTestHexStringVarint_( | |
395 jspb.BinaryReader.prototype.readSint32, | |
396 -6, '08 8B 80 80 80 00'); | |
397 | |
398 // uint64 and sint64 take no more than 10 bytes | |
399 // 08 - field prefix (type = 0 means varint) | |
400 doTestHexStringVarint_( | |
401 jspb.BinaryReader.prototype.readUint64, | |
402 12, '08 8C 80 80 80 80 80 80 80 80 00'); | |
403 | |
404 // 11 stands for -6 in zigzag encoding | |
405 doTestHexStringVarint_( | |
406 jspb.BinaryReader.prototype.readSint64, | |
407 -6, '08 8B 80 80 80 80 80 80 80 80 00'); | |
408 }); | |
409 | |
410 | |
411 /** | |
412 * Tests 64-bit fields that are handled as strings. | 363 * Tests 64-bit fields that are handled as strings. |
413 */ | 364 */ |
414 it('testStringInt64Fields', function() { | 365 it('testStringInt64Fields', function() { |
415 var writer = new jspb.BinaryWriter(); | 366 var writer = new jspb.BinaryWriter(); |
416 | 367 |
417 var testSignedData = [ | 368 var testSignedData = [ |
418 '2730538252207801776', | 369 '2730538252207801776', |
419 '-2688470994844604560', | 370 '-2688470994844604560', |
420 '3398529779486536359', | 371 '3398529779486536359', |
421 '3568577411627971000', | 372 '3568577411627971000', |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
913 reader.runReadCallback('readCallback'); | 864 reader.runReadCallback('readCallback'); |
914 }); | 865 }); |
915 | 866 |
916 reader.nextField(); | 867 reader.nextField(); |
917 assertEquals(7, reader.getFieldNumber()); | 868 assertEquals(7, reader.getFieldNumber()); |
918 assertEquals(700, reader.readInt32()); | 869 assertEquals(700, reader.readInt32()); |
919 | 870 |
920 assertEquals(false, reader.nextField()); | 871 assertEquals(false, reader.nextField()); |
921 }); | 872 }); |
922 }); | 873 }); |
OLD | NEW |