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 binary protocol buffer writer. In
33ffe3c632Sopenharmony_ci * practice BinaryWriter is used to drive the Decoder and Reader test cases,
34ffe3c632Sopenharmony_ci * so only writer-specific tests are here.
35ffe3c632Sopenharmony_ci *
36ffe3c632Sopenharmony_ci * Test suite is written using Jasmine -- see http://jasmine.github.io/
37ffe3c632Sopenharmony_ci *
38ffe3c632Sopenharmony_ci * @author aappleby@google.com (Austin Appleby)
39ffe3c632Sopenharmony_ci */
40ffe3c632Sopenharmony_ci
41ffe3c632Sopenharmony_cigoog.require('goog.crypt');
42ffe3c632Sopenharmony_cigoog.require('goog.testing.asserts');
43ffe3c632Sopenharmony_cigoog.require('jspb.BinaryConstants');
44ffe3c632Sopenharmony_cigoog.require('jspb.BinaryReader');
45ffe3c632Sopenharmony_cigoog.require('jspb.BinaryWriter');
46ffe3c632Sopenharmony_cigoog.require('jspb.utils');
47ffe3c632Sopenharmony_ci
48ffe3c632Sopenharmony_ci
49ffe3c632Sopenharmony_ci/**
50ffe3c632Sopenharmony_ci * @param {function()} func This function should throw an error when run.
51ffe3c632Sopenharmony_ci */
52ffe3c632Sopenharmony_cifunction assertFails(func) {
53ffe3c632Sopenharmony_ci  assertThrows(func);
54ffe3c632Sopenharmony_ci}
55ffe3c632Sopenharmony_ci
56ffe3c632Sopenharmony_ci
57ffe3c632Sopenharmony_cidescribe('binaryWriterTest', function() {
58ffe3c632Sopenharmony_ci  /**
59ffe3c632Sopenharmony_ci   * Verifies that misuse of the writer class triggers assertions.
60ffe3c632Sopenharmony_ci   */
61ffe3c632Sopenharmony_ci  it('testWriteErrors', function() {
62ffe3c632Sopenharmony_ci    // Submessages with invalid field indices should assert.
63ffe3c632Sopenharmony_ci    var writer = new jspb.BinaryWriter();
64ffe3c632Sopenharmony_ci    var dummyMessage = /** @type {!jspb.BinaryMessage} */({});
65ffe3c632Sopenharmony_ci
66ffe3c632Sopenharmony_ci    assertFails(function() {
67ffe3c632Sopenharmony_ci      writer.writeMessage(-1, dummyMessage, goog.nullFunction);
68ffe3c632Sopenharmony_ci    });
69ffe3c632Sopenharmony_ci
70ffe3c632Sopenharmony_ci    // Writing invalid field indices should assert.
71ffe3c632Sopenharmony_ci    writer = new jspb.BinaryWriter();
72ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeUint64(-1, 1);});
73ffe3c632Sopenharmony_ci
74ffe3c632Sopenharmony_ci    // Writing out-of-range field values should assert.
75ffe3c632Sopenharmony_ci    writer = new jspb.BinaryWriter();
76ffe3c632Sopenharmony_ci
77ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeInt32(1, -Infinity);});
78ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeInt32(1, Infinity);});
79ffe3c632Sopenharmony_ci
80ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeInt64(1, -Infinity);});
81ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeInt64(1, Infinity);});
82ffe3c632Sopenharmony_ci
83ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeUint32(1, -1);});
84ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeUint32(1, Infinity);});
85ffe3c632Sopenharmony_ci
86ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeUint64(1, -1);});
87ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeUint64(1, Infinity);});
88ffe3c632Sopenharmony_ci
89ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeSint32(1, -Infinity);});
90ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeSint32(1, Infinity);});
91ffe3c632Sopenharmony_ci
92ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeSint64(1, -Infinity);});
93ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeSint64(1, Infinity);});
94ffe3c632Sopenharmony_ci
95ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeFixed32(1, -1);});
96ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeFixed32(1, Infinity);});
97ffe3c632Sopenharmony_ci
98ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeFixed64(1, -1);});
99ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeFixed64(1, Infinity);});
100ffe3c632Sopenharmony_ci
101ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeSfixed32(1, -Infinity);});
102ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeSfixed32(1, Infinity);});
103ffe3c632Sopenharmony_ci
104ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeSfixed64(1, -Infinity);});
105ffe3c632Sopenharmony_ci    assertFails(function() {writer.writeSfixed64(1, Infinity);});
106ffe3c632Sopenharmony_ci  });
107ffe3c632Sopenharmony_ci
108ffe3c632Sopenharmony_ci
109ffe3c632Sopenharmony_ci  /**
110ffe3c632Sopenharmony_ci   * Basic test of retrieving the result as a Uint8Array buffer
111ffe3c632Sopenharmony_ci   */
112ffe3c632Sopenharmony_ci  it('testGetResultBuffer', function() {
113ffe3c632Sopenharmony_ci    var expected = '0864120b48656c6c6f20776f726c641a0301020320c801';
114ffe3c632Sopenharmony_ci
115ffe3c632Sopenharmony_ci    var writer = new jspb.BinaryWriter();
116ffe3c632Sopenharmony_ci    writer.writeUint32(1, 100);
117ffe3c632Sopenharmony_ci    writer.writeString(2, 'Hello world');
118ffe3c632Sopenharmony_ci    writer.writeBytes(3, new Uint8Array([1, 2, 3]));
119ffe3c632Sopenharmony_ci    writer.writeUint32(4, 200);
120ffe3c632Sopenharmony_ci
121ffe3c632Sopenharmony_ci    var buffer = writer.getResultBuffer();
122ffe3c632Sopenharmony_ci    assertEquals(expected, goog.crypt.byteArrayToHex(buffer));
123ffe3c632Sopenharmony_ci  });
124ffe3c632Sopenharmony_ci
125ffe3c632Sopenharmony_ci
126ffe3c632Sopenharmony_ci  /**
127ffe3c632Sopenharmony_ci   * Tests websafe encodings for base64 strings.
128ffe3c632Sopenharmony_ci   */
129ffe3c632Sopenharmony_ci  it('testWebSafeOption', function() {
130ffe3c632Sopenharmony_ci    var writer = new jspb.BinaryWriter();
131ffe3c632Sopenharmony_ci    writer.writeBytes(1, new Uint8Array([127]));
132ffe3c632Sopenharmony_ci    assertEquals('CgF/', writer.getResultBase64String());
133ffe3c632Sopenharmony_ci    assertEquals(
134ffe3c632Sopenharmony_ci        'CgF/',
135ffe3c632Sopenharmony_ci        writer.getResultBase64String(goog.crypt.base64.Alphabet.DEFAULT));
136ffe3c632Sopenharmony_ci    assertEquals(
137ffe3c632Sopenharmony_ci        'CgF_',
138ffe3c632Sopenharmony_ci        writer.getResultBase64String(
139ffe3c632Sopenharmony_ci            goog.crypt.base64.Alphabet.WEBSAFE_NO_PADDING));
140ffe3c632Sopenharmony_ci  });
141ffe3c632Sopenharmony_ci
142ffe3c632Sopenharmony_ci  it('writes split 64 fields', function() {
143ffe3c632Sopenharmony_ci    var writer = new jspb.BinaryWriter();
144ffe3c632Sopenharmony_ci    writer.writeSplitVarint64(1, 0x1, 0x2);
145ffe3c632Sopenharmony_ci    writer.writeSplitVarint64(1, 0xFFFFFFFF, 0xFFFFFFFF);
146ffe3c632Sopenharmony_ci    writer.writeSplitFixed64(2, 0x1, 0x2);
147ffe3c632Sopenharmony_ci    writer.writeSplitFixed64(2, 0xFFFFFFF0, 0xFFFFFFFF);
148ffe3c632Sopenharmony_ci    function lo(i) {
149ffe3c632Sopenharmony_ci      return i + 1;
150ffe3c632Sopenharmony_ci    }
151ffe3c632Sopenharmony_ci    function hi(i) {
152ffe3c632Sopenharmony_ci      return i + 2;
153ffe3c632Sopenharmony_ci    }
154ffe3c632Sopenharmony_ci    writer.writeRepeatedSplitVarint64(3, [0, 1, 2], lo, hi);
155ffe3c632Sopenharmony_ci    writer.writeRepeatedSplitFixed64(4, [0, 1, 2], lo, hi);
156ffe3c632Sopenharmony_ci    writer.writePackedSplitVarint64(5, [0, 1, 2], lo, hi);
157ffe3c632Sopenharmony_ci    writer.writePackedSplitFixed64(6, [0, 1, 2], lo, hi);
158ffe3c632Sopenharmony_ci
159ffe3c632Sopenharmony_ci    function bitsAsArray(lowBits, highBits) {
160ffe3c632Sopenharmony_ci      return [lowBits >>> 0, highBits >>> 0];
161ffe3c632Sopenharmony_ci    }
162ffe3c632Sopenharmony_ci    var reader = jspb.BinaryReader.alloc(writer.getResultBuffer());
163ffe3c632Sopenharmony_ci    reader.nextField();
164ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(1);
165ffe3c632Sopenharmony_ci    expect(reader.readSplitVarint64(bitsAsArray)).toEqual([0x1, 0x2]);
166ffe3c632Sopenharmony_ci
167ffe3c632Sopenharmony_ci    reader.nextField();
168ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(1);
169ffe3c632Sopenharmony_ci    expect(reader.readSplitVarint64(bitsAsArray)).toEqual([
170ffe3c632Sopenharmony_ci      0xFFFFFFFF, 0xFFFFFFFF
171ffe3c632Sopenharmony_ci    ]);
172ffe3c632Sopenharmony_ci
173ffe3c632Sopenharmony_ci    reader.nextField();
174ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(2);
175ffe3c632Sopenharmony_ci    expect(reader.readSplitFixed64(bitsAsArray)).toEqual([0x1, 0x2]);
176ffe3c632Sopenharmony_ci
177ffe3c632Sopenharmony_ci    reader.nextField();
178ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(2);
179ffe3c632Sopenharmony_ci    expect(reader.readSplitFixed64(bitsAsArray)).toEqual([
180ffe3c632Sopenharmony_ci      0xFFFFFFF0, 0xFFFFFFFF
181ffe3c632Sopenharmony_ci    ]);
182ffe3c632Sopenharmony_ci
183ffe3c632Sopenharmony_ci    for (let i = 0; i < 3; i++) {
184ffe3c632Sopenharmony_ci      reader.nextField();
185ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(3);
186ffe3c632Sopenharmony_ci      expect(reader.readSplitVarint64(bitsAsArray)).toEqual([i + 1, i + 2]);
187ffe3c632Sopenharmony_ci    }
188ffe3c632Sopenharmony_ci
189ffe3c632Sopenharmony_ci    for (let i = 0; i < 3; i++) {
190ffe3c632Sopenharmony_ci      reader.nextField();
191ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(4);
192ffe3c632Sopenharmony_ci      expect(reader.readSplitFixed64(bitsAsArray)).toEqual([i + 1, i + 2]);
193ffe3c632Sopenharmony_ci    }
194ffe3c632Sopenharmony_ci
195ffe3c632Sopenharmony_ci    reader.nextField();
196ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(5);
197ffe3c632Sopenharmony_ci    expect(reader.readPackedInt64String()).toEqual([
198ffe3c632Sopenharmony_ci      String(2 * 2 ** 32 + 1),
199ffe3c632Sopenharmony_ci      String(3 * 2 ** 32 + 2),
200ffe3c632Sopenharmony_ci      String(4 * 2 ** 32 + 3),
201ffe3c632Sopenharmony_ci    ]);
202ffe3c632Sopenharmony_ci
203ffe3c632Sopenharmony_ci    reader.nextField();
204ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(6);
205ffe3c632Sopenharmony_ci    expect(reader.readPackedFixed64String()).toEqual([
206ffe3c632Sopenharmony_ci      String(2 * 2 ** 32 + 1),
207ffe3c632Sopenharmony_ci      String(3 * 2 ** 32 + 2),
208ffe3c632Sopenharmony_ci      String(4 * 2 ** 32 + 3),
209ffe3c632Sopenharmony_ci    ]);
210ffe3c632Sopenharmony_ci  });
211ffe3c632Sopenharmony_ci
212ffe3c632Sopenharmony_ci  it('writes zigzag 64 fields', function() {
213ffe3c632Sopenharmony_ci    // Test cases directly from the protobuf dev guide.
214ffe3c632Sopenharmony_ci    // https://engdoc.corp.google.com/eng/howto/protocolbuffers/developerguide/encoding.shtml?cl=head#types
215ffe3c632Sopenharmony_ci    var testCases = [
216ffe3c632Sopenharmony_ci      {original: '0', zigzag: '0'},
217ffe3c632Sopenharmony_ci      {original: '-1', zigzag: '1'},
218ffe3c632Sopenharmony_ci      {original: '1', zigzag: '2'},
219ffe3c632Sopenharmony_ci      {original: '-2', zigzag: '3'},
220ffe3c632Sopenharmony_ci      {original: '2147483647', zigzag: '4294967294'},
221ffe3c632Sopenharmony_ci      {original: '-2147483648', zigzag: '4294967295'},
222ffe3c632Sopenharmony_ci      // 64-bit extremes, not in dev guide.
223ffe3c632Sopenharmony_ci      {original: '9223372036854775807', zigzag: '18446744073709551614'},
224ffe3c632Sopenharmony_ci      {original: '-9223372036854775808', zigzag: '18446744073709551615'},
225ffe3c632Sopenharmony_ci    ];
226ffe3c632Sopenharmony_ci    function decimalToLowBits(v) {
227ffe3c632Sopenharmony_ci      jspb.utils.splitDecimalString(v);
228ffe3c632Sopenharmony_ci      return jspb.utils.split64Low >>> 0;
229ffe3c632Sopenharmony_ci    }
230ffe3c632Sopenharmony_ci    function decimalToHighBits(v) {
231ffe3c632Sopenharmony_ci      jspb.utils.splitDecimalString(v);
232ffe3c632Sopenharmony_ci      return jspb.utils.split64High >>> 0;
233ffe3c632Sopenharmony_ci    }
234ffe3c632Sopenharmony_ci
235ffe3c632Sopenharmony_ci    var writer = new jspb.BinaryWriter();
236ffe3c632Sopenharmony_ci    testCases.forEach(function(c) {
237ffe3c632Sopenharmony_ci      writer.writeSint64String(1, c.original);
238ffe3c632Sopenharmony_ci      writer.writeSintHash64(1, jspb.utils.decimalStringToHash64(c.original));
239ffe3c632Sopenharmony_ci      jspb.utils.splitDecimalString(c.original);
240ffe3c632Sopenharmony_ci      writer.writeSplitZigzagVarint64(
241ffe3c632Sopenharmony_ci          1, jspb.utils.split64Low, jspb.utils.split64High);
242ffe3c632Sopenharmony_ci    });
243ffe3c632Sopenharmony_ci
244ffe3c632Sopenharmony_ci    writer.writeRepeatedSint64String(2, testCases.map(function(c) {
245ffe3c632Sopenharmony_ci      return c.original;
246ffe3c632Sopenharmony_ci    }));
247ffe3c632Sopenharmony_ci
248ffe3c632Sopenharmony_ci    writer.writeRepeatedSintHash64(3, testCases.map(function(c) {
249ffe3c632Sopenharmony_ci      return jspb.utils.decimalStringToHash64(c.original);
250ffe3c632Sopenharmony_ci    }));
251ffe3c632Sopenharmony_ci
252ffe3c632Sopenharmony_ci    writer.writeRepeatedSplitZigzagVarint64(
253ffe3c632Sopenharmony_ci        4, testCases.map(function(c) {
254ffe3c632Sopenharmony_ci          return c.original;
255ffe3c632Sopenharmony_ci        }),
256ffe3c632Sopenharmony_ci        decimalToLowBits, decimalToHighBits);
257ffe3c632Sopenharmony_ci
258ffe3c632Sopenharmony_ci    writer.writePackedSint64String(5, testCases.map(function(c) {
259ffe3c632Sopenharmony_ci      return c.original;
260ffe3c632Sopenharmony_ci    }));
261ffe3c632Sopenharmony_ci
262ffe3c632Sopenharmony_ci    writer.writePackedSintHash64(6, testCases.map(function(c) {
263ffe3c632Sopenharmony_ci      return jspb.utils.decimalStringToHash64(c.original);
264ffe3c632Sopenharmony_ci    }));
265ffe3c632Sopenharmony_ci
266ffe3c632Sopenharmony_ci    writer.writePackedSplitZigzagVarint64(
267ffe3c632Sopenharmony_ci        7, testCases.map(function(c) {
268ffe3c632Sopenharmony_ci          return c.original;
269ffe3c632Sopenharmony_ci        }),
270ffe3c632Sopenharmony_ci        decimalToLowBits, decimalToHighBits);
271ffe3c632Sopenharmony_ci
272ffe3c632Sopenharmony_ci    // Verify by reading the stream as normal int64 fields and checking with
273ffe3c632Sopenharmony_ci    // the canonical zigzag encoding of each value.
274ffe3c632Sopenharmony_ci    var reader = jspb.BinaryReader.alloc(writer.getResultBuffer());
275ffe3c632Sopenharmony_ci    testCases.forEach(function(c) {
276ffe3c632Sopenharmony_ci      reader.nextField();
277ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(1);
278ffe3c632Sopenharmony_ci      expect(reader.readUint64String()).toEqual(c.zigzag);
279ffe3c632Sopenharmony_ci      reader.nextField();
280ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(1);
281ffe3c632Sopenharmony_ci      expect(reader.readUint64String()).toEqual(c.zigzag);
282ffe3c632Sopenharmony_ci      reader.nextField();
283ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(1);
284ffe3c632Sopenharmony_ci      expect(reader.readUint64String()).toEqual(c.zigzag);
285ffe3c632Sopenharmony_ci    });
286ffe3c632Sopenharmony_ci
287ffe3c632Sopenharmony_ci    testCases.forEach(function(c) {
288ffe3c632Sopenharmony_ci      reader.nextField();
289ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(2);
290ffe3c632Sopenharmony_ci      expect(reader.readUint64String()).toEqual(c.zigzag);
291ffe3c632Sopenharmony_ci    });
292ffe3c632Sopenharmony_ci
293ffe3c632Sopenharmony_ci    testCases.forEach(function(c) {
294ffe3c632Sopenharmony_ci      reader.nextField();
295ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(3);
296ffe3c632Sopenharmony_ci      expect(reader.readUint64String()).toEqual(c.zigzag);
297ffe3c632Sopenharmony_ci    });
298ffe3c632Sopenharmony_ci
299ffe3c632Sopenharmony_ci    testCases.forEach(function(c) {
300ffe3c632Sopenharmony_ci      reader.nextField();
301ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(4);
302ffe3c632Sopenharmony_ci      expect(reader.readUint64String()).toEqual(c.zigzag);
303ffe3c632Sopenharmony_ci    });
304ffe3c632Sopenharmony_ci
305ffe3c632Sopenharmony_ci    reader.nextField();
306ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(5);
307ffe3c632Sopenharmony_ci    expect(reader.readPackedUint64String()).toEqual(testCases.map(function(c) {
308ffe3c632Sopenharmony_ci      return c.zigzag;
309ffe3c632Sopenharmony_ci    }));
310ffe3c632Sopenharmony_ci
311ffe3c632Sopenharmony_ci    reader.nextField();
312ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(6);
313ffe3c632Sopenharmony_ci    expect(reader.readPackedUint64String()).toEqual(testCases.map(function(c) {
314ffe3c632Sopenharmony_ci      return c.zigzag;
315ffe3c632Sopenharmony_ci    }));
316ffe3c632Sopenharmony_ci
317ffe3c632Sopenharmony_ci    reader.nextField();
318ffe3c632Sopenharmony_ci    expect(reader.getFieldNumber()).toEqual(7);
319ffe3c632Sopenharmony_ci    expect(reader.readPackedUint64String()).toEqual(testCases.map(function(c) {
320ffe3c632Sopenharmony_ci      return c.zigzag;
321ffe3c632Sopenharmony_ci    }));
322ffe3c632Sopenharmony_ci  });
323ffe3c632Sopenharmony_ci
324ffe3c632Sopenharmony_ci  it('writes float32 fields', function() {
325ffe3c632Sopenharmony_ci    var testCases = [
326ffe3c632Sopenharmony_ci      0, 1, -1, jspb.BinaryConstants.FLOAT32_MIN,
327ffe3c632Sopenharmony_ci      -jspb.BinaryConstants.FLOAT32_MIN, jspb.BinaryConstants.FLOAT32_MAX,
328ffe3c632Sopenharmony_ci      -jspb.BinaryConstants.FLOAT32_MAX, 3.1415927410125732, Infinity,
329ffe3c632Sopenharmony_ci      -Infinity, NaN
330ffe3c632Sopenharmony_ci    ];
331ffe3c632Sopenharmony_ci    var writer = new jspb.BinaryWriter();
332ffe3c632Sopenharmony_ci    testCases.forEach(function(f) {
333ffe3c632Sopenharmony_ci      writer.writeFloat(1, f);
334ffe3c632Sopenharmony_ci    });
335ffe3c632Sopenharmony_ci    var reader = jspb.BinaryReader.alloc(writer.getResultBuffer());
336ffe3c632Sopenharmony_ci    testCases.forEach(function(f) {
337ffe3c632Sopenharmony_ci      reader.nextField();
338ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(1);
339ffe3c632Sopenharmony_ci      if (isNaN(f)) {
340ffe3c632Sopenharmony_ci        expect(isNaN(reader.readFloat())).toEqual(true);
341ffe3c632Sopenharmony_ci      } else {
342ffe3c632Sopenharmony_ci        expect(reader.readFloat()).toEqual(f);
343ffe3c632Sopenharmony_ci      }
344ffe3c632Sopenharmony_ci    });
345ffe3c632Sopenharmony_ci  });
346ffe3c632Sopenharmony_ci
347ffe3c632Sopenharmony_ci  it('writes double fields', function() {
348ffe3c632Sopenharmony_ci    var testCases = [
349ffe3c632Sopenharmony_ci      0, 1, -1, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER,
350ffe3c632Sopenharmony_ci      Number.MAX_VALUE, Number.MIN_VALUE, jspb.BinaryConstants.FLOAT32_MIN,
351ffe3c632Sopenharmony_ci      -jspb.BinaryConstants.FLOAT32_MIN, jspb.BinaryConstants.FLOAT32_MAX,
352ffe3c632Sopenharmony_ci      -jspb.BinaryConstants.FLOAT32_MAX, Math.PI, Infinity, -Infinity, NaN
353ffe3c632Sopenharmony_ci    ];
354ffe3c632Sopenharmony_ci    var writer = new jspb.BinaryWriter();
355ffe3c632Sopenharmony_ci    testCases.forEach(function(f) {
356ffe3c632Sopenharmony_ci      writer.writeDouble(1, f);
357ffe3c632Sopenharmony_ci    });
358ffe3c632Sopenharmony_ci    var reader = jspb.BinaryReader.alloc(writer.getResultBuffer());
359ffe3c632Sopenharmony_ci    testCases.forEach(function(f) {
360ffe3c632Sopenharmony_ci      reader.nextField();
361ffe3c632Sopenharmony_ci      expect(reader.getFieldNumber()).toEqual(1);
362ffe3c632Sopenharmony_ci      if (isNaN(f)) {
363ffe3c632Sopenharmony_ci        expect(isNaN(reader.readDouble())).toEqual(true);
364ffe3c632Sopenharmony_ci      } else {
365ffe3c632Sopenharmony_ci        expect(reader.readDouble()).toEqual(f);
366ffe3c632Sopenharmony_ci      }
367ffe3c632Sopenharmony_ci    });
368ffe3c632Sopenharmony_ci  });
369ffe3c632Sopenharmony_ci});
370