Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1464)

Side by Side Diff: third_party/protobuf/js/binary/utils_test.js

Issue 1983203003: Update third_party/protobuf to protobuf-v3.0.0-beta-3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: owners Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/protobuf/js/binary/utils.js ('k') | third_party/protobuf/js/binary/writer.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 result = jspb.utils.hash64ArrayToDecimalStrings([ 190 result = jspb.utils.hash64ArrayToDecimalStrings([
191 toHashString(0xFFFFFFFF, 0xFFFFFFFF), 191 toHashString(0xFFFFFFFF, 0xFFFFFFFF),
192 toHashString(0x00000000, 0x80000000), 192 toHashString(0x00000000, 0x80000000),
193 toHashString(0xacd05f15, 0x01b69b4b)], false); 193 toHashString(0xacd05f15, 0x01b69b4b)], false);
194 assertEquals(3, result.length); 194 assertEquals(3, result.length);
195 assertEquals('18446744073709551615', result[0]); 195 assertEquals('18446744073709551615', result[0]);
196 assertEquals('9223372036854775808', result[1]); 196 assertEquals('9223372036854775808', result[1]);
197 assertEquals('123456789123456789', result[2]); 197 assertEquals('123456789123456789', result[2]);
198 }); 198 });
199 199
200 /*
201 * Going from decimal strings to hash strings should be lossless.
202 */
203 it('testDecimalToHashConversion', function() {
204 var result;
205 var convert = jspb.utils.decimalStringToHash64;
206
207 result = convert('0');
208 assertEquals(String.fromCharCode.apply(null,
209 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result);
210
211 result = convert('-1');
212 assertEquals(String.fromCharCode.apply(null,
213 [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
214
215 result = convert('18446744073709551615');
216 assertEquals(String.fromCharCode.apply(null,
217 [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
218
219 result = convert('9223372036854775808');
220 assertEquals(String.fromCharCode.apply(null,
221 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result);
222
223 result = convert('-9223372036854775808');
224 assertEquals(String.fromCharCode.apply(null,
225 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result);
226
227 result = convert('123456789123456789');
228 assertEquals(String.fromCharCode.apply(null,
229 [0x15, 0x5F, 0xD0, 0xAC, 0x4B, 0x9B, 0xB6, 0x01]), result);
230
231 result = convert('-123456789123456789');
232 assertEquals(String.fromCharCode.apply(null,
233 [0xEB, 0xA0, 0x2F, 0x53, 0xB4, 0x64, 0x49, 0xFE]), result);
234 });
200 235
201 /** 236 /**
202 * Going from hash strings to hex strings should be lossless. 237 * Going from hash strings to hex strings should be lossless.
203 */ 238 */
204 it('testHashToHexConversion', function() { 239 it('testHashToHexConversion', function() {
205 var result; 240 var result;
206 var convert = jspb.utils.hash64ToHexString; 241 var convert = jspb.utils.hash64ToHexString;
207 242
208 result = convert(toHashString(0x00000000, 0x00000000)); 243 result = convert(toHashString(0x00000000, 0x00000000));
209 assertEquals('0x0000000000000000', result); 244 assertEquals('0x0000000000000000', result);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 * Tests conversion from 32-bit floating point numbers to split64 numbers. 338 * Tests conversion from 32-bit floating point numbers to split64 numbers.
304 */ 339 */
305 it('testFloat32ToSplit64', function() { 340 it('testFloat32ToSplit64', function() {
306 var f32_eps = jspb.BinaryConstants.FLOAT32_EPS; 341 var f32_eps = jspb.BinaryConstants.FLOAT32_EPS;
307 var f32_min = jspb.BinaryConstants.FLOAT32_MIN; 342 var f32_min = jspb.BinaryConstants.FLOAT32_MIN;
308 var f32_max = jspb.BinaryConstants.FLOAT32_MAX; 343 var f32_max = jspb.BinaryConstants.FLOAT32_MAX;
309 344
310 // NaN. 345 // NaN.
311 jspb.utils.splitFloat32(NaN); 346 jspb.utils.splitFloat32(NaN);
312 if (!isNaN(jspb.utils.joinFloat32(jspb.utils.split64Low, 347 if (!isNaN(jspb.utils.joinFloat32(jspb.utils.split64Low,
313 jspb.utils.split64High))) { 348 jspb.utils.split64High))) {
314 throw 'fail!'; 349 throw 'fail!';
315 } 350 }
316 351
317 /** 352 /**
318 * @param {number} x 353 * @param {number} x
319 * @param {number=} opt_bits 354 * @param {number=} opt_bits
320 */ 355 */
321 function test(x, opt_bits) { 356 function test(x, opt_bits) {
322 jspb.utils.splitFloat32(x); 357 jspb.utils.splitFloat32(x);
323 if (goog.isDef(opt_bits)) { 358 if (goog.isDef(opt_bits)) {
324 if (opt_bits != jspb.utils.split64Low) throw 'fail!'; 359 if (opt_bits != jspb.utils.split64Low) throw 'fail!';
325 } 360 }
326 if (truncate(x) != jspb.utils.joinFloat32(jspb.utils.split64Low, 361 if (truncate(x) != jspb.utils.joinFloat32(jspb.utils.split64Low,
327 jspb.utils.split64High)) { 362 jspb.utils.split64High)) {
328 throw 'fail!'; 363 throw 'fail!';
329 } 364 }
330 } 365 }
331 366
332 // Positive and negative infinity. 367 // Positive and negative infinity.
333 test(Infinity, 0x7f800000); 368 test(Infinity, 0x7f800000);
334 test(-Infinity, 0xff800000); 369 test(-Infinity, 0xff800000);
335 370
336 // Positive and negative zero. 371 // Positive and negative zero.
337 test(0, 0x00000000); 372 test(0, 0x00000000);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 * Tests conversion from 64-bit floating point numbers to split64 numbers. 404 * Tests conversion from 64-bit floating point numbers to split64 numbers.
370 */ 405 */
371 it('testFloat64ToSplit64', function() { 406 it('testFloat64ToSplit64', function() {
372 var f64_eps = jspb.BinaryConstants.FLOAT64_EPS; 407 var f64_eps = jspb.BinaryConstants.FLOAT64_EPS;
373 var f64_min = jspb.BinaryConstants.FLOAT64_MIN; 408 var f64_min = jspb.BinaryConstants.FLOAT64_MIN;
374 var f64_max = jspb.BinaryConstants.FLOAT64_MAX; 409 var f64_max = jspb.BinaryConstants.FLOAT64_MAX;
375 410
376 // NaN. 411 // NaN.
377 jspb.utils.splitFloat64(NaN); 412 jspb.utils.splitFloat64(NaN);
378 if (!isNaN(jspb.utils.joinFloat64(jspb.utils.split64Low, 413 if (!isNaN(jspb.utils.joinFloat64(jspb.utils.split64Low,
379 jspb.utils.split64High))) { 414 jspb.utils.split64High))) {
380 throw 'fail!'; 415 throw 'fail!';
381 } 416 }
382 417
383 /** 418 /**
384 * @param {number} x 419 * @param {number} x
385 * @param {number=} opt_highBits 420 * @param {number=} opt_highBits
386 * @param {number=} opt_lowBits 421 * @param {number=} opt_lowBits
387 */ 422 */
388 function test(x, opt_highBits, opt_lowBits) { 423 function test(x, opt_highBits, opt_lowBits) {
389 jspb.utils.splitFloat64(x); 424 jspb.utils.splitFloat64(x);
390 if (goog.isDef(opt_highBits)) { 425 if (goog.isDef(opt_highBits)) {
391 if (opt_highBits != jspb.utils.split64High) throw 'fail!'; 426 if (opt_highBits != jspb.utils.split64High) throw 'fail!';
392 } 427 }
393 if (goog.isDef(opt_lowBits)) { 428 if (goog.isDef(opt_lowBits)) {
394 if (opt_lowBits != jspb.utils.split64Low) throw 'fail!'; 429 if (opt_lowBits != jspb.utils.split64Low) throw 'fail!';
395 } 430 }
396 if (x != jspb.utils.joinFloat64(jspb.utils.split64Low, 431 if (x != jspb.utils.joinFloat64(jspb.utils.split64Low,
397 jspb.utils.split64High)) { 432 jspb.utils.split64High)) {
398 throw 'fail!'; 433 throw 'fail!';
399 } 434 }
400 } 435 }
401 436
402 // Positive and negative infinity. 437 // Positive and negative infinity.
403 test(Infinity, 0x7ff00000, 0x00000000); 438 test(Infinity, 0x7ff00000, 0x00000000);
404 test(-Infinity, 0xfff00000, 0x00000000); 439 test(-Infinity, 0xfff00000, 0x00000000);
405 440
406 // Positive and negative zero. 441 // Positive and negative zero.
407 test(0, 0x00000000, 0x00000000); 442 test(0, 0x00000000, 0x00000000);
(...skipping 24 matching lines...) Expand all
432 test(cursor); 467 test(cursor);
433 cursor *= 1.1; 468 cursor *= 1.1;
434 } 469 }
435 }); 470 });
436 471
437 472
438 /** 473 /**
439 * Tests counting packed varints. 474 * Tests counting packed varints.
440 */ 475 */
441 it('testCountVarints', function() { 476 it('testCountVarints', function() {
442 var writer = new jspb.BinaryWriter(); 477 var values = [];
443
444 var count = 0;
445 for (var i = 1; i < 1000000000; i *= 1.1) { 478 for (var i = 1; i < 1000000000; i *= 1.1) {
446 writer.rawWriteVarint(Math.floor(i)); 479 values.push(Math.floor(i));
447 count++;
448 } 480 }
449 481
482 var writer = new jspb.BinaryWriter();
483 writer.writePackedUint64(1, values);
484
450 var buffer = new Uint8Array(writer.getResultBuffer()); 485 var buffer = new Uint8Array(writer.getResultBuffer());
451 assertEquals(count, jspb.utils.countVarints(buffer, 0, buffer.length)); 486
487 // We should have two more varints than we started with - one for the field
488 // tag, one for the packed length.
489 assertEquals(values.length + 2,
490 jspb.utils.countVarints(buffer, 0, buffer.length));
452 }); 491 });
453 492
454 493
455 /** 494 /**
456 * Tests counting matching varint fields. 495 * Tests counting matching varint fields.
457 */ 496 */
458 it('testCountVarintFields', function() { 497 it('testCountVarintFields', function() {
459 var writer = new jspb.BinaryWriter(); 498 var writer = new jspb.BinaryWriter();
460 499
461 var count = 0; 500 var count = 0;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 assertEquals(sourceBytes, convert(sourceBytes)); 657 assertEquals(sourceBytes, convert(sourceBytes));
619 658
620 // Converting Array.<numbers> into Uint8Arrays should work. 659 // Converting Array.<numbers> into Uint8Arrays should work.
621 check(convert(sourceData)); 660 check(convert(sourceData));
622 661
623 // Converting ArrayBuffers into Uint8Arrays should work. 662 // Converting ArrayBuffers into Uint8Arrays should work.
624 check(convert(sourceBuffer)); 663 check(convert(sourceBuffer));
625 664
626 // Converting base64-encoded strings into Uint8Arrays should work. 665 // Converting base64-encoded strings into Uint8Arrays should work.
627 check(convert(sourceBase64)); 666 check(convert(sourceBase64));
628
629 // Converting binary-data strings into Uint8Arrays should work.
630 check(convert(sourceString, /* opt_stringIsRawBytes = */ true));
631 }); 667 });
632 }); 668 });
OLDNEW
« no previous file with comments | « third_party/protobuf/js/binary/utils.js ('k') | third_party/protobuf/js/binary/writer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698