lazy_accessor_test.js 76 KB

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