| 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} | |
| 56 */ | 55 */ |
| 57 it('testInstanceCaches', function() { | 56 it('testInstanceCaches', /** @suppress {visibility} */ function() { |
| 58 var writer = new jspb.BinaryWriter(); | 57 var writer = new jspb.BinaryWriter(); |
| 59 var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); | 58 var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); |
| 60 writer.writeMessage(1, dummyMessage, goog.nullFunction); | 59 writer.writeMessage(1, dummyMessage, goog.nullFunction); |
| 61 writer.writeMessage(2, dummyMessage, goog.nullFunction); | 60 writer.writeMessage(2, dummyMessage, goog.nullFunction); |
| 62 | 61 |
| 63 var buffer = writer.getResultBuffer(); | 62 var buffer = writer.getResultBuffer(); |
| 64 | 63 |
| 65 // Empty the instance caches. | 64 // Empty the instance caches. |
| 66 jspb.BinaryReader.instanceCache_ = []; | 65 jspb.BinaryReader.instanceCache_ = []; |
| 67 | 66 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 */ | 123 */ |
| 125 function truncate(x) { | 124 function truncate(x) { |
| 126 var temp = new Float32Array(1); | 125 var temp = new Float32Array(1); |
| 127 temp[0] = x; | 126 temp[0] = x; |
| 128 return temp[0]; | 127 return temp[0]; |
| 129 } | 128 } |
| 130 | 129 |
| 131 | 130 |
| 132 /** | 131 /** |
| 133 * Verifies that misuse of the reader class triggers assertions. | 132 * Verifies that misuse of the reader class triggers assertions. |
| 134 * @suppress {checkTypes|visibility} | |
| 135 */ | 133 */ |
| 136 it('testReadErrors', function() { | 134 it('testReadErrors', /** @suppress {checkTypes|visibility} */ function() { |
| 137 // Calling readMessage on a non-delimited field should trigger an | 135 // Calling readMessage on a non-delimited field should trigger an |
| 138 // assertion. | 136 // assertion. |
| 139 var reader = jspb.BinaryReader.alloc([8, 1]); | 137 var reader = jspb.BinaryReader.alloc([8, 1]); |
| 140 var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); | 138 var dummyMessage = /** @type {!jspb.BinaryMessage} */({}); |
| 141 reader.nextField(); | 139 reader.nextField(); |
| 142 assertThrows(function() { | 140 assertThrows(function() { |
| 143 reader.readMessage(dummyMessage, goog.nullFunction); | 141 reader.readMessage(dummyMessage, goog.nullFunction); |
| 144 }); | 142 }); |
| 145 | 143 |
| 146 // Reading past the end of the stream should trigger an assertion. | 144 // Reading past the end of the stream should trigger an assertion. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 /** | 191 /** |
| 194 * Tests encoding and decoding of unsigned field types. | 192 * Tests encoding and decoding of unsigned field types. |
| 195 * @param {Function} readField | 193 * @param {Function} readField |
| 196 * @param {Function} writeField | 194 * @param {Function} writeField |
| 197 * @param {number} epsilon | 195 * @param {number} epsilon |
| 198 * @param {number} upperLimit | 196 * @param {number} upperLimit |
| 199 * @param {Function} filter | 197 * @param {Function} filter |
| 200 * @private | 198 * @private |
| 201 * @suppress {missingProperties} | 199 * @suppress {missingProperties} |
| 202 */ | 200 */ |
| 203 function doTestUnsignedField_(readField, | 201 var doTestUnsignedField_ = function(readField, |
| 204 writeField, epsilon, upperLimit, filter) { | 202 writeField, epsilon, upperLimit, filter) { |
| 205 assertNotNull(readField); | 203 assertNotNull(readField); |
| 206 assertNotNull(writeField); | 204 assertNotNull(writeField); |
| 207 | 205 |
| 208 var writer = new jspb.BinaryWriter(); | 206 var writer = new jspb.BinaryWriter(); |
| 209 | 207 |
| 210 // Encode zero and limits. | 208 // Encode zero and limits. |
| 211 writeField.call(writer, 1, filter(0)); | 209 writeField.call(writer, 1, filter(0)); |
| 212 writeField.call(writer, 2, filter(epsilon)); | 210 writeField.call(writer, 2, filter(epsilon)); |
| 213 writeField.call(writer, 3, filter(upperLimit)); | 211 writeField.call(writer, 3, filter(upperLimit)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 * Tests encoding and decoding of signed field types. | 243 * Tests encoding and decoding of signed field types. |
| 246 * @param {Function} readField | 244 * @param {Function} readField |
| 247 * @param {Function} writeField | 245 * @param {Function} writeField |
| 248 * @param {number} epsilon | 246 * @param {number} epsilon |
| 249 * @param {number} lowerLimit | 247 * @param {number} lowerLimit |
| 250 * @param {number} upperLimit | 248 * @param {number} upperLimit |
| 251 * @param {Function} filter | 249 * @param {Function} filter |
| 252 * @private | 250 * @private |
| 253 * @suppress {missingProperties} | 251 * @suppress {missingProperties} |
| 254 */ | 252 */ |
| 255 function doTestSignedField_(readField, | 253 var doTestSignedField_ = function(readField, |
| 256 writeField, epsilon, lowerLimit, upperLimit, filter) { | 254 writeField, epsilon, lowerLimit, upperLimit, filter) { |
| 257 var writer = new jspb.BinaryWriter(); | 255 var writer = new jspb.BinaryWriter(); |
| 258 | 256 |
| 259 // Encode zero and limits. | 257 // Encode zero and limits. |
| 260 writeField.call(writer, 1, filter(lowerLimit)); | 258 writeField.call(writer, 1, filter(lowerLimit)); |
| 261 writeField.call(writer, 2, filter(-epsilon)); | 259 writeField.call(writer, 2, filter(-epsilon)); |
| 262 writeField.call(writer, 3, filter(0)); | 260 writeField.call(writer, 3, filter(0)); |
| 263 writeField.call(writer, 4, filter(epsilon)); | 261 writeField.call(writer, 4, filter(epsilon)); |
| 264 writeField.call(writer, 5, filter(upperLimit)); | 262 writeField.call(writer, 5, filter(upperLimit)); |
| 265 | 263 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 assertEquals(expected.fieldNumber, reader.getFieldNumber()); | 312 assertEquals(expected.fieldNumber, reader.getFieldNumber()); |
| 315 assertEquals(expected.value, readField.call(reader)); | 313 assertEquals(expected.value, readField.call(reader)); |
| 316 } | 314 } |
| 317 }; | 315 }; |
| 318 | 316 |
| 319 | 317 |
| 320 /** | 318 /** |
| 321 * Tests fields that use varint encoding. | 319 * Tests fields that use varint encoding. |
| 322 */ | 320 */ |
| 323 it('testVarintFields', function() { | 321 it('testVarintFields', function() { |
| 324 assertNotNull(jspb.BinaryReader.prototype.readUint32); | 322 assertNotUndefined(jspb.BinaryReader.prototype.readUint32); |
| 325 assertNotNull(jspb.BinaryReader.prototype.writeUint32); | 323 assertNotUndefined(jspb.BinaryWriter.prototype.writeUint32); |
| 326 assertNotNull(jspb.BinaryReader.prototype.readUint64); | 324 assertNotUndefined(jspb.BinaryReader.prototype.readUint64); |
| 327 assertNotNull(jspb.BinaryReader.prototype.writeUint64); | 325 assertNotUndefined(jspb.BinaryWriter.prototype.writeUint64); |
| 328 assertNotNull(jspb.BinaryReader.prototype.readBool); | 326 assertNotUndefined(jspb.BinaryReader.prototype.readBool); |
| 329 assertNotNull(jspb.BinaryReader.prototype.writeBool); | 327 assertNotUndefined(jspb.BinaryWriter.prototype.writeBool); |
| 330 doTestUnsignedField_( | 328 doTestUnsignedField_( |
| 331 jspb.BinaryReader.prototype.readUint32, | 329 jspb.BinaryReader.prototype.readUint32, |
| 332 jspb.BinaryWriter.prototype.writeUint32, | 330 jspb.BinaryWriter.prototype.writeUint32, |
| 333 1, Math.pow(2, 32) - 1, Math.round); | 331 1, Math.pow(2, 32) - 1, Math.round); |
| 334 | 332 |
| 335 doTestUnsignedField_( | 333 doTestUnsignedField_( |
| 336 jspb.BinaryReader.prototype.readUint64, | 334 jspb.BinaryReader.prototype.readUint64, |
| 337 jspb.BinaryWriter.prototype.writeUint64, | 335 jspb.BinaryWriter.prototype.writeUint64, |
| 338 1, Math.pow(2, 64) - 1025, Math.round); | 336 1, Math.pow(2, 64) - 1025, Math.round); |
| 339 | 337 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 353 1, -Math.pow(2, 31), Math.pow(2, 31) - 1, Math.round); | 351 1, -Math.pow(2, 31), Math.pow(2, 31) - 1, Math.round); |
| 354 | 352 |
| 355 doTestUnsignedField_( | 353 doTestUnsignedField_( |
| 356 jspb.BinaryReader.prototype.readBool, | 354 jspb.BinaryReader.prototype.readBool, |
| 357 jspb.BinaryWriter.prototype.writeBool, | 355 jspb.BinaryWriter.prototype.writeBool, |
| 358 1, 1, function(x) { return !!x; }); | 356 1, 1, function(x) { return !!x; }); |
| 359 }); | 357 }); |
| 360 | 358 |
| 361 | 359 |
| 362 /** | 360 /** |
| 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 /** |
| 363 * Tests 64-bit fields that are handled as strings. | 412 * Tests 64-bit fields that are handled as strings. |
| 364 */ | 413 */ |
| 365 it('testStringInt64Fields', function() { | 414 it('testStringInt64Fields', function() { |
| 366 var writer = new jspb.BinaryWriter(); | 415 var writer = new jspb.BinaryWriter(); |
| 367 | 416 |
| 368 var testSignedData = [ | 417 var testSignedData = [ |
| 369 '2730538252207801776', | 418 '2730538252207801776', |
| 370 '-2688470994844604560', | 419 '-2688470994844604560', |
| 371 '3398529779486536359', | 420 '3398529779486536359', |
| 372 '3568577411627971000', | 421 '3568577411627971000', |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 864 reader.runReadCallback('readCallback'); | 913 reader.runReadCallback('readCallback'); |
| 865 }); | 914 }); |
| 866 | 915 |
| 867 reader.nextField(); | 916 reader.nextField(); |
| 868 assertEquals(7, reader.getFieldNumber()); | 917 assertEquals(7, reader.getFieldNumber()); |
| 869 assertEquals(700, reader.readInt32()); | 918 assertEquals(700, reader.readInt32()); |
| 870 | 919 |
| 871 assertEquals(false, reader.nextField()); | 920 assertEquals(false, reader.nextField()); |
| 872 }); | 921 }); |
| 873 }); | 922 }); |
| OLD | NEW |