1b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// Protocol Buffers - Google's data interchange format 2b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// Copyright 2008 Google Inc. All rights reserved. 3b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// https://developers.google.com/protocol-buffers/ 4b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// 5b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// Redistribution and use in source and binary forms, with or without 6b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// modification, are permitted provided that the following conditions are 7b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// met: 8b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// 9b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// * Redistributions of source code must retain the above copyright 10b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// notice, this list of conditions and the following disclaimer. 11b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// * Redistributions in binary form must reproduce the above 12b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// copyright notice, this list of conditions and the following disclaimer 13b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// in the documentation and/or other materials provided with the 14b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// distribution. 15b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// * Neither the name of Google Inc. nor the names of its 16b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// contributors may be used to endorse or promote products derived from 17b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// this software without specific prior written permission. 18b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// 19b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 31b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer/** 32b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @fileoverview Test cases for jspb's helper functions. 33b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * 34b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Test suite is written using Jasmine -- see http://jasmine.github.io/ 35b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * 36b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @author aappleby@google.com (Austin Appleby) 37b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 38b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 39b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammergoog.require('goog.crypt.base64'); 40b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammergoog.require('goog.testing.asserts'); 41b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammergoog.require('jspb.BinaryConstants'); 42b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammergoog.require('jspb.BinaryWriter'); 43b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammergoog.require('jspb.utils'); 44b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 45b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 46b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer/** 47b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @param {number} x 48b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @return {number} 49b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 50b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammerfunction truncate(x) { 51b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var temp = new Float32Array(1); 52b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer temp[0] = x; 53b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer return temp[0]; 54b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer} 55b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 56b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 57b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer/** 58b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Converts an 64-bit integer in split representation to a 64-bit hash string 59b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * (8 bits encoded per character). 60b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @param {number} bitsLow The low 32 bits of the split 64-bit integer. 61b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @param {number} bitsHigh The high 32 bits of the split 64-bit integer. 62b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @return {string} The encoded hash string, 8 bits per character. 63b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 64b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammerfunction toHashString(bitsLow, bitsHigh) { 65b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer return String.fromCharCode((bitsLow >>> 0) & 0xFF, 66b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer (bitsLow >>> 8) & 0xFF, 67b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer (bitsLow >>> 16) & 0xFF, 68b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer (bitsLow >>> 24) & 0xFF, 69b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer (bitsHigh >>> 0) & 0xFF, 70b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer (bitsHigh >>> 8) & 0xFF, 71b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer (bitsHigh >>> 16) & 0xFF, 72b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer (bitsHigh >>> 24) & 0xFF); 73b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer} 74b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 75b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 76b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammerdescribe('binaryUtilsTest', function() { 77b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 78b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests lossless binary-to-decimal conversion. 79b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 80b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testDecimalConversion', function() { 81b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Check some magic numbers. 82b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var result = 83b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.joinUnsignedDecimalString(0x89e80001, 0x8ac72304); 84b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('10000000000000000001', result); 85b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 86b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0xacd05f15, 0x1b69b4b); 87b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('123456789123456789', result); 88b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 89b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0xeb1f0ad2, 0xab54a98c); 90b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('12345678901234567890', result); 91b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 92b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0xe3b70cb1, 0x891087b8); 93b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('9876543210987654321', result); 94b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 95b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Check limits. 96b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00000000); 97b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0', result); 98b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 99b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0xFFFFFFFF, 0xFFFFFFFF); 100b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('18446744073709551615', result); 101b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 102b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Check each bit of the low dword. 103b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 0; i < 32; i++) { 104b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var low = (1 << i) >>> 0; 105b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(low, 0); 106b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('' + Math.pow(2, i), result); 107b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 108b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 109b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Check the first 20 bits of the high dword. 110b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 0; i < 20; i++) { 111b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var high = (1 << i) >>> 0; 112b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0, high); 113b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('' + Math.pow(2, 32 + i), result); 114b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 115b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 116b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // V8's internal double-to-string conversion is inaccurate for values above 117b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // 2^52, even if they're representable integers - check the rest of the bits 118b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // manually against the correct string representations of 2^N. 119b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 120b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00100000); 121b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('4503599627370496', result); 122b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 123b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00200000); 124b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('9007199254740992', result); 125b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 126b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00400000); 127b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('18014398509481984', result); 128b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 129b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x00800000); 130b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('36028797018963968', result); 131b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 132b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x01000000); 133b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('72057594037927936', result); 134b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 135b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x02000000); 136b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('144115188075855872', result); 137b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 138b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x04000000); 139b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('288230376151711744', result); 140b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 141b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x08000000); 142b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('576460752303423488', result); 143b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 144b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x10000000); 145b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('1152921504606846976', result); 146b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 147b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x20000000); 148b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('2305843009213693952', result); 149b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 150b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x40000000); 151b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('4611686018427387904', result); 152b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 153b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.joinUnsignedDecimalString(0x00000000, 0x80000000); 154b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('9223372036854775808', result); 155b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 156b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 157b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 158b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 159b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Going from hash strings to decimal strings should also be lossless. 160b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 161b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testHashToDecimalConversion', function() { 162b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var result; 163b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var convert = jspb.utils.hash64ToDecimalString; 164b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 165b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0x00000000, 0x00000000), false); 166b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0', result); 167b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 168b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0x00000000, 0x00000000), true); 169b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0', result); 170b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 171b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF), false); 172b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('18446744073709551615', result); 173b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 174b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF), true); 175b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('-1', result); 176b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 177b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0x00000000, 0x80000000), false); 178b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('9223372036854775808', result); 179b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 180b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0x00000000, 0x80000000), true); 181b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('-9223372036854775808', result); 182b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 183b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0xacd05f15, 0x01b69b4b), false); 184b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('123456789123456789', result); 185b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 186b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(~0xacd05f15 + 1, ~0x01b69b4b), true); 187b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('-123456789123456789', result); 188b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 189b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // And converting arrays of hashes should work the same way. 190b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = jspb.utils.hash64ArrayToDecimalStrings([ 191b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer toHashString(0xFFFFFFFF, 0xFFFFFFFF), 192b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer toHashString(0x00000000, 0x80000000), 193b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer toHashString(0xacd05f15, 0x01b69b4b)], false); 194b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(3, result.length); 195b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('18446744073709551615', result[0]); 196b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('9223372036854775808', result[1]); 197b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('123456789123456789', result[2]); 198b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 199b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 200b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /* 201b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Going from decimal strings to hash strings should be lossless. 202b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 203b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testDecimalToHashConversion', function() { 204b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var result; 205b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var convert = jspb.utils.decimalStringToHash64; 206b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 207b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('0'); 208b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 209b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result); 210b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 211b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('-1'); 212b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 213b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result); 214b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 215b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('18446744073709551615'); 216b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 217b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result); 218b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 219b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('9223372036854775808'); 220b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 221b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result); 222b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 223b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('-9223372036854775808'); 224b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 225b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result); 226b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 227b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('123456789123456789'); 228b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 229b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0x15, 0x5F, 0xD0, 0xAC, 0x4B, 0x9B, 0xB6, 0x01]), result); 230b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 231b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('-123456789123456789'); 232b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 233b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0xEB, 0xA0, 0x2F, 0x53, 0xB4, 0x64, 0x49, 0xFE]), result); 234b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 235b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 236b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 237b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Going from hash strings to hex strings should be lossless. 238b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 239b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testHashToHexConversion', function() { 240b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var result; 241b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var convert = jspb.utils.hash64ToHexString; 242b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 243b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0x00000000, 0x00000000)); 244b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0x0000000000000000', result); 245b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 246b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0xFFFFFFFF, 0xFFFFFFFF)); 247b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0xffffffffffffffff', result); 248b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 249b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(toHashString(0x12345678, 0x9ABCDEF0)); 250b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0x9abcdef012345678', result); 251b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 252b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 253b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 254b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 255b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Going from hex strings to hash strings should be lossless. 256b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 257b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testHexToHashConversion', function() { 258b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var result; 259b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var convert = jspb.utils.hexStringToHash64; 260b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 261b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('0x0000000000000000'); 262b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 263b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result); 264b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 265b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('0xffffffffffffffff'); 266b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 267b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result); 268b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 269b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Hex string is big-endian, hash string is little-endian. 270b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('0x123456789ABCDEF0'); 271b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 272b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]), result); 273b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 274b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Capitalization should not matter. 275b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert('0x0000abcdefABCDEF'); 276b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(String.fromCharCode.apply(null, 277b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer [0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB, 0x00, 0x00]), result); 278b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 279b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 280b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 281b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 282b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Going from numbers to hash strings should be lossless for up to 53 bits of 283b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * precision. 284b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 285b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testNumberToHashConversion', function() { 286b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var result; 287b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var convert = jspb.utils.numberToHash64; 288b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 289b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(0x0000000000000); 290b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0x0000000000000000', jspb.utils.hash64ToHexString(result)); 291b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 292b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(0xFFFFFFFFFFFFF); 293b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0x000fffffffffffff', jspb.utils.hash64ToHexString(result)); 294b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 295b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(0x123456789ABCD); 296b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0x000123456789abcd', jspb.utils.hash64ToHexString(result)); 297b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 298b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(0xDCBA987654321); 299b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0x000dcba987654321', jspb.utils.hash64ToHexString(result)); 300b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 301b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // 53 bits of precision should not be truncated. 302b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(0x10000000000001); 303b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('0x0010000000000001', jspb.utils.hash64ToHexString(result)); 304b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 305b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // 54 bits of precision should be truncated. 306b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer result = convert(0x20000000000001); 307b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertNotEquals( 308b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer '0x0020000000000001', jspb.utils.hash64ToHexString(result)); 309b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 310b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 311b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 312b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 313b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Sanity check the behavior of Javascript's strings when doing funny things 314b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * with unicode characters. 315b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 316b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('sanityCheckUnicodeStrings', function() { 317b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var strings = new Array(65536); 318b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 319b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // All possible unsigned 16-bit values should be storable in a string, they 320b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // shouldn't do weird things with the length of the string, and they should 321b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // come back out of the string unchanged. 322b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 0; i < 65536; i++) { 323b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer strings[i] = 'a' + String.fromCharCode(i) + 'a'; 324b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (3 != strings[i].length) throw 'fail!'; 325b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (i != strings[i].charCodeAt(1)) throw 'fail!'; 326b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 327b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 328b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Each unicode character should compare equal to itself and not equal to a 329b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // different unicode character. 330b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 0; i < 65536; i++) { 331b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (strings[i] != strings[i]) throw 'fail!'; 332b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (strings[i] == strings[(i + 1) % 65536]) throw 'fail!'; 333b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 334b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 335b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 336b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 337b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 338b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests conversion from 32-bit floating point numbers to split64 numbers. 339b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 340b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testFloat32ToSplit64', function() { 341b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var f32_eps = jspb.BinaryConstants.FLOAT32_EPS; 342b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var f32_min = jspb.BinaryConstants.FLOAT32_MIN; 343b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var f32_max = jspb.BinaryConstants.FLOAT32_MAX; 344b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 345b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // NaN. 346b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.splitFloat32(NaN); 347b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (!isNaN(jspb.utils.joinFloat32(jspb.utils.split64Low, 348b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.split64High))) { 349b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer throw 'fail!'; 350b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 351b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 352b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 353b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @param {number} x 354b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @param {number=} opt_bits 355b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 356b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer function test(x, opt_bits) { 357b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.splitFloat32(x); 358b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (goog.isDef(opt_bits)) { 359b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (opt_bits != jspb.utils.split64Low) throw 'fail!'; 360b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 361b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (truncate(x) != jspb.utils.joinFloat32(jspb.utils.split64Low, 362b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.split64High)) { 363b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer throw 'fail!'; 364b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 365b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 366b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 367b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative infinity. 368b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(Infinity, 0x7f800000); 369b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-Infinity, 0xff800000); 370b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 371b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative zero. 372b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(0, 0x00000000); 373b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-0, 0x80000000); 374b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 375b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative epsilon. 376b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(f32_eps, 0x00000001); 377b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-f32_eps, 0x80000001); 378b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 379b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative min. 380b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(f32_min, 0x00800000); 381b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-f32_min, 0x80800000); 382b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 383b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative max. 384b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(f32_max, 0x7F7FFFFF); 385b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-f32_max, 0xFF7FFFFF); 386b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 387b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Various positive values. 388b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var cursor = f32_eps * 10; 389b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer while (cursor != Infinity) { 390b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(cursor); 391b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer cursor *= 1.1; 392b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 393b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 394b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Various negative values. 395b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer cursor = -f32_eps * 10; 396b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer while (cursor != -Infinity) { 397b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(cursor); 398b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer cursor *= 1.1; 399b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 400b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 401b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 402b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 403b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 404b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests conversion from 64-bit floating point numbers to split64 numbers. 405b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 406b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testFloat64ToSplit64', function() { 407b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var f64_eps = jspb.BinaryConstants.FLOAT64_EPS; 408b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var f64_min = jspb.BinaryConstants.FLOAT64_MIN; 409b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var f64_max = jspb.BinaryConstants.FLOAT64_MAX; 410b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 411b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // NaN. 412b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.splitFloat64(NaN); 413b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (!isNaN(jspb.utils.joinFloat64(jspb.utils.split64Low, 414b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.split64High))) { 415b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer throw 'fail!'; 416b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 417b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 418b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 419b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @param {number} x 420b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @param {number=} opt_highBits 421b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * @param {number=} opt_lowBits 422b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 423b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer function test(x, opt_highBits, opt_lowBits) { 424b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.splitFloat64(x); 425b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (goog.isDef(opt_highBits)) { 426b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (opt_highBits != jspb.utils.split64High) throw 'fail!'; 427b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 428b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (goog.isDef(opt_lowBits)) { 429b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (opt_lowBits != jspb.utils.split64Low) throw 'fail!'; 430b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 431b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer if (x != jspb.utils.joinFloat64(jspb.utils.split64Low, 432b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.split64High)) { 433b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer throw 'fail!'; 434b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 435b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 436b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 437b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative infinity. 438b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(Infinity, 0x7ff00000, 0x00000000); 439b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-Infinity, 0xfff00000, 0x00000000); 440b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 441b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative zero. 442b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(0, 0x00000000, 0x00000000); 443b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-0, 0x80000000, 0x00000000); 444b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 445b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative epsilon. 446b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(f64_eps, 0x00000000, 0x00000001); 447b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-f64_eps, 0x80000000, 0x00000001); 448b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 449b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative min. 450b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(f64_min, 0x00100000, 0x00000000); 451b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-f64_min, 0x80100000, 0x00000000); 452b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 453b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Positive and negative max. 454b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(f64_max, 0x7FEFFFFF, 0xFFFFFFFF); 455b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(-f64_max, 0xFFEFFFFF, 0xFFFFFFFF); 456b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 457b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Various positive values. 458b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var cursor = f64_eps * 10; 459b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer while (cursor != Infinity) { 460b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(cursor); 461b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer cursor *= 1.1; 462b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 463b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 464b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Various negative values. 465b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer cursor = -f64_eps * 10; 466b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer while (cursor != -Infinity) { 467b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer test(cursor); 468b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer cursor *= 1.1; 469b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 470b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 471b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 472b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 473b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 474b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests counting packed varints. 475b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 476b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testCountVarints', function() { 477b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var values = []; 478b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000000000; i *= 1.1) { 479b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer values.push(Math.floor(i)); 480b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 481b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 482b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var writer = new jspb.BinaryWriter(); 483b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writePackedUint64(1, values); 484b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 485b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var buffer = new Uint8Array(writer.getResultBuffer()); 486b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 487b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // We should have two more varints than we started with - one for the field 488b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // tag, one for the packed length. 489b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(values.length + 2, 490b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countVarints(buffer, 0, buffer.length)); 491b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 492b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 493b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 494b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 495b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests counting matching varint fields. 496b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 497b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testCountVarintFields', function() { 498b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var writer = new jspb.BinaryWriter(); 499b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 500b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var count = 0; 501b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000000000; i *= 1.1) { 502b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeUint64(1, Math.floor(i)); 503b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count++; 504b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 505b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeString(2, 'terminator'); 506b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 507b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var buffer = new Uint8Array(writer.getResultBuffer()); 508b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(count, 509b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countVarintFields(buffer, 0, buffer.length, 1)); 510b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 511b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer = new jspb.BinaryWriter(); 512b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 513b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count = 0; 514b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000000000; i *= 1.1) { 515b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeUint64(123456789, Math.floor(i)); 516b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count++; 517b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 518b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeString(2, 'terminator'); 519b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 520b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer buffer = new Uint8Array(writer.getResultBuffer()); 521b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(count, 522b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countVarintFields(buffer, 0, buffer.length, 123456789)); 523b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 524b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 525b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 526b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 527b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests counting matching fixed32 fields. 528b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 529b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testCountFixed32Fields', function() { 530b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var writer = new jspb.BinaryWriter(); 531b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 532b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var count = 0; 533b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000000000; i *= 1.1) { 534b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeFixed32(1, Math.floor(i)); 535b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count++; 536b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 537b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeString(2, 'terminator'); 538b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 539b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var buffer = new Uint8Array(writer.getResultBuffer()); 540b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(count, 541b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countFixed32Fields(buffer, 0, buffer.length, 1)); 542b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 543b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer = new jspb.BinaryWriter(); 544b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 545b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count = 0; 546b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000000000; i *= 1.1) { 547b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeFixed32(123456789, Math.floor(i)); 548b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count++; 549b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 550b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeString(2, 'terminator'); 551b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 552b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer buffer = new Uint8Array(writer.getResultBuffer()); 553b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(count, 554b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countFixed32Fields(buffer, 0, buffer.length, 123456789)); 555b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 556b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 557b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 558b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 559b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests counting matching fixed64 fields. 560b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 561b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testCountFixed64Fields', function() { 562b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var writer = new jspb.BinaryWriter(); 563b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 564b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var count = 0; 565b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000000000; i *= 1.1) { 566b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeDouble(1, i); 567b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count++; 568b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 569b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeString(2, 'terminator'); 570b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 571b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var buffer = new Uint8Array(writer.getResultBuffer()); 572b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(count, 573b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countFixed64Fields(buffer, 0, buffer.length, 1)); 574b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 575b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer = new jspb.BinaryWriter(); 576b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 577b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count = 0; 578b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000000000; i *= 1.1) { 579b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeDouble(123456789, i); 580b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count++; 581b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 582b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeString(2, 'terminator'); 583b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 584b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer buffer = new Uint8Array(writer.getResultBuffer()); 585b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(count, 586b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countFixed64Fields(buffer, 0, buffer.length, 123456789)); 587b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 588b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 589b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 590b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 591b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests counting matching delimited fields. 592b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 593b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testCountDelimitedFields', function() { 594b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var writer = new jspb.BinaryWriter(); 595b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 596b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var count = 0; 597b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000; i *= 1.1) { 598b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeBytes(1, [Math.floor(i)]); 599b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count++; 600b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 601b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeString(2, 'terminator'); 602b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 603b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var buffer = new Uint8Array(writer.getResultBuffer()); 604b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(count, 605b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countDelimitedFields(buffer, 0, buffer.length, 1)); 606b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 607b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer = new jspb.BinaryWriter(); 608b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 609b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count = 0; 610b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 1; i < 1000; i *= 1.1) { 611b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeBytes(123456789, [Math.floor(i)]); 612b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer count++; 613b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 614b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer writer.writeString(2, 'terminator'); 615b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 616b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer buffer = new Uint8Array(writer.getResultBuffer()); 617b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(count, 618b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.countDelimitedFields(buffer, 0, buffer.length, 123456789)); 619b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 620b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 621b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 622b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 623b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests byte format for debug strings. 624b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 625b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testDebugBytesToTextFormat', function() { 626b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('""', jspb.utils.debugBytesToTextFormat(null)); 627b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals('"\\x00\\x10\\xff"', 628b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer jspb.utils.debugBytesToTextFormat([0, 16, 255])); 629b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 630b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 631b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 632b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer /** 633b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer * Tests converting byte blob sources into byte blobs. 634b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer */ 635b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer it('testByteSourceToUint8Array', function() { 636b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var convert = jspb.utils.byteSourceToUint8Array; 637b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 638b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var sourceData = []; 639b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 0; i < 256; i++) { 640b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer sourceData.push(i); 641b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 642b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 643b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var sourceBytes = new Uint8Array(sourceData); 644b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var sourceBuffer = sourceBytes.buffer; 645b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var sourceBase64 = goog.crypt.base64.encodeByteArray(sourceData); 646b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer var sourceString = String.fromCharCode.apply(null, sourceData); 647b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 648b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer function check(result) { 649b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(Uint8Array, result.constructor); 650b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(sourceData.length, result.length); 651b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer for (var i = 0; i < result.length; i++) { 652b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(sourceData[i], result[i]); 653b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 654b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer } 655b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 656b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Converting Uint8Arrays into Uint8Arrays should be a no-op. 657b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer assertEquals(sourceBytes, convert(sourceBytes)); 658b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 659b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Converting Array.<numbers> into Uint8Arrays should work. 660b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer check(convert(sourceData)); 661b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 662b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Converting ArrayBuffers into Uint8Arrays should work. 663b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer check(convert(sourceBuffer)); 664b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer 665b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer // Converting base64-encoded strings into Uint8Arrays should work. 666b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer check(convert(sourceBase64)); 667b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer }); 668b0575e93e4c39dec69365b850088a1eb7f82c5b3Tamas Berghammer}); 669