utils_test.js 20.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/**
 * @fileoverview Test cases for jspb's helper functions.
 *
 * Test suite is written using Jasmine -- see http://jasmine.github.io/
 *
 * @author aappleby@google.com (Austin Appleby)
 */

39
goog.require('goog.crypt');
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
goog.require('goog.crypt.base64');
goog.require('goog.testing.asserts');
goog.require('jspb.BinaryConstants');
goog.require('jspb.BinaryWriter');
goog.require('jspb.utils');


/**
 * @param {number} x
 * @return {number}
 */
function truncate(x) {
  var temp = new Float32Array(1);
  temp[0] = x;
  return temp[0];
}


/**
 * Converts an 64-bit integer in split representation to a 64-bit hash string
 * (8 bits encoded per character).
 * @param {number} bitsLow The low 32 bits of the split 64-bit integer.
 * @param {number} bitsHigh The high 32 bits of the split 64-bit integer.
 * @return {string} The encoded hash string, 8 bits per character.
 */
function toHashString(bitsLow, bitsHigh) {
  return String.fromCharCode((bitsLow >>> 0) & 0xFF,
                             (bitsLow >>> 8) & 0xFF,
                             (bitsLow >>> 16) & 0xFF,
                             (bitsLow >>> 24) & 0xFF,
                             (bitsHigh >>> 0) & 0xFF,
                             (bitsHigh >>> 8) & 0xFF,
                             (bitsHigh >>> 16) & 0xFF,
                             (bitsHigh >>> 24) & 0xFF);
}


describe('binaryUtilsTest', function() {
  /**
   * Tests lossless binary-to-decimal conversion.
   */
  it('testDecimalConversion', function() {
    // Check some magic numbers.
    var result =
        jspb.utils.joinUnsignedDecimalString(0x89e80001, 0x8ac72304);
    assertEquals('10000000000000000001', result);

    result = jspb.utils.joinUnsignedDecimalString(0xacd05f15, 0x1b69b4b);
    assertEquals('123456789123456789', result);

    result = jspb.utils.joinUnsignedDecimalString(0xeb1f0ad2, 0xab54a98c);
    assertEquals('12345678901234567890', result);

    result = jspb.utils.joinUnsignedDecimalString(0xe3b70cb1, 0x891087b8);
    assertEquals('9876543210987654321', result);

    // Check limits.
    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00000000);
    assertEquals('0', result);

    result = jspb.utils.joinUnsignedDecimalString(0xFFFFFFFF, 0xFFFFFFFF);
    assertEquals('18446744073709551615', result);

    // Check each bit of the low dword.
    for (var i = 0; i < 32; i++) {
      var low = (1 << i) >>> 0;
      result = jspb.utils.joinUnsignedDecimalString(low, 0);
      assertEquals('' + Math.pow(2, i), result);
    }

    // Check the first 20 bits of the high dword.
    for (var i = 0; i < 20; i++) {
      var high = (1 << i) >>> 0;
      result = jspb.utils.joinUnsignedDecimalString(0, high);
      assertEquals('' + Math.pow(2, 32 + i), result);
    }

    // V8's internal double-to-string conversion is inaccurate for values above
    // 2^52, even if they're representable integers - check the rest of the bits
    // manually against the correct string representations of 2^N.

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00100000);
    assertEquals('4503599627370496', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00200000);
    assertEquals('9007199254740992', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00400000);
    assertEquals('18014398509481984', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00800000);
    assertEquals('36028797018963968', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x01000000);
    assertEquals('72057594037927936', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x02000000);
    assertEquals('144115188075855872', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x04000000);
    assertEquals('288230376151711744', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x08000000);
    assertEquals('576460752303423488', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x10000000);
    assertEquals('1152921504606846976', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x20000000);
    assertEquals('2305843009213693952', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x40000000);
    assertEquals('4611686018427387904', result);

    result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x80000000);
    assertEquals('9223372036854775808', result);
  });


  /**
   * Going from hash strings to decimal strings should also be lossless.
   */
  it('testHashToDecimalConversion', function() {
    var result;
    var convert = jspb.utils.hash64ToDecimalString;

    result = convert(toHashString(0x00000000, 0x00000000), false);
    assertEquals('0', result);

    result = convert(toHashString(0x00000000, 0x00000000), true);
    assertEquals('0', result);

    result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF), false);
    assertEquals('18446744073709551615', result);

    result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF), true);
    assertEquals('-1', result);

    result = convert(toHashString(0x00000000, 0x80000000), false);
    assertEquals('9223372036854775808', result);

    result = convert(toHashString(0x00000000, 0x80000000), true);
    assertEquals('-9223372036854775808', result);

    result = convert(toHashString(0xacd05f15, 0x01b69b4b), false);
    assertEquals('123456789123456789', result);

    result = convert(toHashString(~0xacd05f15 + 1, ~0x01b69b4b), true);
    assertEquals('-123456789123456789', result);

    // And converting arrays of hashes should work the same way.
    result = jspb.utils.hash64ArrayToDecimalStrings([
      toHashString(0xFFFFFFFF, 0xFFFFFFFF),
      toHashString(0x00000000, 0x80000000),
      toHashString(0xacd05f15, 0x01b69b4b)], false);
    assertEquals(3, result.length);
    assertEquals('18446744073709551615', result[0]);
    assertEquals('9223372036854775808', result[1]);
    assertEquals('123456789123456789', result[2]);
  });

