lazy_accessor_test.js 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  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. it('throws in setter for negative value', () => {
  765. if (CHECK_CRITICAL_TYPE) {
  766. expect(() => LazyAccessor.createEmpty().setFixed32(1, -1)).toThrow();
  767. } else {
  768. const accessor = LazyAccessor.createEmpty();
  769. accessor.setFixed32(1, -1);
  770. expect(accessor.getFixed32WithDefault(1)).toEqual(-1);
  771. }
  772. });
  773. });
  774. describe('Fixed64 access', () => {
  775. it('returns default value for empty input', () => {
  776. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  777. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(0));
  778. });
  779. it('returns the default from parameter', () => {
  780. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  781. expect(accessor.getFixed64WithDefault(1, Int64.fromInt(2)))
  782. .toEqual(Int64.fromInt(2));
  783. });
  784. it('decodes value from wire', () => {
  785. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  786. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  787. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  788. });
  789. it('decodes value from wire with multple values being present', () => {
  790. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  791. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x02, 0x00,
  792. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  793. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(2));
  794. });
  795. if (CHECK_CRITICAL_STATE) {
  796. it('fails when getting value with other wire types', () => {
  797. const accessor = LazyAccessor.fromArrayBuffer(
  798. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  799. expect(() => {
  800. accessor.getFixed64WithDefault(1);
  801. }).toThrow();
  802. });
  803. }
  804. it('throws in getter for invalid fieldNumber', () => {
  805. if (CHECK_BOUNDS) {
  806. expect(
  807. () => LazyAccessor.createEmpty().getFixed64WithDefault(
  808. -1, Int64.fromInt(1)))
  809. .toThrowError('Field number is out of range: -1');
  810. } else {
  811. expect(LazyAccessor.createEmpty().getFixed64WithDefault(
  812. -1, Int64.fromInt(1)))
  813. .toEqual(Int64.fromInt(1));
  814. }
  815. });
  816. it('returns the value from setter', () => {
  817. const bytes =
  818. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  819. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  820. accessor.setFixed64(1, Int64.fromInt(2));
  821. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(2));
  822. });
  823. it('encode the value from setter', () => {
  824. const bytes =
  825. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  826. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  827. const newBytes =
  828. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  829. accessor.setFixed64(1, Int64.fromInt(0));
  830. expect(accessor.serialize()).toEqual(newBytes);
  831. });
  832. it('returns value from cache', () => {
  833. const bytes =
  834. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  835. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  836. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  837. // Make sure the value is cached.
  838. bytes[2] = 0x00;
  839. expect(accessor.getFixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  840. });
  841. it('throws in setter for invalid fieldNumber', () => {
  842. if (CHECK_BOUNDS) {
  843. expect(() => LazyAccessor.createEmpty().setFixed64(-1, Int64.fromInt(1)))
  844. .toThrowError('Field number is out of range: -1');
  845. } else {
  846. const accessor = LazyAccessor.createEmpty();
  847. accessor.setFixed64(-1, Int64.fromInt(1));
  848. expect(accessor.getFixed64WithDefault(-1)).toEqual(Int64.fromInt(1));
  849. }
  850. });
  851. it('throws in setter for invalid value', () => {
  852. if (CHECK_CRITICAL_TYPE) {
  853. expect(
  854. () => LazyAccessor.createEmpty().setSfixed64(
  855. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  856. .toThrow();
  857. } else {
  858. const accessor = LazyAccessor.createEmpty();
  859. accessor.setFixed64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  860. expect(accessor.getFixed64WithDefault(1)).toEqual(null);
  861. }
  862. });
  863. });
  864. describe('Float access', () => {
  865. it('returns default value for empty input', () => {
  866. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  867. expect(accessor.getFloatWithDefault(1)).toEqual(0);
  868. });
  869. it('returns the default from parameter', () => {
  870. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  871. expect(accessor.getFloatWithDefault(1, 2)).toEqual(2);
  872. });
  873. it('decodes value from wire', () => {
  874. const accessor = LazyAccessor.fromArrayBuffer(
  875. createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F));
  876. expect(accessor.getFloatWithDefault(1)).toEqual(1);
  877. });
  878. it('decodes value from wire with multple values being present', () => {
  879. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  880. 0x0D, 0x00, 0x00, 0x80, 0x3F, 0x0D, 0x00, 0x00, 0x80, 0xBF));
  881. expect(accessor.getFloatWithDefault(1)).toEqual(-1);
  882. });
  883. if (CHECK_CRITICAL_STATE) {
  884. it('fails when getting float value with other wire types', () => {
  885. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  886. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F));
  887. expect(() => {
  888. accessor.getFloatWithDefault(1);
  889. }).toThrow();
  890. });
  891. }
  892. it('throws in getter for invalid fieldNumber', () => {
  893. if (CHECK_BOUNDS) {
  894. expect(() => LazyAccessor.createEmpty().getFloatWithDefault(-1, 1))
  895. .toThrowError('Field number is out of range: -1');
  896. } else {
  897. expect(LazyAccessor.createEmpty().getFloatWithDefault(-1, 1)).toEqual(1);
  898. }
  899. });
  900. it('returns the value from setter', () => {
  901. const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F);
  902. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  903. accessor.setFloat(1, 1.6);
  904. expect(accessor.getFloatWithDefault(1)).toEqual(Math.fround(1.6));
  905. });
  906. it('encode the value from setter', () => {
  907. const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F);
  908. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  909. const newBytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00);
  910. accessor.setFloat(1, 0);
  911. expect(accessor.serialize()).toEqual(newBytes);
  912. });
  913. it('returns float value from cache', () => {
  914. const bytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x80, 0x3F);
  915. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  916. expect(accessor.getFloatWithDefault(1)).toBe(1);
  917. // Make sure the value is cached.
  918. bytes[2] = 0x00;
  919. expect(accessor.getFloatWithDefault(1)).toBe(1);
  920. });
  921. it('throws in setter for invalid fieldNumber', () => {
  922. if (CHECK_BOUNDS) {
  923. expect(() => LazyAccessor.createEmpty().setFloat(-1, 1))
  924. .toThrowError('Field number is out of range: -1');
  925. } else {
  926. const accessor = LazyAccessor.createEmpty();
  927. accessor.setFloat(-1, 1);
  928. expect(accessor.getFloatWithDefault(-1)).toEqual(1);
  929. }
  930. });
  931. it('throws in setter for invalid value', () => {
  932. if (CHECK_CRITICAL_TYPE) {
  933. expect(
  934. () => LazyAccessor.createEmpty().setFloat(
  935. 1, /** @type {number} */ (/** @type {*} */ (null))))
  936. .toThrow();
  937. } else {
  938. const accessor = LazyAccessor.createEmpty();
  939. accessor.setFloat(1, /** @type {number} */ (/** @type {*} */ (null)));
  940. expect(accessor.getFloatWithDefault(1)).toEqual(0);
  941. }
  942. });
  943. it('throws in setter for value outside of float32 precision', () => {
  944. if (CHECK_CRITICAL_TYPE) {
  945. expect(() => LazyAccessor.createEmpty().setFloat(1, Number.MAX_VALUE))
  946. .toThrow();
  947. } else {
  948. const accessor = LazyAccessor.createEmpty();
  949. accessor.setFloat(1, Number.MAX_VALUE);
  950. expect(accessor.getFloatWithDefault(1)).toEqual(Infinity);
  951. }
  952. });
  953. });
  954. describe('Int32 access', () => {
  955. it('returns default value for empty input', () => {
  956. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  957. expect(accessor.getInt32WithDefault(1)).toEqual(0);
  958. });
  959. it('returns the default from parameter', () => {
  960. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  961. expect(accessor.getInt32WithDefault(1, 2)).toEqual(2);
  962. });
  963. it('decodes value from wire', () => {
  964. const accessor =
  965. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  966. expect(accessor.getInt32WithDefault(1)).toEqual(1);
  967. });
  968. it('decodes value from wire with multple values being present', () => {
  969. const accessor =
  970. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  971. expect(accessor.getInt32WithDefault(1)).toEqual(2);
  972. });
  973. it('fails when getting value with other wire types', () => {
  974. const accessor = LazyAccessor.fromArrayBuffer(
  975. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  976. if (CHECK_CRITICAL_TYPE) {
  977. expect(() => {
  978. accessor.getInt32WithDefault(1);
  979. }).toThrowError('Expected wire type: 0 but found: 5');
  980. } else {
  981. // Note in unchecked mode we produce invalid output for invalid inputs.
  982. // This test just documents our behavior in those cases.
  983. // These values might change at any point and are not considered
  984. // what the implementation should be doing here.
  985. expect(accessor.getInt32WithDefault(1)).toEqual(0);
  986. }
  987. });
  988. it('throws in getter for invalid fieldNumber', () => {
  989. if (CHECK_BOUNDS) {
  990. expect(() => LazyAccessor.createEmpty().getInt32WithDefault(-1, 1))
  991. .toThrowError('Field number is out of range: -1');
  992. } else {
  993. expect(LazyAccessor.createEmpty().getInt32WithDefault(-1, 1)).toEqual(1);
  994. }
  995. });
  996. it('returns the value from setter', () => {
  997. const bytes = createArrayBuffer(0x08, 0x01);
  998. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  999. accessor.setInt32(1, 2);
  1000. expect(accessor.getInt32WithDefault(1)).toEqual(2);
  1001. });
  1002. it('encode the value from setter', () => {
  1003. const bytes = createArrayBuffer(0x08, 0x01);
  1004. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1005. const newBytes = createArrayBuffer(0x08, 0x00);
  1006. accessor.setInt32(1, 0);
  1007. expect(accessor.serialize()).toEqual(newBytes);
  1008. });
  1009. it('returns value from cache', () => {
  1010. const bytes = createArrayBuffer(0x08, 0x01);
  1011. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1012. expect(accessor.getInt32WithDefault(1)).toBe(1);
  1013. // Make sure the value is cached.
  1014. bytes[2] = 0x00;
  1015. expect(accessor.getInt32WithDefault(1)).toBe(1);
  1016. });
  1017. it('throws in setter for invalid fieldNumber', () => {
  1018. if (CHECK_BOUNDS) {
  1019. expect(() => LazyAccessor.createEmpty().setInt32(-1, 1))
  1020. .toThrowError('Field number is out of range: -1');
  1021. } else {
  1022. const accessor = LazyAccessor.createEmpty();
  1023. accessor.setInt32(-1, 1);
  1024. expect(accessor.getInt32WithDefault(-1)).toEqual(1);
  1025. }
  1026. });
  1027. it('throws in setter for invalid value', () => {
  1028. if (CHECK_CRITICAL_TYPE) {
  1029. expect(
  1030. () => LazyAccessor.createEmpty().setInt32(
  1031. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1032. .toThrow();
  1033. } else {
  1034. const accessor = LazyAccessor.createEmpty();
  1035. accessor.setInt32(1, /** @type {number} */ (/** @type {*} */ (null)));
  1036. expect(accessor.getInt32WithDefault(1)).toEqual(null);
  1037. }
  1038. });
  1039. });
  1040. describe('Int64 access', () => {
  1041. it('returns default value for empty input', () => {
  1042. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1043. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(0));
  1044. });
  1045. it('returns the default from parameter', () => {
  1046. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1047. expect(accessor.getInt64WithDefault(1, Int64.fromInt(2)))
  1048. .toEqual(Int64.fromInt(2));
  1049. });
  1050. it('decodes value from wire', () => {
  1051. const accessor =
  1052. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  1053. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1));
  1054. });
  1055. it('decodes value from wire with multple values being present', () => {
  1056. const accessor =
  1057. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  1058. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(2));
  1059. });
  1060. it('fails when getting value with other wire types', () => {
  1061. const accessor = LazyAccessor.fromArrayBuffer(
  1062. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1063. if (CHECK_CRITICAL_TYPE) {
  1064. expect(() => {
  1065. accessor.getInt64WithDefault(1);
  1066. }).toThrowError('Expected wire type: 0 but found: 5');
  1067. } else {
  1068. // Note in unchecked mode we produce invalid output for invalid inputs.
  1069. // This test just documents our behavior in those cases.
  1070. // These values might change at any point and are not considered
  1071. // what the implementation should be doing here.
  1072. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(0));
  1073. }
  1074. });
  1075. it('throws in getter for invalid fieldNumber', () => {
  1076. if (CHECK_BOUNDS) {
  1077. expect(
  1078. () => LazyAccessor.createEmpty().getInt64WithDefault(
  1079. -1, Int64.fromInt(1)))
  1080. .toThrowError('Field number is out of range: -1');
  1081. } else {
  1082. expect(
  1083. LazyAccessor.createEmpty().getInt64WithDefault(-1, Int64.fromInt(1)))
  1084. .toEqual(Int64.fromInt(1));
  1085. }
  1086. });
  1087. it('returns the value from setter', () => {
  1088. const bytes = createArrayBuffer(0x08, 0x01);
  1089. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1090. accessor.setInt64(1, Int64.fromInt(2));
  1091. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(2));
  1092. });
  1093. it('encode the value from setter', () => {
  1094. const bytes = createArrayBuffer(0x08, 0x01);
  1095. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1096. const newBytes = createArrayBuffer(0x08, 0x00);
  1097. accessor.setInt64(1, Int64.fromInt(0));
  1098. expect(accessor.serialize()).toEqual(newBytes);
  1099. });
  1100. it('returns value from cache', () => {
  1101. const bytes = createArrayBuffer(0x08, 0x01);
  1102. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1103. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1));
  1104. // Make sure the value is cached.
  1105. bytes[2] = 0x00;
  1106. expect(accessor.getInt64WithDefault(1)).toEqual(Int64.fromInt(1));
  1107. });
  1108. it('throws in setter for invalid fieldNumber', () => {
  1109. if (CHECK_BOUNDS) {
  1110. expect(() => LazyAccessor.createEmpty().setInt64(-1, Int64.fromInt(1)))
  1111. .toThrowError('Field number is out of range: -1');
  1112. } else {
  1113. const accessor = LazyAccessor.createEmpty();
  1114. accessor.setInt64(-1, Int64.fromInt(1));
  1115. expect(accessor.getInt64WithDefault(-1)).toEqual(Int64.fromInt(1));
  1116. }
  1117. });
  1118. it('throws in setter for invalid value', () => {
  1119. if (CHECK_CRITICAL_TYPE) {
  1120. expect(
  1121. () => LazyAccessor.createEmpty().setInt64(
  1122. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  1123. .toThrow();
  1124. } else {
  1125. const accessor = LazyAccessor.createEmpty();
  1126. accessor.setInt64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  1127. expect(accessor.getInt64WithDefault(1)).toEqual(null);
  1128. }
  1129. });
  1130. });
  1131. describe('Sfixed32 access', () => {
  1132. it('returns default value for empty input', () => {
  1133. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1134. expect(accessor.getSfixed32WithDefault(1)).toEqual(0);
  1135. });
  1136. it('returns the default from parameter', () => {
  1137. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1138. expect(accessor.getSfixed32WithDefault(1, 2)).toEqual(2);
  1139. });
  1140. it('decodes value from wire', () => {
  1141. const accessor = LazyAccessor.fromArrayBuffer(
  1142. createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00));
  1143. expect(accessor.getSfixed32WithDefault(1)).toEqual(1);
  1144. });
  1145. it('decodes value from wire with multple values being present', () => {
  1146. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1147. 0x0D, 0x01, 0x00, 0x80, 0x00, 0x0D, 0x02, 0x00, 0x00, 0x00));
  1148. expect(accessor.getSfixed32WithDefault(1)).toEqual(2);
  1149. });
  1150. it('fails when getting value with other wire types', () => {
  1151. const accessor = LazyAccessor.fromArrayBuffer(
  1152. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  1153. if (CHECK_CRITICAL_TYPE) {
  1154. expect(() => {
  1155. accessor.getSfixed32WithDefault(1);
  1156. }).toThrowError('Expected wire type: 5 but found: 0');
  1157. } else {
  1158. // Note in unchecked mode we produce invalid output for invalid inputs.
  1159. // This test just documents our behavior in those cases.
  1160. // These values might change at any point and are not considered
  1161. // what the implementation should be doing here.
  1162. expect(accessor.getSfixed32WithDefault(1)).toEqual(8421504);
  1163. }
  1164. });
  1165. it('throws in getter for invalid fieldNumber', () => {
  1166. if (CHECK_BOUNDS) {
  1167. expect(() => LazyAccessor.createEmpty().getSfixed32WithDefault(-1, 1))
  1168. .toThrowError('Field number is out of range: -1');
  1169. } else {
  1170. expect(LazyAccessor.createEmpty().getSfixed32WithDefault(-1, 1))
  1171. .toEqual(1);
  1172. }
  1173. });
  1174. it('returns the value from setter', () => {
  1175. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  1176. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1177. accessor.setSfixed32(1, 2);
  1178. expect(accessor.getSfixed32WithDefault(1)).toEqual(2);
  1179. });
  1180. it('encode the value from setter', () => {
  1181. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  1182. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1183. const newBytes = createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00);
  1184. accessor.setSfixed32(1, 0);
  1185. expect(accessor.serialize()).toEqual(newBytes);
  1186. });
  1187. it('returns value from cache', () => {
  1188. const bytes = createArrayBuffer(0x0D, 0x01, 0x00, 0x00, 0x00);
  1189. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1190. expect(accessor.getSfixed32WithDefault(1)).toBe(1);
  1191. // Make sure the value is cached.
  1192. bytes[2] = 0x00;
  1193. expect(accessor.getSfixed32WithDefault(1)).toBe(1);
  1194. });
  1195. it('throws in setter for invalid fieldNumber', () => {
  1196. if (CHECK_BOUNDS) {
  1197. expect(() => LazyAccessor.createEmpty().setSfixed32(-1, 1))
  1198. .toThrowError('Field number is out of range: -1');
  1199. } else {
  1200. const accessor = LazyAccessor.createEmpty();
  1201. accessor.setSfixed32(-1, 1);
  1202. expect(accessor.getSfixed32WithDefault(-1)).toEqual(1);
  1203. }
  1204. });
  1205. it('throws in setter for invalid value', () => {
  1206. if (CHECK_CRITICAL_TYPE) {
  1207. expect(
  1208. () => LazyAccessor.createEmpty().setSfixed32(
  1209. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1210. .toThrow();
  1211. } else {
  1212. const accessor = LazyAccessor.createEmpty();
  1213. accessor.setSfixed32(1, /** @type {number} */ (/** @type {*} */ (null)));
  1214. expect(accessor.getSfixed32WithDefault(1)).toEqual(null);
  1215. }
  1216. });
  1217. });
  1218. describe('Sfixed64 access', () => {
  1219. it('returns default value for empty input', () => {
  1220. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1221. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(0));
  1222. });
  1223. it('returns the default from parameter', () => {
  1224. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1225. expect(accessor.getSfixed64WithDefault(1, Int64.fromInt(2)))
  1226. .toEqual(Int64.fromInt(2));
  1227. });
  1228. it('decodes value from wire', () => {
  1229. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1230. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  1231. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  1232. });
  1233. it('decodes value from wire with multple values being present', () => {
  1234. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1235. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x02, 0x00,
  1236. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  1237. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(2));
  1238. });
  1239. if (CHECK_CRITICAL_STATE) {
  1240. it('fails when getting value with other wire types', () => {
  1241. const accessor = LazyAccessor.fromArrayBuffer(
  1242. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1243. expect(() => {
  1244. accessor.getSfixed64WithDefault(1);
  1245. }).toThrow();
  1246. });
  1247. }
  1248. it('throws in getter for invalid fieldNumber', () => {
  1249. if (CHECK_BOUNDS) {
  1250. expect(
  1251. () => LazyAccessor.createEmpty().getSfixed64WithDefault(
  1252. -1, Int64.fromInt(1)))
  1253. .toThrowError('Field number is out of range: -1');
  1254. } else {
  1255. expect(LazyAccessor.createEmpty().getSfixed64WithDefault(
  1256. -1, Int64.fromInt(1)))
  1257. .toEqual(Int64.fromInt(1));
  1258. }
  1259. });
  1260. it('returns the value from setter', () => {
  1261. const bytes =
  1262. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1263. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1264. accessor.setSfixed64(1, Int64.fromInt(2));
  1265. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(2));
  1266. });
  1267. it('encode the value from setter', () => {
  1268. const bytes =
  1269. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1270. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1271. const newBytes =
  1272. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1273. accessor.setSfixed64(1, Int64.fromInt(0));
  1274. expect(accessor.serialize()).toEqual(newBytes);
  1275. });
  1276. it('returns value from cache', () => {
  1277. const bytes =
  1278. createArrayBuffer(0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1279. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1280. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  1281. // Make sure the value is cached.
  1282. bytes[2] = 0x00;
  1283. expect(accessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(1));
  1284. });
  1285. it('throws in setter for invalid fieldNumber', () => {
  1286. if (CHECK_BOUNDS) {
  1287. expect(() => LazyAccessor.createEmpty().setSfixed64(-1, Int64.fromInt(1)))
  1288. .toThrowError('Field number is out of range: -1');
  1289. } else {
  1290. const accessor = LazyAccessor.createEmpty();
  1291. accessor.setSfixed64(-1, Int64.fromInt(1));
  1292. expect(accessor.getSfixed64WithDefault(-1)).toEqual(Int64.fromInt(1));
  1293. }
  1294. });
  1295. it('throws in setter for invalid value', () => {
  1296. if (CHECK_CRITICAL_TYPE) {
  1297. expect(
  1298. () => LazyAccessor.createEmpty().setSfixed64(
  1299. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  1300. .toThrow();
  1301. } else {
  1302. const accessor = LazyAccessor.createEmpty();
  1303. accessor.setSfixed64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  1304. expect(accessor.getSfixed64WithDefault(1)).toEqual(null);
  1305. }
  1306. });
  1307. });
  1308. describe('Sint32 access', () => {
  1309. it('returns default value for empty input', () => {
  1310. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1311. expect(accessor.getSint32WithDefault(1)).toEqual(0);
  1312. });
  1313. it('returns the default from parameter', () => {
  1314. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1315. expect(accessor.getSint32WithDefault(1, 2)).toEqual(2);
  1316. });
  1317. it('decodes value from wire', () => {
  1318. const accessor =
  1319. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x02));
  1320. expect(accessor.getSint32WithDefault(1)).toEqual(1);
  1321. });
  1322. it('decodes value from wire with multple values being present', () => {
  1323. const accessor =
  1324. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x03, 0x08, 0x02));
  1325. expect(accessor.getSint32WithDefault(1)).toEqual(1);
  1326. });
  1327. it('fails when getting value with other wire types', () => {
  1328. const accessor = LazyAccessor.fromArrayBuffer(
  1329. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1330. if (CHECK_CRITICAL_TYPE) {
  1331. expect(() => {
  1332. accessor.getSint32WithDefault(1);
  1333. }).toThrowError('Expected wire type: 0 but found: 5');
  1334. } else {
  1335. // Note in unchecked mode we produce invalid output for invalid inputs.
  1336. // This test just documents our behavior in those cases.
  1337. // These values might change at any point and are not considered
  1338. // what the implementation should be doing here.
  1339. expect(accessor.getSint32WithDefault(1)).toEqual(0);
  1340. }
  1341. });
  1342. it('throws in getter for invalid fieldNumber', () => {
  1343. if (CHECK_BOUNDS) {
  1344. expect(() => LazyAccessor.createEmpty().getSint32WithDefault(-1, 1))
  1345. .toThrowError('Field number is out of range: -1');
  1346. } else {
  1347. expect(LazyAccessor.createEmpty().getSint32WithDefault(-1, 1)).toEqual(1);
  1348. }
  1349. });
  1350. it('returns the value from setter', () => {
  1351. const bytes = createArrayBuffer(0x08, 0x01);
  1352. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1353. accessor.setSint32(1, 2);
  1354. expect(accessor.getSint32WithDefault(1)).toEqual(2);
  1355. });
  1356. it('encode the value from setter', () => {
  1357. const bytes = createArrayBuffer(0x08, 0x01);
  1358. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1359. const newBytes = createArrayBuffer(0x08, 0x00);
  1360. accessor.setSint32(1, 0);
  1361. expect(accessor.serialize()).toEqual(newBytes);
  1362. });
  1363. it('returns value from cache', () => {
  1364. const bytes = createArrayBuffer(0x08, 0x02);
  1365. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1366. expect(accessor.getSint32WithDefault(1)).toBe(1);
  1367. // Make sure the value is cached.
  1368. bytes[2] = 0x00;
  1369. expect(accessor.getSint32WithDefault(1)).toBe(1);
  1370. });
  1371. it('throws in setter for invalid fieldNumber', () => {
  1372. if (CHECK_BOUNDS) {
  1373. expect(() => LazyAccessor.createEmpty().setSint32(-1, 1))
  1374. .toThrowError('Field number is out of range: -1');
  1375. } else {
  1376. const accessor = LazyAccessor.createEmpty();
  1377. accessor.setSint32(-1, 1);
  1378. expect(accessor.getSint32WithDefault(-1)).toEqual(1);
  1379. }
  1380. });
  1381. it('throws in setter for invalid value', () => {
  1382. if (CHECK_CRITICAL_TYPE) {
  1383. expect(
  1384. () => LazyAccessor.createEmpty().setSint32(
  1385. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1386. .toThrow();
  1387. } else {
  1388. const accessor = LazyAccessor.createEmpty();
  1389. accessor.setSint32(1, /** @type {number} */ (/** @type {*} */ (null)));
  1390. expect(accessor.getSint32WithDefault(1)).toEqual(null);
  1391. }
  1392. });
  1393. });
  1394. describe('SInt64 access', () => {
  1395. it('returns default value for empty input', () => {
  1396. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1397. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(0));
  1398. });
  1399. it('returns the default from parameter', () => {
  1400. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1401. expect(accessor.getSint64WithDefault(1, Int64.fromInt(2)))
  1402. .toEqual(Int64.fromInt(2));
  1403. });
  1404. it('decodes value from wire', () => {
  1405. const accessor =
  1406. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x02));
  1407. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1408. });
  1409. it('decodes value from wire with multple values being present', () => {
  1410. const accessor =
  1411. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  1412. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1413. });
  1414. it('fails when getting value with other wire types', () => {
  1415. const accessor = LazyAccessor.fromArrayBuffer(
  1416. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1417. if (CHECK_CRITICAL_TYPE) {
  1418. expect(() => {
  1419. accessor.getSint64WithDefault(1);
  1420. }).toThrowError('Expected wire type: 0 but found: 5');
  1421. } else {
  1422. // Note in unchecked mode we produce invalid output for invalid inputs.
  1423. // This test just documents our behavior in those cases.
  1424. // These values might change at any point and are not considered
  1425. // what the implementation should be doing here.
  1426. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(0));
  1427. }
  1428. });
  1429. it('throws in getter for invalid fieldNumber', () => {
  1430. if (CHECK_BOUNDS) {
  1431. expect(
  1432. () => LazyAccessor.createEmpty().getSint64WithDefault(
  1433. -1, Int64.fromInt(1)))
  1434. .toThrowError('Field number is out of range: -1');
  1435. } else {
  1436. expect(
  1437. LazyAccessor.createEmpty().getSint64WithDefault(-1, Int64.fromInt(1)))
  1438. .toEqual(Int64.fromInt(1));
  1439. }
  1440. });
  1441. it('returns the value from setter', () => {
  1442. const bytes = createArrayBuffer(0x08, 0x01);
  1443. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1444. accessor.setSint64(1, Int64.fromInt(2));
  1445. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(2));
  1446. });
  1447. it('encode the value from setter', () => {
  1448. const bytes = createArrayBuffer(0x08, 0x01);
  1449. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1450. const newBytes = createArrayBuffer(0x08, 0x00);
  1451. accessor.setSint64(1, Int64.fromInt(0));
  1452. expect(accessor.serialize()).toEqual(newBytes);
  1453. });
  1454. it('returns value from cache', () => {
  1455. const bytes = createArrayBuffer(0x08, 0x02);
  1456. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1457. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1458. // Make sure the value is cached.
  1459. bytes[1] = 0x00;
  1460. expect(accessor.getSint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1461. });
  1462. it('throws in setter for invalid fieldNumber', () => {
  1463. if (CHECK_BOUNDS) {
  1464. expect(() => LazyAccessor.createEmpty().setSint64(-1, Int64.fromInt(1)))
  1465. .toThrowError('Field number is out of range: -1');
  1466. } else {
  1467. const accessor = LazyAccessor.createEmpty();
  1468. accessor.setInt64(-1, Int64.fromInt(1));
  1469. expect(accessor.getSint64WithDefault(-1)).toEqual(Int64.fromInt(1));
  1470. }
  1471. });
  1472. it('throws in setter for invalid value', () => {
  1473. if (CHECK_CRITICAL_TYPE) {
  1474. expect(
  1475. () => LazyAccessor.createEmpty().setSint64(
  1476. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  1477. .toThrow();
  1478. } else {
  1479. const accessor = LazyAccessor.createEmpty();
  1480. accessor.setSint64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  1481. expect(accessor.getSint64WithDefault(1)).toEqual(null);
  1482. }
  1483. });
  1484. });
  1485. describe('String access', () => {
  1486. it('returns empty string for the empty input', () => {
  1487. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1488. expect(accessor.getStringWithDefault(1)).toEqual('');
  1489. });
  1490. it('returns the default for the empty input', () => {
  1491. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1492. expect(accessor.getStringWithDefault(1, 'bar')).toEqual('bar');
  1493. });
  1494. it('decodes value from wire', () => {
  1495. const accessor =
  1496. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x61));
  1497. expect(accessor.getStringWithDefault(1)).toEqual('a');
  1498. });
  1499. it('decodes value from wire with multple values being present', () => {
  1500. const accessor = LazyAccessor.fromArrayBuffer(
  1501. createArrayBuffer(0x0A, 0x01, 0x60, 0x0A, 0x01, 0x61));
  1502. expect(accessor.getStringWithDefault(1)).toEqual('a');
  1503. });
  1504. if (CHECK_CRITICAL_STATE) {
  1505. it('fails when getting string value with other wire types', () => {
  1506. const accessor = LazyAccessor.fromArrayBuffer(
  1507. createArrayBuffer(0x08, 0x02, 0x08, 0x08));
  1508. expect(() => {
  1509. accessor.getStringWithDefault(1);
  1510. }).toThrow();
  1511. });
  1512. }
  1513. it('throws in getter for invalid fieldNumber', () => {
  1514. if (CHECK_BOUNDS) {
  1515. expect(() => LazyAccessor.createEmpty().getStringWithDefault(-1, 'a'))
  1516. .toThrowError('Field number is out of range: -1');
  1517. } else {
  1518. expect(LazyAccessor.createEmpty().getStringWithDefault(-1, 'a'))
  1519. .toEqual('a');
  1520. }
  1521. });
  1522. it('returns the value from setter', () => {
  1523. const bytes = createArrayBuffer(0x0A, 0x01, 0x61);
  1524. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1525. accessor.setString(1, 'b');
  1526. expect(accessor.getStringWithDefault(1)).toEqual('b');
  1527. });
  1528. it('encode the value from setter', () => {
  1529. const bytes = createArrayBuffer(0x0A, 0x01, 0x61);
  1530. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1531. const newBytes = createArrayBuffer(0x0A, 0x01, 0x62);
  1532. accessor.setString(1, 'b');
  1533. expect(accessor.serialize()).toEqual(newBytes);
  1534. });
  1535. it('returns string value from cache', () => {
  1536. const bytes = createArrayBuffer(0x0A, 0x01, 0x61);
  1537. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1538. expect(accessor.getStringWithDefault(1)).toBe('a');
  1539. // Make sure the value is cached.
  1540. bytes[2] = 0x00;
  1541. expect(accessor.getStringWithDefault(1)).toBe('a');
  1542. });
  1543. it('throws in setter for invalid fieldNumber', () => {
  1544. if (CHECK_TYPE) {
  1545. expect(() => LazyAccessor.createEmpty().setString(-1, 'a'))
  1546. .toThrowError('Field number is out of range: -1');
  1547. } else {
  1548. const accessor = LazyAccessor.createEmpty();
  1549. accessor.setString(-1, 'a');
  1550. expect(accessor.getStringWithDefault(-1)).toEqual('a');
  1551. }
  1552. });
  1553. it('throws in setter for invalid value', () => {
  1554. if (CHECK_CRITICAL_TYPE) {
  1555. expect(
  1556. () => LazyAccessor.createEmpty().setString(
  1557. 1, /** @type {string} */ (/** @type {*} */ (null))))
  1558. .toThrowError('Must be string, but got: null');
  1559. } else {
  1560. const accessor = LazyAccessor.createEmpty();
  1561. accessor.setString(1, /** @type {string} */ (/** @type {*} */ (null)));
  1562. expect(accessor.getStringWithDefault(1)).toEqual(null);
  1563. }
  1564. });
  1565. });
  1566. describe('Uint32 access', () => {
  1567. it('returns default value for empty input', () => {
  1568. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1569. expect(accessor.getUint32WithDefault(1)).toEqual(0);
  1570. });
  1571. it('returns the default from parameter', () => {
  1572. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1573. expect(accessor.getUint32WithDefault(1, 2)).toEqual(2);
  1574. });
  1575. it('decodes value from wire', () => {
  1576. const accessor =
  1577. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  1578. expect(accessor.getUint32WithDefault(1)).toEqual(1);
  1579. });
  1580. it('decodes value from wire with multple values being present', () => {
  1581. const accessor =
  1582. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  1583. expect(accessor.getUint32WithDefault(1)).toEqual(2);
  1584. });
  1585. it('fails when getting value with other wire types', () => {
  1586. const accessor = LazyAccessor.fromArrayBuffer(
  1587. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1588. if (CHECK_CRITICAL_TYPE) {
  1589. expect(() => {
  1590. accessor.getUint32WithDefault(1);
  1591. }).toThrowError('Expected wire type: 0 but found: 5');
  1592. } else {
  1593. // Note in unchecked mode we produce invalid output for invalid inputs.
  1594. // This test just documents our behavior in those cases.
  1595. // These values might change at any point and are not considered
  1596. // what the implementation should be doing here.
  1597. expect(accessor.getUint32WithDefault(1)).toEqual(0);
  1598. }
  1599. });
  1600. it('throws in getter for invalid fieldNumber', () => {
  1601. if (CHECK_BOUNDS) {
  1602. expect(() => LazyAccessor.createEmpty().getUint32WithDefault(-1, 1))
  1603. .toThrowError('Field number is out of range: -1');
  1604. } else {
  1605. expect(LazyAccessor.createEmpty().getUint32WithDefault(-1, 1)).toEqual(1);
  1606. }
  1607. });
  1608. it('returns the value from setter', () => {
  1609. const bytes = createArrayBuffer(0x08, 0x01);
  1610. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1611. accessor.setUint32(1, 2);
  1612. expect(accessor.getUint32WithDefault(1)).toEqual(2);
  1613. });
  1614. it('encode the value from setter', () => {
  1615. const bytes = createArrayBuffer(0x08, 0x01);
  1616. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1617. const newBytes = createArrayBuffer(0x08, 0x00);
  1618. accessor.setUint32(1, 0);
  1619. expect(accessor.serialize()).toEqual(newBytes);
  1620. });
  1621. it('returns value from cache', () => {
  1622. const bytes = createArrayBuffer(0x08, 0x01);
  1623. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1624. expect(accessor.getUint32WithDefault(1)).toBe(1);
  1625. // Make sure the value is cached.
  1626. bytes[2] = 0x00;
  1627. expect(accessor.getUint32WithDefault(1)).toBe(1);
  1628. });
  1629. it('throws in setter for invalid fieldNumber', () => {
  1630. if (CHECK_BOUNDS) {
  1631. expect(() => LazyAccessor.createEmpty().setInt32(-1, 1))
  1632. .toThrowError('Field number is out of range: -1');
  1633. } else {
  1634. const accessor = LazyAccessor.createEmpty();
  1635. accessor.setUint32(-1, 1);
  1636. expect(accessor.getUint32WithDefault(-1)).toEqual(1);
  1637. }
  1638. });
  1639. it('throws in setter for invalid value', () => {
  1640. if (CHECK_CRITICAL_TYPE) {
  1641. expect(
  1642. () => LazyAccessor.createEmpty().setUint32(
  1643. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1644. .toThrow();
  1645. } else {
  1646. const accessor = LazyAccessor.createEmpty();
  1647. accessor.setUint32(1, /** @type {number} */ (/** @type {*} */ (null)));
  1648. expect(accessor.getUint32WithDefault(1)).toEqual(null);
  1649. }
  1650. });
  1651. it('throws in setter for negative value', () => {
  1652. if (CHECK_CRITICAL_TYPE) {
  1653. expect(() => LazyAccessor.createEmpty().setUint32(1, -1)).toThrow();
  1654. } else {
  1655. const accessor = LazyAccessor.createEmpty();
  1656. accessor.setUint32(1, -1);
  1657. expect(accessor.getUint32WithDefault(1)).toEqual(-1);
  1658. }
  1659. });
  1660. });
  1661. describe('Uint64 access', () => {
  1662. it('returns default value for empty input', () => {
  1663. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1664. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(0));
  1665. });
  1666. it('returns the default from parameter', () => {
  1667. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1668. expect(accessor.getUint64WithDefault(1, Int64.fromInt(2)))
  1669. .toEqual(Int64.fromInt(2));
  1670. });
  1671. it('decodes value from wire', () => {
  1672. const accessor =
  1673. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01));
  1674. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1675. });
  1676. it('decodes value from wire with multple values being present', () => {
  1677. const accessor =
  1678. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x01, 0x08, 0x02));
  1679. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(2));
  1680. });
  1681. it('fails when getting value with other wire types', () => {
  1682. const accessor = LazyAccessor.fromArrayBuffer(
  1683. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1684. if (CHECK_CRITICAL_TYPE) {
  1685. expect(() => {
  1686. accessor.getUint64WithDefault(1);
  1687. }).toThrowError('Expected wire type: 0 but found: 5');
  1688. } else {
  1689. // Note in unchecked mode we produce invalid output for invalid inputs.
  1690. // This test just documents our behavior in those cases.
  1691. // These values might change at any point and are not considered
  1692. // what the implementation should be doing here.
  1693. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(0));
  1694. }
  1695. });
  1696. it('throws in getter for invalid fieldNumber', () => {
  1697. if (CHECK_BOUNDS) {
  1698. expect(
  1699. () => LazyAccessor.createEmpty().getUint64WithDefault(
  1700. -1, Int64.fromInt(1)))
  1701. .toThrowError('Field number is out of range: -1');
  1702. } else {
  1703. expect(
  1704. LazyAccessor.createEmpty().getUint64WithDefault(-1, Int64.fromInt(1)))
  1705. .toEqual(Int64.fromInt(1));
  1706. }
  1707. });
  1708. it('returns the value from setter', () => {
  1709. const bytes = createArrayBuffer(0x08, 0x01);
  1710. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1711. accessor.setUint64(1, Int64.fromInt(2));
  1712. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(2));
  1713. });
  1714. it('encode the value from setter', () => {
  1715. const bytes = createArrayBuffer(0x08, 0x01);
  1716. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1717. const newBytes = createArrayBuffer(0x08, 0x00);
  1718. accessor.setUint64(1, Int64.fromInt(0));
  1719. expect(accessor.serialize()).toEqual(newBytes);
  1720. });
  1721. it('returns value from cache', () => {
  1722. const bytes = createArrayBuffer(0x08, 0x01);
  1723. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1724. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1725. // Make sure the value is cached.
  1726. bytes[2] = 0x00;
  1727. expect(accessor.getUint64WithDefault(1)).toEqual(Int64.fromInt(1));
  1728. });
  1729. it('throws in setter for invalid fieldNumber', () => {
  1730. if (CHECK_BOUNDS) {
  1731. expect(() => LazyAccessor.createEmpty().setUint64(-1, Int64.fromInt(1)))
  1732. .toThrowError('Field number is out of range: -1');
  1733. } else {
  1734. const accessor = LazyAccessor.createEmpty();
  1735. accessor.setUint64(-1, Int64.fromInt(1));
  1736. expect(accessor.getUint64WithDefault(-1)).toEqual(Int64.fromInt(1));
  1737. }
  1738. });
  1739. it('throws in setter for invalid value', () => {
  1740. if (CHECK_CRITICAL_TYPE) {
  1741. expect(
  1742. () => LazyAccessor.createEmpty().setUint64(
  1743. 1, /** @type {!Int64} */ (/** @type {*} */ (null))))
  1744. .toThrow();
  1745. } else {
  1746. const accessor = LazyAccessor.createEmpty();
  1747. accessor.setUint64(1, /** @type {!Int64} */ (/** @type {*} */ (null)));
  1748. expect(accessor.getUint64WithDefault(1)).toEqual(null);
  1749. }
  1750. });
  1751. });
  1752. describe('Double access', () => {
  1753. it('returns default value for empty input', () => {
  1754. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1755. expect(accessor.getDoubleWithDefault(1)).toEqual(0);
  1756. });
  1757. it('returns the default from parameter', () => {
  1758. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer());
  1759. expect(accessor.getDoubleWithDefault(1, 2)).toEqual(2);
  1760. });
  1761. it('decodes value from wire', () => {
  1762. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1763. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F));
  1764. expect(accessor.getDoubleWithDefault(1)).toEqual(1);
  1765. });
  1766. it('decodes value from wire with multple values being present', () => {
  1767. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1768. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, 0x09, 0x00, 0x00,
  1769. 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF));
  1770. expect(accessor.getDoubleWithDefault(1)).toEqual(-1);
  1771. });
  1772. if (CHECK_CRITICAL_STATE) {
  1773. it('fails when getting double value with other wire types', () => {
  1774. const accessor = LazyAccessor.fromArrayBuffer(
  1775. createArrayBuffer(0x0D, 0x00, 0x00, 0xF0, 0x3F));
  1776. expect(() => {
  1777. accessor.getDoubleWithDefault(1);
  1778. }).toThrow();
  1779. });
  1780. }
  1781. it('throws in getter for invalid fieldNumber', () => {
  1782. if (CHECK_BOUNDS) {
  1783. expect(() => LazyAccessor.createEmpty().getDoubleWithDefault(-1, 1))
  1784. .toThrowError('Field number is out of range: -1');
  1785. } else {
  1786. expect(LazyAccessor.createEmpty().getDoubleWithDefault(-1, 1)).toEqual(1);
  1787. }
  1788. });
  1789. it('returns the value from setter', () => {
  1790. const bytes =
  1791. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F);
  1792. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1793. accessor.setDouble(1, 2);
  1794. expect(accessor.getDoubleWithDefault(1)).toEqual(2);
  1795. });
  1796. it('encode the value from setter', () => {
  1797. const bytes =
  1798. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F);
  1799. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1800. const newBytes =
  1801. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  1802. accessor.setDouble(1, 0);
  1803. expect(accessor.serialize()).toEqual(newBytes);
  1804. });
  1805. it('returns string value from cache', () => {
  1806. const bytes =
  1807. createArrayBuffer(0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F);
  1808. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  1809. expect(accessor.getDoubleWithDefault(1)).toBe(1);
  1810. // Make sure the value is cached.
  1811. bytes[2] = 0x00;
  1812. expect(accessor.getDoubleWithDefault(1)).toBe(1);
  1813. });
  1814. it('throws in setter for invalid fieldNumber', () => {
  1815. if (CHECK_BOUNDS) {
  1816. expect(() => LazyAccessor.createEmpty().setDouble(-1, 1))
  1817. .toThrowError('Field number is out of range: -1');
  1818. } else {
  1819. const accessor = LazyAccessor.createEmpty();
  1820. accessor.setDouble(-1, 1);
  1821. expect(accessor.getDoubleWithDefault(-1)).toEqual(1);
  1822. }
  1823. });
  1824. it('throws in setter for invalid value', () => {
  1825. if (CHECK_CRITICAL_TYPE) {
  1826. expect(
  1827. () => LazyAccessor.createEmpty().setDouble(
  1828. 1, /** @type {number} */ (/** @type {*} */ (null))))
  1829. .toThrowError('Must be a number, but got: null');
  1830. } else {
  1831. const accessor = LazyAccessor.createEmpty();
  1832. accessor.setDouble(1, /** @type {number} */ (/** @type {*} */ (null)));
  1833. expect(accessor.getDoubleWithDefault(1)).toEqual(null);
  1834. }
  1835. });
  1836. });