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