201 202 203 204 205 206 207 208
  /*
   * Going from decimal strings to hash strings should be lossless.
   */
  it('testDecimalToHashConversion', function() {
    var result;
    var convert = jspb.utils.decimalStringToHash64;

    result = convert('0');
209
    assertEquals(goog.crypt.byteArrayToString(
210 211 212
      [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result);

    result = convert('-1');
213
    assertEquals(goog.crypt.byteArrayToString(
214 215 216
      [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);

    result = convert('18446744073709551615');
217
    assertEquals(goog.crypt.byteArrayToString(
218 219 220
      [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);

    result = convert('9223372036854775808');
221
    assertEquals(goog.crypt.byteArrayToString(
222 223 224
      [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result);

    result = convert('-9223372036854775808');
225
    assertEquals(goog.crypt.byteArrayToString(
226 227 228
      [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result);

    result = convert('123456789123456789');
229
    assertEquals(goog.crypt.byteArrayToString(
230 231 232
      [0x15, 0x5F, 0xD0, 0xAC, 0x4B, 0x9B, 0xB6, 0x01]), result);

    result = convert('-123456789123456789');
233
    assertEquals(goog.crypt.byteArrayToString(
234 235
      [0xEB, 0xA0, 0x2F, 0x53, 0xB4, 0x64, 0x49, 0xFE]), result);
  });
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

  /**
   * Going from hash strings to hex strings should be lossless.
   */
  it('testHashToHexConversion', function() {
    var result;
    var convert = jspb.utils.hash64ToHexString;

    result = convert(toHashString(0x00000000, 0x00000000));
    assertEquals('0x0000000000000000', result);

    result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF));
    assertEquals('0xffffffffffffffff', result);

    result = convert(toHashString(0x12345678, 0x9ABCDEF0));
    assertEquals('0x9abcdef012345678', result);
  });


  /**
   * Going from hex strings to hash strings should be lossless.
   */
  it('testHexToHashConversion', function() {
    var result;
    var convert = jspb.utils.hexStringToHash64;

    result = convert('0x0000000000000000');
263
    assertEquals(goog.crypt.byteArrayToString(
264 265 266
        [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result);

    result = convert('0xffffffffffffffff');
267
    assertEquals(goog.crypt.byteArrayToString(
268 269 270 271
        [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);

    // Hex string is big-endian, hash string is little-endian.
    result = convert('0x123456789ABCDEF0');
272
    assertEquals(goog.crypt.byteArrayToString(
273 274 275 276
        [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]), result);

    // Capitalization should not matter.
    result = convert('0x0000abcdefABCDEF');
277
    assertEquals(goog.crypt.byteArrayToString(
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
        [0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB, 0x00, 0x00]), result);
  });


  /**
   * Going from numbers to hash strings should be lossless for up to 53 bits of
   * precision.
   */
  it('testNumberToHashConversion', function() {
    var result;
    var convert = jspb.utils.numberToHash64;

    result = convert(0x0000000000000);
    assertEquals('0x0000000000000000', jspb.utils.hash64ToHexString(result));

    result = convert(0xFFFFFFFFFFFFF);
    assertEquals('0x000fffffffffffff', jspb.utils.hash64ToHexString(result));

    result = convert(0x123456789ABCD);
    assertEquals('0x000123456789abcd', jspb.utils.hash64ToHexString(result));

    result = convert(0xDCBA987654321);
    assertEquals('0x000dcba987654321', jspb.utils.hash64ToHexString(result));

    // 53 bits of precision should not be truncated.
    result = convert(0x10000000000001);
    assertEquals('0x0010000000000001', jspb.utils.hash64ToHexString(result));

    // 54 bits of precision should be truncated.
    result = convert(0x20000000000001);
    assertNotEquals(
        '0x0020000000000001', jspb.utils.hash64ToHexString(result));
  });


  /**
   * Sanity check the behavior of Javascript's strings when doing funny things
   * with unicode characters.
   */
  it('sanityCheckUnicodeStrings', function() {
    var strings = new Array(65536);

    // All possible unsigned 16-bit values should be storable in a string, they
    // shouldn't do weird things with the length of the string, and they should
    // come back out of the string unchanged.
    for (var i = 0; i < 65536; i++) {
      strings[i] = 'a' + String.fromCharCode(i) + 'a';
      if (3 != strings[i].length) throw 'fail!';
      if (i != strings[i].charCodeAt(1)) throw 'fail!';
    }

    // Each unicode character should compare equal to itself and not equal to a
    // different unicode character.
    for (var i = 0; i < 65536; i++) {
      if (strings[i] != strings[i]) throw 'fail!';
      if (strings[i] == strings[(i + 1) % 65536]) throw 'fail!';
    }
  });


  /**
   * Tests conversion from 32-bit floating point numbers to split64 numbers.
   */
  it('testFloat32ToSplit64', function() {
    var f32_eps = jspb.BinaryConstants.FLOAT32_EPS;
    var f32_min = jspb.BinaryConstants.FLOAT32_MIN;
    var f32_max = jspb.BinaryConstants.FLOAT32_MAX;

    // NaN.
    jspb.utils.splitFloat32(NaN);
    if (!isNaN(jspb.utils.joinFloat32(jspb.utils.split64Low,
349
                                      jspb.utils.split64High))) {
350 351 352 353 354 355 356 357 358 359 360 361 362
      throw 'fail!';
    }

    /**
     * @param {number} x
     * @param {number=} opt_bits
     */
    function test(x, opt_bits) {
      jspb.utils.splitFloat32(x);
      if (goog.isDef(opt_bits)) {
        if (opt_bits != jspb.utils.split64Low) throw 'fail!';
      }
      if (truncate(x) != jspb.utils.joinFloat32(jspb.utils.split64Low,
363
          jspb.utils.split64High)) {
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
        throw 'fail!';
      }
    }

    // Positive and negative infinity.
    test(Infinity, 0x7f800000);
    test(-Infinity, 0xff800000);

    // Positive and negative zero.
    test(0, 0x00000000);
    test(-0, 0x80000000);

    // Positive and negative epsilon.
    test(f32_eps, 0x00000001);
    test(-f32_eps, 0x80000001);

    // Positive and negative min.
    test(f32_min, 0x00800000);
    test(-f32_min, 0x80800000);

    // Positive and negative max.
    test(f32_max, 0x7F7FFFFF);
    test(-f32_max, 0xFF7FFFFF);

    // Various positive values.
    var cursor = f32_eps * 10;
    while (cursor != Infinity) {
      test(cursor);
      cursor *= 1.1;
    }

    // Various negative values.
    cursor = -f32_eps * 10;
    while (cursor != -Infinity) {
      test(cursor);
      cursor *= 1.1;
    }
  });


  /**
   * Tests conversion from 64-bit floating point numbers to split64 numbers.
   */
  it('testFloat64ToSplit64', function() {
    var f64_eps = jspb.BinaryConstants.FLOAT64_EPS;
    var f64_min = jspb.BinaryConstants.FLOAT64_MIN;
    var f64_max = jspb.BinaryConstants.FLOAT64_MAX;

    // NaN.
    jspb.utils.splitFloat64(NaN);
    if (!isNaN(jspb.utils.joinFloat64(jspb.utils.split64Low,
415
        jspb.utils.split64High))) {
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
      throw 'fail!';
    }

    /**
     * @param {number} x
     * @param {number=} opt_highBits
     * @param {number=} opt_lowBits
     */
    function test(x, opt_highBits, opt_lowBits) {
      jspb.utils.splitFloat64(x);
      if (goog.isDef(opt_highBits)) {
        if (opt_highBits != jspb.utils.split64High) throw 'fail!';
      }
      if (goog.isDef(opt_lowBits)) {
        if (opt_lowBits != jspb.utils.split64Low) throw 'fail!';
      }
      if (x != jspb.utils.joinFloat64(jspb.utils.split64Low,
433
          jspb.utils.split64High)) {
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        throw 'fail!';
      }
    }

    // Positive and negative infinity.
    test(Infinity, 0x7ff00000, 0x00000000);
    test(-Infinity, 0xfff00000, 0x00000000);

    // Positive and negative zero.
    test(0, 0x00000000, 0x00000000);
    test(-0, 0x80000000, 0x00000000);

    // Positive and negative epsilon.
    test(f64_eps, 0x00000000, 0x00000001);
    test(-f64_eps, 0x80000000, 0x00000001);

    // Positive and negative min.
    test(f64_min, 0x00100000, 0x00000000);
    test(-f64_min, 0x80100000, 0x00000000);

    // Positive and negative max.
    test(f64_max, 0x7FEFFFFF, 0xFFFFFFFF);
    test(-f64_max, 0xFFEFFFFF, 0xFFFFFFFF);

    // Various positive values.
    var cursor = f64_eps * 10;
    while (cursor != Infinity) {
      test(cursor);
      cursor *= 1.1;
    }

    // Various negative values.
    cursor = -f64_eps * 10;
    while (cursor != -Infinity) {
      test(cursor);
      cursor *= 1.1;
    }
  });


  /**
   * Tests counting packed varints.
   */
  it('testCountVarints', function() {
478
    var values = [];
479
    for (var i = 1; i < 1000000000; i *= 1.1) {
480
      values.push(Math.floor(i));
481 482
    }

483 484 485
    var writer = new jspb.BinaryWriter();
    writer.writePackedUint64(1, values);

486
    var buffer = new Uint8Array(writer.getResultBuffer());
487 488 489 490 491

    // We should have two more varints than we started with - one for the field
    // tag, one for the packed length.
    assertEquals(values.length + 2,
                 jspb.utils.countVarints(buffer, 0, buffer.length));
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
  });


  /**
   * Tests counting matching varint fields.
   */
  it('testCountVarintFields', function() {
    var writer = new jspb.BinaryWriter();

    var count = 0;
    for (var i = 1; i < 1000000000; i *= 1.1) {
      writer.writeUint64(1, Math.floor(i));
      count++;
    }
    writer.writeString(2, 'terminator');

    var buffer = new Uint8Array(writer.getResultBuffer());
    assertEquals(count,
        jspb.utils.countVarintFields(buffer, 0, buffer.length, 1));

    writer = new jspb.BinaryWriter();

    count = 0;
    for (var i = 1; i < 1000000000; i *= 1.1) {
      writer.writeUint64(123456789, Math.floor(i));
      count++;
    }
    writer.writeString(2, 'terminator');

    buffer = new Uint8Array(writer.getResultBuffer());
    assertEquals(count,
        jspb.utils.countVarintFields(buffer, 0, buffer.length, 123456789));
  });


  /**
   * Tests counting matching fixed32 fields.
   */
  it('testCountFixed32Fields', function() {
    var writer = new jspb.BinaryWriter();

    var count = 0;
    for (var i = 1; i < 1000000000; i *= 1.1) {
      writer.writeFixed32(1, Math.floor(i));
      count++;
    }
    writer.writeString(2, 'terminator');

    var buffer = new Uint8Array(writer.getResultBuffer());
    assertEquals(count,
        jspb.utils.countFixed32Fields(buffer, 0, buffer.length, 1));

    writer = new jspb.BinaryWriter();

    count = 0;
    for (var i = 1; i < 1000000000; i *= 1.1) {
      writer.writeFixed32(123456789, Math.floor(i));
      count++;
    }
    writer.writeString(2, 'terminator');

    buffer = new Uint8Array(writer.getResultBuffer());
    assertEquals(count,
        jspb.utils.countFixed32Fields(buffer, 0, buffer.length, 123456789));
  });


  /**
   * Tests counting matching fixed64 fields.
   */
  it('testCountFixed64Fields', function() {
    var writer = new jspb.BinaryWriter();

    var count = 0;
    for (var i = 1; i < 1000000000; i *= 1.1) {
      writer.writeDouble(1, i);
      count++;
    }
    writer.writeString(2, 'terminator');

    var buffer = new Uint8Array(writer.getResultBuffer());
    assertEquals(count,
        jspb.utils.countFixed64Fields(buffer, 0, buffer.length, 1));

    writer = new jspb.BinaryWriter();

    count = 0;
    for (var i = 1; i < 1000000000; i *= 1.1) {
      writer.writeDouble(123456789, i);
      count++;
    }
    writer.writeString(2, 'terminator');

    buffer = new Uint8Array(writer.getResultBuffer());
    assertEquals(count,
        jspb.utils.countFixed64Fields(buffer, 0, buffer.length, 123456789));
  });


  /**
   * Tests counting matching delimited fields.
   */
  it('testCountDelimitedFields', function() {
    var writer = new jspb.BinaryWriter();

    var count = 0;
    for (var i = 1; i < 1000; i *= 1.1) {
      writer.writeBytes(1, [Math.floor(i)]);
      count++;
    }
    writer.writeString(2, 'terminator');

    var buffer = new Uint8Array(writer.getResultBuffer());
    assertEquals(count,
        jspb.utils.countDelimitedFields(buffer, 0, buffer.length, 1));

    writer = new jspb.BinaryWriter();

    count = 0;
    for (var i = 1; i < 1000; i *= 1.1) {
      writer.writeBytes(123456789, [Math.floor(i)]);
      count++;
    }
    writer.writeString(2, 'terminator');

    buffer = new Uint8Array(writer.getResultBuffer());
    assertEquals(count,
        jspb.utils.countDelimitedFields(buffer, 0, buffer.length, 123456789));
  });


  /**
   * Tests byte format for debug strings.
   */
  it('testDebugBytesToTextFormat', function() {
    assertEquals('""', jspb.utils.debugBytesToTextFormat(null));
    assertEquals('"\\x00\\x10\\xff"',
        jspb.utils.debugBytesToTextFormat([0, 16, 255]));
  });


  /**
   * Tests converting byte blob sources into byte blobs.
   */
  it('testByteSourceToUint8Array', function() {
    var convert = jspb.utils.byteSourceToUint8Array;

    var sourceData = [];
    for (var i = 0; i < 256; i++) {
      sourceData.push(i);
    }

    var sourceBytes = new Uint8Array(sourceData);
    var sourceBuffer = sourceBytes.buffer;
    var sourceBase64 = goog.crypt.base64.encodeByteArray(sourceData);
647
    var sourceString = goog.crypt.byteArrayToString(sourceData);
648 649 650 651 652 653 654 655 656 657 658 659

    function check(result) {
      assertEquals(Uint8Array, result.constructor);
      assertEquals(sourceData.length, result.length);
      for (var i = 0; i < result.length; i++) {
        assertEquals(sourceData[i], result[i]);
      }
    }

    // Converting Uint8Arrays into Uint8Arrays should be a no-op.
    assertEquals(sourceBytes, convert(sourceBytes));

660
    // Converting Array<numbers> into Uint8Arrays should work.
661 662 663 664 665 666 667 668 669
    check(convert(sourceData));

    // Converting ArrayBuffers into Uint8Arrays should work.
    check(convert(sourceBuffer));

    // Converting base64-encoded strings into Uint8Arrays should work.
    check(convert(sourceBase64));
  });
});