lazy_accessor_test.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  1. /**
  2. * @fileoverview Tests for lazy_accessor.js.
  3. */
  4. goog.module('protobuf.binary.LazyAccessorTest');
  5. goog.setTestOnly();
  6. const ByteString = goog.require('protobuf.ByteString');
  7. const Int64 = goog.require('protobuf.Int64');
  8. const InternalMessage = goog.require('protobuf.binary.InternalMessage');
  9. const LazyAccessor = goog.require('protobuf.binary.LazyAccessor');
  10. const TestMessage = goog.require('protobuf.testing.binary.TestMessage');
  11. // Note to the reader:
  12. // Since the lazy accessor behavior changes with the checking level some of the
  13. // tests in this file have to know which checking level is enable to make
  14. // correct assertions.
  15. const {CHECK_BOUNDS, CHECK_CRITICAL_STATE, CHECK_CRITICAL_TYPE, CHECK_TYPE, MAX_FIELD_NUMBER} = goog.require('protobuf.internal.checks');
  16. /**
  17. * @param {...number} bytes
  18. * @return {!ArrayBuffer}
  19. */
  20. function createArrayBuffer(...bytes) {
  21. return new Uint8Array(bytes).buffer;
  22. }
  23. describe('LazyAccessor', () => {
  24. it('uses the default pivot point', () => {
  25. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  26. expect(accessor.getPivot()).toBe(24);
  27. });
  28. it('makes the pivot point configurable', () => {
  29. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0), 50);
  30. expect(accessor.getPivot()).toBe(50);
  31. });
  32. });
  33. describe('LazyAccessor hasFieldNumber', () => {
  34. it('returns false for empty input', () => {
  35. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  36. expect(accessor.hasFieldNumber(1)).toBe(false);
  37. });
  38. it('returns true for non-empty input', () => {
  39. const bytes = createArrayBuffer(0x08, 0x01);
  40. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  41. expect(accessor.hasFieldNumber(1)).toBe(true);
  42. });
  43. it('returns false for empty array', () => {
  44. const accessor = LazyAccessor.createEmpty();
  45. accessor.setPackedBoolIterable(1, []);
  46. expect(accessor.hasFieldNumber(1)).toBe(false);
  47. });
  48. it('returns true for non-empty array', () => {
  49. const accessor = LazyAccessor.createEmpty();
  50. accessor.setPackedBoolIterable(1, [true]);
  51. expect(accessor.hasFieldNumber(1)).toBe(true);
  52. });
  53. it('updates value after write', () => {
  54. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  55. expect(accessor.hasFieldNumber(1)).toBe(false);
  56. accessor.setBool(1, false);
  57. expect(accessor.hasFieldNumber(1)).toBe(true);
  58. });
  59. });
  60. describe('LazyAccessor clear field does', () => {
  61. it('clear the field set', () => {
  62. const accessor = LazyAccessor.createEmpty();
  63. accessor.setBool(1, true);
  64. accessor.clearField(1);
  65. expect(accessor.hasFieldNumber(1)).toEqual(false);
  66. expect(accessor.serialize()).toEqual(new ArrayBuffer(0));
  67. expect(accessor.getBoolWithDefault(1)).toEqual(false);
  68. });
  69. it('clear the field decoded', () => {
  70. const bytes = createArrayBuffer(0x08, 0x01);
  71. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  72. accessor.clearField(1);
  73. expect(accessor.hasFieldNumber(1)).toEqual(false);
  74. expect(accessor.serialize()).toEqual(new ArrayBuffer(0));
  75. expect(accessor.getBoolWithDefault(1)).toEqual(false);
  76. });
  77. it('clear the field read', () => {
  78. const bytes = createArrayBuffer(0x08, 0x01);
  79. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  80. expect(accessor.getBoolWithDefault(1)).toEqual(true);
  81. accessor.clearField(1);
  82. expect(accessor.hasFieldNumber(1)).toEqual(false);
  83. expect(accessor.serialize()).toEqual(new ArrayBuffer(0));
  84. expect(accessor.getBoolWithDefault(1)).toEqual(false);
  85. });
  86. it('clear set and copied fields without affecting the old', () => {
  87. const accessor = LazyAccessor.createEmpty();
  88. accessor.setBool(1, true);
  89. const clonedAccessor = accessor.shallowCopy();
  90. clonedAccessor.clearField(1);
  91. expect(accessor.hasFieldNumber(1)).toEqual(true);
  92. expect(accessor.getBoolWithDefault(1)).toEqual(true);
  93. expect(clonedAccessor.hasFieldNumber(1)).toEqual(false);
  94. expect(clonedAccessor.serialize()).toEqual(new ArrayBuffer(0));
  95. expect(clonedAccessor.getBoolWithDefault(1)).toEqual(false);
  96. });
  97. it('clear decoded and copied fields without affecting the old', () => {
  98. const bytes = createArrayBuffer(0x08, 0x01);
  99. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  100. const clonedAccessor = accessor.shallowCopy();
  101. clonedAccessor.clearField(1);
  102. expect(accessor.hasFieldNumber(1)).toEqual(true);
  103. expect(accessor.getBoolWithDefault(1)).toEqual(true);
  104. expect(clonedAccessor.hasFieldNumber(1)).toEqual(false);
  105. expect(clonedAccessor.serialize()).toEqual(new ArrayBuffer(0));
  106. expect(clonedAccessor.getBoolWithDefault(1)).toEqual(false);
  107. });
  108. it('clear read and copied fields without affecting the old', () => {
  109. const bytes = createArrayBuffer(0x08, 0x01);
  110. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  111. expect(accessor.getBoolWithDefault(1)).toEqual(true);
  112. const clonedAccessor = accessor.shallowCopy();
  113. clonedAccessor.clearField(1);
  114. expect(accessor.hasFieldNumber(1)).toEqual(true);
  115. expect(accessor.getBoolWithDefault(1)).toEqual(true);
  116. expect(clonedAccessor.hasFieldNumber(1)).toEqual(false);
  117. expect(clonedAccessor.serialize()).toEqual(new ArrayBuffer(0));
  118. expect(clonedAccessor.getBoolWithDefault(1)).toEqual(false);
  119. });
  120. });
  121. describe('LazyAccessor shallow copy does', () => {
  122. it('work for singular fields', () => {
  123. const accessor = LazyAccessor.createEmpty();
  124. accessor.setBool(1, true);
  125. const clonedAccessor = accessor.shallowCopy();
  126. expect(clonedAccessor.getBoolWithDefault(1)).toEqual(true);
  127. accessor.setBool(1, false);
  128. expect(clonedAccessor.getBoolWithDefault(1)).toEqual(true);
  129. });
  130. it('work for repeated fields', () => {
  131. const accessor = LazyAccessor.createEmpty();
  132. accessor.addUnpackedBoolIterable(2, [true, true]);
  133. const clonedAccessor = accessor.shallowCopy();
  134. // Modify a repeated field after clone
  135. accessor.addUnpackedBoolElement(2, true);
  136. const array = Array.from(clonedAccessor.getRepeatedBoolIterable(2));
  137. expect(array).toEqual([true, true]);
  138. });
  139. it('work for repeated fields', () => {
  140. const accessor = LazyAccessor.createEmpty();
  141. accessor.addUnpackedBoolIterable(2, [true, true]);
  142. const clonedAccessor = accessor.shallowCopy();
  143. // Modify a repeated field after clone
  144. accessor.addUnpackedBoolElement(2, true);
  145. const array = Array.from(clonedAccessor.getRepeatedBoolIterable(2));
  146. expect(array).toEqual([true, true]);
  147. });
  148. it('return the correct bytes after serialization', () => {
  149. const bytes = createArrayBuffer(0x08, 0x01, 0x10, 0x01);
  150. const accessor = LazyAccessor.fromArrayBuffer(bytes, /* pivot= */ 1);
  151. const clonedAccessor = accessor.shallowCopy();
  152. accessor.setBool(1, false);
  153. expect(clonedAccessor.getBoolWithDefault(1)).toEqual(true);
  154. expect(clonedAccessor.serialize()).toEqual(bytes);
  155. });
  156. });
  157. describe('LazyAccessor for singular boolean does', () => {
  158. it('return false for the empty input', () => {
  159. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  160. expect(accessor.getBoolWithDefault(
  161. /* fieldNumber= */ 1))
  162. .toBe(false);
  163. });
  164. it('return the value from the input', () => {
  165. const bytes = createArrayBuffer(0x08, 0x01);
  166. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  167. expect(accessor.getBoolWithDefault(
  168. /* fieldNumber= */ 1))
  169. .toBe(true);
  170. });
  171. it('encode the value from the input', () => {
  172. const bytes = createArrayBuffer(0x08, 0x01);
  173. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  174. expect(accessor.serialize()).toEqual(bytes);
  175. });
  176. it('encode the value from the input after read', () => {
  177. const bytes = createArrayBuffer(0x08, 0x01);
  178. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  179. accessor.getBoolWithDefault(
  180. /* fieldNumber= */ 1);
  181. expect(accessor.serialize()).toEqual(bytes);
  182. });
  183. it('return the value from multiple inputs', () => {
  184. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  185. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  186. expect(accessor.getBoolWithDefault(
  187. /* fieldNumber= */ 1))
  188. .toBe(false);
  189. });
  190. it('encode the value from multiple inputs', () => {
  191. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  192. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  193. expect(accessor.serialize()).toEqual(bytes);
  194. });
  195. it('encode the value from multiple inputs after read', () => {
  196. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  197. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  198. accessor.getBoolWithDefault(/* fieldNumber= */ 1);
  199. expect(accessor.serialize()).toEqual(bytes);
  200. });
  201. it('return the value from setter', () => {
  202. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  203. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  204. accessor.setBool(1, true);
  205. expect(accessor.getBoolWithDefault(
  206. /* fieldNumber= */ 1))
  207. .toBe(true);
  208. });
  209. it('encode the value from setter', () => {
  210. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  211. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  212. const newBytes = createArrayBuffer(0x08, 0x01);
  213. accessor.setBool(1, true);
  214. expect(accessor.serialize()).toEqual(newBytes);
  215. });
  216. it('return the bool value from cache', () => {
  217. const bytes = createArrayBuffer(0x08, 0x01);
  218. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  219. expect(accessor.getBoolWithDefault(
  220. /* fieldNumber= */ 1))
  221. .toBe(true);
  222. // Make sure the value is cached.
  223. bytes[1] = 0x00;
  224. expect(accessor.getBoolWithDefault(
  225. /* fieldNumber= */ 1))
  226. .toBe(true);
  227. });
  228. it('fail when getting bool value with other wire types', () => {
  229. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  230. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  231. if (CHECK_CRITICAL_TYPE) {
  232. expect(() => {
  233. accessor.getBoolWithDefault(/* fieldNumber= */ 1);
  234. }).toThrowError('Expected wire type: 0 but found: 1');
  235. } else {
  236. // Note in unchecked mode we produce invalid output for invalid inputs.
  237. // This test just documents our behavior in those cases.
  238. // These values might change at any point and are not considered
  239. // what the implementation should be doing here.
  240. expect(accessor.getBoolWithDefault(
  241. /* fieldNumber= */ 1))
  242. .toBe(true);
  243. }
  244. });
  245. it('fail when setting bool value with out-of-range field number', () => {
  246. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  247. if (CHECK_TYPE) {
  248. expect(() => accessor.setBool(MAX_FIELD_NUMBER + 1, false))
  249. .toThrowError('Field number is out of range: 536870912');
  250. } else {
  251. // Note in unchecked mode we produce invalid output for invalid inputs.
  252. // This test just documents our behavior in those cases.
  253. // These values might change at any point and are not considered
  254. // what the implementation should be doing here.
  255. accessor.setBool(MAX_FIELD_NUMBER + 1, false);
  256. expect(accessor.getBoolWithDefault(MAX_FIELD_NUMBER + 1)).toBe(false);
  257. }
  258. });
  259. it('fail when setting bool value with number value', () => {
  260. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  261. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  262. if (CHECK_CRITICAL_TYPE) {
  263. expect(() => accessor.setBool(1, fakeBoolean))
  264. .toThrowError('Must be a boolean, but got: 2');
  265. } else {
  266. // Note in unchecked mode we produce invalid output for invalid inputs.
  267. // This test just documents our behavior in those cases.
  268. // These values might change at any point and are not considered
  269. // what the implementation should be doing here.
  270. accessor.setBool(1, fakeBoolean);
  271. expect(accessor.getBoolWithDefault(
  272. /* fieldNumber= */ 1))
  273. .toBe(2);
  274. }
  275. });
  276. });
  277. describe('LazyAccessor for singular message does', () => {
  278. it('return message from the input', () => {
  279. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  280. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  281. const msg = accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  282. expect(msg.getBoolWithDefault(1, false)).toBe(true);
  283. });
  284. it('return message from the input when pivot is set', () => {
  285. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  286. const accessor = LazyAccessor.fromArrayBuffer(bytes, /* pivot= */ 0);
  287. const msg = accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  288. expect(msg.getBoolWithDefault(1, false)).toBe(true);
  289. });
  290. it('encode message from the input', () => {
  291. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  292. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  293. expect(accessor.serialize()).toEqual(bytes);
  294. });
  295. it('encode message from the input after read', () => {
  296. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  297. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  298. accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  299. expect(accessor.serialize()).toEqual(bytes);
  300. });
  301. it('return message from multiple inputs', () => {
  302. const bytes =
  303. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x10, 0x01);
  304. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  305. const msg = accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  306. expect(msg.getBoolWithDefault(1, false)).toBe(true);
  307. expect(msg.getBoolWithDefault(2, false)).toBe(true);
  308. });
  309. it('encode message from multiple inputs', () => {
  310. const bytes =
  311. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x10, 0x01);
  312. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  313. expect(accessor.serialize()).toEqual(bytes);
  314. });
  315. it('encode message merged from multiple inputs after read', () => {
  316. const bytes =
  317. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x10, 0x01);
  318. const expected = createArrayBuffer(0x0A, 0x04, 0x08, 0x01, 0x10, 0x01);
  319. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  320. accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  321. expect(accessor.serialize()).toEqual(expected);
  322. });
  323. it('return null for generic accessor', () => {
  324. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  325. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  326. const accessor1 = accessor.getMessageAccessorOrNull(7);
  327. expect(accessor1).toBe(null);
  328. });
  329. it('return null for generic accessor when pivot is set', () => {
  330. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  331. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  332. const accessor1 = accessor.getMessageAccessorOrNull(7, /* pivot= */ 0);
  333. expect(accessor1).toBe(null);
  334. });
  335. it('return generic accessor from the input', () => {
  336. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  337. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  338. const accessor1 = accessor.getMessageAccessorOrNull(1);
  339. expect(accessor1.getBoolWithDefault(1, false)).toBe(true);
  340. // Second call returns a new instance, isn't cached.
  341. const accessor2 = accessor.getMessageAccessorOrNull(1);
  342. expect(accessor2.getBoolWithDefault(1, false)).toBe(true);
  343. expect(accessor2).not.toBe(accessor1);
  344. });
  345. it('return generic accessor from the cached input', () => {
  346. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  347. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  348. const wrappedMessage =
  349. accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  350. // Returns accessor from the cached wrapper instance.
  351. const accessor1 = accessor.getMessageAccessorOrNull(1);
  352. expect(accessor1.getBoolWithDefault(1, false)).toBe(true);
  353. expect(accessor1).toBe(
  354. (/** @type {!InternalMessage} */ (wrappedMessage)).internalGetKernel());
  355. // Second call returns exact same instance.
  356. const accessor2 = accessor.getMessageAccessorOrNull(1);
  357. expect(accessor2.getBoolWithDefault(1, false)).toBe(true);
  358. expect(accessor2).toBe(
  359. (/** @type {!InternalMessage} */ (wrappedMessage)).internalGetKernel());
  360. expect(accessor2).toBe(accessor1);
  361. });
  362. it('return message from setter', () => {
  363. const bytes = createArrayBuffer(0x08, 0x01);
  364. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  365. const subaccessor = LazyAccessor.fromArrayBuffer(bytes);
  366. const submsg1 = new TestMessage(subaccessor);
  367. accessor.setMessage(1, submsg1);
  368. const submsg2 = accessor.getMessage(1, TestMessage.instanceCreator);
  369. expect(submsg1).toBe(submsg2);
  370. });
  371. it('encode message from setter', () => {
  372. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  373. const subaccessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  374. const subsubaccessor =
  375. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  376. const subsubmsg = new TestMessage(subsubaccessor);
  377. subaccessor.setMessage(1, subsubmsg);
  378. const submsg = new TestMessage(subaccessor);
  379. accessor.setMessage(1, submsg);
  380. const expected = createArrayBuffer(0x0A, 0x04, 0x0A, 0x02, 0x08, 0x01);
  381. expect(accessor.serialize()).toEqual(expected);
  382. });
  383. it('encode message with multiple submessage from setter', () => {
  384. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  385. const subaccessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  386. const subsubaccessor1 =
  387. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  388. const subsubaccessor2 =
  389. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x02));
  390. const subsubmsg1 = new TestMessage(subsubaccessor1);
  391. const subsubmsg2 = new TestMessage(subsubaccessor2);
  392. subaccessor.setMessage(1, subsubmsg1);
  393. subaccessor.setMessage(2, subsubmsg2);
  394. const submsg = new TestMessage(subaccessor);
  395. accessor.setMessage(1, submsg);
  396. const expected = createArrayBuffer(
  397. 0x0A, 0x08, 0x0A, 0x02, 0x08, 0x01, 0x12, 0x02, 0x08, 0x02);
  398. expect(accessor.serialize()).toEqual(expected);
  399. });
  400. it('leave hasFieldNumber unchanged after getMessageOrNull', () => {
  401. const accessor = LazyAccessor.createEmpty();
  402. expect(accessor.hasFieldNumber(1)).toBe(false);
  403. expect(accessor.getMessageOrNull(1, TestMessage.instanceCreator))
  404. .toBe(null);
  405. expect(accessor.hasFieldNumber(1)).toBe(false);
  406. });
  407. it('serialize changes to submessages made with getMessageOrNull', () => {
  408. const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02);
  409. const accessor = LazyAccessor.fromArrayBuffer(intTwoBytes);
  410. const mutableSubMessage =
  411. accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  412. mutableSubMessage.setInt32(1, 10);
  413. const intTenBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x0A);
  414. expect(accessor.serialize()).toEqual(intTenBytes);
  415. });
  416. it('serialize additions to submessages made with getMessageOrNull', () => {
  417. const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02);
  418. const accessor = LazyAccessor.fromArrayBuffer(intTwoBytes);
  419. const mutableSubMessage =
  420. accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  421. mutableSubMessage.setInt32(2, 3);
  422. // Sub message contains the original field, plus the new one.
  423. expect(accessor.serialize())
  424. .toEqual(createArrayBuffer(0x0A, 0x04, 0x08, 0x02, 0x10, 0x03));
  425. });
  426. it('fail with getMessageOrNull if immutable message exist in cache', () => {
  427. const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02);
  428. const accessor = LazyAccessor.fromArrayBuffer(intTwoBytes);
  429. const readOnly = accessor.getMessage(1, TestMessage.instanceCreator);
  430. if (CHECK_TYPE) {
  431. expect(() => accessor.getMessageOrNull(1, TestMessage.instanceCreator))
  432. .toThrow();
  433. } else {
  434. const mutableSubMessage =
  435. accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  436. // The instance returned by getMessageOrNull is the exact same instance.
  437. expect(mutableSubMessage).toBe(readOnly);
  438. // Serializing the submessage does not write the changes
  439. mutableSubMessage.setInt32(1, 0);
  440. expect(accessor.serialize()).toEqual(intTwoBytes);
  441. }
  442. });
  443. it('change hasFieldNumber after getMessageAttach', () => {
  444. const accessor = LazyAccessor.createEmpty();
  445. expect(accessor.hasFieldNumber(1)).toBe(false);
  446. expect(accessor.getMessageAttach(1, TestMessage.instanceCreator))
  447. .not.toBe(null);
  448. expect(accessor.hasFieldNumber(1)).toBe(true);
  449. });
  450. it('change hasFieldNumber after getMessageAttach when pivot is set', () => {
  451. const accessor = LazyAccessor.createEmpty();
  452. expect(accessor.hasFieldNumber(1)).toBe(false);
  453. expect(accessor.getMessageAttach(
  454. 1, TestMessage.instanceCreator, /* pivot= */ 1))
  455. .not.toBe(null);
  456. expect(accessor.hasFieldNumber(1)).toBe(true);
  457. });
  458. it('serialize submessages made with getMessageAttach', () => {
  459. const accessor = LazyAccessor.createEmpty();
  460. const mutableSubMessage =
  461. accessor.getMessageAttach(1, TestMessage.instanceCreator);
  462. mutableSubMessage.setInt32(1, 10);
  463. const intTenBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x0A);
  464. expect(accessor.serialize()).toEqual(intTenBytes);
  465. });
  466. it('serialize additions to submessages using getMessageAttach', () => {
  467. const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02);
  468. const accessor = LazyAccessor.fromArrayBuffer(intTwoBytes);
  469. const mutableSubMessage =
  470. accessor.getMessageAttach(1, TestMessage.instanceCreator);
  471. mutableSubMessage.setInt32(2, 3);
  472. // Sub message contains the original field, plus the new one.
  473. expect(accessor.serialize())
  474. .toEqual(createArrayBuffer(0x0A, 0x04, 0x08, 0x02, 0x10, 0x03));
  475. });
  476. it('fail with getMessageAttach if immutable message exist in cache', () => {
  477. const intTwoBytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x02);
  478. const accessor = LazyAccessor.fromArrayBuffer(intTwoBytes);
  479. const readOnly = accessor.getMessage(1, TestMessage.instanceCreator);
  480. if (CHECK_TYPE) {
  481. expect(() => accessor.getMessageAttach(1, TestMessage.instanceCreator))
  482. .toThrow();
  483. } else {
  484. const mutableSubMessage =
  485. accessor.getMessageAttach(1, TestMessage.instanceCreator);
  486. // The instance returned by getMessageOrNull is the exact same instance.
  487. expect(mutableSubMessage).toBe(readOnly);
  488. // Serializing the submessage does not write the changes
  489. mutableSubMessage.setInt32(1, 0);
  490. expect(accessor.serialize()).toEqual(intTwoBytes);
  491. }
  492. });
  493. it('read default message return empty message with getMessage', () => {
  494. const bytes = new ArrayBuffer(0);
  495. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  496. expect(accessor.getMessage(1, TestMessage.instanceCreator)).toBeTruthy();
  497. expect(accessor.getMessage(1, TestMessage.instanceCreator).serialize())
  498. .toEqual(bytes);
  499. });
  500. it('read default message return null with getMessageOrNull', () => {
  501. const bytes = new ArrayBuffer(0);
  502. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  503. expect(accessor.getMessageOrNull(1, TestMessage.instanceCreator))
  504. .toBe(null);
  505. });
  506. it('read message preserve reference equality', () => {
  507. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  508. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  509. const msg1 = accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  510. const msg2 = accessor.getMessageOrNull(1, TestMessage.instanceCreator);
  511. const msg3 = accessor.getMessageAttach(1, TestMessage.instanceCreator);
  512. expect(msg1).toBe(msg2);
  513. expect(msg1).toBe(msg3);
  514. });
  515. it('fail when getting message with other wire types', () => {
  516. const accessor =
  517. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  518. expect(() => accessor.getMessageOrNull(1, TestMessage.instanceCreator))
  519. .toThrow();
  520. });
  521. it('fail when submessage has incomplete data', () => {
  522. const accessor =
  523. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x08));
  524. expect(() => accessor.getMessageOrNull(1, TestMessage.instanceCreator))
  525. .toThrow();
  526. });
  527. it('fail when mutable submessage has incomplete data', () => {
  528. const accessor =
  529. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x08));
  530. expect(() => accessor.getMessageAttach(1, TestMessage.instanceCreator))
  531. .toThrow();
  532. });
  533. it('fail when getting message with null instance constructor', () => {
  534. const accessor =
  535. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x02, 0x08, 0x01));
  536. const nullMessage = /** @type {function(!LazyAccessor):!TestMessage} */
  537. (/** @type {*} */ (null));
  538. expect(() => accessor.getMessageOrNull(1, nullMessage)).toThrow();
  539. });
  540. it('fail when setting message value with null value', () => {
  541. const accessor = LazyAccessor.fromArrayBuffer(new ArrayBuffer(0));
  542. const fakeMessage = /** @type {!TestMessage} */ (/** @type {*} */ (null));
  543. if (CHECK_CRITICAL_TYPE) {
  544. expect(() => accessor.setMessage(1, fakeMessage))
  545. .toThrowError('Given value is not a message instance: null');
  546. } else {
  547. // Note in unchecked mode we produce invalid output for invalid inputs.
  548. // This test just documents our behavior in those cases.
  549. // These values might change at any point and are not considered
  550. // what the implementation should be doing here.
  551. accessor.setMessage(1, fakeMessage);
  552. expect(accessor.getMessageOrNull(
  553. /* fieldNumber= */ 1, TestMessage.instanceCreator))
  554. .toBeNull();
  555. }
  556. });
  557. });
  558. describe('Bytes access', () => {
  559. const simpleByteString = ByteString.fromArrayBuffer(createArrayBuffer(1));
  560. it('returns default value for empty input', () => {
  561. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  562. expect(accessor.getBytesWithDefault(1)).toEqual(ByteString.EMPTY);
  563. });
  564. it('returns the default from parameter', () => {
  565. const defaultByteString = ByteString.fromArrayBuffer(createArrayBuffer(1));
  566. const returnValue = ByteString.fromArrayBuffer(createArrayBuffer(1));
  567. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  568. expect(accessor.getBytesWithDefault(1, defaultByteString))
  569. .toEqual(returnValue);
  570. });
  571. it('decodes value from wire', () => {
  572. const accessor =
  573. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x01));
  574. expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString);
  575. });
  576. it('decodes value from wire with multple values being present', () => {
  577. const accessor = LazyAccessor.fromArrayBuffer(
  578. createArrayBuffer(0x0A, 0x01, 0x00, 0x0A, 0x01, 0x01));
  579. expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString);
  580. });
  581. it('fails when getting value with other wire types', () => {
  582. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  583. 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01));
  584. if (CHECK_CRITICAL_TYPE) {
  585. expect(() => {
  586. accessor.getBytesWithDefault(1);
  587. }).toThrowError('Expected wire type: 2 but found: 1');
  588. } else {
  589. // Note in unchecked mode we produce invalid output for invalid inputs.
  590. // This test just documents our behavior in those cases.
  591. // These values might change at any point and are not considered
  592. // what the implementation should be doing here.
  593. const arrayBuffer = createArrayBuffer(1);
  594. expect(accessor.getBytesWithDefault(1))
  595. .toEqual(ByteString.fromArrayBuffer(arrayBuffer));
  596. }
  597. });
  598. it('throws in getter for invalid fieldNumber', () => {
  599. if (CHECK_BOUNDS) {
  600. expect(
  601. () => LazyAccessor.createEmpty().getBytesWithDefault(
  602. -1, simpleByteString))
  603. .toThrowError('Field number is out of range: -1');
  604. } else {
  605. expect(
  606. LazyAccessor.createEmpty().getBytesWithDefault(-1, simpleByteString))
  607. .toEqual(simpleByteString);
  608. }
  609. });
  610. it('returns the value from setter', () => {
  611. const bytes = createArrayBuffer(0x0A, 0x01, 0x00);
  612. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  613. accessor.setBytes(1, simpleByteString);
  614. expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString);
  615. });
  616. it('encode the value from setter', () => {
  617. const bytes = createArrayBuffer(0x0A, 0x01, 0x00);
  618. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  619. const newBytes = createArrayBuffer(0x0A, 0x01, 0x01);
  620. accessor.setBytes(1, simpleByteString);
  621. expect(accessor.serialize()).toEqual(newBytes);
  622. });
  623. it('returns value from cache', () => {
  624. const bytes = createArrayBuffer(0x0A, 0x01, 0x01);
  625. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  626. expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString);
  627. // Make sure the value is cached.
  628. bytes[2] = 0x00;
  629. expect(accessor.getBytesWithDefault(1)).toEqual(simpleByteString);
  630. });
  631. it('throws in setter for invalid fieldNumber', () => {
  632. if (CHECK_BOUNDS) {
  633. expect(() => LazyAccessor.createEmpty().setBytes(-1, simpleByteString))
  634. .toThrowError('Field number is out of range: -1');
  635. } else {
  636. const accessor = LazyAccessor.createEmpty();
  637. accessor.setBytes(-1, simpleByteString);
  638. expect(accessor.getBytesWithDefault(-1)).toEqual(simpleByteString);
  639. }
  640. });
  641. it('throws in setter for invalid value', () => {
  642. if (CHECK_CRITICAL_TYPE) {
  643. expect(
  644. () => LazyAccessor.createEmpty().setBytes(
  645. 1, /** @type {!ByteString} */ (/** @type {*} */ (null))))
  646. .toThrow();
  647. } else {
  648. const accessor = LazyAccessor.createEmpty();
  649. accessor.setBytes(
  650. 1, /** @type {!ByteString} */ (/** @type {*} */ (null)));
  651. expect(accessor.getBytesWithDefault(1)).toEqual(null);
  652. }
  653. });
  654. });
  655. describe('Fixed32 access', () => {
  656. it('returns default value for empty input', () => {
  657. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  658. expect(accessor.getFixed32WithDefault(1)).toEqual(0);
  659. });
  660. it('returns the default from parameter', () => {
  661. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  662. expect(accessor.getFixed32WithDefault(1, 2)).toEqual(2);
  663. });
  664. it('decodes value from wire', () => {
  665. const accessor = LazyAccessor.fromArrayBuffer(
  666. createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00));
  667. expect(accessor.getFixed32WithDefault(1)).toEqual(1);
  668. });
  669. it('decodes value from wire with multple values being present', () => {
  670. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  671. 0x0D, 0x01, 0x00, 0x80, 0x00, 0x0D, 0x02, 0x00, 0x00, 0x00));
  672. expect(accessor.getFixed32WithDefault(1)).toEqual(2);
  673. });
  674. it('fails when getting value with other wire types', () => {
  675. const accessor = LazyAccessor.fromArrayBuffer(
  676. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  677. if (CHECK_CRITICAL_TYPE) {
  678. expect(() => {
  679. accessor.getFixed32WithDefault(1);
  680. }).toThrowError('Expected wire type: 5 but found: 0');
  681. } else {
  682. // Note in unchecked mode we produce invalid output for invalid inputs.
  683. // This test just documents our behavior in those cases.
  684. // These values might change at any point and are not considered
  685. // what the implementation should be doing here.
  686. expect(accessor.getFixed32WithDefault(1)).toEqual(8421504);
  687. }
  688. });
  689. it('throws in getter for invalid fieldNumber', () => {
  690. if (CHECK_BOUNDS) {
  691. expect(() => LazyAccessor.createEmpty().getFixed32WithDefault(-1, 1))
  692. .toThrowError('Field number is out of range: -1');
  693. } else {
  694. expect(LazyAccessor.createEmpty().getFixed32WithDefault(-1, 1))
  695. .toEqual(1);
  696. }
  697. });
  698. it('returns the value from setter', () => {
  699. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  700. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  701. accessor.setFixed32(1, 2);
  702. expect(accessor.getFixed32WithDefault(1)).toEqual(2);
  703. });
  704. it('encode the value from setter', () => {
  705. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  706. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  707. const newBytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00);
  708. accessor.setFixed32(1, 0);
  709. expect(accessor.serialize()).toEqual(newBytes);
  710. });
  711. it('returns value from cache', () => {
  712. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  713. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  714. expect(accessor.getFixed32WithDefault(1)).toBe(1);
  715. // Make sure the value is cached.
  716. bytes[2] = 0x00;
  717. expect(accessor.getFixed32WithDefault(1)).toBe(1);
  718. });
  719. it('throws in setter for invalid fieldNumber', () => {
  720. if (CHECK_BOUNDS) {
  721. expect(() => LazyAccessor.createEmpty().setFixed32(-1, 1))
  722. .toThrowError('Field number is out of range: -1');
  723. } else {
  724. const accessor = LazyAccessor.createEmpty();
  725. accessor.setFixed32(-1, 1);
  726. expect(accessor.getFixed32WithDefault(-1)).toEqual(1);
  727. }
  728. });
  729. it('throws in setter for invalid value', () => {
  730. if (CHECK_CRITICAL_TYPE) {
  731. expect(
  732. () => LazyAccessor.createEmpty().setFixed32(
  733. 1, /** @type {number} */ (/** @type {*} */ (null))))
  734. .toThrow();
  735. } else {
  736. const accessor = LazyAccessor.createEmpty();
  737. accessor.setFixed32(1, /** @type {number} */ (/** @type {*} */ (null)));
  738. expect(accessor.getFixed32WithDefault(1)).toEqual(null);
  739. }
  740. });
  741. });
  742. describe('Fixed64 access', () => {
  743. it('returns default value for empty input', () => {
  744. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  745. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(0));
  746. });
  747. it('returns the default from parameter', () => {
  748. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  749. expect(accessor.getFixed64WithDefault(1, Int64.fromInt(2)))
  750. .toEqual(Int64.fromInt(2));
  751. });
  752. it('decodes value from wire', () => {
  753. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  754. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  755. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  756. });
  757. it('decodes value from wire with multple values being present', () => {
  758. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  759. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x02, 0x00,
  760. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  761. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(2));
  762. });
  763. if (CHECK_CRITICAL_STATE) {
  764. it('fails when getting value with other wire types', () => {
  765. const accessor = LazyAccessor.fromArrayBuffer(
  766. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  767. expect(() => {
  768. accessor.getFixed64WithDefault(1);
  769. }).toThrow();
  770. });
  771. }
  772. it('throws in getter for invalid fieldNumber', () => {
  773. if (CHECK_BOUNDS) {
  774. expect(
  775. () => LazyAccessor.createEmpty().getFixed64WithDefault(
  776. -1, Int64.fromInt(1)))
  777. .toThrowError('Field number is out of range: -1');
  778. } else {
  779. expect(LazyAccessor.createEmpty().getFixed64WithDefault(
  780. -1, Int64.fromInt(1)))
  781. .toEqual(Int64.fromInt(1));
  782. }
  783. });
  784. it('returns the value from setter', () => {
  785. const bytes =
  786. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  787. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  788. accessor.setFixed64(1, Int64.fromInt(2));
  789. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(2));
  790. });
  791. it('encode the value from setter', () => {
  792. const bytes =
  793. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  794. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  795. const newBytes =
  796. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  797. accessor.setFixed64(1, Int64.fromInt(0));
  798. expect(accessor.serialize()).toEqual(newBytes);
  799. });
  800. it('returns value from cache', () => {
  801. const bytes =
  802. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  803. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  804. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  805. // Make sure the value is cached.
  806. bytes[2] = 0x00;
  807. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  808. });
  809. it('throws in setter for invalid fieldNumber', () => {
  810. if (CHECK_BOUNDS) {
  811. expect(() => LazyAccessor.createEmpty().setFixed64(-1, Int64.fromInt(1)))
  812. .toThrowError('Field number is out of range: -1');
  813. } else {
  814. const accessor = LazyAccessor.createEmpty();
  815. accessor.setFixed64(-1, Int64.fromInt(1));
  816. expect(accessor.getFixed64WithDefault(-1)).toEqual(Int64.fromInt(1));
  817. }
  818. });
  819. it('throws in setter for invalid value', () => {
  820. if (CHECK_CRITICAL_TYPE) {
  821. expect(
  822. () => LazyAccessor.createEmpty().setSfixed64(
  823. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  824. .toThrow();
  825. } else {
  826. const accessor = LazyAccessor.createEmpty();
  827. accessor.setFixed64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  828. expect(accessor.getFixed64WithDefault(1)).toEqual(null);
  829. }
  830. });
  831. });
  832. describe('Float access', () => {
  833. it('returns default value for empty input', () => {
  834. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  835. expect(accessor.getFloatWithDefault(1)).toEqual(0);
  836. });
  837. it('returns the default from parameter', () => {
  838. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  839. expect(accessor.getFloatWithDefault(1, 2)).toEqual(2);
  840. });
  841. it('decodes value from wire', () => {
  842. const accessor = LazyAccessor.fromArrayBuffer(
  843. createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F));
  844. expect(accessor.getFloatWithDefault(1)).toEqual(1);
  845. });
  846. it('decodes value from wire with multple values being present', () => {
  847. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  848. 0x0D, 0x00, 0x00, 0x80, 0x3F, 0x0D, 0x00, 0x00, 0x80, 0xBF));
  849. expect(accessor.getFloatWithDefault(1)).toEqual(-1);
  850. });
  851. if (CHECK_CRITICAL_STATE) {
  852. it('fails when getting float value with other wire types', () => {
  853. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  854. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F));
  855. expect(() => {
  856. accessor.getFloatWithDefault(1);
  857. }).toThrow();
  858. });
  859. }
  860. it('throws in getter for invalid fieldNumber', () => {
  861. if (CHECK_BOUNDS) {
  862. expect(() => LazyAccessor.createEmpty().getFloatWithDefault(-1, 1))
  863. .toThrowError('Field number is out of range: -1');
  864. } else {
  865. expect(LazyAccessor.createEmpty().getFloatWithDefault(-1, 1)).toEqual(1);
  866. }
  867. });
  868. it('returns the value from setter', () => {
  869. const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F);
  870. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  871. accessor.setFloat(1, 1.6);
  872. expect(accessor.getFloatWithDefault(1)).toEqual(Math.fround(1.6));
  873. });
  874. it('encode the value from setter', () => {
  875. const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F);
  876. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  877. const newBytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00);
  878. accessor.setFloat(1, 0);
  879. expect(accessor.serialize()).toEqual(newBytes);
  880. });
  881. it('returns float value from cache', () => {
  882. const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F);
  883. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  884. expect(accessor.getFloatWithDefault(1)).toBe(1);
  885. // Make sure the value is cached.
  886. bytes[2] = 0x00;
  887. expect(accessor.getFloatWithDefault(1)).toBe(1);
  888. });
  889. it('throws in setter for invalid fieldNumber', () => {
  890. if (CHECK_BOUNDS) {
  891. expect(() => LazyAccessor.createEmpty().setFloat(-1, 1))
  892. .toThrowError('Field number is out of range: -1');
  893. } else {
  894. const accessor = LazyAccessor.createEmpty();
  895. accessor.setFloat(-1, 1);
  896. expect(accessor.getFloatWithDefault(-1)).toEqual(1);
  897. }
  898. });
  899. it('throws in setter for invalid value', () => {
  900. if (CHECK_CRITICAL_TYPE) {
  901. expect(
  902. () => LazyAccessor.createEmpty().setFloat(
  903. 1, /** @type {number} */ (/** @type {*} */ (null))))
  904. .toThrow();
  905. } else {
  906. const accessor = LazyAccessor.createEmpty();
  907. accessor.setFloat(1, /** @type {number} */ (/** @type {*} */ (null)));
  908. expect(accessor.getFloatWithDefault(1)).toEqual(0);
  909. }
  910. });
  911. it('throws in setter for value outside of float32 precision', () => {
  912. if (CHECK_CRITICAL_TYPE) {
  913. expect(() => LazyAccessor.createEmpty().setFloat(1, Number.MAX_VALUE))
  914. .toThrow();
  915. } else {
  916. const accessor = LazyAccessor.createEmpty();
  917. accessor.setFloat(1, Number.MAX_VALUE);
  918. expect(accessor.getFloatWithDefault(1)).toEqual(Infinity);
  919. }
  920. });
  921. });
  922. describe('Int32 access', () => {
  923. it('returns default value for empty input', () => {
  924. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  925. expect(accessor.getInt32WithDefault(1)).toEqual(0);
  926. });
  927. it('returns the default from parameter', () => {
  928. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  929. expect(accessor.getInt32WithDefault(1, 2)).toEqual(2);
  930. });
  931. it('decodes value from wire', () => {
  932. const accessor =
  933. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  934. expect(accessor.getInt32WithDefault(1)).toEqual(1);
  935. });
  936. it('decodes value from wire with multple values being present', () => {
  937. const accessor =
  938. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  939. expect(accessor.getInt32WithDefault(1)).toEqual(2);
  940. });
  941. it('fails when getting value with other wire types', () => {
  942. const accessor = LazyAccessor.fromArrayBuffer(
  943. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  944. if (CHECK_CRITICAL_TYPE) {
  945. expect(() => {
  946. accessor.getInt32WithDefault(1);
  947. }).toThrowError('Expected wire type: 0 but found: 5');
  948. } else {
  949. // Note in unchecked mode we produce invalid output for invalid inputs.
  950. // This test just documents our behavior in those cases.
  951. // These values might change at any point and are not considered
  952. // what the implementation should be doing here.
  953. expect(accessor.getInt32WithDefault(1)).toEqual(0);
  954. }
  955. });
  956. it('throws in getter for invalid fieldNumber', () => {
  957. if (CHECK_BOUNDS) {
  958. expect(() => LazyAccessor.createEmpty().getInt32WithDefault(-1, 1))
  959. .toThrowError('Field number is out of range: -1');
  960. } else {
  961. expect(LazyAccessor.createEmpty().getInt32WithDefault(-1, 1)).toEqual(1);
  962. }
  963. });
  964. it('returns the value from setter', () => {
  965. const bytes = createArrayBuffer(0x08, 0x01);
  966. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  967. accessor.setInt32(1, 2);
  968. expect(accessor.getInt32WithDefault(1)).toEqual(2);
  969. });
  970. it('encode the value from setter', () => {
  971. const bytes = createArrayBuffer(0x08, 0x01);
  972. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  973. const newBytes = createArrayBuffer(0x08, 0x00);
  974. accessor.setInt32(1, 0);
  975. expect(accessor.serialize()).toEqual(newBytes);
  976. });
  977. it('returns value from cache', () => {
  978. const bytes = createArrayBuffer(0x08, 0x01);
  979. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  980. expect(accessor.getInt32WithDefault(1)).toBe(1);
  981. // Make sure the value is cached.
  982. bytes[2] = 0x00;
  983. expect(accessor.getInt32WithDefault(1)).toBe(1);
  984. });
  985. it('throws in setter for invalid fieldNumber', () => {
  986. if (CHECK_BOUNDS) {
  987. expect(() => LazyAccessor.createEmpty().setInt32(-1, 1))
  988. .toThrowError('Field number is out of range: -1');
  989. } else {
  990. const accessor = LazyAccessor.createEmpty();
  991. accessor.setInt32(-1, 1);
  992. expect(accessor.getInt32WithDefault(-1)).toEqual(1);
  993. }
  994. });
  995. it('throws in setter for invalid value', () => {
  996. if (CHECK_CRITICAL_TYPE) {
  997. expect(
  998. () => LazyAccessor.createEmpty().setInt32(
  999. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1000. .toThrow();
  1001. } else {
  1002. const accessor = LazyAccessor.createEmpty();
  1003. accessor.setInt32(1, /** @type {number} */ (/** @type {*} */ (null)));
  1004. expect(accessor.getInt32WithDefault(1)).toEqual(null);
  1005. }
  1006. });
  1007. });
  1008. describe('Int64 access', () => {
  1009. it('returns default value for empty input', () => {
  1010. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1011. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(0));
  1012. });
  1013. it('returns the default from parameter', () => {
  1014. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1015. expect(accessor.getInt64WithDefault(1, Int64.fromInt(2)))
  1016. .toEqual(Int64.fromInt(2));
  1017. });
  1018. it('decodes value from wire', () => {
  1019. const accessor =
  1020. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  1021. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1));
  1022. });
  1023. it('decodes value from wire with multple values being present', () => {
  1024. const accessor =
  1025. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  1026. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(2));
  1027. });
  1028. it('fails when getting value with other wire types', () => {
  1029. const accessor = LazyAccessor.fromArrayBuffer(
  1030. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1031. if (CHECK_CRITICAL_TYPE) {
  1032. expect(() => {
  1033. accessor.getInt64WithDefault(1);
  1034. }).toThrowError('Expected wire type: 0 but found: 5');
  1035. } else {
  1036. // Note in unchecked mode we produce invalid output for invalid inputs.
  1037. // This test just documents our behavior in those cases.
  1038. // These values might change at any point and are not considered
  1039. // what the implementation should be doing here.
  1040. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(0));
  1041. }
  1042. });
  1043. it('throws in getter for invalid fieldNumber', () => {
  1044. if (CHECK_BOUNDS) {
  1045. expect(
  1046. () => LazyAccessor.createEmpty().getInt64WithDefault(
  1047. -1, Int64.fromInt(1)))
  1048. .toThrowError('Field number is out of range: -1');
  1049. } else {
  1050. expect(
  1051. LazyAccessor.createEmpty().getInt64WithDefault(-1, Int64.fromInt(1)))
  1052. .toEqual(Int64.fromInt(1));
  1053. }
  1054. });
  1055. it('returns the value from setter', () => {
  1056. const bytes = createArrayBuffer(0x08, 0x01);
  1057. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1058. accessor.setInt64(1, Int64.fromInt(2));
  1059. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(2));
  1060. });
  1061. it('encode the value from setter', () => {
  1062. const bytes = createArrayBuffer(0x08, 0x01);
  1063. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1064. const newBytes = createArrayBuffer(0x08, 0x00);
  1065. accessor.setInt64(1, Int64.fromInt(0));
  1066. expect(accessor.serialize()).toEqual(newBytes);
  1067. });
  1068. it('returns value from cache', () => {
  1069. const bytes = createArrayBuffer(0x08, 0x01);
  1070. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1071. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1));
  1072. // Make sure the value is cached.
  1073. bytes[2] = 0x00;
  1074. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1));
  1075. });
  1076. it('throws in setter for invalid fieldNumber', () => {
  1077. if (CHECK_BOUNDS) {
  1078. expect(() => LazyAccessor.createEmpty().setInt64(-1, Int64.fromInt(1)))
  1079. .toThrowError('Field number is out of range: -1');
  1080. } else {
  1081. const accessor = LazyAccessor.createEmpty();
  1082. accessor.setInt64(-1, Int64.fromInt(1));
  1083. expect(accessor.getInt64WithDefault(-1)).toEqual(Int64.fromInt(1));
  1084. }
  1085. });
  1086. it('throws in setter for invalid value', () => {
  1087. if (CHECK_CRITICAL_TYPE) {
  1088. expect(
  1089. () => LazyAccessor.createEmpty().setInt64(
  1090. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  1091. .toThrow();
  1092. } else {
  1093. const accessor = LazyAccessor.createEmpty();
  1094. accessor.setInt64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  1095. expect(accessor.getInt64WithDefault(1)).toEqual(null);
  1096. }
  1097. });
  1098. });
  1099. describe('Sfixed32 access', () => {
  1100. it('returns default value for empty input', () => {
  1101. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1102. expect(accessor.getSfixed32WithDefault(1)).toEqual(0);
  1103. });
  1104. it('returns the default from parameter', () => {
  1105. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1106. expect(accessor.getSfixed32WithDefault(1, 2)).toEqual(2);
  1107. });
  1108. it('decodes value from wire', () => {
  1109. const accessor = LazyAccessor.fromArrayBuffer(
  1110. createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00));
  1111. expect(accessor.getSfixed32WithDefault(1)).toEqual(1);
  1112. });
  1113. it('decodes value from wire with multple values being present', () => {
  1114. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1115. 0x0D, 0x01, 0x00, 0x80, 0x00, 0x0D, 0x02, 0x00, 0x00, 0x00));
  1116. expect(accessor.getSfixed32WithDefault(1)).toEqual(2);
  1117. });
  1118. it('fails when getting value with other wire types', () => {
  1119. const accessor = LazyAccessor.fromArrayBuffer(
  1120. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  1121. if (CHECK_CRITICAL_TYPE) {
  1122. expect(() => {
  1123. accessor.getSfixed32WithDefault(1);
  1124. }).toThrowError('Expected wire type: 5 but found: 0');
  1125. } else {
  1126. // Note in unchecked mode we produce invalid output for invalid inputs.
  1127. // This test just documents our behavior in those cases.
  1128. // These values might change at any point and are not considered
  1129. // what the implementation should be doing here.
  1130. expect(accessor.getSfixed32WithDefault(1)).toEqual(8421504);
  1131. }
  1132. });
  1133. it('throws in getter for invalid fieldNumber', () => {
  1134. if (CHECK_BOUNDS) {
  1135. expect(() => LazyAccessor.createEmpty().getSfixed32WithDefault(-1, 1))
  1136. .toThrowError('Field number is out of range: -1');
  1137. } else {
  1138. expect(LazyAccessor.createEmpty().getSfixed32WithDefault(-1, 1))
  1139. .toEqual(1);
  1140. }
  1141. });
  1142. it('returns the value from setter', () => {
  1143. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  1144. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1145. accessor.setSfixed32(1, 2);
  1146. expect(accessor.getSfixed32WithDefault(1)).toEqual(2);
  1147. });
  1148. it('encode the value from setter', () => {
  1149. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  1150. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1151. const newBytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00);
  1152. accessor.setSfixed32(1, 0);
  1153. expect(accessor.serialize()).toEqual(newBytes);
  1154. });
  1155. it('returns value from cache', () => {
  1156. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  1157. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1158. expect(accessor.getSfixed32WithDefault(1)).toBe(1);
  1159. // Make sure the value is cached.
  1160. bytes[2] = 0x00;
  1161. expect(accessor.getSfixed32WithDefault(1)).toBe(1);
  1162. });
  1163. it('throws in setter for invalid fieldNumber', () => {
  1164. if (CHECK_BOUNDS) {
  1165. expect(() => LazyAccessor.createEmpty().setSfixed32(-1, 1))
  1166. .toThrowError('Field number is out of range: -1');
  1167. } else {
  1168. const accessor = LazyAccessor.createEmpty();
  1169. accessor.setSfixed32(-1, 1);
  1170. expect(accessor.getSfixed32WithDefault(-1)).toEqual(1);
  1171. }
  1172. });
  1173. it('throws in setter for invalid value', () => {
  1174. if (CHECK_CRITICAL_TYPE) {
  1175. expect(
  1176. () => LazyAccessor.createEmpty().setSfixed32(
  1177. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1178. .toThrow();
  1179. } else {
  1180. const accessor = LazyAccessor.createEmpty();
  1181. accessor.setSfixed32(1, /** @type {number} */ (/** @type {*} */ (null)));
  1182. expect(accessor.getSfixed32WithDefault(1)).toEqual(null);
  1183. }
  1184. });
  1185. });
  1186. describe('Sfixed64 access', () => {
  1187. it('returns default value for empty input', () => {
  1188. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1189. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(0));
  1190. });
  1191. it('returns the default from parameter', () => {
  1192. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1193. expect(accessor.getSfixed64WithDefault(1, Int64.fromInt(2)))
  1194. .toEqual(Int64.fromInt(2));
  1195. });
  1196. it('decodes value from wire', () => {
  1197. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1198. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  1199. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  1200. });
  1201. it('decodes value from wire with multple values being present', () => {
  1202. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1203. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x02, 0x00,
  1204. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  1205. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(2));
  1206. });
  1207. if (CHECK_CRITICAL_STATE) {
  1208. it('fails when getting value with other wire types', () => {
  1209. const accessor = LazyAccessor.fromArrayBuffer(
  1210. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1211. expect(() => {
  1212. accessor.getSfixed64WithDefault(1);
  1213. }).toThrow();
  1214. });
  1215. }
  1216. it('throws in getter for invalid fieldNumber', () => {
  1217. if (CHECK_BOUNDS) {
  1218. expect(
  1219. () => LazyAccessor.createEmpty().getSfixed64WithDefault(
  1220. -1, Int64.fromInt(1)))
  1221. .toThrowError('Field number is out of range: -1');
  1222. } else {
  1223. expect(LazyAccessor.createEmpty().getSfixed64WithDefault(
  1224. -1, Int64.fromInt(1)))
  1225. .toEqual(Int64.fromInt(1));
  1226. }
  1227. });
  1228. it('returns the value from setter', () => {
  1229. const bytes =
  1230. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1231. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1232. accessor.setSfixed64(1, Int64.fromInt(2));
  1233. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(2));
  1234. });
  1235. it('encode the value from setter', () => {
  1236. const bytes =
  1237. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1238. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1239. const newBytes =
  1240. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1241. accessor.setSfixed64(1, Int64.fromInt(0));
  1242. expect(accessor.serialize()).toEqual(newBytes);
  1243. });
  1244. it('returns value from cache', () => {
  1245. const bytes =
  1246. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1247. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1248. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  1249. // Make sure the value is cached.
  1250. bytes[2] = 0x00;
  1251. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  1252. });
  1253. it('throws in setter for invalid fieldNumber', () => {
  1254. if (CHECK_BOUNDS) {
  1255. expect(() => LazyAccessor.createEmpty().setSfixed64(-1, Int64.fromInt(1)))
  1256. .toThrowError('Field number is out of range: -1');
  1257. } else {
  1258. const accessor = LazyAccessor.createEmpty();
  1259. accessor.setSfixed64(-1, Int64.fromInt(1));
  1260. expect(accessor.getSfixed64WithDefault(-1)).toEqual(Int64.fromInt(1));
  1261. }
  1262. });
  1263. it('throws in setter for invalid value', () => {
  1264. if (CHECK_CRITICAL_TYPE) {
  1265. expect(
  1266. () => LazyAccessor.createEmpty().setSfixed64(
  1267. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  1268. .toThrow();
  1269. } else {
  1270. const accessor = LazyAccessor.createEmpty();
  1271. accessor.setSfixed64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  1272. expect(accessor.getSfixed64WithDefault(1)).toEqual(null);
  1273. }
  1274. });
  1275. });
  1276. describe('Sint32 access', () => {
  1277. it('returns default value for empty input', () => {
  1278. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1279. expect(accessor.getSint32WithDefault(1)).toEqual(0);
  1280. });
  1281. it('returns the default from parameter', () => {
  1282. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1283. expect(accessor.getSint32WithDefault(1, 2)).toEqual(2);
  1284. });
  1285. it('decodes value from wire', () => {
  1286. const accessor =
  1287. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x02));
  1288. expect(accessor.getSint32WithDefault(1)).toEqual(1);
  1289. });
  1290. it('decodes value from wire with multple values being present', () => {
  1291. const accessor =
  1292. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x03, 0x08, 0x02));
  1293. expect(accessor.getSint32WithDefault(1)).toEqual(1);
  1294. });
  1295. it('fails when getting value with other wire types', () => {
  1296. const accessor = LazyAccessor.fromArrayBuffer(
  1297. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1298. if (CHECK_CRITICAL_TYPE) {
  1299. expect(() => {
  1300. accessor.getSint32WithDefault(1);
  1301. }).toThrowError('Expected wire type: 0 but found: 5');
  1302. } else {
  1303. // Note in unchecked mode we produce invalid output for invalid inputs.
  1304. // This test just documents our behavior in those cases.
  1305. // These values might change at any point and are not considered
  1306. // what the implementation should be doing here.
  1307. expect(accessor.getSint32WithDefault(1)).toEqual(0);
  1308. }
  1309. });
  1310. it('throws in getter for invalid fieldNumber', () => {
  1311. if (CHECK_BOUNDS) {
  1312. expect(() => LazyAccessor.createEmpty().getSint32WithDefault(-1, 1))
  1313. .toThrowError('Field number is out of range: -1');
  1314. } else {
  1315. expect(LazyAccessor.createEmpty().getSint32WithDefault(-1, 1)).toEqual(1);
  1316. }
  1317. });
  1318. it('returns the value from setter', () => {
  1319. const bytes = createArrayBuffer(0x08, 0x01);
  1320. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1321. accessor.setSint32(1, 2);
  1322. expect(accessor.getSint32WithDefault(1)).toEqual(2);
  1323. });
  1324. it('encode the value from setter', () => {
  1325. const bytes = createArrayBuffer(0x08, 0x01);
  1326. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1327. const newBytes = createArrayBuffer(0x08, 0x00);
  1328. accessor.setSint32(1, 0);
  1329. expect(accessor.serialize()).toEqual(newBytes);
  1330. });
  1331. it('returns value from cache', () => {
  1332. const bytes = createArrayBuffer(0x08, 0x02);
  1333. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1334. expect(accessor.getSint32WithDefault(1)).toBe(1);
  1335. // Make sure the value is cached.
  1336. bytes[2] = 0x00;
  1337. expect(accessor.getSint32WithDefault(1)).toBe(1);
  1338. });
  1339. it('throws in setter for invalid fieldNumber', () => {
  1340. if (CHECK_BOUNDS) {
  1341. expect(() => LazyAccessor.createEmpty().setSint32(-1, 1))
  1342. .toThrowError('Field number is out of range: -1');
  1343. } else {
  1344. const accessor = LazyAccessor.createEmpty();
  1345. accessor.setSint32(-1, 1);
  1346. expect(accessor.getSint32WithDefault(-1)).toEqual(1);
  1347. }
  1348. });
  1349. it('throws in setter for invalid value', () => {
  1350. if (CHECK_CRITICAL_TYPE) {
  1351. expect(
  1352. () => LazyAccessor.createEmpty().setSint32(
  1353. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1354. .toThrow();
  1355. } else {
  1356. const accessor = LazyAccessor.createEmpty();
  1357. accessor.setSint32(1, /** @type {number} */ (/** @type {*} */ (null)));
  1358. expect(accessor.getSint32WithDefault(1)).toEqual(null);
  1359. }
  1360. });
  1361. });
  1362. describe('SInt64 access', () => {
  1363. it('returns default value for empty input', () => {
  1364. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1365. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(0));
  1366. });
  1367. it('returns the default from parameter', () => {
  1368. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1369. expect(accessor.getSint64WithDefault(1, Int64.fromInt(2)))
  1370. .toEqual(Int64.fromInt(2));
  1371. });
  1372. it('decodes value from wire', () => {
  1373. const accessor =
  1374. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x02));
  1375. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1376. });
  1377. it('decodes value from wire with multple values being present', () => {
  1378. const accessor =
  1379. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  1380. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1381. });
  1382. it('fails when getting value with other wire types', () => {
  1383. const accessor = LazyAccessor.fromArrayBuffer(
  1384. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1385. if (CHECK_CRITICAL_TYPE) {
  1386. expect(() => {
  1387. accessor.getSint64WithDefault(1);
  1388. }).toThrowError('Expected wire type: 0 but found: 5');
  1389. } else {
  1390. // Note in unchecked mode we produce invalid output for invalid inputs.
  1391. // This test just documents our behavior in those cases.
  1392. // These values might change at any point and are not considered
  1393. // what the implementation should be doing here.
  1394. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(0));
  1395. }
  1396. });
  1397. it('throws in getter for invalid fieldNumber', () => {
  1398. if (CHECK_BOUNDS) {
  1399. expect(
  1400. () => LazyAccessor.createEmpty().getSint64WithDefault(
  1401. -1, Int64.fromInt(1)))
  1402. .toThrowError('Field number is out of range: -1');
  1403. } else {
  1404. expect(
  1405. LazyAccessor.createEmpty().getSint64WithDefault(-1, Int64.fromInt(1)))
  1406. .toEqual(Int64.fromInt(1));
  1407. }
  1408. });
  1409. it('returns the value from setter', () => {
  1410. const bytes = createArrayBuffer(0x08, 0x01);
  1411. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1412. accessor.setSint64(1, Int64.fromInt(2));
  1413. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(2));
  1414. });
  1415. it('encode the value from setter', () => {
  1416. const bytes = createArrayBuffer(0x08, 0x01);
  1417. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1418. const newBytes = createArrayBuffer(0x08, 0x00);
  1419. accessor.setSint64(1, Int64.fromInt(0));
  1420. expect(accessor.serialize()).toEqual(newBytes);
  1421. });
  1422. it('returns value from cache', () => {
  1423. const bytes = createArrayBuffer(0x08, 0x02);
  1424. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1425. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1426. // Make sure the value is cached.
  1427. bytes[1] = 0x00;
  1428. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1429. });
  1430. it('throws in setter for invalid fieldNumber', () => {
  1431. if (CHECK_BOUNDS) {
  1432. expect(() => LazyAccessor.createEmpty().setSint64(-1, Int64.fromInt(1)))
  1433. .toThrowError('Field number is out of range: -1');
  1434. } else {
  1435. const accessor = LazyAccessor.createEmpty();
  1436. accessor.setInt64(-1, Int64.fromInt(1));
  1437. expect(accessor.getSint64WithDefault(-1)).toEqual(Int64.fromInt(1));
  1438. }
  1439. });
  1440. it('throws in setter for invalid value', () => {
  1441. if (CHECK_CRITICAL_TYPE) {
  1442. expect(
  1443. () => LazyAccessor.createEmpty().setSint64(
  1444. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  1445. .toThrow();
  1446. } else {
  1447. const accessor = LazyAccessor.createEmpty();
  1448. accessor.setSint64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  1449. expect(accessor.getSint64WithDefault(1)).toEqual(null);
  1450. }
  1451. });
  1452. });
  1453. describe('String access', () => {
  1454. it('returns empty string for the empty input', () => {
  1455. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1456. expect(accessor.getStringWithDefault(1)).toEqual('');
  1457. });
  1458. it('returns the default for the empty input', () => {
  1459. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1460. expect(accessor.getStringWithDefault(1, 'bar')).toEqual('bar');
  1461. });
  1462. it('decodes value from wire', () => {
  1463. const accessor =
  1464. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x61));
  1465. expect(accessor.getStringWithDefault(1)).toEqual('a');
  1466. });
  1467. it('decodes value from wire with multple values being present', () => {
  1468. const accessor = LazyAccessor.fromArrayBuffer(
  1469. createArrayBuffer(0x0A, 0x01, 0x60, 0x0A, 0x01, 0x61));
  1470. expect(accessor.getStringWithDefault(1)).toEqual('a');
  1471. });
  1472. if (CHECK_CRITICAL_STATE) {
  1473. it('fails when getting string value with other wire types', () => {
  1474. const accessor = LazyAccessor.fromArrayBuffer(
  1475. createArrayBuffer(0x08, 0x02, 0x08, 0x08));
  1476. expect(() => {
  1477. accessor.getStringWithDefault(1);
  1478. }).toThrow();
  1479. });
  1480. }
  1481. it('throws in getter for invalid fieldNumber', () => {
  1482. if (CHECK_BOUNDS) {
  1483. expect(() => LazyAccessor.createEmpty().getStringWithDefault(-1, 'a'))
  1484. .toThrowError('Field number is out of range: -1');
  1485. } else {
  1486. expect(LazyAccessor.createEmpty().getStringWithDefault(-1, 'a'))
  1487. .toEqual('a');
  1488. }
  1489. });
  1490. it('returns the value from setter', () => {
  1491. const bytes = createArrayBuffer(0x0A, 0x01, 0x61);
  1492. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1493. accessor.setString(1, 'b');
  1494. expect(accessor.getStringWithDefault(1)).toEqual('b');
  1495. });
  1496. it('encode the value from setter', () => {
  1497. const bytes = createArrayBuffer(0x0A, 0x01, 0x61);
  1498. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1499. const newBytes = createArrayBuffer(0x0A, 0x01, 0x62);
  1500. accessor.setString(1, 'b');
  1501. expect(accessor.serialize()).toEqual(newBytes);
  1502. });
  1503. it('returns string value from cache', () => {
  1504. const bytes = createArrayBuffer(0x0A, 0x01, 0x61);
  1505. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1506. expect(accessor.getStringWithDefault(1)).toBe('a');
  1507. // Make sure the value is cached.
  1508. bytes[2] = 0x00;
  1509. expect(accessor.getStringWithDefault(1)).toBe('a');
  1510. });
  1511. it('throws in setter for invalid fieldNumber', () => {
  1512. if (CHECK_TYPE) {
  1513. expect(() => LazyAccessor.createEmpty().setString(-1, 'a'))
  1514. .toThrowError('Field number is out of range: -1');
  1515. } else {
  1516. const accessor = LazyAccessor.createEmpty();
  1517. accessor.setString(-1, 'a');
  1518. expect(accessor.getStringWithDefault(-1)).toEqual('a');
  1519. }
  1520. });
  1521. it('throws in setter for invalid value', () => {
  1522. if (CHECK_CRITICAL_TYPE) {
  1523. expect(
  1524. () => LazyAccessor.createEmpty().setString(
  1525. 1, /** @type {string} */ (/** @type {*} */ (null))))
  1526. .toThrowError('Must be string, but got: null');
  1527. } else {
  1528. const accessor = LazyAccessor.createEmpty();
  1529. accessor.setString(1, /** @type {string} */ (/** @type {*} */ (null)));
  1530. expect(accessor.getStringWithDefault(1)).toEqual(null);
  1531. }
  1532. });
  1533. });
  1534. describe('Uint32 access', () => {
  1535. it('returns default value for empty input', () => {
  1536. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1537. expect(accessor.getUint32WithDefault(1)).toEqual(0);
  1538. });
  1539. it('returns the default from parameter', () => {
  1540. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1541. expect(accessor.getUint32WithDefault(1, 2)).toEqual(2);
  1542. });
  1543. it('decodes value from wire', () => {
  1544. const accessor =
  1545. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  1546. expect(accessor.getUint32WithDefault(1)).toEqual(1);
  1547. });
  1548. it('decodes value from wire with multple values being present', () => {
  1549. const accessor =
  1550. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  1551. expect(accessor.getUint32WithDefault(1)).toEqual(2);
  1552. });
  1553. it('fails when getting value with other wire types', () => {
  1554. const accessor = LazyAccessor.fromArrayBuffer(
  1555. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1556. if (CHECK_CRITICAL_TYPE) {
  1557. expect(() => {
  1558. accessor.getUint32WithDefault(1);
  1559. }).toThrowError('Expected wire type: 0 but found: 5');
  1560. } else {
  1561. // Note in unchecked mode we produce invalid output for invalid inputs.
  1562. // This test just documents our behavior in those cases.
  1563. // These values might change at any point and are not considered
  1564. // what the implementation should be doing here.
  1565. expect(accessor.getUint32WithDefault(1)).toEqual(0);
  1566. }
  1567. });
  1568. it('throws in getter for invalid fieldNumber', () => {
  1569. if (CHECK_BOUNDS) {
  1570. expect(() => LazyAccessor.createEmpty().getUint32WithDefault(-1, 1))
  1571. .toThrowError('Field number is out of range: -1');
  1572. } else {
  1573. expect(LazyAccessor.createEmpty().getUint32WithDefault(-1, 1)).toEqual(1);
  1574. }
  1575. });
  1576. it('returns the value from setter', () => {
  1577. const bytes = createArrayBuffer(0x08, 0x01);
  1578. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1579. accessor.setUint32(1, 2);
  1580. expect(accessor.getUint32WithDefault(1)).toEqual(2);
  1581. });
  1582. it('encode the value from setter', () => {
  1583. const bytes = createArrayBuffer(0x08, 0x01);
  1584. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1585. const newBytes = createArrayBuffer(0x08, 0x00);
  1586. accessor.setUint32(1, 0);
  1587. expect(accessor.serialize()).toEqual(newBytes);
  1588. });
  1589. it('returns value from cache', () => {
  1590. const bytes = createArrayBuffer(0x08, 0x01);
  1591. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1592. expect(accessor.getUint32WithDefault(1)).toBe(1);
  1593. // Make sure the value is cached.
  1594. bytes[2] = 0x00;
  1595. expect(accessor.getUint32WithDefault(1)).toBe(1);
  1596. });
  1597. it('throws in setter for invalid fieldNumber', () => {
  1598. if (CHECK_BOUNDS) {
  1599. expect(() => LazyAccessor.createEmpty().setInt32(-1, 1))
  1600. .toThrowError('Field number is out of range: -1');
  1601. } else {
  1602. const accessor = LazyAccessor.createEmpty();
  1603. accessor.setUint32(-1, 1);
  1604. expect(accessor.getUint32WithDefault(-1)).toEqual(1);
  1605. }
  1606. });
  1607. it('throws in setter for invalid value', () => {
  1608. if (CHECK_CRITICAL_TYPE) {
  1609. expect(
  1610. () => LazyAccessor.createEmpty().setUint32(
  1611. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1612. .toThrow();
  1613. } else {
  1614. const accessor = LazyAccessor.createEmpty();
  1615. accessor.setUint32(1, /** @type {number} */ (/** @type {*} */ (null)));
  1616. expect(accessor.getUint32WithDefault(1)).toEqual(null);
  1617. }
  1618. });
  1619. });
  1620. describe('Uint64 access', () => {
  1621. it('returns default value for empty input', () => {
  1622. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1623. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(0));
  1624. });
  1625. it('returns the default from parameter', () => {
  1626. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1627. expect(accessor.getUint64WithDefault(1, Int64.fromInt(2)))
  1628. .toEqual(Int64.fromInt(2));
  1629. });
  1630. it('decodes value from wire', () => {
  1631. const accessor =
  1632. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  1633. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1634. });
  1635. it('decodes value from wire with multple values being present', () => {
  1636. const accessor =
  1637. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  1638. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(2));
  1639. });
  1640. it('fails when getting value with other wire types', () => {
  1641. const accessor = LazyAccessor.fromArrayBuffer(
  1642. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1643. if (CHECK_CRITICAL_TYPE) {
  1644. expect(() => {
  1645. accessor.getUint64WithDefault(1);
  1646. }).toThrowError('Expected wire type: 0 but found: 5');
  1647. } else {
  1648. // Note in unchecked mode we produce invalid output for invalid inputs.
  1649. // This test just documents our behavior in those cases.
  1650. // These values might change at any point and are not considered
  1651. // what the implementation should be doing here.
  1652. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(0));
  1653. }
  1654. });
  1655. it('throws in getter for invalid fieldNumber', () => {
  1656. if (CHECK_BOUNDS) {
  1657. expect(
  1658. () => LazyAccessor.createEmpty().getUint64WithDefault(
  1659. -1, Int64.fromInt(1)))
  1660. .toThrowError('Field number is out of range: -1');
  1661. } else {
  1662. expect(
  1663. LazyAccessor.createEmpty().getUint64WithDefault(-1, Int64.fromInt(1)))
  1664. .toEqual(Int64.fromInt(1));
  1665. }
  1666. });
  1667. it('returns the value from setter', () => {
  1668. const bytes = createArrayBuffer(0x08, 0x01);
  1669. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1670. accessor.setUint64(1, Int64.fromInt(2));
  1671. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(2));
  1672. });
  1673. it('encode the value from setter', () => {
  1674. const bytes = createArrayBuffer(0x08, 0x01);
  1675. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1676. const newBytes = createArrayBuffer(0x08, 0x00);
  1677. accessor.setUint64(1, Int64.fromInt(0));
  1678. expect(accessor.serialize()).toEqual(newBytes);
  1679. });
  1680. it('returns value from cache', () => {
  1681. const bytes = createArrayBuffer(0x08, 0x01);
  1682. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1683. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1684. // Make sure the value is cached.
  1685. bytes[2] = 0x00;
  1686. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1687. });
  1688. it('throws in setter for invalid fieldNumber', () => {
  1689. if (CHECK_BOUNDS) {
  1690. expect(() => LazyAccessor.createEmpty().setUint64(-1, Int64.fromInt(1)))
  1691. .toThrowError('Field number is out of range: -1');
  1692. } else {
  1693. const accessor = LazyAccessor.createEmpty();
  1694. accessor.setUint64(-1, Int64.fromInt(1));
  1695. expect(accessor.getUint64WithDefault(-1)).toEqual(Int64.fromInt(1));
  1696. }
  1697. });
  1698. it('throws in setter for invalid value', () => {
  1699. if (CHECK_CRITICAL_TYPE) {
  1700. expect(
  1701. () => LazyAccessor.createEmpty().setUint64(
  1702. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  1703. .toThrow();
  1704. } else {
  1705. const accessor = LazyAccessor.createEmpty();
  1706. accessor.setUint64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  1707. expect(accessor.getUint64WithDefault(1)).toEqual(null);
  1708. }
  1709. });
  1710. });
  1711. describe('Double access', () => {
  1712. it('returns default value for empty input', () => {
  1713. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1714. expect(accessor.getDoubleWithDefault(1)).toEqual(0);
  1715. });
  1716. it('returns the default from parameter', () => {
  1717. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1718. expect(accessor.getDoubleWithDefault(1, 2)).toEqual(2);
  1719. });
  1720. it('decodes value from wire', () => {
  1721. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1722. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F));
  1723. expect(accessor.getDoubleWithDefault(1)).toEqual(1);
  1724. });
  1725. it('decodes value from wire with multple values being present', () => {
  1726. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1727. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, 0x09, 0x00, 0x00,
  1728. 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF));
  1729. expect(accessor.getDoubleWithDefault(1)).toEqual(-1);
  1730. });
  1731. if (CHECK_CRITICAL_STATE) {
  1732. it('fails when getting double value with other wire types', () => {
  1733. const accessor = LazyAccessor.fromArrayBuffer(
  1734. createArrayBuffer(0x0D, 0x00, 0x00, 0xF0, 0x3F));
  1735. expect(() => {
  1736. accessor.getDoubleWithDefault(1);
  1737. }).toThrow();
  1738. });
  1739. }
  1740. it('throws in getter for invalid fieldNumber', () => {
  1741. if (CHECK_BOUNDS) {
  1742. expect(() => LazyAccessor.createEmpty().getDoubleWithDefault(-1, 1))
  1743. .toThrowError('Field number is out of range: -1');
  1744. } else {
  1745. expect(LazyAccessor.createEmpty().getDoubleWithDefault(-1, 1)).toEqual(1);
  1746. }
  1747. });
  1748. it('returns the value from setter', () => {
  1749. const bytes =
  1750. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F);
  1751. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1752. accessor.setDouble(1, 2);
  1753. expect(accessor.getDoubleWithDefault(1)).toEqual(2);
  1754. });
  1755. it('encode the value from setter', () => {
  1756. const bytes =
  1757. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F);
  1758. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1759. const newBytes =
  1760. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1761. accessor.setDouble(1, 0);
  1762. expect(accessor.serialize()).toEqual(newBytes);
  1763. });
  1764. it('returns string value from cache', () => {
  1765. const bytes =
  1766. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F);
  1767. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1768. expect(accessor.getDoubleWithDefault(1)).toBe(1);
  1769. // Make sure the value is cached.
  1770. bytes[2] = 0x00;
  1771. expect(accessor.getDoubleWithDefault(1)).toBe(1);
  1772. });
  1773. it('throws in setter for invalid fieldNumber', () => {
  1774. if (CHECK_BOUNDS) {
  1775. expect(() => LazyAccessor.createEmpty().setDouble(-1, 1))
  1776. .toThrowError('Field number is out of range: -1');
  1777. } else {
  1778. const accessor = LazyAccessor.createEmpty();
  1779. accessor.setDouble(-1, 1);
  1780. expect(accessor.getDoubleWithDefault(-1)).toEqual(1);
  1781. }
  1782. });
  1783. it('throws in setter for invalid value', () => {
  1784. if (CHECK_CRITICAL_TYPE) {
  1785. expect(
  1786. () => LazyAccessor.createEmpty().setDouble(
  1787. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1788. .toThrowError('Must be a number, but got: null');
  1789. } else {
  1790. const accessor = LazyAccessor.createEmpty();
  1791. accessor.setDouble(1, /** @type {number} */ (/** @type {*} */ (null)));
  1792. expect(accessor.getDoubleWithDefault(1)).toEqual(null);
  1793. }
  1794. });
  1795. });