Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | /** |
| 32 | * @fileoverview Test cases for jspb's binary protocol buffer decoder. |
| 33 | * |
| 34 | * There are two particular magic numbers that need to be pointed out - |
| 35 | * 2^64-1025 is the largest number representable as both a double and an |
| 36 | * unsigned 64-bit integer, and 2^63-513 is the largest number representable as |
| 37 | * both a double and a signed 64-bit integer. |
| 38 | * |
| 39 | * Test suite is written using Jasmine -- see http://jasmine.github.io/ |
| 40 | * |
| 41 | * @author aappleby@google.com (Austin Appleby) |
| 42 | */ |
| 43 | |
| 44 | goog.require('goog.testing.asserts'); |
| 45 | goog.require('jspb.BinaryConstants'); |
| 46 | goog.require('jspb.BinaryDecoder'); |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 47 | goog.require('jspb.BinaryEncoder'); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 48 | |
| 49 | |
| 50 | /** |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 51 | * Tests encoding and decoding of unsigned types. |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 52 | * @param {Function} readValue |
| 53 | * @param {Function} writeValue |
| 54 | * @param {number} epsilon |
| 55 | * @param {number} upperLimit |
| 56 | * @param {Function} filter |
| 57 | * @suppress {missingProperties|visibility} |
| 58 | */ |
| 59 | function doTestUnsignedValue(readValue, |
| 60 | writeValue, epsilon, upperLimit, filter) { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 61 | var encoder = new jspb.BinaryEncoder(); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 62 | |
| 63 | // Encode zero and limits. |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 64 | writeValue.call(encoder, filter(0)); |
| 65 | writeValue.call(encoder, filter(epsilon)); |
| 66 | writeValue.call(encoder, filter(upperLimit)); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 67 | |
| 68 | // Encode positive values. |
| 69 | for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 70 | writeValue.call(encoder, filter(cursor)); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 73 | var decoder = jspb.BinaryDecoder.alloc(encoder.end()); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 74 | |
| 75 | // Check zero and limits. |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 76 | assertEquals(filter(0), readValue.call(decoder)); |
| 77 | assertEquals(filter(epsilon), readValue.call(decoder)); |
| 78 | assertEquals(filter(upperLimit), readValue.call(decoder)); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 79 | |
| 80 | // Check positive values. |
| 81 | for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 82 | if (filter(cursor) != readValue.call(decoder)) throw 'fail!'; |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 83 | } |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 84 | |
| 85 | // Encoding values outside the valid range should assert. |
| 86 | assertThrows(function() {writeValue.call(encoder, -1);}); |
| 87 | assertThrows(function() {writeValue.call(encoder, upperLimit * 1.1);}); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 92 | * Tests encoding and decoding of signed types. |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 93 | * @param {Function} readValue |
| 94 | * @param {Function} writeValue |
| 95 | * @param {number} epsilon |
| 96 | * @param {number} lowerLimit |
| 97 | * @param {number} upperLimit |
| 98 | * @param {Function} filter |
| 99 | * @suppress {missingProperties} |
| 100 | */ |
| 101 | function doTestSignedValue(readValue, |
| 102 | writeValue, epsilon, lowerLimit, upperLimit, filter) { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 103 | var encoder = new jspb.BinaryEncoder(); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 104 | |
| 105 | // Encode zero and limits. |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 106 | writeValue.call(encoder, filter(lowerLimit)); |
| 107 | writeValue.call(encoder, filter(-epsilon)); |
| 108 | writeValue.call(encoder, filter(0)); |
| 109 | writeValue.call(encoder, filter(epsilon)); |
| 110 | writeValue.call(encoder, filter(upperLimit)); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 111 | |
| 112 | var inputValues = []; |
| 113 | |
| 114 | // Encode negative values. |
| 115 | for (var cursor = lowerLimit; cursor < -epsilon; cursor /= 1.1) { |
| 116 | var val = filter(cursor); |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 117 | writeValue.call(encoder, val); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 118 | inputValues.push(val); |
| 119 | } |
| 120 | |
| 121 | // Encode positive values. |
| 122 | for (var cursor = epsilon; cursor < upperLimit; cursor *= 1.1) { |
| 123 | var val = filter(cursor); |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 124 | writeValue.call(encoder, val); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 125 | inputValues.push(val); |
| 126 | } |
| 127 | |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 128 | var decoder = jspb.BinaryDecoder.alloc(encoder.end()); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 129 | |
| 130 | // Check zero and limits. |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 131 | assertEquals(filter(lowerLimit), readValue.call(decoder)); |
| 132 | assertEquals(filter(-epsilon), readValue.call(decoder)); |
| 133 | assertEquals(filter(0), readValue.call(decoder)); |
| 134 | assertEquals(filter(epsilon), readValue.call(decoder)); |
| 135 | assertEquals(filter(upperLimit), readValue.call(decoder)); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 136 | |
| 137 | // Verify decoded values. |
| 138 | for (var i = 0; i < inputValues.length; i++) { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 139 | assertEquals(inputValues[i], readValue.call(decoder)); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 140 | } |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 141 | |
| 142 | // Encoding values outside the valid range should assert. |
| 143 | assertThrows(function() {writeValue.call(encoder, lowerLimit * 1.1);}); |
| 144 | assertThrows(function() {writeValue.call(encoder, upperLimit * 1.1);}); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | describe('binaryDecoderTest', function() { |
| 148 | /** |
| 149 | * Tests the decoder instance cache. |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 150 | */ |
Adam Cozzette | d64a2d9 | 2016-06-29 15:23:27 -0700 | [diff] [blame] | 151 | it('testInstanceCache', /** @suppress {visibility} */ function() { |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 152 | // Empty the instance caches. |
| 153 | jspb.BinaryDecoder.instanceCache_ = []; |
| 154 | |
| 155 | // Allocating and then freeing a decoder should put it in the instance |
| 156 | // cache. |
| 157 | jspb.BinaryDecoder.alloc().free(); |
| 158 | |
| 159 | assertEquals(1, jspb.BinaryDecoder.instanceCache_.length); |
| 160 | |
| 161 | // Allocating and then freeing three decoders should leave us with three in |
| 162 | // the cache. |
| 163 | |
| 164 | var decoder1 = jspb.BinaryDecoder.alloc(); |
| 165 | var decoder2 = jspb.BinaryDecoder.alloc(); |
| 166 | var decoder3 = jspb.BinaryDecoder.alloc(); |
| 167 | decoder1.free(); |
| 168 | decoder2.free(); |
| 169 | decoder3.free(); |
| 170 | |
| 171 | assertEquals(3, jspb.BinaryDecoder.instanceCache_.length); |
| 172 | }); |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * Tests reading 64-bit integers as hash strings. |
| 177 | */ |
| 178 | it('testHashStrings', function() { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 179 | var encoder = new jspb.BinaryEncoder(); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 180 | |
| 181 | var hashA = String.fromCharCode(0x00, 0x00, 0x00, 0x00, |
| 182 | 0x00, 0x00, 0x00, 0x00); |
| 183 | var hashB = String.fromCharCode(0x12, 0x34, 0x00, 0x00, |
| 184 | 0x00, 0x00, 0x00, 0x00); |
| 185 | var hashC = String.fromCharCode(0x12, 0x34, 0x56, 0x78, |
| 186 | 0x87, 0x65, 0x43, 0x21); |
| 187 | var hashD = String.fromCharCode(0xFF, 0xFF, 0xFF, 0xFF, |
| 188 | 0xFF, 0xFF, 0xFF, 0xFF); |
| 189 | |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 190 | encoder.writeVarintHash64(hashA); |
| 191 | encoder.writeVarintHash64(hashB); |
| 192 | encoder.writeVarintHash64(hashC); |
| 193 | encoder.writeVarintHash64(hashD); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 194 | |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 195 | encoder.writeFixedHash64(hashA); |
| 196 | encoder.writeFixedHash64(hashB); |
| 197 | encoder.writeFixedHash64(hashC); |
| 198 | encoder.writeFixedHash64(hashD); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 199 | |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 200 | var decoder = jspb.BinaryDecoder.alloc(encoder.end()); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 201 | |
| 202 | assertEquals(hashA, decoder.readVarintHash64()); |
| 203 | assertEquals(hashB, decoder.readVarintHash64()); |
| 204 | assertEquals(hashC, decoder.readVarintHash64()); |
| 205 | assertEquals(hashD, decoder.readVarintHash64()); |
| 206 | |
| 207 | assertEquals(hashA, decoder.readFixedHash64()); |
| 208 | assertEquals(hashB, decoder.readFixedHash64()); |
| 209 | assertEquals(hashC, decoder.readFixedHash64()); |
| 210 | assertEquals(hashD, decoder.readFixedHash64()); |
| 211 | }); |
Paul Yang | 7f3e237 | 2017-01-31 09:17:32 -0800 | [diff] [blame] | 212 | |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 213 | /** |
Feng Xiao | d36c0c5 | 2017-03-29 14:32:48 -0700 | [diff] [blame] | 214 | * Tests reading and writing large strings |
| 215 | */ |
| 216 | it('testLargeStrings', function() { |
| 217 | var encoder = new jspb.BinaryEncoder(); |
| 218 | |
| 219 | var len = 150000; |
| 220 | var long_string = ''; |
| 221 | for (var i = 0; i < len; i++) { |
| 222 | long_string += 'a'; |
| 223 | } |
| 224 | |
| 225 | encoder.writeString(long_string); |
| 226 | |
| 227 | var decoder = jspb.BinaryDecoder.alloc(encoder.end()); |
| 228 | |
| 229 | assertEquals(long_string, decoder.readString(len)); |
| 230 | }); |
| 231 | |
| 232 | /** |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 233 | * Test encoding and decoding utf-8. |
| 234 | */ |
| 235 | it('testUtf8', function() { |
| 236 | var encoder = new jspb.BinaryEncoder(); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 237 | |
Paul Yang | 7f3e237 | 2017-01-31 09:17:32 -0800 | [diff] [blame] | 238 | var ascii = "ASCII should work in 3, 2, 1..."; |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 239 | var utf8_two_bytes = "©"; |
Wojciech Mandrysz | 7332ffb | 2016-10-03 18:59:34 +0200 | [diff] [blame] | 240 | var utf8_three_bytes = "❄"; |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 241 | var utf8_four_bytes = "😁"; |
Paul Yang | 7f3e237 | 2017-01-31 09:17:32 -0800 | [diff] [blame] | 242 | |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 243 | encoder.writeString(ascii); |
| 244 | encoder.writeString(utf8_two_bytes); |
Wojciech Mandrysz | 7332ffb | 2016-10-03 18:59:34 +0200 | [diff] [blame] | 245 | encoder.writeString(utf8_three_bytes); |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 246 | encoder.writeString(utf8_four_bytes); |
Paul Yang | 7f3e237 | 2017-01-31 09:17:32 -0800 | [diff] [blame] | 247 | |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 248 | var decoder = jspb.BinaryDecoder.alloc(encoder.end()); |
Paul Yang | 7f3e237 | 2017-01-31 09:17:32 -0800 | [diff] [blame] | 249 | |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 250 | assertEquals(ascii, decoder.readString(ascii.length)); |
| 251 | assertEquals(utf8_two_bytes, decoder.readString(utf8_two_bytes.length)); |
Wojciech Mandrysz | 7332ffb | 2016-10-03 18:59:34 +0200 | [diff] [blame] | 252 | assertEquals(utf8_three_bytes, decoder.readString(utf8_three_bytes.length)); |
Wojciech Mandrysz | fe1d0a1 | 2016-10-03 01:42:58 +0200 | [diff] [blame] | 253 | assertEquals(utf8_four_bytes, decoder.readString(utf8_four_bytes.length)); |
| 254 | }); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * Verifies that misuse of the decoder class triggers assertions. |
| 258 | * @suppress {checkTypes|visibility} |
| 259 | */ |
| 260 | it('testDecodeErrors', function() { |
| 261 | // Reading a value past the end of the stream should trigger an assertion. |
| 262 | var decoder = jspb.BinaryDecoder.alloc([0, 1, 2]); |
| 263 | assertThrows(function() {decoder.readUint64()}); |
| 264 | |
| 265 | // Overlong varints should trigger assertions. |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 266 | decoder.setBlock([255, 255, 255, 255, 255, 255, |
| 267 | 255, 255, 255, 255, 255, 0]); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 268 | assertThrows(function() {decoder.readUnsignedVarint64()}); |
| 269 | decoder.reset(); |
| 270 | assertThrows(function() {decoder.readSignedVarint64()}); |
| 271 | decoder.reset(); |
| 272 | assertThrows(function() {decoder.readZigzagVarint64()}); |
Jisi Liu | 1a7a7fc | 2017-10-18 12:22:18 -0700 | [diff] [blame] | 273 | decoder.reset(); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 274 | assertThrows(function() {decoder.readUnsignedVarint32()}); |
| 275 | }); |
| 276 | |
| 277 | |
| 278 | /** |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 279 | * Tests encoding and decoding of unsigned integers. |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 280 | */ |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 281 | it('testUnsignedIntegers', function() { |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 282 | doTestUnsignedValue( |
| 283 | jspb.BinaryDecoder.prototype.readUint8, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 284 | jspb.BinaryEncoder.prototype.writeUint8, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 285 | 1, 0xFF, Math.round); |
| 286 | |
| 287 | doTestUnsignedValue( |
| 288 | jspb.BinaryDecoder.prototype.readUint16, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 289 | jspb.BinaryEncoder.prototype.writeUint16, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 290 | 1, 0xFFFF, Math.round); |
| 291 | |
| 292 | doTestUnsignedValue( |
| 293 | jspb.BinaryDecoder.prototype.readUint32, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 294 | jspb.BinaryEncoder.prototype.writeUint32, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 295 | 1, 0xFFFFFFFF, Math.round); |
| 296 | |
| 297 | doTestUnsignedValue( |
| 298 | jspb.BinaryDecoder.prototype.readUint64, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 299 | jspb.BinaryEncoder.prototype.writeUint64, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 300 | 1, Math.pow(2, 64) - 1025, Math.round); |
| 301 | }); |
| 302 | |
| 303 | |
| 304 | /** |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 305 | * Tests encoding and decoding of signed integers. |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 306 | */ |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 307 | it('testSignedIntegers', function() { |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 308 | doTestSignedValue( |
| 309 | jspb.BinaryDecoder.prototype.readInt8, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 310 | jspb.BinaryEncoder.prototype.writeInt8, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 311 | 1, -0x80, 0x7F, Math.round); |
| 312 | |
| 313 | doTestSignedValue( |
| 314 | jspb.BinaryDecoder.prototype.readInt16, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 315 | jspb.BinaryEncoder.prototype.writeInt16, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 316 | 1, -0x8000, 0x7FFF, Math.round); |
| 317 | |
| 318 | doTestSignedValue( |
| 319 | jspb.BinaryDecoder.prototype.readInt32, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 320 | jspb.BinaryEncoder.prototype.writeInt32, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 321 | 1, -0x80000000, 0x7FFFFFFF, Math.round); |
| 322 | |
| 323 | doTestSignedValue( |
| 324 | jspb.BinaryDecoder.prototype.readInt64, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 325 | jspb.BinaryEncoder.prototype.writeInt64, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 326 | 1, -Math.pow(2, 63), Math.pow(2, 63) - 513, Math.round); |
| 327 | }); |
| 328 | |
| 329 | |
| 330 | /** |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 331 | * Tests encoding and decoding of floats. |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 332 | */ |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 333 | it('testFloats', function() { |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 334 | /** |
| 335 | * @param {number} x |
| 336 | * @return {number} |
| 337 | */ |
| 338 | function truncate(x) { |
| 339 | var temp = new Float32Array(1); |
| 340 | temp[0] = x; |
| 341 | return temp[0]; |
| 342 | } |
| 343 | doTestSignedValue( |
| 344 | jspb.BinaryDecoder.prototype.readFloat, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 345 | jspb.BinaryEncoder.prototype.writeFloat, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 346 | jspb.BinaryConstants.FLOAT32_EPS, |
| 347 | -jspb.BinaryConstants.FLOAT32_MAX, |
| 348 | jspb.BinaryConstants.FLOAT32_MAX, |
| 349 | truncate); |
| 350 | |
| 351 | doTestSignedValue( |
| 352 | jspb.BinaryDecoder.prototype.readDouble, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 353 | jspb.BinaryEncoder.prototype.writeDouble, |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 354 | jspb.BinaryConstants.FLOAT64_EPS * 10, |
| 355 | -jspb.BinaryConstants.FLOAT64_MAX, |
| 356 | jspb.BinaryConstants.FLOAT64_MAX, |
| 357 | function(x) { return x; }); |
| 358 | }); |
| 359 | }); |