lazy_accessor_test.js 77 KB

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