1ffe3c632Sopenharmony_ci/** 2ffe3c632Sopenharmony_ci * @fileoverview Tests for kernel.js. 3ffe3c632Sopenharmony_ci */ 4ffe3c632Sopenharmony_cigoog.module('protobuf.runtime.KernelTest'); 5ffe3c632Sopenharmony_ci 6ffe3c632Sopenharmony_cigoog.setTestOnly(); 7ffe3c632Sopenharmony_ci 8ffe3c632Sopenharmony_ciconst ByteString = goog.require('protobuf.ByteString'); 9ffe3c632Sopenharmony_ciconst Int64 = goog.require('protobuf.Int64'); 10ffe3c632Sopenharmony_ciconst InternalMessage = goog.require('protobuf.binary.InternalMessage'); 11ffe3c632Sopenharmony_ciconst Kernel = goog.require('protobuf.runtime.Kernel'); 12ffe3c632Sopenharmony_ciconst TestMessage = goog.require('protobuf.testing.binary.TestMessage'); 13ffe3c632Sopenharmony_ci// Note to the reader: 14ffe3c632Sopenharmony_ci// Since the lazy accessor behavior changes with the checking level some of the 15ffe3c632Sopenharmony_ci// tests in this file have to know which checking level is enable to make 16ffe3c632Sopenharmony_ci// correct assertions. 17ffe3c632Sopenharmony_ciconst {CHECK_BOUNDS, CHECK_CRITICAL_STATE, CHECK_CRITICAL_TYPE, CHECK_TYPE, MAX_FIELD_NUMBER} = goog.require('protobuf.internal.checks'); 18ffe3c632Sopenharmony_ci 19ffe3c632Sopenharmony_ci/** 20ffe3c632Sopenharmony_ci * @param {...number} bytes 21ffe3c632Sopenharmony_ci * @return {!ArrayBuffer} 22ffe3c632Sopenharmony_ci */ 23ffe3c632Sopenharmony_cifunction createArrayBuffer(...bytes) { 24ffe3c632Sopenharmony_ci return new Uint8Array(bytes).buffer; 25ffe3c632Sopenharmony_ci} 26ffe3c632Sopenharmony_ci 27ffe3c632Sopenharmony_cidescribe('Kernel', () => { 28ffe3c632Sopenharmony_ci it('encodes none for the empty input', () => { 29ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 30ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(new ArrayBuffer(0)); 31ffe3c632Sopenharmony_ci }); 32ffe3c632Sopenharmony_ci 33ffe3c632Sopenharmony_ci it('encodes and decodes max field number', () => { 34ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer( 35ffe3c632Sopenharmony_ci createArrayBuffer(0xF8, 0xFF, 0xFF, 0xFF, 0x0F, 0x01)); 36ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(MAX_FIELD_NUMBER)).toBe(true); 37ffe3c632Sopenharmony_ci accessor.setBool(MAX_FIELD_NUMBER, false); 38ffe3c632Sopenharmony_ci expect(accessor.serialize()) 39ffe3c632Sopenharmony_ci .toEqual(createArrayBuffer(0xF8, 0xFF, 0xFF, 0xFF, 0x0F, 0x00)); 40ffe3c632Sopenharmony_ci }); 41ffe3c632Sopenharmony_ci 42ffe3c632Sopenharmony_ci it('uses the default pivot point', () => { 43ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 44ffe3c632Sopenharmony_ci expect(accessor.getPivot()).toBe(24); 45ffe3c632Sopenharmony_ci }); 46ffe3c632Sopenharmony_ci 47ffe3c632Sopenharmony_ci it('makes the pivot point configurable', () => { 48ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0), 50); 49ffe3c632Sopenharmony_ci expect(accessor.getPivot()).toBe(50); 50ffe3c632Sopenharmony_ci }); 51ffe3c632Sopenharmony_ci}); 52ffe3c632Sopenharmony_ci 53ffe3c632Sopenharmony_cidescribe('Kernel hasFieldNumber', () => { 54ffe3c632Sopenharmony_ci it('returns false for empty input', () => { 55ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 56ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 57ffe3c632Sopenharmony_ci }); 58ffe3c632Sopenharmony_ci 59ffe3c632Sopenharmony_ci it('returns true for non-empty input', () => { 60ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 61ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 62ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(true); 63ffe3c632Sopenharmony_ci }); 64ffe3c632Sopenharmony_ci 65ffe3c632Sopenharmony_ci it('returns false for empty array', () => { 66ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 67ffe3c632Sopenharmony_ci accessor.setPackedBoolIterable(1, []); 68ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 69ffe3c632Sopenharmony_ci }); 70ffe3c632Sopenharmony_ci 71ffe3c632Sopenharmony_ci it('returns true for non-empty array', () => { 72ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 73ffe3c632Sopenharmony_ci accessor.setPackedBoolIterable(1, [true]); 74ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(true); 75ffe3c632Sopenharmony_ci }); 76ffe3c632Sopenharmony_ci 77ffe3c632Sopenharmony_ci it('updates value after write', () => { 78ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 79ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 80ffe3c632Sopenharmony_ci accessor.setBool(1, false); 81ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(true); 82ffe3c632Sopenharmony_ci }); 83ffe3c632Sopenharmony_ci}); 84ffe3c632Sopenharmony_ci 85ffe3c632Sopenharmony_cidescribe('Kernel clear field does', () => { 86ffe3c632Sopenharmony_ci it('clear the field set', () => { 87ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 88ffe3c632Sopenharmony_ci accessor.setBool(1, true); 89ffe3c632Sopenharmony_ci accessor.clearField(1); 90ffe3c632Sopenharmony_ci 91ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toEqual(false); 92ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(new ArrayBuffer(0)); 93ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(1)).toEqual(false); 94ffe3c632Sopenharmony_ci }); 95ffe3c632Sopenharmony_ci 96ffe3c632Sopenharmony_ci it('clear the field decoded', () => { 97ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 98ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 99ffe3c632Sopenharmony_ci accessor.clearField(1); 100ffe3c632Sopenharmony_ci 101ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toEqual(false); 102ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(new ArrayBuffer(0)); 103ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(1)).toEqual(false); 104ffe3c632Sopenharmony_ci }); 105ffe3c632Sopenharmony_ci 106ffe3c632Sopenharmony_ci it('clear the field read', () => { 107ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 108ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 109ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(1)).toEqual(true); 110ffe3c632Sopenharmony_ci accessor.clearField(1); 111ffe3c632Sopenharmony_ci 112ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toEqual(false); 113ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(new ArrayBuffer(0)); 114ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(1)).toEqual(false); 115ffe3c632Sopenharmony_ci }); 116ffe3c632Sopenharmony_ci 117ffe3c632Sopenharmony_ci it('clear set and copied fields without affecting the old', () => { 118ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 119ffe3c632Sopenharmony_ci accessor.setBool(1, true); 120ffe3c632Sopenharmony_ci 121ffe3c632Sopenharmony_ci const clonedAccessor = accessor.shallowCopy(); 122ffe3c632Sopenharmony_ci clonedAccessor.clearField(1); 123ffe3c632Sopenharmony_ci 124ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toEqual(true); 125ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(1)).toEqual(true); 126ffe3c632Sopenharmony_ci expect(clonedAccessor.hasFieldNumber(1)).toEqual(false); 127ffe3c632Sopenharmony_ci expect(clonedAccessor.serialize()).toEqual(new ArrayBuffer(0)); 128ffe3c632Sopenharmony_ci expect(clonedAccessor.getBoolWithDefault(1)).toEqual(false); 129ffe3c632Sopenharmony_ci }); 130ffe3c632Sopenharmony_ci 131ffe3c632Sopenharmony_ci it('clear decoded and copied fields without affecting the old', () => { 132ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 133ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 134ffe3c632Sopenharmony_ci 135ffe3c632Sopenharmony_ci const clonedAccessor = accessor.shallowCopy(); 136ffe3c632Sopenharmony_ci clonedAccessor.clearField(1); 137ffe3c632Sopenharmony_ci 138ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toEqual(true); 139ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(1)).toEqual(true); 140ffe3c632Sopenharmony_ci expect(clonedAccessor.hasFieldNumber(1)).toEqual(false); 141ffe3c632Sopenharmony_ci expect(clonedAccessor.serialize()).toEqual(new ArrayBuffer(0)); 142ffe3c632Sopenharmony_ci expect(clonedAccessor.getBoolWithDefault(1)).toEqual(false); 143ffe3c632Sopenharmony_ci }); 144ffe3c632Sopenharmony_ci 145ffe3c632Sopenharmony_ci it('clear read and copied fields without affecting the old', () => { 146ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 147ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 148ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(1)).toEqual(true); 149ffe3c632Sopenharmony_ci 150ffe3c632Sopenharmony_ci const clonedAccessor = accessor.shallowCopy(); 151ffe3c632Sopenharmony_ci clonedAccessor.clearField(1); 152ffe3c632Sopenharmony_ci 153ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toEqual(true); 154ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(1)).toEqual(true); 155ffe3c632Sopenharmony_ci expect(clonedAccessor.hasFieldNumber(1)).toEqual(false); 156ffe3c632Sopenharmony_ci expect(clonedAccessor.serialize()).toEqual(new ArrayBuffer(0)); 157ffe3c632Sopenharmony_ci expect(clonedAccessor.getBoolWithDefault(1)).toEqual(false); 158ffe3c632Sopenharmony_ci }); 159ffe3c632Sopenharmony_ci 160ffe3c632Sopenharmony_ci it('clear the max field number', () => { 161ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 162ffe3c632Sopenharmony_ci accessor.setBool(MAX_FIELD_NUMBER, true); 163ffe3c632Sopenharmony_ci 164ffe3c632Sopenharmony_ci accessor.clearField(MAX_FIELD_NUMBER); 165ffe3c632Sopenharmony_ci 166ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(MAX_FIELD_NUMBER)).toEqual(false); 167ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(MAX_FIELD_NUMBER)).toEqual(false); 168ffe3c632Sopenharmony_ci }); 169ffe3c632Sopenharmony_ci}); 170ffe3c632Sopenharmony_ci 171ffe3c632Sopenharmony_cidescribe('Kernel shallow copy does', () => { 172ffe3c632Sopenharmony_ci it('work for singular fields', () => { 173ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 174ffe3c632Sopenharmony_ci accessor.setBool(1, true); 175ffe3c632Sopenharmony_ci accessor.setBool(MAX_FIELD_NUMBER, true); 176ffe3c632Sopenharmony_ci const clonedAccessor = accessor.shallowCopy(); 177ffe3c632Sopenharmony_ci expect(clonedAccessor.getBoolWithDefault(1)).toEqual(true); 178ffe3c632Sopenharmony_ci expect(clonedAccessor.getBoolWithDefault(MAX_FIELD_NUMBER)).toEqual(true); 179ffe3c632Sopenharmony_ci 180ffe3c632Sopenharmony_ci accessor.setBool(1, false); 181ffe3c632Sopenharmony_ci accessor.setBool(MAX_FIELD_NUMBER, false); 182ffe3c632Sopenharmony_ci expect(clonedAccessor.getBoolWithDefault(1)).toEqual(true); 183ffe3c632Sopenharmony_ci expect(clonedAccessor.getBoolWithDefault(MAX_FIELD_NUMBER)).toEqual(true); 184ffe3c632Sopenharmony_ci }); 185ffe3c632Sopenharmony_ci 186ffe3c632Sopenharmony_ci it('work for repeated fields', () => { 187ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 188ffe3c632Sopenharmony_ci 189ffe3c632Sopenharmony_ci accessor.addUnpackedBoolIterable(2, [true, true]); 190ffe3c632Sopenharmony_ci 191ffe3c632Sopenharmony_ci const clonedAccessor = accessor.shallowCopy(); 192ffe3c632Sopenharmony_ci 193ffe3c632Sopenharmony_ci // Modify a repeated field after clone 194ffe3c632Sopenharmony_ci accessor.addUnpackedBoolElement(2, true); 195ffe3c632Sopenharmony_ci 196ffe3c632Sopenharmony_ci const array = Array.from(clonedAccessor.getRepeatedBoolIterable(2)); 197ffe3c632Sopenharmony_ci expect(array).toEqual([true, true]); 198ffe3c632Sopenharmony_ci }); 199ffe3c632Sopenharmony_ci 200ffe3c632Sopenharmony_ci it('work for repeated fields', () => { 201ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 202ffe3c632Sopenharmony_ci 203ffe3c632Sopenharmony_ci accessor.addUnpackedBoolIterable(2, [true, true]); 204ffe3c632Sopenharmony_ci 205ffe3c632Sopenharmony_ci const clonedAccessor = accessor.shallowCopy(); 206ffe3c632Sopenharmony_ci 207ffe3c632Sopenharmony_ci // Modify a repeated field after clone 208ffe3c632Sopenharmony_ci accessor.addUnpackedBoolElement(2, true); 209ffe3c632Sopenharmony_ci 210ffe3c632Sopenharmony_ci const array = Array.from(clonedAccessor.getRepeatedBoolIterable(2)); 211ffe3c632Sopenharmony_ci expect(array).toEqual([true, true]); 212ffe3c632Sopenharmony_ci }); 213ffe3c632Sopenharmony_ci 214ffe3c632Sopenharmony_ci it('return the correct bytes after serialization', () => { 215ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01, 0x10, 0x01); 216ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes, /* pivot= */ 1); 217ffe3c632Sopenharmony_ci const clonedAccessor = accessor.shallowCopy(); 218ffe3c632Sopenharmony_ci 219ffe3c632Sopenharmony_ci accessor.setBool(1, false); 220ffe3c632Sopenharmony_ci 221ffe3c632Sopenharmony_ci expect(clonedAccessor.getBoolWithDefault(1)).toEqual(true); 222ffe3c632Sopenharmony_ci expect(clonedAccessor.serialize()).toEqual(bytes); 223ffe3c632Sopenharmony_ci }); 224ffe3c632Sopenharmony_ci}); 225ffe3c632Sopenharmony_ci 226ffe3c632Sopenharmony_cidescribe('Kernel for singular boolean does', () => { 227ffe3c632Sopenharmony_ci it('return false for the empty input', () => { 228ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 229ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault( 230ffe3c632Sopenharmony_ci /* fieldNumber= */ 1)) 231ffe3c632Sopenharmony_ci .toBe(false); 232ffe3c632Sopenharmony_ci }); 233ffe3c632Sopenharmony_ci 234ffe3c632Sopenharmony_ci it('return the value from the input', () => { 235ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 236ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 237ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault( 238ffe3c632Sopenharmony_ci /* fieldNumber= */ 1)) 239ffe3c632Sopenharmony_ci .toBe(true); 240ffe3c632Sopenharmony_ci }); 241ffe3c632Sopenharmony_ci 242ffe3c632Sopenharmony_ci it('encode the value from the input', () => { 243ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 244ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 245ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 246ffe3c632Sopenharmony_ci }); 247ffe3c632Sopenharmony_ci 248ffe3c632Sopenharmony_ci it('encode the value from the input after read', () => { 249ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 250ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 251ffe3c632Sopenharmony_ci accessor.getBoolWithDefault( 252ffe3c632Sopenharmony_ci /* fieldNumber= */ 1); 253ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 254ffe3c632Sopenharmony_ci }); 255ffe3c632Sopenharmony_ci 256ffe3c632Sopenharmony_ci it('return the value from multiple inputs', () => { 257ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00); 258ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 259ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault( 260ffe3c632Sopenharmony_ci /* fieldNumber= */ 1)) 261ffe3c632Sopenharmony_ci .toBe(false); 262ffe3c632Sopenharmony_ci }); 263ffe3c632Sopenharmony_ci 264ffe3c632Sopenharmony_ci it('encode the value from multiple inputs', () => { 265ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00); 266ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 267ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 268ffe3c632Sopenharmony_ci }); 269ffe3c632Sopenharmony_ci 270ffe3c632Sopenharmony_ci it('encode the value from multiple inputs after read', () => { 271ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00); 272ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 273ffe3c632Sopenharmony_ci accessor.getBoolWithDefault(/* fieldNumber= */ 1); 274ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 275ffe3c632Sopenharmony_ci }); 276ffe3c632Sopenharmony_ci 277ffe3c632Sopenharmony_ci it('return the value from setter', () => { 278ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00); 279ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 280ffe3c632Sopenharmony_ci accessor.setBool(1, true); 281ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault( 282ffe3c632Sopenharmony_ci /* fieldNumber= */ 1)) 283ffe3c632Sopenharmony_ci .toBe(true); 284ffe3c632Sopenharmony_ci }); 285ffe3c632Sopenharmony_ci 286ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 287ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00); 288ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 289ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x08, 0x01); 290ffe3c632Sopenharmony_ci accessor.setBool(1, true); 291ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 292ffe3c632Sopenharmony_ci }); 293ffe3c632Sopenharmony_ci 294ffe3c632Sopenharmony_ci it('return the bool value from cache', () => { 295ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 296ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 297ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault( 298ffe3c632Sopenharmony_ci /* fieldNumber= */ 1)) 299ffe3c632Sopenharmony_ci .toBe(true); 300ffe3c632Sopenharmony_ci // Make sure the value is cached. 301ffe3c632Sopenharmony_ci bytes[1] = 0x00; 302ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault( 303ffe3c632Sopenharmony_ci /* fieldNumber= */ 1)) 304ffe3c632Sopenharmony_ci .toBe(true); 305ffe3c632Sopenharmony_ci }); 306ffe3c632Sopenharmony_ci 307ffe3c632Sopenharmony_ci it('fail when getting bool value with other wire types', () => { 308ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 309ffe3c632Sopenharmony_ci 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)); 310ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 311ffe3c632Sopenharmony_ci expect(() => { 312ffe3c632Sopenharmony_ci accessor.getBoolWithDefault(/* fieldNumber= */ 1); 313ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 0 but found: 1'); 314ffe3c632Sopenharmony_ci } else { 315ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 316ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 317ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 318ffe3c632Sopenharmony_ci // what the implementation should be doing here. 319ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault( 320ffe3c632Sopenharmony_ci /* fieldNumber= */ 1)) 321ffe3c632Sopenharmony_ci .toBe(true); 322ffe3c632Sopenharmony_ci } 323ffe3c632Sopenharmony_ci }); 324ffe3c632Sopenharmony_ci 325ffe3c632Sopenharmony_ci it('fail when setting bool value with out-of-range field number', () => { 326ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 327ffe3c632Sopenharmony_ci if (CHECK_TYPE) { 328ffe3c632Sopenharmony_ci expect(() => accessor.setBool(MAX_FIELD_NUMBER + 1, false)) 329ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: 536870912'); 330ffe3c632Sopenharmony_ci } else { 331ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 332ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 333ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 334ffe3c632Sopenharmony_ci // what the implementation should be doing here. 335ffe3c632Sopenharmony_ci accessor.setBool(MAX_FIELD_NUMBER + 1, false); 336ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault(MAX_FIELD_NUMBER + 1)).toBe(false); 337ffe3c632Sopenharmony_ci } 338ffe3c632Sopenharmony_ci }); 339ffe3c632Sopenharmony_ci 340ffe3c632Sopenharmony_ci it('fail when setting bool value with number value', () => { 341ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 342ffe3c632Sopenharmony_ci const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2)); 343ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 344ffe3c632Sopenharmony_ci expect(() => accessor.setBool(1, fakeBoolean)) 345ffe3c632Sopenharmony_ci .toThrowError('Must be a boolean, but got: 2'); 346ffe3c632Sopenharmony_ci } else { 347ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 348ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 349ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 350ffe3c632Sopenharmony_ci // what the implementation should be doing here. 351ffe3c632Sopenharmony_ci accessor.setBool(1, fakeBoolean); 352ffe3c632Sopenharmony_ci expect(accessor.getBoolWithDefault( 353ffe3c632Sopenharmony_ci /* fieldNumber= */ 1)) 354ffe3c632Sopenharmony_ci .toBe(2); 355ffe3c632Sopenharmony_ci } 356ffe3c632Sopenharmony_ci }); 357ffe3c632Sopenharmony_ci}); 358ffe3c632Sopenharmony_ci 359ffe3c632Sopenharmony_cidescribe('Kernel for singular message does', () => { 360ffe3c632Sopenharmony_ci it('return message from the input', () => { 361ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 362ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 363ffe3c632Sopenharmony_ci const msg = accessor.getMessageOrNull(1, TestMessage.instanceCreator); 364ffe3c632Sopenharmony_ci expect(msg.getBoolWithDefault(1, false)).toBe(true); 365ffe3c632Sopenharmony_ci }); 366ffe3c632Sopenharmony_ci 367ffe3c632Sopenharmony_ci it('return message from the input when pivot is set', () => { 368ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 369ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes, /* pivot= */ 0); 370ffe3c632Sopenharmony_ci const msg = accessor.getMessageOrNull(1, TestMessage.instanceCreator); 371ffe3c632Sopenharmony_ci expect(msg.getBoolWithDefault(1, false)).toBe(true); 372ffe3c632Sopenharmony_ci }); 373ffe3c632Sopenharmony_ci 374ffe3c632Sopenharmony_ci it('encode message from the input', () => { 375ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 376ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 377ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 378ffe3c632Sopenharmony_ci }); 379ffe3c632Sopenharmony_ci 380ffe3c632Sopenharmony_ci it('encode message from the input after read', () => { 381ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 382ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 383ffe3c632Sopenharmony_ci accessor.getMessageOrNull(1, TestMessage.instanceCreator); 384ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 385ffe3c632Sopenharmony_ci }); 386ffe3c632Sopenharmony_ci 387ffe3c632Sopenharmony_ci it('return message from multiple inputs', () => { 388ffe3c632Sopenharmony_ci const bytes = 389ffe3c632Sopenharmony_ci createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x10, 0x01); 390ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 391ffe3c632Sopenharmony_ci const msg = accessor.getMessageOrNull(1, TestMessage.instanceCreator); 392ffe3c632Sopenharmony_ci expect(msg.getBoolWithDefault(1, false)).toBe(true); 393ffe3c632Sopenharmony_ci expect(msg.getBoolWithDefault(2, false)).toBe(true); 394ffe3c632Sopenharmony_ci }); 395ffe3c632Sopenharmony_ci 396ffe3c632Sopenharmony_ci it('encode message from multiple inputs', () => { 397ffe3c632Sopenharmony_ci const bytes = 398ffe3c632Sopenharmony_ci createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x10, 0x01); 399ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 400ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 401ffe3c632Sopenharmony_ci }); 402ffe3c632Sopenharmony_ci 403ffe3c632Sopenharmony_ci it('encode message merged from multiple inputs after read', () => { 404ffe3c632Sopenharmony_ci const bytes = 405ffe3c632Sopenharmony_ci createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x10, 0x01); 406ffe3c632Sopenharmony_ci const expected = createArrayBuffer(0x0A, 0x04, 0x08, 0x01, 0x10, 0x01); 407ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 408ffe3c632Sopenharmony_ci accessor.getMessageOrNull(1, TestMessage.instanceCreator); 409ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(expected); 410ffe3c632Sopenharmony_ci }); 411ffe3c632Sopenharmony_ci 412ffe3c632Sopenharmony_ci it('return null for generic accessor', () => { 413ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 414ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 415ffe3c632Sopenharmony_ci const accessor1 = accessor.getMessageAccessorOrNull(7); 416ffe3c632Sopenharmony_ci expect(accessor1).toBe(null); 417ffe3c632Sopenharmony_ci }); 418ffe3c632Sopenharmony_ci 419ffe3c632Sopenharmony_ci it('return null for generic accessor when pivot is set', () => { 420ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 421ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 422ffe3c632Sopenharmony_ci const accessor1 = accessor.getMessageAccessorOrNull(7, /* pivot= */ 0); 423ffe3c632Sopenharmony_ci expect(accessor1).toBe(null); 424ffe3c632Sopenharmony_ci }); 425ffe3c632Sopenharmony_ci 426ffe3c632Sopenharmony_ci it('return generic accessor from the input', () => { 427ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 428ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 429ffe3c632Sopenharmony_ci const accessor1 = accessor.getMessageAccessorOrNull(1); 430ffe3c632Sopenharmony_ci expect(accessor1.getBoolWithDefault(1, false)).toBe(true); 431ffe3c632Sopenharmony_ci // Second call returns a new instance, isn't cached. 432ffe3c632Sopenharmony_ci const accessor2 = accessor.getMessageAccessorOrNull(1); 433ffe3c632Sopenharmony_ci expect(accessor2.getBoolWithDefault(1, false)).toBe(true); 434ffe3c632Sopenharmony_ci expect(accessor2).not.toBe(accessor1); 435ffe3c632Sopenharmony_ci }); 436ffe3c632Sopenharmony_ci 437ffe3c632Sopenharmony_ci it('return generic accessor from the cached input', () => { 438ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 439ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 440ffe3c632Sopenharmony_ci const wrappedMessage = 441ffe3c632Sopenharmony_ci accessor.getMessageOrNull(1, TestMessage.instanceCreator); 442ffe3c632Sopenharmony_ci 443ffe3c632Sopenharmony_ci // Returns accessor from the cached wrapper instance. 444ffe3c632Sopenharmony_ci const accessor1 = accessor.getMessageAccessorOrNull(1); 445ffe3c632Sopenharmony_ci expect(accessor1.getBoolWithDefault(1, false)).toBe(true); 446ffe3c632Sopenharmony_ci expect(accessor1).toBe( 447ffe3c632Sopenharmony_ci (/** @type {!InternalMessage} */ (wrappedMessage)).internalGetKernel()); 448ffe3c632Sopenharmony_ci 449ffe3c632Sopenharmony_ci // Second call returns exact same instance. 450ffe3c632Sopenharmony_ci const accessor2 = accessor.getMessageAccessorOrNull(1); 451ffe3c632Sopenharmony_ci expect(accessor2.getBoolWithDefault(1, false)).toBe(true); 452ffe3c632Sopenharmony_ci expect(accessor2).toBe( 453ffe3c632Sopenharmony_ci (/** @type {!InternalMessage} */ (wrappedMessage)).internalGetKernel()); 454ffe3c632Sopenharmony_ci expect(accessor2).toBe(accessor1); 455ffe3c632Sopenharmony_ci }); 456ffe3c632Sopenharmony_ci 457ffe3c632Sopenharmony_ci it('return message from setter', () => { 458ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 459ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 460ffe3c632Sopenharmony_ci const subaccessor = Kernel.fromArrayBuffer(bytes); 461ffe3c632Sopenharmony_ci const submsg1 = new TestMessage(subaccessor); 462ffe3c632Sopenharmony_ci accessor.setMessage(1, submsg1); 463ffe3c632Sopenharmony_ci const submsg2 = accessor.getMessage(1, TestMessage.instanceCreator); 464ffe3c632Sopenharmony_ci expect(submsg1).toBe(submsg2); 465ffe3c632Sopenharmony_ci }); 466ffe3c632Sopenharmony_ci 467ffe3c632Sopenharmony_ci it('encode message from setter', () => { 468ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 469ffe3c632Sopenharmony_ci const subaccessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 470ffe3c632Sopenharmony_ci const subsubaccessor = 471ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01)); 472ffe3c632Sopenharmony_ci const subsubmsg = new TestMessage(subsubaccessor); 473ffe3c632Sopenharmony_ci subaccessor.setMessage(1, subsubmsg); 474ffe3c632Sopenharmony_ci const submsg = new TestMessage(subaccessor); 475ffe3c632Sopenharmony_ci accessor.setMessage(1, submsg); 476ffe3c632Sopenharmony_ci const expected = createArrayBuffer(0x0A, 0x04, 0x0A, 0x02, 0x08, 0x01); 477ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(expected); 478ffe3c632Sopenharmony_ci }); 479ffe3c632Sopenharmony_ci 480ffe3c632Sopenharmony_ci it('encode message with multiple submessage from setter', () => { 481ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 482ffe3c632Sopenharmony_ci const subaccessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 483ffe3c632Sopenharmony_ci const subsubaccessor1 = 484ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01)); 485ffe3c632Sopenharmony_ci const subsubaccessor2 = 486ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x02)); 487ffe3c632Sopenharmony_ci 488ffe3c632Sopenharmony_ci const subsubmsg1 = new TestMessage(subsubaccessor1); 489ffe3c632Sopenharmony_ci const subsubmsg2 = new TestMessage(subsubaccessor2); 490ffe3c632Sopenharmony_ci 491ffe3c632Sopenharmony_ci subaccessor.setMessage(1, subsubmsg1); 492ffe3c632Sopenharmony_ci subaccessor.setMessage(2, subsubmsg2); 493ffe3c632Sopenharmony_ci 494ffe3c632Sopenharmony_ci const submsg = new TestMessage(subaccessor); 495ffe3c632Sopenharmony_ci accessor.setMessage(1, submsg); 496ffe3c632Sopenharmony_ci 497ffe3c632Sopenharmony_ci const expected = createArrayBuffer( 498ffe3c632Sopenharmony_ci 0x0A, 0x08, 0x0A, 0x02, 0x08, 0x01, 0x12, 0x02, 0x08, 0x02); 499ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(expected); 500ffe3c632Sopenharmony_ci }); 501ffe3c632Sopenharmony_ci 502ffe3c632Sopenharmony_ci it('leave hasFieldNumber unchanged after getMessageOrNull', () => { 503ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 504ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 505ffe3c632Sopenharmony_ci expect(accessor.getMessageOrNull(1, TestMessage.instanceCreator)) 506ffe3c632Sopenharmony_ci .toBe(null); 507ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 508ffe3c632Sopenharmony_ci }); 509ffe3c632Sopenharmony_ci 510ffe3c632Sopenharmony_ci it('serialize changes to submessages made with getMessageOrNull', () => { 511ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02); 512ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 513ffe3c632Sopenharmony_ci const mutableSubMessage = 514ffe3c632Sopenharmony_ci accessor.getMessageOrNull(1, TestMessage.instanceCreator); 515ffe3c632Sopenharmony_ci mutableSubMessage.setInt32(1, 10); 516ffe3c632Sopenharmony_ci const intTenBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x0A); 517ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(intTenBytes); 518ffe3c632Sopenharmony_ci }); 519ffe3c632Sopenharmony_ci 520ffe3c632Sopenharmony_ci it('serialize additions to submessages made with getMessageOrNull', () => { 521ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02); 522ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 523ffe3c632Sopenharmony_ci const mutableSubMessage = 524ffe3c632Sopenharmony_ci accessor.getMessageOrNull(1, TestMessage.instanceCreator); 525ffe3c632Sopenharmony_ci mutableSubMessage.setInt32(2, 3); 526ffe3c632Sopenharmony_ci // Sub message contains the original field, plus the new one. 527ffe3c632Sopenharmony_ci expect(accessor.serialize()) 528ffe3c632Sopenharmony_ci .toEqual(createArrayBuffer(0x0A, 0x04, 0x08, 0x02, 0x10, 0x03)); 529ffe3c632Sopenharmony_ci }); 530ffe3c632Sopenharmony_ci 531ffe3c632Sopenharmony_ci it('fail with getMessageOrNull if immutable message exist in cache', () => { 532ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02); 533ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 534ffe3c632Sopenharmony_ci 535ffe3c632Sopenharmony_ci const readOnly = accessor.getMessage(1, TestMessage.instanceCreator); 536ffe3c632Sopenharmony_ci if (CHECK_TYPE) { 537ffe3c632Sopenharmony_ci expect(() => accessor.getMessageOrNull(1, TestMessage.instanceCreator)) 538ffe3c632Sopenharmony_ci .toThrow(); 539ffe3c632Sopenharmony_ci } else { 540ffe3c632Sopenharmony_ci const mutableSubMessage = 541ffe3c632Sopenharmony_ci accessor.getMessageOrNull(1, TestMessage.instanceCreator); 542ffe3c632Sopenharmony_ci // The instance returned by getMessageOrNull is the exact same instance. 543ffe3c632Sopenharmony_ci expect(mutableSubMessage).toBe(readOnly); 544ffe3c632Sopenharmony_ci 545ffe3c632Sopenharmony_ci // Serializing the submessage does not write the changes 546ffe3c632Sopenharmony_ci mutableSubMessage.setInt32(1, 0); 547ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(intTwoBytes); 548ffe3c632Sopenharmony_ci } 549ffe3c632Sopenharmony_ci }); 550ffe3c632Sopenharmony_ci 551ffe3c632Sopenharmony_ci it('change hasFieldNumber after getMessageAttach', () => { 552ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 553ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 554ffe3c632Sopenharmony_ci expect(accessor.getMessageAttach(1, TestMessage.instanceCreator)) 555ffe3c632Sopenharmony_ci .not.toBe(null); 556ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(true); 557ffe3c632Sopenharmony_ci }); 558ffe3c632Sopenharmony_ci 559ffe3c632Sopenharmony_ci it('change hasFieldNumber after getMessageAttach when pivot is set', () => { 560ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 561ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 562ffe3c632Sopenharmony_ci expect(accessor.getMessageAttach( 563ffe3c632Sopenharmony_ci 1, TestMessage.instanceCreator, /* pivot= */ 1)) 564ffe3c632Sopenharmony_ci .not.toBe(null); 565ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(true); 566ffe3c632Sopenharmony_ci }); 567ffe3c632Sopenharmony_ci 568ffe3c632Sopenharmony_ci it('serialize submessages made with getMessageAttach', () => { 569ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 570ffe3c632Sopenharmony_ci const mutableSubMessage = 571ffe3c632Sopenharmony_ci accessor.getMessageAttach(1, TestMessage.instanceCreator); 572ffe3c632Sopenharmony_ci mutableSubMessage.setInt32(1, 10); 573ffe3c632Sopenharmony_ci const intTenBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x0A); 574ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(intTenBytes); 575ffe3c632Sopenharmony_ci }); 576ffe3c632Sopenharmony_ci 577ffe3c632Sopenharmony_ci it('serialize additions to submessages using getMessageAttach', () => { 578ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02); 579ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 580ffe3c632Sopenharmony_ci const mutableSubMessage = 581ffe3c632Sopenharmony_ci accessor.getMessageAttach(1, TestMessage.instanceCreator); 582ffe3c632Sopenharmony_ci mutableSubMessage.setInt32(2, 3); 583ffe3c632Sopenharmony_ci // Sub message contains the original field, plus the new one. 584ffe3c632Sopenharmony_ci expect(accessor.serialize()) 585ffe3c632Sopenharmony_ci .toEqual(createArrayBuffer(0x0A, 0x04, 0x08, 0x02, 0x10, 0x03)); 586ffe3c632Sopenharmony_ci }); 587ffe3c632Sopenharmony_ci 588ffe3c632Sopenharmony_ci it('fail with getMessageAttach if immutable message exist in cache', () => { 589ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02); 590ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 591ffe3c632Sopenharmony_ci 592ffe3c632Sopenharmony_ci const readOnly = accessor.getMessage(1, TestMessage.instanceCreator); 593ffe3c632Sopenharmony_ci if (CHECK_TYPE) { 594ffe3c632Sopenharmony_ci expect(() => accessor.getMessageAttach(1, TestMessage.instanceCreator)) 595ffe3c632Sopenharmony_ci .toThrow(); 596ffe3c632Sopenharmony_ci } else { 597ffe3c632Sopenharmony_ci const mutableSubMessage = 598ffe3c632Sopenharmony_ci accessor.getMessageAttach(1, TestMessage.instanceCreator); 599ffe3c632Sopenharmony_ci // The instance returned by getMessageOrNull is the exact same instance. 600ffe3c632Sopenharmony_ci expect(mutableSubMessage).toBe(readOnly); 601ffe3c632Sopenharmony_ci 602ffe3c632Sopenharmony_ci // Serializing the submessage does not write the changes 603ffe3c632Sopenharmony_ci mutableSubMessage.setInt32(1, 0); 604ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(intTwoBytes); 605ffe3c632Sopenharmony_ci } 606ffe3c632Sopenharmony_ci }); 607ffe3c632Sopenharmony_ci 608ffe3c632Sopenharmony_ci it('read default message return empty message with getMessage', () => { 609ffe3c632Sopenharmony_ci const bytes = new ArrayBuffer(0); 610ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 611ffe3c632Sopenharmony_ci expect(accessor.getMessage(1, TestMessage.instanceCreator)).toBeTruthy(); 612ffe3c632Sopenharmony_ci expect(accessor.getMessage(1, TestMessage.instanceCreator).serialize()) 613ffe3c632Sopenharmony_ci .toEqual(bytes); 614ffe3c632Sopenharmony_ci }); 615ffe3c632Sopenharmony_ci 616ffe3c632Sopenharmony_ci it('read default message return null with getMessageOrNull', () => { 617ffe3c632Sopenharmony_ci const bytes = new ArrayBuffer(0); 618ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 619ffe3c632Sopenharmony_ci expect(accessor.getMessageOrNull(1, TestMessage.instanceCreator)) 620ffe3c632Sopenharmony_ci .toBe(null); 621ffe3c632Sopenharmony_ci }); 622ffe3c632Sopenharmony_ci 623ffe3c632Sopenharmony_ci it('read message preserve reference equality', () => { 624ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01); 625ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 626ffe3c632Sopenharmony_ci const msg1 = accessor.getMessageOrNull(1, TestMessage.instanceCreator); 627ffe3c632Sopenharmony_ci const msg2 = accessor.getMessageOrNull(1, TestMessage.instanceCreator); 628ffe3c632Sopenharmony_ci const msg3 = accessor.getMessageAttach(1, TestMessage.instanceCreator); 629ffe3c632Sopenharmony_ci expect(msg1).toBe(msg2); 630ffe3c632Sopenharmony_ci expect(msg1).toBe(msg3); 631ffe3c632Sopenharmony_ci }); 632ffe3c632Sopenharmony_ci 633ffe3c632Sopenharmony_ci it('fail when getting message with other wire types', () => { 634ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01)); 635ffe3c632Sopenharmony_ci expect(() => accessor.getMessageOrNull(1, TestMessage.instanceCreator)) 636ffe3c632Sopenharmony_ci .toThrow(); 637ffe3c632Sopenharmony_ci }); 638ffe3c632Sopenharmony_ci 639ffe3c632Sopenharmony_ci it('fail when submessage has incomplete data', () => { 640ffe3c632Sopenharmony_ci const accessor = 641ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x08)); 642ffe3c632Sopenharmony_ci expect(() => accessor.getMessageOrNull(1, TestMessage.instanceCreator)) 643ffe3c632Sopenharmony_ci .toThrow(); 644ffe3c632Sopenharmony_ci }); 645ffe3c632Sopenharmony_ci 646ffe3c632Sopenharmony_ci it('fail when mutable submessage has incomplete data', () => { 647ffe3c632Sopenharmony_ci const accessor = 648ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x08)); 649ffe3c632Sopenharmony_ci expect(() => accessor.getMessageAttach(1, TestMessage.instanceCreator)) 650ffe3c632Sopenharmony_ci .toThrow(); 651ffe3c632Sopenharmony_ci }); 652ffe3c632Sopenharmony_ci 653ffe3c632Sopenharmony_ci it('fail when getting message with null instance constructor', () => { 654ffe3c632Sopenharmony_ci const accessor = 655ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0A, 0x02, 0x08, 0x01)); 656ffe3c632Sopenharmony_ci const nullMessage = /** @type {function(!Kernel):!TestMessage} */ 657ffe3c632Sopenharmony_ci (/** @type {*} */ (null)); 658ffe3c632Sopenharmony_ci expect(() => accessor.getMessageOrNull(1, nullMessage)).toThrow(); 659ffe3c632Sopenharmony_ci }); 660ffe3c632Sopenharmony_ci 661ffe3c632Sopenharmony_ci it('fail when setting message value with null value', () => { 662ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 663ffe3c632Sopenharmony_ci const fakeMessage = /** @type {!TestMessage} */ (/** @type {*} */ (null)); 664ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 665ffe3c632Sopenharmony_ci expect(() => accessor.setMessage(1, fakeMessage)) 666ffe3c632Sopenharmony_ci .toThrowError('Given value is not a message instance: null'); 667ffe3c632Sopenharmony_ci } else { 668ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 669ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 670ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 671ffe3c632Sopenharmony_ci // what the implementation should be doing here. 672ffe3c632Sopenharmony_ci accessor.setMessage(1, fakeMessage); 673ffe3c632Sopenharmony_ci expect(accessor.getMessageOrNull( 674ffe3c632Sopenharmony_ci /* fieldNumber= */ 1, TestMessage.instanceCreator)) 675ffe3c632Sopenharmony_ci .toBeNull(); 676ffe3c632Sopenharmony_ci } 677ffe3c632Sopenharmony_ci }); 678ffe3c632Sopenharmony_ci}); 679ffe3c632Sopenharmony_ci 680ffe3c632Sopenharmony_cidescribe('Bytes access', () => { 681ffe3c632Sopenharmony_ci const simpleByteString = ByteString.fromArrayBuffer(createArrayBuffer(1)); 682ffe3c632Sopenharmony_ci 683ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 684ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 685ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1)).toEqual(ByteString.EMPTY); 686ffe3c632Sopenharmony_ci }); 687ffe3c632Sopenharmony_ci 688ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 689ffe3c632Sopenharmony_ci const defaultByteString = ByteString.fromArrayBuffer(createArrayBuffer(1)); 690ffe3c632Sopenharmony_ci const returnValue = ByteString.fromArrayBuffer(createArrayBuffer(1)); 691ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 692ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1, defaultByteString)) 693ffe3c632Sopenharmony_ci .toEqual(returnValue); 694ffe3c632Sopenharmony_ci }); 695ffe3c632Sopenharmony_ci 696ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 697ffe3c632Sopenharmony_ci const accessor = 698ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x01)); 699ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString); 700ffe3c632Sopenharmony_ci }); 701ffe3c632Sopenharmony_ci 702ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 703ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer( 704ffe3c632Sopenharmony_ci createArrayBuffer(0x0A, 0x01, 0x00, 0x0A, 0x01, 0x01)); 705ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString); 706ffe3c632Sopenharmony_ci }); 707ffe3c632Sopenharmony_ci 708ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 709ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 710ffe3c632Sopenharmony_ci 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01)); 711ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 712ffe3c632Sopenharmony_ci expect(() => { 713ffe3c632Sopenharmony_ci accessor.getBytesWithDefault(1); 714ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 2 but found: 1'); 715ffe3c632Sopenharmony_ci } else { 716ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 717ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 718ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 719ffe3c632Sopenharmony_ci // what the implementation should be doing here. 720ffe3c632Sopenharmony_ci const arrayBuffer = createArrayBuffer(1); 721ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1)) 722ffe3c632Sopenharmony_ci .toEqual(ByteString.fromArrayBuffer(arrayBuffer)); 723ffe3c632Sopenharmony_ci } 724ffe3c632Sopenharmony_ci }); 725ffe3c632Sopenharmony_ci 726ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 727ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 728ffe3c632Sopenharmony_ci expect( 729ffe3c632Sopenharmony_ci () => Kernel.createEmpty().getBytesWithDefault(-1, simpleByteString)) 730ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 731ffe3c632Sopenharmony_ci } else { 732ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getBytesWithDefault(-1, simpleByteString)) 733ffe3c632Sopenharmony_ci .toEqual(simpleByteString); 734ffe3c632Sopenharmony_ci } 735ffe3c632Sopenharmony_ci }); 736ffe3c632Sopenharmony_ci 737ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 738ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x01, 0x00); 739ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 740ffe3c632Sopenharmony_ci accessor.setBytes(1, simpleByteString); 741ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString); 742ffe3c632Sopenharmony_ci }); 743ffe3c632Sopenharmony_ci 744ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 745ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x01, 0x00); 746ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 747ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x0A, 0x01, 0x01); 748ffe3c632Sopenharmony_ci accessor.setBytes(1, simpleByteString); 749ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 750ffe3c632Sopenharmony_ci }); 751ffe3c632Sopenharmony_ci 752ffe3c632Sopenharmony_ci it('returns value from cache', () => { 753ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x01, 0x01); 754ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 755ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString); 756ffe3c632Sopenharmony_ci // Make sure the value is cached. 757ffe3c632Sopenharmony_ci bytes[2] = 0x00; 758ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString); 759ffe3c632Sopenharmony_ci }); 760ffe3c632Sopenharmony_ci 761ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 762ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 763ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setBytes(-1, simpleByteString)) 764ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 765ffe3c632Sopenharmony_ci } else { 766ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 767ffe3c632Sopenharmony_ci accessor.setBytes(-1, simpleByteString); 768ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(-1)).toEqual(simpleByteString); 769ffe3c632Sopenharmony_ci } 770ffe3c632Sopenharmony_ci }); 771ffe3c632Sopenharmony_ci 772ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 773ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 774ffe3c632Sopenharmony_ci expect( 775ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setBytes( 776ffe3c632Sopenharmony_ci 1, /** @type {!ByteString} */ (/** @type {*} */ (null)))) 777ffe3c632Sopenharmony_ci .toThrow(); 778ffe3c632Sopenharmony_ci } else { 779ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 780ffe3c632Sopenharmony_ci accessor.setBytes( 781ffe3c632Sopenharmony_ci 1, /** @type {!ByteString} */ (/** @type {*} */ (null))); 782ffe3c632Sopenharmony_ci expect(accessor.getBytesWithDefault(1)).toEqual(null); 783ffe3c632Sopenharmony_ci } 784ffe3c632Sopenharmony_ci }); 785ffe3c632Sopenharmony_ci}); 786ffe3c632Sopenharmony_ci 787ffe3c632Sopenharmony_cidescribe('Fixed32 access', () => { 788ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 789ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 790ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toEqual(0); 791ffe3c632Sopenharmony_ci }); 792ffe3c632Sopenharmony_ci 793ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 794ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 795ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1, 2)).toEqual(2); 796ffe3c632Sopenharmony_ci }); 797ffe3c632Sopenharmony_ci 798ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 799ffe3c632Sopenharmony_ci const accessor = 800ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00)); 801ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toEqual(1); 802ffe3c632Sopenharmony_ci }); 803ffe3c632Sopenharmony_ci 804ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 805ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 806ffe3c632Sopenharmony_ci 0x0D, 0x01, 0x00, 0x80, 0x00, 0x0D, 0x02, 0x00, 0x00, 0x00)); 807ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toEqual(2); 808ffe3c632Sopenharmony_ci }); 809ffe3c632Sopenharmony_ci 810ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 811ffe3c632Sopenharmony_ci const accessor = 812ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00)); 813ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 814ffe3c632Sopenharmony_ci expect(() => { 815ffe3c632Sopenharmony_ci accessor.getFixed32WithDefault(1); 816ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 5 but found: 0'); 817ffe3c632Sopenharmony_ci } else { 818ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 819ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 820ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 821ffe3c632Sopenharmony_ci // what the implementation should be doing here. 822ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toEqual(8421504); 823ffe3c632Sopenharmony_ci } 824ffe3c632Sopenharmony_ci }); 825ffe3c632Sopenharmony_ci 826ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 827ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 828ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().getFixed32WithDefault(-1, 1)) 829ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 830ffe3c632Sopenharmony_ci } else { 831ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getFixed32WithDefault(-1, 1)).toEqual(1); 832ffe3c632Sopenharmony_ci } 833ffe3c632Sopenharmony_ci }); 834ffe3c632Sopenharmony_ci 835ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 836ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00); 837ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 838ffe3c632Sopenharmony_ci accessor.setFixed32(1, 2); 839ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toEqual(2); 840ffe3c632Sopenharmony_ci }); 841ffe3c632Sopenharmony_ci 842ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 843ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00); 844ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 845ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00); 846ffe3c632Sopenharmony_ci accessor.setFixed32(1, 0); 847ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 848ffe3c632Sopenharmony_ci }); 849ffe3c632Sopenharmony_ci 850ffe3c632Sopenharmony_ci it('returns value from cache', () => { 851ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00); 852ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 853ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toBe(1); 854ffe3c632Sopenharmony_ci // Make sure the value is cached. 855ffe3c632Sopenharmony_ci bytes[2] = 0x00; 856ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toBe(1); 857ffe3c632Sopenharmony_ci }); 858ffe3c632Sopenharmony_ci 859ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 860ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 861ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setFixed32(-1, 1)) 862ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 863ffe3c632Sopenharmony_ci } else { 864ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 865ffe3c632Sopenharmony_ci accessor.setFixed32(-1, 1); 866ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(-1)).toEqual(1); 867ffe3c632Sopenharmony_ci } 868ffe3c632Sopenharmony_ci }); 869ffe3c632Sopenharmony_ci 870ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 871ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 872ffe3c632Sopenharmony_ci expect( 873ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setFixed32( 874ffe3c632Sopenharmony_ci 1, /** @type {number} */ (/** @type {*} */ (null)))) 875ffe3c632Sopenharmony_ci .toThrow(); 876ffe3c632Sopenharmony_ci } else { 877ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 878ffe3c632Sopenharmony_ci accessor.setFixed32(1, /** @type {number} */ (/** @type {*} */ (null))); 879ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toEqual(null); 880ffe3c632Sopenharmony_ci } 881ffe3c632Sopenharmony_ci }); 882ffe3c632Sopenharmony_ci 883ffe3c632Sopenharmony_ci it('throws in setter for negative value', () => { 884ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 885ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setFixed32(1, -1)).toThrow(); 886ffe3c632Sopenharmony_ci } else { 887ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 888ffe3c632Sopenharmony_ci accessor.setFixed32(1, -1); 889ffe3c632Sopenharmony_ci expect(accessor.getFixed32WithDefault(1)).toEqual(-1); 890ffe3c632Sopenharmony_ci } 891ffe3c632Sopenharmony_ci }); 892ffe3c632Sopenharmony_ci}); 893ffe3c632Sopenharmony_ci 894ffe3c632Sopenharmony_cidescribe('Fixed64 access', () => { 895ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 896ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 897ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(0)); 898ffe3c632Sopenharmony_ci }); 899ffe3c632Sopenharmony_ci 900ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 901ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 902ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(1, Int64.fromInt(2))) 903ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(2)); 904ffe3c632Sopenharmony_ci }); 905ffe3c632Sopenharmony_ci 906ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 907ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 908ffe3c632Sopenharmony_ci 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)); 909ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1)); 910ffe3c632Sopenharmony_ci }); 911ffe3c632Sopenharmony_ci 912ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 913ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 914ffe3c632Sopenharmony_ci 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x02, 0x00, 915ffe3c632Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)); 916ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(2)); 917ffe3c632Sopenharmony_ci }); 918ffe3c632Sopenharmony_ci 919ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_STATE) { 920ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 921ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer( 922ffe3c632Sopenharmony_ci createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00)); 923ffe3c632Sopenharmony_ci expect(() => { 924ffe3c632Sopenharmony_ci accessor.getFixed64WithDefault(1); 925ffe3c632Sopenharmony_ci }).toThrow(); 926ffe3c632Sopenharmony_ci }); 927ffe3c632Sopenharmony_ci } 928ffe3c632Sopenharmony_ci 929ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 930ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 931ffe3c632Sopenharmony_ci expect( 932ffe3c632Sopenharmony_ci () => 933ffe3c632Sopenharmony_ci Kernel.createEmpty().getFixed64WithDefault(-1, Int64.fromInt(1))) 934ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 935ffe3c632Sopenharmony_ci } else { 936ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getFixed64WithDefault(-1, Int64.fromInt(1))) 937ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(1)); 938ffe3c632Sopenharmony_ci } 939ffe3c632Sopenharmony_ci }); 940ffe3c632Sopenharmony_ci 941ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 942ffe3c632Sopenharmony_ci const bytes = 943ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 944ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 945ffe3c632Sopenharmony_ci accessor.setFixed64(1, Int64.fromInt(2)); 946ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(2)); 947ffe3c632Sopenharmony_ci }); 948ffe3c632Sopenharmony_ci 949ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 950ffe3c632Sopenharmony_ci const bytes = 951ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 952ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 953ffe3c632Sopenharmony_ci const newBytes = 954ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 955ffe3c632Sopenharmony_ci accessor.setFixed64(1, Int64.fromInt(0)); 956ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 957ffe3c632Sopenharmony_ci }); 958ffe3c632Sopenharmony_ci 959ffe3c632Sopenharmony_ci it('returns value from cache', () => { 960ffe3c632Sopenharmony_ci const bytes = 961ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 962ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 963ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1)); 964ffe3c632Sopenharmony_ci // Make sure the value is cached. 965ffe3c632Sopenharmony_ci bytes[2] = 0x00; 966ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1)); 967ffe3c632Sopenharmony_ci }); 968ffe3c632Sopenharmony_ci 969ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 970ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 971ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setFixed64(-1, Int64.fromInt(1))) 972ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 973ffe3c632Sopenharmony_ci } else { 974ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 975ffe3c632Sopenharmony_ci accessor.setFixed64(-1, Int64.fromInt(1)); 976ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(-1)).toEqual(Int64.fromInt(1)); 977ffe3c632Sopenharmony_ci } 978ffe3c632Sopenharmony_ci }); 979ffe3c632Sopenharmony_ci 980ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 981ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 982ffe3c632Sopenharmony_ci expect( 983ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setSfixed64( 984ffe3c632Sopenharmony_ci 1, /** @type {!Int64} */ (/** @type {*} */ (null)))) 985ffe3c632Sopenharmony_ci .toThrow(); 986ffe3c632Sopenharmony_ci } else { 987ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 988ffe3c632Sopenharmony_ci accessor.setFixed64(1, /** @type {!Int64} */ (/** @type {*} */ (null))); 989ffe3c632Sopenharmony_ci expect(accessor.getFixed64WithDefault(1)).toEqual(null); 990ffe3c632Sopenharmony_ci } 991ffe3c632Sopenharmony_ci }); 992ffe3c632Sopenharmony_ci}); 993ffe3c632Sopenharmony_ci 994ffe3c632Sopenharmony_cidescribe('Float access', () => { 995ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 996ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 997ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1)).toEqual(0); 998ffe3c632Sopenharmony_ci }); 999ffe3c632Sopenharmony_ci 1000ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1001ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1002ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1, 2)).toEqual(2); 1003ffe3c632Sopenharmony_ci }); 1004ffe3c632Sopenharmony_ci 1005ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1006ffe3c632Sopenharmony_ci const accessor = 1007ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F)); 1008ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1)).toEqual(1); 1009ffe3c632Sopenharmony_ci }); 1010ffe3c632Sopenharmony_ci 1011ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1012ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 1013ffe3c632Sopenharmony_ci 0x0D, 0x00, 0x00, 0x80, 0x3F, 0x0D, 0x00, 0x00, 0x80, 0xBF)); 1014ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1)).toEqual(-1); 1015ffe3c632Sopenharmony_ci }); 1016ffe3c632Sopenharmony_ci 1017ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_STATE) { 1018ffe3c632Sopenharmony_ci it('fails when getting float value with other wire types', () => { 1019ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 1020ffe3c632Sopenharmony_ci 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F)); 1021ffe3c632Sopenharmony_ci expect(() => { 1022ffe3c632Sopenharmony_ci accessor.getFloatWithDefault(1); 1023ffe3c632Sopenharmony_ci }).toThrow(); 1024ffe3c632Sopenharmony_ci }); 1025ffe3c632Sopenharmony_ci } 1026ffe3c632Sopenharmony_ci 1027ffe3c632Sopenharmony_ci 1028ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1029ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1030ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().getFloatWithDefault(-1, 1)) 1031ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1032ffe3c632Sopenharmony_ci } else { 1033ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getFloatWithDefault(-1, 1)).toEqual(1); 1034ffe3c632Sopenharmony_ci } 1035ffe3c632Sopenharmony_ci }); 1036ffe3c632Sopenharmony_ci 1037ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1038ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F); 1039ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1040ffe3c632Sopenharmony_ci accessor.setFloat(1, 1.6); 1041ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1)).toEqual(Math.fround(1.6)); 1042ffe3c632Sopenharmony_ci }); 1043ffe3c632Sopenharmony_ci 1044ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1045ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F); 1046ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1047ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00); 1048ffe3c632Sopenharmony_ci accessor.setFloat(1, 0); 1049ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1050ffe3c632Sopenharmony_ci }); 1051ffe3c632Sopenharmony_ci 1052ffe3c632Sopenharmony_ci it('returns float value from cache', () => { 1053ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F); 1054ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1055ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1)).toBe(1); 1056ffe3c632Sopenharmony_ci // Make sure the value is cached. 1057ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1058ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1)).toBe(1); 1059ffe3c632Sopenharmony_ci }); 1060ffe3c632Sopenharmony_ci 1061ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1062ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1063ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setFloat(-1, 1)) 1064ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1065ffe3c632Sopenharmony_ci } else { 1066ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1067ffe3c632Sopenharmony_ci accessor.setFloat(-1, 1); 1068ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(-1)).toEqual(1); 1069ffe3c632Sopenharmony_ci } 1070ffe3c632Sopenharmony_ci }); 1071ffe3c632Sopenharmony_ci 1072ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1073ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1074ffe3c632Sopenharmony_ci expect( 1075ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setFloat( 1076ffe3c632Sopenharmony_ci 1, /** @type {number} */ (/** @type {*} */ (null)))) 1077ffe3c632Sopenharmony_ci .toThrow(); 1078ffe3c632Sopenharmony_ci } else { 1079ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1080ffe3c632Sopenharmony_ci accessor.setFloat(1, /** @type {number} */ (/** @type {*} */ (null))); 1081ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1)).toEqual(0); 1082ffe3c632Sopenharmony_ci } 1083ffe3c632Sopenharmony_ci }); 1084ffe3c632Sopenharmony_ci 1085ffe3c632Sopenharmony_ci it('throws in setter for value outside of float32 precision', () => { 1086ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1087ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setFloat(1, Number.MAX_VALUE)) 1088ffe3c632Sopenharmony_ci .toThrow(); 1089ffe3c632Sopenharmony_ci } else { 1090ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1091ffe3c632Sopenharmony_ci accessor.setFloat(1, Number.MAX_VALUE); 1092ffe3c632Sopenharmony_ci expect(accessor.getFloatWithDefault(1)).toEqual(Infinity); 1093ffe3c632Sopenharmony_ci } 1094ffe3c632Sopenharmony_ci }); 1095ffe3c632Sopenharmony_ci}); 1096ffe3c632Sopenharmony_ci 1097ffe3c632Sopenharmony_cidescribe('Int32 access', () => { 1098ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1099ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1100ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1)).toEqual(0); 1101ffe3c632Sopenharmony_ci }); 1102ffe3c632Sopenharmony_ci 1103ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1104ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1105ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1, 2)).toEqual(2); 1106ffe3c632Sopenharmony_ci }); 1107ffe3c632Sopenharmony_ci 1108ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1109ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01)); 1110ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1)).toEqual(1); 1111ffe3c632Sopenharmony_ci }); 1112ffe3c632Sopenharmony_ci 1113ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1114ffe3c632Sopenharmony_ci const accessor = 1115ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02)); 1116ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1)).toEqual(2); 1117ffe3c632Sopenharmony_ci }); 1118ffe3c632Sopenharmony_ci 1119ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 1120ffe3c632Sopenharmony_ci const accessor = 1121ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00)); 1122ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1123ffe3c632Sopenharmony_ci expect(() => { 1124ffe3c632Sopenharmony_ci accessor.getInt32WithDefault(1); 1125ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 0 but found: 5'); 1126ffe3c632Sopenharmony_ci } else { 1127ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 1128ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 1129ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 1130ffe3c632Sopenharmony_ci // what the implementation should be doing here. 1131ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1)).toEqual(0); 1132ffe3c632Sopenharmony_ci } 1133ffe3c632Sopenharmony_ci }); 1134ffe3c632Sopenharmony_ci 1135ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1136ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1137ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().getInt32WithDefault(-1, 1)) 1138ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1139ffe3c632Sopenharmony_ci } else { 1140ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getInt32WithDefault(-1, 1)).toEqual(1); 1141ffe3c632Sopenharmony_ci } 1142ffe3c632Sopenharmony_ci }); 1143ffe3c632Sopenharmony_ci 1144ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1145ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1146ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1147ffe3c632Sopenharmony_ci accessor.setInt32(1, 2); 1148ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1)).toEqual(2); 1149ffe3c632Sopenharmony_ci }); 1150ffe3c632Sopenharmony_ci 1151ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1152ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1153ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1154ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x08, 0x00); 1155ffe3c632Sopenharmony_ci accessor.setInt32(1, 0); 1156ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1157ffe3c632Sopenharmony_ci }); 1158ffe3c632Sopenharmony_ci 1159ffe3c632Sopenharmony_ci it('returns value from cache', () => { 1160ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1161ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1162ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1)).toBe(1); 1163ffe3c632Sopenharmony_ci // Make sure the value is cached. 1164ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1165ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1)).toBe(1); 1166ffe3c632Sopenharmony_ci }); 1167ffe3c632Sopenharmony_ci 1168ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1169ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1170ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setInt32(-1, 1)) 1171ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1172ffe3c632Sopenharmony_ci } else { 1173ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1174ffe3c632Sopenharmony_ci accessor.setInt32(-1, 1); 1175ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(-1)).toEqual(1); 1176ffe3c632Sopenharmony_ci } 1177ffe3c632Sopenharmony_ci }); 1178ffe3c632Sopenharmony_ci 1179ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1180ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1181ffe3c632Sopenharmony_ci expect( 1182ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setInt32( 1183ffe3c632Sopenharmony_ci 1, /** @type {number} */ (/** @type {*} */ (null)))) 1184ffe3c632Sopenharmony_ci .toThrow(); 1185ffe3c632Sopenharmony_ci } else { 1186ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1187ffe3c632Sopenharmony_ci accessor.setInt32(1, /** @type {number} */ (/** @type {*} */ (null))); 1188ffe3c632Sopenharmony_ci expect(accessor.getInt32WithDefault(1)).toEqual(null); 1189ffe3c632Sopenharmony_ci } 1190ffe3c632Sopenharmony_ci }); 1191ffe3c632Sopenharmony_ci}); 1192ffe3c632Sopenharmony_ci 1193ffe3c632Sopenharmony_cidescribe('Int64 access', () => { 1194ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1195ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1196ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(0)); 1197ffe3c632Sopenharmony_ci }); 1198ffe3c632Sopenharmony_ci 1199ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1200ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1201ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1, Int64.fromInt(2))) 1202ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(2)); 1203ffe3c632Sopenharmony_ci }); 1204ffe3c632Sopenharmony_ci 1205ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1206ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01)); 1207ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1)); 1208ffe3c632Sopenharmony_ci }); 1209ffe3c632Sopenharmony_ci 1210ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1211ffe3c632Sopenharmony_ci const accessor = 1212ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02)); 1213ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(2)); 1214ffe3c632Sopenharmony_ci }); 1215ffe3c632Sopenharmony_ci 1216ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 1217ffe3c632Sopenharmony_ci const accessor = 1218ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00)); 1219ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1220ffe3c632Sopenharmony_ci expect(() => { 1221ffe3c632Sopenharmony_ci accessor.getInt64WithDefault(1); 1222ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 0 but found: 5'); 1223ffe3c632Sopenharmony_ci } else { 1224ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 1225ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 1226ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 1227ffe3c632Sopenharmony_ci // what the implementation should be doing here. 1228ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(0)); 1229ffe3c632Sopenharmony_ci } 1230ffe3c632Sopenharmony_ci }); 1231ffe3c632Sopenharmony_ci 1232ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1233ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1234ffe3c632Sopenharmony_ci expect( 1235ffe3c632Sopenharmony_ci () => Kernel.createEmpty().getInt64WithDefault(-1, Int64.fromInt(1))) 1236ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1237ffe3c632Sopenharmony_ci } else { 1238ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getInt64WithDefault(-1, Int64.fromInt(1))) 1239ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(1)); 1240ffe3c632Sopenharmony_ci } 1241ffe3c632Sopenharmony_ci }); 1242ffe3c632Sopenharmony_ci 1243ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1244ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1245ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1246ffe3c632Sopenharmony_ci accessor.setInt64(1, Int64.fromInt(2)); 1247ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(2)); 1248ffe3c632Sopenharmony_ci }); 1249ffe3c632Sopenharmony_ci 1250ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1251ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1252ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1253ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x08, 0x00); 1254ffe3c632Sopenharmony_ci accessor.setInt64(1, Int64.fromInt(0)); 1255ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1256ffe3c632Sopenharmony_ci }); 1257ffe3c632Sopenharmony_ci 1258ffe3c632Sopenharmony_ci it('returns value from cache', () => { 1259ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1260ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1261ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1)); 1262ffe3c632Sopenharmony_ci // Make sure the value is cached. 1263ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1264ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1)); 1265ffe3c632Sopenharmony_ci }); 1266ffe3c632Sopenharmony_ci 1267ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1268ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1269ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setInt64(-1, Int64.fromInt(1))) 1270ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1271ffe3c632Sopenharmony_ci } else { 1272ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1273ffe3c632Sopenharmony_ci accessor.setInt64(-1, Int64.fromInt(1)); 1274ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(-1)).toEqual(Int64.fromInt(1)); 1275ffe3c632Sopenharmony_ci } 1276ffe3c632Sopenharmony_ci }); 1277ffe3c632Sopenharmony_ci 1278ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1279ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1280ffe3c632Sopenharmony_ci expect( 1281ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setInt64( 1282ffe3c632Sopenharmony_ci 1, /** @type {!Int64} */ (/** @type {*} */ (null)))) 1283ffe3c632Sopenharmony_ci .toThrow(); 1284ffe3c632Sopenharmony_ci } else { 1285ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1286ffe3c632Sopenharmony_ci accessor.setInt64(1, /** @type {!Int64} */ (/** @type {*} */ (null))); 1287ffe3c632Sopenharmony_ci expect(accessor.getInt64WithDefault(1)).toEqual(null); 1288ffe3c632Sopenharmony_ci } 1289ffe3c632Sopenharmony_ci }); 1290ffe3c632Sopenharmony_ci}); 1291ffe3c632Sopenharmony_ci 1292ffe3c632Sopenharmony_cidescribe('Sfixed32 access', () => { 1293ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1294ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1295ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1)).toEqual(0); 1296ffe3c632Sopenharmony_ci }); 1297ffe3c632Sopenharmony_ci 1298ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1299ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1300ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1, 2)).toEqual(2); 1301ffe3c632Sopenharmony_ci }); 1302ffe3c632Sopenharmony_ci 1303ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1304ffe3c632Sopenharmony_ci const accessor = 1305ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00)); 1306ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1)).toEqual(1); 1307ffe3c632Sopenharmony_ci }); 1308ffe3c632Sopenharmony_ci 1309ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1310ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 1311ffe3c632Sopenharmony_ci 0x0D, 0x01, 0x00, 0x80, 0x00, 0x0D, 0x02, 0x00, 0x00, 0x00)); 1312ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1)).toEqual(2); 1313ffe3c632Sopenharmony_ci }); 1314ffe3c632Sopenharmony_ci 1315ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 1316ffe3c632Sopenharmony_ci const accessor = 1317ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00)); 1318ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1319ffe3c632Sopenharmony_ci expect(() => { 1320ffe3c632Sopenharmony_ci accessor.getSfixed32WithDefault(1); 1321ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 5 but found: 0'); 1322ffe3c632Sopenharmony_ci } else { 1323ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 1324ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 1325ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 1326ffe3c632Sopenharmony_ci // what the implementation should be doing here. 1327ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1)).toEqual(8421504); 1328ffe3c632Sopenharmony_ci } 1329ffe3c632Sopenharmony_ci }); 1330ffe3c632Sopenharmony_ci 1331ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1332ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1333ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().getSfixed32WithDefault(-1, 1)) 1334ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1335ffe3c632Sopenharmony_ci } else { 1336ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getSfixed32WithDefault(-1, 1)).toEqual(1); 1337ffe3c632Sopenharmony_ci } 1338ffe3c632Sopenharmony_ci }); 1339ffe3c632Sopenharmony_ci 1340ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1341ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00); 1342ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1343ffe3c632Sopenharmony_ci accessor.setSfixed32(1, 2); 1344ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1)).toEqual(2); 1345ffe3c632Sopenharmony_ci }); 1346ffe3c632Sopenharmony_ci 1347ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1348ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00); 1349ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1350ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00); 1351ffe3c632Sopenharmony_ci accessor.setSfixed32(1, 0); 1352ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1353ffe3c632Sopenharmony_ci }); 1354ffe3c632Sopenharmony_ci 1355ffe3c632Sopenharmony_ci it('returns value from cache', () => { 1356ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00); 1357ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1358ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1)).toBe(1); 1359ffe3c632Sopenharmony_ci // Make sure the value is cached. 1360ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1361ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1)).toBe(1); 1362ffe3c632Sopenharmony_ci }); 1363ffe3c632Sopenharmony_ci 1364ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1365ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1366ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setSfixed32(-1, 1)) 1367ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1368ffe3c632Sopenharmony_ci } else { 1369ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1370ffe3c632Sopenharmony_ci accessor.setSfixed32(-1, 1); 1371ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(-1)).toEqual(1); 1372ffe3c632Sopenharmony_ci } 1373ffe3c632Sopenharmony_ci }); 1374ffe3c632Sopenharmony_ci 1375ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1376ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1377ffe3c632Sopenharmony_ci expect( 1378ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setSfixed32( 1379ffe3c632Sopenharmony_ci 1, /** @type {number} */ (/** @type {*} */ (null)))) 1380ffe3c632Sopenharmony_ci .toThrow(); 1381ffe3c632Sopenharmony_ci } else { 1382ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1383ffe3c632Sopenharmony_ci accessor.setSfixed32(1, /** @type {number} */ (/** @type {*} */ (null))); 1384ffe3c632Sopenharmony_ci expect(accessor.getSfixed32WithDefault(1)).toEqual(null); 1385ffe3c632Sopenharmony_ci } 1386ffe3c632Sopenharmony_ci }); 1387ffe3c632Sopenharmony_ci}); 1388ffe3c632Sopenharmony_ci 1389ffe3c632Sopenharmony_cidescribe('Sfixed64 access', () => { 1390ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1391ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1392ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(0)); 1393ffe3c632Sopenharmony_ci }); 1394ffe3c632Sopenharmony_ci 1395ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1396ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1397ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(1, Int64.fromInt(2))) 1398ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(2)); 1399ffe3c632Sopenharmony_ci }); 1400ffe3c632Sopenharmony_ci 1401ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1402ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 1403ffe3c632Sopenharmony_ci 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)); 1404ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1)); 1405ffe3c632Sopenharmony_ci }); 1406ffe3c632Sopenharmony_ci 1407ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1408ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 1409ffe3c632Sopenharmony_ci 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x02, 0x00, 1410ffe3c632Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)); 1411ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(2)); 1412ffe3c632Sopenharmony_ci }); 1413ffe3c632Sopenharmony_ci 1414ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_STATE) { 1415ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 1416ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer( 1417ffe3c632Sopenharmony_ci createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00)); 1418ffe3c632Sopenharmony_ci expect(() => { 1419ffe3c632Sopenharmony_ci accessor.getSfixed64WithDefault(1); 1420ffe3c632Sopenharmony_ci }).toThrow(); 1421ffe3c632Sopenharmony_ci }); 1422ffe3c632Sopenharmony_ci } 1423ffe3c632Sopenharmony_ci 1424ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1425ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1426ffe3c632Sopenharmony_ci expect( 1427ffe3c632Sopenharmony_ci () => 1428ffe3c632Sopenharmony_ci Kernel.createEmpty().getSfixed64WithDefault(-1, Int64.fromInt(1))) 1429ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1430ffe3c632Sopenharmony_ci } else { 1431ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getSfixed64WithDefault(-1, Int64.fromInt(1))) 1432ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(1)); 1433ffe3c632Sopenharmony_ci } 1434ffe3c632Sopenharmony_ci }); 1435ffe3c632Sopenharmony_ci 1436ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1437ffe3c632Sopenharmony_ci const bytes = 1438ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 1439ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1440ffe3c632Sopenharmony_ci accessor.setSfixed64(1, Int64.fromInt(2)); 1441ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(2)); 1442ffe3c632Sopenharmony_ci }); 1443ffe3c632Sopenharmony_ci 1444ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1445ffe3c632Sopenharmony_ci const bytes = 1446ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 1447ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1448ffe3c632Sopenharmony_ci const newBytes = 1449ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 1450ffe3c632Sopenharmony_ci accessor.setSfixed64(1, Int64.fromInt(0)); 1451ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1452ffe3c632Sopenharmony_ci }); 1453ffe3c632Sopenharmony_ci 1454ffe3c632Sopenharmony_ci it('returns value from cache', () => { 1455ffe3c632Sopenharmony_ci const bytes = 1456ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 1457ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1458ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1)); 1459ffe3c632Sopenharmony_ci // Make sure the value is cached. 1460ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1461ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1)); 1462ffe3c632Sopenharmony_ci }); 1463ffe3c632Sopenharmony_ci 1464ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1465ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1466ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setSfixed64(-1, Int64.fromInt(1))) 1467ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1468ffe3c632Sopenharmony_ci } else { 1469ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1470ffe3c632Sopenharmony_ci accessor.setSfixed64(-1, Int64.fromInt(1)); 1471ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(-1)).toEqual(Int64.fromInt(1)); 1472ffe3c632Sopenharmony_ci } 1473ffe3c632Sopenharmony_ci }); 1474ffe3c632Sopenharmony_ci 1475ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1476ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1477ffe3c632Sopenharmony_ci expect( 1478ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setSfixed64( 1479ffe3c632Sopenharmony_ci 1, /** @type {!Int64} */ (/** @type {*} */ (null)))) 1480ffe3c632Sopenharmony_ci .toThrow(); 1481ffe3c632Sopenharmony_ci } else { 1482ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1483ffe3c632Sopenharmony_ci accessor.setSfixed64(1, /** @type {!Int64} */ (/** @type {*} */ (null))); 1484ffe3c632Sopenharmony_ci expect(accessor.getSfixed64WithDefault(1)).toEqual(null); 1485ffe3c632Sopenharmony_ci } 1486ffe3c632Sopenharmony_ci }); 1487ffe3c632Sopenharmony_ci}); 1488ffe3c632Sopenharmony_ci 1489ffe3c632Sopenharmony_cidescribe('Sint32 access', () => { 1490ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1491ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1492ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1)).toEqual(0); 1493ffe3c632Sopenharmony_ci }); 1494ffe3c632Sopenharmony_ci 1495ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1496ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1497ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1, 2)).toEqual(2); 1498ffe3c632Sopenharmony_ci }); 1499ffe3c632Sopenharmony_ci 1500ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1501ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x02)); 1502ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1)).toEqual(1); 1503ffe3c632Sopenharmony_ci }); 1504ffe3c632Sopenharmony_ci 1505ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1506ffe3c632Sopenharmony_ci const accessor = 1507ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x03, 0x08, 0x02)); 1508ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1)).toEqual(1); 1509ffe3c632Sopenharmony_ci }); 1510ffe3c632Sopenharmony_ci 1511ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 1512ffe3c632Sopenharmony_ci const accessor = 1513ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00)); 1514ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1515ffe3c632Sopenharmony_ci expect(() => { 1516ffe3c632Sopenharmony_ci accessor.getSint32WithDefault(1); 1517ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 0 but found: 5'); 1518ffe3c632Sopenharmony_ci } else { 1519ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 1520ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 1521ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 1522ffe3c632Sopenharmony_ci // what the implementation should be doing here. 1523ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1)).toEqual(0); 1524ffe3c632Sopenharmony_ci } 1525ffe3c632Sopenharmony_ci }); 1526ffe3c632Sopenharmony_ci 1527ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1528ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1529ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().getSint32WithDefault(-1, 1)) 1530ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1531ffe3c632Sopenharmony_ci } else { 1532ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getSint32WithDefault(-1, 1)).toEqual(1); 1533ffe3c632Sopenharmony_ci } 1534ffe3c632Sopenharmony_ci }); 1535ffe3c632Sopenharmony_ci 1536ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1537ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1538ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1539ffe3c632Sopenharmony_ci accessor.setSint32(1, 2); 1540ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1)).toEqual(2); 1541ffe3c632Sopenharmony_ci }); 1542ffe3c632Sopenharmony_ci 1543ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1544ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1545ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1546ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x08, 0x00); 1547ffe3c632Sopenharmony_ci accessor.setSint32(1, 0); 1548ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1549ffe3c632Sopenharmony_ci }); 1550ffe3c632Sopenharmony_ci 1551ffe3c632Sopenharmony_ci it('returns value from cache', () => { 1552ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x02); 1553ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1554ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1)).toBe(1); 1555ffe3c632Sopenharmony_ci // Make sure the value is cached. 1556ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1557ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1)).toBe(1); 1558ffe3c632Sopenharmony_ci }); 1559ffe3c632Sopenharmony_ci 1560ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1561ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1562ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setSint32(-1, 1)) 1563ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1564ffe3c632Sopenharmony_ci } else { 1565ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1566ffe3c632Sopenharmony_ci accessor.setSint32(-1, 1); 1567ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(-1)).toEqual(1); 1568ffe3c632Sopenharmony_ci } 1569ffe3c632Sopenharmony_ci }); 1570ffe3c632Sopenharmony_ci 1571ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1572ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1573ffe3c632Sopenharmony_ci expect( 1574ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setSint32( 1575ffe3c632Sopenharmony_ci 1, /** @type {number} */ (/** @type {*} */ (null)))) 1576ffe3c632Sopenharmony_ci .toThrow(); 1577ffe3c632Sopenharmony_ci } else { 1578ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1579ffe3c632Sopenharmony_ci accessor.setSint32(1, /** @type {number} */ (/** @type {*} */ (null))); 1580ffe3c632Sopenharmony_ci expect(accessor.getSint32WithDefault(1)).toEqual(null); 1581ffe3c632Sopenharmony_ci } 1582ffe3c632Sopenharmony_ci }); 1583ffe3c632Sopenharmony_ci}); 1584ffe3c632Sopenharmony_ci 1585ffe3c632Sopenharmony_cidescribe('SInt64 access', () => { 1586ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1587ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1588ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(0)); 1589ffe3c632Sopenharmony_ci }); 1590ffe3c632Sopenharmony_ci 1591ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1592ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1593ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1, Int64.fromInt(2))) 1594ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(2)); 1595ffe3c632Sopenharmony_ci }); 1596ffe3c632Sopenharmony_ci 1597ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1598ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x02)); 1599ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1)); 1600ffe3c632Sopenharmony_ci }); 1601ffe3c632Sopenharmony_ci 1602ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1603ffe3c632Sopenharmony_ci const accessor = 1604ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02)); 1605ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1)); 1606ffe3c632Sopenharmony_ci }); 1607ffe3c632Sopenharmony_ci 1608ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 1609ffe3c632Sopenharmony_ci const accessor = 1610ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00)); 1611ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1612ffe3c632Sopenharmony_ci expect(() => { 1613ffe3c632Sopenharmony_ci accessor.getSint64WithDefault(1); 1614ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 0 but found: 5'); 1615ffe3c632Sopenharmony_ci } else { 1616ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 1617ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 1618ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 1619ffe3c632Sopenharmony_ci // what the implementation should be doing here. 1620ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(0)); 1621ffe3c632Sopenharmony_ci } 1622ffe3c632Sopenharmony_ci }); 1623ffe3c632Sopenharmony_ci 1624ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1625ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1626ffe3c632Sopenharmony_ci expect( 1627ffe3c632Sopenharmony_ci () => Kernel.createEmpty().getSint64WithDefault(-1, Int64.fromInt(1))) 1628ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1629ffe3c632Sopenharmony_ci } else { 1630ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getSint64WithDefault(-1, Int64.fromInt(1))) 1631ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(1)); 1632ffe3c632Sopenharmony_ci } 1633ffe3c632Sopenharmony_ci }); 1634ffe3c632Sopenharmony_ci 1635ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1636ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1637ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1638ffe3c632Sopenharmony_ci accessor.setSint64(1, Int64.fromInt(2)); 1639ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(2)); 1640ffe3c632Sopenharmony_ci }); 1641ffe3c632Sopenharmony_ci 1642ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1643ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1644ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1645ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x08, 0x00); 1646ffe3c632Sopenharmony_ci accessor.setSint64(1, Int64.fromInt(0)); 1647ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1648ffe3c632Sopenharmony_ci }); 1649ffe3c632Sopenharmony_ci 1650ffe3c632Sopenharmony_ci it('returns value from cache', () => { 1651ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x02); 1652ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1653ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1)); 1654ffe3c632Sopenharmony_ci // Make sure the value is cached. 1655ffe3c632Sopenharmony_ci bytes[1] = 0x00; 1656ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1)); 1657ffe3c632Sopenharmony_ci }); 1658ffe3c632Sopenharmony_ci 1659ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1660ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1661ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setSint64(-1, Int64.fromInt(1))) 1662ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1663ffe3c632Sopenharmony_ci } else { 1664ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1665ffe3c632Sopenharmony_ci accessor.setInt64(-1, Int64.fromInt(1)); 1666ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(-1)).toEqual(Int64.fromInt(1)); 1667ffe3c632Sopenharmony_ci } 1668ffe3c632Sopenharmony_ci }); 1669ffe3c632Sopenharmony_ci 1670ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1671ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1672ffe3c632Sopenharmony_ci expect( 1673ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setSint64( 1674ffe3c632Sopenharmony_ci 1, /** @type {!Int64} */ (/** @type {*} */ (null)))) 1675ffe3c632Sopenharmony_ci .toThrow(); 1676ffe3c632Sopenharmony_ci } else { 1677ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1678ffe3c632Sopenharmony_ci accessor.setSint64(1, /** @type {!Int64} */ (/** @type {*} */ (null))); 1679ffe3c632Sopenharmony_ci expect(accessor.getSint64WithDefault(1)).toEqual(null); 1680ffe3c632Sopenharmony_ci } 1681ffe3c632Sopenharmony_ci }); 1682ffe3c632Sopenharmony_ci}); 1683ffe3c632Sopenharmony_ci 1684ffe3c632Sopenharmony_cidescribe('String access', () => { 1685ffe3c632Sopenharmony_ci it('returns empty string for the empty input', () => { 1686ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1687ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(1)).toEqual(''); 1688ffe3c632Sopenharmony_ci }); 1689ffe3c632Sopenharmony_ci 1690ffe3c632Sopenharmony_ci it('returns the default for the empty input', () => { 1691ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1692ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(1, 'bar')).toEqual('bar'); 1693ffe3c632Sopenharmony_ci }); 1694ffe3c632Sopenharmony_ci 1695ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1696ffe3c632Sopenharmony_ci const accessor = 1697ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x61)); 1698ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(1)).toEqual('a'); 1699ffe3c632Sopenharmony_ci }); 1700ffe3c632Sopenharmony_ci 1701ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1702ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer( 1703ffe3c632Sopenharmony_ci createArrayBuffer(0x0A, 0x01, 0x60, 0x0A, 0x01, 0x61)); 1704ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(1)).toEqual('a'); 1705ffe3c632Sopenharmony_ci }); 1706ffe3c632Sopenharmony_ci 1707ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_STATE) { 1708ffe3c632Sopenharmony_ci it('fails when getting string value with other wire types', () => { 1709ffe3c632Sopenharmony_ci const accessor = 1710ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x02, 0x08, 0x08)); 1711ffe3c632Sopenharmony_ci expect(() => { 1712ffe3c632Sopenharmony_ci accessor.getStringWithDefault(1); 1713ffe3c632Sopenharmony_ci }).toThrow(); 1714ffe3c632Sopenharmony_ci }); 1715ffe3c632Sopenharmony_ci } 1716ffe3c632Sopenharmony_ci 1717ffe3c632Sopenharmony_ci 1718ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1719ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1720ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().getStringWithDefault(-1, 'a')) 1721ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1722ffe3c632Sopenharmony_ci } else { 1723ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getStringWithDefault(-1, 'a')).toEqual('a'); 1724ffe3c632Sopenharmony_ci } 1725ffe3c632Sopenharmony_ci }); 1726ffe3c632Sopenharmony_ci 1727ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1728ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x01, 0x61); 1729ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1730ffe3c632Sopenharmony_ci accessor.setString(1, 'b'); 1731ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(1)).toEqual('b'); 1732ffe3c632Sopenharmony_ci }); 1733ffe3c632Sopenharmony_ci 1734ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1735ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x01, 0x61); 1736ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1737ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x0A, 0x01, 0x62); 1738ffe3c632Sopenharmony_ci accessor.setString(1, 'b'); 1739ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1740ffe3c632Sopenharmony_ci }); 1741ffe3c632Sopenharmony_ci 1742ffe3c632Sopenharmony_ci it('returns string value from cache', () => { 1743ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0A, 0x01, 0x61); 1744ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1745ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(1)).toBe('a'); 1746ffe3c632Sopenharmony_ci // Make sure the value is cached. 1747ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1748ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(1)).toBe('a'); 1749ffe3c632Sopenharmony_ci }); 1750ffe3c632Sopenharmony_ci 1751ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1752ffe3c632Sopenharmony_ci if (CHECK_TYPE) { 1753ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setString(-1, 'a')) 1754ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1755ffe3c632Sopenharmony_ci } else { 1756ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1757ffe3c632Sopenharmony_ci accessor.setString(-1, 'a'); 1758ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(-1)).toEqual('a'); 1759ffe3c632Sopenharmony_ci } 1760ffe3c632Sopenharmony_ci }); 1761ffe3c632Sopenharmony_ci 1762ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1763ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1764ffe3c632Sopenharmony_ci expect( 1765ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setString( 1766ffe3c632Sopenharmony_ci 1, /** @type {string} */ (/** @type {*} */ (null)))) 1767ffe3c632Sopenharmony_ci .toThrowError('Must be string, but got: null'); 1768ffe3c632Sopenharmony_ci } else { 1769ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1770ffe3c632Sopenharmony_ci accessor.setString(1, /** @type {string} */ (/** @type {*} */ (null))); 1771ffe3c632Sopenharmony_ci expect(accessor.getStringWithDefault(1)).toEqual(null); 1772ffe3c632Sopenharmony_ci } 1773ffe3c632Sopenharmony_ci }); 1774ffe3c632Sopenharmony_ci}); 1775ffe3c632Sopenharmony_ci 1776ffe3c632Sopenharmony_cidescribe('Uint32 access', () => { 1777ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1778ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1779ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toEqual(0); 1780ffe3c632Sopenharmony_ci }); 1781ffe3c632Sopenharmony_ci 1782ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1783ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1784ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1, 2)).toEqual(2); 1785ffe3c632Sopenharmony_ci }); 1786ffe3c632Sopenharmony_ci 1787ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1788ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01)); 1789ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toEqual(1); 1790ffe3c632Sopenharmony_ci }); 1791ffe3c632Sopenharmony_ci 1792ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1793ffe3c632Sopenharmony_ci const accessor = 1794ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02)); 1795ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toEqual(2); 1796ffe3c632Sopenharmony_ci }); 1797ffe3c632Sopenharmony_ci 1798ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 1799ffe3c632Sopenharmony_ci const accessor = 1800ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00)); 1801ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1802ffe3c632Sopenharmony_ci expect(() => { 1803ffe3c632Sopenharmony_ci accessor.getUint32WithDefault(1); 1804ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 0 but found: 5'); 1805ffe3c632Sopenharmony_ci } else { 1806ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 1807ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 1808ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 1809ffe3c632Sopenharmony_ci // what the implementation should be doing here. 1810ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toEqual(0); 1811ffe3c632Sopenharmony_ci } 1812ffe3c632Sopenharmony_ci }); 1813ffe3c632Sopenharmony_ci 1814ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1815ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1816ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().getUint32WithDefault(-1, 1)) 1817ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1818ffe3c632Sopenharmony_ci } else { 1819ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getUint32WithDefault(-1, 1)).toEqual(1); 1820ffe3c632Sopenharmony_ci } 1821ffe3c632Sopenharmony_ci }); 1822ffe3c632Sopenharmony_ci 1823ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1824ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1825ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1826ffe3c632Sopenharmony_ci accessor.setUint32(1, 2); 1827ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toEqual(2); 1828ffe3c632Sopenharmony_ci }); 1829ffe3c632Sopenharmony_ci 1830ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1831ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1832ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1833ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x08, 0x00); 1834ffe3c632Sopenharmony_ci accessor.setUint32(1, 0); 1835ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1836ffe3c632Sopenharmony_ci }); 1837ffe3c632Sopenharmony_ci 1838ffe3c632Sopenharmony_ci it('returns value from cache', () => { 1839ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1840ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1841ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toBe(1); 1842ffe3c632Sopenharmony_ci // Make sure the value is cached. 1843ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1844ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toBe(1); 1845ffe3c632Sopenharmony_ci }); 1846ffe3c632Sopenharmony_ci 1847ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1848ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1849ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setInt32(-1, 1)) 1850ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1851ffe3c632Sopenharmony_ci } else { 1852ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1853ffe3c632Sopenharmony_ci accessor.setUint32(-1, 1); 1854ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(-1)).toEqual(1); 1855ffe3c632Sopenharmony_ci } 1856ffe3c632Sopenharmony_ci }); 1857ffe3c632Sopenharmony_ci 1858ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1859ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1860ffe3c632Sopenharmony_ci expect( 1861ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setUint32( 1862ffe3c632Sopenharmony_ci 1, /** @type {number} */ (/** @type {*} */ (null)))) 1863ffe3c632Sopenharmony_ci .toThrow(); 1864ffe3c632Sopenharmony_ci } else { 1865ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1866ffe3c632Sopenharmony_ci accessor.setUint32(1, /** @type {number} */ (/** @type {*} */ (null))); 1867ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toEqual(null); 1868ffe3c632Sopenharmony_ci } 1869ffe3c632Sopenharmony_ci }); 1870ffe3c632Sopenharmony_ci 1871ffe3c632Sopenharmony_ci it('throws in setter for negative value', () => { 1872ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1873ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setUint32(1, -1)).toThrow(); 1874ffe3c632Sopenharmony_ci } else { 1875ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1876ffe3c632Sopenharmony_ci accessor.setUint32(1, -1); 1877ffe3c632Sopenharmony_ci expect(accessor.getUint32WithDefault(1)).toEqual(-1); 1878ffe3c632Sopenharmony_ci } 1879ffe3c632Sopenharmony_ci }); 1880ffe3c632Sopenharmony_ci}); 1881ffe3c632Sopenharmony_ci 1882ffe3c632Sopenharmony_cidescribe('Uint64 access', () => { 1883ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1884ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1885ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(0)); 1886ffe3c632Sopenharmony_ci }); 1887ffe3c632Sopenharmony_ci 1888ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1889ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1890ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1, Int64.fromInt(2))) 1891ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(2)); 1892ffe3c632Sopenharmony_ci }); 1893ffe3c632Sopenharmony_ci 1894ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1895ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01)); 1896ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1)); 1897ffe3c632Sopenharmony_ci }); 1898ffe3c632Sopenharmony_ci 1899ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 1900ffe3c632Sopenharmony_ci const accessor = 1901ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02)); 1902ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(2)); 1903ffe3c632Sopenharmony_ci }); 1904ffe3c632Sopenharmony_ci 1905ffe3c632Sopenharmony_ci it('fails when getting value with other wire types', () => { 1906ffe3c632Sopenharmony_ci const accessor = 1907ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00)); 1908ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1909ffe3c632Sopenharmony_ci expect(() => { 1910ffe3c632Sopenharmony_ci accessor.getUint64WithDefault(1); 1911ffe3c632Sopenharmony_ci }).toThrowError('Expected wire type: 0 but found: 5'); 1912ffe3c632Sopenharmony_ci } else { 1913ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 1914ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 1915ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 1916ffe3c632Sopenharmony_ci // what the implementation should be doing here. 1917ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(0)); 1918ffe3c632Sopenharmony_ci } 1919ffe3c632Sopenharmony_ci }); 1920ffe3c632Sopenharmony_ci 1921ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 1922ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1923ffe3c632Sopenharmony_ci expect( 1924ffe3c632Sopenharmony_ci () => Kernel.createEmpty().getUint64WithDefault(-1, Int64.fromInt(1))) 1925ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1926ffe3c632Sopenharmony_ci } else { 1927ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getUint64WithDefault(-1, Int64.fromInt(1))) 1928ffe3c632Sopenharmony_ci .toEqual(Int64.fromInt(1)); 1929ffe3c632Sopenharmony_ci } 1930ffe3c632Sopenharmony_ci }); 1931ffe3c632Sopenharmony_ci 1932ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 1933ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1934ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1935ffe3c632Sopenharmony_ci accessor.setUint64(1, Int64.fromInt(2)); 1936ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(2)); 1937ffe3c632Sopenharmony_ci }); 1938ffe3c632Sopenharmony_ci 1939ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 1940ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1941ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1942ffe3c632Sopenharmony_ci const newBytes = createArrayBuffer(0x08, 0x00); 1943ffe3c632Sopenharmony_ci accessor.setUint64(1, Int64.fromInt(0)); 1944ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 1945ffe3c632Sopenharmony_ci }); 1946ffe3c632Sopenharmony_ci 1947ffe3c632Sopenharmony_ci it('returns value from cache', () => { 1948ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 1949ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 1950ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1)); 1951ffe3c632Sopenharmony_ci // Make sure the value is cached. 1952ffe3c632Sopenharmony_ci bytes[2] = 0x00; 1953ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1)); 1954ffe3c632Sopenharmony_ci }); 1955ffe3c632Sopenharmony_ci 1956ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 1957ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 1958ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setUint64(-1, Int64.fromInt(1))) 1959ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 1960ffe3c632Sopenharmony_ci } else { 1961ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1962ffe3c632Sopenharmony_ci accessor.setUint64(-1, Int64.fromInt(1)); 1963ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(-1)).toEqual(Int64.fromInt(1)); 1964ffe3c632Sopenharmony_ci } 1965ffe3c632Sopenharmony_ci }); 1966ffe3c632Sopenharmony_ci 1967ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 1968ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 1969ffe3c632Sopenharmony_ci expect( 1970ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setUint64( 1971ffe3c632Sopenharmony_ci 1, /** @type {!Int64} */ (/** @type {*} */ (null)))) 1972ffe3c632Sopenharmony_ci .toThrow(); 1973ffe3c632Sopenharmony_ci } else { 1974ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 1975ffe3c632Sopenharmony_ci accessor.setUint64(1, /** @type {!Int64} */ (/** @type {*} */ (null))); 1976ffe3c632Sopenharmony_ci expect(accessor.getUint64WithDefault(1)).toEqual(null); 1977ffe3c632Sopenharmony_ci } 1978ffe3c632Sopenharmony_ci }); 1979ffe3c632Sopenharmony_ci}); 1980ffe3c632Sopenharmony_ci 1981ffe3c632Sopenharmony_cidescribe('Double access', () => { 1982ffe3c632Sopenharmony_ci it('returns default value for empty input', () => { 1983ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1984ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(1)).toEqual(0); 1985ffe3c632Sopenharmony_ci }); 1986ffe3c632Sopenharmony_ci 1987ffe3c632Sopenharmony_ci it('returns the default from parameter', () => { 1988ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer()); 1989ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(1, 2)).toEqual(2); 1990ffe3c632Sopenharmony_ci }); 1991ffe3c632Sopenharmony_ci 1992ffe3c632Sopenharmony_ci it('decodes value from wire', () => { 1993ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 1994ffe3c632Sopenharmony_ci 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F)); 1995ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(1)).toEqual(1); 1996ffe3c632Sopenharmony_ci }); 1997ffe3c632Sopenharmony_ci 1998ffe3c632Sopenharmony_ci 1999ffe3c632Sopenharmony_ci it('decodes value from wire with multple values being present', () => { 2000ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(createArrayBuffer( 2001ffe3c632Sopenharmony_ci 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, 0x09, 0x00, 0x00, 2002ffe3c632Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF)); 2003ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(1)).toEqual(-1); 2004ffe3c632Sopenharmony_ci }); 2005ffe3c632Sopenharmony_ci 2006ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_STATE) { 2007ffe3c632Sopenharmony_ci it('fails when getting double value with other wire types', () => { 2008ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer( 2009ffe3c632Sopenharmony_ci createArrayBuffer(0x0D, 0x00, 0x00, 0xF0, 0x3F)); 2010ffe3c632Sopenharmony_ci expect(() => { 2011ffe3c632Sopenharmony_ci accessor.getDoubleWithDefault(1); 2012ffe3c632Sopenharmony_ci }).toThrow(); 2013ffe3c632Sopenharmony_ci }); 2014ffe3c632Sopenharmony_ci } 2015ffe3c632Sopenharmony_ci 2016ffe3c632Sopenharmony_ci 2017ffe3c632Sopenharmony_ci it('throws in getter for invalid fieldNumber', () => { 2018ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 2019ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().getDoubleWithDefault(-1, 1)) 2020ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 2021ffe3c632Sopenharmony_ci } else { 2022ffe3c632Sopenharmony_ci expect(Kernel.createEmpty().getDoubleWithDefault(-1, 1)).toEqual(1); 2023ffe3c632Sopenharmony_ci } 2024ffe3c632Sopenharmony_ci }); 2025ffe3c632Sopenharmony_ci 2026ffe3c632Sopenharmony_ci it('returns the value from setter', () => { 2027ffe3c632Sopenharmony_ci const bytes = 2028ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F); 2029ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2030ffe3c632Sopenharmony_ci accessor.setDouble(1, 2); 2031ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(1)).toEqual(2); 2032ffe3c632Sopenharmony_ci }); 2033ffe3c632Sopenharmony_ci 2034ffe3c632Sopenharmony_ci it('encode the value from setter', () => { 2035ffe3c632Sopenharmony_ci const bytes = 2036ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F); 2037ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2038ffe3c632Sopenharmony_ci const newBytes = 2039ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); 2040ffe3c632Sopenharmony_ci accessor.setDouble(1, 0); 2041ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(newBytes); 2042ffe3c632Sopenharmony_ci }); 2043ffe3c632Sopenharmony_ci 2044ffe3c632Sopenharmony_ci it('returns string value from cache', () => { 2045ffe3c632Sopenharmony_ci const bytes = 2046ffe3c632Sopenharmony_ci createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F); 2047ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2048ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(1)).toBe(1); 2049ffe3c632Sopenharmony_ci // Make sure the value is cached. 2050ffe3c632Sopenharmony_ci bytes[2] = 0x00; 2051ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(1)).toBe(1); 2052ffe3c632Sopenharmony_ci }); 2053ffe3c632Sopenharmony_ci 2054ffe3c632Sopenharmony_ci it('throws in setter for invalid fieldNumber', () => { 2055ffe3c632Sopenharmony_ci if (CHECK_BOUNDS) { 2056ffe3c632Sopenharmony_ci expect(() => Kernel.createEmpty().setDouble(-1, 1)) 2057ffe3c632Sopenharmony_ci .toThrowError('Field number is out of range: -1'); 2058ffe3c632Sopenharmony_ci } else { 2059ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 2060ffe3c632Sopenharmony_ci accessor.setDouble(-1, 1); 2061ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(-1)).toEqual(1); 2062ffe3c632Sopenharmony_ci } 2063ffe3c632Sopenharmony_ci }); 2064ffe3c632Sopenharmony_ci 2065ffe3c632Sopenharmony_ci it('throws in setter for invalid value', () => { 2066ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 2067ffe3c632Sopenharmony_ci expect( 2068ffe3c632Sopenharmony_ci () => Kernel.createEmpty().setDouble( 2069ffe3c632Sopenharmony_ci 1, /** @type {number} */ (/** @type {*} */ (null)))) 2070ffe3c632Sopenharmony_ci .toThrowError('Must be a number, but got: null'); 2071ffe3c632Sopenharmony_ci } else { 2072ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 2073ffe3c632Sopenharmony_ci accessor.setDouble(1, /** @type {number} */ (/** @type {*} */ (null))); 2074ffe3c632Sopenharmony_ci expect(accessor.getDoubleWithDefault(1)).toEqual(null); 2075ffe3c632Sopenharmony_ci } 2076ffe3c632Sopenharmony_ci }); 2077ffe3c632Sopenharmony_ci}); 2078ffe3c632Sopenharmony_ci 2079ffe3c632Sopenharmony_cidescribe('Kernel for singular group does', () => { 2080ffe3c632Sopenharmony_ci it('return group from the input', () => { 2081ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0B, 0x08, 0x01, 0x0C); 2082ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2083ffe3c632Sopenharmony_ci const msg = accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2084ffe3c632Sopenharmony_ci expect(msg.getBoolWithDefault(1, false)).toBe(true); 2085ffe3c632Sopenharmony_ci }); 2086ffe3c632Sopenharmony_ci 2087ffe3c632Sopenharmony_ci it('return group from the input when pivot is set', () => { 2088ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0B, 0x08, 0x01, 0x0C); 2089ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2090ffe3c632Sopenharmony_ci const msg = accessor.getGroupOrNull(1, TestMessage.instanceCreator, 0); 2091ffe3c632Sopenharmony_ci expect(msg.getBoolWithDefault(1, false)).toBe(true); 2092ffe3c632Sopenharmony_ci }); 2093ffe3c632Sopenharmony_ci 2094ffe3c632Sopenharmony_ci it('encode group from the input', () => { 2095ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0B, 0x08, 0x01, 0x0C); 2096ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2097ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 2098ffe3c632Sopenharmony_ci }); 2099ffe3c632Sopenharmony_ci 2100ffe3c632Sopenharmony_ci it('encode group from the input after read', () => { 2101ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0B, 0x08, 0x01, 0x0C); 2102ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2103ffe3c632Sopenharmony_ci accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2104ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 2105ffe3c632Sopenharmony_ci }); 2106ffe3c632Sopenharmony_ci 2107ffe3c632Sopenharmony_ci it('return last group from multiple inputs', () => { 2108ffe3c632Sopenharmony_ci const bytes = 2109ffe3c632Sopenharmony_ci createArrayBuffer(0x0B, 0x08, 0x00, 0x0C, 0x0B, 0x08, 0x01, 0x0C); 2110ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2111ffe3c632Sopenharmony_ci const msg = accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2112ffe3c632Sopenharmony_ci expect(msg.getBoolWithDefault(1, false)).toBe(true); 2113ffe3c632Sopenharmony_ci }); 2114ffe3c632Sopenharmony_ci 2115ffe3c632Sopenharmony_ci it('removes duplicated group when serializing', () => { 2116ffe3c632Sopenharmony_ci const bytes = 2117ffe3c632Sopenharmony_ci createArrayBuffer(0x0B, 0x08, 0x00, 0x0C, 0x0B, 0x08, 0x01, 0x0C); 2118ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2119ffe3c632Sopenharmony_ci accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2120ffe3c632Sopenharmony_ci expect(accessor.serialize()) 2121ffe3c632Sopenharmony_ci .toEqual(createArrayBuffer(0x0B, 0x08, 0x01, 0x0C)); 2122ffe3c632Sopenharmony_ci }); 2123ffe3c632Sopenharmony_ci 2124ffe3c632Sopenharmony_ci it('encode group from multiple inputs', () => { 2125ffe3c632Sopenharmony_ci const bytes = 2126ffe3c632Sopenharmony_ci createArrayBuffer(0x0B, 0x08, 0x00, 0x0C, 0x0B, 0x08, 0x01, 0x0C); 2127ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2128ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(bytes); 2129ffe3c632Sopenharmony_ci }); 2130ffe3c632Sopenharmony_ci 2131ffe3c632Sopenharmony_ci it('encode group after read', () => { 2132ffe3c632Sopenharmony_ci const bytes = 2133ffe3c632Sopenharmony_ci createArrayBuffer(0x0B, 0x08, 0x00, 0x0C, 0x0B, 0x08, 0x01, 0x0C); 2134ffe3c632Sopenharmony_ci const expected = createArrayBuffer(0x0B, 0x08, 0x01, 0x0C); 2135ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2136ffe3c632Sopenharmony_ci accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2137ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(expected); 2138ffe3c632Sopenharmony_ci }); 2139ffe3c632Sopenharmony_ci 2140ffe3c632Sopenharmony_ci it('return group from setter', () => { 2141ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x08, 0x01); 2142ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 2143ffe3c632Sopenharmony_ci const subaccessor = Kernel.fromArrayBuffer(bytes); 2144ffe3c632Sopenharmony_ci const submsg1 = new TestMessage(subaccessor); 2145ffe3c632Sopenharmony_ci accessor.setGroup(1, submsg1); 2146ffe3c632Sopenharmony_ci const submsg2 = accessor.getGroup(1, TestMessage.instanceCreator); 2147ffe3c632Sopenharmony_ci expect(submsg1).toBe(submsg2); 2148ffe3c632Sopenharmony_ci }); 2149ffe3c632Sopenharmony_ci 2150ffe3c632Sopenharmony_ci it('encode group from setter', () => { 2151ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 2152ffe3c632Sopenharmony_ci const subaccessor = Kernel.fromArrayBuffer(createArrayBuffer(0x08, 0x01)); 2153ffe3c632Sopenharmony_ci const submsg = new TestMessage(subaccessor); 2154ffe3c632Sopenharmony_ci accessor.setGroup(1, submsg); 2155ffe3c632Sopenharmony_ci const expected = createArrayBuffer(0x0B, 0x08, 0x01, 0x0C); 2156ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(expected); 2157ffe3c632Sopenharmony_ci }); 2158ffe3c632Sopenharmony_ci 2159ffe3c632Sopenharmony_ci it('leave hasFieldNumber unchanged after getGroupOrNull', () => { 2160ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 2161ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 2162ffe3c632Sopenharmony_ci expect(accessor.getGroupOrNull(1, TestMessage.instanceCreator)).toBe(null); 2163ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 2164ffe3c632Sopenharmony_ci }); 2165ffe3c632Sopenharmony_ci 2166ffe3c632Sopenharmony_ci it('serialize changes to subgroups made with getGroupsOrNull', () => { 2167ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0B, 0x08, 0x02, 0x0C); 2168ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 2169ffe3c632Sopenharmony_ci const mutableSubMessage = 2170ffe3c632Sopenharmony_ci accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2171ffe3c632Sopenharmony_ci mutableSubMessage.setInt32(1, 10); 2172ffe3c632Sopenharmony_ci const intTenBytes = createArrayBuffer(0x0B, 0x08, 0x0A, 0x0C); 2173ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(intTenBytes); 2174ffe3c632Sopenharmony_ci }); 2175ffe3c632Sopenharmony_ci 2176ffe3c632Sopenharmony_ci it('serialize additions to subgroups made with getGroupOrNull', () => { 2177ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0B, 0x08, 0x02, 0x0C); 2178ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 2179ffe3c632Sopenharmony_ci const mutableSubMessage = 2180ffe3c632Sopenharmony_ci accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2181ffe3c632Sopenharmony_ci mutableSubMessage.setInt32(2, 3); 2182ffe3c632Sopenharmony_ci // Sub group contains the original field, plus the new one. 2183ffe3c632Sopenharmony_ci expect(accessor.serialize()) 2184ffe3c632Sopenharmony_ci .toEqual(createArrayBuffer(0x0B, 0x08, 0x02, 0x10, 0x03, 0x0C)); 2185ffe3c632Sopenharmony_ci }); 2186ffe3c632Sopenharmony_ci 2187ffe3c632Sopenharmony_ci it('fail with getGroupOrNull if immutable group exist in cache', () => { 2188ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0B, 0x08, 0x02, 0x0C); 2189ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 2190ffe3c632Sopenharmony_ci 2191ffe3c632Sopenharmony_ci const readOnly = accessor.getGroup(1, TestMessage.instanceCreator); 2192ffe3c632Sopenharmony_ci if (CHECK_TYPE) { 2193ffe3c632Sopenharmony_ci expect(() => accessor.getGroupOrNull(1, TestMessage.instanceCreator)) 2194ffe3c632Sopenharmony_ci .toThrow(); 2195ffe3c632Sopenharmony_ci } else { 2196ffe3c632Sopenharmony_ci const mutableSubGropu = 2197ffe3c632Sopenharmony_ci accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2198ffe3c632Sopenharmony_ci // The instance returned by getGroupOrNull is the exact same instance. 2199ffe3c632Sopenharmony_ci expect(mutableSubGropu).toBe(readOnly); 2200ffe3c632Sopenharmony_ci 2201ffe3c632Sopenharmony_ci // Serializing the subgroup does not write the changes 2202ffe3c632Sopenharmony_ci mutableSubGropu.setInt32(1, 0); 2203ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(intTwoBytes); 2204ffe3c632Sopenharmony_ci } 2205ffe3c632Sopenharmony_ci }); 2206ffe3c632Sopenharmony_ci 2207ffe3c632Sopenharmony_ci it('change hasFieldNumber after getGroupAttach', () => { 2208ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 2209ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 2210ffe3c632Sopenharmony_ci expect(accessor.getGroupAttach(1, TestMessage.instanceCreator)) 2211ffe3c632Sopenharmony_ci .not.toBe(null); 2212ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(true); 2213ffe3c632Sopenharmony_ci }); 2214ffe3c632Sopenharmony_ci 2215ffe3c632Sopenharmony_ci it('change hasFieldNumber after getGroupAttach when pivot is set', () => { 2216ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 2217ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(false); 2218ffe3c632Sopenharmony_ci expect( 2219ffe3c632Sopenharmony_ci accessor.getGroupAttach(1, TestMessage.instanceCreator, /* pivot= */ 1)) 2220ffe3c632Sopenharmony_ci .not.toBe(null); 2221ffe3c632Sopenharmony_ci expect(accessor.hasFieldNumber(1)).toBe(true); 2222ffe3c632Sopenharmony_ci }); 2223ffe3c632Sopenharmony_ci 2224ffe3c632Sopenharmony_ci it('serialize subgroups made with getGroupAttach', () => { 2225ffe3c632Sopenharmony_ci const accessor = Kernel.createEmpty(); 2226ffe3c632Sopenharmony_ci const mutableSubGroup = 2227ffe3c632Sopenharmony_ci accessor.getGroupAttach(1, TestMessage.instanceCreator); 2228ffe3c632Sopenharmony_ci mutableSubGroup.setInt32(1, 10); 2229ffe3c632Sopenharmony_ci const intTenBytes = createArrayBuffer(0x0B, 0x08, 0x0A, 0x0C); 2230ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(intTenBytes); 2231ffe3c632Sopenharmony_ci }); 2232ffe3c632Sopenharmony_ci 2233ffe3c632Sopenharmony_ci it('serialize additions to subgroups using getMessageAttach', () => { 2234ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0B, 0x08, 0x02, 0x0C); 2235ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 2236ffe3c632Sopenharmony_ci const mutableSubGroup = 2237ffe3c632Sopenharmony_ci accessor.getGroupAttach(1, TestMessage.instanceCreator); 2238ffe3c632Sopenharmony_ci mutableSubGroup.setInt32(2, 3); 2239ffe3c632Sopenharmony_ci // Sub message contains the original field, plus the new one. 2240ffe3c632Sopenharmony_ci expect(accessor.serialize()) 2241ffe3c632Sopenharmony_ci .toEqual(createArrayBuffer(0x0B, 0x08, 0x02, 0x10, 0x03, 0x0C)); 2242ffe3c632Sopenharmony_ci }); 2243ffe3c632Sopenharmony_ci 2244ffe3c632Sopenharmony_ci it('fail with getGroupAttach if immutable message exist in cache', () => { 2245ffe3c632Sopenharmony_ci const intTwoBytes = createArrayBuffer(0x0B, 0x08, 0x02, 0x0C); 2246ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(intTwoBytes); 2247ffe3c632Sopenharmony_ci 2248ffe3c632Sopenharmony_ci const readOnly = accessor.getGroup(1, TestMessage.instanceCreator); 2249ffe3c632Sopenharmony_ci if (CHECK_TYPE) { 2250ffe3c632Sopenharmony_ci expect(() => accessor.getGroupAttach(1, TestMessage.instanceCreator)) 2251ffe3c632Sopenharmony_ci .toThrow(); 2252ffe3c632Sopenharmony_ci } else { 2253ffe3c632Sopenharmony_ci const mutableSubGroup = 2254ffe3c632Sopenharmony_ci accessor.getGroupAttach(1, TestMessage.instanceCreator); 2255ffe3c632Sopenharmony_ci // The instance returned by getMessageOrNull is the exact same instance. 2256ffe3c632Sopenharmony_ci expect(mutableSubGroup).toBe(readOnly); 2257ffe3c632Sopenharmony_ci 2258ffe3c632Sopenharmony_ci // Serializing the submessage does not write the changes 2259ffe3c632Sopenharmony_ci mutableSubGroup.setInt32(1, 0); 2260ffe3c632Sopenharmony_ci expect(accessor.serialize()).toEqual(intTwoBytes); 2261ffe3c632Sopenharmony_ci } 2262ffe3c632Sopenharmony_ci }); 2263ffe3c632Sopenharmony_ci 2264ffe3c632Sopenharmony_ci it('read default group return empty group with getGroup', () => { 2265ffe3c632Sopenharmony_ci const bytes = new ArrayBuffer(0); 2266ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2267ffe3c632Sopenharmony_ci expect(accessor.getGroup(1, TestMessage.instanceCreator)).toBeTruthy(); 2268ffe3c632Sopenharmony_ci expect(accessor.getGroup(1, TestMessage.instanceCreator).serialize()) 2269ffe3c632Sopenharmony_ci .toEqual(bytes); 2270ffe3c632Sopenharmony_ci }); 2271ffe3c632Sopenharmony_ci 2272ffe3c632Sopenharmony_ci it('read default group return null with getGroupOrNull', () => { 2273ffe3c632Sopenharmony_ci const bytes = new ArrayBuffer(0); 2274ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2275ffe3c632Sopenharmony_ci expect(accessor.getGroupOrNull(1, TestMessage.instanceCreator)).toBe(null); 2276ffe3c632Sopenharmony_ci }); 2277ffe3c632Sopenharmony_ci 2278ffe3c632Sopenharmony_ci it('read group preserve reference equality', () => { 2279ffe3c632Sopenharmony_ci const bytes = createArrayBuffer(0x0B, 0x08, 0x02, 0x0C); 2280ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2281ffe3c632Sopenharmony_ci const msg1 = accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2282ffe3c632Sopenharmony_ci const msg2 = accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2283ffe3c632Sopenharmony_ci const msg3 = accessor.getGroupAttach(1, TestMessage.instanceCreator); 2284ffe3c632Sopenharmony_ci expect(msg1).toBe(msg2); 2285ffe3c632Sopenharmony_ci expect(msg1).toBe(msg3); 2286ffe3c632Sopenharmony_ci }); 2287ffe3c632Sopenharmony_ci 2288ffe3c632Sopenharmony_ci it('fail when getting group with null instance constructor', () => { 2289ffe3c632Sopenharmony_ci const accessor = 2290ffe3c632Sopenharmony_ci Kernel.fromArrayBuffer(createArrayBuffer(0x0A, 0x02, 0x08, 0x01)); 2291ffe3c632Sopenharmony_ci const nullMessage = /** @type {function(!Kernel):!TestMessage} */ 2292ffe3c632Sopenharmony_ci (/** @type {*} */ (null)); 2293ffe3c632Sopenharmony_ci expect(() => accessor.getGroupOrNull(1, nullMessage)).toThrow(); 2294ffe3c632Sopenharmony_ci }); 2295ffe3c632Sopenharmony_ci 2296ffe3c632Sopenharmony_ci it('fail when setting group value with null value', () => { 2297ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(new ArrayBuffer(0)); 2298ffe3c632Sopenharmony_ci const fakeMessage = /** @type {!TestMessage} */ (/** @type {*} */ (null)); 2299ffe3c632Sopenharmony_ci if (CHECK_CRITICAL_TYPE) { 2300ffe3c632Sopenharmony_ci expect(() => accessor.setGroup(1, fakeMessage)) 2301ffe3c632Sopenharmony_ci .toThrowError('Given value is not a message instance: null'); 2302ffe3c632Sopenharmony_ci } else { 2303ffe3c632Sopenharmony_ci // Note in unchecked mode we produce invalid output for invalid inputs. 2304ffe3c632Sopenharmony_ci // This test just documents our behavior in those cases. 2305ffe3c632Sopenharmony_ci // These values might change at any point and are not considered 2306ffe3c632Sopenharmony_ci // what the implementation should be doing here. 2307ffe3c632Sopenharmony_ci accessor.setMessage(1, fakeMessage); 2308ffe3c632Sopenharmony_ci expect(accessor.getGroupOrNull( 2309ffe3c632Sopenharmony_ci /* fieldNumber= */ 1, TestMessage.instanceCreator)) 2310ffe3c632Sopenharmony_ci .toBeNull(); 2311ffe3c632Sopenharmony_ci } 2312ffe3c632Sopenharmony_ci }); 2313ffe3c632Sopenharmony_ci 2314ffe3c632Sopenharmony_ci it('reads group in a longer buffer', () => { 2315ffe3c632Sopenharmony_ci const bytes = createArrayBuffer( 2316ffe3c632Sopenharmony_ci 0x12, 0x20, // 32 length delimited 2317ffe3c632Sopenharmony_ci 0x00, // random values for padding start 2318ffe3c632Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2319ffe3c632Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2320ffe3c632Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2321ffe3c632Sopenharmony_ci 0x00, // random values for padding end 2322ffe3c632Sopenharmony_ci 0x0B, // Group tag 2323ffe3c632Sopenharmony_ci 0x08, 0x02, 0x0C); 2324ffe3c632Sopenharmony_ci const accessor = Kernel.fromArrayBuffer(bytes); 2325ffe3c632Sopenharmony_ci const msg1 = accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2326ffe3c632Sopenharmony_ci const msg2 = accessor.getGroupOrNull(1, TestMessage.instanceCreator); 2327ffe3c632Sopenharmony_ci expect(msg1).toBe(msg2); 2328ffe3c632Sopenharmony_ci }); 2329ffe3c632Sopenharmony_ci}); 2330