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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 } | 140 } |
141 | 141 |
142 // Encoding values outside the valid range should assert. | 142 // Encoding values outside the valid range should assert. |
143 assertThrows(function() {writeValue.call(encoder, lowerLimit * 1.1);}); | 143 assertThrows(function() {writeValue.call(encoder, lowerLimit * 1.1);}); |
144 assertThrows(function() {writeValue.call(encoder, upperLimit * 1.1);}); | 144 assertThrows(function() {writeValue.call(encoder, upperLimit * 1.1);}); |
145 } | 145 } |
146 | 146 |
147 describe('binaryDecoderTest', function() { | 147 describe('binaryDecoderTest', function() { |
148 /** | 148 /** |
149 * Tests the decoder instance cache. | 149 * Tests the decoder instance cache. |
150 * @suppress {visibility} | |
151 */ | 150 */ |
152 it('testInstanceCache', function() { | 151 it('testInstanceCache', /** @suppress {visibility} */ function() { |
153 // Empty the instance caches. | 152 // Empty the instance caches. |
154 jspb.BinaryDecoder.instanceCache_ = []; | 153 jspb.BinaryDecoder.instanceCache_ = []; |
155 | 154 |
156 // Allocating and then freeing a decoder should put it in the instance | 155 // Allocating and then freeing a decoder should put it in the instance |
157 // cache. | 156 // cache. |
158 jspb.BinaryDecoder.alloc().free(); | 157 jspb.BinaryDecoder.alloc().free(); |
159 | 158 |
160 assertEquals(1, jspb.BinaryDecoder.instanceCache_.length); | 159 assertEquals(1, jspb.BinaryDecoder.instanceCache_.length); |
161 | 160 |
162 // Allocating and then freeing three decoders should leave us with three in | 161 // Allocating and then freeing three decoders should leave us with three in |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 assertEquals(hashA, decoder.readVarintHash64()); | 202 assertEquals(hashA, decoder.readVarintHash64()); |
204 assertEquals(hashB, decoder.readVarintHash64()); | 203 assertEquals(hashB, decoder.readVarintHash64()); |
205 assertEquals(hashC, decoder.readVarintHash64()); | 204 assertEquals(hashC, decoder.readVarintHash64()); |
206 assertEquals(hashD, decoder.readVarintHash64()); | 205 assertEquals(hashD, decoder.readVarintHash64()); |
207 | 206 |
208 assertEquals(hashA, decoder.readFixedHash64()); | 207 assertEquals(hashA, decoder.readFixedHash64()); |
209 assertEquals(hashB, decoder.readFixedHash64()); | 208 assertEquals(hashB, decoder.readFixedHash64()); |
210 assertEquals(hashC, decoder.readFixedHash64()); | 209 assertEquals(hashC, decoder.readFixedHash64()); |
211 assertEquals(hashD, decoder.readFixedHash64()); | 210 assertEquals(hashD, decoder.readFixedHash64()); |
212 }); | 211 }); |
| 212 |
| 213 /** |
| 214 * Test encoding and decoding utf-8. |
| 215 */ |
| 216 it('testUtf8', function() { |
| 217 var encoder = new jspb.BinaryEncoder(); |
213 | 218 |
| 219 var ascii = "ASCII should work in 3, 2, 1..." |
| 220 var utf8_two_bytes = "©"; |
| 221 var utf8_three_bytes = "❄"; |
| 222 var utf8_four_bytes = "😁"; |
| 223 |
| 224 encoder.writeString(ascii); |
| 225 encoder.writeString(utf8_two_bytes); |
| 226 encoder.writeString(utf8_three_bytes); |
| 227 encoder.writeString(utf8_four_bytes); |
| 228 |
| 229 var decoder = jspb.BinaryDecoder.alloc(encoder.end()); |
| 230 |
| 231 assertEquals(ascii, decoder.readString(ascii.length)); |
| 232 assertEquals(utf8_two_bytes, decoder.readString(utf8_two_bytes.length)); |
| 233 assertEquals(utf8_three_bytes, decoder.readString(utf8_three_bytes.length)); |
| 234 assertEquals(utf8_four_bytes, decoder.readString(utf8_four_bytes.length)); |
| 235 }); |
214 | 236 |
215 /** | 237 /** |
216 * Verifies that misuse of the decoder class triggers assertions. | 238 * Verifies that misuse of the decoder class triggers assertions. |
217 * @suppress {checkTypes|visibility} | 239 * @suppress {checkTypes|visibility} |
218 */ | 240 */ |
219 it('testDecodeErrors', function() { | 241 it('testDecodeErrors', function() { |
220 // Reading a value past the end of the stream should trigger an assertion. | 242 // Reading a value past the end of the stream should trigger an assertion. |
221 var decoder = jspb.BinaryDecoder.alloc([0, 1, 2]); | 243 var decoder = jspb.BinaryDecoder.alloc([0, 1, 2]); |
222 assertThrows(function() {decoder.readUint64()}); | 244 assertThrows(function() {decoder.readUint64()}); |
223 | 245 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 348 |
327 doTestSignedValue( | 349 doTestSignedValue( |
328 jspb.BinaryDecoder.prototype.readDouble, | 350 jspb.BinaryDecoder.prototype.readDouble, |
329 jspb.BinaryEncoder.prototype.writeDouble, | 351 jspb.BinaryEncoder.prototype.writeDouble, |
330 jspb.BinaryConstants.FLOAT64_EPS * 10, | 352 jspb.BinaryConstants.FLOAT64_EPS * 10, |
331 -jspb.BinaryConstants.FLOAT64_MAX, | 353 -jspb.BinaryConstants.FLOAT64_MAX, |
332 jspb.BinaryConstants.FLOAT64_MAX, | 354 jspb.BinaryConstants.FLOAT64_MAX, |
333 function(x) { return x; }); | 355 function(x) { return x; }); |
334 }); | 356 }); |
335 }); | 357 }); |
OLD | NEW |