lazy_accessor_repeated_test.js 272 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443
  1. /**
  2. * @fileoverview Tests for repeated methods in lazy_accessor.js.
  3. */
  4. goog.module('protobuf.binary.LazyAccessorTest');
  5. goog.setTestOnly();
  6. const ByteString = goog.require('protobuf.ByteString');
  7. const Int64 = goog.require('protobuf.Int64');
  8. const InternalMessage = goog.require('protobuf.binary.InternalMessage');
  9. const LazyAccessor = goog.require('protobuf.binary.LazyAccessor');
  10. const TestMessage = goog.require('protobuf.testing.binary.TestMessage');
  11. // Note to the reader:
  12. // Since the lazy accessor behavior changes with the checking level some of the
  13. // tests in this file have to know which checking level is enable to make
  14. // correct assertions.
  15. const {CHECK_CRITICAL_STATE} = 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. /**
  24. * Expects the Iterable instance yield the same values as the expected array.
  25. * @param {!Iterable<T>} iterable
  26. * @param {!Array<T>} expected
  27. * @template T
  28. * TODO: Implement this as a custom matcher.
  29. */
  30. function expectEqualToArray(iterable, expected) {
  31. const array = Array.from(iterable);
  32. expect(array).toEqual(expected);
  33. }
  34. /**
  35. * Expects the Iterable instance yield qualified values.
  36. * @param {!Iterable<T>} iterable
  37. * @param {(function(T): boolean)=} verify
  38. * @template T
  39. */
  40. function expectQualifiedIterable(iterable, verify) {
  41. if (verify) {
  42. for (const value of iterable) {
  43. expect(verify(value)).toBe(true);
  44. }
  45. }
  46. }
  47. /**
  48. * Expects the Iterable instance yield the same values as the expected array of
  49. * messages.
  50. * @param {!Iterable<!TestMessage>} iterable
  51. * @param {!Array<!TestMessage>} expected
  52. * @template T
  53. * TODO: Implement this as a custom matcher.
  54. */
  55. function expectEqualToMessageArray(iterable, expected) {
  56. const array = Array.from(iterable);
  57. expect(array.length).toEqual(expected.length);
  58. for (let i = 0; i < array.length; i++) {
  59. const value = array[i].getBoolWithDefault(1, false);
  60. const expectedValue = expected[i].getBoolWithDefault(1, false);
  61. expect(value).toBe(expectedValue);
  62. }
  63. }
  64. describe('LazyAccessor for repeated boolean does', () => {
  65. it('return empty array for the empty input', () => {
  66. const accessor = LazyAccessor.createEmpty();
  67. expectEqualToArray(accessor.getRepeatedBoolIterable(1), []);
  68. });
  69. it('ensure not the same instance returned for the empty input', () => {
  70. const accessor = LazyAccessor.createEmpty();
  71. const list1 = accessor.getRepeatedBoolIterable(1);
  72. const list2 = accessor.getRepeatedBoolIterable(1);
  73. expect(list1).not.toBe(list2);
  74. });
  75. it('return size for the empty input', () => {
  76. const accessor = LazyAccessor.createEmpty();
  77. expect(accessor.getRepeatedBoolSize(1)).toEqual(0);
  78. });
  79. it('return unpacked values from the input', () => {
  80. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  81. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  82. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, false]);
  83. });
  84. it('ensure not the same instance returned for unpacked values', () => {
  85. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  86. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  87. const list1 = accessor.getRepeatedBoolIterable(1);
  88. const list2 = accessor.getRepeatedBoolIterable(1);
  89. expect(list1).not.toBe(list2);
  90. });
  91. it('return unpacked multibytes values from the input', () => {
  92. const bytes = createArrayBuffer(0x08, 0x80, 0x01, 0x08, 0x80, 0x00);
  93. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  94. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, false]);
  95. });
  96. it('return for adding single unpacked value', () => {
  97. const accessor = LazyAccessor.createEmpty();
  98. accessor.addUnpackedBoolElement(1, true);
  99. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true]);
  100. accessor.addUnpackedBoolElement(1, false);
  101. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, false]);
  102. });
  103. it('return for adding unpacked values', () => {
  104. const accessor = LazyAccessor.createEmpty();
  105. accessor.addUnpackedBoolIterable(1, [true]);
  106. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true]);
  107. accessor.addUnpackedBoolIterable(1, [false]);
  108. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, false]);
  109. });
  110. it('return for setting single unpacked value', () => {
  111. const accessor =
  112. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00, 0x08, 0x01));
  113. accessor.setUnpackedBoolElement(1, 0, true);
  114. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, true]);
  115. });
  116. it('return for setting unpacked values', () => {
  117. const accessor = LazyAccessor.createEmpty();
  118. accessor.setUnpackedBoolIterable(1, [true]);
  119. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true]);
  120. accessor.setUnpackedBoolIterable(1, [false]);
  121. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [false]);
  122. });
  123. it('encode for adding single unpacked value', () => {
  124. const accessor = LazyAccessor.createEmpty();
  125. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  126. accessor.addUnpackedBoolElement(1, true);
  127. accessor.addUnpackedBoolElement(1, false);
  128. expect(accessor.serialize()).toEqual(bytes);
  129. });
  130. it('encode for adding unpacked values', () => {
  131. const accessor = LazyAccessor.createEmpty();
  132. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  133. accessor.addUnpackedBoolIterable(1, [true, false]);
  134. expect(accessor.serialize()).toEqual(bytes);
  135. });
  136. it('encode for setting single unpacked value', () => {
  137. const accessor =
  138. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x02, 0x00, 0x01));
  139. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x01);
  140. accessor.setUnpackedBoolElement(1, 0, true);
  141. expect(accessor.serialize()).toEqual(bytes);
  142. });
  143. it('encode for setting unpacked values', () => {
  144. const accessor = LazyAccessor.createEmpty();
  145. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  146. accessor.setUnpackedBoolIterable(1, [true, false]);
  147. expect(accessor.serialize()).toEqual(bytes);
  148. });
  149. it('return packed values from the input', () => {
  150. const bytes = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  151. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  152. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, false]);
  153. });
  154. it('ensure not the same instance returned for packed values', () => {
  155. const bytes = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  156. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  157. const list1 = accessor.getRepeatedBoolIterable(1);
  158. const list2 = accessor.getRepeatedBoolIterable(1);
  159. expect(list1).not.toBe(list2);
  160. });
  161. it('return packed multibytes values from the input', () => {
  162. const bytes = createArrayBuffer(0x0A, 0x04, 0x80, 0x01, 0x80, 0x00);
  163. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  164. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, false]);
  165. });
  166. it('return for adding single packed value', () => {
  167. const accessor = LazyAccessor.createEmpty();
  168. accessor.addPackedBoolElement(1, true);
  169. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true]);
  170. accessor.addPackedBoolElement(1, false);
  171. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, false]);
  172. });
  173. it('return for adding packed values', () => {
  174. const accessor = LazyAccessor.createEmpty();
  175. accessor.addPackedBoolIterable(1, [true]);
  176. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true]);
  177. accessor.addPackedBoolIterable(1, [false]);
  178. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, false]);
  179. });
  180. it('return for setting single packed value', () => {
  181. const accessor =
  182. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00, 0x08, 0x01));
  183. accessor.setPackedBoolElement(1, 0, true);
  184. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true, true]);
  185. });
  186. it('return for setting packed values', () => {
  187. const accessor = LazyAccessor.createEmpty();
  188. accessor.setPackedBoolIterable(1, [true]);
  189. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true]);
  190. accessor.setPackedBoolIterable(1, [false]);
  191. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [false]);
  192. });
  193. it('encode for adding single packed value', () => {
  194. const accessor = LazyAccessor.createEmpty();
  195. const bytes = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  196. accessor.addPackedBoolElement(1, true);
  197. accessor.addPackedBoolElement(1, false);
  198. expect(accessor.serialize()).toEqual(bytes);
  199. });
  200. it('encode for adding packed values', () => {
  201. const accessor = LazyAccessor.createEmpty();
  202. const bytes = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  203. accessor.addPackedBoolIterable(1, [true, false]);
  204. expect(accessor.serialize()).toEqual(bytes);
  205. });
  206. it('encode for setting single packed value', () => {
  207. const accessor =
  208. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00, 0x08, 0x01));
  209. const bytes = createArrayBuffer(0x0A, 0x02, 0x01, 0x01);
  210. accessor.setPackedBoolElement(1, 0, true);
  211. expect(accessor.serialize()).toEqual(bytes);
  212. });
  213. it('encode for setting packed values', () => {
  214. const accessor = LazyAccessor.createEmpty();
  215. const bytes = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  216. accessor.setPackedBoolIterable(1, [true, false]);
  217. expect(accessor.serialize()).toEqual(bytes);
  218. });
  219. it('return combined values from the input', () => {
  220. const bytes =
  221. createArrayBuffer(0x08, 0x01, 0x0A, 0x02, 0x01, 0x00, 0x08, 0x00);
  222. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  223. expectEqualToArray(
  224. accessor.getRepeatedBoolIterable(1), [true, true, false, false]);
  225. });
  226. it('return the repeated field element from the input', () => {
  227. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  228. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  229. expect(accessor.getRepeatedBoolElement(
  230. /* fieldNumber= */ 1, /* index= */ 0))
  231. .toEqual(true);
  232. expect(accessor.getRepeatedBoolElement(
  233. /* fieldNumber= */ 1, /* index= */ 1))
  234. .toEqual(false);
  235. });
  236. it('return the size from the input', () => {
  237. const bytes = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  238. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  239. expect(accessor.getRepeatedBoolSize(1)).toEqual(2);
  240. });
  241. it('fail when getting unpacked bool value with other wire types', () => {
  242. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  243. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  244. if (CHECK_CRITICAL_STATE) {
  245. expect(() => {
  246. accessor.getRepeatedBoolIterable(1);
  247. }).toThrowError('Expected wire type: 0 but found: 1');
  248. } else {
  249. // Note in unchecked mode we produce invalid output for invalid inputs.
  250. // This test just documents our behavior in those cases.
  251. // These values might change at any point and are not considered
  252. // what the implementation should be doing here.
  253. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [true]);
  254. }
  255. });
  256. it('fail when adding unpacked bool values with number value', () => {
  257. const accessor = LazyAccessor.createEmpty();
  258. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  259. if (CHECK_CRITICAL_STATE) {
  260. expect(() => accessor.addUnpackedBoolIterable(1, [fakeBoolean]))
  261. .toThrowError('Must be a boolean, but got: 2');
  262. } else {
  263. // Note in unchecked mode we produce invalid output for invalid inputs.
  264. // This test just documents our behavior in those cases.
  265. // These values might change at any point and are not considered
  266. // what the implementation should be doing here.
  267. accessor.addUnpackedBoolIterable(1, [fakeBoolean]);
  268. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [fakeBoolean]);
  269. }
  270. });
  271. it('fail when adding single unpacked bool value with number value', () => {
  272. const accessor = LazyAccessor.createEmpty();
  273. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  274. if (CHECK_CRITICAL_STATE) {
  275. expect(() => accessor.addUnpackedBoolElement(1, fakeBoolean))
  276. .toThrowError('Must be a boolean, but got: 2');
  277. } else {
  278. // Note in unchecked mode we produce invalid output for invalid inputs.
  279. // This test just documents our behavior in those cases.
  280. // These values might change at any point and are not considered
  281. // what the implementation should be doing here.
  282. accessor.addUnpackedBoolElement(1, fakeBoolean);
  283. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [fakeBoolean]);
  284. }
  285. });
  286. it('fail when setting unpacked bool values with number value', () => {
  287. const accessor = LazyAccessor.createEmpty();
  288. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  289. if (CHECK_CRITICAL_STATE) {
  290. expect(() => accessor.setUnpackedBoolIterable(1, [fakeBoolean]))
  291. .toThrowError('Must be a boolean, but got: 2');
  292. } else {
  293. // Note in unchecked mode we produce invalid output for invalid inputs.
  294. // This test just documents our behavior in those cases.
  295. // These values might change at any point and are not considered
  296. // what the implementation should be doing here.
  297. accessor.setUnpackedBoolIterable(1, [fakeBoolean]);
  298. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [fakeBoolean]);
  299. }
  300. });
  301. it('fail when setting single unpacked bool value with number value', () => {
  302. const accessor =
  303. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  304. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  305. if (CHECK_CRITICAL_STATE) {
  306. expect(() => accessor.setUnpackedBoolElement(1, 0, fakeBoolean))
  307. .toThrowError('Must be a boolean, but got: 2');
  308. } else {
  309. // Note in unchecked mode we produce invalid output for invalid inputs.
  310. // This test just documents our behavior in those cases.
  311. // These values might change at any point and are not considered
  312. // what the implementation should be doing here.
  313. accessor.setUnpackedBoolElement(1, 0, fakeBoolean);
  314. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [fakeBoolean]);
  315. }
  316. });
  317. it('fail when adding packed bool values with number value', () => {
  318. const accessor = LazyAccessor.createEmpty();
  319. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  320. if (CHECK_CRITICAL_STATE) {
  321. expect(() => accessor.addPackedBoolIterable(1, [fakeBoolean]))
  322. .toThrowError('Must be a boolean, but got: 2');
  323. } else {
  324. // Note in unchecked mode we produce invalid output for invalid inputs.
  325. // This test just documents our behavior in those cases.
  326. // These values might change at any point and are not considered
  327. // what the implementation should be doing here.
  328. accessor.addPackedBoolIterable(1, [fakeBoolean]);
  329. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [fakeBoolean]);
  330. }
  331. });
  332. it('fail when adding single packed bool value with number value', () => {
  333. const accessor = LazyAccessor.createEmpty();
  334. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  335. if (CHECK_CRITICAL_STATE) {
  336. expect(() => accessor.addPackedBoolElement(1, fakeBoolean))
  337. .toThrowError('Must be a boolean, but got: 2');
  338. } else {
  339. // Note in unchecked mode we produce invalid output for invalid inputs.
  340. // This test just documents our behavior in those cases.
  341. // These values might change at any point and are not considered
  342. // what the implementation should be doing here.
  343. accessor.addPackedBoolElement(1, fakeBoolean);
  344. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [fakeBoolean]);
  345. }
  346. });
  347. it('fail when setting packed bool values with number value', () => {
  348. const accessor = LazyAccessor.createEmpty();
  349. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  350. if (CHECK_CRITICAL_STATE) {
  351. expect(() => accessor.setPackedBoolIterable(1, [fakeBoolean]))
  352. .toThrowError('Must be a boolean, but got: 2');
  353. } else {
  354. // Note in unchecked mode we produce invalid output for invalid inputs.
  355. // This test just documents our behavior in those cases.
  356. // These values might change at any point and are not considered
  357. // what the implementation should be doing here.
  358. accessor.setPackedBoolIterable(1, [fakeBoolean]);
  359. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [fakeBoolean]);
  360. }
  361. });
  362. it('fail when setting single packed bool value with number value', () => {
  363. const accessor =
  364. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  365. const fakeBoolean = /** @type {boolean} */ (/** @type {*} */ (2));
  366. if (CHECK_CRITICAL_STATE) {
  367. expect(() => accessor.setPackedBoolElement(1, 0, fakeBoolean))
  368. .toThrowError('Must be a boolean, but got: 2');
  369. } else {
  370. // Note in unchecked mode we produce invalid output for invalid inputs.
  371. // This test just documents our behavior in those cases.
  372. // These values might change at any point and are not considered
  373. // what the implementation should be doing here.
  374. accessor.setPackedBoolElement(1, 0, fakeBoolean);
  375. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [fakeBoolean]);
  376. }
  377. });
  378. it('fail when setting single unpacked with out-of-bound index', () => {
  379. const accessor =
  380. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  381. if (CHECK_CRITICAL_STATE) {
  382. expect(() => accessor.setUnpackedBoolElement(1, 1, true))
  383. .toThrowError('Index out of bounds: index: 1 size: 1');
  384. } else {
  385. // Note in unchecked mode we produce invalid output for invalid inputs.
  386. // This test just documents our behavior in those cases.
  387. // These values might change at any point and are not considered
  388. // what the implementation should be doing here.
  389. accessor.setUnpackedBoolElement(1, 1, true);
  390. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [false, true]);
  391. }
  392. });
  393. it('fail when setting single packed with out-of-bound index', () => {
  394. const accessor =
  395. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  396. if (CHECK_CRITICAL_STATE) {
  397. expect(() => accessor.setPackedBoolElement(1, 1, true))
  398. .toThrowError('Index out of bounds: index: 1 size: 1');
  399. } else {
  400. // Note in unchecked mode we produce invalid output for invalid inputs.
  401. // This test just documents our behavior in those cases.
  402. // These values might change at any point and are not considered
  403. // what the implementation should be doing here.
  404. accessor.setPackedBoolElement(1, 1, true);
  405. expectEqualToArray(accessor.getRepeatedBoolIterable(1), [false, true]);
  406. }
  407. });
  408. it('fail when getting element with out-of-range index', () => {
  409. const accessor = LazyAccessor.createEmpty();
  410. if (CHECK_CRITICAL_STATE) {
  411. expect(() => {
  412. accessor.getRepeatedBoolElement(
  413. /* fieldNumber= */ 1, /* index= */ 0);
  414. }).toThrowError('Index out of bounds: index: 0 size: 0');
  415. } else {
  416. // Note in unchecked mode we produce invalid output for invalid inputs.
  417. // This test just documents our behavior in those cases.
  418. // These values might change at any point and are not considered
  419. // what the implementation should be doing here.
  420. expect(accessor.getRepeatedBoolElement(
  421. /* fieldNumber= */ 1, /* index= */ 0))
  422. .toBe(undefined);
  423. }
  424. });
  425. });
  426. describe('LazyAccessor for repeated double does', () => {
  427. const value1 = 1;
  428. const value2 = 0;
  429. const unpackedValue1Value2 = createArrayBuffer(
  430. 0x09,
  431. 0x00,
  432. 0x00,
  433. 0x00,
  434. 0x00,
  435. 0x00,
  436. 0x00,
  437. 0xF0,
  438. 0x3F, // value1
  439. 0x09,
  440. 0x00,
  441. 0x00,
  442. 0x00,
  443. 0x00,
  444. 0x00,
  445. 0x00,
  446. 0x00,
  447. 0x00, // value2
  448. );
  449. const unpackedValue2Value1 = createArrayBuffer(
  450. 0x09,
  451. 0x00,
  452. 0x00,
  453. 0x00,
  454. 0x00,
  455. 0x00,
  456. 0x00,
  457. 0x00,
  458. 0x00, // value1
  459. 0x09,
  460. 0x00,
  461. 0x00,
  462. 0x00,
  463. 0x00,
  464. 0x00,
  465. 0x00,
  466. 0xF0,
  467. 0x3F, // value2
  468. );
  469. const packedValue1Value2 = createArrayBuffer(
  470. 0x0A,
  471. 0x10, // tag
  472. 0x00,
  473. 0x00,
  474. 0x00,
  475. 0x00,
  476. 0x00,
  477. 0x00,
  478. 0xF0,
  479. 0x3F, // value1
  480. 0x00,
  481. 0x00,
  482. 0x00,
  483. 0x00,
  484. 0x00,
  485. 0x00,
  486. 0x00,
  487. 0x00, // value2
  488. );
  489. const packedValue2Value1 = createArrayBuffer(
  490. 0x0A,
  491. 0x10, // tag
  492. 0x00,
  493. 0x00,
  494. 0x00,
  495. 0x00,
  496. 0x00,
  497. 0x00,
  498. 0x00,
  499. 0x00, // value1
  500. 0x00,
  501. 0x00,
  502. 0x00,
  503. 0x00,
  504. 0x00,
  505. 0x00,
  506. 0xF0,
  507. 0x3F, // value2
  508. );
  509. it('return empty array for the empty input', () => {
  510. const accessor = LazyAccessor.createEmpty();
  511. const list = accessor.getRepeatedDoubleIterable(1);
  512. expectEqualToArray(list, []);
  513. });
  514. it('ensure not the same instance returned for the empty input', () => {
  515. const accessor = LazyAccessor.createEmpty();
  516. const list1 = accessor.getRepeatedDoubleIterable(1);
  517. const list2 = accessor.getRepeatedDoubleIterable(1);
  518. expect(list1).not.toBe(list2);
  519. });
  520. it('return size for the empty input', () => {
  521. const accessor = LazyAccessor.createEmpty();
  522. const size = accessor.getRepeatedDoubleSize(1);
  523. expect(size).toEqual(0);
  524. });
  525. it('return unpacked values from the input', () => {
  526. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  527. const list = accessor.getRepeatedDoubleIterable(1);
  528. expectEqualToArray(list, [value1, value2]);
  529. });
  530. it('ensure not the same instance returned for unpacked values', () => {
  531. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  532. const list1 = accessor.getRepeatedDoubleIterable(1);
  533. const list2 = accessor.getRepeatedDoubleIterable(1);
  534. expect(list1).not.toBe(list2);
  535. });
  536. it('add single unpacked value', () => {
  537. const accessor = LazyAccessor.createEmpty();
  538. accessor.addUnpackedDoubleElement(1, value1);
  539. const list1 = accessor.getRepeatedDoubleIterable(1);
  540. accessor.addUnpackedDoubleElement(1, value2);
  541. const list2 = accessor.getRepeatedDoubleIterable(1);
  542. expectEqualToArray(list1, [value1]);
  543. expectEqualToArray(list2, [value1, value2]);
  544. });
  545. it('add unpacked values', () => {
  546. const accessor = LazyAccessor.createEmpty();
  547. accessor.addUnpackedDoubleIterable(1, [value1]);
  548. const list1 = accessor.getRepeatedDoubleIterable(1);
  549. accessor.addUnpackedDoubleIterable(1, [value2]);
  550. const list2 = accessor.getRepeatedDoubleIterable(1);
  551. expectEqualToArray(list1, [value1]);
  552. expectEqualToArray(list2, [value1, value2]);
  553. });
  554. it('set a single unpacked value', () => {
  555. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  556. accessor.setUnpackedDoubleElement(1, 1, value1);
  557. const list = accessor.getRepeatedDoubleIterable(1);
  558. expectEqualToArray(list, [value1, value1]);
  559. });
  560. it('set unpacked values', () => {
  561. const accessor = LazyAccessor.createEmpty();
  562. accessor.setUnpackedDoubleIterable(1, [value1]);
  563. const list = accessor.getRepeatedDoubleIterable(1);
  564. expectEqualToArray(list, [value1]);
  565. });
  566. it('encode for adding single unpacked value', () => {
  567. const accessor = LazyAccessor.createEmpty();
  568. accessor.addUnpackedDoubleElement(1, value1);
  569. accessor.addUnpackedDoubleElement(1, value2);
  570. const serialized = accessor.serialize();
  571. expect(serialized).toEqual(unpackedValue1Value2);
  572. });
  573. it('encode for adding unpacked values', () => {
  574. const accessor = LazyAccessor.createEmpty();
  575. accessor.addUnpackedDoubleIterable(1, [value1]);
  576. accessor.addUnpackedDoubleIterable(1, [value2]);
  577. const serialized = accessor.serialize();
  578. expect(serialized).toEqual(unpackedValue1Value2);
  579. });
  580. it('encode for setting single unpacked value', () => {
  581. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  582. accessor.setUnpackedDoubleElement(1, 0, value2);
  583. accessor.setUnpackedDoubleElement(1, 1, value1);
  584. const serialized = accessor.serialize();
  585. expect(serialized).toEqual(unpackedValue2Value1);
  586. });
  587. it('encode for setting unpacked values', () => {
  588. const accessor = LazyAccessor.createEmpty();
  589. accessor.setUnpackedDoubleIterable(1, [value1, value2]);
  590. const serialized = accessor.serialize();
  591. expect(serialized).toEqual(unpackedValue1Value2);
  592. });
  593. it('return packed values from the input', () => {
  594. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  595. const list = accessor.getRepeatedDoubleIterable(1);
  596. expectEqualToArray(list, [value1, value2]);
  597. });
  598. it('ensure not the same instance returned for packed values', () => {
  599. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  600. const list1 = accessor.getRepeatedDoubleIterable(1);
  601. const list2 = accessor.getRepeatedDoubleIterable(1);
  602. expect(list1).not.toBe(list2);
  603. });
  604. it('add single packed value', () => {
  605. const accessor = LazyAccessor.createEmpty();
  606. accessor.addPackedDoubleElement(1, value1);
  607. const list1 = accessor.getRepeatedDoubleIterable(1);
  608. accessor.addPackedDoubleElement(1, value2);
  609. const list2 = accessor.getRepeatedDoubleIterable(1);
  610. expectEqualToArray(list1, [value1]);
  611. expectEqualToArray(list2, [value1, value2]);
  612. });
  613. it('add packed values', () => {
  614. const accessor = LazyAccessor.createEmpty();
  615. accessor.addPackedDoubleIterable(1, [value1]);
  616. const list1 = accessor.getRepeatedDoubleIterable(1);
  617. accessor.addPackedDoubleIterable(1, [value2]);
  618. const list2 = accessor.getRepeatedDoubleIterable(1);
  619. expectEqualToArray(list1, [value1]);
  620. expectEqualToArray(list2, [value1, value2]);
  621. });
  622. it('set a single packed value', () => {
  623. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  624. accessor.setPackedDoubleElement(1, 1, value1);
  625. const list = accessor.getRepeatedDoubleIterable(1);
  626. expectEqualToArray(list, [value1, value1]);
  627. });
  628. it('set packed values', () => {
  629. const accessor = LazyAccessor.createEmpty();
  630. accessor.setPackedDoubleIterable(1, [value1]);
  631. const list1 = accessor.getRepeatedDoubleIterable(1);
  632. accessor.setPackedDoubleIterable(1, [value2]);
  633. const list2 = accessor.getRepeatedDoubleIterable(1);
  634. expectEqualToArray(list1, [value1]);
  635. expectEqualToArray(list2, [value2]);
  636. });
  637. it('encode for adding single packed value', () => {
  638. const accessor = LazyAccessor.createEmpty();
  639. accessor.addPackedDoubleElement(1, value1);
  640. accessor.addPackedDoubleElement(1, value2);
  641. const serialized = accessor.serialize();
  642. expect(serialized).toEqual(packedValue1Value2);
  643. });
  644. it('encode for adding packed values', () => {
  645. const accessor = LazyAccessor.createEmpty();
  646. accessor.addPackedDoubleIterable(1, [value1, value2]);
  647. const serialized = accessor.serialize();
  648. expect(serialized).toEqual(packedValue1Value2);
  649. });
  650. it('encode for setting single packed value', () => {
  651. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  652. accessor.setPackedDoubleElement(1, 0, value2);
  653. accessor.setPackedDoubleElement(1, 1, value1);
  654. const serialized = accessor.serialize();
  655. expect(serialized).toEqual(packedValue2Value1);
  656. });
  657. it('encode for setting packed values', () => {
  658. const accessor = LazyAccessor.createEmpty();
  659. accessor.setPackedDoubleIterable(1, [value1, value2]);
  660. const serialized = accessor.serialize();
  661. expect(serialized).toEqual(packedValue1Value2);
  662. });
  663. it('return combined values from the input', () => {
  664. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  665. 0x09,
  666. 0x00,
  667. 0x00,
  668. 0x00,
  669. 0x00,
  670. 0x00,
  671. 0x00,
  672. 0xF0,
  673. 0x3F, // value1
  674. 0x0A,
  675. 0x10, // tag
  676. 0x00,
  677. 0x00,
  678. 0x00,
  679. 0x00,
  680. 0x00,
  681. 0x00,
  682. 0xF0,
  683. 0x3F, // value1
  684. 0x00,
  685. 0x00,
  686. 0x00,
  687. 0x00,
  688. 0x00,
  689. 0x00,
  690. 0x00,
  691. 0x00, // value2
  692. 0x09,
  693. 0x00,
  694. 0x00,
  695. 0x00,
  696. 0x00,
  697. 0x00,
  698. 0x00,
  699. 0x00,
  700. 0x00, // value2
  701. ));
  702. const list = accessor.getRepeatedDoubleIterable(1);
  703. expectEqualToArray(list, [value1, value1, value2, value2]);
  704. });
  705. it('return the repeated field element from the input', () => {
  706. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  707. const result1 = accessor.getRepeatedDoubleElement(
  708. /* fieldNumber= */ 1, /* index= */ 0);
  709. const result2 = accessor.getRepeatedDoubleElement(
  710. /* fieldNumber= */ 1, /* index= */ 1);
  711. expect(result1).toEqual(value1);
  712. expect(result2).toEqual(value2);
  713. });
  714. it('return the size from the input', () => {
  715. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  716. const size = accessor.getRepeatedDoubleSize(1);
  717. expect(size).toEqual(2);
  718. });
  719. it('fail when getting unpacked double value with other wire types', () => {
  720. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  721. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  722. if (CHECK_CRITICAL_STATE) {
  723. expect(() => {
  724. accessor.getRepeatedDoubleIterable(1);
  725. }).toThrowError('Expected wire type: 1 but found: 0');
  726. } else {
  727. // Note in unchecked mode we produce invalid output for invalid inputs.
  728. // This test just documents our behavior in those cases.
  729. // These values might change at any point and are not considered
  730. // what the implementation should be doing here.
  731. expectEqualToArray(
  732. accessor.getRepeatedDoubleIterable(1), [2.937446524422997e-306]);
  733. }
  734. });
  735. it('fail when adding unpacked double values with null value', () => {
  736. const accessor = LazyAccessor.createEmpty();
  737. const fakeDouble = /** @type {number} */ (/** @type {*} */ (null));
  738. if (CHECK_CRITICAL_STATE) {
  739. expect(() => accessor.addUnpackedDoubleIterable(1, [fakeDouble]))
  740. .toThrowError('Must be a number, but got: null');
  741. } else {
  742. // Note in unchecked mode we produce invalid output for invalid inputs.
  743. // This test just documents our behavior in those cases.
  744. // These values might change at any point and are not considered
  745. // what the implementation should be doing here.
  746. accessor.addUnpackedDoubleIterable(1, [fakeDouble]);
  747. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [fakeDouble]);
  748. }
  749. });
  750. it('fail when adding single unpacked double value with null value', () => {
  751. const accessor = LazyAccessor.createEmpty();
  752. const fakeDouble = /** @type {number} */ (/** @type {*} */ (null));
  753. if (CHECK_CRITICAL_STATE) {
  754. expect(() => accessor.addUnpackedDoubleElement(1, fakeDouble))
  755. .toThrowError('Must be a number, but got: null');
  756. } else {
  757. // Note in unchecked mode we produce invalid output for invalid inputs.
  758. // This test just documents our behavior in those cases.
  759. // These values might change at any point and are not considered
  760. // what the implementation should be doing here.
  761. accessor.addUnpackedDoubleElement(1, fakeDouble);
  762. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [fakeDouble]);
  763. }
  764. });
  765. it('fail when setting unpacked double values with null value', () => {
  766. const accessor = LazyAccessor.createEmpty();
  767. const fakeDouble = /** @type {number} */ (/** @type {*} */ (null));
  768. if (CHECK_CRITICAL_STATE) {
  769. expect(() => accessor.setUnpackedDoubleIterable(1, [fakeDouble]))
  770. .toThrowError('Must be a number, but got: null');
  771. } else {
  772. // Note in unchecked mode we produce invalid output for invalid inputs.
  773. // This test just documents our behavior in those cases.
  774. // These values might change at any point and are not considered
  775. // what the implementation should be doing here.
  776. accessor.setUnpackedDoubleIterable(1, [fakeDouble]);
  777. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [fakeDouble]);
  778. }
  779. });
  780. it('fail when setting single unpacked double value with null value', () => {
  781. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  782. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  783. const fakeDouble = /** @type {number} */ (/** @type {*} */ (null));
  784. if (CHECK_CRITICAL_STATE) {
  785. expect(() => accessor.setUnpackedDoubleElement(1, 0, fakeDouble))
  786. .toThrowError('Must be a number, but got: null');
  787. } else {
  788. // Note in unchecked mode we produce invalid output for invalid inputs.
  789. // This test just documents our behavior in those cases.
  790. // These values might change at any point and are not considered
  791. // what the implementation should be doing here.
  792. accessor.setUnpackedDoubleElement(1, 0, fakeDouble);
  793. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [fakeDouble]);
  794. }
  795. });
  796. it('fail when adding packed double values with null value', () => {
  797. const accessor = LazyAccessor.createEmpty();
  798. const fakeDouble = /** @type {number} */ (/** @type {*} */ (null));
  799. if (CHECK_CRITICAL_STATE) {
  800. expect(() => accessor.addPackedDoubleIterable(1, [fakeDouble]))
  801. .toThrowError('Must be a number, but got: null');
  802. } else {
  803. // Note in unchecked mode we produce invalid output for invalid inputs.
  804. // This test just documents our behavior in those cases.
  805. // These values might change at any point and are not considered
  806. // what the implementation should be doing here.
  807. accessor.addPackedDoubleIterable(1, [fakeDouble]);
  808. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [fakeDouble]);
  809. }
  810. });
  811. it('fail when adding single packed double value with null value', () => {
  812. const accessor = LazyAccessor.createEmpty();
  813. const fakeDouble = /** @type {number} */ (/** @type {*} */ (null));
  814. if (CHECK_CRITICAL_STATE) {
  815. expect(() => accessor.addPackedDoubleElement(1, fakeDouble))
  816. .toThrowError('Must be a number, but got: null');
  817. } else {
  818. // Note in unchecked mode we produce invalid output for invalid inputs.
  819. // This test just documents our behavior in those cases.
  820. // These values might change at any point and are not considered
  821. // what the implementation should be doing here.
  822. accessor.addPackedDoubleElement(1, fakeDouble);
  823. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [fakeDouble]);
  824. }
  825. });
  826. it('fail when setting packed double values with null value', () => {
  827. const accessor = LazyAccessor.createEmpty();
  828. const fakeDouble = /** @type {number} */ (/** @type {*} */ (null));
  829. if (CHECK_CRITICAL_STATE) {
  830. expect(() => accessor.setPackedDoubleIterable(1, [fakeDouble]))
  831. .toThrowError('Must be a number, but got: null');
  832. } else {
  833. // Note in unchecked mode we produce invalid output for invalid inputs.
  834. // This test just documents our behavior in those cases.
  835. // These values might change at any point and are not considered
  836. // what the implementation should be doing here.
  837. accessor.setPackedDoubleIterable(1, [fakeDouble]);
  838. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [fakeDouble]);
  839. }
  840. });
  841. it('fail when setting single packed double value with null value', () => {
  842. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  843. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  844. const fakeDouble = /** @type {number} */ (/** @type {*} */ (null));
  845. if (CHECK_CRITICAL_STATE) {
  846. expect(() => accessor.setPackedDoubleElement(1, 0, fakeDouble))
  847. .toThrowError('Must be a number, but got: null');
  848. } else {
  849. // Note in unchecked mode we produce invalid output for invalid inputs.
  850. // This test just documents our behavior in those cases.
  851. // These values might change at any point and are not considered
  852. // what the implementation should be doing here.
  853. accessor.setPackedDoubleElement(1, 0, fakeDouble);
  854. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [fakeDouble]);
  855. }
  856. });
  857. it('fail when setting single unpacked with out-of-bound index', () => {
  858. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  859. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  860. if (CHECK_CRITICAL_STATE) {
  861. expect(() => accessor.setUnpackedDoubleElement(1, 1, 1))
  862. .toThrowError('Index out of bounds: index: 1 size: 1');
  863. } else {
  864. // Note in unchecked mode we produce invalid output for invalid inputs.
  865. // This test just documents our behavior in those cases.
  866. // These values might change at any point and are not considered
  867. // what the implementation should be doing here.
  868. accessor.setUnpackedDoubleElement(1, 1, 1);
  869. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [0, 1]);
  870. }
  871. });
  872. it('fail when setting single packed with out-of-bound index', () => {
  873. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  874. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  875. if (CHECK_CRITICAL_STATE) {
  876. expect(() => accessor.setPackedDoubleElement(1, 1, 1))
  877. .toThrowError('Index out of bounds: index: 1 size: 1');
  878. } else {
  879. // Note in unchecked mode we produce invalid output for invalid inputs.
  880. // This test just documents our behavior in those cases.
  881. // These values might change at any point and are not considered
  882. // what the implementation should be doing here.
  883. accessor.setPackedDoubleElement(1, 1, 1);
  884. expectEqualToArray(accessor.getRepeatedDoubleIterable(1), [0, 1]);
  885. }
  886. });
  887. it('fail when getting element with out-of-range index', () => {
  888. const accessor = LazyAccessor.createEmpty();
  889. if (CHECK_CRITICAL_STATE) {
  890. expect(() => {
  891. accessor.getRepeatedDoubleElement(
  892. /* fieldNumber= */ 1, /* index= */ 0);
  893. }).toThrowError('Index out of bounds: index: 0 size: 0');
  894. } else {
  895. // Note in unchecked mode we produce invalid output for invalid inputs.
  896. // This test just documents our behavior in those cases.
  897. // These values might change at any point and are not considered
  898. // what the implementation should be doing here.
  899. expect(accessor.getRepeatedDoubleElement(
  900. /* fieldNumber= */ 1, /* index= */ 0))
  901. .toBe(undefined);
  902. }
  903. });
  904. });
  905. describe('LazyAccessor for repeated fixed32 does', () => {
  906. const value1 = 1;
  907. const value2 = 0;
  908. const unpackedValue1Value2 = createArrayBuffer(
  909. 0x0D, 0x01, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x00);
  910. const unpackedValue2Value1 = createArrayBuffer(
  911. 0x0D, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x01, 0x00, 0x00, 0x00);
  912. const packedValue1Value2 = createArrayBuffer(
  913. 0x0A, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  914. const packedValue2Value1 = createArrayBuffer(
  915. 0x0A, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00);
  916. it('return empty array for the empty input', () => {
  917. const accessor = LazyAccessor.createEmpty();
  918. const list = accessor.getRepeatedFixed32Iterable(1);
  919. expectEqualToArray(list, []);
  920. });
  921. it('ensure not the same instance returned for the empty input', () => {
  922. const accessor = LazyAccessor.createEmpty();
  923. const list1 = accessor.getRepeatedFixed32Iterable(1);
  924. const list2 = accessor.getRepeatedFixed32Iterable(1);
  925. expect(list1).not.toBe(list2);
  926. });
  927. it('return size for the empty input', () => {
  928. const accessor = LazyAccessor.createEmpty();
  929. const size = accessor.getRepeatedFixed32Size(1);
  930. expect(size).toEqual(0);
  931. });
  932. it('return unpacked values from the input', () => {
  933. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  934. const list = accessor.getRepeatedFixed32Iterable(1);
  935. expectEqualToArray(list, [value1, value2]);
  936. });
  937. it('ensure not the same instance returned for unpacked values', () => {
  938. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  939. const list1 = accessor.getRepeatedFixed32Iterable(1);
  940. const list2 = accessor.getRepeatedFixed32Iterable(1);
  941. expect(list1).not.toBe(list2);
  942. });
  943. it('add single unpacked value', () => {
  944. const accessor = LazyAccessor.createEmpty();
  945. accessor.addUnpackedFixed32Element(1, value1);
  946. const list1 = accessor.getRepeatedFixed32Iterable(1);
  947. accessor.addUnpackedFixed32Element(1, value2);
  948. const list2 = accessor.getRepeatedFixed32Iterable(1);
  949. expectEqualToArray(list1, [value1]);
  950. expectEqualToArray(list2, [value1, value2]);
  951. });
  952. it('add unpacked values', () => {
  953. const accessor = LazyAccessor.createEmpty();
  954. accessor.addUnpackedFixed32Iterable(1, [value1]);
  955. const list1 = accessor.getRepeatedFixed32Iterable(1);
  956. accessor.addUnpackedFixed32Iterable(1, [value2]);
  957. const list2 = accessor.getRepeatedFixed32Iterable(1);
  958. expectEqualToArray(list1, [value1]);
  959. expectEqualToArray(list2, [value1, value2]);
  960. });
  961. it('set a single unpacked value', () => {
  962. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  963. accessor.setUnpackedFixed32Element(1, 1, value1);
  964. const list = accessor.getRepeatedFixed32Iterable(1);
  965. expectEqualToArray(list, [value1, value1]);
  966. });
  967. it('set unpacked values', () => {
  968. const accessor = LazyAccessor.createEmpty();
  969. accessor.setUnpackedFixed32Iterable(1, [value1]);
  970. const list = accessor.getRepeatedFixed32Iterable(1);
  971. expectEqualToArray(list, [value1]);
  972. });
  973. it('encode for adding single unpacked value', () => {
  974. const accessor = LazyAccessor.createEmpty();
  975. accessor.addUnpackedFixed32Element(1, value1);
  976. accessor.addUnpackedFixed32Element(1, value2);
  977. const serialized = accessor.serialize();
  978. expect(serialized).toEqual(unpackedValue1Value2);
  979. });
  980. it('encode for adding unpacked values', () => {
  981. const accessor = LazyAccessor.createEmpty();
  982. accessor.addUnpackedFixed32Iterable(1, [value1]);
  983. accessor.addUnpackedFixed32Iterable(1, [value2]);
  984. const serialized = accessor.serialize();
  985. expect(serialized).toEqual(unpackedValue1Value2);
  986. });
  987. it('encode for setting single unpacked value', () => {
  988. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  989. accessor.setUnpackedFixed32Element(1, 0, value2);
  990. accessor.setUnpackedFixed32Element(1, 1, value1);
  991. const serialized = accessor.serialize();
  992. expect(serialized).toEqual(unpackedValue2Value1);
  993. });
  994. it('encode for setting unpacked values', () => {
  995. const accessor = LazyAccessor.createEmpty();
  996. accessor.setUnpackedFixed32Iterable(1, [value1, value2]);
  997. const serialized = accessor.serialize();
  998. expect(serialized).toEqual(unpackedValue1Value2);
  999. });
  1000. it('return packed values from the input', () => {
  1001. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  1002. const list = accessor.getRepeatedFixed32Iterable(1);
  1003. expectEqualToArray(list, [value1, value2]);
  1004. });
  1005. it('ensure not the same instance returned for packed values', () => {
  1006. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  1007. const list1 = accessor.getRepeatedFixed32Iterable(1);
  1008. const list2 = accessor.getRepeatedFixed32Iterable(1);
  1009. expect(list1).not.toBe(list2);
  1010. });
  1011. it('add single packed value', () => {
  1012. const accessor = LazyAccessor.createEmpty();
  1013. accessor.addPackedFixed32Element(1, value1);
  1014. const list1 = accessor.getRepeatedFixed32Iterable(1);
  1015. accessor.addPackedFixed32Element(1, value2);
  1016. const list2 = accessor.getRepeatedFixed32Iterable(1);
  1017. expectEqualToArray(list1, [value1]);
  1018. expectEqualToArray(list2, [value1, value2]);
  1019. });
  1020. it('add packed values', () => {
  1021. const accessor = LazyAccessor.createEmpty();
  1022. accessor.addPackedFixed32Iterable(1, [value1]);
  1023. const list1 = accessor.getRepeatedFixed32Iterable(1);
  1024. accessor.addPackedFixed32Iterable(1, [value2]);
  1025. const list2 = accessor.getRepeatedFixed32Iterable(1);
  1026. expectEqualToArray(list1, [value1]);
  1027. expectEqualToArray(list2, [value1, value2]);
  1028. });
  1029. it('set a single packed value', () => {
  1030. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1031. accessor.setPackedFixed32Element(1, 1, value1);
  1032. const list = accessor.getRepeatedFixed32Iterable(1);
  1033. expectEqualToArray(list, [value1, value1]);
  1034. });
  1035. it('set packed values', () => {
  1036. const accessor = LazyAccessor.createEmpty();
  1037. accessor.setPackedFixed32Iterable(1, [value1]);
  1038. const list1 = accessor.getRepeatedFixed32Iterable(1);
  1039. accessor.setPackedFixed32Iterable(1, [value2]);
  1040. const list2 = accessor.getRepeatedFixed32Iterable(1);
  1041. expectEqualToArray(list1, [value1]);
  1042. expectEqualToArray(list2, [value2]);
  1043. });
  1044. it('encode for adding single packed value', () => {
  1045. const accessor = LazyAccessor.createEmpty();
  1046. accessor.addPackedFixed32Element(1, value1);
  1047. accessor.addPackedFixed32Element(1, value2);
  1048. const serialized = accessor.serialize();
  1049. expect(serialized).toEqual(packedValue1Value2);
  1050. });
  1051. it('encode for adding packed values', () => {
  1052. const accessor = LazyAccessor.createEmpty();
  1053. accessor.addPackedFixed32Iterable(1, [value1, value2]);
  1054. const serialized = accessor.serialize();
  1055. expect(serialized).toEqual(packedValue1Value2);
  1056. });
  1057. it('encode for setting single packed value', () => {
  1058. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1059. accessor.setPackedFixed32Element(1, 0, value2);
  1060. accessor.setPackedFixed32Element(1, 1, value1);
  1061. const serialized = accessor.serialize();
  1062. expect(serialized).toEqual(packedValue2Value1);
  1063. });
  1064. it('encode for setting packed values', () => {
  1065. const accessor = LazyAccessor.createEmpty();
  1066. accessor.setPackedFixed32Iterable(1, [value1, value2]);
  1067. const serialized = accessor.serialize();
  1068. expect(serialized).toEqual(packedValue1Value2);
  1069. });
  1070. it('return combined values from the input', () => {
  1071. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1072. 0x0D,
  1073. 0x01,
  1074. 0x00,
  1075. 0x00,
  1076. 0x00, // value1
  1077. 0x0A,
  1078. 0x08, // tag
  1079. 0x01,
  1080. 0x00,
  1081. 0x00,
  1082. 0x00, // value1
  1083. 0x00,
  1084. 0x00,
  1085. 0x00,
  1086. 0x00, // value2
  1087. 0x0D,
  1088. 0x00,
  1089. 0x00,
  1090. 0x00,
  1091. 0x00, // value2
  1092. ));
  1093. const list = accessor.getRepeatedFixed32Iterable(1);
  1094. expectEqualToArray(list, [value1, value1, value2, value2]);
  1095. });
  1096. it('return the repeated field element from the input', () => {
  1097. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1098. const result1 = accessor.getRepeatedFixed32Element(
  1099. /* fieldNumber= */ 1, /* index= */ 0);
  1100. const result2 = accessor.getRepeatedFixed32Element(
  1101. /* fieldNumber= */ 1, /* index= */ 1);
  1102. expect(result1).toEqual(value1);
  1103. expect(result2).toEqual(value2);
  1104. });
  1105. it('return the size from the input', () => {
  1106. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1107. const size = accessor.getRepeatedFixed32Size(1);
  1108. expect(size).toEqual(2);
  1109. });
  1110. it('fail when getting unpacked fixed32 value with other wire types', () => {
  1111. const accessor = LazyAccessor.fromArrayBuffer(
  1112. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  1113. if (CHECK_CRITICAL_STATE) {
  1114. expect(() => {
  1115. accessor.getRepeatedFixed32Iterable(1);
  1116. }).toThrowError('Expected wire type: 5 but found: 0');
  1117. } else {
  1118. // Note in unchecked mode we produce invalid output for invalid inputs.
  1119. // This test just documents our behavior in those cases.
  1120. // These values might change at any point and are not considered
  1121. // what the implementation should be doing here.
  1122. expectQualifiedIterable(
  1123. accessor.getRepeatedFixed32Iterable(1),
  1124. (value) => Number.isInteger(value));
  1125. }
  1126. });
  1127. it('fail when adding unpacked fixed32 values with null value', () => {
  1128. const accessor = LazyAccessor.createEmpty();
  1129. const fakeFixed32 = /** @type {number} */ (/** @type {*} */ (null));
  1130. if (CHECK_CRITICAL_STATE) {
  1131. expect(() => accessor.addUnpackedFixed32Iterable(1, [fakeFixed32]))
  1132. .toThrowError('Must be a number, but got: null');
  1133. } else {
  1134. // Note in unchecked mode we produce invalid output for invalid inputs.
  1135. // This test just documents our behavior in those cases.
  1136. // These values might change at any point and are not considered
  1137. // what the implementation should be doing here.
  1138. accessor.addUnpackedFixed32Iterable(1, [fakeFixed32]);
  1139. expectQualifiedIterable(accessor.getRepeatedFixed32Iterable(1));
  1140. }
  1141. });
  1142. it('fail when adding single unpacked fixed32 value with null value', () => {
  1143. const accessor = LazyAccessor.createEmpty();
  1144. const fakeFixed32 = /** @type {number} */ (/** @type {*} */ (null));
  1145. if (CHECK_CRITICAL_STATE) {
  1146. expect(() => accessor.addUnpackedFixed32Element(1, fakeFixed32))
  1147. .toThrowError('Must be a number, but got: null');
  1148. } else {
  1149. // Note in unchecked mode we produce invalid output for invalid inputs.
  1150. // This test just documents our behavior in those cases.
  1151. // These values might change at any point and are not considered
  1152. // what the implementation should be doing here.
  1153. accessor.addUnpackedFixed32Element(1, fakeFixed32);
  1154. expectQualifiedIterable(accessor.getRepeatedFixed32Iterable(1));
  1155. }
  1156. });
  1157. it('fail when setting unpacked fixed32 values with null value', () => {
  1158. const accessor = LazyAccessor.createEmpty();
  1159. const fakeFixed32 = /** @type {number} */ (/** @type {*} */ (null));
  1160. if (CHECK_CRITICAL_STATE) {
  1161. expect(() => accessor.setUnpackedFixed32Iterable(1, [fakeFixed32]))
  1162. .toThrowError('Must be a number, but got: null');
  1163. } else {
  1164. // Note in unchecked mode we produce invalid output for invalid inputs.
  1165. // This test just documents our behavior in those cases.
  1166. // These values might change at any point and are not considered
  1167. // what the implementation should be doing here.
  1168. accessor.setUnpackedFixed32Iterable(1, [fakeFixed32]);
  1169. expectQualifiedIterable(accessor.getRepeatedFixed32Iterable(1));
  1170. }
  1171. });
  1172. it('fail when setting single unpacked fixed32 value with null value', () => {
  1173. const accessor = LazyAccessor.fromArrayBuffer(
  1174. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  1175. const fakeFixed32 = /** @type {number} */ (/** @type {*} */ (null));
  1176. if (CHECK_CRITICAL_STATE) {
  1177. expect(() => accessor.setUnpackedFixed32Element(1, 0, fakeFixed32))
  1178. .toThrowError('Must be a number, but got: null');
  1179. } else {
  1180. // Note in unchecked mode we produce invalid output for invalid inputs.
  1181. // This test just documents our behavior in those cases.
  1182. // These values might change at any point and are not considered
  1183. // what the implementation should be doing here.
  1184. accessor.setUnpackedFixed32Element(1, 0, fakeFixed32);
  1185. expectQualifiedIterable(
  1186. accessor.getRepeatedFixed32Iterable(1),
  1187. );
  1188. }
  1189. });
  1190. it('fail when adding packed fixed32 values with null value', () => {
  1191. const accessor = LazyAccessor.createEmpty();
  1192. const fakeFixed32 = /** @type {number} */ (/** @type {*} */ (null));
  1193. if (CHECK_CRITICAL_STATE) {
  1194. expect(() => accessor.addPackedFixed32Iterable(1, [fakeFixed32]))
  1195. .toThrowError('Must be a number, but got: null');
  1196. } else {
  1197. // Note in unchecked mode we produce invalid output for invalid inputs.
  1198. // This test just documents our behavior in those cases.
  1199. // These values might change at any point and are not considered
  1200. // what the implementation should be doing here.
  1201. accessor.addPackedFixed32Iterable(1, [fakeFixed32]);
  1202. expectQualifiedIterable(accessor.getRepeatedFixed32Iterable(1));
  1203. }
  1204. });
  1205. it('fail when adding single packed fixed32 value with null value', () => {
  1206. const accessor = LazyAccessor.createEmpty();
  1207. const fakeFixed32 = /** @type {number} */ (/** @type {*} */ (null));
  1208. if (CHECK_CRITICAL_STATE) {
  1209. expect(() => accessor.addPackedFixed32Element(1, fakeFixed32))
  1210. .toThrowError('Must be a number, but got: null');
  1211. } else {
  1212. // Note in unchecked mode we produce invalid output for invalid inputs.
  1213. // This test just documents our behavior in those cases.
  1214. // These values might change at any point and are not considered
  1215. // what the implementation should be doing here.
  1216. accessor.addPackedFixed32Element(1, fakeFixed32);
  1217. expectQualifiedIterable(accessor.getRepeatedFixed32Iterable(1));
  1218. }
  1219. });
  1220. it('fail when setting packed fixed32 values with null value', () => {
  1221. const accessor = LazyAccessor.createEmpty();
  1222. const fakeFixed32 = /** @type {number} */ (/** @type {*} */ (null));
  1223. if (CHECK_CRITICAL_STATE) {
  1224. expect(() => accessor.setPackedFixed32Iterable(1, [fakeFixed32]))
  1225. .toThrowError('Must be a number, but got: null');
  1226. } else {
  1227. // Note in unchecked mode we produce invalid output for invalid inputs.
  1228. // This test just documents our behavior in those cases.
  1229. // These values might change at any point and are not considered
  1230. // what the implementation should be doing here.
  1231. accessor.setPackedFixed32Iterable(1, [fakeFixed32]);
  1232. expectQualifiedIterable(accessor.getRepeatedFixed32Iterable(1));
  1233. }
  1234. });
  1235. it('fail when setting single packed fixed32 value with null value', () => {
  1236. const accessor = LazyAccessor.fromArrayBuffer(
  1237. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1238. const fakeFixed32 = /** @type {number} */ (/** @type {*} */ (null));
  1239. if (CHECK_CRITICAL_STATE) {
  1240. expect(() => accessor.setPackedFixed32Element(1, 0, fakeFixed32))
  1241. .toThrowError('Must be a number, but got: null');
  1242. } else {
  1243. // Note in unchecked mode we produce invalid output for invalid inputs.
  1244. // This test just documents our behavior in those cases.
  1245. // These values might change at any point and are not considered
  1246. // what the implementation should be doing here.
  1247. accessor.setPackedFixed32Element(1, 0, fakeFixed32);
  1248. expectQualifiedIterable(accessor.getRepeatedFixed32Iterable(1));
  1249. }
  1250. });
  1251. it('fail when setting single unpacked with out-of-bound index', () => {
  1252. const accessor = LazyAccessor.fromArrayBuffer(
  1253. createArrayBuffer(0x0A, 0x04, 0x00, 0x00, 0x00, 0x00));
  1254. if (CHECK_CRITICAL_STATE) {
  1255. expect(() => accessor.setUnpackedFixed32Element(1, 1, 1))
  1256. .toThrowError('Index out of bounds: index: 1 size: 1');
  1257. } else {
  1258. // Note in unchecked mode we produce invalid output for invalid inputs.
  1259. // This test just documents our behavior in those cases.
  1260. // These values might change at any point and are not considered
  1261. // what the implementation should be doing here.
  1262. accessor.setUnpackedFixed32Element(1, 1, 1);
  1263. expectQualifiedIterable(
  1264. accessor.getRepeatedFixed32Iterable(1),
  1265. (value) => Number.isInteger(value));
  1266. }
  1267. });
  1268. it('fail when setting single packed with out-of-bound index', () => {
  1269. const accessor = LazyAccessor.fromArrayBuffer(
  1270. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  1271. if (CHECK_CRITICAL_STATE) {
  1272. expect(() => accessor.setPackedFixed32Element(1, 1, 1))
  1273. .toThrowError('Index out of bounds: index: 1 size: 1');
  1274. } else {
  1275. // Note in unchecked mode we produce invalid output for invalid inputs.
  1276. // This test just documents our behavior in those cases.
  1277. // These values might change at any point and are not considered
  1278. // what the implementation should be doing here.
  1279. accessor.setPackedFixed32Element(1, 1, 1);
  1280. expectQualifiedIterable(
  1281. accessor.getRepeatedFixed32Iterable(1),
  1282. (value) => Number.isInteger(value));
  1283. }
  1284. });
  1285. it('fail when getting element with out-of-range index', () => {
  1286. const accessor = LazyAccessor.createEmpty();
  1287. if (CHECK_CRITICAL_STATE) {
  1288. expect(() => {
  1289. accessor.getRepeatedFixed32Element(
  1290. /* fieldNumber= */ 1, /* index= */ 0);
  1291. }).toThrowError('Index out of bounds: index: 0 size: 0');
  1292. } else {
  1293. // Note in unchecked mode we produce invalid output for invalid inputs.
  1294. // This test just documents our behavior in those cases.
  1295. // These values might change at any point and are not considered
  1296. // what the implementation should be doing here.
  1297. expect(accessor.getRepeatedFixed32Element(
  1298. /* fieldNumber= */ 1, /* index= */ 0))
  1299. .toBe(undefined);
  1300. }
  1301. });
  1302. });
  1303. describe('LazyAccessor for repeated fixed64 does', () => {
  1304. const value1 = Int64.fromInt(1);
  1305. const value2 = Int64.fromInt(0);
  1306. const unpackedValue1Value2 = createArrayBuffer(
  1307. 0x09,
  1308. 0x01,
  1309. 0x00,
  1310. 0x00,
  1311. 0x00,
  1312. 0x00,
  1313. 0x00,
  1314. 0x00,
  1315. 0x00, // value1
  1316. 0x09,
  1317. 0x00,
  1318. 0x00,
  1319. 0x00,
  1320. 0x00,
  1321. 0x00,
  1322. 0x00,
  1323. 0x00,
  1324. 0x00, // value2
  1325. );
  1326. const unpackedValue2Value1 = createArrayBuffer(
  1327. 0x09,
  1328. 0x00,
  1329. 0x00,
  1330. 0x00,
  1331. 0x00,
  1332. 0x00,
  1333. 0x00,
  1334. 0x00,
  1335. 0x00, // value1
  1336. 0x09,
  1337. 0x01,
  1338. 0x00,
  1339. 0x00,
  1340. 0x00,
  1341. 0x00,
  1342. 0x00,
  1343. 0x00,
  1344. 0x00, // value2
  1345. );
  1346. const packedValue1Value2 = createArrayBuffer(
  1347. 0x0A,
  1348. 0x10, // tag
  1349. 0x01,
  1350. 0x00,
  1351. 0x00,
  1352. 0x00,
  1353. 0x00,
  1354. 0x00,
  1355. 0x00,
  1356. 0x00, // value1
  1357. 0x00,
  1358. 0x00,
  1359. 0x00,
  1360. 0x00,
  1361. 0x00,
  1362. 0x00,
  1363. 0x00,
  1364. 0x00, // value2
  1365. );
  1366. const packedValue2Value1 = createArrayBuffer(
  1367. 0x0A,
  1368. 0x10, // tag
  1369. 0x00,
  1370. 0x00,
  1371. 0x00,
  1372. 0x00,
  1373. 0x00,
  1374. 0x00,
  1375. 0x00,
  1376. 0x00, // value2
  1377. 0x01,
  1378. 0x00,
  1379. 0x00,
  1380. 0x00,
  1381. 0x00,
  1382. 0x00,
  1383. 0x00,
  1384. 0x00, // value1
  1385. );
  1386. it('return empty array for the empty input', () => {
  1387. const accessor = LazyAccessor.createEmpty();
  1388. const list = accessor.getRepeatedFixed64Iterable(1);
  1389. expectEqualToArray(list, []);
  1390. });
  1391. it('ensure not the same instance returned for the empty input', () => {
  1392. const accessor = LazyAccessor.createEmpty();
  1393. const list1 = accessor.getRepeatedFixed64Iterable(1);
  1394. const list2 = accessor.getRepeatedFixed64Iterable(1);
  1395. expect(list1).not.toBe(list2);
  1396. });
  1397. it('return size for the empty input', () => {
  1398. const accessor = LazyAccessor.createEmpty();
  1399. const size = accessor.getRepeatedFixed64Size(1);
  1400. expect(size).toEqual(0);
  1401. });
  1402. it('return unpacked values from the input', () => {
  1403. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1404. const list = accessor.getRepeatedFixed64Iterable(1);
  1405. expectEqualToArray(list, [value1, value2]);
  1406. });
  1407. it('ensure not the same instance returned for unpacked values', () => {
  1408. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1409. const list1 = accessor.getRepeatedFixed64Iterable(1);
  1410. const list2 = accessor.getRepeatedFixed64Iterable(1);
  1411. expect(list1).not.toBe(list2);
  1412. });
  1413. it('add single unpacked value', () => {
  1414. const accessor = LazyAccessor.createEmpty();
  1415. accessor.addUnpackedFixed64Element(1, value1);
  1416. const list1 = accessor.getRepeatedFixed64Iterable(1);
  1417. accessor.addUnpackedFixed64Element(1, value2);
  1418. const list2 = accessor.getRepeatedFixed64Iterable(1);
  1419. expectEqualToArray(list1, [value1]);
  1420. expectEqualToArray(list2, [value1, value2]);
  1421. });
  1422. it('add unpacked values', () => {
  1423. const accessor = LazyAccessor.createEmpty();
  1424. accessor.addUnpackedFixed64Iterable(1, [value1]);
  1425. const list1 = accessor.getRepeatedFixed64Iterable(1);
  1426. accessor.addUnpackedFixed64Iterable(1, [value2]);
  1427. const list2 = accessor.getRepeatedFixed64Iterable(1);
  1428. expectEqualToArray(list1, [value1]);
  1429. expectEqualToArray(list2, [value1, value2]);
  1430. });
  1431. it('set a single unpacked value', () => {
  1432. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  1433. accessor.setUnpackedFixed64Element(1, 1, value1);
  1434. const list = accessor.getRepeatedFixed64Iterable(1);
  1435. expectEqualToArray(list, [value1, value1]);
  1436. });
  1437. it('set unpacked values', () => {
  1438. const accessor = LazyAccessor.createEmpty();
  1439. accessor.setUnpackedFixed64Iterable(1, [value1]);
  1440. const list = accessor.getRepeatedFixed64Iterable(1);
  1441. expectEqualToArray(list, [value1]);
  1442. });
  1443. it('encode for adding single unpacked value', () => {
  1444. const accessor = LazyAccessor.createEmpty();
  1445. accessor.addUnpackedFixed64Element(1, value1);
  1446. accessor.addUnpackedFixed64Element(1, value2);
  1447. const serialized = accessor.serialize();
  1448. expect(serialized).toEqual(unpackedValue1Value2);
  1449. });
  1450. it('encode for adding unpacked values', () => {
  1451. const accessor = LazyAccessor.createEmpty();
  1452. accessor.addUnpackedFixed64Iterable(1, [value1]);
  1453. accessor.addUnpackedFixed64Iterable(1, [value2]);
  1454. const serialized = accessor.serialize();
  1455. expect(serialized).toEqual(unpackedValue1Value2);
  1456. });
  1457. it('encode for setting single unpacked value', () => {
  1458. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1459. accessor.setUnpackedFixed64Element(1, 0, value2);
  1460. accessor.setUnpackedFixed64Element(1, 1, value1);
  1461. const serialized = accessor.serialize();
  1462. expect(serialized).toEqual(unpackedValue2Value1);
  1463. });
  1464. it('encode for setting unpacked values', () => {
  1465. const accessor = LazyAccessor.createEmpty();
  1466. accessor.setUnpackedFixed64Iterable(1, [value1, value2]);
  1467. const serialized = accessor.serialize();
  1468. expect(serialized).toEqual(unpackedValue1Value2);
  1469. });
  1470. it('return packed values from the input', () => {
  1471. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  1472. const list = accessor.getRepeatedFixed64Iterable(1);
  1473. expectEqualToArray(list, [value1, value2]);
  1474. });
  1475. it('ensure not the same instance returned for packed values', () => {
  1476. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  1477. const list1 = accessor.getRepeatedFixed64Iterable(1);
  1478. const list2 = accessor.getRepeatedFixed64Iterable(1);
  1479. expect(list1).not.toBe(list2);
  1480. });
  1481. it('add single packed value', () => {
  1482. const accessor = LazyAccessor.createEmpty();
  1483. accessor.addPackedFixed64Element(1, value1);
  1484. const list1 = accessor.getRepeatedFixed64Iterable(1);
  1485. accessor.addPackedFixed64Element(1, value2);
  1486. const list2 = accessor.getRepeatedFixed64Iterable(1);
  1487. expectEqualToArray(list1, [value1]);
  1488. expectEqualToArray(list2, [value1, value2]);
  1489. });
  1490. it('add packed values', () => {
  1491. const accessor = LazyAccessor.createEmpty();
  1492. accessor.addPackedFixed64Iterable(1, [value1]);
  1493. const list1 = accessor.getRepeatedFixed64Iterable(1);
  1494. accessor.addPackedFixed64Iterable(1, [value2]);
  1495. const list2 = accessor.getRepeatedFixed64Iterable(1);
  1496. expectEqualToArray(list1, [value1]);
  1497. expectEqualToArray(list2, [value1, value2]);
  1498. });
  1499. it('set a single packed value', () => {
  1500. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1501. accessor.setPackedFixed64Element(1, 1, value1);
  1502. const list = accessor.getRepeatedFixed64Iterable(1);
  1503. expectEqualToArray(list, [value1, value1]);
  1504. });
  1505. it('set packed values', () => {
  1506. const accessor = LazyAccessor.createEmpty();
  1507. accessor.setPackedFixed64Iterable(1, [value1]);
  1508. const list1 = accessor.getRepeatedFixed64Iterable(1);
  1509. accessor.setPackedFixed64Iterable(1, [value2]);
  1510. const list2 = accessor.getRepeatedFixed64Iterable(1);
  1511. expectEqualToArray(list1, [value1]);
  1512. expectEqualToArray(list2, [value2]);
  1513. });
  1514. it('encode for adding single packed value', () => {
  1515. const accessor = LazyAccessor.createEmpty();
  1516. accessor.addPackedFixed64Element(1, value1);
  1517. accessor.addPackedFixed64Element(1, value2);
  1518. const serialized = accessor.serialize();
  1519. expect(serialized).toEqual(packedValue1Value2);
  1520. });
  1521. it('encode for adding packed values', () => {
  1522. const accessor = LazyAccessor.createEmpty();
  1523. accessor.addPackedFixed64Iterable(1, [value1, value2]);
  1524. const serialized = accessor.serialize();
  1525. expect(serialized).toEqual(packedValue1Value2);
  1526. });
  1527. it('encode for setting single packed value', () => {
  1528. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1529. accessor.setPackedFixed64Element(1, 0, value2);
  1530. accessor.setPackedFixed64Element(1, 1, value1);
  1531. const serialized = accessor.serialize();
  1532. expect(serialized).toEqual(packedValue2Value1);
  1533. });
  1534. it('encode for setting packed values', () => {
  1535. const accessor = LazyAccessor.createEmpty();
  1536. accessor.setPackedFixed64Iterable(1, [value1, value2]);
  1537. const serialized = accessor.serialize();
  1538. expect(serialized).toEqual(packedValue1Value2);
  1539. });
  1540. it('return combined values from the input', () => {
  1541. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1542. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value1
  1543. 0x0A, 0x10, // tag
  1544. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value1
  1545. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value2
  1546. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // value2
  1547. ));
  1548. const list = accessor.getRepeatedFixed64Iterable(1);
  1549. expectEqualToArray(list, [value1, value1, value2, value2]);
  1550. });
  1551. it('return the repeated field element from the input', () => {
  1552. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1553. const result1 = accessor.getRepeatedFixed64Element(
  1554. /* fieldNumber= */ 1, /* index= */ 0);
  1555. const result2 = accessor.getRepeatedFixed64Element(
  1556. /* fieldNumber= */ 1, /* index= */ 1);
  1557. expect(result1).toEqual(value1);
  1558. expect(result2).toEqual(value2);
  1559. });
  1560. it('return the size from the input', () => {
  1561. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1562. const size = accessor.getRepeatedFixed64Size(1);
  1563. expect(size).toEqual(2);
  1564. });
  1565. it('fail when getting unpacked fixed64 value with other wire types', () => {
  1566. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1567. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  1568. if (CHECK_CRITICAL_STATE) {
  1569. expect(() => {
  1570. accessor.getRepeatedFixed64Iterable(1);
  1571. }).toThrowError('Expected wire type: 1 but found: 0');
  1572. } else {
  1573. // Note in unchecked mode we produce invalid output for invalid inputs.
  1574. // This test just documents our behavior in those cases.
  1575. // These values might change at any point and are not considered
  1576. // what the implementation should be doing here.
  1577. expectQualifiedIterable(
  1578. accessor.getRepeatedFixed64Iterable(1),
  1579. (value) => value instanceof Int64);
  1580. }
  1581. });
  1582. it('fail when adding unpacked fixed64 values with null value', () => {
  1583. const accessor = LazyAccessor.createEmpty();
  1584. const fakeFixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  1585. if (CHECK_CRITICAL_STATE) {
  1586. expect(() => accessor.addUnpackedFixed64Iterable(1, [fakeFixed64]))
  1587. .toThrowError('Must be Int64 instance, but got: null');
  1588. } else {
  1589. // Note in unchecked mode we produce invalid output for invalid inputs.
  1590. // This test just documents our behavior in those cases.
  1591. // These values might change at any point and are not considered
  1592. // what the implementation should be doing here.
  1593. accessor.addUnpackedFixed64Iterable(1, [fakeFixed64]);
  1594. expectQualifiedIterable(accessor.getRepeatedFixed64Iterable(1));
  1595. }
  1596. });
  1597. it('fail when adding single unpacked fixed64 value with null value', () => {
  1598. const accessor = LazyAccessor.createEmpty();
  1599. const fakeFixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  1600. if (CHECK_CRITICAL_STATE) {
  1601. expect(() => accessor.addUnpackedFixed64Element(1, fakeFixed64))
  1602. .toThrowError('Must be Int64 instance, but got: null');
  1603. } else {
  1604. // Note in unchecked mode we produce invalid output for invalid inputs.
  1605. // This test just documents our behavior in those cases.
  1606. // These values might change at any point and are not considered
  1607. // what the implementation should be doing here.
  1608. accessor.addUnpackedFixed64Element(1, fakeFixed64);
  1609. expectQualifiedIterable(accessor.getRepeatedFixed64Iterable(1));
  1610. }
  1611. });
  1612. it('fail when setting unpacked fixed64 values with null value', () => {
  1613. const accessor = LazyAccessor.createEmpty();
  1614. const fakeFixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  1615. if (CHECK_CRITICAL_STATE) {
  1616. expect(() => accessor.setUnpackedFixed64Iterable(1, [fakeFixed64]))
  1617. .toThrowError('Must be Int64 instance, but got: null');
  1618. } else {
  1619. // Note in unchecked mode we produce invalid output for invalid inputs.
  1620. // This test just documents our behavior in those cases.
  1621. // These values might change at any point and are not considered
  1622. // what the implementation should be doing here.
  1623. accessor.setUnpackedFixed64Iterable(1, [fakeFixed64]);
  1624. expectQualifiedIterable(accessor.getRepeatedFixed64Iterable(1));
  1625. }
  1626. });
  1627. it('fail when setting single unpacked fixed64 value with null value', () => {
  1628. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1629. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  1630. const fakeFixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  1631. if (CHECK_CRITICAL_STATE) {
  1632. expect(() => accessor.setUnpackedFixed64Element(1, 0, fakeFixed64))
  1633. .toThrowError('Must be Int64 instance, but got: null');
  1634. } else {
  1635. // Note in unchecked mode we produce invalid output for invalid inputs.
  1636. // This test just documents our behavior in those cases.
  1637. // These values might change at any point and are not considered
  1638. // what the implementation should be doing here.
  1639. accessor.setUnpackedFixed64Element(1, 0, fakeFixed64);
  1640. expectQualifiedIterable(accessor.getRepeatedFixed64Iterable(1));
  1641. }
  1642. });
  1643. it('fail when adding packed fixed64 values with null value', () => {
  1644. const accessor = LazyAccessor.createEmpty();
  1645. const fakeFixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  1646. if (CHECK_CRITICAL_STATE) {
  1647. expect(() => accessor.addPackedFixed64Iterable(1, [fakeFixed64]))
  1648. .toThrowError('Must be Int64 instance, but got: null');
  1649. } else {
  1650. // Note in unchecked mode we produce invalid output for invalid inputs.
  1651. // This test just documents our behavior in those cases.
  1652. // These values might change at any point and are not considered
  1653. // what the implementation should be doing here.
  1654. accessor.addPackedFixed64Iterable(1, [fakeFixed64]);
  1655. expectQualifiedIterable(accessor.getRepeatedFixed64Iterable(1));
  1656. }
  1657. });
  1658. it('fail when adding single packed fixed64 value with null value', () => {
  1659. const accessor = LazyAccessor.createEmpty();
  1660. const fakeFixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  1661. if (CHECK_CRITICAL_STATE) {
  1662. expect(() => accessor.addPackedFixed64Element(1, fakeFixed64))
  1663. .toThrowError('Must be Int64 instance, but got: null');
  1664. } else {
  1665. // Note in unchecked mode we produce invalid output for invalid inputs.
  1666. // This test just documents our behavior in those cases.
  1667. // These values might change at any point and are not considered
  1668. // what the implementation should be doing here.
  1669. accessor.addPackedFixed64Element(1, fakeFixed64);
  1670. expectQualifiedIterable(accessor.getRepeatedFixed64Iterable(1));
  1671. }
  1672. });
  1673. it('fail when setting packed fixed64 values with null value', () => {
  1674. const accessor = LazyAccessor.createEmpty();
  1675. const fakeFixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  1676. if (CHECK_CRITICAL_STATE) {
  1677. expect(() => accessor.setPackedFixed64Iterable(1, [fakeFixed64]))
  1678. .toThrowError('Must be Int64 instance, but got: null');
  1679. } else {
  1680. // Note in unchecked mode we produce invalid output for invalid inputs.
  1681. // This test just documents our behavior in those cases.
  1682. // These values might change at any point and are not considered
  1683. // what the implementation should be doing here.
  1684. accessor.setPackedFixed64Iterable(1, [fakeFixed64]);
  1685. expectQualifiedIterable(accessor.getRepeatedFixed64Iterable(1));
  1686. }
  1687. });
  1688. it('fail when setting single packed fixed64 value with null value', () => {
  1689. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1690. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  1691. const fakeFixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  1692. if (CHECK_CRITICAL_STATE) {
  1693. expect(() => accessor.setPackedFixed64Element(1, 0, fakeFixed64))
  1694. .toThrowError('Must be Int64 instance, but got: null');
  1695. } else {
  1696. // Note in unchecked mode we produce invalid output for invalid inputs.
  1697. // This test just documents our behavior in those cases.
  1698. // These values might change at any point and are not considered
  1699. // what the implementation should be doing here.
  1700. accessor.setPackedFixed64Element(1, 0, fakeFixed64);
  1701. expectQualifiedIterable(accessor.getRepeatedFixed64Iterable(1));
  1702. }
  1703. });
  1704. it('fail when setting single unpacked with out-of-bound index', () => {
  1705. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1706. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  1707. if (CHECK_CRITICAL_STATE) {
  1708. expect(() => accessor.setUnpackedFixed64Element(1, 1, Int64.fromInt(1)))
  1709. .toThrowError('Index out of bounds: index: 1 size: 1');
  1710. } else {
  1711. // Note in unchecked mode we produce invalid output for invalid inputs.
  1712. // This test just documents our behavior in those cases.
  1713. // These values might change at any point and are not considered
  1714. // what the implementation should be doing here.
  1715. accessor.setUnpackedFixed64Element(1, 1, Int64.fromInt(1));
  1716. expectQualifiedIterable(
  1717. accessor.getRepeatedFixed64Iterable(1),
  1718. (value) => value instanceof Int64);
  1719. }
  1720. });
  1721. it('fail when setting single packed with out-of-bound index', () => {
  1722. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1723. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  1724. if (CHECK_CRITICAL_STATE) {
  1725. expect(() => accessor.setPackedFixed64Element(1, 1, Int64.fromInt(1)))
  1726. .toThrowError('Index out of bounds: index: 1 size: 1');
  1727. } else {
  1728. // Note in unchecked mode we produce invalid output for invalid inputs.
  1729. // This test just documents our behavior in those cases.
  1730. // These values might change at any point and are not considered
  1731. // what the implementation should be doing here.
  1732. accessor.setPackedFixed64Element(1, 1, Int64.fromInt(1));
  1733. expectQualifiedIterable(
  1734. accessor.getRepeatedFixed64Iterable(1),
  1735. (value) => value instanceof Int64);
  1736. }
  1737. });
  1738. it('fail when getting element with out-of-range index', () => {
  1739. const accessor = LazyAccessor.createEmpty();
  1740. if (CHECK_CRITICAL_STATE) {
  1741. expect(() => {
  1742. accessor.getRepeatedFixed64Element(
  1743. /* fieldNumber= */ 1, /* index= */ 0);
  1744. }).toThrowError('Index out of bounds: index: 0 size: 0');
  1745. } else {
  1746. // Note in unchecked mode we produce invalid output for invalid inputs.
  1747. // This test just documents our behavior in those cases.
  1748. // These values might change at any point and are not considered
  1749. // what the implementation should be doing here.
  1750. expect(accessor.getRepeatedFixed64Element(
  1751. /* fieldNumber= */ 1, /* index= */ 0))
  1752. .toBe(undefined);
  1753. }
  1754. });
  1755. });
  1756. describe('LazyAccessor for repeated float does', () => {
  1757. const value1 = 1.6;
  1758. const value1Float = Math.fround(1.6);
  1759. const value2 = 0;
  1760. const unpackedValue1Value2 = createArrayBuffer(
  1761. 0x0D, 0xCD, 0xCC, 0xCC, 0x3F, 0x0D, 0x00, 0x00, 0x00, 0x00);
  1762. const unpackedValue2Value1 = createArrayBuffer(
  1763. 0x0D, 0x00, 0x00, 0x00, 0x00, 0x0D, 0xCD, 0xCC, 0xCC, 0x3F);
  1764. const packedValue1Value2 = createArrayBuffer(
  1765. 0x0A, 0x08, 0xCD, 0xCC, 0xCC, 0x3F, 0x00, 0x00, 0x00, 0x00);
  1766. const packedValue2Value1 = createArrayBuffer(
  1767. 0x0A, 0x08, 0x00, 0x00, 0x00, 0x00, 0xCD, 0xCC, 0xCC, 0x3F);
  1768. it('return empty array for the empty input', () => {
  1769. const accessor = LazyAccessor.createEmpty();
  1770. const list = accessor.getRepeatedFloatIterable(1);
  1771. expectEqualToArray(list, []);
  1772. });
  1773. it('ensure not the same instance returned for the empty input', () => {
  1774. const accessor = LazyAccessor.createEmpty();
  1775. const list1 = accessor.getRepeatedFloatIterable(1);
  1776. const list2 = accessor.getRepeatedFloatIterable(1);
  1777. expect(list1).not.toBe(list2);
  1778. });
  1779. it('return size for the empty input', () => {
  1780. const accessor = LazyAccessor.createEmpty();
  1781. const size = accessor.getRepeatedFloatSize(1);
  1782. expect(size).toEqual(0);
  1783. });
  1784. it('return unpacked values from the input', () => {
  1785. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1786. const list = accessor.getRepeatedFloatIterable(1);
  1787. expectEqualToArray(list, [value1Float, value2]);
  1788. });
  1789. it('ensure not the same instance returned for unpacked values', () => {
  1790. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1791. const list1 = accessor.getRepeatedFloatIterable(1);
  1792. const list2 = accessor.getRepeatedFloatIterable(1);
  1793. expect(list1).not.toBe(list2);
  1794. });
  1795. it('add single unpacked value', () => {
  1796. const accessor = LazyAccessor.createEmpty();
  1797. accessor.addUnpackedFloatElement(1, value1);
  1798. const list1 = accessor.getRepeatedFloatIterable(1);
  1799. accessor.addUnpackedFloatElement(1, value2);
  1800. const list2 = accessor.getRepeatedFloatIterable(1);
  1801. expectEqualToArray(list1, [value1Float]);
  1802. expectEqualToArray(list2, [value1Float, value2]);
  1803. });
  1804. it('add unpacked values', () => {
  1805. const accessor = LazyAccessor.createEmpty();
  1806. accessor.addUnpackedFloatIterable(1, [value1]);
  1807. const list1 = accessor.getRepeatedFloatIterable(1);
  1808. accessor.addUnpackedFloatIterable(1, [value2]);
  1809. const list2 = accessor.getRepeatedFloatIterable(1);
  1810. expectEqualToArray(list1, [value1Float]);
  1811. expectEqualToArray(list2, [value1Float, value2]);
  1812. });
  1813. it('set a single unpacked value', () => {
  1814. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  1815. accessor.setUnpackedFloatElement(1, 1, value1);
  1816. const list = accessor.getRepeatedFloatIterable(1);
  1817. expectEqualToArray(list, [value1Float, value1Float]);
  1818. });
  1819. it('set unpacked values', () => {
  1820. const accessor = LazyAccessor.createEmpty();
  1821. accessor.setUnpackedFloatIterable(1, [value1]);
  1822. const list = accessor.getRepeatedFloatIterable(1);
  1823. expectEqualToArray(list, [value1Float]);
  1824. });
  1825. it('encode for adding single unpacked value', () => {
  1826. const accessor = LazyAccessor.createEmpty();
  1827. accessor.addUnpackedFloatElement(1, value1);
  1828. accessor.addUnpackedFloatElement(1, value2);
  1829. const serialized = accessor.serialize();
  1830. expect(serialized).toEqual(unpackedValue1Value2);
  1831. });
  1832. it('encode for adding unpacked values', () => {
  1833. const accessor = LazyAccessor.createEmpty();
  1834. accessor.addUnpackedFloatIterable(1, [value1]);
  1835. accessor.addUnpackedFloatIterable(1, [value2]);
  1836. const serialized = accessor.serialize();
  1837. expect(serialized).toEqual(unpackedValue1Value2);
  1838. });
  1839. it('encode for setting single unpacked value', () => {
  1840. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1841. accessor.setUnpackedFloatElement(1, 0, value2);
  1842. accessor.setUnpackedFloatElement(1, 1, value1);
  1843. const serialized = accessor.serialize();
  1844. expect(serialized).toEqual(unpackedValue2Value1);
  1845. });
  1846. it('encode for setting unpacked values', () => {
  1847. const accessor = LazyAccessor.createEmpty();
  1848. accessor.setUnpackedFloatIterable(1, [value1, value2]);
  1849. const serialized = accessor.serialize();
  1850. expect(serialized).toEqual(unpackedValue1Value2);
  1851. });
  1852. it('return packed values from the input', () => {
  1853. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  1854. const list = accessor.getRepeatedFloatIterable(1);
  1855. expectEqualToArray(list, [value1Float, value2]);
  1856. });
  1857. it('ensure not the same instance returned for packed values', () => {
  1858. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  1859. const list1 = accessor.getRepeatedFloatIterable(1);
  1860. const list2 = accessor.getRepeatedFloatIterable(1);
  1861. expect(list1).not.toBe(list2);
  1862. });
  1863. it('add single packed value', () => {
  1864. const accessor = LazyAccessor.createEmpty();
  1865. accessor.addPackedFloatElement(1, value1);
  1866. const list1 = accessor.getRepeatedFloatIterable(1);
  1867. accessor.addPackedFloatElement(1, value2);
  1868. const list2 = accessor.getRepeatedFloatIterable(1);
  1869. expectEqualToArray(list1, [value1Float]);
  1870. expectEqualToArray(list2, [value1Float, value2]);
  1871. });
  1872. it('add packed values', () => {
  1873. const accessor = LazyAccessor.createEmpty();
  1874. accessor.addPackedFloatIterable(1, [value1]);
  1875. const list1 = accessor.getRepeatedFloatIterable(1);
  1876. accessor.addPackedFloatIterable(1, [value2]);
  1877. const list2 = accessor.getRepeatedFloatIterable(1);
  1878. expectEqualToArray(list1, [value1Float]);
  1879. expectEqualToArray(list2, [value1Float, value2]);
  1880. });
  1881. it('set a single packed value', () => {
  1882. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1883. accessor.setPackedFloatElement(1, 1, value1);
  1884. const list = accessor.getRepeatedFloatIterable(1);
  1885. expectEqualToArray(list, [value1Float, value1Float]);
  1886. });
  1887. it('set packed values', () => {
  1888. const accessor = LazyAccessor.createEmpty();
  1889. accessor.setPackedFloatIterable(1, [value1]);
  1890. const list1 = accessor.getRepeatedFloatIterable(1);
  1891. accessor.setPackedFloatIterable(1, [value2]);
  1892. const list2 = accessor.getRepeatedFloatIterable(1);
  1893. expectEqualToArray(list1, [value1Float]);
  1894. expectEqualToArray(list2, [value2]);
  1895. });
  1896. it('encode for adding single packed value', () => {
  1897. const accessor = LazyAccessor.createEmpty();
  1898. accessor.addPackedFloatElement(1, value1);
  1899. accessor.addPackedFloatElement(1, value2);
  1900. const serialized = accessor.serialize();
  1901. expect(serialized).toEqual(packedValue1Value2);
  1902. });
  1903. it('encode for adding packed values', () => {
  1904. const accessor = LazyAccessor.createEmpty();
  1905. accessor.addPackedFloatIterable(1, [value1, value2]);
  1906. const serialized = accessor.serialize();
  1907. expect(serialized).toEqual(packedValue1Value2);
  1908. });
  1909. it('encode for setting single packed value', () => {
  1910. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1911. accessor.setPackedFloatElement(1, 0, value2);
  1912. accessor.setPackedFloatElement(1, 1, value1);
  1913. const serialized = accessor.serialize();
  1914. expect(serialized).toEqual(packedValue2Value1);
  1915. });
  1916. it('encode for setting packed values', () => {
  1917. const accessor = LazyAccessor.createEmpty();
  1918. accessor.setPackedFloatIterable(1, [value1, value2]);
  1919. const serialized = accessor.serialize();
  1920. expect(serialized).toEqual(packedValue1Value2);
  1921. });
  1922. it('return combined values from the input', () => {
  1923. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  1924. 0x0D,
  1925. 0xCD,
  1926. 0xCC,
  1927. 0xCC,
  1928. 0x3F, // value1
  1929. 0x0A,
  1930. 0x08, // tag
  1931. 0xCD,
  1932. 0xCC,
  1933. 0xCC,
  1934. 0x3F, // value1
  1935. 0x00,
  1936. 0x00,
  1937. 0x00,
  1938. 0x00, // value2
  1939. 0x0D,
  1940. 0x00,
  1941. 0x00,
  1942. 0x00,
  1943. 0x00, // value2
  1944. ));
  1945. const list = accessor.getRepeatedFloatIterable(1);
  1946. expectEqualToArray(list, [value1Float, value1Float, value2, value2]);
  1947. });
  1948. it('return the repeated field element from the input', () => {
  1949. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1950. const result1 = accessor.getRepeatedFloatElement(
  1951. /* fieldNumber= */ 1, /* index= */ 0);
  1952. const result2 = accessor.getRepeatedFloatElement(
  1953. /* fieldNumber= */ 1, /* index= */ 1);
  1954. expect(result1).toEqual(value1Float);
  1955. expect(result2).toEqual(value2);
  1956. });
  1957. it('return the size from the input', () => {
  1958. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  1959. const size = accessor.getRepeatedFloatSize(1);
  1960. expect(size).toEqual(2);
  1961. });
  1962. it('fail when getting unpacked float value with other wire types', () => {
  1963. const accessor = LazyAccessor.fromArrayBuffer(
  1964. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  1965. if (CHECK_CRITICAL_STATE) {
  1966. expect(() => {
  1967. accessor.getRepeatedFloatIterable(1);
  1968. }).toThrowError('Expected wire type: 5 but found: 0');
  1969. } else {
  1970. // Note in unchecked mode we produce invalid output for invalid inputs.
  1971. // This test just documents our behavior in those cases.
  1972. // These values might change at any point and are not considered
  1973. // what the implementation should be doing here.
  1974. expectQualifiedIterable(
  1975. accessor.getRepeatedFloatIterable(1),
  1976. (value) => typeof value === 'number');
  1977. }
  1978. });
  1979. it('fail when adding unpacked float values with null value', () => {
  1980. const accessor = LazyAccessor.createEmpty();
  1981. const fakeFloat = /** @type {number} */ (/** @type {*} */ (null));
  1982. if (CHECK_CRITICAL_STATE) {
  1983. expect(() => accessor.addUnpackedFloatIterable(1, [fakeFloat]))
  1984. .toThrowError('Must be a number, but got: null');
  1985. } else {
  1986. // Note in unchecked mode we produce invalid output for invalid inputs.
  1987. // This test just documents our behavior in those cases.
  1988. // These values might change at any point and are not considered
  1989. // what the implementation should be doing here.
  1990. accessor.addUnpackedFloatIterable(1, [fakeFloat]);
  1991. expectQualifiedIterable(accessor.getRepeatedFloatIterable(1));
  1992. }
  1993. });
  1994. it('fail when adding single unpacked float value with null value', () => {
  1995. const accessor = LazyAccessor.createEmpty();
  1996. const fakeFloat = /** @type {number} */ (/** @type {*} */ (null));
  1997. if (CHECK_CRITICAL_STATE) {
  1998. expect(() => accessor.addUnpackedFloatElement(1, fakeFloat))
  1999. .toThrowError('Must be a number, but got: null');
  2000. } else {
  2001. // Note in unchecked mode we produce invalid output for invalid inputs.
  2002. // This test just documents our behavior in those cases.
  2003. // These values might change at any point and are not considered
  2004. // what the implementation should be doing here.
  2005. accessor.addUnpackedFloatElement(1, fakeFloat);
  2006. expectQualifiedIterable(accessor.getRepeatedFloatIterable(1));
  2007. }
  2008. });
  2009. it('fail when setting unpacked float values with null value', () => {
  2010. const accessor = LazyAccessor.createEmpty();
  2011. const fakeFloat = /** @type {number} */ (/** @type {*} */ (null));
  2012. if (CHECK_CRITICAL_STATE) {
  2013. expect(() => accessor.setUnpackedFloatIterable(1, [fakeFloat]))
  2014. .toThrowError('Must be a number, but got: null');
  2015. } else {
  2016. // Note in unchecked mode we produce invalid output for invalid inputs.
  2017. // This test just documents our behavior in those cases.
  2018. // These values might change at any point and are not considered
  2019. // what the implementation should be doing here.
  2020. accessor.setUnpackedFloatIterable(1, [fakeFloat]);
  2021. expectQualifiedIterable(accessor.getRepeatedFloatIterable(1));
  2022. }
  2023. });
  2024. it('fail when setting single unpacked float value with null value', () => {
  2025. const accessor = LazyAccessor.fromArrayBuffer(
  2026. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  2027. const fakeFloat = /** @type {number} */ (/** @type {*} */ (null));
  2028. if (CHECK_CRITICAL_STATE) {
  2029. expect(() => accessor.setUnpackedFloatElement(1, 0, fakeFloat))
  2030. .toThrowError('Must be a number, but got: null');
  2031. } else {
  2032. // Note in unchecked mode we produce invalid output for invalid inputs.
  2033. // This test just documents our behavior in those cases.
  2034. // These values might change at any point and are not considered
  2035. // what the implementation should be doing here.
  2036. accessor.setUnpackedFloatElement(1, 0, fakeFloat);
  2037. expectQualifiedIterable(accessor.getRepeatedFloatIterable(1));
  2038. }
  2039. });
  2040. it('fail when adding packed float values with null value', () => {
  2041. const accessor = LazyAccessor.createEmpty();
  2042. const fakeFloat = /** @type {number} */ (/** @type {*} */ (null));
  2043. if (CHECK_CRITICAL_STATE) {
  2044. expect(() => accessor.addPackedFloatIterable(1, [fakeFloat]))
  2045. .toThrowError('Must be a number, but got: null');
  2046. } else {
  2047. // Note in unchecked mode we produce invalid output for invalid inputs.
  2048. // This test just documents our behavior in those cases.
  2049. // These values might change at any point and are not considered
  2050. // what the implementation should be doing here.
  2051. accessor.addPackedFloatIterable(1, [fakeFloat]);
  2052. expectQualifiedIterable(accessor.getRepeatedFloatIterable(1));
  2053. }
  2054. });
  2055. it('fail when adding single packed float value with null value', () => {
  2056. const accessor = LazyAccessor.createEmpty();
  2057. const fakeFloat = /** @type {number} */ (/** @type {*} */ (null));
  2058. if (CHECK_CRITICAL_STATE) {
  2059. expect(() => accessor.addPackedFloatElement(1, fakeFloat))
  2060. .toThrowError('Must be a number, but got: null');
  2061. } else {
  2062. // Note in unchecked mode we produce invalid output for invalid inputs.
  2063. // This test just documents our behavior in those cases.
  2064. // These values might change at any point and are not considered
  2065. // what the implementation should be doing here.
  2066. accessor.addPackedFloatElement(1, fakeFloat);
  2067. expectQualifiedIterable(accessor.getRepeatedFloatIterable(1));
  2068. }
  2069. });
  2070. it('fail when setting packed float values with null value', () => {
  2071. const accessor = LazyAccessor.createEmpty();
  2072. const fakeFloat = /** @type {number} */ (/** @type {*} */ (null));
  2073. if (CHECK_CRITICAL_STATE) {
  2074. expect(() => accessor.setPackedFloatIterable(1, [fakeFloat]))
  2075. .toThrowError('Must be a number, but got: null');
  2076. } else {
  2077. // Note in unchecked mode we produce invalid output for invalid inputs.
  2078. // This test just documents our behavior in those cases.
  2079. // These values might change at any point and are not considered
  2080. // what the implementation should be doing here.
  2081. accessor.setPackedFloatIterable(1, [fakeFloat]);
  2082. expectQualifiedIterable(accessor.getRepeatedFloatIterable(1));
  2083. }
  2084. });
  2085. it('fail when setting single packed float value with null value', () => {
  2086. const accessor = LazyAccessor.fromArrayBuffer(
  2087. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  2088. const fakeFloat = /** @type {number} */ (/** @type {*} */ (null));
  2089. if (CHECK_CRITICAL_STATE) {
  2090. expect(() => accessor.setPackedFloatElement(1, 0, fakeFloat))
  2091. .toThrowError('Must be a number, but got: null');
  2092. } else {
  2093. // Note in unchecked mode we produce invalid output for invalid inputs.
  2094. // This test just documents our behavior in those cases.
  2095. // These values might change at any point and are not considered
  2096. // what the implementation should be doing here.
  2097. accessor.setPackedFloatElement(1, 0, fakeFloat);
  2098. expectQualifiedIterable(accessor.getRepeatedFloatIterable(1));
  2099. }
  2100. });
  2101. it('fail when setting single unpacked with out-of-bound index', () => {
  2102. const accessor = LazyAccessor.fromArrayBuffer(
  2103. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  2104. if (CHECK_CRITICAL_STATE) {
  2105. expect(() => accessor.setUnpackedFloatElement(1, 1, 1))
  2106. .toThrowError('Index out of bounds: index: 1 size: 1');
  2107. } else {
  2108. // Note in unchecked mode we produce invalid output for invalid inputs.
  2109. // This test just documents our behavior in those cases.
  2110. // These values might change at any point and are not considered
  2111. // what the implementation should be doing here.
  2112. accessor.setUnpackedFloatElement(1, 1, 1);
  2113. expectQualifiedIterable(
  2114. accessor.getRepeatedFloatIterable(1),
  2115. (value) => typeof value === 'number');
  2116. }
  2117. });
  2118. it('fail when setting single packed with out-of-bound index', () => {
  2119. const accessor = LazyAccessor.fromArrayBuffer(
  2120. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  2121. if (CHECK_CRITICAL_STATE) {
  2122. expect(() => accessor.setPackedFloatElement(1, 1, 1))
  2123. .toThrowError('Index out of bounds: index: 1 size: 1');
  2124. } else {
  2125. // Note in unchecked mode we produce invalid output for invalid inputs.
  2126. // This test just documents our behavior in those cases.
  2127. // These values might change at any point and are not considered
  2128. // what the implementation should be doing here.
  2129. accessor.setPackedFloatElement(1, 1, 1);
  2130. expectQualifiedIterable(
  2131. accessor.getRepeatedFloatIterable(1),
  2132. (value) => typeof value === 'number');
  2133. }
  2134. });
  2135. it('fail when getting element with out-of-range index', () => {
  2136. const accessor = LazyAccessor.createEmpty();
  2137. if (CHECK_CRITICAL_STATE) {
  2138. expect(() => {
  2139. accessor.getRepeatedFloatElement(
  2140. /* fieldNumber= */ 1, /* index= */ 0);
  2141. }).toThrowError('Index out of bounds: index: 0 size: 0');
  2142. } else {
  2143. // Note in unchecked mode we produce invalid output for invalid inputs.
  2144. // This test just documents our behavior in those cases.
  2145. // These values might change at any point and are not considered
  2146. // what the implementation should be doing here.
  2147. expect(accessor.getRepeatedFloatElement(
  2148. /* fieldNumber= */ 1, /* index= */ 0))
  2149. .toBe(undefined);
  2150. }
  2151. });
  2152. });
  2153. describe('LazyAccessor for repeated int32 does', () => {
  2154. const value1 = 1;
  2155. const value2 = 0;
  2156. const unpackedValue1Value2 = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  2157. const unpackedValue2Value1 = createArrayBuffer(0x08, 0x00, 0x08, 0x01);
  2158. const packedValue1Value2 = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  2159. const packedValue2Value1 = createArrayBuffer(0x0A, 0x02, 0x00, 0x01);
  2160. it('return empty array for the empty input', () => {
  2161. const accessor = LazyAccessor.createEmpty();
  2162. const list = accessor.getRepeatedInt32Iterable(1);
  2163. expectEqualToArray(list, []);
  2164. });
  2165. it('ensure not the same instance returned for the empty input', () => {
  2166. const accessor = LazyAccessor.createEmpty();
  2167. const list1 = accessor.getRepeatedInt32Iterable(1);
  2168. const list2 = accessor.getRepeatedInt32Iterable(1);
  2169. expect(list1).not.toBe(list2);
  2170. });
  2171. it('return size for the empty input', () => {
  2172. const accessor = LazyAccessor.createEmpty();
  2173. const size = accessor.getRepeatedInt32Size(1);
  2174. expect(size).toEqual(0);
  2175. });
  2176. it('return unpacked values from the input', () => {
  2177. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2178. const list = accessor.getRepeatedInt32Iterable(1);
  2179. expectEqualToArray(list, [value1, value2]);
  2180. });
  2181. it('ensure not the same instance returned for unpacked values', () => {
  2182. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2183. const list1 = accessor.getRepeatedInt32Iterable(1);
  2184. const list2 = accessor.getRepeatedInt32Iterable(1);
  2185. expect(list1).not.toBe(list2);
  2186. });
  2187. it('add single unpacked value', () => {
  2188. const accessor = LazyAccessor.createEmpty();
  2189. accessor.addUnpackedInt32Element(1, value1);
  2190. const list1 = accessor.getRepeatedInt32Iterable(1);
  2191. accessor.addUnpackedInt32Element(1, value2);
  2192. const list2 = accessor.getRepeatedInt32Iterable(1);
  2193. expectEqualToArray(list1, [value1]);
  2194. expectEqualToArray(list2, [value1, value2]);
  2195. });
  2196. it('add unpacked values', () => {
  2197. const accessor = LazyAccessor.createEmpty();
  2198. accessor.addUnpackedInt32Iterable(1, [value1]);
  2199. const list1 = accessor.getRepeatedInt32Iterable(1);
  2200. accessor.addUnpackedInt32Iterable(1, [value2]);
  2201. const list2 = accessor.getRepeatedInt32Iterable(1);
  2202. expectEqualToArray(list1, [value1]);
  2203. expectEqualToArray(list2, [value1, value2]);
  2204. });
  2205. it('set a single unpacked value', () => {
  2206. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  2207. accessor.setUnpackedInt32Element(1, 1, value1);
  2208. const list = accessor.getRepeatedInt32Iterable(1);
  2209. expectEqualToArray(list, [value1, value1]);
  2210. });
  2211. it('set unpacked values', () => {
  2212. const accessor = LazyAccessor.createEmpty();
  2213. accessor.setUnpackedInt32Iterable(1, [value1]);
  2214. const list = accessor.getRepeatedInt32Iterable(1);
  2215. expectEqualToArray(list, [value1]);
  2216. });
  2217. it('encode for adding single unpacked value', () => {
  2218. const accessor = LazyAccessor.createEmpty();
  2219. accessor.addUnpackedInt32Element(1, value1);
  2220. accessor.addUnpackedInt32Element(1, value2);
  2221. const serialized = accessor.serialize();
  2222. expect(serialized).toEqual(unpackedValue1Value2);
  2223. });
  2224. it('encode for adding unpacked values', () => {
  2225. const accessor = LazyAccessor.createEmpty();
  2226. accessor.addUnpackedInt32Iterable(1, [value1]);
  2227. accessor.addUnpackedInt32Iterable(1, [value2]);
  2228. const serialized = accessor.serialize();
  2229. expect(serialized).toEqual(unpackedValue1Value2);
  2230. });
  2231. it('encode for setting single unpacked value', () => {
  2232. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2233. accessor.setUnpackedInt32Element(1, 0, value2);
  2234. accessor.setUnpackedInt32Element(1, 1, value1);
  2235. const serialized = accessor.serialize();
  2236. expect(serialized).toEqual(unpackedValue2Value1);
  2237. });
  2238. it('encode for setting unpacked values', () => {
  2239. const accessor = LazyAccessor.createEmpty();
  2240. accessor.setUnpackedInt32Iterable(1, [value1, value2]);
  2241. const serialized = accessor.serialize();
  2242. expect(serialized).toEqual(unpackedValue1Value2);
  2243. });
  2244. it('return packed values from the input', () => {
  2245. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  2246. const list = accessor.getRepeatedInt32Iterable(1);
  2247. expectEqualToArray(list, [value1, value2]);
  2248. });
  2249. it('ensure not the same instance returned for packed values', () => {
  2250. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  2251. const list1 = accessor.getRepeatedInt32Iterable(1);
  2252. const list2 = accessor.getRepeatedInt32Iterable(1);
  2253. expect(list1).not.toBe(list2);
  2254. });
  2255. it('add single packed value', () => {
  2256. const accessor = LazyAccessor.createEmpty();
  2257. accessor.addPackedInt32Element(1, value1);
  2258. const list1 = accessor.getRepeatedInt32Iterable(1);
  2259. accessor.addPackedInt32Element(1, value2);
  2260. const list2 = accessor.getRepeatedInt32Iterable(1);
  2261. expectEqualToArray(list1, [value1]);
  2262. expectEqualToArray(list2, [value1, value2]);
  2263. });
  2264. it('add packed values', () => {
  2265. const accessor = LazyAccessor.createEmpty();
  2266. accessor.addPackedInt32Iterable(1, [value1]);
  2267. const list1 = accessor.getRepeatedInt32Iterable(1);
  2268. accessor.addPackedInt32Iterable(1, [value2]);
  2269. const list2 = accessor.getRepeatedInt32Iterable(1);
  2270. expectEqualToArray(list1, [value1]);
  2271. expectEqualToArray(list2, [value1, value2]);
  2272. });
  2273. it('set a single packed value', () => {
  2274. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2275. accessor.setPackedInt32Element(1, 1, value1);
  2276. const list = accessor.getRepeatedInt32Iterable(1);
  2277. expectEqualToArray(list, [value1, value1]);
  2278. });
  2279. it('set packed values', () => {
  2280. const accessor = LazyAccessor.createEmpty();
  2281. accessor.setPackedInt32Iterable(1, [value1]);
  2282. const list1 = accessor.getRepeatedInt32Iterable(1);
  2283. accessor.setPackedInt32Iterable(1, [value2]);
  2284. const list2 = accessor.getRepeatedInt32Iterable(1);
  2285. expectEqualToArray(list1, [value1]);
  2286. expectEqualToArray(list2, [value2]);
  2287. });
  2288. it('encode for adding single packed value', () => {
  2289. const accessor = LazyAccessor.createEmpty();
  2290. accessor.addPackedInt32Element(1, value1);
  2291. accessor.addPackedInt32Element(1, value2);
  2292. const serialized = accessor.serialize();
  2293. expect(serialized).toEqual(packedValue1Value2);
  2294. });
  2295. it('encode for adding packed values', () => {
  2296. const accessor = LazyAccessor.createEmpty();
  2297. accessor.addPackedInt32Iterable(1, [value1, value2]);
  2298. const serialized = accessor.serialize();
  2299. expect(serialized).toEqual(packedValue1Value2);
  2300. });
  2301. it('encode for setting single packed value', () => {
  2302. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2303. accessor.setPackedInt32Element(1, 0, value2);
  2304. accessor.setPackedInt32Element(1, 1, value1);
  2305. const serialized = accessor.serialize();
  2306. expect(serialized).toEqual(packedValue2Value1);
  2307. });
  2308. it('encode for setting packed values', () => {
  2309. const accessor = LazyAccessor.createEmpty();
  2310. accessor.setPackedInt32Iterable(1, [value1, value2]);
  2311. const serialized = accessor.serialize();
  2312. expect(serialized).toEqual(packedValue1Value2);
  2313. });
  2314. it('return combined values from the input', () => {
  2315. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  2316. 0x08,
  2317. 0x01, // unpacked value1
  2318. 0x0A,
  2319. 0x02,
  2320. 0x01,
  2321. 0x00, // packed value1 and value2
  2322. 0x08,
  2323. 0x00, // unpacked value2
  2324. ));
  2325. const list = accessor.getRepeatedInt32Iterable(1);
  2326. expectEqualToArray(list, [value1, value1, value2, value2]);
  2327. });
  2328. it('return the repeated field element from the input', () => {
  2329. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2330. const result1 = accessor.getRepeatedInt32Element(
  2331. /* fieldNumber= */ 1, /* index= */ 0);
  2332. const result2 = accessor.getRepeatedInt32Element(
  2333. /* fieldNumber= */ 1, /* index= */ 1);
  2334. expect(result1).toEqual(value1);
  2335. expect(result2).toEqual(value2);
  2336. });
  2337. it('return the size from the input', () => {
  2338. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2339. const size = accessor.getRepeatedInt32Size(1);
  2340. expect(size).toEqual(2);
  2341. });
  2342. it('fail when getting unpacked int32 value with other wire types', () => {
  2343. const accessor = LazyAccessor.fromArrayBuffer(
  2344. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  2345. if (CHECK_CRITICAL_STATE) {
  2346. expect(() => {
  2347. accessor.getRepeatedInt32Iterable(1);
  2348. }).toThrowError('Expected wire type: 0 but found: 5');
  2349. } else {
  2350. // Note in unchecked mode we produce invalid output for invalid inputs.
  2351. // This test just documents our behavior in those cases.
  2352. // These values might change at any point and are not considered
  2353. // what the implementation should be doing here.
  2354. expectQualifiedIterable(
  2355. accessor.getRepeatedInt32Iterable(1),
  2356. (value) => Number.isInteger(value));
  2357. }
  2358. });
  2359. it('fail when adding unpacked int32 values with null value', () => {
  2360. const accessor = LazyAccessor.createEmpty();
  2361. const fakeInt32 = /** @type {number} */ (/** @type {*} */ (null));
  2362. if (CHECK_CRITICAL_STATE) {
  2363. expect(() => accessor.addUnpackedInt32Iterable(1, [fakeInt32]))
  2364. .toThrowError('Must be a number, but got: null');
  2365. } else {
  2366. // Note in unchecked mode we produce invalid output for invalid inputs.
  2367. // This test just documents our behavior in those cases.
  2368. // These values might change at any point and are not considered
  2369. // what the implementation should be doing here.
  2370. accessor.addUnpackedInt32Iterable(1, [fakeInt32]);
  2371. expectQualifiedIterable(accessor.getRepeatedInt32Iterable(1));
  2372. }
  2373. });
  2374. it('fail when adding single unpacked int32 value with null value', () => {
  2375. const accessor = LazyAccessor.createEmpty();
  2376. const fakeInt32 = /** @type {number} */ (/** @type {*} */ (null));
  2377. if (CHECK_CRITICAL_STATE) {
  2378. expect(() => accessor.addUnpackedInt32Element(1, fakeInt32))
  2379. .toThrowError('Must be a number, but got: null');
  2380. } else {
  2381. // Note in unchecked mode we produce invalid output for invalid inputs.
  2382. // This test just documents our behavior in those cases.
  2383. // These values might change at any point and are not considered
  2384. // what the implementation should be doing here.
  2385. accessor.addUnpackedInt32Element(1, fakeInt32);
  2386. expectQualifiedIterable(accessor.getRepeatedInt32Iterable(1));
  2387. }
  2388. });
  2389. it('fail when setting unpacked int32 values with null value', () => {
  2390. const accessor = LazyAccessor.createEmpty();
  2391. const fakeInt32 = /** @type {number} */ (/** @type {*} */ (null));
  2392. if (CHECK_CRITICAL_STATE) {
  2393. expect(() => accessor.setUnpackedInt32Iterable(1, [fakeInt32]))
  2394. .toThrowError('Must be a number, but got: null');
  2395. } else {
  2396. // Note in unchecked mode we produce invalid output for invalid inputs.
  2397. // This test just documents our behavior in those cases.
  2398. // These values might change at any point and are not considered
  2399. // what the implementation should be doing here.
  2400. accessor.setUnpackedInt32Iterable(1, [fakeInt32]);
  2401. expectQualifiedIterable(accessor.getRepeatedInt32Iterable(1));
  2402. }
  2403. });
  2404. it('fail when setting single unpacked int32 value with null value', () => {
  2405. const accessor = LazyAccessor.fromArrayBuffer(
  2406. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  2407. const fakeInt32 = /** @type {number} */ (/** @type {*} */ (null));
  2408. if (CHECK_CRITICAL_STATE) {
  2409. expect(() => accessor.setUnpackedInt32Element(1, 0, fakeInt32))
  2410. .toThrowError('Must be a number, but got: null');
  2411. } else {
  2412. // Note in unchecked mode we produce invalid output for invalid inputs.
  2413. // This test just documents our behavior in those cases.
  2414. // These values might change at any point and are not considered
  2415. // what the implementation should be doing here.
  2416. accessor.setUnpackedInt32Element(1, 0, fakeInt32);
  2417. expectQualifiedIterable(
  2418. accessor.getRepeatedInt32Iterable(1),
  2419. );
  2420. }
  2421. });
  2422. it('fail when adding packed int32 values with null value', () => {
  2423. const accessor = LazyAccessor.createEmpty();
  2424. const fakeInt32 = /** @type {number} */ (/** @type {*} */ (null));
  2425. if (CHECK_CRITICAL_STATE) {
  2426. expect(() => accessor.addPackedInt32Iterable(1, [fakeInt32]))
  2427. .toThrowError('Must be a number, but got: null');
  2428. } else {
  2429. // Note in unchecked mode we produce invalid output for invalid inputs.
  2430. // This test just documents our behavior in those cases.
  2431. // These values might change at any point and are not considered
  2432. // what the implementation should be doing here.
  2433. accessor.addPackedInt32Iterable(1, [fakeInt32]);
  2434. expectQualifiedIterable(accessor.getRepeatedInt32Iterable(1));
  2435. }
  2436. });
  2437. it('fail when adding single packed int32 value with null value', () => {
  2438. const accessor = LazyAccessor.createEmpty();
  2439. const fakeInt32 = /** @type {number} */ (/** @type {*} */ (null));
  2440. if (CHECK_CRITICAL_STATE) {
  2441. expect(() => accessor.addPackedInt32Element(1, fakeInt32))
  2442. .toThrowError('Must be a number, but got: null');
  2443. } else {
  2444. // Note in unchecked mode we produce invalid output for invalid inputs.
  2445. // This test just documents our behavior in those cases.
  2446. // These values might change at any point and are not considered
  2447. // what the implementation should be doing here.
  2448. accessor.addPackedInt32Element(1, fakeInt32);
  2449. expectQualifiedIterable(accessor.getRepeatedInt32Iterable(1));
  2450. }
  2451. });
  2452. it('fail when setting packed int32 values with null value', () => {
  2453. const accessor = LazyAccessor.createEmpty();
  2454. const fakeInt32 = /** @type {number} */ (/** @type {*} */ (null));
  2455. if (CHECK_CRITICAL_STATE) {
  2456. expect(() => accessor.setPackedInt32Iterable(1, [fakeInt32]))
  2457. .toThrowError('Must be a number, but got: null');
  2458. } else {
  2459. // Note in unchecked mode we produce invalid output for invalid inputs.
  2460. // This test just documents our behavior in those cases.
  2461. // These values might change at any point and are not considered
  2462. // what the implementation should be doing here.
  2463. accessor.setPackedInt32Iterable(1, [fakeInt32]);
  2464. expectQualifiedIterable(accessor.getRepeatedInt32Iterable(1));
  2465. }
  2466. });
  2467. it('fail when setting single packed int32 value with null value', () => {
  2468. const accessor =
  2469. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  2470. const fakeInt32 = /** @type {number} */ (/** @type {*} */ (null));
  2471. if (CHECK_CRITICAL_STATE) {
  2472. expect(() => accessor.setPackedInt32Element(1, 0, fakeInt32))
  2473. .toThrowError('Must be a number, but got: null');
  2474. } else {
  2475. // Note in unchecked mode we produce invalid output for invalid inputs.
  2476. // This test just documents our behavior in those cases.
  2477. // These values might change at any point and are not considered
  2478. // what the implementation should be doing here.
  2479. accessor.setPackedInt32Element(1, 0, fakeInt32);
  2480. expectQualifiedIterable(accessor.getRepeatedInt32Iterable(1));
  2481. }
  2482. });
  2483. it('fail when setting single unpacked with out-of-bound index', () => {
  2484. const accessor =
  2485. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x00));
  2486. if (CHECK_CRITICAL_STATE) {
  2487. expect(() => accessor.setUnpackedInt32Element(1, 1, 1))
  2488. .toThrowError('Index out of bounds: index: 1 size: 1');
  2489. } else {
  2490. // Note in unchecked mode we produce invalid output for invalid inputs.
  2491. // This test just documents our behavior in those cases.
  2492. // These values might change at any point and are not considered
  2493. // what the implementation should be doing here.
  2494. accessor.setUnpackedInt32Element(1, 1, 1);
  2495. expectQualifiedIterable(
  2496. accessor.getRepeatedInt32Iterable(1),
  2497. (value) => Number.isInteger(value));
  2498. }
  2499. });
  2500. it('fail when setting single packed with out-of-bound index', () => {
  2501. const accessor =
  2502. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  2503. if (CHECK_CRITICAL_STATE) {
  2504. expect(() => accessor.setPackedInt32Element(1, 1, 1))
  2505. .toThrowError('Index out of bounds: index: 1 size: 1');
  2506. } else {
  2507. // Note in unchecked mode we produce invalid output for invalid inputs.
  2508. // This test just documents our behavior in those cases.
  2509. // These values might change at any point and are not considered
  2510. // what the implementation should be doing here.
  2511. accessor.setPackedInt32Element(1, 1, 1);
  2512. expectQualifiedIterable(
  2513. accessor.getRepeatedInt32Iterable(1),
  2514. (value) => Number.isInteger(value));
  2515. }
  2516. });
  2517. it('fail when getting element with out-of-range index', () => {
  2518. const accessor = LazyAccessor.createEmpty();
  2519. if (CHECK_CRITICAL_STATE) {
  2520. expect(() => {
  2521. accessor.getRepeatedInt32Element(
  2522. /* fieldNumber= */ 1, /* index= */ 0);
  2523. }).toThrowError('Index out of bounds: index: 0 size: 0');
  2524. } else {
  2525. // Note in unchecked mode we produce invalid output for invalid inputs.
  2526. // This test just documents our behavior in those cases.
  2527. // These values might change at any point and are not considered
  2528. // what the implementation should be doing here.
  2529. expect(accessor.getRepeatedInt32Element(
  2530. /* fieldNumber= */ 1, /* index= */ 0))
  2531. .toBe(undefined);
  2532. }
  2533. });
  2534. });
  2535. describe('LazyAccessor for repeated int64 does', () => {
  2536. const value1 = Int64.fromInt(1);
  2537. const value2 = Int64.fromInt(0);
  2538. const unpackedValue1Value2 = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  2539. const unpackedValue2Value1 = createArrayBuffer(0x08, 0x00, 0x08, 0x01);
  2540. const packedValue1Value2 = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  2541. const packedValue2Value1 = createArrayBuffer(0x0A, 0x02, 0x00, 0x01);
  2542. it('return empty array for the empty input', () => {
  2543. const accessor = LazyAccessor.createEmpty();
  2544. const list = accessor.getRepeatedInt64Iterable(1);
  2545. expectEqualToArray(list, []);
  2546. });
  2547. it('ensure not the same instance returned for the empty input', () => {
  2548. const accessor = LazyAccessor.createEmpty();
  2549. const list1 = accessor.getRepeatedInt64Iterable(1);
  2550. const list2 = accessor.getRepeatedInt64Iterable(1);
  2551. expect(list1).not.toBe(list2);
  2552. });
  2553. it('return size for the empty input', () => {
  2554. const accessor = LazyAccessor.createEmpty();
  2555. const size = accessor.getRepeatedInt64Size(1);
  2556. expect(size).toEqual(0);
  2557. });
  2558. it('return unpacked values from the input', () => {
  2559. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2560. const list = accessor.getRepeatedInt64Iterable(1);
  2561. expectEqualToArray(list, [value1, value2]);
  2562. });
  2563. it('ensure not the same instance returned for unpacked values', () => {
  2564. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2565. const list1 = accessor.getRepeatedInt64Iterable(1);
  2566. const list2 = accessor.getRepeatedInt64Iterable(1);
  2567. expect(list1).not.toBe(list2);
  2568. });
  2569. it('add single unpacked value', () => {
  2570. const accessor = LazyAccessor.createEmpty();
  2571. accessor.addUnpackedInt64Element(1, value1);
  2572. const list1 = accessor.getRepeatedInt64Iterable(1);
  2573. accessor.addUnpackedInt64Element(1, value2);
  2574. const list2 = accessor.getRepeatedInt64Iterable(1);
  2575. expectEqualToArray(list1, [value1]);
  2576. expectEqualToArray(list2, [value1, value2]);
  2577. });
  2578. it('add unpacked values', () => {
  2579. const accessor = LazyAccessor.createEmpty();
  2580. accessor.addUnpackedInt64Iterable(1, [value1]);
  2581. const list1 = accessor.getRepeatedInt64Iterable(1);
  2582. accessor.addUnpackedInt64Iterable(1, [value2]);
  2583. const list2 = accessor.getRepeatedInt64Iterable(1);
  2584. expectEqualToArray(list1, [value1]);
  2585. expectEqualToArray(list2, [value1, value2]);
  2586. });
  2587. it('set a single unpacked value', () => {
  2588. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  2589. accessor.setUnpackedInt64Element(1, 1, value1);
  2590. const list = accessor.getRepeatedInt64Iterable(1);
  2591. expectEqualToArray(list, [value1, value1]);
  2592. });
  2593. it('set unpacked values', () => {
  2594. const accessor = LazyAccessor.createEmpty();
  2595. accessor.setUnpackedInt64Iterable(1, [value1]);
  2596. const list = accessor.getRepeatedInt64Iterable(1);
  2597. expectEqualToArray(list, [value1]);
  2598. });
  2599. it('encode for adding single unpacked value', () => {
  2600. const accessor = LazyAccessor.createEmpty();
  2601. accessor.addUnpackedInt64Element(1, value1);
  2602. accessor.addUnpackedInt64Element(1, value2);
  2603. const serialized = accessor.serialize();
  2604. expect(serialized).toEqual(unpackedValue1Value2);
  2605. });
  2606. it('encode for adding unpacked values', () => {
  2607. const accessor = LazyAccessor.createEmpty();
  2608. accessor.addUnpackedInt64Iterable(1, [value1]);
  2609. accessor.addUnpackedInt64Iterable(1, [value2]);
  2610. const serialized = accessor.serialize();
  2611. expect(serialized).toEqual(unpackedValue1Value2);
  2612. });
  2613. it('encode for setting single unpacked value', () => {
  2614. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2615. accessor.setUnpackedInt64Element(1, 0, value2);
  2616. accessor.setUnpackedInt64Element(1, 1, value1);
  2617. const serialized = accessor.serialize();
  2618. expect(serialized).toEqual(unpackedValue2Value1);
  2619. });
  2620. it('encode for setting unpacked values', () => {
  2621. const accessor = LazyAccessor.createEmpty();
  2622. accessor.setUnpackedInt64Iterable(1, [value1, value2]);
  2623. const serialized = accessor.serialize();
  2624. expect(serialized).toEqual(unpackedValue1Value2);
  2625. });
  2626. it('return packed values from the input', () => {
  2627. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  2628. const list = accessor.getRepeatedInt64Iterable(1);
  2629. expectEqualToArray(list, [value1, value2]);
  2630. });
  2631. it('ensure not the same instance returned for packed values', () => {
  2632. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  2633. const list1 = accessor.getRepeatedInt64Iterable(1);
  2634. const list2 = accessor.getRepeatedInt64Iterable(1);
  2635. expect(list1).not.toBe(list2);
  2636. });
  2637. it('add single packed value', () => {
  2638. const accessor = LazyAccessor.createEmpty();
  2639. accessor.addPackedInt64Element(1, value1);
  2640. const list1 = accessor.getRepeatedInt64Iterable(1);
  2641. accessor.addPackedInt64Element(1, value2);
  2642. const list2 = accessor.getRepeatedInt64Iterable(1);
  2643. expectEqualToArray(list1, [value1]);
  2644. expectEqualToArray(list2, [value1, value2]);
  2645. });
  2646. it('add packed values', () => {
  2647. const accessor = LazyAccessor.createEmpty();
  2648. accessor.addPackedInt64Iterable(1, [value1]);
  2649. const list1 = accessor.getRepeatedInt64Iterable(1);
  2650. accessor.addPackedInt64Iterable(1, [value2]);
  2651. const list2 = accessor.getRepeatedInt64Iterable(1);
  2652. expectEqualToArray(list1, [value1]);
  2653. expectEqualToArray(list2, [value1, value2]);
  2654. });
  2655. it('set a single packed value', () => {
  2656. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2657. accessor.setPackedInt64Element(1, 1, value1);
  2658. const list = accessor.getRepeatedInt64Iterable(1);
  2659. expectEqualToArray(list, [value1, value1]);
  2660. });
  2661. it('set packed values', () => {
  2662. const accessor = LazyAccessor.createEmpty();
  2663. accessor.setPackedInt64Iterable(1, [value1]);
  2664. const list1 = accessor.getRepeatedInt64Iterable(1);
  2665. accessor.setPackedInt64Iterable(1, [value2]);
  2666. const list2 = accessor.getRepeatedInt64Iterable(1);
  2667. expectEqualToArray(list1, [value1]);
  2668. expectEqualToArray(list2, [value2]);
  2669. });
  2670. it('encode for adding single packed value', () => {
  2671. const accessor = LazyAccessor.createEmpty();
  2672. accessor.addPackedInt64Element(1, value1);
  2673. accessor.addPackedInt64Element(1, value2);
  2674. const serialized = accessor.serialize();
  2675. expect(serialized).toEqual(packedValue1Value2);
  2676. });
  2677. it('encode for adding packed values', () => {
  2678. const accessor = LazyAccessor.createEmpty();
  2679. accessor.addPackedInt64Iterable(1, [value1, value2]);
  2680. const serialized = accessor.serialize();
  2681. expect(serialized).toEqual(packedValue1Value2);
  2682. });
  2683. it('encode for setting single packed value', () => {
  2684. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2685. accessor.setPackedInt64Element(1, 0, value2);
  2686. accessor.setPackedInt64Element(1, 1, value1);
  2687. const serialized = accessor.serialize();
  2688. expect(serialized).toEqual(packedValue2Value1);
  2689. });
  2690. it('encode for setting packed values', () => {
  2691. const accessor = LazyAccessor.createEmpty();
  2692. accessor.setPackedInt64Iterable(1, [value1, value2]);
  2693. const serialized = accessor.serialize();
  2694. expect(serialized).toEqual(packedValue1Value2);
  2695. });
  2696. it('return combined values from the input', () => {
  2697. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  2698. 0x08,
  2699. 0x01, // unpacked value1
  2700. 0x0A,
  2701. 0x02,
  2702. 0x01,
  2703. 0x00, // packed value1 and value2
  2704. 0x08,
  2705. 0x00, // unpacked value2
  2706. ));
  2707. const list = accessor.getRepeatedInt64Iterable(1);
  2708. expectEqualToArray(list, [value1, value1, value2, value2]);
  2709. });
  2710. it('return the repeated field element from the input', () => {
  2711. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2712. const result1 = accessor.getRepeatedInt64Element(
  2713. /* fieldNumber= */ 1, /* index= */ 0);
  2714. const result2 = accessor.getRepeatedInt64Element(
  2715. /* fieldNumber= */ 1, /* index= */ 1);
  2716. expect(result1).toEqual(value1);
  2717. expect(result2).toEqual(value2);
  2718. });
  2719. it('return the size from the input', () => {
  2720. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2721. const size = accessor.getRepeatedInt64Size(1);
  2722. expect(size).toEqual(2);
  2723. });
  2724. it('fail when getting unpacked int64 value with other wire types', () => {
  2725. const accessor = LazyAccessor.fromArrayBuffer(
  2726. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  2727. if (CHECK_CRITICAL_STATE) {
  2728. expect(() => {
  2729. accessor.getRepeatedInt64Iterable(1);
  2730. }).toThrowError('Expected wire type: 0 but found: 5');
  2731. } else {
  2732. // Note in unchecked mode we produce invalid output for invalid inputs.
  2733. // This test just documents our behavior in those cases.
  2734. // These values might change at any point and are not considered
  2735. // what the implementation should be doing here.
  2736. expectQualifiedIterable(
  2737. accessor.getRepeatedInt64Iterable(1),
  2738. (value) => value instanceof Int64);
  2739. }
  2740. });
  2741. it('fail when adding unpacked int64 values with null value', () => {
  2742. const accessor = LazyAccessor.createEmpty();
  2743. const fakeInt64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  2744. if (CHECK_CRITICAL_STATE) {
  2745. expect(() => accessor.addUnpackedInt64Iterable(1, [fakeInt64]))
  2746. .toThrowError('Must be Int64 instance, but got: null');
  2747. } else {
  2748. // Note in unchecked mode we produce invalid output for invalid inputs.
  2749. // This test just documents our behavior in those cases.
  2750. // These values might change at any point and are not considered
  2751. // what the implementation should be doing here.
  2752. accessor.addUnpackedInt64Iterable(1, [fakeInt64]);
  2753. expectQualifiedIterable(accessor.getRepeatedInt64Iterable(1));
  2754. }
  2755. });
  2756. it('fail when adding single unpacked int64 value with null value', () => {
  2757. const accessor = LazyAccessor.createEmpty();
  2758. const fakeInt64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  2759. if (CHECK_CRITICAL_STATE) {
  2760. expect(() => accessor.addUnpackedInt64Element(1, fakeInt64))
  2761. .toThrowError('Must be Int64 instance, but got: null');
  2762. } else {
  2763. // Note in unchecked mode we produce invalid output for invalid inputs.
  2764. // This test just documents our behavior in those cases.
  2765. // These values might change at any point and are not considered
  2766. // what the implementation should be doing here.
  2767. accessor.addUnpackedInt64Element(1, fakeInt64);
  2768. expectQualifiedIterable(accessor.getRepeatedInt64Iterable(1));
  2769. }
  2770. });
  2771. it('fail when setting unpacked int64 values with null value', () => {
  2772. const accessor = LazyAccessor.createEmpty();
  2773. const fakeInt64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  2774. if (CHECK_CRITICAL_STATE) {
  2775. expect(() => accessor.setUnpackedInt64Iterable(1, [fakeInt64]))
  2776. .toThrowError('Must be Int64 instance, but got: null');
  2777. } else {
  2778. // Note in unchecked mode we produce invalid output for invalid inputs.
  2779. // This test just documents our behavior in those cases.
  2780. // These values might change at any point and are not considered
  2781. // what the implementation should be doing here.
  2782. accessor.setUnpackedInt64Iterable(1, [fakeInt64]);
  2783. expectQualifiedIterable(accessor.getRepeatedInt64Iterable(1));
  2784. }
  2785. });
  2786. it('fail when setting single unpacked int64 value with null value', () => {
  2787. const accessor = LazyAccessor.fromArrayBuffer(
  2788. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  2789. const fakeInt64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  2790. if (CHECK_CRITICAL_STATE) {
  2791. expect(() => accessor.setUnpackedInt64Element(1, 0, fakeInt64))
  2792. .toThrowError('Must be Int64 instance, but got: null');
  2793. } else {
  2794. // Note in unchecked mode we produce invalid output for invalid inputs.
  2795. // This test just documents our behavior in those cases.
  2796. // These values might change at any point and are not considered
  2797. // what the implementation should be doing here.
  2798. accessor.setUnpackedInt64Element(1, 0, fakeInt64);
  2799. expectQualifiedIterable(accessor.getRepeatedInt64Iterable(1));
  2800. }
  2801. });
  2802. it('fail when adding packed int64 values with null value', () => {
  2803. const accessor = LazyAccessor.createEmpty();
  2804. const fakeInt64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  2805. if (CHECK_CRITICAL_STATE) {
  2806. expect(() => accessor.addPackedInt64Iterable(1, [fakeInt64]))
  2807. .toThrowError('Must be Int64 instance, but got: null');
  2808. } else {
  2809. // Note in unchecked mode we produce invalid output for invalid inputs.
  2810. // This test just documents our behavior in those cases.
  2811. // These values might change at any point and are not considered
  2812. // what the implementation should be doing here.
  2813. accessor.addPackedInt64Iterable(1, [fakeInt64]);
  2814. expectQualifiedIterable(accessor.getRepeatedInt64Iterable(1));
  2815. }
  2816. });
  2817. it('fail when adding single packed int64 value with null value', () => {
  2818. const accessor = LazyAccessor.createEmpty();
  2819. const fakeInt64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  2820. if (CHECK_CRITICAL_STATE) {
  2821. expect(() => accessor.addPackedInt64Element(1, fakeInt64))
  2822. .toThrowError('Must be Int64 instance, but got: null');
  2823. } else {
  2824. // Note in unchecked mode we produce invalid output for invalid inputs.
  2825. // This test just documents our behavior in those cases.
  2826. // These values might change at any point and are not considered
  2827. // what the implementation should be doing here.
  2828. accessor.addPackedInt64Element(1, fakeInt64);
  2829. expectQualifiedIterable(accessor.getRepeatedInt64Iterable(1));
  2830. }
  2831. });
  2832. it('fail when setting packed int64 values with null value', () => {
  2833. const accessor = LazyAccessor.createEmpty();
  2834. const fakeInt64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  2835. if (CHECK_CRITICAL_STATE) {
  2836. expect(() => accessor.setPackedInt64Iterable(1, [fakeInt64]))
  2837. .toThrowError('Must be Int64 instance, but got: null');
  2838. } else {
  2839. // Note in unchecked mode we produce invalid output for invalid inputs.
  2840. // This test just documents our behavior in those cases.
  2841. // These values might change at any point and are not considered
  2842. // what the implementation should be doing here.
  2843. accessor.setPackedInt64Iterable(1, [fakeInt64]);
  2844. expectQualifiedIterable(accessor.getRepeatedInt64Iterable(1));
  2845. }
  2846. });
  2847. it('fail when setting single packed int64 value with null value', () => {
  2848. const accessor =
  2849. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  2850. const fakeInt64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  2851. if (CHECK_CRITICAL_STATE) {
  2852. expect(() => accessor.setPackedInt64Element(1, 0, fakeInt64))
  2853. .toThrowError('Must be Int64 instance, but got: null');
  2854. } else {
  2855. // Note in unchecked mode we produce invalid output for invalid inputs.
  2856. // This test just documents our behavior in those cases.
  2857. // These values might change at any point and are not considered
  2858. // what the implementation should be doing here.
  2859. accessor.setPackedInt64Element(1, 0, fakeInt64);
  2860. expectQualifiedIterable(accessor.getRepeatedInt64Iterable(1));
  2861. }
  2862. });
  2863. it('fail when setting single unpacked with out-of-bound index', () => {
  2864. const accessor =
  2865. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x00));
  2866. if (CHECK_CRITICAL_STATE) {
  2867. expect(() => accessor.setUnpackedInt64Element(1, 1, Int64.fromInt(1)))
  2868. .toThrowError('Index out of bounds: index: 1 size: 1');
  2869. } else {
  2870. // Note in unchecked mode we produce invalid output for invalid inputs.
  2871. // This test just documents our behavior in those cases.
  2872. // These values might change at any point and are not considered
  2873. // what the implementation should be doing here.
  2874. accessor.setUnpackedInt64Element(1, 1, Int64.fromInt(1));
  2875. expectQualifiedIterable(
  2876. accessor.getRepeatedInt64Iterable(1),
  2877. (value) => value instanceof Int64);
  2878. }
  2879. });
  2880. it('fail when setting single packed with out-of-bound index', () => {
  2881. const accessor =
  2882. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  2883. if (CHECK_CRITICAL_STATE) {
  2884. expect(() => accessor.setPackedInt64Element(1, 1, Int64.fromInt(1)))
  2885. .toThrowError('Index out of bounds: index: 1 size: 1');
  2886. } else {
  2887. // Note in unchecked mode we produce invalid output for invalid inputs.
  2888. // This test just documents our behavior in those cases.
  2889. // These values might change at any point and are not considered
  2890. // what the implementation should be doing here.
  2891. accessor.setPackedInt64Element(1, 1, Int64.fromInt(1));
  2892. expectQualifiedIterable(
  2893. accessor.getRepeatedInt64Iterable(1),
  2894. (value) => value instanceof Int64);
  2895. }
  2896. });
  2897. it('fail when getting element with out-of-range index', () => {
  2898. const accessor = LazyAccessor.createEmpty();
  2899. if (CHECK_CRITICAL_STATE) {
  2900. expect(() => {
  2901. accessor.getRepeatedInt64Element(
  2902. /* fieldNumber= */ 1, /* index= */ 0);
  2903. }).toThrowError('Index out of bounds: index: 0 size: 0');
  2904. } else {
  2905. // Note in unchecked mode we produce invalid output for invalid inputs.
  2906. // This test just documents our behavior in those cases.
  2907. // These values might change at any point and are not considered
  2908. // what the implementation should be doing here.
  2909. expect(accessor.getRepeatedInt64Element(
  2910. /* fieldNumber= */ 1, /* index= */ 0))
  2911. .toBe(undefined);
  2912. }
  2913. });
  2914. });
  2915. describe('LazyAccessor for repeated sfixed32 does', () => {
  2916. const value1 = 1;
  2917. const value2 = 0;
  2918. const unpackedValue1Value2 = createArrayBuffer(
  2919. 0x0D, 0x01, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x00);
  2920. const unpackedValue2Value1 = createArrayBuffer(
  2921. 0x0D, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x01, 0x00, 0x00, 0x00);
  2922. const packedValue1Value2 = createArrayBuffer(
  2923. 0x0A, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  2924. const packedValue2Value1 = createArrayBuffer(
  2925. 0x0A, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00);
  2926. it('return empty array for the empty input', () => {
  2927. const accessor = LazyAccessor.createEmpty();
  2928. const list = accessor.getRepeatedSfixed32Iterable(1);
  2929. expectEqualToArray(list, []);
  2930. });
  2931. it('ensure not the same instance returned for the empty input', () => {
  2932. const accessor = LazyAccessor.createEmpty();
  2933. const list1 = accessor.getRepeatedSfixed32Iterable(1);
  2934. const list2 = accessor.getRepeatedSfixed32Iterable(1);
  2935. expect(list1).not.toBe(list2);
  2936. });
  2937. it('return size for the empty input', () => {
  2938. const accessor = LazyAccessor.createEmpty();
  2939. const size = accessor.getRepeatedSfixed32Size(1);
  2940. expect(size).toEqual(0);
  2941. });
  2942. it('return unpacked values from the input', () => {
  2943. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2944. const list = accessor.getRepeatedSfixed32Iterable(1);
  2945. expectEqualToArray(list, [value1, value2]);
  2946. });
  2947. it('ensure not the same instance returned for unpacked values', () => {
  2948. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2949. const list1 = accessor.getRepeatedSfixed32Iterable(1);
  2950. const list2 = accessor.getRepeatedSfixed32Iterable(1);
  2951. expect(list1).not.toBe(list2);
  2952. });
  2953. it('add single unpacked value', () => {
  2954. const accessor = LazyAccessor.createEmpty();
  2955. accessor.addUnpackedSfixed32Element(1, value1);
  2956. const list1 = accessor.getRepeatedSfixed32Iterable(1);
  2957. accessor.addUnpackedSfixed32Element(1, value2);
  2958. const list2 = accessor.getRepeatedSfixed32Iterable(1);
  2959. expectEqualToArray(list1, [value1]);
  2960. expectEqualToArray(list2, [value1, value2]);
  2961. });
  2962. it('add unpacked values', () => {
  2963. const accessor = LazyAccessor.createEmpty();
  2964. accessor.addUnpackedSfixed32Iterable(1, [value1]);
  2965. const list1 = accessor.getRepeatedSfixed32Iterable(1);
  2966. accessor.addUnpackedSfixed32Iterable(1, [value2]);
  2967. const list2 = accessor.getRepeatedSfixed32Iterable(1);
  2968. expectEqualToArray(list1, [value1]);
  2969. expectEqualToArray(list2, [value1, value2]);
  2970. });
  2971. it('set a single unpacked value', () => {
  2972. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  2973. accessor.setUnpackedSfixed32Element(1, 1, value1);
  2974. const list = accessor.getRepeatedSfixed32Iterable(1);
  2975. expectEqualToArray(list, [value1, value1]);
  2976. });
  2977. it('set unpacked values', () => {
  2978. const accessor = LazyAccessor.createEmpty();
  2979. accessor.setUnpackedSfixed32Iterable(1, [value1]);
  2980. const list = accessor.getRepeatedSfixed32Iterable(1);
  2981. expectEqualToArray(list, [value1]);
  2982. });
  2983. it('encode for adding single unpacked value', () => {
  2984. const accessor = LazyAccessor.createEmpty();
  2985. accessor.addUnpackedSfixed32Element(1, value1);
  2986. accessor.addUnpackedSfixed32Element(1, value2);
  2987. const serialized = accessor.serialize();
  2988. expect(serialized).toEqual(unpackedValue1Value2);
  2989. });
  2990. it('encode for adding unpacked values', () => {
  2991. const accessor = LazyAccessor.createEmpty();
  2992. accessor.addUnpackedSfixed32Iterable(1, [value1]);
  2993. accessor.addUnpackedSfixed32Iterable(1, [value2]);
  2994. const serialized = accessor.serialize();
  2995. expect(serialized).toEqual(unpackedValue1Value2);
  2996. });
  2997. it('encode for setting single unpacked value', () => {
  2998. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  2999. accessor.setUnpackedSfixed32Element(1, 0, value2);
  3000. accessor.setUnpackedSfixed32Element(1, 1, value1);
  3001. const serialized = accessor.serialize();
  3002. expect(serialized).toEqual(unpackedValue2Value1);
  3003. });
  3004. it('encode for setting unpacked values', () => {
  3005. const accessor = LazyAccessor.createEmpty();
  3006. accessor.setUnpackedSfixed32Iterable(1, [value1, value2]);
  3007. const serialized = accessor.serialize();
  3008. expect(serialized).toEqual(unpackedValue1Value2);
  3009. });
  3010. it('return packed values from the input', () => {
  3011. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  3012. const list = accessor.getRepeatedSfixed32Iterable(1);
  3013. expectEqualToArray(list, [value1, value2]);
  3014. });
  3015. it('ensure not the same instance returned for packed values', () => {
  3016. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  3017. const list1 = accessor.getRepeatedSfixed32Iterable(1);
  3018. const list2 = accessor.getRepeatedSfixed32Iterable(1);
  3019. expect(list1).not.toBe(list2);
  3020. });
  3021. it('add single packed value', () => {
  3022. const accessor = LazyAccessor.createEmpty();
  3023. accessor.addPackedSfixed32Element(1, value1);
  3024. const list1 = accessor.getRepeatedSfixed32Iterable(1);
  3025. accessor.addPackedSfixed32Element(1, value2);
  3026. const list2 = accessor.getRepeatedSfixed32Iterable(1);
  3027. expectEqualToArray(list1, [value1]);
  3028. expectEqualToArray(list2, [value1, value2]);
  3029. });
  3030. it('add packed values', () => {
  3031. const accessor = LazyAccessor.createEmpty();
  3032. accessor.addPackedSfixed32Iterable(1, [value1]);
  3033. const list1 = accessor.getRepeatedSfixed32Iterable(1);
  3034. accessor.addPackedSfixed32Iterable(1, [value2]);
  3035. const list2 = accessor.getRepeatedSfixed32Iterable(1);
  3036. expectEqualToArray(list1, [value1]);
  3037. expectEqualToArray(list2, [value1, value2]);
  3038. });
  3039. it('set a single packed value', () => {
  3040. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3041. accessor.setPackedSfixed32Element(1, 1, value1);
  3042. const list = accessor.getRepeatedSfixed32Iterable(1);
  3043. expectEqualToArray(list, [value1, value1]);
  3044. });
  3045. it('set packed values', () => {
  3046. const accessor = LazyAccessor.createEmpty();
  3047. accessor.setPackedSfixed32Iterable(1, [value1]);
  3048. const list1 = accessor.getRepeatedSfixed32Iterable(1);
  3049. accessor.setPackedSfixed32Iterable(1, [value2]);
  3050. const list2 = accessor.getRepeatedSfixed32Iterable(1);
  3051. expectEqualToArray(list1, [value1]);
  3052. expectEqualToArray(list2, [value2]);
  3053. });
  3054. it('encode for adding single packed value', () => {
  3055. const accessor = LazyAccessor.createEmpty();
  3056. accessor.addPackedSfixed32Element(1, value1);
  3057. accessor.addPackedSfixed32Element(1, value2);
  3058. const serialized = accessor.serialize();
  3059. expect(serialized).toEqual(packedValue1Value2);
  3060. });
  3061. it('encode for adding packed values', () => {
  3062. const accessor = LazyAccessor.createEmpty();
  3063. accessor.addPackedSfixed32Iterable(1, [value1, value2]);
  3064. const serialized = accessor.serialize();
  3065. expect(serialized).toEqual(packedValue1Value2);
  3066. });
  3067. it('encode for setting single packed value', () => {
  3068. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3069. accessor.setPackedSfixed32Element(1, 0, value2);
  3070. accessor.setPackedSfixed32Element(1, 1, value1);
  3071. const serialized = accessor.serialize();
  3072. expect(serialized).toEqual(packedValue2Value1);
  3073. });
  3074. it('encode for setting packed values', () => {
  3075. const accessor = LazyAccessor.createEmpty();
  3076. accessor.setPackedSfixed32Iterable(1, [value1, value2]);
  3077. const serialized = accessor.serialize();
  3078. expect(serialized).toEqual(packedValue1Value2);
  3079. });
  3080. it('return combined values from the input', () => {
  3081. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  3082. 0x0D,
  3083. 0x01,
  3084. 0x00,
  3085. 0x00,
  3086. 0x00, // value1
  3087. 0x0A,
  3088. 0x08, // tag
  3089. 0x01,
  3090. 0x00,
  3091. 0x00,
  3092. 0x00, // value1
  3093. 0x00,
  3094. 0x00,
  3095. 0x00,
  3096. 0x00, // value2
  3097. 0x0D,
  3098. 0x00,
  3099. 0x00,
  3100. 0x00,
  3101. 0x00, // value2
  3102. ));
  3103. const list = accessor.getRepeatedSfixed32Iterable(1);
  3104. expectEqualToArray(list, [value1, value1, value2, value2]);
  3105. });
  3106. it('return the repeated field element from the input', () => {
  3107. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3108. const result1 = accessor.getRepeatedSfixed32Element(
  3109. /* fieldNumber= */ 1, /* index= */ 0);
  3110. const result2 = accessor.getRepeatedSfixed32Element(
  3111. /* fieldNumber= */ 1, /* index= */ 1);
  3112. expect(result1).toEqual(value1);
  3113. expect(result2).toEqual(value2);
  3114. });
  3115. it('return the size from the input', () => {
  3116. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3117. const size = accessor.getRepeatedSfixed32Size(1);
  3118. expect(size).toEqual(2);
  3119. });
  3120. it('fail when getting unpacked sfixed32 value with other wire types', () => {
  3121. const accessor = LazyAccessor.fromArrayBuffer(
  3122. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  3123. if (CHECK_CRITICAL_STATE) {
  3124. expect(() => {
  3125. accessor.getRepeatedSfixed32Iterable(1);
  3126. }).toThrowError('Expected wire type: 5 but found: 0');
  3127. } else {
  3128. // Note in unchecked mode we produce invalid output for invalid inputs.
  3129. // This test just documents our behavior in those cases.
  3130. // These values might change at any point and are not considered
  3131. // what the implementation should be doing here.
  3132. expectQualifiedIterable(
  3133. accessor.getRepeatedSfixed32Iterable(1),
  3134. (value) => typeof value === 'number');
  3135. }
  3136. });
  3137. it('fail when adding unpacked sfixed32 values with null value', () => {
  3138. const accessor = LazyAccessor.createEmpty();
  3139. const fakeSfixed32 = /** @type {number} */ (/** @type {*} */ (null));
  3140. if (CHECK_CRITICAL_STATE) {
  3141. expect(() => accessor.addUnpackedSfixed32Iterable(1, [fakeSfixed32]))
  3142. .toThrowError('Must be a number, but got: null');
  3143. } else {
  3144. // Note in unchecked mode we produce invalid output for invalid inputs.
  3145. // This test just documents our behavior in those cases.
  3146. // These values might change at any point and are not considered
  3147. // what the implementation should be doing here.
  3148. accessor.addUnpackedSfixed32Iterable(1, [fakeSfixed32]);
  3149. expectQualifiedIterable(accessor.getRepeatedSfixed32Iterable(1));
  3150. }
  3151. });
  3152. it('fail when adding single unpacked sfixed32 value with null value', () => {
  3153. const accessor = LazyAccessor.createEmpty();
  3154. const fakeSfixed32 = /** @type {number} */ (/** @type {*} */ (null));
  3155. if (CHECK_CRITICAL_STATE) {
  3156. expect(() => accessor.addUnpackedSfixed32Element(1, fakeSfixed32))
  3157. .toThrowError('Must be a number, but got: null');
  3158. } else {
  3159. // Note in unchecked mode we produce invalid output for invalid inputs.
  3160. // This test just documents our behavior in those cases.
  3161. // These values might change at any point and are not considered
  3162. // what the implementation should be doing here.
  3163. accessor.addUnpackedSfixed32Element(1, fakeSfixed32);
  3164. expectQualifiedIterable(accessor.getRepeatedSfixed32Iterable(1));
  3165. }
  3166. });
  3167. it('fail when setting unpacked sfixed32 values with null value', () => {
  3168. const accessor = LazyAccessor.createEmpty();
  3169. const fakeSfixed32 = /** @type {number} */ (/** @type {*} */ (null));
  3170. if (CHECK_CRITICAL_STATE) {
  3171. expect(() => accessor.setUnpackedSfixed32Iterable(1, [fakeSfixed32]))
  3172. .toThrowError('Must be a number, but got: null');
  3173. } else {
  3174. // Note in unchecked mode we produce invalid output for invalid inputs.
  3175. // This test just documents our behavior in those cases.
  3176. // These values might change at any point and are not considered
  3177. // what the implementation should be doing here.
  3178. accessor.setUnpackedSfixed32Iterable(1, [fakeSfixed32]);
  3179. expectQualifiedIterable(accessor.getRepeatedSfixed32Iterable(1));
  3180. }
  3181. });
  3182. it('fail when setting single unpacked sfixed32 value with null value', () => {
  3183. const accessor = LazyAccessor.fromArrayBuffer(
  3184. createArrayBuffer(0x08, 0x80, 0x80, 0x80, 0x00));
  3185. const fakeSfixed32 = /** @type {number} */ (/** @type {*} */ (null));
  3186. if (CHECK_CRITICAL_STATE) {
  3187. expect(() => accessor.setUnpackedSfixed32Element(1, 0, fakeSfixed32))
  3188. .toThrowError('Must be a number, but got: null');
  3189. } else {
  3190. // Note in unchecked mode we produce invalid output for invalid inputs.
  3191. // This test just documents our behavior in those cases.
  3192. // These values might change at any point and are not considered
  3193. // what the implementation should be doing here.
  3194. accessor.setUnpackedSfixed32Element(1, 0, fakeSfixed32);
  3195. expectQualifiedIterable(accessor.getRepeatedSfixed32Iterable(1));
  3196. }
  3197. });
  3198. it('fail when adding packed sfixed32 values with null value', () => {
  3199. const accessor = LazyAccessor.createEmpty();
  3200. const fakeSfixed32 = /** @type {number} */ (/** @type {*} */ (null));
  3201. if (CHECK_CRITICAL_STATE) {
  3202. expect(() => accessor.addPackedSfixed32Iterable(1, [fakeSfixed32]))
  3203. .toThrowError('Must be a number, but got: null');
  3204. } else {
  3205. // Note in unchecked mode we produce invalid output for invalid inputs.
  3206. // This test just documents our behavior in those cases.
  3207. // These values might change at any point and are not considered
  3208. // what the implementation should be doing here.
  3209. accessor.addPackedSfixed32Iterable(1, [fakeSfixed32]);
  3210. expectQualifiedIterable(accessor.getRepeatedSfixed32Iterable(1));
  3211. }
  3212. });
  3213. it('fail when adding single packed sfixed32 value with null value', () => {
  3214. const accessor = LazyAccessor.createEmpty();
  3215. const fakeSfixed32 = /** @type {number} */ (/** @type {*} */ (null));
  3216. if (CHECK_CRITICAL_STATE) {
  3217. expect(() => accessor.addPackedSfixed32Element(1, fakeSfixed32))
  3218. .toThrowError('Must be a number, but got: null');
  3219. } else {
  3220. // Note in unchecked mode we produce invalid output for invalid inputs.
  3221. // This test just documents our behavior in those cases.
  3222. // These values might change at any point and are not considered
  3223. // what the implementation should be doing here.
  3224. accessor.addPackedSfixed32Element(1, fakeSfixed32);
  3225. expectQualifiedIterable(accessor.getRepeatedSfixed32Iterable(1));
  3226. }
  3227. });
  3228. it('fail when setting packed sfixed32 values with null value', () => {
  3229. const accessor = LazyAccessor.createEmpty();
  3230. const fakeSfixed32 = /** @type {number} */ (/** @type {*} */ (null));
  3231. if (CHECK_CRITICAL_STATE) {
  3232. expect(() => accessor.setPackedSfixed32Iterable(1, [fakeSfixed32]))
  3233. .toThrowError('Must be a number, but got: null');
  3234. } else {
  3235. // Note in unchecked mode we produce invalid output for invalid inputs.
  3236. // This test just documents our behavior in those cases.
  3237. // These values might change at any point and are not considered
  3238. // what the implementation should be doing here.
  3239. accessor.setPackedSfixed32Iterable(1, [fakeSfixed32]);
  3240. expectQualifiedIterable(accessor.getRepeatedSfixed32Iterable(1));
  3241. }
  3242. });
  3243. it('fail when setting single packed sfixed32 value with null value', () => {
  3244. const accessor = LazyAccessor.fromArrayBuffer(
  3245. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  3246. const fakeSfixed32 = /** @type {number} */ (/** @type {*} */ (null));
  3247. if (CHECK_CRITICAL_STATE) {
  3248. expect(() => accessor.setPackedSfixed32Element(1, 0, fakeSfixed32))
  3249. .toThrowError('Must be a number, but got: null');
  3250. } else {
  3251. // Note in unchecked mode we produce invalid output for invalid inputs.
  3252. // This test just documents our behavior in those cases.
  3253. // These values might change at any point and are not considered
  3254. // what the implementation should be doing here.
  3255. accessor.setPackedSfixed32Element(1, 0, fakeSfixed32);
  3256. expectQualifiedIterable(accessor.getRepeatedSfixed32Iterable(1));
  3257. }
  3258. });
  3259. it('fail when setting single unpacked with out-of-bound index', () => {
  3260. const accessor = LazyAccessor.fromArrayBuffer(
  3261. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  3262. if (CHECK_CRITICAL_STATE) {
  3263. expect(() => accessor.setUnpackedSfixed32Element(1, 1, 1))
  3264. .toThrowError('Index out of bounds: index: 1 size: 1');
  3265. } else {
  3266. // Note in unchecked mode we produce invalid output for invalid inputs.
  3267. // This test just documents our behavior in those cases.
  3268. // These values might change at any point and are not considered
  3269. // what the implementation should be doing here.
  3270. accessor.setUnpackedSfixed32Element(1, 1, 1);
  3271. expectQualifiedIterable(
  3272. accessor.getRepeatedSfixed32Iterable(1),
  3273. (value) => typeof value === 'number');
  3274. }
  3275. });
  3276. it('fail when setting single packed with out-of-bound index', () => {
  3277. const accessor = LazyAccessor.fromArrayBuffer(
  3278. createArrayBuffer(0x0D, 0x00, 0x00, 0x00, 0x00));
  3279. if (CHECK_CRITICAL_STATE) {
  3280. expect(() => accessor.setPackedSfixed32Element(1, 1, 1))
  3281. .toThrowError('Index out of bounds: index: 1 size: 1');
  3282. } else {
  3283. // Note in unchecked mode we produce invalid output for invalid inputs.
  3284. // This test just documents our behavior in those cases.
  3285. // These values might change at any point and are not considered
  3286. // what the implementation should be doing here.
  3287. accessor.setPackedSfixed32Element(1, 1, 1);
  3288. expectQualifiedIterable(
  3289. accessor.getRepeatedSfixed32Iterable(1),
  3290. (value) => typeof value === 'number');
  3291. }
  3292. });
  3293. it('fail when getting element with out-of-range index', () => {
  3294. const accessor = LazyAccessor.createEmpty();
  3295. if (CHECK_CRITICAL_STATE) {
  3296. expect(() => {
  3297. accessor.getRepeatedSfixed32Element(
  3298. /* fieldNumber= */ 1, /* index= */ 0);
  3299. }).toThrowError('Index out of bounds: index: 0 size: 0');
  3300. } else {
  3301. // Note in unchecked mode we produce invalid output for invalid inputs.
  3302. // This test just documents our behavior in those cases.
  3303. // These values might change at any point and are not considered
  3304. // what the implementation should be doing here.
  3305. expect(accessor.getRepeatedSfixed32Element(
  3306. /* fieldNumber= */ 1, /* index= */ 0))
  3307. .toBe(undefined);
  3308. }
  3309. });
  3310. });
  3311. describe('LazyAccessor for repeated sfixed64 does', () => {
  3312. const value1 = Int64.fromInt(1);
  3313. const value2 = Int64.fromInt(0);
  3314. const unpackedValue1Value2 = createArrayBuffer(
  3315. 0x09,
  3316. 0x01,
  3317. 0x00,
  3318. 0x00,
  3319. 0x00,
  3320. 0x00,
  3321. 0x00,
  3322. 0x00,
  3323. 0x00, // value1
  3324. 0x09,
  3325. 0x00,
  3326. 0x00,
  3327. 0x00,
  3328. 0x00,
  3329. 0x00,
  3330. 0x00,
  3331. 0x00,
  3332. 0x00, // value2
  3333. );
  3334. const unpackedValue2Value1 = createArrayBuffer(
  3335. 0x09,
  3336. 0x00,
  3337. 0x00,
  3338. 0x00,
  3339. 0x00,
  3340. 0x00,
  3341. 0x00,
  3342. 0x00,
  3343. 0x00, // value1
  3344. 0x09,
  3345. 0x01,
  3346. 0x00,
  3347. 0x00,
  3348. 0x00,
  3349. 0x00,
  3350. 0x00,
  3351. 0x00,
  3352. 0x00, // value2
  3353. );
  3354. const packedValue1Value2 = createArrayBuffer(
  3355. 0x0A,
  3356. 0x10, // tag
  3357. 0x01,
  3358. 0x00,
  3359. 0x00,
  3360. 0x00,
  3361. 0x00,
  3362. 0x00,
  3363. 0x00,
  3364. 0x00, // value1
  3365. 0x00,
  3366. 0x00,
  3367. 0x00,
  3368. 0x00,
  3369. 0x00,
  3370. 0x00,
  3371. 0x00,
  3372. 0x00, // value2
  3373. );
  3374. const packedValue2Value1 = createArrayBuffer(
  3375. 0x0A,
  3376. 0x10, // tag
  3377. 0x00,
  3378. 0x00,
  3379. 0x00,
  3380. 0x00,
  3381. 0x00,
  3382. 0x00,
  3383. 0x00,
  3384. 0x00, // value2
  3385. 0x01,
  3386. 0x00,
  3387. 0x00,
  3388. 0x00,
  3389. 0x00,
  3390. 0x00,
  3391. 0x00,
  3392. 0x00, // value1
  3393. );
  3394. it('return empty array for the empty input', () => {
  3395. const accessor = LazyAccessor.createEmpty();
  3396. const list = accessor.getRepeatedSfixed64Iterable(1);
  3397. expectEqualToArray(list, []);
  3398. });
  3399. it('ensure not the same instance returned for the empty input', () => {
  3400. const accessor = LazyAccessor.createEmpty();
  3401. const list1 = accessor.getRepeatedSfixed64Iterable(1);
  3402. const list2 = accessor.getRepeatedSfixed64Iterable(1);
  3403. expect(list1).not.toBe(list2);
  3404. });
  3405. it('return size for the empty input', () => {
  3406. const accessor = LazyAccessor.createEmpty();
  3407. const size = accessor.getRepeatedSfixed64Size(1);
  3408. expect(size).toEqual(0);
  3409. });
  3410. it('return unpacked values from the input', () => {
  3411. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3412. const list = accessor.getRepeatedSfixed64Iterable(1);
  3413. expectEqualToArray(list, [value1, value2]);
  3414. });
  3415. it('ensure not the same instance returned for unpacked values', () => {
  3416. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3417. const list1 = accessor.getRepeatedSfixed64Iterable(1);
  3418. const list2 = accessor.getRepeatedSfixed64Iterable(1);
  3419. expect(list1).not.toBe(list2);
  3420. });
  3421. it('add single unpacked value', () => {
  3422. const accessor = LazyAccessor.createEmpty();
  3423. accessor.addUnpackedSfixed64Element(1, value1);
  3424. const list1 = accessor.getRepeatedSfixed64Iterable(1);
  3425. accessor.addUnpackedSfixed64Element(1, value2);
  3426. const list2 = accessor.getRepeatedSfixed64Iterable(1);
  3427. expectEqualToArray(list1, [value1]);
  3428. expectEqualToArray(list2, [value1, value2]);
  3429. });
  3430. it('add unpacked values', () => {
  3431. const accessor = LazyAccessor.createEmpty();
  3432. accessor.addUnpackedSfixed64Iterable(1, [value1]);
  3433. const list1 = accessor.getRepeatedSfixed64Iterable(1);
  3434. accessor.addUnpackedSfixed64Iterable(1, [value2]);
  3435. const list2 = accessor.getRepeatedSfixed64Iterable(1);
  3436. expectEqualToArray(list1, [value1]);
  3437. expectEqualToArray(list2, [value1, value2]);
  3438. });
  3439. it('set a single unpacked value', () => {
  3440. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  3441. accessor.setUnpackedSfixed64Element(1, 1, value1);
  3442. const list = accessor.getRepeatedSfixed64Iterable(1);
  3443. expectEqualToArray(list, [value1, value1]);
  3444. });
  3445. it('set unpacked values', () => {
  3446. const accessor = LazyAccessor.createEmpty();
  3447. accessor.setUnpackedSfixed64Iterable(1, [value1]);
  3448. const list = accessor.getRepeatedSfixed64Iterable(1);
  3449. expectEqualToArray(list, [value1]);
  3450. });
  3451. it('encode for adding single unpacked value', () => {
  3452. const accessor = LazyAccessor.createEmpty();
  3453. accessor.addUnpackedSfixed64Element(1, value1);
  3454. accessor.addUnpackedSfixed64Element(1, value2);
  3455. const serialized = accessor.serialize();
  3456. expect(serialized).toEqual(unpackedValue1Value2);
  3457. });
  3458. it('encode for adding unpacked values', () => {
  3459. const accessor = LazyAccessor.createEmpty();
  3460. accessor.addUnpackedSfixed64Iterable(1, [value1]);
  3461. accessor.addUnpackedSfixed64Iterable(1, [value2]);
  3462. const serialized = accessor.serialize();
  3463. expect(serialized).toEqual(unpackedValue1Value2);
  3464. });
  3465. it('encode for setting single unpacked value', () => {
  3466. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3467. accessor.setUnpackedSfixed64Element(1, 0, value2);
  3468. accessor.setUnpackedSfixed64Element(1, 1, value1);
  3469. const serialized = accessor.serialize();
  3470. expect(serialized).toEqual(unpackedValue2Value1);
  3471. });
  3472. it('encode for setting unpacked values', () => {
  3473. const accessor = LazyAccessor.createEmpty();
  3474. accessor.setUnpackedSfixed64Iterable(1, [value1, value2]);
  3475. const serialized = accessor.serialize();
  3476. expect(serialized).toEqual(unpackedValue1Value2);
  3477. });
  3478. it('return packed values from the input', () => {
  3479. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  3480. const list = accessor.getRepeatedSfixed64Iterable(1);
  3481. expectEqualToArray(list, [value1, value2]);
  3482. });
  3483. it('ensure not the same instance returned for packed values', () => {
  3484. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  3485. const list1 = accessor.getRepeatedSfixed64Iterable(1);
  3486. const list2 = accessor.getRepeatedSfixed64Iterable(1);
  3487. expect(list1).not.toBe(list2);
  3488. });
  3489. it('add single packed value', () => {
  3490. const accessor = LazyAccessor.createEmpty();
  3491. accessor.addPackedSfixed64Element(1, value1);
  3492. const list1 = accessor.getRepeatedSfixed64Iterable(1);
  3493. accessor.addPackedSfixed64Element(1, value2);
  3494. const list2 = accessor.getRepeatedSfixed64Iterable(1);
  3495. expectEqualToArray(list1, [value1]);
  3496. expectEqualToArray(list2, [value1, value2]);
  3497. });
  3498. it('add packed values', () => {
  3499. const accessor = LazyAccessor.createEmpty();
  3500. accessor.addPackedSfixed64Iterable(1, [value1]);
  3501. const list1 = accessor.getRepeatedSfixed64Iterable(1);
  3502. accessor.addPackedSfixed64Iterable(1, [value2]);
  3503. const list2 = accessor.getRepeatedSfixed64Iterable(1);
  3504. expectEqualToArray(list1, [value1]);
  3505. expectEqualToArray(list2, [value1, value2]);
  3506. });
  3507. it('set a single packed value', () => {
  3508. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3509. accessor.setPackedSfixed64Element(1, 1, value1);
  3510. const list = accessor.getRepeatedSfixed64Iterable(1);
  3511. expectEqualToArray(list, [value1, value1]);
  3512. });
  3513. it('set packed values', () => {
  3514. const accessor = LazyAccessor.createEmpty();
  3515. accessor.setPackedSfixed64Iterable(1, [value1]);
  3516. const list1 = accessor.getRepeatedSfixed64Iterable(1);
  3517. accessor.setPackedSfixed64Iterable(1, [value2]);
  3518. const list2 = accessor.getRepeatedSfixed64Iterable(1);
  3519. expectEqualToArray(list1, [value1]);
  3520. expectEqualToArray(list2, [value2]);
  3521. });
  3522. it('encode for adding single packed value', () => {
  3523. const accessor = LazyAccessor.createEmpty();
  3524. accessor.addPackedSfixed64Element(1, value1);
  3525. accessor.addPackedSfixed64Element(1, value2);
  3526. const serialized = accessor.serialize();
  3527. expect(serialized).toEqual(packedValue1Value2);
  3528. });
  3529. it('encode for adding packed values', () => {
  3530. const accessor = LazyAccessor.createEmpty();
  3531. accessor.addPackedSfixed64Iterable(1, [value1, value2]);
  3532. const serialized = accessor.serialize();
  3533. expect(serialized).toEqual(packedValue1Value2);
  3534. });
  3535. it('encode for setting single packed value', () => {
  3536. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3537. accessor.setPackedSfixed64Element(1, 0, value2);
  3538. accessor.setPackedSfixed64Element(1, 1, value1);
  3539. const serialized = accessor.serialize();
  3540. expect(serialized).toEqual(packedValue2Value1);
  3541. });
  3542. it('encode for setting packed values', () => {
  3543. const accessor = LazyAccessor.createEmpty();
  3544. accessor.setPackedSfixed64Iterable(1, [value1, value2]);
  3545. const serialized = accessor.serialize();
  3546. expect(serialized).toEqual(packedValue1Value2);
  3547. });
  3548. it('return combined values from the input', () => {
  3549. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  3550. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value1
  3551. 0x0A, 0x10, // tag
  3552. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value1
  3553. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // value2
  3554. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // value2
  3555. ));
  3556. const list = accessor.getRepeatedSfixed64Iterable(1);
  3557. expectEqualToArray(list, [value1, value1, value2, value2]);
  3558. });
  3559. it('return the repeated field element from the input', () => {
  3560. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3561. const result1 = accessor.getRepeatedSfixed64Element(
  3562. /* fieldNumber= */ 1, /* index= */ 0);
  3563. const result2 = accessor.getRepeatedSfixed64Element(
  3564. /* fieldNumber= */ 1, /* index= */ 1);
  3565. expect(result1).toEqual(value1);
  3566. expect(result2).toEqual(value2);
  3567. });
  3568. it('return the size from the input', () => {
  3569. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3570. const size = accessor.getRepeatedSfixed64Size(1);
  3571. expect(size).toEqual(2);
  3572. });
  3573. it('fail when getting unpacked sfixed64 value with other wire types', () => {
  3574. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  3575. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  3576. if (CHECK_CRITICAL_STATE) {
  3577. expect(() => {
  3578. accessor.getRepeatedSfixed64Iterable(1);
  3579. }).toThrowError('Expected wire type: 1 but found: 0');
  3580. } else {
  3581. // Note in unchecked mode we produce invalid output for invalid inputs.
  3582. // This test just documents our behavior in those cases.
  3583. // These values might change at any point and are not considered
  3584. // what the implementation should be doing here.
  3585. expectQualifiedIterable(
  3586. accessor.getRepeatedSfixed64Iterable(1),
  3587. (value) => value instanceof Int64);
  3588. }
  3589. });
  3590. it('fail when adding unpacked sfixed64 values with null value', () => {
  3591. const accessor = LazyAccessor.createEmpty();
  3592. const fakeSfixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  3593. if (CHECK_CRITICAL_STATE) {
  3594. expect(() => accessor.addUnpackedSfixed64Iterable(1, [fakeSfixed64]))
  3595. .toThrowError('Must be Int64 instance, but got: null');
  3596. } else {
  3597. // Note in unchecked mode we produce invalid output for invalid inputs.
  3598. // This test just documents our behavior in those cases.
  3599. // These values might change at any point and are not considered
  3600. // what the implementation should be doing here.
  3601. accessor.addUnpackedSfixed64Iterable(1, [fakeSfixed64]);
  3602. expectQualifiedIterable(accessor.getRepeatedSfixed64Iterable(1));
  3603. }
  3604. });
  3605. it('fail when adding single unpacked sfixed64 value with null value', () => {
  3606. const accessor = LazyAccessor.createEmpty();
  3607. const fakeSfixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  3608. if (CHECK_CRITICAL_STATE) {
  3609. expect(() => accessor.addUnpackedSfixed64Element(1, fakeSfixed64))
  3610. .toThrowError('Must be Int64 instance, but got: null');
  3611. } else {
  3612. // Note in unchecked mode we produce invalid output for invalid inputs.
  3613. // This test just documents our behavior in those cases.
  3614. // These values might change at any point and are not considered
  3615. // what the implementation should be doing here.
  3616. accessor.addUnpackedSfixed64Element(1, fakeSfixed64);
  3617. expectQualifiedIterable(accessor.getRepeatedSfixed64Iterable(1));
  3618. }
  3619. });
  3620. it('fail when setting unpacked sfixed64 values with null value', () => {
  3621. const accessor = LazyAccessor.createEmpty();
  3622. const fakeSfixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  3623. if (CHECK_CRITICAL_STATE) {
  3624. expect(() => accessor.setUnpackedSfixed64Iterable(1, [fakeSfixed64]))
  3625. .toThrowError('Must be Int64 instance, but got: null');
  3626. } else {
  3627. // Note in unchecked mode we produce invalid output for invalid inputs.
  3628. // This test just documents our behavior in those cases.
  3629. // These values might change at any point and are not considered
  3630. // what the implementation should be doing here.
  3631. accessor.setUnpackedSfixed64Iterable(1, [fakeSfixed64]);
  3632. expectQualifiedIterable(accessor.getRepeatedSfixed64Iterable(1));
  3633. }
  3634. });
  3635. it('fail when setting single unpacked sfixed64 value with null value', () => {
  3636. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  3637. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  3638. const fakeSfixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  3639. if (CHECK_CRITICAL_STATE) {
  3640. expect(() => accessor.setUnpackedSfixed64Element(1, 0, fakeSfixed64))
  3641. .toThrowError('Must be Int64 instance, but got: null');
  3642. } else {
  3643. // Note in unchecked mode we produce invalid output for invalid inputs.
  3644. // This test just documents our behavior in those cases.
  3645. // These values might change at any point and are not considered
  3646. // what the implementation should be doing here.
  3647. accessor.setUnpackedSfixed64Element(1, 0, fakeSfixed64);
  3648. expectQualifiedIterable(accessor.getRepeatedSfixed64Iterable(1));
  3649. }
  3650. });
  3651. it('fail when adding packed sfixed64 values with null value', () => {
  3652. const accessor = LazyAccessor.createEmpty();
  3653. const fakeSfixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  3654. if (CHECK_CRITICAL_STATE) {
  3655. expect(() => accessor.addPackedSfixed64Iterable(1, [fakeSfixed64]))
  3656. .toThrowError('Must be Int64 instance, but got: null');
  3657. } else {
  3658. // Note in unchecked mode we produce invalid output for invalid inputs.
  3659. // This test just documents our behavior in those cases.
  3660. // These values might change at any point and are not considered
  3661. // what the implementation should be doing here.
  3662. accessor.addPackedSfixed64Iterable(1, [fakeSfixed64]);
  3663. expectQualifiedIterable(accessor.getRepeatedSfixed64Iterable(1));
  3664. }
  3665. });
  3666. it('fail when adding single packed sfixed64 value with null value', () => {
  3667. const accessor = LazyAccessor.createEmpty();
  3668. const fakeSfixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  3669. if (CHECK_CRITICAL_STATE) {
  3670. expect(() => accessor.addPackedSfixed64Element(1, fakeSfixed64))
  3671. .toThrowError('Must be Int64 instance, but got: null');
  3672. } else {
  3673. // Note in unchecked mode we produce invalid output for invalid inputs.
  3674. // This test just documents our behavior in those cases.
  3675. // These values might change at any point and are not considered
  3676. // what the implementation should be doing here.
  3677. accessor.addPackedSfixed64Element(1, fakeSfixed64);
  3678. expectQualifiedIterable(accessor.getRepeatedSfixed64Iterable(1));
  3679. }
  3680. });
  3681. it('fail when setting packed sfixed64 values with null value', () => {
  3682. const accessor = LazyAccessor.createEmpty();
  3683. const fakeSfixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  3684. if (CHECK_CRITICAL_STATE) {
  3685. expect(() => accessor.setPackedSfixed64Iterable(1, [fakeSfixed64]))
  3686. .toThrowError('Must be Int64 instance, but got: null');
  3687. } else {
  3688. // Note in unchecked mode we produce invalid output for invalid inputs.
  3689. // This test just documents our behavior in those cases.
  3690. // These values might change at any point and are not considered
  3691. // what the implementation should be doing here.
  3692. accessor.setPackedSfixed64Iterable(1, [fakeSfixed64]);
  3693. expectQualifiedIterable(accessor.getRepeatedSfixed64Iterable(1));
  3694. }
  3695. });
  3696. it('fail when setting single packed sfixed64 value with null value', () => {
  3697. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  3698. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  3699. const fakeSfixed64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  3700. if (CHECK_CRITICAL_STATE) {
  3701. expect(() => accessor.setPackedSfixed64Element(1, 0, fakeSfixed64))
  3702. .toThrowError('Must be Int64 instance, but got: null');
  3703. } else {
  3704. // Note in unchecked mode we produce invalid output for invalid inputs.
  3705. // This test just documents our behavior in those cases.
  3706. // These values might change at any point and are not considered
  3707. // what the implementation should be doing here.
  3708. accessor.setPackedSfixed64Element(1, 0, fakeSfixed64);
  3709. expectQualifiedIterable(accessor.getRepeatedSfixed64Iterable(1));
  3710. }
  3711. });
  3712. it('fail when setting single unpacked with out-of-bound index', () => {
  3713. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  3714. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  3715. if (CHECK_CRITICAL_STATE) {
  3716. expect(() => accessor.setUnpackedSfixed64Element(1, 1, Int64.fromInt(1)))
  3717. .toThrowError('Index out of bounds: index: 1 size: 1');
  3718. } else {
  3719. // Note in unchecked mode we produce invalid output for invalid inputs.
  3720. // This test just documents our behavior in those cases.
  3721. // These values might change at any point and are not considered
  3722. // what the implementation should be doing here.
  3723. accessor.setUnpackedSfixed64Element(1, 1, Int64.fromInt(1));
  3724. expectQualifiedIterable(
  3725. accessor.getRepeatedSfixed64Iterable(1),
  3726. (value) => value instanceof Int64);
  3727. }
  3728. });
  3729. it('fail when setting single packed with out-of-bound index', () => {
  3730. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  3731. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  3732. if (CHECK_CRITICAL_STATE) {
  3733. expect(() => accessor.setPackedSfixed64Element(1, 1, Int64.fromInt(1)))
  3734. .toThrowError('Index out of bounds: index: 1 size: 1');
  3735. } else {
  3736. // Note in unchecked mode we produce invalid output for invalid inputs.
  3737. // This test just documents our behavior in those cases.
  3738. // These values might change at any point and are not considered
  3739. // what the implementation should be doing here.
  3740. accessor.setPackedSfixed64Element(1, 1, Int64.fromInt(1));
  3741. expectQualifiedIterable(
  3742. accessor.getRepeatedSfixed64Iterable(1),
  3743. (value) => value instanceof Int64);
  3744. }
  3745. });
  3746. it('fail when getting element with out-of-range index', () => {
  3747. const accessor = LazyAccessor.createEmpty();
  3748. if (CHECK_CRITICAL_STATE) {
  3749. expect(() => {
  3750. accessor.getRepeatedSfixed64Element(
  3751. /* fieldNumber= */ 1, /* index= */ 0);
  3752. }).toThrowError('Index out of bounds: index: 0 size: 0');
  3753. } else {
  3754. // Note in unchecked mode we produce invalid output for invalid inputs.
  3755. // This test just documents our behavior in those cases.
  3756. // These values might change at any point and are not considered
  3757. // what the implementation should be doing here.
  3758. expect(accessor.getRepeatedSfixed64Element(
  3759. /* fieldNumber= */ 1, /* index= */ 0))
  3760. .toBe(undefined);
  3761. }
  3762. });
  3763. });
  3764. describe('LazyAccessor for repeated sint32 does', () => {
  3765. const value1 = -1;
  3766. const value2 = 0;
  3767. const unpackedValue1Value2 = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  3768. const unpackedValue2Value1 = createArrayBuffer(0x08, 0x00, 0x08, 0x01);
  3769. const packedValue1Value2 = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  3770. const packedValue2Value1 = createArrayBuffer(0x0A, 0x02, 0x00, 0x01);
  3771. it('return empty array for the empty input', () => {
  3772. const accessor = LazyAccessor.createEmpty();
  3773. const list = accessor.getRepeatedSint32Iterable(1);
  3774. expectEqualToArray(list, []);
  3775. });
  3776. it('ensure not the same instance returned for the empty input', () => {
  3777. const accessor = LazyAccessor.createEmpty();
  3778. const list1 = accessor.getRepeatedSint32Iterable(1);
  3779. const list2 = accessor.getRepeatedSint32Iterable(1);
  3780. expect(list1).not.toBe(list2);
  3781. });
  3782. it('return size for the empty input', () => {
  3783. const accessor = LazyAccessor.createEmpty();
  3784. const size = accessor.getRepeatedSint32Size(1);
  3785. expect(size).toEqual(0);
  3786. });
  3787. it('return unpacked values from the input', () => {
  3788. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3789. const list = accessor.getRepeatedSint32Iterable(1);
  3790. expectEqualToArray(list, [value1, value2]);
  3791. });
  3792. it('ensure not the same instance returned for unpacked values', () => {
  3793. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3794. const list1 = accessor.getRepeatedSint32Iterable(1);
  3795. const list2 = accessor.getRepeatedSint32Iterable(1);
  3796. expect(list1).not.toBe(list2);
  3797. });
  3798. it('add single unpacked value', () => {
  3799. const accessor = LazyAccessor.createEmpty();
  3800. accessor.addUnpackedSint32Element(1, value1);
  3801. const list1 = accessor.getRepeatedSint32Iterable(1);
  3802. accessor.addUnpackedSint32Element(1, value2);
  3803. const list2 = accessor.getRepeatedSint32Iterable(1);
  3804. expectEqualToArray(list1, [value1]);
  3805. expectEqualToArray(list2, [value1, value2]);
  3806. });
  3807. it('add unpacked values', () => {
  3808. const accessor = LazyAccessor.createEmpty();
  3809. accessor.addUnpackedSint32Iterable(1, [value1]);
  3810. const list1 = accessor.getRepeatedSint32Iterable(1);
  3811. accessor.addUnpackedSint32Iterable(1, [value2]);
  3812. const list2 = accessor.getRepeatedSint32Iterable(1);
  3813. expectEqualToArray(list1, [value1]);
  3814. expectEqualToArray(list2, [value1, value2]);
  3815. });
  3816. it('set a single unpacked value', () => {
  3817. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  3818. accessor.setUnpackedSint32Element(1, 1, value1);
  3819. const list = accessor.getRepeatedSint32Iterable(1);
  3820. expectEqualToArray(list, [value1, value1]);
  3821. });
  3822. it('set unpacked values', () => {
  3823. const accessor = LazyAccessor.createEmpty();
  3824. accessor.setUnpackedSint32Iterable(1, [value1]);
  3825. const list = accessor.getRepeatedSint32Iterable(1);
  3826. expectEqualToArray(list, [value1]);
  3827. });
  3828. it('encode for adding single unpacked value', () => {
  3829. const accessor = LazyAccessor.createEmpty();
  3830. accessor.addUnpackedSint32Element(1, value1);
  3831. accessor.addUnpackedSint32Element(1, value2);
  3832. const serialized = accessor.serialize();
  3833. expect(serialized).toEqual(unpackedValue1Value2);
  3834. });
  3835. it('encode for adding unpacked values', () => {
  3836. const accessor = LazyAccessor.createEmpty();
  3837. accessor.addUnpackedSint32Iterable(1, [value1]);
  3838. accessor.addUnpackedSint32Iterable(1, [value2]);
  3839. const serialized = accessor.serialize();
  3840. expect(serialized).toEqual(unpackedValue1Value2);
  3841. });
  3842. it('encode for setting single unpacked value', () => {
  3843. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3844. accessor.setUnpackedSint32Element(1, 0, value2);
  3845. accessor.setUnpackedSint32Element(1, 1, value1);
  3846. const serialized = accessor.serialize();
  3847. expect(serialized).toEqual(unpackedValue2Value1);
  3848. });
  3849. it('encode for setting unpacked values', () => {
  3850. const accessor = LazyAccessor.createEmpty();
  3851. accessor.setUnpackedSint32Iterable(1, [value1, value2]);
  3852. const serialized = accessor.serialize();
  3853. expect(serialized).toEqual(unpackedValue1Value2);
  3854. });
  3855. it('return packed values from the input', () => {
  3856. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  3857. const list = accessor.getRepeatedSint32Iterable(1);
  3858. expectEqualToArray(list, [value1, value2]);
  3859. });
  3860. it('ensure not the same instance returned for packed values', () => {
  3861. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  3862. const list1 = accessor.getRepeatedSint32Iterable(1);
  3863. const list2 = accessor.getRepeatedSint32Iterable(1);
  3864. expect(list1).not.toBe(list2);
  3865. });
  3866. it('add single packed value', () => {
  3867. const accessor = LazyAccessor.createEmpty();
  3868. accessor.addPackedSint32Element(1, value1);
  3869. const list1 = accessor.getRepeatedSint32Iterable(1);
  3870. accessor.addPackedSint32Element(1, value2);
  3871. const list2 = accessor.getRepeatedSint32Iterable(1);
  3872. expectEqualToArray(list1, [value1]);
  3873. expectEqualToArray(list2, [value1, value2]);
  3874. });
  3875. it('add packed values', () => {
  3876. const accessor = LazyAccessor.createEmpty();
  3877. accessor.addPackedSint32Iterable(1, [value1]);
  3878. const list1 = accessor.getRepeatedSint32Iterable(1);
  3879. accessor.addPackedSint32Iterable(1, [value2]);
  3880. const list2 = accessor.getRepeatedSint32Iterable(1);
  3881. expectEqualToArray(list1, [value1]);
  3882. expectEqualToArray(list2, [value1, value2]);
  3883. });
  3884. it('set a single packed value', () => {
  3885. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3886. accessor.setPackedSint32Element(1, 1, value1);
  3887. const list = accessor.getRepeatedSint32Iterable(1);
  3888. expectEqualToArray(list, [value1, value1]);
  3889. });
  3890. it('set packed values', () => {
  3891. const accessor = LazyAccessor.createEmpty();
  3892. accessor.setPackedSint32Iterable(1, [value1]);
  3893. const list1 = accessor.getRepeatedSint32Iterable(1);
  3894. accessor.setPackedSint32Iterable(1, [value2]);
  3895. const list2 = accessor.getRepeatedSint32Iterable(1);
  3896. expectEqualToArray(list1, [value1]);
  3897. expectEqualToArray(list2, [value2]);
  3898. });
  3899. it('encode for adding single packed value', () => {
  3900. const accessor = LazyAccessor.createEmpty();
  3901. accessor.addPackedSint32Element(1, value1);
  3902. accessor.addPackedSint32Element(1, value2);
  3903. const serialized = accessor.serialize();
  3904. expect(serialized).toEqual(packedValue1Value2);
  3905. });
  3906. it('encode for adding packed values', () => {
  3907. const accessor = LazyAccessor.createEmpty();
  3908. accessor.addPackedSint32Iterable(1, [value1, value2]);
  3909. const serialized = accessor.serialize();
  3910. expect(serialized).toEqual(packedValue1Value2);
  3911. });
  3912. it('encode for setting single packed value', () => {
  3913. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3914. accessor.setPackedSint32Element(1, 0, value2);
  3915. accessor.setPackedSint32Element(1, 1, value1);
  3916. const serialized = accessor.serialize();
  3917. expect(serialized).toEqual(packedValue2Value1);
  3918. });
  3919. it('encode for setting packed values', () => {
  3920. const accessor = LazyAccessor.createEmpty();
  3921. accessor.setPackedSint32Iterable(1, [value1, value2]);
  3922. const serialized = accessor.serialize();
  3923. expect(serialized).toEqual(packedValue1Value2);
  3924. });
  3925. it('return combined values from the input', () => {
  3926. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  3927. 0x08,
  3928. 0x01, // unpacked value1
  3929. 0x0A,
  3930. 0x02,
  3931. 0x01,
  3932. 0x00, // packed value1 and value2
  3933. 0x08,
  3934. 0x00, // unpacked value2
  3935. ));
  3936. const list = accessor.getRepeatedSint32Iterable(1);
  3937. expectEqualToArray(list, [value1, value1, value2, value2]);
  3938. });
  3939. it('return the repeated field element from the input', () => {
  3940. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3941. const result1 = accessor.getRepeatedSint32Element(
  3942. /* fieldNumber= */ 1, /* index= */ 0);
  3943. const result2 = accessor.getRepeatedSint32Element(
  3944. /* fieldNumber= */ 1, /* index= */ 1);
  3945. expect(result1).toEqual(value1);
  3946. expect(result2).toEqual(value2);
  3947. });
  3948. it('return the size from the input', () => {
  3949. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  3950. const size = accessor.getRepeatedSint32Size(1);
  3951. expect(size).toEqual(2);
  3952. });
  3953. it('fail when getting unpacked sint32 value with other wire types', () => {
  3954. const accessor = LazyAccessor.fromArrayBuffer(
  3955. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  3956. if (CHECK_CRITICAL_STATE) {
  3957. expect(() => {
  3958. accessor.getRepeatedSint32Iterable(1);
  3959. }).toThrowError('Expected wire type: 0 but found: 5');
  3960. } else {
  3961. // Note in unchecked mode we produce invalid output for invalid inputs.
  3962. // This test just documents our behavior in those cases.
  3963. // These values might change at any point and are not considered
  3964. // what the implementation should be doing here.
  3965. expectQualifiedIterable(
  3966. accessor.getRepeatedSint32Iterable(1),
  3967. (value) => Number.isInteger(value));
  3968. }
  3969. });
  3970. it('fail when adding unpacked sint32 values with null value', () => {
  3971. const accessor = LazyAccessor.createEmpty();
  3972. const fakeSint32 = /** @type {number} */ (/** @type {*} */ (null));
  3973. if (CHECK_CRITICAL_STATE) {
  3974. expect(() => accessor.addUnpackedSint32Iterable(1, [fakeSint32]))
  3975. .toThrowError('Must be a number, but got: null');
  3976. } else {
  3977. // Note in unchecked mode we produce invalid output for invalid inputs.
  3978. // This test just documents our behavior in those cases.
  3979. // These values might change at any point and are not considered
  3980. // what the implementation should be doing here.
  3981. accessor.addUnpackedSint32Iterable(1, [fakeSint32]);
  3982. expectQualifiedIterable(accessor.getRepeatedSint32Iterable(1));
  3983. }
  3984. });
  3985. it('fail when adding single unpacked sint32 value with null value', () => {
  3986. const accessor = LazyAccessor.createEmpty();
  3987. const fakeSint32 = /** @type {number} */ (/** @type {*} */ (null));
  3988. if (CHECK_CRITICAL_STATE) {
  3989. expect(() => accessor.addUnpackedSint32Element(1, fakeSint32))
  3990. .toThrowError('Must be a number, but got: null');
  3991. } else {
  3992. // Note in unchecked mode we produce invalid output for invalid inputs.
  3993. // This test just documents our behavior in those cases.
  3994. // These values might change at any point and are not considered
  3995. // what the implementation should be doing here.
  3996. accessor.addUnpackedSint32Element(1, fakeSint32);
  3997. expectQualifiedIterable(accessor.getRepeatedSint32Iterable(1));
  3998. }
  3999. });
  4000. it('fail when setting unpacked sint32 values with null value', () => {
  4001. const accessor = LazyAccessor.createEmpty();
  4002. const fakeSint32 = /** @type {number} */ (/** @type {*} */ (null));
  4003. if (CHECK_CRITICAL_STATE) {
  4004. expect(() => accessor.setUnpackedSint32Iterable(1, [fakeSint32]))
  4005. .toThrowError('Must be a number, but got: null');
  4006. } else {
  4007. // Note in unchecked mode we produce invalid output for invalid inputs.
  4008. // This test just documents our behavior in those cases.
  4009. // These values might change at any point and are not considered
  4010. // what the implementation should be doing here.
  4011. accessor.setUnpackedSint32Iterable(1, [fakeSint32]);
  4012. expectQualifiedIterable(accessor.getRepeatedSint32Iterable(1));
  4013. }
  4014. });
  4015. it('fail when setting single unpacked sint32 value with null value', () => {
  4016. const accessor = LazyAccessor.fromArrayBuffer(
  4017. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  4018. const fakeSint32 = /** @type {number} */ (/** @type {*} */ (null));
  4019. if (CHECK_CRITICAL_STATE) {
  4020. expect(() => accessor.setUnpackedSint32Element(1, 0, fakeSint32))
  4021. .toThrowError('Must be a number, but got: null');
  4022. } else {
  4023. // Note in unchecked mode we produce invalid output for invalid inputs.
  4024. // This test just documents our behavior in those cases.
  4025. // These values might change at any point and are not considered
  4026. // what the implementation should be doing here.
  4027. accessor.setUnpackedSint32Element(1, 0, fakeSint32);
  4028. expectQualifiedIterable(
  4029. accessor.getRepeatedSint32Iterable(1),
  4030. );
  4031. }
  4032. });
  4033. it('fail when adding packed sint32 values with null value', () => {
  4034. const accessor = LazyAccessor.createEmpty();
  4035. const fakeSint32 = /** @type {number} */ (/** @type {*} */ (null));
  4036. if (CHECK_CRITICAL_STATE) {
  4037. expect(() => accessor.addPackedSint32Iterable(1, [fakeSint32]))
  4038. .toThrowError('Must be a number, but got: null');
  4039. } else {
  4040. // Note in unchecked mode we produce invalid output for invalid inputs.
  4041. // This test just documents our behavior in those cases.
  4042. // These values might change at any point and are not considered
  4043. // what the implementation should be doing here.
  4044. accessor.addPackedSint32Iterable(1, [fakeSint32]);
  4045. expectQualifiedIterable(accessor.getRepeatedSint32Iterable(1));
  4046. }
  4047. });
  4048. it('fail when adding single packed sint32 value with null value', () => {
  4049. const accessor = LazyAccessor.createEmpty();
  4050. const fakeSint32 = /** @type {number} */ (/** @type {*} */ (null));
  4051. if (CHECK_CRITICAL_STATE) {
  4052. expect(() => accessor.addPackedSint32Element(1, fakeSint32))
  4053. .toThrowError('Must be a number, but got: null');
  4054. } else {
  4055. // Note in unchecked mode we produce invalid output for invalid inputs.
  4056. // This test just documents our behavior in those cases.
  4057. // These values might change at any point and are not considered
  4058. // what the implementation should be doing here.
  4059. accessor.addPackedSint32Element(1, fakeSint32);
  4060. expectQualifiedIterable(accessor.getRepeatedSint32Iterable(1));
  4061. }
  4062. });
  4063. it('fail when setting packed sint32 values with null value', () => {
  4064. const accessor = LazyAccessor.createEmpty();
  4065. const fakeSint32 = /** @type {number} */ (/** @type {*} */ (null));
  4066. if (CHECK_CRITICAL_STATE) {
  4067. expect(() => accessor.setPackedSint32Iterable(1, [fakeSint32]))
  4068. .toThrowError('Must be a number, but got: null');
  4069. } else {
  4070. // Note in unchecked mode we produce invalid output for invalid inputs.
  4071. // This test just documents our behavior in those cases.
  4072. // These values might change at any point and are not considered
  4073. // what the implementation should be doing here.
  4074. accessor.setPackedSint32Iterable(1, [fakeSint32]);
  4075. expectQualifiedIterable(accessor.getRepeatedSint32Iterable(1));
  4076. }
  4077. });
  4078. it('fail when setting single packed sint32 value with null value', () => {
  4079. const accessor =
  4080. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  4081. const fakeSint32 = /** @type {number} */ (/** @type {*} */ (null));
  4082. if (CHECK_CRITICAL_STATE) {
  4083. expect(() => accessor.setPackedSint32Element(1, 0, fakeSint32))
  4084. .toThrowError('Must be a number, but got: null');
  4085. } else {
  4086. // Note in unchecked mode we produce invalid output for invalid inputs.
  4087. // This test just documents our behavior in those cases.
  4088. // These values might change at any point and are not considered
  4089. // what the implementation should be doing here.
  4090. accessor.setPackedSint32Element(1, 0, fakeSint32);
  4091. expectQualifiedIterable(accessor.getRepeatedSint32Iterable(1));
  4092. }
  4093. });
  4094. it('fail when setting single unpacked with out-of-bound index', () => {
  4095. const accessor =
  4096. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x00));
  4097. if (CHECK_CRITICAL_STATE) {
  4098. expect(() => accessor.setUnpackedSint32Element(1, 1, 1))
  4099. .toThrowError('Index out of bounds: index: 1 size: 1');
  4100. } else {
  4101. // Note in unchecked mode we produce invalid output for invalid inputs.
  4102. // This test just documents our behavior in those cases.
  4103. // These values might change at any point and are not considered
  4104. // what the implementation should be doing here.
  4105. accessor.setUnpackedSint32Element(1, 1, 1);
  4106. expectQualifiedIterable(
  4107. accessor.getRepeatedSint32Iterable(1),
  4108. (value) => Number.isInteger(value));
  4109. }
  4110. });
  4111. it('fail when setting single packed with out-of-bound index', () => {
  4112. const accessor =
  4113. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  4114. if (CHECK_CRITICAL_STATE) {
  4115. expect(() => accessor.setPackedSint32Element(1, 1, 1))
  4116. .toThrowError('Index out of bounds: index: 1 size: 1');
  4117. } else {
  4118. // Note in unchecked mode we produce invalid output for invalid inputs.
  4119. // This test just documents our behavior in those cases.
  4120. // These values might change at any point and are not considered
  4121. // what the implementation should be doing here.
  4122. accessor.setPackedSint32Element(1, 1, 1);
  4123. expectQualifiedIterable(
  4124. accessor.getRepeatedSint32Iterable(1),
  4125. (value) => Number.isInteger(value));
  4126. }
  4127. });
  4128. it('fail when getting element with out-of-range index', () => {
  4129. const accessor = LazyAccessor.createEmpty();
  4130. if (CHECK_CRITICAL_STATE) {
  4131. expect(() => {
  4132. accessor.getRepeatedSint32Element(
  4133. /* fieldNumber= */ 1, /* index= */ 0);
  4134. }).toThrowError('Index out of bounds: index: 0 size: 0');
  4135. } else {
  4136. // Note in unchecked mode we produce invalid output for invalid inputs.
  4137. // This test just documents our behavior in those cases.
  4138. // These values might change at any point and are not considered
  4139. // what the implementation should be doing here.
  4140. expect(accessor.getRepeatedSint32Element(
  4141. /* fieldNumber= */ 1, /* index= */ 0))
  4142. .toBe(undefined);
  4143. }
  4144. });
  4145. });
  4146. describe('LazyAccessor for repeated sint64 does', () => {
  4147. const value1 = Int64.fromInt(-1);
  4148. const value2 = Int64.fromInt(0);
  4149. const unpackedValue1Value2 = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  4150. const unpackedValue2Value1 = createArrayBuffer(0x08, 0x00, 0x08, 0x01);
  4151. const packedValue1Value2 = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  4152. const packedValue2Value1 = createArrayBuffer(0x0A, 0x02, 0x00, 0x01);
  4153. it('return empty array for the empty input', () => {
  4154. const accessor = LazyAccessor.createEmpty();
  4155. const list = accessor.getRepeatedSint64Iterable(1);
  4156. expectEqualToArray(list, []);
  4157. });
  4158. it('ensure not the same instance returned for the empty input', () => {
  4159. const accessor = LazyAccessor.createEmpty();
  4160. const list1 = accessor.getRepeatedSint64Iterable(1);
  4161. const list2 = accessor.getRepeatedSint64Iterable(1);
  4162. expect(list1).not.toBe(list2);
  4163. });
  4164. it('return size for the empty input', () => {
  4165. const accessor = LazyAccessor.createEmpty();
  4166. const size = accessor.getRepeatedSint64Size(1);
  4167. expect(size).toEqual(0);
  4168. });
  4169. it('return unpacked values from the input', () => {
  4170. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4171. const list = accessor.getRepeatedSint64Iterable(1);
  4172. expectEqualToArray(list, [value1, value2]);
  4173. });
  4174. it('ensure not the same instance returned for unpacked values', () => {
  4175. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4176. const list1 = accessor.getRepeatedSint64Iterable(1);
  4177. const list2 = accessor.getRepeatedSint64Iterable(1);
  4178. expect(list1).not.toBe(list2);
  4179. });
  4180. it('add single unpacked value', () => {
  4181. const accessor = LazyAccessor.createEmpty();
  4182. accessor.addUnpackedSint64Element(1, value1);
  4183. const list1 = accessor.getRepeatedSint64Iterable(1);
  4184. accessor.addUnpackedSint64Element(1, value2);
  4185. const list2 = accessor.getRepeatedSint64Iterable(1);
  4186. expectEqualToArray(list1, [value1]);
  4187. expectEqualToArray(list2, [value1, value2]);
  4188. });
  4189. it('add unpacked values', () => {
  4190. const accessor = LazyAccessor.createEmpty();
  4191. accessor.addUnpackedSint64Iterable(1, [value1]);
  4192. const list1 = accessor.getRepeatedSint64Iterable(1);
  4193. accessor.addUnpackedSint64Iterable(1, [value2]);
  4194. const list2 = accessor.getRepeatedSint64Iterable(1);
  4195. expectEqualToArray(list1, [value1]);
  4196. expectEqualToArray(list2, [value1, value2]);
  4197. });
  4198. it('set a single unpacked value', () => {
  4199. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  4200. accessor.setUnpackedSint64Element(1, 1, value1);
  4201. const list = accessor.getRepeatedSint64Iterable(1);
  4202. expectEqualToArray(list, [value1, value1]);
  4203. });
  4204. it('set unpacked values', () => {
  4205. const accessor = LazyAccessor.createEmpty();
  4206. accessor.setUnpackedSint64Iterable(1, [value1]);
  4207. const list = accessor.getRepeatedSint64Iterable(1);
  4208. expectEqualToArray(list, [value1]);
  4209. });
  4210. it('encode for adding single unpacked value', () => {
  4211. const accessor = LazyAccessor.createEmpty();
  4212. accessor.addUnpackedSint64Element(1, value1);
  4213. accessor.addUnpackedSint64Element(1, value2);
  4214. const serialized = accessor.serialize();
  4215. expect(serialized).toEqual(unpackedValue1Value2);
  4216. });
  4217. it('encode for adding unpacked values', () => {
  4218. const accessor = LazyAccessor.createEmpty();
  4219. accessor.addUnpackedSint64Iterable(1, [value1]);
  4220. accessor.addUnpackedSint64Iterable(1, [value2]);
  4221. const serialized = accessor.serialize();
  4222. expect(serialized).toEqual(unpackedValue1Value2);
  4223. });
  4224. it('encode for setting single unpacked value', () => {
  4225. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4226. accessor.setUnpackedSint64Element(1, 0, value2);
  4227. accessor.setUnpackedSint64Element(1, 1, value1);
  4228. const serialized = accessor.serialize();
  4229. expect(serialized).toEqual(unpackedValue2Value1);
  4230. });
  4231. it('encode for setting unpacked values', () => {
  4232. const accessor = LazyAccessor.createEmpty();
  4233. accessor.setUnpackedSint64Iterable(1, [value1, value2]);
  4234. const serialized = accessor.serialize();
  4235. expect(serialized).toEqual(unpackedValue1Value2);
  4236. });
  4237. it('return packed values from the input', () => {
  4238. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  4239. const list = accessor.getRepeatedSint64Iterable(1);
  4240. expectEqualToArray(list, [value1, value2]);
  4241. });
  4242. it('ensure not the same instance returned for packed values', () => {
  4243. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  4244. const list1 = accessor.getRepeatedSint64Iterable(1);
  4245. const list2 = accessor.getRepeatedSint64Iterable(1);
  4246. expect(list1).not.toBe(list2);
  4247. });
  4248. it('add single packed value', () => {
  4249. const accessor = LazyAccessor.createEmpty();
  4250. accessor.addPackedSint64Element(1, value1);
  4251. const list1 = accessor.getRepeatedSint64Iterable(1);
  4252. accessor.addPackedSint64Element(1, value2);
  4253. const list2 = accessor.getRepeatedSint64Iterable(1);
  4254. expectEqualToArray(list1, [value1]);
  4255. expectEqualToArray(list2, [value1, value2]);
  4256. });
  4257. it('add packed values', () => {
  4258. const accessor = LazyAccessor.createEmpty();
  4259. accessor.addPackedSint64Iterable(1, [value1]);
  4260. const list1 = accessor.getRepeatedSint64Iterable(1);
  4261. accessor.addPackedSint64Iterable(1, [value2]);
  4262. const list2 = accessor.getRepeatedSint64Iterable(1);
  4263. expectEqualToArray(list1, [value1]);
  4264. expectEqualToArray(list2, [value1, value2]);
  4265. });
  4266. it('set a single packed value', () => {
  4267. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4268. accessor.setPackedSint64Element(1, 1, value1);
  4269. const list = accessor.getRepeatedSint64Iterable(1);
  4270. expectEqualToArray(list, [value1, value1]);
  4271. });
  4272. it('set packed values', () => {
  4273. const accessor = LazyAccessor.createEmpty();
  4274. accessor.setPackedSint64Iterable(1, [value1]);
  4275. const list1 = accessor.getRepeatedSint64Iterable(1);
  4276. accessor.setPackedSint64Iterable(1, [value2]);
  4277. const list2 = accessor.getRepeatedSint64Iterable(1);
  4278. expectEqualToArray(list1, [value1]);
  4279. expectEqualToArray(list2, [value2]);
  4280. });
  4281. it('encode for adding single packed value', () => {
  4282. const accessor = LazyAccessor.createEmpty();
  4283. accessor.addPackedSint64Element(1, value1);
  4284. accessor.addPackedSint64Element(1, value2);
  4285. const serialized = accessor.serialize();
  4286. expect(serialized).toEqual(packedValue1Value2);
  4287. });
  4288. it('encode for adding packed values', () => {
  4289. const accessor = LazyAccessor.createEmpty();
  4290. accessor.addPackedSint64Iterable(1, [value1, value2]);
  4291. const serialized = accessor.serialize();
  4292. expect(serialized).toEqual(packedValue1Value2);
  4293. });
  4294. it('encode for setting single packed value', () => {
  4295. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4296. accessor.setPackedSint64Element(1, 0, value2);
  4297. accessor.setPackedSint64Element(1, 1, value1);
  4298. const serialized = accessor.serialize();
  4299. expect(serialized).toEqual(packedValue2Value1);
  4300. });
  4301. it('encode for setting packed values', () => {
  4302. const accessor = LazyAccessor.createEmpty();
  4303. accessor.setPackedSint64Iterable(1, [value1, value2]);
  4304. const serialized = accessor.serialize();
  4305. expect(serialized).toEqual(packedValue1Value2);
  4306. });
  4307. it('return combined values from the input', () => {
  4308. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  4309. 0x08,
  4310. 0x01, // unpacked value1
  4311. 0x0A,
  4312. 0x02,
  4313. 0x01,
  4314. 0x00, // packed value1 and value2
  4315. 0x08,
  4316. 0x00, // unpacked value2
  4317. ));
  4318. const list = accessor.getRepeatedSint64Iterable(1);
  4319. expectEqualToArray(list, [value1, value1, value2, value2]);
  4320. });
  4321. it('return the repeated field element from the input', () => {
  4322. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4323. const result1 = accessor.getRepeatedSint64Element(
  4324. /* fieldNumber= */ 1, /* index= */ 0);
  4325. const result2 = accessor.getRepeatedSint64Element(
  4326. /* fieldNumber= */ 1, /* index= */ 1);
  4327. expect(result1).toEqual(value1);
  4328. expect(result2).toEqual(value2);
  4329. });
  4330. it('return the size from the input', () => {
  4331. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4332. const size = accessor.getRepeatedSint64Size(1);
  4333. expect(size).toEqual(2);
  4334. });
  4335. it('fail when getting unpacked sint64 value with other wire types', () => {
  4336. const accessor = LazyAccessor.fromArrayBuffer(
  4337. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  4338. if (CHECK_CRITICAL_STATE) {
  4339. expect(() => {
  4340. accessor.getRepeatedSint64Iterable(1);
  4341. }).toThrowError('Expected wire type: 0 but found: 5');
  4342. } else {
  4343. // Note in unchecked mode we produce invalid output for invalid inputs.
  4344. // This test just documents our behavior in those cases.
  4345. // These values might change at any point and are not considered
  4346. // what the implementation should be doing here.
  4347. expectQualifiedIterable(
  4348. accessor.getRepeatedSint64Iterable(1),
  4349. (value) => value instanceof Int64);
  4350. }
  4351. });
  4352. it('fail when adding unpacked sint64 values with null value', () => {
  4353. const accessor = LazyAccessor.createEmpty();
  4354. const fakeSint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  4355. if (CHECK_CRITICAL_STATE) {
  4356. expect(() => accessor.addUnpackedSint64Iterable(1, [fakeSint64]))
  4357. .toThrowError('Must be Int64 instance, but got: null');
  4358. } else {
  4359. // Note in unchecked mode we produce invalid output for invalid inputs.
  4360. // This test just documents our behavior in those cases.
  4361. // These values might change at any point and are not considered
  4362. // what the implementation should be doing here.
  4363. accessor.addUnpackedSint64Iterable(1, [fakeSint64]);
  4364. expectQualifiedIterable(accessor.getRepeatedSint64Iterable(1));
  4365. }
  4366. });
  4367. it('fail when adding single unpacked sint64 value with null value', () => {
  4368. const accessor = LazyAccessor.createEmpty();
  4369. const fakeSint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  4370. if (CHECK_CRITICAL_STATE) {
  4371. expect(() => accessor.addUnpackedSint64Element(1, fakeSint64))
  4372. .toThrowError('Must be Int64 instance, but got: null');
  4373. } else {
  4374. // Note in unchecked mode we produce invalid output for invalid inputs.
  4375. // This test just documents our behavior in those cases.
  4376. // These values might change at any point and are not considered
  4377. // what the implementation should be doing here.
  4378. accessor.addUnpackedSint64Element(1, fakeSint64);
  4379. expectQualifiedIterable(accessor.getRepeatedSint64Iterable(1));
  4380. }
  4381. });
  4382. it('fail when setting unpacked sint64 values with null value', () => {
  4383. const accessor = LazyAccessor.createEmpty();
  4384. const fakeSint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  4385. if (CHECK_CRITICAL_STATE) {
  4386. expect(() => accessor.setUnpackedSint64Iterable(1, [fakeSint64]))
  4387. .toThrowError('Must be Int64 instance, but got: null');
  4388. } else {
  4389. // Note in unchecked mode we produce invalid output for invalid inputs.
  4390. // This test just documents our behavior in those cases.
  4391. // These values might change at any point and are not considered
  4392. // what the implementation should be doing here.
  4393. accessor.setUnpackedSint64Iterable(1, [fakeSint64]);
  4394. expectQualifiedIterable(accessor.getRepeatedSint64Iterable(1));
  4395. }
  4396. });
  4397. it('fail when setting single unpacked sint64 value with null value', () => {
  4398. const accessor = LazyAccessor.fromArrayBuffer(
  4399. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  4400. const fakeSint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  4401. if (CHECK_CRITICAL_STATE) {
  4402. expect(() => accessor.setUnpackedSint64Element(1, 0, fakeSint64))
  4403. .toThrowError('Must be Int64 instance, but got: null');
  4404. } else {
  4405. // Note in unchecked mode we produce invalid output for invalid inputs.
  4406. // This test just documents our behavior in those cases.
  4407. // These values might change at any point and are not considered
  4408. // what the implementation should be doing here.
  4409. accessor.setUnpackedSint64Element(1, 0, fakeSint64);
  4410. expectQualifiedIterable(accessor.getRepeatedSint64Iterable(1));
  4411. }
  4412. });
  4413. it('fail when adding packed sint64 values with null value', () => {
  4414. const accessor = LazyAccessor.createEmpty();
  4415. const fakeSint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  4416. if (CHECK_CRITICAL_STATE) {
  4417. expect(() => accessor.addPackedSint64Iterable(1, [fakeSint64]))
  4418. .toThrowError('Must be Int64 instance, but got: null');
  4419. } else {
  4420. // Note in unchecked mode we produce invalid output for invalid inputs.
  4421. // This test just documents our behavior in those cases.
  4422. // These values might change at any point and are not considered
  4423. // what the implementation should be doing here.
  4424. accessor.addPackedSint64Iterable(1, [fakeSint64]);
  4425. expectQualifiedIterable(accessor.getRepeatedSint64Iterable(1));
  4426. }
  4427. });
  4428. it('fail when adding single packed sint64 value with null value', () => {
  4429. const accessor = LazyAccessor.createEmpty();
  4430. const fakeSint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  4431. if (CHECK_CRITICAL_STATE) {
  4432. expect(() => accessor.addPackedSint64Element(1, fakeSint64))
  4433. .toThrowError('Must be Int64 instance, but got: null');
  4434. } else {
  4435. // Note in unchecked mode we produce invalid output for invalid inputs.
  4436. // This test just documents our behavior in those cases.
  4437. // These values might change at any point and are not considered
  4438. // what the implementation should be doing here.
  4439. accessor.addPackedSint64Element(1, fakeSint64);
  4440. expectQualifiedIterable(accessor.getRepeatedSint64Iterable(1));
  4441. }
  4442. });
  4443. it('fail when setting packed sint64 values with null value', () => {
  4444. const accessor = LazyAccessor.createEmpty();
  4445. const fakeSint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  4446. if (CHECK_CRITICAL_STATE) {
  4447. expect(() => accessor.setPackedSint64Iterable(1, [fakeSint64]))
  4448. .toThrowError('Must be Int64 instance, but got: null');
  4449. } else {
  4450. // Note in unchecked mode we produce invalid output for invalid inputs.
  4451. // This test just documents our behavior in those cases.
  4452. // These values might change at any point and are not considered
  4453. // what the implementation should be doing here.
  4454. accessor.setPackedSint64Iterable(1, [fakeSint64]);
  4455. expectQualifiedIterable(accessor.getRepeatedSint64Iterable(1));
  4456. }
  4457. });
  4458. it('fail when setting single packed sint64 value with null value', () => {
  4459. const accessor =
  4460. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  4461. const fakeSint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  4462. if (CHECK_CRITICAL_STATE) {
  4463. expect(() => accessor.setPackedSint64Element(1, 0, fakeSint64))
  4464. .toThrowError('Must be Int64 instance, but got: null');
  4465. } else {
  4466. // Note in unchecked mode we produce invalid output for invalid inputs.
  4467. // This test just documents our behavior in those cases.
  4468. // These values might change at any point and are not considered
  4469. // what the implementation should be doing here.
  4470. accessor.setPackedSint64Element(1, 0, fakeSint64);
  4471. expectQualifiedIterable(accessor.getRepeatedSint64Iterable(1));
  4472. }
  4473. });
  4474. it('fail when setting single unpacked with out-of-bound index', () => {
  4475. const accessor =
  4476. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x00));
  4477. if (CHECK_CRITICAL_STATE) {
  4478. expect(() => accessor.setUnpackedSint64Element(1, 1, Int64.fromInt(1)))
  4479. .toThrowError('Index out of bounds: index: 1 size: 1');
  4480. } else {
  4481. // Note in unchecked mode we produce invalid output for invalid inputs.
  4482. // This test just documents our behavior in those cases.
  4483. // These values might change at any point and are not considered
  4484. // what the implementation should be doing here.
  4485. accessor.setUnpackedSint64Element(1, 1, Int64.fromInt(1));
  4486. expectQualifiedIterable(
  4487. accessor.getRepeatedSint64Iterable(1),
  4488. (value) => value instanceof Int64);
  4489. }
  4490. });
  4491. it('fail when setting single packed with out-of-bound index', () => {
  4492. const accessor =
  4493. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  4494. if (CHECK_CRITICAL_STATE) {
  4495. expect(() => accessor.setPackedSint64Element(1, 1, Int64.fromInt(1)))
  4496. .toThrowError('Index out of bounds: index: 1 size: 1');
  4497. } else {
  4498. // Note in unchecked mode we produce invalid output for invalid inputs.
  4499. // This test just documents our behavior in those cases.
  4500. // These values might change at any point and are not considered
  4501. // what the implementation should be doing here.
  4502. accessor.setPackedSint64Element(1, 1, Int64.fromInt(1));
  4503. expectQualifiedIterable(
  4504. accessor.getRepeatedSint64Iterable(1),
  4505. (value) => value instanceof Int64);
  4506. }
  4507. });
  4508. it('fail when getting element with out-of-range index', () => {
  4509. const accessor = LazyAccessor.createEmpty();
  4510. if (CHECK_CRITICAL_STATE) {
  4511. expect(() => {
  4512. accessor.getRepeatedSint64Element(
  4513. /* fieldNumber= */ 1, /* index= */ 0);
  4514. }).toThrowError('Index out of bounds: index: 0 size: 0');
  4515. } else {
  4516. // Note in unchecked mode we produce invalid output for invalid inputs.
  4517. // This test just documents our behavior in those cases.
  4518. // These values might change at any point and are not considered
  4519. // what the implementation should be doing here.
  4520. expect(accessor.getRepeatedSint64Element(
  4521. /* fieldNumber= */ 1, /* index= */ 0))
  4522. .toBe(undefined);
  4523. }
  4524. });
  4525. });
  4526. describe('LazyAccessor for repeated uint32 does', () => {
  4527. const value1 = 1;
  4528. const value2 = 0;
  4529. const unpackedValue1Value2 = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  4530. const unpackedValue2Value1 = createArrayBuffer(0x08, 0x00, 0x08, 0x01);
  4531. const packedValue1Value2 = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  4532. const packedValue2Value1 = createArrayBuffer(0x0A, 0x02, 0x00, 0x01);
  4533. it('return empty array for the empty input', () => {
  4534. const accessor = LazyAccessor.createEmpty();
  4535. const list = accessor.getRepeatedUint32Iterable(1);
  4536. expectEqualToArray(list, []);
  4537. });
  4538. it('ensure not the same instance returned for the empty input', () => {
  4539. const accessor = LazyAccessor.createEmpty();
  4540. const list1 = accessor.getRepeatedUint32Iterable(1);
  4541. const list2 = accessor.getRepeatedUint32Iterable(1);
  4542. expect(list1).not.toBe(list2);
  4543. });
  4544. it('return size for the empty input', () => {
  4545. const accessor = LazyAccessor.createEmpty();
  4546. const size = accessor.getRepeatedUint32Size(1);
  4547. expect(size).toEqual(0);
  4548. });
  4549. it('return unpacked values from the input', () => {
  4550. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4551. const list = accessor.getRepeatedUint32Iterable(1);
  4552. expectEqualToArray(list, [value1, value2]);
  4553. });
  4554. it('ensure not the same instance returned for unpacked values', () => {
  4555. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4556. const list1 = accessor.getRepeatedUint32Iterable(1);
  4557. const list2 = accessor.getRepeatedUint32Iterable(1);
  4558. expect(list1).not.toBe(list2);
  4559. });
  4560. it('add single unpacked value', () => {
  4561. const accessor = LazyAccessor.createEmpty();
  4562. accessor.addUnpackedUint32Element(1, value1);
  4563. const list1 = accessor.getRepeatedUint32Iterable(1);
  4564. accessor.addUnpackedUint32Element(1, value2);
  4565. const list2 = accessor.getRepeatedUint32Iterable(1);
  4566. expectEqualToArray(list1, [value1]);
  4567. expectEqualToArray(list2, [value1, value2]);
  4568. });
  4569. it('add unpacked values', () => {
  4570. const accessor = LazyAccessor.createEmpty();
  4571. accessor.addUnpackedUint32Iterable(1, [value1]);
  4572. const list1 = accessor.getRepeatedUint32Iterable(1);
  4573. accessor.addUnpackedUint32Iterable(1, [value2]);
  4574. const list2 = accessor.getRepeatedUint32Iterable(1);
  4575. expectEqualToArray(list1, [value1]);
  4576. expectEqualToArray(list2, [value1, value2]);
  4577. });
  4578. it('set a single unpacked value', () => {
  4579. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  4580. accessor.setUnpackedUint32Element(1, 1, value1);
  4581. const list = accessor.getRepeatedUint32Iterable(1);
  4582. expectEqualToArray(list, [value1, value1]);
  4583. });
  4584. it('set unpacked values', () => {
  4585. const accessor = LazyAccessor.createEmpty();
  4586. accessor.setUnpackedUint32Iterable(1, [value1]);
  4587. const list = accessor.getRepeatedUint32Iterable(1);
  4588. expectEqualToArray(list, [value1]);
  4589. });
  4590. it('encode for adding single unpacked value', () => {
  4591. const accessor = LazyAccessor.createEmpty();
  4592. accessor.addUnpackedUint32Element(1, value1);
  4593. accessor.addUnpackedUint32Element(1, value2);
  4594. const serialized = accessor.serialize();
  4595. expect(serialized).toEqual(unpackedValue1Value2);
  4596. });
  4597. it('encode for adding unpacked values', () => {
  4598. const accessor = LazyAccessor.createEmpty();
  4599. accessor.addUnpackedUint32Iterable(1, [value1]);
  4600. accessor.addUnpackedUint32Iterable(1, [value2]);
  4601. const serialized = accessor.serialize();
  4602. expect(serialized).toEqual(unpackedValue1Value2);
  4603. });
  4604. it('encode for setting single unpacked value', () => {
  4605. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4606. accessor.setUnpackedUint32Element(1, 0, value2);
  4607. accessor.setUnpackedUint32Element(1, 1, value1);
  4608. const serialized = accessor.serialize();
  4609. expect(serialized).toEqual(unpackedValue2Value1);
  4610. });
  4611. it('encode for setting unpacked values', () => {
  4612. const accessor = LazyAccessor.createEmpty();
  4613. accessor.setUnpackedUint32Iterable(1, [value1, value2]);
  4614. const serialized = accessor.serialize();
  4615. expect(serialized).toEqual(unpackedValue1Value2);
  4616. });
  4617. it('return packed values from the input', () => {
  4618. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  4619. const list = accessor.getRepeatedUint32Iterable(1);
  4620. expectEqualToArray(list, [value1, value2]);
  4621. });
  4622. it('ensure not the same instance returned for packed values', () => {
  4623. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  4624. const list1 = accessor.getRepeatedUint32Iterable(1);
  4625. const list2 = accessor.getRepeatedUint32Iterable(1);
  4626. expect(list1).not.toBe(list2);
  4627. });
  4628. it('add single packed value', () => {
  4629. const accessor = LazyAccessor.createEmpty();
  4630. accessor.addPackedUint32Element(1, value1);
  4631. const list1 = accessor.getRepeatedUint32Iterable(1);
  4632. accessor.addPackedUint32Element(1, value2);
  4633. const list2 = accessor.getRepeatedUint32Iterable(1);
  4634. expectEqualToArray(list1, [value1]);
  4635. expectEqualToArray(list2, [value1, value2]);
  4636. });
  4637. it('add packed values', () => {
  4638. const accessor = LazyAccessor.createEmpty();
  4639. accessor.addPackedUint32Iterable(1, [value1]);
  4640. const list1 = accessor.getRepeatedUint32Iterable(1);
  4641. accessor.addPackedUint32Iterable(1, [value2]);
  4642. const list2 = accessor.getRepeatedUint32Iterable(1);
  4643. expectEqualToArray(list1, [value1]);
  4644. expectEqualToArray(list2, [value1, value2]);
  4645. });
  4646. it('set a single packed value', () => {
  4647. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4648. accessor.setPackedUint32Element(1, 1, value1);
  4649. const list = accessor.getRepeatedUint32Iterable(1);
  4650. expectEqualToArray(list, [value1, value1]);
  4651. });
  4652. it('set packed values', () => {
  4653. const accessor = LazyAccessor.createEmpty();
  4654. accessor.setPackedUint32Iterable(1, [value1]);
  4655. const list1 = accessor.getRepeatedUint32Iterable(1);
  4656. accessor.setPackedUint32Iterable(1, [value2]);
  4657. const list2 = accessor.getRepeatedUint32Iterable(1);
  4658. expectEqualToArray(list1, [value1]);
  4659. expectEqualToArray(list2, [value2]);
  4660. });
  4661. it('encode for adding single packed value', () => {
  4662. const accessor = LazyAccessor.createEmpty();
  4663. accessor.addPackedUint32Element(1, value1);
  4664. accessor.addPackedUint32Element(1, value2);
  4665. const serialized = accessor.serialize();
  4666. expect(serialized).toEqual(packedValue1Value2);
  4667. });
  4668. it('encode for adding packed values', () => {
  4669. const accessor = LazyAccessor.createEmpty();
  4670. accessor.addPackedUint32Iterable(1, [value1, value2]);
  4671. const serialized = accessor.serialize();
  4672. expect(serialized).toEqual(packedValue1Value2);
  4673. });
  4674. it('encode for setting single packed value', () => {
  4675. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4676. accessor.setPackedUint32Element(1, 0, value2);
  4677. accessor.setPackedUint32Element(1, 1, value1);
  4678. const serialized = accessor.serialize();
  4679. expect(serialized).toEqual(packedValue2Value1);
  4680. });
  4681. it('encode for setting packed values', () => {
  4682. const accessor = LazyAccessor.createEmpty();
  4683. accessor.setPackedUint32Iterable(1, [value1, value2]);
  4684. const serialized = accessor.serialize();
  4685. expect(serialized).toEqual(packedValue1Value2);
  4686. });
  4687. it('return combined values from the input', () => {
  4688. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  4689. 0x08,
  4690. 0x01, // unpacked value1
  4691. 0x0A,
  4692. 0x02,
  4693. 0x01,
  4694. 0x00, // packed value1 and value2
  4695. 0x08,
  4696. 0x00, // unpacked value2
  4697. ));
  4698. const list = accessor.getRepeatedUint32Iterable(1);
  4699. expectEqualToArray(list, [value1, value1, value2, value2]);
  4700. });
  4701. it('return the repeated field element from the input', () => {
  4702. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4703. const result1 = accessor.getRepeatedUint32Element(
  4704. /* fieldNumber= */ 1, /* index= */ 0);
  4705. const result2 = accessor.getRepeatedUint32Element(
  4706. /* fieldNumber= */ 1, /* index= */ 1);
  4707. expect(result1).toEqual(value1);
  4708. expect(result2).toEqual(value2);
  4709. });
  4710. it('return the size from the input', () => {
  4711. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4712. const size = accessor.getRepeatedUint32Size(1);
  4713. expect(size).toEqual(2);
  4714. });
  4715. it('fail when getting unpacked uint32 value with other wire types', () => {
  4716. const accessor = LazyAccessor.fromArrayBuffer(
  4717. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  4718. if (CHECK_CRITICAL_STATE) {
  4719. expect(() => {
  4720. accessor.getRepeatedUint32Iterable(1);
  4721. }).toThrowError('Expected wire type: 0 but found: 5');
  4722. } else {
  4723. // Note in unchecked mode we produce invalid output for invalid inputs.
  4724. // This test just documents our behavior in those cases.
  4725. // These values might change at any point and are not considered
  4726. // what the implementation should be doing here.
  4727. expectQualifiedIterable(
  4728. accessor.getRepeatedUint32Iterable(1),
  4729. (value) => Number.isInteger(value));
  4730. }
  4731. });
  4732. it('fail when adding unpacked uint32 values with null value', () => {
  4733. const accessor = LazyAccessor.createEmpty();
  4734. const fakeUint32 = /** @type {number} */ (/** @type {*} */ (null));
  4735. if (CHECK_CRITICAL_STATE) {
  4736. expect(() => accessor.addUnpackedUint32Iterable(1, [fakeUint32]))
  4737. .toThrowError('Must be a number, but got: null');
  4738. } else {
  4739. // Note in unchecked mode we produce invalid output for invalid inputs.
  4740. // This test just documents our behavior in those cases.
  4741. // These values might change at any point and are not considered
  4742. // what the implementation should be doing here.
  4743. accessor.addUnpackedUint32Iterable(1, [fakeUint32]);
  4744. expectQualifiedIterable(accessor.getRepeatedUint32Iterable(1));
  4745. }
  4746. });
  4747. it('fail when adding single unpacked uint32 value with null value', () => {
  4748. const accessor = LazyAccessor.createEmpty();
  4749. const fakeUint32 = /** @type {number} */ (/** @type {*} */ (null));
  4750. if (CHECK_CRITICAL_STATE) {
  4751. expect(() => accessor.addUnpackedUint32Element(1, fakeUint32))
  4752. .toThrowError('Must be a number, but got: null');
  4753. } else {
  4754. // Note in unchecked mode we produce invalid output for invalid inputs.
  4755. // This test just documents our behavior in those cases.
  4756. // These values might change at any point and are not considered
  4757. // what the implementation should be doing here.
  4758. accessor.addUnpackedUint32Element(1, fakeUint32);
  4759. expectQualifiedIterable(accessor.getRepeatedUint32Iterable(1));
  4760. }
  4761. });
  4762. it('fail when setting unpacked uint32 values with null value', () => {
  4763. const accessor = LazyAccessor.createEmpty();
  4764. const fakeUint32 = /** @type {number} */ (/** @type {*} */ (null));
  4765. if (CHECK_CRITICAL_STATE) {
  4766. expect(() => accessor.setUnpackedUint32Iterable(1, [fakeUint32]))
  4767. .toThrowError('Must be a number, but got: null');
  4768. } else {
  4769. // Note in unchecked mode we produce invalid output for invalid inputs.
  4770. // This test just documents our behavior in those cases.
  4771. // These values might change at any point and are not considered
  4772. // what the implementation should be doing here.
  4773. accessor.setUnpackedUint32Iterable(1, [fakeUint32]);
  4774. expectQualifiedIterable(accessor.getRepeatedUint32Iterable(1));
  4775. }
  4776. });
  4777. it('fail when setting single unpacked uint32 value with null value', () => {
  4778. const accessor = LazyAccessor.fromArrayBuffer(
  4779. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  4780. const fakeUint32 = /** @type {number} */ (/** @type {*} */ (null));
  4781. if (CHECK_CRITICAL_STATE) {
  4782. expect(() => accessor.setUnpackedUint32Element(1, 0, fakeUint32))
  4783. .toThrowError('Must be a number, but got: null');
  4784. } else {
  4785. // Note in unchecked mode we produce invalid output for invalid inputs.
  4786. // This test just documents our behavior in those cases.
  4787. // These values might change at any point and are not considered
  4788. // what the implementation should be doing here.
  4789. accessor.setUnpackedUint32Element(1, 0, fakeUint32);
  4790. expectQualifiedIterable(
  4791. accessor.getRepeatedUint32Iterable(1),
  4792. );
  4793. }
  4794. });
  4795. it('fail when adding packed uint32 values with null value', () => {
  4796. const accessor = LazyAccessor.createEmpty();
  4797. const fakeUint32 = /** @type {number} */ (/** @type {*} */ (null));
  4798. if (CHECK_CRITICAL_STATE) {
  4799. expect(() => accessor.addPackedUint32Iterable(1, [fakeUint32]))
  4800. .toThrowError('Must be a number, but got: null');
  4801. } else {
  4802. // Note in unchecked mode we produce invalid output for invalid inputs.
  4803. // This test just documents our behavior in those cases.
  4804. // These values might change at any point and are not considered
  4805. // what the implementation should be doing here.
  4806. accessor.addPackedUint32Iterable(1, [fakeUint32]);
  4807. expectQualifiedIterable(accessor.getRepeatedUint32Iterable(1));
  4808. }
  4809. });
  4810. it('fail when adding single packed uint32 value with null value', () => {
  4811. const accessor = LazyAccessor.createEmpty();
  4812. const fakeUint32 = /** @type {number} */ (/** @type {*} */ (null));
  4813. if (CHECK_CRITICAL_STATE) {
  4814. expect(() => accessor.addPackedUint32Element(1, fakeUint32))
  4815. .toThrowError('Must be a number, but got: null');
  4816. } else {
  4817. // Note in unchecked mode we produce invalid output for invalid inputs.
  4818. // This test just documents our behavior in those cases.
  4819. // These values might change at any point and are not considered
  4820. // what the implementation should be doing here.
  4821. accessor.addPackedUint32Element(1, fakeUint32);
  4822. expectQualifiedIterable(accessor.getRepeatedUint32Iterable(1));
  4823. }
  4824. });
  4825. it('fail when setting packed uint32 values with null value', () => {
  4826. const accessor = LazyAccessor.createEmpty();
  4827. const fakeUint32 = /** @type {number} */ (/** @type {*} */ (null));
  4828. if (CHECK_CRITICAL_STATE) {
  4829. expect(() => accessor.setPackedUint32Iterable(1, [fakeUint32]))
  4830. .toThrowError('Must be a number, but got: null');
  4831. } else {
  4832. // Note in unchecked mode we produce invalid output for invalid inputs.
  4833. // This test just documents our behavior in those cases.
  4834. // These values might change at any point and are not considered
  4835. // what the implementation should be doing here.
  4836. accessor.setPackedUint32Iterable(1, [fakeUint32]);
  4837. expectQualifiedIterable(accessor.getRepeatedUint32Iterable(1));
  4838. }
  4839. });
  4840. it('fail when setting single packed uint32 value with null value', () => {
  4841. const accessor =
  4842. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  4843. const fakeUint32 = /** @type {number} */ (/** @type {*} */ (null));
  4844. if (CHECK_CRITICAL_STATE) {
  4845. expect(() => accessor.setPackedUint32Element(1, 0, fakeUint32))
  4846. .toThrowError('Must be a number, but got: null');
  4847. } else {
  4848. // Note in unchecked mode we produce invalid output for invalid inputs.
  4849. // This test just documents our behavior in those cases.
  4850. // These values might change at any point and are not considered
  4851. // what the implementation should be doing here.
  4852. accessor.setPackedUint32Element(1, 0, fakeUint32);
  4853. expectQualifiedIterable(accessor.getRepeatedUint32Iterable(1));
  4854. }
  4855. });
  4856. it('fail when setting single unpacked with out-of-bound index', () => {
  4857. const accessor =
  4858. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x00));
  4859. if (CHECK_CRITICAL_STATE) {
  4860. expect(() => accessor.setUnpackedUint32Element(1, 1, 1))
  4861. .toThrowError('Index out of bounds: index: 1 size: 1');
  4862. } else {
  4863. // Note in unchecked mode we produce invalid output for invalid inputs.
  4864. // This test just documents our behavior in those cases.
  4865. // These values might change at any point and are not considered
  4866. // what the implementation should be doing here.
  4867. accessor.setUnpackedUint32Element(1, 1, 1);
  4868. expectQualifiedIterable(
  4869. accessor.getRepeatedUint32Iterable(1),
  4870. (value) => Number.isInteger(value));
  4871. }
  4872. });
  4873. it('fail when setting single packed with out-of-bound index', () => {
  4874. const accessor =
  4875. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  4876. if (CHECK_CRITICAL_STATE) {
  4877. expect(() => accessor.setPackedUint32Element(1, 1, 1))
  4878. .toThrowError('Index out of bounds: index: 1 size: 1');
  4879. } else {
  4880. // Note in unchecked mode we produce invalid output for invalid inputs.
  4881. // This test just documents our behavior in those cases.
  4882. // These values might change at any point and are not considered
  4883. // what the implementation should be doing here.
  4884. accessor.setPackedUint32Element(1, 1, 1);
  4885. expectQualifiedIterable(
  4886. accessor.getRepeatedUint32Iterable(1),
  4887. (value) => Number.isInteger(value));
  4888. }
  4889. });
  4890. it('fail when getting element with out-of-range index', () => {
  4891. const accessor = LazyAccessor.createEmpty();
  4892. if (CHECK_CRITICAL_STATE) {
  4893. expect(() => {
  4894. accessor.getRepeatedUint32Element(
  4895. /* fieldNumber= */ 1, /* index= */ 0);
  4896. }).toThrowError('Index out of bounds: index: 0 size: 0');
  4897. } else {
  4898. // Note in unchecked mode we produce invalid output for invalid inputs.
  4899. // This test just documents our behavior in those cases.
  4900. // These values might change at any point and are not considered
  4901. // what the implementation should be doing here.
  4902. expect(accessor.getRepeatedUint32Element(
  4903. /* fieldNumber= */ 1, /* index= */ 0))
  4904. .toBe(undefined);
  4905. }
  4906. });
  4907. });
  4908. describe('LazyAccessor for repeated uint64 does', () => {
  4909. const value1 = Int64.fromInt(1);
  4910. const value2 = Int64.fromInt(0);
  4911. const unpackedValue1Value2 = createArrayBuffer(0x08, 0x01, 0x08, 0x00);
  4912. const unpackedValue2Value1 = createArrayBuffer(0x08, 0x00, 0x08, 0x01);
  4913. const packedValue1Value2 = createArrayBuffer(0x0A, 0x02, 0x01, 0x00);
  4914. const packedValue2Value1 = createArrayBuffer(0x0A, 0x02, 0x00, 0x01);
  4915. it('return empty array for the empty input', () => {
  4916. const accessor = LazyAccessor.createEmpty();
  4917. const list = accessor.getRepeatedUint64Iterable(1);
  4918. expectEqualToArray(list, []);
  4919. });
  4920. it('ensure not the same instance returned for the empty input', () => {
  4921. const accessor = LazyAccessor.createEmpty();
  4922. const list1 = accessor.getRepeatedUint64Iterable(1);
  4923. const list2 = accessor.getRepeatedUint64Iterable(1);
  4924. expect(list1).not.toBe(list2);
  4925. });
  4926. it('return size for the empty input', () => {
  4927. const accessor = LazyAccessor.createEmpty();
  4928. const size = accessor.getRepeatedUint64Size(1);
  4929. expect(size).toEqual(0);
  4930. });
  4931. it('return unpacked values from the input', () => {
  4932. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4933. const list = accessor.getRepeatedUint64Iterable(1);
  4934. expectEqualToArray(list, [value1, value2]);
  4935. });
  4936. it('ensure not the same instance returned for unpacked values', () => {
  4937. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4938. const list1 = accessor.getRepeatedUint64Iterable(1);
  4939. const list2 = accessor.getRepeatedUint64Iterable(1);
  4940. expect(list1).not.toBe(list2);
  4941. });
  4942. it('add single unpacked value', () => {
  4943. const accessor = LazyAccessor.createEmpty();
  4944. accessor.addUnpackedUint64Element(1, value1);
  4945. const list1 = accessor.getRepeatedUint64Iterable(1);
  4946. accessor.addUnpackedUint64Element(1, value2);
  4947. const list2 = accessor.getRepeatedUint64Iterable(1);
  4948. expectEqualToArray(list1, [value1]);
  4949. expectEqualToArray(list2, [value1, value2]);
  4950. });
  4951. it('add unpacked values', () => {
  4952. const accessor = LazyAccessor.createEmpty();
  4953. accessor.addUnpackedUint64Iterable(1, [value1]);
  4954. const list1 = accessor.getRepeatedUint64Iterable(1);
  4955. accessor.addUnpackedUint64Iterable(1, [value2]);
  4956. const list2 = accessor.getRepeatedUint64Iterable(1);
  4957. expectEqualToArray(list1, [value1]);
  4958. expectEqualToArray(list2, [value1, value2]);
  4959. });
  4960. it('set a single unpacked value', () => {
  4961. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  4962. accessor.setUnpackedUint64Element(1, 1, value1);
  4963. const list = accessor.getRepeatedUint64Iterable(1);
  4964. expectEqualToArray(list, [value1, value1]);
  4965. });
  4966. it('set unpacked values', () => {
  4967. const accessor = LazyAccessor.createEmpty();
  4968. accessor.setUnpackedUint64Iterable(1, [value1]);
  4969. const list = accessor.getRepeatedUint64Iterable(1);
  4970. expectEqualToArray(list, [value1]);
  4971. });
  4972. it('encode for adding single unpacked value', () => {
  4973. const accessor = LazyAccessor.createEmpty();
  4974. accessor.addUnpackedUint64Element(1, value1);
  4975. accessor.addUnpackedUint64Element(1, value2);
  4976. const serialized = accessor.serialize();
  4977. expect(serialized).toEqual(unpackedValue1Value2);
  4978. });
  4979. it('encode for adding unpacked values', () => {
  4980. const accessor = LazyAccessor.createEmpty();
  4981. accessor.addUnpackedUint64Iterable(1, [value1]);
  4982. accessor.addUnpackedUint64Iterable(1, [value2]);
  4983. const serialized = accessor.serialize();
  4984. expect(serialized).toEqual(unpackedValue1Value2);
  4985. });
  4986. it('encode for setting single unpacked value', () => {
  4987. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  4988. accessor.setUnpackedUint64Element(1, 0, value2);
  4989. accessor.setUnpackedUint64Element(1, 1, value1);
  4990. const serialized = accessor.serialize();
  4991. expect(serialized).toEqual(unpackedValue2Value1);
  4992. });
  4993. it('encode for setting unpacked values', () => {
  4994. const accessor = LazyAccessor.createEmpty();
  4995. accessor.setUnpackedUint64Iterable(1, [value1, value2]);
  4996. const serialized = accessor.serialize();
  4997. expect(serialized).toEqual(unpackedValue1Value2);
  4998. });
  4999. it('return packed values from the input', () => {
  5000. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  5001. const list = accessor.getRepeatedUint64Iterable(1);
  5002. expectEqualToArray(list, [value1, value2]);
  5003. });
  5004. it('ensure not the same instance returned for packed values', () => {
  5005. const accessor = LazyAccessor.fromArrayBuffer(packedValue1Value2);
  5006. const list1 = accessor.getRepeatedUint64Iterable(1);
  5007. const list2 = accessor.getRepeatedUint64Iterable(1);
  5008. expect(list1).not.toBe(list2);
  5009. });
  5010. it('add single packed value', () => {
  5011. const accessor = LazyAccessor.createEmpty();
  5012. accessor.addPackedUint64Element(1, value1);
  5013. const list1 = accessor.getRepeatedUint64Iterable(1);
  5014. accessor.addPackedUint64Element(1, value2);
  5015. const list2 = accessor.getRepeatedUint64Iterable(1);
  5016. expectEqualToArray(list1, [value1]);
  5017. expectEqualToArray(list2, [value1, value2]);
  5018. });
  5019. it('add packed values', () => {
  5020. const accessor = LazyAccessor.createEmpty();
  5021. accessor.addPackedUint64Iterable(1, [value1]);
  5022. const list1 = accessor.getRepeatedUint64Iterable(1);
  5023. accessor.addPackedUint64Iterable(1, [value2]);
  5024. const list2 = accessor.getRepeatedUint64Iterable(1);
  5025. expectEqualToArray(list1, [value1]);
  5026. expectEqualToArray(list2, [value1, value2]);
  5027. });
  5028. it('set a single packed value', () => {
  5029. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  5030. accessor.setPackedUint64Element(1, 1, value1);
  5031. const list = accessor.getRepeatedUint64Iterable(1);
  5032. expectEqualToArray(list, [value1, value1]);
  5033. });
  5034. it('set packed values', () => {
  5035. const accessor = LazyAccessor.createEmpty();
  5036. accessor.setPackedUint64Iterable(1, [value1]);
  5037. const list1 = accessor.getRepeatedUint64Iterable(1);
  5038. accessor.setPackedUint64Iterable(1, [value2]);
  5039. const list2 = accessor.getRepeatedUint64Iterable(1);
  5040. expectEqualToArray(list1, [value1]);
  5041. expectEqualToArray(list2, [value2]);
  5042. });
  5043. it('encode for adding single packed value', () => {
  5044. const accessor = LazyAccessor.createEmpty();
  5045. accessor.addPackedUint64Element(1, value1);
  5046. accessor.addPackedUint64Element(1, value2);
  5047. const serialized = accessor.serialize();
  5048. expect(serialized).toEqual(packedValue1Value2);
  5049. });
  5050. it('encode for adding packed values', () => {
  5051. const accessor = LazyAccessor.createEmpty();
  5052. accessor.addPackedUint64Iterable(1, [value1, value2]);
  5053. const serialized = accessor.serialize();
  5054. expect(serialized).toEqual(packedValue1Value2);
  5055. });
  5056. it('encode for setting single packed value', () => {
  5057. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  5058. accessor.setPackedUint64Element(1, 0, value2);
  5059. accessor.setPackedUint64Element(1, 1, value1);
  5060. const serialized = accessor.serialize();
  5061. expect(serialized).toEqual(packedValue2Value1);
  5062. });
  5063. it('encode for setting packed values', () => {
  5064. const accessor = LazyAccessor.createEmpty();
  5065. accessor.setPackedUint64Iterable(1, [value1, value2]);
  5066. const serialized = accessor.serialize();
  5067. expect(serialized).toEqual(packedValue1Value2);
  5068. });
  5069. it('return combined values from the input', () => {
  5070. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  5071. 0x08,
  5072. 0x01, // unpacked value1
  5073. 0x0A,
  5074. 0x02,
  5075. 0x01,
  5076. 0x00, // packed value1 and value2
  5077. 0x08,
  5078. 0x00, // unpacked value2
  5079. ));
  5080. const list = accessor.getRepeatedUint64Iterable(1);
  5081. expectEqualToArray(list, [value1, value1, value2, value2]);
  5082. });
  5083. it('return the repeated field element from the input', () => {
  5084. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  5085. const result1 = accessor.getRepeatedUint64Element(
  5086. /* fieldNumber= */ 1, /* index= */ 0);
  5087. const result2 = accessor.getRepeatedUint64Element(
  5088. /* fieldNumber= */ 1, /* index= */ 1);
  5089. expect(result1).toEqual(value1);
  5090. expect(result2).toEqual(value2);
  5091. });
  5092. it('return the size from the input', () => {
  5093. const accessor = LazyAccessor.fromArrayBuffer(unpackedValue1Value2);
  5094. const size = accessor.getRepeatedUint64Size(1);
  5095. expect(size).toEqual(2);
  5096. });
  5097. it('fail when getting unpacked uint64 value with other wire types', () => {
  5098. const accessor = LazyAccessor.fromArrayBuffer(
  5099. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  5100. if (CHECK_CRITICAL_STATE) {
  5101. expect(() => {
  5102. accessor.getRepeatedUint64Iterable(1);
  5103. }).toThrowError('Expected wire type: 0 but found: 5');
  5104. } else {
  5105. // Note in unchecked mode we produce invalid output for invalid inputs.
  5106. // This test just documents our behavior in those cases.
  5107. // These values might change at any point and are not considered
  5108. // what the implementation should be doing here.
  5109. expectQualifiedIterable(
  5110. accessor.getRepeatedUint64Iterable(1),
  5111. (value) => value instanceof Int64);
  5112. }
  5113. });
  5114. it('fail when adding unpacked uint64 values with null value', () => {
  5115. const accessor = LazyAccessor.createEmpty();
  5116. const fakeUint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  5117. if (CHECK_CRITICAL_STATE) {
  5118. expect(() => accessor.addUnpackedUint64Iterable(1, [fakeUint64]))
  5119. .toThrowError('Must be Int64 instance, but got: null');
  5120. } else {
  5121. // Note in unchecked mode we produce invalid output for invalid inputs.
  5122. // This test just documents our behavior in those cases.
  5123. // These values might change at any point and are not considered
  5124. // what the implementation should be doing here.
  5125. accessor.addUnpackedUint64Iterable(1, [fakeUint64]);
  5126. expectQualifiedIterable(accessor.getRepeatedUint64Iterable(1));
  5127. }
  5128. });
  5129. it('fail when adding single unpacked uint64 value with null value', () => {
  5130. const accessor = LazyAccessor.createEmpty();
  5131. const fakeUint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  5132. if (CHECK_CRITICAL_STATE) {
  5133. expect(() => accessor.addUnpackedUint64Element(1, fakeUint64))
  5134. .toThrowError('Must be Int64 instance, but got: null');
  5135. } else {
  5136. // Note in unchecked mode we produce invalid output for invalid inputs.
  5137. // This test just documents our behavior in those cases.
  5138. // These values might change at any point and are not considered
  5139. // what the implementation should be doing here.
  5140. accessor.addUnpackedUint64Element(1, fakeUint64);
  5141. expectQualifiedIterable(accessor.getRepeatedUint64Iterable(1));
  5142. }
  5143. });
  5144. it('fail when setting unpacked uint64 values with null value', () => {
  5145. const accessor = LazyAccessor.createEmpty();
  5146. const fakeUint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  5147. if (CHECK_CRITICAL_STATE) {
  5148. expect(() => accessor.setUnpackedUint64Iterable(1, [fakeUint64]))
  5149. .toThrowError('Must be Int64 instance, but got: null');
  5150. } else {
  5151. // Note in unchecked mode we produce invalid output for invalid inputs.
  5152. // This test just documents our behavior in those cases.
  5153. // These values might change at any point and are not considered
  5154. // what the implementation should be doing here.
  5155. accessor.setUnpackedUint64Iterable(1, [fakeUint64]);
  5156. expectQualifiedIterable(accessor.getRepeatedUint64Iterable(1));
  5157. }
  5158. });
  5159. it('fail when setting single unpacked uint64 value with null value', () => {
  5160. const accessor = LazyAccessor.fromArrayBuffer(
  5161. createArrayBuffer(0x0D, 0x80, 0x80, 0x80, 0x00));
  5162. const fakeUint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  5163. if (CHECK_CRITICAL_STATE) {
  5164. expect(() => accessor.setUnpackedUint64Element(1, 0, fakeUint64))
  5165. .toThrowError('Must be Int64 instance, but got: null');
  5166. } else {
  5167. // Note in unchecked mode we produce invalid output for invalid inputs.
  5168. // This test just documents our behavior in those cases.
  5169. // These values might change at any point and are not considered
  5170. // what the implementation should be doing here.
  5171. accessor.setUnpackedUint64Element(1, 0, fakeUint64);
  5172. expectQualifiedIterable(accessor.getRepeatedUint64Iterable(1));
  5173. }
  5174. });
  5175. it('fail when adding packed uint64 values with null value', () => {
  5176. const accessor = LazyAccessor.createEmpty();
  5177. const fakeUint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  5178. if (CHECK_CRITICAL_STATE) {
  5179. expect(() => accessor.addPackedUint64Iterable(1, [fakeUint64]))
  5180. .toThrowError('Must be Int64 instance, but got: null');
  5181. } else {
  5182. // Note in unchecked mode we produce invalid output for invalid inputs.
  5183. // This test just documents our behavior in those cases.
  5184. // These values might change at any point and are not considered
  5185. // what the implementation should be doing here.
  5186. accessor.addPackedUint64Iterable(1, [fakeUint64]);
  5187. expectQualifiedIterable(accessor.getRepeatedUint64Iterable(1));
  5188. }
  5189. });
  5190. it('fail when adding single packed uint64 value with null value', () => {
  5191. const accessor = LazyAccessor.createEmpty();
  5192. const fakeUint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  5193. if (CHECK_CRITICAL_STATE) {
  5194. expect(() => accessor.addPackedUint64Element(1, fakeUint64))
  5195. .toThrowError('Must be Int64 instance, but got: null');
  5196. } else {
  5197. // Note in unchecked mode we produce invalid output for invalid inputs.
  5198. // This test just documents our behavior in those cases.
  5199. // These values might change at any point and are not considered
  5200. // what the implementation should be doing here.
  5201. accessor.addPackedUint64Element(1, fakeUint64);
  5202. expectQualifiedIterable(accessor.getRepeatedUint64Iterable(1));
  5203. }
  5204. });
  5205. it('fail when setting packed uint64 values with null value', () => {
  5206. const accessor = LazyAccessor.createEmpty();
  5207. const fakeUint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  5208. if (CHECK_CRITICAL_STATE) {
  5209. expect(() => accessor.setPackedUint64Iterable(1, [fakeUint64]))
  5210. .toThrowError('Must be Int64 instance, but got: null');
  5211. } else {
  5212. // Note in unchecked mode we produce invalid output for invalid inputs.
  5213. // This test just documents our behavior in those cases.
  5214. // These values might change at any point and are not considered
  5215. // what the implementation should be doing here.
  5216. accessor.setPackedUint64Iterable(1, [fakeUint64]);
  5217. expectQualifiedIterable(accessor.getRepeatedUint64Iterable(1));
  5218. }
  5219. });
  5220. it('fail when setting single packed uint64 value with null value', () => {
  5221. const accessor =
  5222. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  5223. const fakeUint64 = /** @type {!Int64} */ (/** @type {*} */ (null));
  5224. if (CHECK_CRITICAL_STATE) {
  5225. expect(() => accessor.setPackedUint64Element(1, 0, fakeUint64))
  5226. .toThrowError('Must be Int64 instance, but got: null');
  5227. } else {
  5228. // Note in unchecked mode we produce invalid output for invalid inputs.
  5229. // This test just documents our behavior in those cases.
  5230. // These values might change at any point and are not considered
  5231. // what the implementation should be doing here.
  5232. accessor.setPackedUint64Element(1, 0, fakeUint64);
  5233. expectQualifiedIterable(accessor.getRepeatedUint64Iterable(1));
  5234. }
  5235. });
  5236. it('fail when setting single unpacked with out-of-bound index', () => {
  5237. const accessor =
  5238. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x00));
  5239. if (CHECK_CRITICAL_STATE) {
  5240. expect(() => accessor.setUnpackedUint64Element(1, 1, Int64.fromInt(1)))
  5241. .toThrowError('Index out of bounds: index: 1 size: 1');
  5242. } else {
  5243. // Note in unchecked mode we produce invalid output for invalid inputs.
  5244. // This test just documents our behavior in those cases.
  5245. // These values might change at any point and are not considered
  5246. // what the implementation should be doing here.
  5247. accessor.setUnpackedUint64Element(1, 1, Int64.fromInt(1));
  5248. expectQualifiedIterable(
  5249. accessor.getRepeatedUint64Iterable(1),
  5250. (value) => value instanceof Int64);
  5251. }
  5252. });
  5253. it('fail when setting single packed with out-of-bound index', () => {
  5254. const accessor =
  5255. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x08, 0x00));
  5256. if (CHECK_CRITICAL_STATE) {
  5257. expect(() => accessor.setPackedUint64Element(1, 1, Int64.fromInt(1)))
  5258. .toThrowError('Index out of bounds: index: 1 size: 1');
  5259. } else {
  5260. // Note in unchecked mode we produce invalid output for invalid inputs.
  5261. // This test just documents our behavior in those cases.
  5262. // These values might change at any point and are not considered
  5263. // what the implementation should be doing here.
  5264. accessor.setPackedUint64Element(1, 1, Int64.fromInt(1));
  5265. expectQualifiedIterable(
  5266. accessor.getRepeatedUint64Iterable(1),
  5267. (value) => value instanceof Int64);
  5268. }
  5269. });
  5270. it('fail when getting element with out-of-range index', () => {
  5271. const accessor = LazyAccessor.createEmpty();
  5272. if (CHECK_CRITICAL_STATE) {
  5273. expect(() => {
  5274. accessor.getRepeatedUint64Element(
  5275. /* fieldNumber= */ 1, /* index= */ 0);
  5276. }).toThrowError('Index out of bounds: index: 0 size: 0');
  5277. } else {
  5278. // Note in unchecked mode we produce invalid output for invalid inputs.
  5279. // This test just documents our behavior in those cases.
  5280. // These values might change at any point and are not considered
  5281. // what the implementation should be doing here.
  5282. expect(accessor.getRepeatedUint64Element(
  5283. /* fieldNumber= */ 1, /* index= */ 0))
  5284. .toBe(undefined);
  5285. }
  5286. });
  5287. });
  5288. describe('LazyAccessor for repeated bytes does', () => {
  5289. const value1 = ByteString.fromArrayBuffer((createArrayBuffer(0x61)));
  5290. const value2 = ByteString.fromArrayBuffer((createArrayBuffer(0x62)));
  5291. const repeatedValue1Value2 = createArrayBuffer(
  5292. 0x0A,
  5293. 0x01,
  5294. 0x61, // value1
  5295. 0x0A,
  5296. 0x01,
  5297. 0x62, // value2
  5298. );
  5299. const repeatedValue2Value1 = createArrayBuffer(
  5300. 0x0A,
  5301. 0x01,
  5302. 0x62, // value2
  5303. 0x0A,
  5304. 0x01,
  5305. 0x61, // value1
  5306. );
  5307. it('return empty array for the empty input', () => {
  5308. const accessor = LazyAccessor.createEmpty();
  5309. const list = accessor.getRepeatedBytesIterable(1);
  5310. expectEqualToArray(list, []);
  5311. });
  5312. it('ensure not the same instance returned for the empty input', () => {
  5313. const accessor = LazyAccessor.createEmpty();
  5314. const list1 = accessor.getRepeatedBytesIterable(1);
  5315. const list2 = accessor.getRepeatedBytesIterable(1);
  5316. expect(list1).not.toBe(list2);
  5317. });
  5318. it('return size for the empty input', () => {
  5319. const accessor = LazyAccessor.createEmpty();
  5320. const size = accessor.getRepeatedBytesSize(1);
  5321. expect(size).toEqual(0);
  5322. });
  5323. it('return values from the input', () => {
  5324. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5325. const list = accessor.getRepeatedBytesIterable(1);
  5326. expectEqualToArray(list, [value1, value2]);
  5327. });
  5328. it('ensure not the same instance returned for values', () => {
  5329. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5330. const list1 = accessor.getRepeatedBytesIterable(1);
  5331. const list2 = accessor.getRepeatedBytesIterable(1);
  5332. expect(list1).not.toBe(list2);
  5333. });
  5334. it('add single value', () => {
  5335. const accessor = LazyAccessor.createEmpty();
  5336. accessor.addRepeatedBytesElement(1, value1);
  5337. const list1 = accessor.getRepeatedBytesIterable(1);
  5338. accessor.addRepeatedBytesElement(1, value2);
  5339. const list2 = accessor.getRepeatedBytesIterable(1);
  5340. expectEqualToArray(list1, [value1]);
  5341. expectEqualToArray(list2, [value1, value2]);
  5342. });
  5343. it('add values', () => {
  5344. const accessor = LazyAccessor.createEmpty();
  5345. accessor.addRepeatedBytesIterable(1, [value1]);
  5346. const list1 = accessor.getRepeatedBytesIterable(1);
  5347. accessor.addRepeatedBytesIterable(1, [value2]);
  5348. const list2 = accessor.getRepeatedBytesIterable(1);
  5349. expectEqualToArray(list1, [value1]);
  5350. expectEqualToArray(list2, [value1, value2]);
  5351. });
  5352. it('set a single value', () => {
  5353. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5354. accessor.setRepeatedBytesElement(1, 1, value1);
  5355. const list = accessor.getRepeatedBytesIterable(1);
  5356. expectEqualToArray(list, [value1, value1]);
  5357. });
  5358. it('set values', () => {
  5359. const accessor = LazyAccessor.createEmpty();
  5360. accessor.setRepeatedBytesIterable(1, [value1]);
  5361. const list = accessor.getRepeatedBytesIterable(1);
  5362. expectEqualToArray(list, [value1]);
  5363. });
  5364. it('encode for adding single value', () => {
  5365. const accessor = LazyAccessor.createEmpty();
  5366. accessor.addRepeatedBytesElement(1, value1);
  5367. accessor.addRepeatedBytesElement(1, value2);
  5368. const serialized = accessor.serialize();
  5369. expect(serialized).toEqual(repeatedValue1Value2);
  5370. });
  5371. it('encode for adding values', () => {
  5372. const accessor = LazyAccessor.createEmpty();
  5373. accessor.addRepeatedBytesIterable(1, [value1]);
  5374. accessor.addRepeatedBytesIterable(1, [value2]);
  5375. const serialized = accessor.serialize();
  5376. expect(serialized).toEqual(repeatedValue1Value2);
  5377. });
  5378. it('encode for setting single value', () => {
  5379. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5380. accessor.setRepeatedBytesElement(1, 0, value2);
  5381. accessor.setRepeatedBytesElement(1, 1, value1);
  5382. const serialized = accessor.serialize();
  5383. expect(serialized).toEqual(repeatedValue2Value1);
  5384. });
  5385. it('encode for setting values', () => {
  5386. const accessor = LazyAccessor.createEmpty();
  5387. accessor.setRepeatedBytesIterable(1, [value1, value2]);
  5388. const serialized = accessor.serialize();
  5389. expect(serialized).toEqual(repeatedValue1Value2);
  5390. });
  5391. it('return the repeated field element from the input', () => {
  5392. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5393. const result1 = accessor.getRepeatedBytesElement(
  5394. /* fieldNumber= */ 1, /* index= */ 0);
  5395. const result2 = accessor.getRepeatedBytesElement(
  5396. /* fieldNumber= */ 1, /* index= */ 1);
  5397. expect(result1).toEqual(value1);
  5398. expect(result2).toEqual(value2);
  5399. });
  5400. it('return the size from the input', () => {
  5401. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5402. const size = accessor.getRepeatedBytesSize(1);
  5403. expect(size).toEqual(2);
  5404. });
  5405. it('fail when getting bytes value with other wire types', () => {
  5406. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  5407. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  5408. if (CHECK_CRITICAL_STATE) {
  5409. expect(() => {
  5410. accessor.getRepeatedBytesIterable(1);
  5411. }).toThrowError('Expected wire type: 2 but found: 0');
  5412. } else {
  5413. // Note in unchecked mode we produce invalid output for invalid inputs.
  5414. // This test just documents our behavior in those cases.
  5415. // These values might change at any point and are not considered
  5416. // what the implementation should be doing here.
  5417. expectQualifiedIterable(
  5418. accessor.getRepeatedBytesIterable(1),
  5419. (value) => value instanceof ByteString);
  5420. }
  5421. });
  5422. it('fail when adding bytes values with null value', () => {
  5423. const accessor = LazyAccessor.createEmpty();
  5424. const fakeBytes = /** @type {!ByteString} */ (/** @type {*} */ (null));
  5425. if (CHECK_CRITICAL_STATE) {
  5426. expect(() => accessor.addRepeatedBytesIterable(1, [fakeBytes]))
  5427. .toThrowError('Must be a ByteString, but got: null');
  5428. } else {
  5429. // Note in unchecked mode we produce invalid output for invalid inputs.
  5430. // This test just documents our behavior in those cases.
  5431. // These values might change at any point and are not considered
  5432. // what the implementation should be doing here.
  5433. accessor.addRepeatedBytesIterable(1, [fakeBytes]);
  5434. expectQualifiedIterable(accessor.getRepeatedBytesIterable(1));
  5435. }
  5436. });
  5437. it('fail when adding single bytes value with null value', () => {
  5438. const accessor = LazyAccessor.createEmpty();
  5439. const fakeBytes = /** @type {!ByteString} */ (/** @type {*} */ (null));
  5440. if (CHECK_CRITICAL_STATE) {
  5441. expect(() => accessor.addRepeatedBytesElement(1, fakeBytes))
  5442. .toThrowError('Must be a ByteString, but got: null');
  5443. } else {
  5444. // Note in unchecked mode we produce invalid output for invalid inputs.
  5445. // This test just documents our behavior in those cases.
  5446. // These values might change at any point and are not considered
  5447. // what the implementation should be doing here.
  5448. accessor.addRepeatedBytesElement(1, fakeBytes);
  5449. expectQualifiedIterable(accessor.getRepeatedBytesIterable(1));
  5450. }
  5451. });
  5452. it('fail when setting bytes values with null value', () => {
  5453. const accessor = LazyAccessor.createEmpty();
  5454. const fakeBytes = /** @type {!ByteString} */ (/** @type {*} */ (null));
  5455. if (CHECK_CRITICAL_STATE) {
  5456. expect(() => accessor.setRepeatedBytesIterable(1, [fakeBytes]))
  5457. .toThrowError('Must be a ByteString, but got: null');
  5458. } else {
  5459. // Note in unchecked mode we produce invalid output for invalid inputs.
  5460. // This test just documents our behavior in those cases.
  5461. // These values might change at any point and are not considered
  5462. // what the implementation should be doing here.
  5463. accessor.setRepeatedBytesIterable(1, [fakeBytes]);
  5464. expectQualifiedIterable(accessor.getRepeatedBytesIterable(1));
  5465. }
  5466. });
  5467. it('fail when setting single bytes value with null value', () => {
  5468. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  5469. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  5470. const fakeBytes = /** @type {!ByteString} */ (/** @type {*} */ (null));
  5471. if (CHECK_CRITICAL_STATE) {
  5472. expect(() => accessor.setRepeatedBytesElement(1, 0, fakeBytes))
  5473. .toThrowError('Must be a ByteString, but got: null');
  5474. } else {
  5475. // Note in unchecked mode we produce invalid output for invalid inputs.
  5476. // This test just documents our behavior in those cases.
  5477. // These values might change at any point and are not considered
  5478. // what the implementation should be doing here.
  5479. accessor.setRepeatedBytesElement(1, 0, fakeBytes);
  5480. expectQualifiedIterable(accessor.getRepeatedBytesIterable(1));
  5481. }
  5482. });
  5483. it('fail when setting single with out-of-bound index', () => {
  5484. const accessor =
  5485. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x61));
  5486. if (CHECK_CRITICAL_STATE) {
  5487. expect(() => accessor.setRepeatedBytesElement(1, 1, value1))
  5488. .toThrowError('Index out of bounds: index: 1 size: 1');
  5489. } else {
  5490. // Note in unchecked mode we produce invalid output for invalid inputs.
  5491. // This test just documents our behavior in those cases.
  5492. // These values might change at any point and are not considered
  5493. // what the implementation should be doing here.
  5494. accessor.setRepeatedBytesElement(1, 1, value1);
  5495. expectQualifiedIterable(
  5496. accessor.getRepeatedBytesIterable(1),
  5497. (value) => value instanceof ByteString);
  5498. }
  5499. });
  5500. it('fail when getting element with out-of-range index', () => {
  5501. const accessor = LazyAccessor.createEmpty();
  5502. if (CHECK_CRITICAL_STATE) {
  5503. expect(() => {
  5504. accessor.getRepeatedBytesElement(
  5505. /* fieldNumber= */ 1, /* index= */ 0);
  5506. }).toThrowError('Index out of bounds: index: 0 size: 0');
  5507. } else {
  5508. // Note in unchecked mode we produce invalid output for invalid inputs.
  5509. // This test just documents our behavior in those cases.
  5510. // These values might change at any point and are not considered
  5511. // what the implementation should be doing here.
  5512. expect(accessor.getRepeatedBytesElement(
  5513. /* fieldNumber= */ 1, /* index= */ 0))
  5514. .toBe(undefined);
  5515. }
  5516. });
  5517. });
  5518. describe('LazyAccessor for repeated string does', () => {
  5519. const value1 = 'a';
  5520. const value2 = 'b';
  5521. const repeatedValue1Value2 = createArrayBuffer(
  5522. 0x0A,
  5523. 0x01,
  5524. 0x61, // value1
  5525. 0x0A,
  5526. 0x01,
  5527. 0x62, // value2
  5528. );
  5529. const repeatedValue2Value1 = createArrayBuffer(
  5530. 0x0A,
  5531. 0x01,
  5532. 0x62, // value2
  5533. 0x0A,
  5534. 0x01,
  5535. 0x61, // value1
  5536. );
  5537. it('return empty array for the empty input', () => {
  5538. const accessor = LazyAccessor.createEmpty();
  5539. const list = accessor.getRepeatedStringIterable(1);
  5540. expectEqualToArray(list, []);
  5541. });
  5542. it('ensure not the same instance returned for the empty input', () => {
  5543. const accessor = LazyAccessor.createEmpty();
  5544. const list1 = accessor.getRepeatedStringIterable(1);
  5545. const list2 = accessor.getRepeatedStringIterable(1);
  5546. expect(list1).not.toBe(list2);
  5547. });
  5548. it('return size for the empty input', () => {
  5549. const accessor = LazyAccessor.createEmpty();
  5550. const size = accessor.getRepeatedStringSize(1);
  5551. expect(size).toEqual(0);
  5552. });
  5553. it('return values from the input', () => {
  5554. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5555. const list = accessor.getRepeatedStringIterable(1);
  5556. expectEqualToArray(list, [value1, value2]);
  5557. });
  5558. it('ensure not the same instance returned for values', () => {
  5559. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5560. const list1 = accessor.getRepeatedStringIterable(1);
  5561. const list2 = accessor.getRepeatedStringIterable(1);
  5562. expect(list1).not.toBe(list2);
  5563. });
  5564. it('add single value', () => {
  5565. const accessor = LazyAccessor.createEmpty();
  5566. accessor.addRepeatedStringElement(1, value1);
  5567. const list1 = accessor.getRepeatedStringIterable(1);
  5568. accessor.addRepeatedStringElement(1, value2);
  5569. const list2 = accessor.getRepeatedStringIterable(1);
  5570. expectEqualToArray(list1, [value1]);
  5571. expectEqualToArray(list2, [value1, value2]);
  5572. });
  5573. it('add values', () => {
  5574. const accessor = LazyAccessor.createEmpty();
  5575. accessor.addRepeatedStringIterable(1, [value1]);
  5576. const list1 = accessor.getRepeatedStringIterable(1);
  5577. accessor.addRepeatedStringIterable(1, [value2]);
  5578. const list2 = accessor.getRepeatedStringIterable(1);
  5579. expectEqualToArray(list1, [value1]);
  5580. expectEqualToArray(list2, [value1, value2]);
  5581. });
  5582. it('set a single value', () => {
  5583. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5584. accessor.setRepeatedStringElement(1, 1, value1);
  5585. const list = accessor.getRepeatedStringIterable(1);
  5586. expectEqualToArray(list, [value1, value1]);
  5587. });
  5588. it('set values', () => {
  5589. const accessor = LazyAccessor.createEmpty();
  5590. accessor.setRepeatedStringIterable(1, [value1]);
  5591. const list = accessor.getRepeatedStringIterable(1);
  5592. expectEqualToArray(list, [value1]);
  5593. });
  5594. it('encode for adding single value', () => {
  5595. const accessor = LazyAccessor.createEmpty();
  5596. accessor.addRepeatedStringElement(1, value1);
  5597. accessor.addRepeatedStringElement(1, value2);
  5598. const serialized = accessor.serialize();
  5599. expect(serialized).toEqual(repeatedValue1Value2);
  5600. });
  5601. it('encode for adding values', () => {
  5602. const accessor = LazyAccessor.createEmpty();
  5603. accessor.addRepeatedStringIterable(1, [value1]);
  5604. accessor.addRepeatedStringIterable(1, [value2]);
  5605. const serialized = accessor.serialize();
  5606. expect(serialized).toEqual(repeatedValue1Value2);
  5607. });
  5608. it('encode for setting single value', () => {
  5609. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5610. accessor.setRepeatedStringElement(1, 0, value2);
  5611. accessor.setRepeatedStringElement(1, 1, value1);
  5612. const serialized = accessor.serialize();
  5613. expect(serialized).toEqual(repeatedValue2Value1);
  5614. });
  5615. it('encode for setting values', () => {
  5616. const accessor = LazyAccessor.createEmpty();
  5617. accessor.setRepeatedStringIterable(1, [value1, value2]);
  5618. const serialized = accessor.serialize();
  5619. expect(serialized).toEqual(repeatedValue1Value2);
  5620. });
  5621. it('return the repeated field element from the input', () => {
  5622. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5623. const result1 = accessor.getRepeatedStringElement(
  5624. /* fieldNumber= */ 1, /* index= */ 0);
  5625. const result2 = accessor.getRepeatedStringElement(
  5626. /* fieldNumber= */ 1, /* index= */ 1);
  5627. expect(result1).toEqual(value1);
  5628. expect(result2).toEqual(value2);
  5629. });
  5630. it('return the size from the input', () => {
  5631. const accessor = LazyAccessor.fromArrayBuffer(repeatedValue1Value2);
  5632. const size = accessor.getRepeatedStringSize(1);
  5633. expect(size).toEqual(2);
  5634. });
  5635. it('fail when getting string value with other wire types', () => {
  5636. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  5637. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  5638. if (CHECK_CRITICAL_STATE) {
  5639. expect(() => {
  5640. accessor.getRepeatedStringIterable(1);
  5641. }).toThrowError('Expected wire type: 2 but found: 0');
  5642. } else {
  5643. // Note in unchecked mode we produce invalid output for invalid inputs.
  5644. // This test just documents our behavior in those cases.
  5645. // These values might change at any point and are not considered
  5646. // what the implementation should be doing here.
  5647. expectQualifiedIterable(
  5648. accessor.getRepeatedStringIterable(1),
  5649. (value) => typeof value === 'string');
  5650. }
  5651. });
  5652. it('fail when adding string values with null value', () => {
  5653. const accessor = LazyAccessor.createEmpty();
  5654. const fakeString = /** @type {string} */ (/** @type {*} */ (null));
  5655. if (CHECK_CRITICAL_STATE) {
  5656. expect(() => accessor.addRepeatedStringIterable(1, [fakeString]))
  5657. .toThrowError('Must be string, but got: null');
  5658. } else {
  5659. // Note in unchecked mode we produce invalid output for invalid inputs.
  5660. // This test just documents our behavior in those cases.
  5661. // These values might change at any point and are not considered
  5662. // what the implementation should be doing here.
  5663. accessor.addRepeatedStringIterable(1, [fakeString]);
  5664. expectQualifiedIterable(accessor.getRepeatedStringIterable(1));
  5665. }
  5666. });
  5667. it('fail when adding single string value with null value', () => {
  5668. const accessor = LazyAccessor.createEmpty();
  5669. const fakeString = /** @type {string} */ (/** @type {*} */ (null));
  5670. if (CHECK_CRITICAL_STATE) {
  5671. expect(() => accessor.addRepeatedStringElement(1, fakeString))
  5672. .toThrowError('Must be string, but got: null');
  5673. } else {
  5674. // Note in unchecked mode we produce invalid output for invalid inputs.
  5675. // This test just documents our behavior in those cases.
  5676. // These values might change at any point and are not considered
  5677. // what the implementation should be doing here.
  5678. accessor.addRepeatedStringElement(1, fakeString);
  5679. expectQualifiedIterable(accessor.getRepeatedStringIterable(1));
  5680. }
  5681. });
  5682. it('fail when setting string values with null value', () => {
  5683. const accessor = LazyAccessor.createEmpty();
  5684. const fakeString = /** @type {string} */ (/** @type {*} */ (null));
  5685. if (CHECK_CRITICAL_STATE) {
  5686. expect(() => accessor.setRepeatedStringIterable(1, [fakeString]))
  5687. .toThrowError('Must be string, but got: null');
  5688. } else {
  5689. // Note in unchecked mode we produce invalid output for invalid inputs.
  5690. // This test just documents our behavior in those cases.
  5691. // These values might change at any point and are not considered
  5692. // what the implementation should be doing here.
  5693. accessor.setRepeatedStringIterable(1, [fakeString]);
  5694. expectQualifiedIterable(accessor.getRepeatedStringIterable(1));
  5695. }
  5696. });
  5697. it('fail when setting single string value with null value', () => {
  5698. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  5699. 0x08, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00));
  5700. const fakeString = /** @type {string} */ (/** @type {*} */ (null));
  5701. if (CHECK_CRITICAL_STATE) {
  5702. expect(() => accessor.setRepeatedStringElement(1, 0, fakeString))
  5703. .toThrowError('Must be string, but got: null');
  5704. } else {
  5705. // Note in unchecked mode we produce invalid output for invalid inputs.
  5706. // This test just documents our behavior in those cases.
  5707. // These values might change at any point and are not considered
  5708. // what the implementation should be doing here.
  5709. accessor.setRepeatedStringElement(1, 0, fakeString);
  5710. expectQualifiedIterable(accessor.getRepeatedStringIterable(1));
  5711. }
  5712. });
  5713. it('fail when setting single with out-of-bound index', () => {
  5714. const accessor =
  5715. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x01, 0x61));
  5716. if (CHECK_CRITICAL_STATE) {
  5717. expect(() => accessor.setRepeatedStringElement(1, 1, value1))
  5718. .toThrowError('Index out of bounds: index: 1 size: 1');
  5719. } else {
  5720. // Note in unchecked mode we produce invalid output for invalid inputs.
  5721. // This test just documents our behavior in those cases.
  5722. // These values might change at any point and are not considered
  5723. // what the implementation should be doing here.
  5724. accessor.setRepeatedStringElement(1, 1, value1);
  5725. expectQualifiedIterable(
  5726. accessor.getRepeatedStringIterable(1),
  5727. (value) => typeof value === 'string');
  5728. }
  5729. });
  5730. it('fail when getting element with out-of-range index', () => {
  5731. const accessor = LazyAccessor.createEmpty();
  5732. if (CHECK_CRITICAL_STATE) {
  5733. expect(() => {
  5734. accessor.getRepeatedStringElement(
  5735. /* fieldNumber= */ 1, /* index= */ 0);
  5736. }).toThrowError('Index out of bounds: index: 0 size: 0');
  5737. } else {
  5738. // Note in unchecked mode we produce invalid output for invalid inputs.
  5739. // This test just documents our behavior in those cases.
  5740. // These values might change at any point and are not considered
  5741. // what the implementation should be doing here.
  5742. expect(accessor.getRepeatedStringElement(
  5743. /* fieldNumber= */ 1, /* index= */ 0))
  5744. .toBe(undefined);
  5745. }
  5746. });
  5747. });
  5748. describe('LazyAccessor for repeated message does', () => {
  5749. it('return empty array for the empty input', () => {
  5750. const accessor = LazyAccessor.createEmpty();
  5751. expectEqualToArray(
  5752. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator),
  5753. []);
  5754. });
  5755. it('return empty accessor array for the empty input', () => {
  5756. const accessor = LazyAccessor.createEmpty();
  5757. expectEqualToArray(accessor.getRepeatedMessageAccessorIterable(1), []);
  5758. });
  5759. it('ensure not the same instance returned for the empty input', () => {
  5760. const accessor = LazyAccessor.createEmpty();
  5761. const list1 =
  5762. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5763. const list2 =
  5764. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5765. expect(list1).not.toBe(list2);
  5766. });
  5767. it('return size for the empty input', () => {
  5768. const accessor = LazyAccessor.createEmpty();
  5769. expect(accessor.getRepeatedMessageSize(1, TestMessage.instanceCreator))
  5770. .toEqual(0);
  5771. });
  5772. it('return values from the input', () => {
  5773. const bytes1 = createArrayBuffer(0x08, 0x01);
  5774. const bytes2 = createArrayBuffer(0x08, 0x00);
  5775. const msg1 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes1));
  5776. const msg2 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes2));
  5777. const bytes =
  5778. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5779. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5780. expectEqualToMessageArray(
  5781. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator),
  5782. [msg1, msg2]);
  5783. });
  5784. it('ensure not the same array instance returned', () => {
  5785. const bytes =
  5786. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5787. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5788. const list1 =
  5789. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5790. const list2 =
  5791. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5792. expect(list1).not.toBe(list2);
  5793. });
  5794. it('ensure the same array element returned for get iterable', () => {
  5795. const bytes =
  5796. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5797. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5798. const list1 =
  5799. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5800. const list2 = accessor.getRepeatedMessageIterable(
  5801. 1, TestMessage.instanceCreator, /* pivot= */ 0);
  5802. const array1 = Array.from(list1);
  5803. const array2 = Array.from(list2);
  5804. for (let i = 0; i < array1.length; i++) {
  5805. expect(array1[i]).toBe(array2[i]);
  5806. }
  5807. });
  5808. it('return accessors from the input', () => {
  5809. const bytes =
  5810. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5811. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5812. const [accessor1, accessor2] =
  5813. [...accessor.getRepeatedMessageAccessorIterable(1)];
  5814. expect(accessor1.getInt32WithDefault(1)).toEqual(1);
  5815. expect(accessor2.getInt32WithDefault(1)).toEqual(0);
  5816. });
  5817. it('return accessors from the input when pivot is set', () => {
  5818. const bytes =
  5819. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5820. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5821. const [accessor1, accessor2] =
  5822. [...accessor.getRepeatedMessageAccessorIterable(1, /* pivot= */ 0)];
  5823. expect(accessor1.getInt32WithDefault(1)).toEqual(1);
  5824. expect(accessor2.getInt32WithDefault(1)).toEqual(0);
  5825. });
  5826. it('return the repeated field element from the input', () => {
  5827. const bytes =
  5828. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5829. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5830. const msg1 = accessor.getRepeatedMessageElement(
  5831. /* fieldNumber= */ 1, TestMessage.instanceCreator,
  5832. /* index= */ 0);
  5833. const msg2 = accessor.getRepeatedMessageElement(
  5834. /* fieldNumber= */ 1, TestMessage.instanceCreator,
  5835. /* index= */ 1, /* pivot= */ 0);
  5836. expect(msg1.getBoolWithDefault(
  5837. /* fieldNumber= */ 1, /* default= */ false))
  5838. .toEqual(true);
  5839. expect(msg2.getBoolWithDefault(
  5840. /* fieldNumber= */ 1, /* default= */ false))
  5841. .toEqual(false);
  5842. });
  5843. it('ensure the same array element returned', () => {
  5844. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  5845. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5846. const msg1 = accessor.getRepeatedMessageElement(
  5847. /* fieldNumber= */ 1, TestMessage.instanceCreator,
  5848. /* index= */ 0);
  5849. const msg2 = accessor.getRepeatedMessageElement(
  5850. /* fieldNumber= */ 1, TestMessage.instanceCreator,
  5851. /* index= */ 0);
  5852. expect(msg1).toBe(msg2);
  5853. });
  5854. it('return the size from the input', () => {
  5855. const bytes =
  5856. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5857. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5858. expect(accessor.getRepeatedMessageSize(1, TestMessage.instanceCreator))
  5859. .toEqual(2);
  5860. });
  5861. it('encode repeated message from the input', () => {
  5862. const bytes =
  5863. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5864. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5865. expect(accessor.serialize()).toEqual(bytes);
  5866. });
  5867. it('add a single value', () => {
  5868. const accessor = LazyAccessor.createEmpty();
  5869. const bytes1 = createArrayBuffer(0x08, 0x01);
  5870. const msg1 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes1));
  5871. const bytes2 = createArrayBuffer(0x08, 0x00);
  5872. const msg2 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes2));
  5873. accessor.addRepeatedMessageElement(1, msg1, TestMessage.instanceCreator);
  5874. accessor.addRepeatedMessageElement(1, msg2, TestMessage.instanceCreator);
  5875. const result =
  5876. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5877. expect(Array.from(result)).toEqual([msg1, msg2]);
  5878. });
  5879. it('add values', () => {
  5880. const accessor = LazyAccessor.createEmpty();
  5881. const bytes1 = createArrayBuffer(0x08, 0x01);
  5882. const msg1 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes1));
  5883. const bytes2 = createArrayBuffer(0x08, 0x00);
  5884. const msg2 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes2));
  5885. accessor.addRepeatedMessageIterable(1, [msg1], TestMessage.instanceCreator);
  5886. accessor.addRepeatedMessageIterable(1, [msg2], TestMessage.instanceCreator);
  5887. const result =
  5888. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5889. expect(Array.from(result)).toEqual([msg1, msg2]);
  5890. });
  5891. it('set a single value', () => {
  5892. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x00);
  5893. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5894. const subbytes = createArrayBuffer(0x08, 0x01);
  5895. const submsg = new TestMessage(LazyAccessor.fromArrayBuffer(subbytes));
  5896. accessor.setRepeatedMessageElement(
  5897. /* fieldNumber= */ 1, submsg, TestMessage.instanceCreator,
  5898. /* index= */ 0);
  5899. const result =
  5900. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5901. expect(Array.from(result)).toEqual([submsg]);
  5902. });
  5903. it('write submessage changes made via getRepeatedMessagElement', () => {
  5904. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x05);
  5905. const expected = createArrayBuffer(0x0A, 0x02, 0x08, 0x00);
  5906. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5907. const submsg = accessor.getRepeatedMessageElement(
  5908. /* fieldNumber= */ 1, TestMessage.instanceCreator,
  5909. /* index= */ 0);
  5910. expect(submsg.getInt32WithDefault(1, 0)).toEqual(5);
  5911. submsg.setInt32(1, 0);
  5912. expect(accessor.serialize()).toEqual(expected);
  5913. });
  5914. it('set values', () => {
  5915. const accessor = LazyAccessor.createEmpty();
  5916. const subbytes = createArrayBuffer(0x08, 0x01);
  5917. const submsg = new TestMessage(LazyAccessor.fromArrayBuffer(subbytes));
  5918. accessor.setRepeatedMessageIterable(1, [submsg]);
  5919. const result =
  5920. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5921. expect(Array.from(result)).toEqual([submsg]);
  5922. });
  5923. it('encode for adding single value', () => {
  5924. const accessor = LazyAccessor.createEmpty();
  5925. const bytes1 = createArrayBuffer(0x08, 0x01);
  5926. const msg1 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes1));
  5927. const bytes2 = createArrayBuffer(0x08, 0x00);
  5928. const msg2 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes2));
  5929. const expected =
  5930. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5931. accessor.addRepeatedMessageElement(1, msg1, TestMessage.instanceCreator);
  5932. accessor.addRepeatedMessageElement(1, msg2, TestMessage.instanceCreator);
  5933. const result = accessor.serialize();
  5934. expect(result).toEqual(expected);
  5935. });
  5936. it('encode for adding values', () => {
  5937. const accessor = LazyAccessor.createEmpty();
  5938. const bytes1 = createArrayBuffer(0x08, 0x01);
  5939. const msg1 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes1));
  5940. const bytes2 = createArrayBuffer(0x08, 0x00);
  5941. const msg2 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes2));
  5942. const expected =
  5943. createArrayBuffer(0x0A, 0x02, 0x08, 0x01, 0x0A, 0x02, 0x08, 0x00);
  5944. accessor.addRepeatedMessageIterable(
  5945. 1, [msg1, msg2], TestMessage.instanceCreator);
  5946. const result = accessor.serialize();
  5947. expect(result).toEqual(expected);
  5948. });
  5949. it('encode for setting single value', () => {
  5950. const bytes = createArrayBuffer(0x0A, 0x02, 0x08, 0x00);
  5951. const accessor = LazyAccessor.fromArrayBuffer(bytes);
  5952. const subbytes = createArrayBuffer(0x08, 0x01);
  5953. const submsg = new TestMessage(LazyAccessor.fromArrayBuffer(subbytes));
  5954. const expected = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  5955. accessor.setRepeatedMessageElement(
  5956. /* fieldNumber= */ 1, submsg, TestMessage.instanceCreator,
  5957. /* index= */ 0);
  5958. const result = accessor.serialize();
  5959. expect(result).toEqual(expected);
  5960. });
  5961. it('encode for setting values', () => {
  5962. const accessor = LazyAccessor.createEmpty();
  5963. const subbytes = createArrayBuffer(0x08, 0x01);
  5964. const submsg = new TestMessage(LazyAccessor.fromArrayBuffer(subbytes));
  5965. const expected = createArrayBuffer(0x0A, 0x02, 0x08, 0x01);
  5966. accessor.setRepeatedMessageIterable(1, [submsg]);
  5967. const result = accessor.serialize();
  5968. expect(result).toEqual(expected);
  5969. });
  5970. it('get accessors from set values.', () => {
  5971. const accessor = LazyAccessor.createEmpty();
  5972. const bytes1 = createArrayBuffer(0x08, 0x01);
  5973. const msg1 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes1));
  5974. const bytes2 = createArrayBuffer(0x08, 0x00);
  5975. const msg2 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes2));
  5976. accessor.addRepeatedMessageIterable(
  5977. 1, [msg1, msg2], TestMessage.instanceCreator);
  5978. const [accessor1, accessor2] =
  5979. [...accessor.getRepeatedMessageAccessorIterable(1)];
  5980. expect(accessor1.getInt32WithDefault(1)).toEqual(1);
  5981. expect(accessor2.getInt32WithDefault(1)).toEqual(0);
  5982. // Retrieved accessors are the exact same accessors as the added messages.
  5983. expect(accessor1).toBe(
  5984. (/** @type {!InternalMessage} */ (msg1)).internalGetKernel());
  5985. expect(accessor2).toBe(
  5986. (/** @type {!InternalMessage} */ (msg2)).internalGetKernel());
  5987. });
  5988. it('fail when getting message value with other wire types', () => {
  5989. const accessor = LazyAccessor.fromArrayBuffer(createArrayBuffer(
  5990. 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
  5991. if (CHECK_CRITICAL_STATE) {
  5992. expect(() => {
  5993. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  5994. }).toThrow();
  5995. } else {
  5996. // Note in unchecked mode we produce invalid output for invalid inputs.
  5997. // This test just documents our behavior in those cases.
  5998. // These values might change at any point and are not considered
  5999. // what the implementation should be doing here.
  6000. const [msg1] =
  6001. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  6002. expect(msg1.serialize()).toEqual(createArrayBuffer());
  6003. }
  6004. });
  6005. it('fail when adding message values with wrong type value', () => {
  6006. const accessor = LazyAccessor.createEmpty();
  6007. const fakeValue = /** @type {!TestMessage} */ (/** @type {*} */ (null));
  6008. if (CHECK_CRITICAL_STATE) {
  6009. expect(
  6010. () => accessor.addRepeatedMessageIterable(
  6011. 1, [fakeValue], TestMessage.instanceCreator))
  6012. .toThrowError('Given value is not a message instance: null');
  6013. } else {
  6014. // Note in unchecked mode we produce invalid output for invalid inputs.
  6015. // This test just documents our behavior in those cases.
  6016. // These values might change at any point and are not considered
  6017. // what the implementation should be doing here.
  6018. accessor.addRepeatedMessageIterable(
  6019. 1, [fakeValue], TestMessage.instanceCreator);
  6020. const list =
  6021. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  6022. expect(Array.from(list)).toEqual([null]);
  6023. }
  6024. });
  6025. it('fail when adding single message value with wrong type value', () => {
  6026. const accessor = LazyAccessor.createEmpty();
  6027. const fakeValue = /** @type {!TestMessage} */ (/** @type {*} */ (null));
  6028. if (CHECK_CRITICAL_STATE) {
  6029. expect(
  6030. () => accessor.addRepeatedMessageElement(
  6031. 1, fakeValue, TestMessage.instanceCreator))
  6032. .toThrowError('Given value is not a message instance: null');
  6033. } else {
  6034. // Note in unchecked mode we produce invalid output for invalid inputs.
  6035. // This test just documents our behavior in those cases.
  6036. // These values might change at any point and are not considered
  6037. // what the implementation should be doing here.
  6038. accessor.addRepeatedMessageElement(
  6039. 1, fakeValue, TestMessage.instanceCreator);
  6040. const list =
  6041. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  6042. expect(Array.from(list)).toEqual([null]);
  6043. }
  6044. });
  6045. it('fail when setting message values with wrong type value', () => {
  6046. const accessor = LazyAccessor.createEmpty();
  6047. const fakeValue = /** @type {!TestMessage} */ (/** @type {*} */ (null));
  6048. if (CHECK_CRITICAL_STATE) {
  6049. expect(() => accessor.setRepeatedMessageIterable(1, [fakeValue]))
  6050. .toThrowError('Given value is not a message instance: null');
  6051. } else {
  6052. // Note in unchecked mode we produce invalid output for invalid inputs.
  6053. // This test just documents our behavior in those cases.
  6054. // These values might change at any point and are not considered
  6055. // what the implementation should be doing here.
  6056. accessor.setRepeatedMessageIterable(1, [fakeValue]);
  6057. const list =
  6058. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  6059. expect(Array.from(list)).toEqual([null]);
  6060. }
  6061. });
  6062. it('fail when setting single value with wrong type value', () => {
  6063. const accessor =
  6064. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x02, 0x08, 0x00));
  6065. const fakeValue = /** @type {!TestMessage} */ (/** @type {*} */ (null));
  6066. if (CHECK_CRITICAL_STATE) {
  6067. expect(
  6068. () => accessor.setRepeatedMessageElement(
  6069. /* fieldNumber= */ 1, fakeValue, TestMessage.instanceCreator,
  6070. /* index= */ 0))
  6071. .toThrowError('Given value is not a message instance: null');
  6072. } else {
  6073. // Note in unchecked mode we produce invalid output for invalid inputs.
  6074. // This test just documents our behavior in those cases.
  6075. // These values might change at any point and are not considered
  6076. // what the implementation should be doing here.
  6077. accessor.setRepeatedMessageElement(
  6078. /* fieldNumber= */ 1, fakeValue, TestMessage.instanceCreator,
  6079. /* index= */ 0);
  6080. const list =
  6081. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator);
  6082. expect(Array.from(list).length).toEqual(1);
  6083. }
  6084. });
  6085. it('fail when setting single value with out-of-bound index', () => {
  6086. const accessor =
  6087. LazyAccessor.fromArrayBuffer(createArrayBuffer(0x0A, 0x02, 0x08, 0x00));
  6088. const msg1 =
  6089. accessor.getRepeatedMessageElement(1, TestMessage.instanceCreator, 0);
  6090. const bytes2 = createArrayBuffer(0x08, 0x01);
  6091. const msg2 = new TestMessage(LazyAccessor.fromArrayBuffer(bytes2));
  6092. if (CHECK_CRITICAL_STATE) {
  6093. expect(
  6094. () => accessor.setRepeatedMessageElement(
  6095. /* fieldNumber= */ 1, msg2, TestMessage.instanceCreator,
  6096. /* index= */ 1))
  6097. .toThrowError('Index out of bounds: index: 1 size: 1');
  6098. } else {
  6099. // Note in unchecked mode we produce invalid output for invalid inputs.
  6100. // This test just documents our behavior in those cases.
  6101. // These values might change at any point and are not considered
  6102. // what the implementation should be doing here.
  6103. accessor.setRepeatedMessageElement(
  6104. /* fieldNumber= */ 1, msg2, TestMessage.instanceCreator,
  6105. /* index= */ 1);
  6106. expectEqualToArray(
  6107. accessor.getRepeatedMessageIterable(1, TestMessage.instanceCreator),
  6108. [msg1, msg2]);
  6109. }
  6110. });
  6111. });