No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FixedListTests.gen.cs 164KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433
  1. //------------------------------------------------------------------------------
  2. // <auto-generated>
  3. // This code was generated by a tool.
  4. //
  5. // TextTransform Samples/Packages/com.unity.collections/Unity.Collections.Tests/FixedListTests.tt
  6. //
  7. // Changes to this file may cause incorrect behavior and will be lost if
  8. // the code is regenerated.
  9. // </auto-generated>
  10. //------------------------------------------------------------------------------
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.Threading;
  15. using NUnit.Framework;
  16. using Unity.Collections;
  17. using Unity.Collections.LowLevel.Unsafe;
  18. using Unity.Collections.Tests;
  19. using UnityEngine;
  20. internal class FixedListTests : CollectionsTestFixture
  21. {
  22. struct NonComparableStruct
  23. {
  24. public int a;
  25. }
  26. struct DescendingComparer<T> : IComparer<T> where T : IComparable<T>
  27. {
  28. public int Compare(T x, T y) => y.CompareTo(x);
  29. }
  30. [Test]
  31. public void FixedList32BytesDebugView()
  32. {
  33. var list = new FixedList32Bytes<NonComparableStruct>();
  34. CollectionAssert.IsEmpty(new FixedList32BytesDebugView<NonComparableStruct>(list).Items);
  35. var reference = new []
  36. {
  37. new NonComparableStruct{ a = 123 },
  38. new NonComparableStruct{ a = 234 },
  39. new NonComparableStruct{ a = 345 },
  40. };
  41. list.Add(reference[0]);
  42. list.Add(reference[1]);
  43. list.Add(reference[2]);
  44. CollectionAssert.AreEqual(reference, new FixedList32BytesDebugView<NonComparableStruct>(list).Items);
  45. }
  46. [Test]
  47. public void FixedList64BytesDebugView()
  48. {
  49. var list = new FixedList64Bytes<NonComparableStruct>();
  50. CollectionAssert.IsEmpty(new FixedList64BytesDebugView<NonComparableStruct>(list).Items);
  51. var reference = new []
  52. {
  53. new NonComparableStruct{ a = 123 },
  54. new NonComparableStruct{ a = 234 },
  55. new NonComparableStruct{ a = 345 },
  56. };
  57. list.Add(reference[0]);
  58. list.Add(reference[1]);
  59. list.Add(reference[2]);
  60. CollectionAssert.AreEqual(reference, new FixedList64BytesDebugView<NonComparableStruct>(list).Items);
  61. }
  62. [Test]
  63. public void FixedList128BytesDebugView()
  64. {
  65. var list = new FixedList128Bytes<NonComparableStruct>();
  66. CollectionAssert.IsEmpty(new FixedList128BytesDebugView<NonComparableStruct>(list).Items);
  67. var reference = new []
  68. {
  69. new NonComparableStruct{ a = 123 },
  70. new NonComparableStruct{ a = 234 },
  71. new NonComparableStruct{ a = 345 },
  72. };
  73. list.Add(reference[0]);
  74. list.Add(reference[1]);
  75. list.Add(reference[2]);
  76. CollectionAssert.AreEqual(reference, new FixedList128BytesDebugView<NonComparableStruct>(list).Items);
  77. }
  78. public readonly unsafe struct FixedList32Byte_Wrapper
  79. {
  80. readonly FixedList32Bytes<byte> _values;
  81. internal byte* Values => (byte*)_values.Buffer;
  82. public FixedList32Byte_Wrapper(byte x, byte y)
  83. {
  84. _values = new FixedList32Bytes<byte>();
  85. _values.Add(x);
  86. _values.Add(y);
  87. _values.Sort();
  88. }
  89. }
  90. unsafe void FixedList32Byte_ReadonlyWorksFunc1(in FixedList32Byte_Wrapper list, byte* originalValues)
  91. {
  92. byte* values = list.Values;
  93. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  94. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  95. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  96. }
  97. [Test]
  98. public unsafe void FixedList32Byte_ReadonlyWorks()
  99. {
  100. var list = new FixedList32Byte_Wrapper(17, 23);
  101. byte* values = list.Values;
  102. FixedList32Byte_ReadonlyWorksFunc1(list, values);
  103. }
  104. [Test]
  105. public unsafe void FixedList32Byte_FixedBytes32Align8IsAlignedGeneric()
  106. {
  107. var list = new FixedList<byte,FixedBytes32Align8>();
  108. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(byte) - 1)) == 0);
  109. }
  110. [Test]
  111. public void FixedList32Byte_FixedBytes32Align8ToNativeArrayWorksGeneric()
  112. {
  113. var list = new FixedList<byte,FixedBytes32Align8>();
  114. for(var i = 0; i < 30; ++i)
  115. list.Add((byte)(i * 123 + 234));
  116. using(var array = list.ToNativeArray(Allocator.Temp))
  117. {
  118. for(var i = 0; i < 30; ++i)
  119. Assert.AreEqual((byte)(i * 123 + 234), array[i]);
  120. }
  121. }
  122. [Test]
  123. public void FixedList32Byte_ToNativeArrayWorks()
  124. {
  125. var list = new FixedList32Bytes<byte>();
  126. for(var i = 0; i < 30; ++i)
  127. list.Add((byte)(i * 123 + 234));
  128. using(var array = list.ToNativeArray(Allocator.Temp))
  129. {
  130. for(var i = 0; i < 30; ++i)
  131. Assert.AreEqual((byte)(i * 123 + 234), array[i]);
  132. }
  133. }
  134. [Test]
  135. public void FixedList32Byte_GenericHasExpectedLayout()
  136. {
  137. var actual = new FixedList32Bytes<byte>();
  138. for(var i = 0; i < 30; ++i)
  139. actual.Add((byte)i);
  140. unsafe
  141. {
  142. var e = stackalloc byte[32];
  143. e[0] = (byte)((30 >> 0) & 0xFF);
  144. e[1] = (byte)((30 >> 8) & 0xFF);
  145. for(var i = 0; i < 30; ++i)
  146. {
  147. var s = (byte)i;
  148. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<byte>() + sizeof(byte) * i, &s, sizeof(byte));
  149. }
  150. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 32));
  151. }
  152. }
  153. [Test]
  154. [TestRequiresDotsDebugOrCollectionChecks]
  155. public void FixedList32Byte_GenericHasExpectedCapacity()
  156. {
  157. var list = new FixedList32Bytes<byte>();
  158. var expectedCapacity = list.Capacity;
  159. for(int i = 0; i < expectedCapacity; ++i)
  160. list.Add((byte)i);
  161. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((byte)expectedCapacity); });
  162. }
  163. [Test]
  164. public unsafe void FixedList32Byte_GenericAdd()
  165. {
  166. var list = new FixedList32Bytes<byte>();
  167. var capacity = list.Capacity;
  168. for (var i = 0; i < capacity; ++i)
  169. {
  170. list.Add((byte)i);
  171. Assert.AreEqual(i + 1, list.Length);
  172. Assert.AreEqual(i, list[i]);
  173. }
  174. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  175. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  176. #endif
  177. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  178. }
  179. [Test]
  180. public unsafe void FixedList32Byte_GenericAddRange()
  181. {
  182. var list = new FixedList32Bytes<byte>();
  183. var capacity = list.Capacity;
  184. var items = stackalloc byte[capacity];
  185. for (var i = 0; i < capacity; ++i)
  186. items[i] = (byte)i;
  187. var half = capacity / 2;
  188. list.AddRange(items, half);
  189. Assert.AreEqual(half, list.Length);
  190. for (var i = 0; i < half; ++i)
  191. Assert.AreEqual(i, list[i]);
  192. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  193. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  194. #endif
  195. Assert.AreEqual(half, list.Length); // Verify length didn't change
  196. }
  197. [Test]
  198. public unsafe void FixedList32Byte_GenericAddNoResize()
  199. {
  200. var list = new FixedList32Bytes<byte>();
  201. var capacity = list.Capacity;
  202. for (var i = 0; i < capacity; ++i)
  203. {
  204. list.AddNoResize((byte)i);
  205. Assert.AreEqual(i + 1, list.Length);
  206. Assert.AreEqual(i, list[i]);
  207. }
  208. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  209. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  210. #endif
  211. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  212. }
  213. [Test]
  214. public unsafe void FixedList32Byte_GenericAddRangeNoResize()
  215. {
  216. var list = new FixedList32Bytes<byte>();
  217. var capacity = list.Capacity;
  218. var items = stackalloc byte[capacity];
  219. for (var i = 0; i < capacity; ++i)
  220. items[i] = (byte)i;
  221. var half = capacity / 2;
  222. list.AddRangeNoResize(items, half);
  223. Assert.AreEqual(half, list.Length);
  224. for (var i = 0; i < half; ++i)
  225. Assert.AreEqual(i, list[i]);
  226. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  227. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  228. #endif
  229. Assert.AreEqual(half, list.Length); // Verify length didn't change
  230. }
  231. [Test]
  232. public void FixedList32Byte_GenericAddReplicate()
  233. {
  234. var list = new FixedList32Bytes<byte>();
  235. list.AddReplicate(42, 2);
  236. Assert.AreEqual(2, list.Length);
  237. foreach (var item in list)
  238. Assert.AreEqual(42, item);
  239. list.AddReplicate(42, 3);
  240. Assert.AreEqual(5, list.Length);
  241. foreach (var item in list)
  242. Assert.AreEqual(42, item);
  243. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  244. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  245. #endif
  246. Assert.AreEqual(5, list.Length); // Verify length didn't change
  247. }
  248. [Test]
  249. public void FixedList32Byte_GenericInsertRangeWithBeginEnd()
  250. {
  251. var list = new FixedList32Bytes<byte>() { 0, 3, 4 };
  252. list.InsertRangeWithBeginEnd(1,3);
  253. list[1] = 1;
  254. list[2] = 2;
  255. for(var i = 0; i < 5; ++i)
  256. Assert.AreEqual(i, list[i]);
  257. }
  258. [Test]
  259. public void FixedList32Byte_GenericInsertRange()
  260. {
  261. var list = new FixedList32Bytes<byte>() { 0, 3, 4 };
  262. Assert.AreEqual(3, list.Length);
  263. list.InsertRange(1, 2);
  264. Assert.AreEqual(5, list.Length);
  265. list[1] = 1;
  266. list[2] = 2;
  267. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  268. Assert.AreEqual(5, list.Length);
  269. for(var i = 0; i < 5; ++i)
  270. Assert.AreEqual(i, list[i]);
  271. }
  272. [Test]
  273. public void FixedList32Byte_GenericRemoveRange()
  274. {
  275. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 1, 2 };
  276. list.RemoveRange(1, 2);
  277. for(var i = 0; i < 3; ++i)
  278. Assert.AreEqual(i, list[i]);
  279. }
  280. [Test]
  281. public void FixedList32Byte_GenericInsert()
  282. {
  283. var list = new FixedList32Bytes<byte>() { 0, 3, 4 };
  284. list.Insert(1,1);
  285. list.Insert(2,2);
  286. for(var i = 0; i < 5; ++i)
  287. Assert.AreEqual(i, list[i]);
  288. }
  289. [Test]
  290. public void FixedList32Byte_GenericRemoveAt()
  291. {
  292. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 1, 2 };
  293. list.RemoveAt(1);
  294. list.RemoveAt(1);
  295. for(var i = 0; i < 3; ++i)
  296. Assert.AreEqual(i, list[i]);
  297. }
  298. [Test]
  299. public void FixedList32Byte_GenericRemove()
  300. {
  301. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 1, 2 };
  302. Assert.True(list.Remove((byte)3));
  303. Assert.True(list.Remove((byte)3));
  304. for(var i = 0; i < 3; ++i)
  305. Assert.AreEqual(i, list[i]);
  306. }
  307. [Test]
  308. public void FixedList32Byte_GenericRemoveSwapBack()
  309. {
  310. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 2, 1 };
  311. Assert.True(list.RemoveSwapBack((byte)3));
  312. Assert.True(list.RemoveSwapBack((byte)3));
  313. for(var i = 0; i < 3; ++i)
  314. Assert.AreEqual(i, list[i]);
  315. }
  316. [Test]
  317. public void FixedList32Byte_GenericSort()
  318. {
  319. var list = new FixedList32Bytes<byte>();
  320. for(var i = 0; i < 5; ++i)
  321. list.Add((byte)(4-i));
  322. list.Sort();
  323. for(var i = 0; i < 5; ++i)
  324. Assert.AreEqual(i, list[i]);
  325. }
  326. [Test]
  327. public void FixedList32Byte_GenericSortCustomComparer()
  328. {
  329. var list = new FixedList32Bytes<byte>();
  330. for(var i = 0; i < 5; ++i)
  331. list.Add((byte)(i));
  332. list.Sort(new DescendingComparer<byte>());
  333. for(var i = 0; i < 5; ++i)
  334. Assert.AreEqual(4-i, list[i]);
  335. }
  336. [Test]
  337. public unsafe void FixedList32Byte_IndexOf()
  338. {
  339. var list = new FixedList32Bytes<byte>() { 123, 178 };
  340. bool r0 = false, r1 = false, r2 = false;
  341. GCAllocRecorder.ValidateNoGCAllocs(() =>
  342. {
  343. r0 = -1 != list.IndexOf((byte)145);
  344. r1 = list.Contains((byte)123);
  345. r2 = list.Contains((byte)178);
  346. });
  347. Assert.False(r0);
  348. Assert.True(r1);
  349. Assert.True(r2);
  350. }
  351. public readonly unsafe struct FixedList64Byte_Wrapper
  352. {
  353. readonly FixedList64Bytes<byte> _values;
  354. internal byte* Values => (byte*)_values.Buffer;
  355. public FixedList64Byte_Wrapper(byte x, byte y)
  356. {
  357. _values = new FixedList64Bytes<byte>();
  358. _values.Add(x);
  359. _values.Add(y);
  360. _values.Sort();
  361. }
  362. }
  363. unsafe void FixedList64Byte_ReadonlyWorksFunc1(in FixedList64Byte_Wrapper list, byte* originalValues)
  364. {
  365. byte* values = list.Values;
  366. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  367. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  368. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  369. }
  370. [Test]
  371. public unsafe void FixedList64Byte_ReadonlyWorks()
  372. {
  373. var list = new FixedList64Byte_Wrapper(17, 23);
  374. byte* values = list.Values;
  375. FixedList64Byte_ReadonlyWorksFunc1(list, values);
  376. }
  377. [Test]
  378. public unsafe void FixedList64Byte_FixedBytes64Align8IsAlignedGeneric()
  379. {
  380. var list = new FixedList<byte,FixedBytes64Align8>();
  381. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(byte) - 1)) == 0);
  382. }
  383. [Test]
  384. public void FixedList64Byte_FixedBytes64Align8ToNativeArrayWorksGeneric()
  385. {
  386. var list = new FixedList<byte,FixedBytes64Align8>();
  387. for(var i = 0; i < 62; ++i)
  388. list.Add((byte)(i * 123 + 234));
  389. using(var array = list.ToNativeArray(Allocator.Temp))
  390. {
  391. for(var i = 0; i < 62; ++i)
  392. Assert.AreEqual((byte)(i * 123 + 234), array[i]);
  393. }
  394. }
  395. [Test]
  396. public void FixedList64Byte_ToNativeArrayWorks()
  397. {
  398. var list = new FixedList64Bytes<byte>();
  399. for(var i = 0; i < 62; ++i)
  400. list.Add((byte)(i * 123 + 234));
  401. using(var array = list.ToNativeArray(Allocator.Temp))
  402. {
  403. for(var i = 0; i < 62; ++i)
  404. Assert.AreEqual((byte)(i * 123 + 234), array[i]);
  405. }
  406. }
  407. [Test]
  408. public void FixedList64Byte_GenericHasExpectedLayout()
  409. {
  410. var actual = new FixedList64Bytes<byte>();
  411. for(var i = 0; i < 62; ++i)
  412. actual.Add((byte)i);
  413. unsafe
  414. {
  415. var e = stackalloc byte[64];
  416. e[0] = (byte)((62 >> 0) & 0xFF);
  417. e[1] = (byte)((62 >> 8) & 0xFF);
  418. for(var i = 0; i < 62; ++i)
  419. {
  420. var s = (byte)i;
  421. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<byte>() + sizeof(byte) * i, &s, sizeof(byte));
  422. }
  423. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 64));
  424. }
  425. }
  426. [Test]
  427. [TestRequiresDotsDebugOrCollectionChecks]
  428. public void FixedList64Byte_GenericHasExpectedCapacity()
  429. {
  430. var list = new FixedList64Bytes<byte>();
  431. var expectedCapacity = list.Capacity;
  432. for(int i = 0; i < expectedCapacity; ++i)
  433. list.Add((byte)i);
  434. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((byte)expectedCapacity); });
  435. }
  436. [Test]
  437. public unsafe void FixedList64Byte_GenericAdd()
  438. {
  439. var list = new FixedList64Bytes<byte>();
  440. var capacity = list.Capacity;
  441. for (var i = 0; i < capacity; ++i)
  442. {
  443. list.Add((byte)i);
  444. Assert.AreEqual(i + 1, list.Length);
  445. Assert.AreEqual(i, list[i]);
  446. }
  447. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  448. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  449. #endif
  450. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  451. }
  452. [Test]
  453. public unsafe void FixedList64Byte_GenericAddRange()
  454. {
  455. var list = new FixedList64Bytes<byte>();
  456. var capacity = list.Capacity;
  457. var items = stackalloc byte[capacity];
  458. for (var i = 0; i < capacity; ++i)
  459. items[i] = (byte)i;
  460. var half = capacity / 2;
  461. list.AddRange(items, half);
  462. Assert.AreEqual(half, list.Length);
  463. for (var i = 0; i < half; ++i)
  464. Assert.AreEqual(i, list[i]);
  465. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  466. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  467. #endif
  468. Assert.AreEqual(half, list.Length); // Verify length didn't change
  469. }
  470. [Test]
  471. public unsafe void FixedList64Byte_GenericAddNoResize()
  472. {
  473. var list = new FixedList64Bytes<byte>();
  474. var capacity = list.Capacity;
  475. for (var i = 0; i < capacity; ++i)
  476. {
  477. list.AddNoResize((byte)i);
  478. Assert.AreEqual(i + 1, list.Length);
  479. Assert.AreEqual(i, list[i]);
  480. }
  481. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  482. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  483. #endif
  484. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  485. }
  486. [Test]
  487. public unsafe void FixedList64Byte_GenericAddRangeNoResize()
  488. {
  489. var list = new FixedList64Bytes<byte>();
  490. var capacity = list.Capacity;
  491. var items = stackalloc byte[capacity];
  492. for (var i = 0; i < capacity; ++i)
  493. items[i] = (byte)i;
  494. var half = capacity / 2;
  495. list.AddRangeNoResize(items, half);
  496. Assert.AreEqual(half, list.Length);
  497. for (var i = 0; i < half; ++i)
  498. Assert.AreEqual(i, list[i]);
  499. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  500. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  501. #endif
  502. Assert.AreEqual(half, list.Length); // Verify length didn't change
  503. }
  504. [Test]
  505. public void FixedList64Byte_GenericAddReplicate()
  506. {
  507. var list = new FixedList64Bytes<byte>();
  508. list.AddReplicate(42, 2);
  509. Assert.AreEqual(2, list.Length);
  510. foreach (var item in list)
  511. Assert.AreEqual(42, item);
  512. list.AddReplicate(42, 3);
  513. Assert.AreEqual(5, list.Length);
  514. foreach (var item in list)
  515. Assert.AreEqual(42, item);
  516. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  517. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  518. #endif
  519. Assert.AreEqual(5, list.Length); // Verify length didn't change
  520. }
  521. [Test]
  522. public void FixedList64Byte_GenericInsertRangeWithBeginEnd()
  523. {
  524. var list = new FixedList64Bytes<byte>() { 0, 3, 4 };
  525. list.InsertRangeWithBeginEnd(1,3);
  526. list[1] = 1;
  527. list[2] = 2;
  528. for(var i = 0; i < 5; ++i)
  529. Assert.AreEqual(i, list[i]);
  530. }
  531. [Test]
  532. public void FixedList64Byte_GenericInsertRange()
  533. {
  534. var list = new FixedList64Bytes<byte>() { 0, 3, 4 };
  535. Assert.AreEqual(3, list.Length);
  536. list.InsertRange(1, 2);
  537. Assert.AreEqual(5, list.Length);
  538. list[1] = 1;
  539. list[2] = 2;
  540. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  541. Assert.AreEqual(5, list.Length);
  542. for(var i = 0; i < 5; ++i)
  543. Assert.AreEqual(i, list[i]);
  544. }
  545. [Test]
  546. public void FixedList64Byte_GenericRemoveRange()
  547. {
  548. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 1, 2 };
  549. list.RemoveRange(1, 2);
  550. for(var i = 0; i < 3; ++i)
  551. Assert.AreEqual(i, list[i]);
  552. }
  553. [Test]
  554. public void FixedList64Byte_GenericInsert()
  555. {
  556. var list = new FixedList64Bytes<byte>() { 0, 3, 4 };
  557. list.Insert(1,1);
  558. list.Insert(2,2);
  559. for(var i = 0; i < 5; ++i)
  560. Assert.AreEqual(i, list[i]);
  561. }
  562. [Test]
  563. public void FixedList64Byte_GenericRemoveAt()
  564. {
  565. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 1, 2 };
  566. list.RemoveAt(1);
  567. list.RemoveAt(1);
  568. for(var i = 0; i < 3; ++i)
  569. Assert.AreEqual(i, list[i]);
  570. }
  571. [Test]
  572. public void FixedList64Byte_GenericRemove()
  573. {
  574. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 1, 2 };
  575. Assert.True(list.Remove((byte)3));
  576. Assert.True(list.Remove((byte)3));
  577. for(var i = 0; i < 3; ++i)
  578. Assert.AreEqual(i, list[i]);
  579. }
  580. [Test]
  581. public void FixedList64Byte_GenericRemoveSwapBack()
  582. {
  583. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 2, 1 };
  584. Assert.True(list.RemoveSwapBack((byte)3));
  585. Assert.True(list.RemoveSwapBack((byte)3));
  586. for(var i = 0; i < 3; ++i)
  587. Assert.AreEqual(i, list[i]);
  588. }
  589. [Test]
  590. public void FixedList64Byte_GenericSort()
  591. {
  592. var list = new FixedList64Bytes<byte>();
  593. for(var i = 0; i < 5; ++i)
  594. list.Add((byte)(4-i));
  595. list.Sort();
  596. for(var i = 0; i < 5; ++i)
  597. Assert.AreEqual(i, list[i]);
  598. }
  599. [Test]
  600. public void FixedList64Byte_GenericSortCustomComparer()
  601. {
  602. var list = new FixedList64Bytes<byte>();
  603. for(var i = 0; i < 5; ++i)
  604. list.Add((byte)(i));
  605. list.Sort(new DescendingComparer<byte>());
  606. for(var i = 0; i < 5; ++i)
  607. Assert.AreEqual(4-i, list[i]);
  608. }
  609. [Test]
  610. public unsafe void FixedList64Byte_IndexOf()
  611. {
  612. var list = new FixedList64Bytes<byte>() { 123, 178 };
  613. bool r0 = false, r1 = false, r2 = false;
  614. GCAllocRecorder.ValidateNoGCAllocs(() =>
  615. {
  616. r0 = -1 != list.IndexOf((byte)145);
  617. r1 = list.Contains((byte)123);
  618. r2 = list.Contains((byte)178);
  619. });
  620. Assert.False(r0);
  621. Assert.True(r1);
  622. Assert.True(r2);
  623. }
  624. public readonly unsafe struct FixedList128Byte_Wrapper
  625. {
  626. readonly FixedList128Bytes<byte> _values;
  627. internal byte* Values => (byte*)_values.Buffer;
  628. public FixedList128Byte_Wrapper(byte x, byte y)
  629. {
  630. _values = new FixedList128Bytes<byte>();
  631. _values.Add(x);
  632. _values.Add(y);
  633. _values.Sort();
  634. }
  635. }
  636. unsafe void FixedList128Byte_ReadonlyWorksFunc1(in FixedList128Byte_Wrapper list, byte* originalValues)
  637. {
  638. byte* values = list.Values;
  639. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  640. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  641. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  642. }
  643. [Test]
  644. public unsafe void FixedList128Byte_ReadonlyWorks()
  645. {
  646. var list = new FixedList128Byte_Wrapper(17, 23);
  647. byte* values = list.Values;
  648. FixedList128Byte_ReadonlyWorksFunc1(list, values);
  649. }
  650. [Test]
  651. public unsafe void FixedList128Byte_FixedBytes128Align8IsAlignedGeneric()
  652. {
  653. var list = new FixedList<byte,FixedBytes128Align8>();
  654. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(byte) - 1)) == 0);
  655. }
  656. [Test]
  657. public void FixedList128Byte_FixedBytes128Align8ToNativeArrayWorksGeneric()
  658. {
  659. var list = new FixedList<byte,FixedBytes128Align8>();
  660. for(var i = 0; i < 126; ++i)
  661. list.Add((byte)(i * 123 + 234));
  662. using(var array = list.ToNativeArray(Allocator.Temp))
  663. {
  664. for(var i = 0; i < 126; ++i)
  665. Assert.AreEqual((byte)(i * 123 + 234), array[i]);
  666. }
  667. }
  668. [Test]
  669. public void FixedList128Byte_ToNativeArrayWorks()
  670. {
  671. var list = new FixedList128Bytes<byte>();
  672. for(var i = 0; i < 126; ++i)
  673. list.Add((byte)(i * 123 + 234));
  674. using(var array = list.ToNativeArray(Allocator.Temp))
  675. {
  676. for(var i = 0; i < 126; ++i)
  677. Assert.AreEqual((byte)(i * 123 + 234), array[i]);
  678. }
  679. }
  680. [Test]
  681. public void FixedList128Byte_GenericHasExpectedLayout()
  682. {
  683. var actual = new FixedList128Bytes<byte>();
  684. for(var i = 0; i < 126; ++i)
  685. actual.Add((byte)i);
  686. unsafe
  687. {
  688. var e = stackalloc byte[128];
  689. e[0] = (byte)((126 >> 0) & 0xFF);
  690. e[1] = (byte)((126 >> 8) & 0xFF);
  691. for(var i = 0; i < 126; ++i)
  692. {
  693. var s = (byte)i;
  694. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<byte>() + sizeof(byte) * i, &s, sizeof(byte));
  695. }
  696. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 128));
  697. }
  698. }
  699. [Test]
  700. [TestRequiresDotsDebugOrCollectionChecks]
  701. public void FixedList128Byte_GenericHasExpectedCapacity()
  702. {
  703. var list = new FixedList128Bytes<byte>();
  704. var expectedCapacity = list.Capacity;
  705. for(int i = 0; i < expectedCapacity; ++i)
  706. list.Add((byte)i);
  707. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((byte)expectedCapacity); });
  708. }
  709. [Test]
  710. public unsafe void FixedList128Byte_GenericAdd()
  711. {
  712. var list = new FixedList128Bytes<byte>();
  713. var capacity = list.Capacity;
  714. for (var i = 0; i < capacity; ++i)
  715. {
  716. list.Add((byte)i);
  717. Assert.AreEqual(i + 1, list.Length);
  718. Assert.AreEqual(i, list[i]);
  719. }
  720. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  721. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  722. #endif
  723. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  724. }
  725. [Test]
  726. public unsafe void FixedList128Byte_GenericAddRange()
  727. {
  728. var list = new FixedList128Bytes<byte>();
  729. var capacity = list.Capacity;
  730. var items = stackalloc byte[capacity];
  731. for (var i = 0; i < capacity; ++i)
  732. items[i] = (byte)i;
  733. var half = capacity / 2;
  734. list.AddRange(items, half);
  735. Assert.AreEqual(half, list.Length);
  736. for (var i = 0; i < half; ++i)
  737. Assert.AreEqual(i, list[i]);
  738. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  739. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  740. #endif
  741. Assert.AreEqual(half, list.Length); // Verify length didn't change
  742. }
  743. [Test]
  744. public unsafe void FixedList128Byte_GenericAddNoResize()
  745. {
  746. var list = new FixedList128Bytes<byte>();
  747. var capacity = list.Capacity;
  748. for (var i = 0; i < capacity; ++i)
  749. {
  750. list.AddNoResize((byte)i);
  751. Assert.AreEqual(i + 1, list.Length);
  752. Assert.AreEqual(i, list[i]);
  753. }
  754. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  755. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  756. #endif
  757. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  758. }
  759. [Test]
  760. public unsafe void FixedList128Byte_GenericAddRangeNoResize()
  761. {
  762. var list = new FixedList128Bytes<byte>();
  763. var capacity = list.Capacity;
  764. var items = stackalloc byte[capacity];
  765. for (var i = 0; i < capacity; ++i)
  766. items[i] = (byte)i;
  767. var half = capacity / 2;
  768. list.AddRangeNoResize(items, half);
  769. Assert.AreEqual(half, list.Length);
  770. for (var i = 0; i < half; ++i)
  771. Assert.AreEqual(i, list[i]);
  772. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  773. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  774. #endif
  775. Assert.AreEqual(half, list.Length); // Verify length didn't change
  776. }
  777. [Test]
  778. public void FixedList128Byte_GenericAddReplicate()
  779. {
  780. var list = new FixedList128Bytes<byte>();
  781. list.AddReplicate(42, 2);
  782. Assert.AreEqual(2, list.Length);
  783. foreach (var item in list)
  784. Assert.AreEqual(42, item);
  785. list.AddReplicate(42, 3);
  786. Assert.AreEqual(5, list.Length);
  787. foreach (var item in list)
  788. Assert.AreEqual(42, item);
  789. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  790. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  791. #endif
  792. Assert.AreEqual(5, list.Length); // Verify length didn't change
  793. }
  794. [Test]
  795. public void FixedList128Byte_GenericInsertRangeWithBeginEnd()
  796. {
  797. var list = new FixedList128Bytes<byte>() { 0, 3, 4 };
  798. list.InsertRangeWithBeginEnd(1,3);
  799. list[1] = 1;
  800. list[2] = 2;
  801. for(var i = 0; i < 5; ++i)
  802. Assert.AreEqual(i, list[i]);
  803. }
  804. [Test]
  805. public void FixedList128Byte_GenericInsertRange()
  806. {
  807. var list = new FixedList128Bytes<byte>() { 0, 3, 4 };
  808. Assert.AreEqual(3, list.Length);
  809. list.InsertRange(1, 2);
  810. Assert.AreEqual(5, list.Length);
  811. list[1] = 1;
  812. list[2] = 2;
  813. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  814. Assert.AreEqual(5, list.Length);
  815. for(var i = 0; i < 5; ++i)
  816. Assert.AreEqual(i, list[i]);
  817. }
  818. [Test]
  819. public void FixedList128Byte_GenericRemoveRange()
  820. {
  821. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 1, 2 };
  822. list.RemoveRange(1, 2);
  823. for(var i = 0; i < 3; ++i)
  824. Assert.AreEqual(i, list[i]);
  825. }
  826. [Test]
  827. public void FixedList128Byte_GenericInsert()
  828. {
  829. var list = new FixedList128Bytes<byte>() { 0, 3, 4 };
  830. list.Insert(1,1);
  831. list.Insert(2,2);
  832. for(var i = 0; i < 5; ++i)
  833. Assert.AreEqual(i, list[i]);
  834. }
  835. [Test]
  836. public void FixedList128Byte_GenericRemoveAt()
  837. {
  838. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 1, 2 };
  839. list.RemoveAt(1);
  840. list.RemoveAt(1);
  841. for(var i = 0; i < 3; ++i)
  842. Assert.AreEqual(i, list[i]);
  843. }
  844. [Test]
  845. public void FixedList128Byte_GenericRemove()
  846. {
  847. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 1, 2 };
  848. Assert.True(list.Remove((byte)3));
  849. Assert.True(list.Remove((byte)3));
  850. for(var i = 0; i < 3; ++i)
  851. Assert.AreEqual(i, list[i]);
  852. }
  853. [Test]
  854. public void FixedList128Byte_GenericRemoveSwapBack()
  855. {
  856. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 2, 1 };
  857. Assert.True(list.RemoveSwapBack((byte)3));
  858. Assert.True(list.RemoveSwapBack((byte)3));
  859. for(var i = 0; i < 3; ++i)
  860. Assert.AreEqual(i, list[i]);
  861. }
  862. [Test]
  863. public void FixedList128Byte_GenericSort()
  864. {
  865. var list = new FixedList128Bytes<byte>();
  866. for(var i = 0; i < 5; ++i)
  867. list.Add((byte)(4-i));
  868. list.Sort();
  869. for(var i = 0; i < 5; ++i)
  870. Assert.AreEqual(i, list[i]);
  871. }
  872. [Test]
  873. public void FixedList128Byte_GenericSortCustomComparer()
  874. {
  875. var list = new FixedList128Bytes<byte>();
  876. for(var i = 0; i < 5; ++i)
  877. list.Add((byte)(i));
  878. list.Sort(new DescendingComparer<byte>());
  879. for(var i = 0; i < 5; ++i)
  880. Assert.AreEqual(4-i, list[i]);
  881. }
  882. [Test]
  883. public unsafe void FixedList128Byte_IndexOf()
  884. {
  885. var list = new FixedList128Bytes<byte>() { 123, 178 };
  886. bool r0 = false, r1 = false, r2 = false;
  887. GCAllocRecorder.ValidateNoGCAllocs(() =>
  888. {
  889. r0 = -1 != list.IndexOf((byte)145);
  890. r1 = list.Contains((byte)123);
  891. r2 = list.Contains((byte)178);
  892. });
  893. Assert.False(r0);
  894. Assert.True(r1);
  895. Assert.True(r2);
  896. }
  897. [Test]
  898. public void FixedList32Byte_HasExpectedLayout()
  899. {
  900. var actual = new FixedList32Bytes<byte>();
  901. for(var i = 0; i < 30; ++i)
  902. actual.Add((byte)i);
  903. unsafe
  904. {
  905. var e = stackalloc byte[32];
  906. e[0] = (byte)((30 >> 0) & 0xFF);
  907. e[1] = (byte)((30 >> 8) & 0xFF);
  908. for(var i = 0; i < 30; ++i)
  909. {
  910. var s = (byte)i;
  911. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<byte>() + sizeof(byte) * i, &s, sizeof(byte));
  912. }
  913. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 32));
  914. }
  915. }
  916. [Test]
  917. [TestRequiresDotsDebugOrCollectionChecks]
  918. public void FixedList32Byte_HasExpectedCapacity()
  919. {
  920. var list = new FixedList32Bytes<byte>();
  921. var expectedCapacity = list.Capacity;
  922. for(int i = 0; i < expectedCapacity; ++i)
  923. list.Add((byte)i);
  924. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((byte)expectedCapacity); });
  925. }
  926. class ScriptableObjectFixedList32Byte_ : UnityEngine.ScriptableObject
  927. {
  928. public FixedList32Bytes<byte> List;
  929. }
  930. [Test]
  931. public void FixedList32Byte_Serializes()
  932. {
  933. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList32Byte_ >();
  934. for(int i = 0; i < a.List.Capacity; ++i)
  935. a.List.Add((byte)i);
  936. var b = UnityEngine.Object.Instantiate(a);
  937. CollectionAssert.AreEqual(a.List, b.List);
  938. }
  939. [Test]
  940. public unsafe void FixedList32Byte_Add()
  941. {
  942. var list = new FixedList32Bytes<byte>();
  943. var capacity = list.Capacity;
  944. for (var i = 0; i < capacity; ++i)
  945. {
  946. list.Add((byte)i);
  947. Assert.AreEqual(i + 1, list.Length);
  948. Assert.AreEqual(i, list[i]);
  949. }
  950. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  951. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  952. #endif
  953. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  954. }
  955. [Test]
  956. public unsafe void FixedList32Byte_AddRange()
  957. {
  958. var list = new FixedList32Bytes<byte>();
  959. var capacity = list.Capacity;
  960. var items = stackalloc byte[capacity];
  961. for (var i = 0; i < capacity; ++i)
  962. items[i] = (byte)i;
  963. var half = capacity / 2;
  964. list.AddRange(items, half);
  965. Assert.AreEqual(half, list.Length);
  966. for (var i = 0; i < half; ++i)
  967. Assert.AreEqual(i, list[i]);
  968. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  969. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  970. #endif
  971. Assert.AreEqual(half, list.Length); // Verify length didn't change
  972. }
  973. [Test]
  974. public unsafe void FixedList32Byte_AddNoResize()
  975. {
  976. var list = new FixedList32Bytes<byte>();
  977. var capacity = list.Capacity;
  978. for (var i = 0; i < capacity; ++i)
  979. {
  980. list.AddNoResize((byte)i);
  981. Assert.AreEqual(i + 1, list.Length);
  982. Assert.AreEqual(i, list[i]);
  983. }
  984. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  985. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  986. #endif
  987. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  988. }
  989. [Test]
  990. public unsafe void FixedList32Byte_AddRangeNoResize()
  991. {
  992. var list = new FixedList32Bytes<byte>();
  993. var capacity = list.Capacity;
  994. var items = stackalloc byte[capacity];
  995. for (var i = 0; i < capacity; ++i)
  996. items[i] = (byte)i;
  997. var half = capacity / 2;
  998. list.AddRangeNoResize(items, half);
  999. Assert.AreEqual(half, list.Length);
  1000. for (var i = 0; i < half; ++i)
  1001. Assert.AreEqual(i, list[i]);
  1002. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1003. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  1004. #endif
  1005. Assert.AreEqual(half, list.Length); // Verify length didn't change
  1006. }
  1007. [Test]
  1008. public void FixedList32Byte_AddReplicate()
  1009. {
  1010. var list = new FixedList32Bytes<byte>();
  1011. list.AddReplicate(42, 2);
  1012. Assert.AreEqual(2, list.Length);
  1013. foreach (var item in list)
  1014. Assert.AreEqual(42, item);
  1015. list.AddReplicate(42, 3);
  1016. Assert.AreEqual(5, list.Length);
  1017. foreach (var item in list)
  1018. Assert.AreEqual(42, item);
  1019. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1020. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  1021. #endif
  1022. Assert.AreEqual(5, list.Length); // Verify length didn't change
  1023. }
  1024. [Test]
  1025. public void FixedList32Byte_InsertRangeWithBeginEnd()
  1026. {
  1027. var list = new FixedList32Bytes<byte>() { 0, 3, 4 };
  1028. list.InsertRangeWithBeginEnd(1,3);
  1029. list[1] = 1;
  1030. list[2] = 2;
  1031. for(var i = 0; i < 5; ++i)
  1032. Assert.AreEqual(i, list[i]);
  1033. }
  1034. [Test]
  1035. public void FixedList32Byte_InsertRange()
  1036. {
  1037. var list = new FixedList32Bytes<byte>() { 0, 3, 4 };
  1038. Assert.AreEqual(3, list.Length);
  1039. list.InsertRange(1, 2);
  1040. Assert.AreEqual(5, list.Length);
  1041. list[1] = 1;
  1042. list[2] = 2;
  1043. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  1044. Assert.AreEqual(5, list.Length);
  1045. for(var i = 0; i < 5; ++i)
  1046. Assert.AreEqual(i, list[i]);
  1047. }
  1048. private static void Expected(ref FixedList32Bytes<byte> container, int expectedLength, int[] expected)
  1049. {
  1050. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  1051. Assert.AreEqual(container.Length, expectedLength);
  1052. for (var i = 0; i < container.Length; ++i)
  1053. {
  1054. Assert.AreEqual(expected[i], container[i]);
  1055. }
  1056. }
  1057. [Test]
  1058. public void FixedList32Byte_RemoveAt()
  1059. {
  1060. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 1, 2 };
  1061. list.RemoveAt(1);
  1062. list.RemoveAt(1);
  1063. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1064. }
  1065. [Test]
  1066. public void FixedList32Byte_Remove()
  1067. {
  1068. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 1, 2 };
  1069. Assert.True(list.Remove((byte)3));
  1070. Assert.True(list.Remove((byte)3));
  1071. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1072. }
  1073. [Test]
  1074. public void FixedList32Byte_RemoveSwapBack()
  1075. {
  1076. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 2, 1 };
  1077. Assert.True(list.RemoveSwapBack((byte)3));
  1078. Assert.True(list.RemoveSwapBack((byte)3));
  1079. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1080. }
  1081. [Test]
  1082. public void FixedList32Byte_RemoveRange()
  1083. {
  1084. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 1, 2 };
  1085. list.RemoveRange(1, 2);
  1086. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1087. }
  1088. [Test]
  1089. public void FixedList32Byte_RemoveAtSwapBack()
  1090. {
  1091. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 1, 2 };
  1092. list.RemoveAtSwapBack(1);
  1093. list.RemoveAtSwapBack(1);
  1094. Expected(ref list, 3, new int[] { 0, 1, 3 });
  1095. }
  1096. [Test]
  1097. public void FixedList32Byte_RemoveRangeSwapBack()
  1098. {
  1099. var list = new FixedList32Bytes<byte>() { 0, 3, 3, 1, 2 };
  1100. list.RemoveRangeSwapBack(1, 2);
  1101. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1102. }
  1103. [Test]
  1104. public void FixedList32Byte_Insert()
  1105. {
  1106. var list = new FixedList32Bytes<byte>() { 0, 3, 4 };
  1107. list.Insert(1,1);
  1108. list.Insert(2,2);
  1109. for(var i = 0; i < 5; ++i)
  1110. Assert.AreEqual(i, list[i]);
  1111. }
  1112. [Test]
  1113. public void FixedList32Byte_Sort()
  1114. {
  1115. var list = new FixedList32Bytes<byte>();
  1116. for(var i = 0; i < 5; ++i)
  1117. list.Add((byte)(4-i));
  1118. list.Sort();
  1119. for(var i = 0; i < 5; ++i)
  1120. Assert.AreEqual(i, list[i]);
  1121. }
  1122. [Test]
  1123. public void FixedList32Byte_To_FixedList64Byte()
  1124. {
  1125. var a = new FixedList32Bytes<byte>();
  1126. for(var i = 0; i < 30; ++i)
  1127. a.Add((byte)i);
  1128. var b = new FixedList64Bytes<byte>(a);
  1129. for(var i = 0; i < 30; ++i)
  1130. Assert.AreEqual((byte)i, b[i]);
  1131. }
  1132. [Test]
  1133. public void FixedList32Byte_To_FixedList128Byte()
  1134. {
  1135. var a = new FixedList32Bytes<byte>();
  1136. for(var i = 0; i < 30; ++i)
  1137. a.Add((byte)i);
  1138. var b = new FixedList128Bytes<byte>(a);
  1139. for(var i = 0; i < 30; ++i)
  1140. Assert.AreEqual((byte)i, b[i]);
  1141. }
  1142. [Test]
  1143. public void FixedList64Byte_HasExpectedLayout()
  1144. {
  1145. var actual = new FixedList64Bytes<byte>();
  1146. for(var i = 0; i < 62; ++i)
  1147. actual.Add((byte)i);
  1148. unsafe
  1149. {
  1150. var e = stackalloc byte[64];
  1151. e[0] = (byte)((62 >> 0) & 0xFF);
  1152. e[1] = (byte)((62 >> 8) & 0xFF);
  1153. for(var i = 0; i < 62; ++i)
  1154. {
  1155. var s = (byte)i;
  1156. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<byte>() + sizeof(byte) * i, &s, sizeof(byte));
  1157. }
  1158. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 64));
  1159. }
  1160. }
  1161. [Test]
  1162. [TestRequiresDotsDebugOrCollectionChecks]
  1163. public void FixedList64Byte_HasExpectedCapacity()
  1164. {
  1165. var list = new FixedList64Bytes<byte>();
  1166. var expectedCapacity = list.Capacity;
  1167. for(int i = 0; i < expectedCapacity; ++i)
  1168. list.Add((byte)i);
  1169. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((byte)expectedCapacity); });
  1170. }
  1171. class ScriptableObjectFixedList64Byte_ : UnityEngine.ScriptableObject
  1172. {
  1173. public FixedList64Bytes<byte> List;
  1174. }
  1175. [Test]
  1176. public void FixedList64Byte_Serializes()
  1177. {
  1178. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList64Byte_ >();
  1179. for(int i = 0; i < a.List.Capacity; ++i)
  1180. a.List.Add((byte)i);
  1181. var b = UnityEngine.Object.Instantiate(a);
  1182. CollectionAssert.AreEqual(a.List, b.List);
  1183. }
  1184. [Test]
  1185. public unsafe void FixedList64Byte_Add()
  1186. {
  1187. var list = new FixedList64Bytes<byte>();
  1188. var capacity = list.Capacity;
  1189. for (var i = 0; i < capacity; ++i)
  1190. {
  1191. list.Add((byte)i);
  1192. Assert.AreEqual(i + 1, list.Length);
  1193. Assert.AreEqual(i, list[i]);
  1194. }
  1195. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1196. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  1197. #endif
  1198. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  1199. }
  1200. [Test]
  1201. public unsafe void FixedList64Byte_AddRange()
  1202. {
  1203. var list = new FixedList64Bytes<byte>();
  1204. var capacity = list.Capacity;
  1205. var items = stackalloc byte[capacity];
  1206. for (var i = 0; i < capacity; ++i)
  1207. items[i] = (byte)i;
  1208. var half = capacity / 2;
  1209. list.AddRange(items, half);
  1210. Assert.AreEqual(half, list.Length);
  1211. for (var i = 0; i < half; ++i)
  1212. Assert.AreEqual(i, list[i]);
  1213. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1214. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  1215. #endif
  1216. Assert.AreEqual(half, list.Length); // Verify length didn't change
  1217. }
  1218. [Test]
  1219. public unsafe void FixedList64Byte_AddNoResize()
  1220. {
  1221. var list = new FixedList64Bytes<byte>();
  1222. var capacity = list.Capacity;
  1223. for (var i = 0; i < capacity; ++i)
  1224. {
  1225. list.AddNoResize((byte)i);
  1226. Assert.AreEqual(i + 1, list.Length);
  1227. Assert.AreEqual(i, list[i]);
  1228. }
  1229. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1230. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  1231. #endif
  1232. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  1233. }
  1234. [Test]
  1235. public unsafe void FixedList64Byte_AddRangeNoResize()
  1236. {
  1237. var list = new FixedList64Bytes<byte>();
  1238. var capacity = list.Capacity;
  1239. var items = stackalloc byte[capacity];
  1240. for (var i = 0; i < capacity; ++i)
  1241. items[i] = (byte)i;
  1242. var half = capacity / 2;
  1243. list.AddRangeNoResize(items, half);
  1244. Assert.AreEqual(half, list.Length);
  1245. for (var i = 0; i < half; ++i)
  1246. Assert.AreEqual(i, list[i]);
  1247. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1248. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  1249. #endif
  1250. Assert.AreEqual(half, list.Length); // Verify length didn't change
  1251. }
  1252. [Test]
  1253. public void FixedList64Byte_AddReplicate()
  1254. {
  1255. var list = new FixedList64Bytes<byte>();
  1256. list.AddReplicate(42, 2);
  1257. Assert.AreEqual(2, list.Length);
  1258. foreach (var item in list)
  1259. Assert.AreEqual(42, item);
  1260. list.AddReplicate(42, 3);
  1261. Assert.AreEqual(5, list.Length);
  1262. foreach (var item in list)
  1263. Assert.AreEqual(42, item);
  1264. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1265. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  1266. #endif
  1267. Assert.AreEqual(5, list.Length); // Verify length didn't change
  1268. }
  1269. [Test]
  1270. public void FixedList64Byte_InsertRangeWithBeginEnd()
  1271. {
  1272. var list = new FixedList64Bytes<byte>() { 0, 3, 4 };
  1273. list.InsertRangeWithBeginEnd(1,3);
  1274. list[1] = 1;
  1275. list[2] = 2;
  1276. for(var i = 0; i < 5; ++i)
  1277. Assert.AreEqual(i, list[i]);
  1278. }
  1279. [Test]
  1280. public void FixedList64Byte_InsertRange()
  1281. {
  1282. var list = new FixedList64Bytes<byte>() { 0, 3, 4 };
  1283. Assert.AreEqual(3, list.Length);
  1284. list.InsertRange(1, 2);
  1285. Assert.AreEqual(5, list.Length);
  1286. list[1] = 1;
  1287. list[2] = 2;
  1288. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  1289. Assert.AreEqual(5, list.Length);
  1290. for(var i = 0; i < 5; ++i)
  1291. Assert.AreEqual(i, list[i]);
  1292. }
  1293. private static void Expected(ref FixedList64Bytes<byte> container, int expectedLength, int[] expected)
  1294. {
  1295. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  1296. Assert.AreEqual(container.Length, expectedLength);
  1297. for (var i = 0; i < container.Length; ++i)
  1298. {
  1299. Assert.AreEqual(expected[i], container[i]);
  1300. }
  1301. }
  1302. [Test]
  1303. public void FixedList64Byte_RemoveAt()
  1304. {
  1305. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 1, 2 };
  1306. list.RemoveAt(1);
  1307. list.RemoveAt(1);
  1308. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1309. }
  1310. [Test]
  1311. public void FixedList64Byte_Remove()
  1312. {
  1313. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 1, 2 };
  1314. Assert.True(list.Remove((byte)3));
  1315. Assert.True(list.Remove((byte)3));
  1316. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1317. }
  1318. [Test]
  1319. public void FixedList64Byte_RemoveSwapBack()
  1320. {
  1321. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 2, 1 };
  1322. Assert.True(list.RemoveSwapBack((byte)3));
  1323. Assert.True(list.RemoveSwapBack((byte)3));
  1324. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1325. }
  1326. [Test]
  1327. public void FixedList64Byte_RemoveRange()
  1328. {
  1329. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 1, 2 };
  1330. list.RemoveRange(1, 2);
  1331. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1332. }
  1333. [Test]
  1334. public void FixedList64Byte_RemoveAtSwapBack()
  1335. {
  1336. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 1, 2 };
  1337. list.RemoveAtSwapBack(1);
  1338. list.RemoveAtSwapBack(1);
  1339. Expected(ref list, 3, new int[] { 0, 1, 3 });
  1340. }
  1341. [Test]
  1342. public void FixedList64Byte_RemoveRangeSwapBack()
  1343. {
  1344. var list = new FixedList64Bytes<byte>() { 0, 3, 3, 1, 2 };
  1345. list.RemoveRangeSwapBack(1, 2);
  1346. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1347. }
  1348. [Test]
  1349. public void FixedList64Byte_Insert()
  1350. {
  1351. var list = new FixedList64Bytes<byte>() { 0, 3, 4 };
  1352. list.Insert(1,1);
  1353. list.Insert(2,2);
  1354. for(var i = 0; i < 5; ++i)
  1355. Assert.AreEqual(i, list[i]);
  1356. }
  1357. [Test]
  1358. public void FixedList64Byte_Sort()
  1359. {
  1360. var list = new FixedList64Bytes<byte>();
  1361. for(var i = 0; i < 5; ++i)
  1362. list.Add((byte)(4-i));
  1363. list.Sort();
  1364. for(var i = 0; i < 5; ++i)
  1365. Assert.AreEqual(i, list[i]);
  1366. }
  1367. [Test]
  1368. public void FixedList64Byte_To_FixedList32Byte()
  1369. {
  1370. var a = new FixedList64Bytes<byte>();
  1371. for(var i = 0; i < 62; ++i)
  1372. a.Add((byte)i);
  1373. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1374. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList32Bytes<byte>(a); } );
  1375. #endif
  1376. }
  1377. [Test]
  1378. public void FixedList64Byte_To_FixedList128Byte()
  1379. {
  1380. var a = new FixedList64Bytes<byte>();
  1381. for(var i = 0; i < 62; ++i)
  1382. a.Add((byte)i);
  1383. var b = new FixedList128Bytes<byte>(a);
  1384. for(var i = 0; i < 62; ++i)
  1385. Assert.AreEqual((byte)i, b[i]);
  1386. }
  1387. [Test]
  1388. public void FixedList128Byte_HasExpectedLayout()
  1389. {
  1390. var actual = new FixedList128Bytes<byte>();
  1391. for(var i = 0; i < 126; ++i)
  1392. actual.Add((byte)i);
  1393. unsafe
  1394. {
  1395. var e = stackalloc byte[128];
  1396. e[0] = (byte)((126 >> 0) & 0xFF);
  1397. e[1] = (byte)((126 >> 8) & 0xFF);
  1398. for(var i = 0; i < 126; ++i)
  1399. {
  1400. var s = (byte)i;
  1401. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<byte>() + sizeof(byte) * i, &s, sizeof(byte));
  1402. }
  1403. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 128));
  1404. }
  1405. }
  1406. [Test]
  1407. [TestRequiresDotsDebugOrCollectionChecks]
  1408. public void FixedList128Byte_HasExpectedCapacity()
  1409. {
  1410. var list = new FixedList128Bytes<byte>();
  1411. var expectedCapacity = list.Capacity;
  1412. for(int i = 0; i < expectedCapacity; ++i)
  1413. list.Add((byte)i);
  1414. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((byte)expectedCapacity); });
  1415. }
  1416. class ScriptableObjectFixedList128Byte_ : UnityEngine.ScriptableObject
  1417. {
  1418. public FixedList128Bytes<byte> List;
  1419. }
  1420. [Test]
  1421. public void FixedList128Byte_Serializes()
  1422. {
  1423. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList128Byte_ >();
  1424. for(int i = 0; i < a.List.Capacity; ++i)
  1425. a.List.Add((byte)i);
  1426. var b = UnityEngine.Object.Instantiate(a);
  1427. CollectionAssert.AreEqual(a.List, b.List);
  1428. }
  1429. [Test]
  1430. public unsafe void FixedList128Byte_Add()
  1431. {
  1432. var list = new FixedList128Bytes<byte>();
  1433. var capacity = list.Capacity;
  1434. for (var i = 0; i < capacity; ++i)
  1435. {
  1436. list.Add((byte)i);
  1437. Assert.AreEqual(i + 1, list.Length);
  1438. Assert.AreEqual(i, list[i]);
  1439. }
  1440. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1441. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  1442. #endif
  1443. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  1444. }
  1445. [Test]
  1446. public unsafe void FixedList128Byte_AddRange()
  1447. {
  1448. var list = new FixedList128Bytes<byte>();
  1449. var capacity = list.Capacity;
  1450. var items = stackalloc byte[capacity];
  1451. for (var i = 0; i < capacity; ++i)
  1452. items[i] = (byte)i;
  1453. var half = capacity / 2;
  1454. list.AddRange(items, half);
  1455. Assert.AreEqual(half, list.Length);
  1456. for (var i = 0; i < half; ++i)
  1457. Assert.AreEqual(i, list[i]);
  1458. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1459. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  1460. #endif
  1461. Assert.AreEqual(half, list.Length); // Verify length didn't change
  1462. }
  1463. [Test]
  1464. public unsafe void FixedList128Byte_AddNoResize()
  1465. {
  1466. var list = new FixedList128Bytes<byte>();
  1467. var capacity = list.Capacity;
  1468. for (var i = 0; i < capacity; ++i)
  1469. {
  1470. list.AddNoResize((byte)i);
  1471. Assert.AreEqual(i + 1, list.Length);
  1472. Assert.AreEqual(i, list[i]);
  1473. }
  1474. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1475. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  1476. #endif
  1477. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  1478. }
  1479. [Test]
  1480. public unsafe void FixedList128Byte_AddRangeNoResize()
  1481. {
  1482. var list = new FixedList128Bytes<byte>();
  1483. var capacity = list.Capacity;
  1484. var items = stackalloc byte[capacity];
  1485. for (var i = 0; i < capacity; ++i)
  1486. items[i] = (byte)i;
  1487. var half = capacity / 2;
  1488. list.AddRangeNoResize(items, half);
  1489. Assert.AreEqual(half, list.Length);
  1490. for (var i = 0; i < half; ++i)
  1491. Assert.AreEqual(i, list[i]);
  1492. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1493. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  1494. #endif
  1495. Assert.AreEqual(half, list.Length); // Verify length didn't change
  1496. }
  1497. [Test]
  1498. public void FixedList128Byte_AddReplicate()
  1499. {
  1500. var list = new FixedList128Bytes<byte>();
  1501. list.AddReplicate(42, 2);
  1502. Assert.AreEqual(2, list.Length);
  1503. foreach (var item in list)
  1504. Assert.AreEqual(42, item);
  1505. list.AddReplicate(42, 3);
  1506. Assert.AreEqual(5, list.Length);
  1507. foreach (var item in list)
  1508. Assert.AreEqual(42, item);
  1509. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1510. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  1511. #endif
  1512. Assert.AreEqual(5, list.Length); // Verify length didn't change
  1513. }
  1514. [Test]
  1515. public void FixedList128Byte_InsertRangeWithBeginEnd()
  1516. {
  1517. var list = new FixedList128Bytes<byte>() { 0, 3, 4 };
  1518. list.InsertRangeWithBeginEnd(1,3);
  1519. list[1] = 1;
  1520. list[2] = 2;
  1521. for(var i = 0; i < 5; ++i)
  1522. Assert.AreEqual(i, list[i]);
  1523. }
  1524. [Test]
  1525. public void FixedList128Byte_InsertRange()
  1526. {
  1527. var list = new FixedList128Bytes<byte>() { 0, 3, 4 };
  1528. Assert.AreEqual(3, list.Length);
  1529. list.InsertRange(1, 2);
  1530. Assert.AreEqual(5, list.Length);
  1531. list[1] = 1;
  1532. list[2] = 2;
  1533. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  1534. Assert.AreEqual(5, list.Length);
  1535. for(var i = 0; i < 5; ++i)
  1536. Assert.AreEqual(i, list[i]);
  1537. }
  1538. private static void Expected(ref FixedList128Bytes<byte> container, int expectedLength, int[] expected)
  1539. {
  1540. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  1541. Assert.AreEqual(container.Length, expectedLength);
  1542. for (var i = 0; i < container.Length; ++i)
  1543. {
  1544. Assert.AreEqual(expected[i], container[i]);
  1545. }
  1546. }
  1547. [Test]
  1548. public void FixedList128Byte_RemoveAt()
  1549. {
  1550. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 1, 2 };
  1551. list.RemoveAt(1);
  1552. list.RemoveAt(1);
  1553. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1554. }
  1555. [Test]
  1556. public void FixedList128Byte_Remove()
  1557. {
  1558. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 1, 2 };
  1559. Assert.True(list.Remove((byte)3));
  1560. Assert.True(list.Remove((byte)3));
  1561. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1562. }
  1563. [Test]
  1564. public void FixedList128Byte_RemoveSwapBack()
  1565. {
  1566. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 2, 1 };
  1567. Assert.True(list.RemoveSwapBack((byte)3));
  1568. Assert.True(list.RemoveSwapBack((byte)3));
  1569. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1570. }
  1571. [Test]
  1572. public void FixedList128Byte_RemoveRange()
  1573. {
  1574. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 1, 2 };
  1575. list.RemoveRange(1, 2);
  1576. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1577. }
  1578. [Test]
  1579. public void FixedList128Byte_RemoveAtSwapBack()
  1580. {
  1581. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 1, 2 };
  1582. list.RemoveAtSwapBack(1);
  1583. list.RemoveAtSwapBack(1);
  1584. Expected(ref list, 3, new int[] { 0, 1, 3 });
  1585. }
  1586. [Test]
  1587. public void FixedList128Byte_RemoveRangeSwapBack()
  1588. {
  1589. var list = new FixedList128Bytes<byte>() { 0, 3, 3, 1, 2 };
  1590. list.RemoveRangeSwapBack(1, 2);
  1591. Expected(ref list, 3, new int[] { 0, 1, 2 });
  1592. }
  1593. [Test]
  1594. public void FixedList128Byte_Insert()
  1595. {
  1596. var list = new FixedList128Bytes<byte>() { 0, 3, 4 };
  1597. list.Insert(1,1);
  1598. list.Insert(2,2);
  1599. for(var i = 0; i < 5; ++i)
  1600. Assert.AreEqual(i, list[i]);
  1601. }
  1602. [Test]
  1603. public void FixedList128Byte_Sort()
  1604. {
  1605. var list = new FixedList128Bytes<byte>();
  1606. for(var i = 0; i < 5; ++i)
  1607. list.Add((byte)(4-i));
  1608. list.Sort();
  1609. for(var i = 0; i < 5; ++i)
  1610. Assert.AreEqual(i, list[i]);
  1611. }
  1612. [Test]
  1613. public void FixedList128Byte_To_FixedList32Byte()
  1614. {
  1615. var a = new FixedList128Bytes<byte>();
  1616. for(var i = 0; i < 126; ++i)
  1617. a.Add((byte)i);
  1618. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1619. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList32Bytes<byte>(a); } );
  1620. #endif
  1621. }
  1622. [Test]
  1623. public void FixedList128Byte_To_FixedList64Byte()
  1624. {
  1625. var a = new FixedList128Bytes<byte>();
  1626. for(var i = 0; i < 126; ++i)
  1627. a.Add((byte)i);
  1628. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1629. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList64Bytes<byte>(a); } );
  1630. #endif
  1631. }
  1632. public readonly unsafe struct FixedList32Int_Wrapper
  1633. {
  1634. readonly FixedList32Bytes<int> _values;
  1635. internal int* Values => (int*)_values.Buffer;
  1636. public FixedList32Int_Wrapper(int x, int y)
  1637. {
  1638. _values = new FixedList32Bytes<int>();
  1639. _values.Add(x);
  1640. _values.Add(y);
  1641. _values.Sort();
  1642. }
  1643. }
  1644. unsafe void FixedList32Int_ReadonlyWorksFunc1(in FixedList32Int_Wrapper list, int* originalValues)
  1645. {
  1646. int* values = list.Values;
  1647. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  1648. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  1649. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  1650. }
  1651. [Test]
  1652. public unsafe void FixedList32Int_ReadonlyWorks()
  1653. {
  1654. var list = new FixedList32Int_Wrapper(17, 23);
  1655. int* values = list.Values;
  1656. FixedList32Int_ReadonlyWorksFunc1(list, values);
  1657. }
  1658. [Test]
  1659. public unsafe void FixedList32Int_FixedBytes32Align8IsAlignedGeneric()
  1660. {
  1661. var list = new FixedList<int,FixedBytes32Align8>();
  1662. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(int) - 1)) == 0);
  1663. }
  1664. [Test]
  1665. public void FixedList32Int_FixedBytes32Align8ToNativeArrayWorksGeneric()
  1666. {
  1667. var list = new FixedList<int,FixedBytes32Align8>();
  1668. for(var i = 0; i < 7; ++i)
  1669. list.Add((int)(i * 123 + 234));
  1670. using(var array = list.ToNativeArray(Allocator.Temp))
  1671. {
  1672. for(var i = 0; i < 7; ++i)
  1673. Assert.AreEqual((int)(i * 123 + 234), array[i]);
  1674. }
  1675. }
  1676. [Test]
  1677. public void FixedList32Int_ToNativeArrayWorks()
  1678. {
  1679. var list = new FixedList32Bytes<int>();
  1680. for(var i = 0; i < 7; ++i)
  1681. list.Add((int)(i * 123 + 234));
  1682. using(var array = list.ToNativeArray(Allocator.Temp))
  1683. {
  1684. for(var i = 0; i < 7; ++i)
  1685. Assert.AreEqual((int)(i * 123 + 234), array[i]);
  1686. }
  1687. }
  1688. [Test]
  1689. public void FixedList32Int_GenericHasExpectedLayout()
  1690. {
  1691. var actual = new FixedList32Bytes<int>();
  1692. for(var i = 0; i < 7; ++i)
  1693. actual.Add((int)i);
  1694. unsafe
  1695. {
  1696. var e = stackalloc byte[32];
  1697. e[0] = (byte)((7 >> 0) & 0xFF);
  1698. e[1] = (byte)((7 >> 8) & 0xFF);
  1699. for(var i = 0; i < 7; ++i)
  1700. {
  1701. var s = (int)i;
  1702. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<int>() + sizeof(int) * i, &s, sizeof(int));
  1703. }
  1704. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 32));
  1705. }
  1706. }
  1707. [Test]
  1708. [TestRequiresDotsDebugOrCollectionChecks]
  1709. public void FixedList32Int_GenericHasExpectedCapacity()
  1710. {
  1711. var list = new FixedList32Bytes<int>();
  1712. var expectedCapacity = list.Capacity;
  1713. for(int i = 0; i < expectedCapacity; ++i)
  1714. list.Add((int)i);
  1715. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((int)expectedCapacity); });
  1716. }
  1717. [Test]
  1718. public unsafe void FixedList32Int_GenericAdd()
  1719. {
  1720. var list = new FixedList32Bytes<int>();
  1721. var capacity = list.Capacity;
  1722. for (var i = 0; i < capacity; ++i)
  1723. {
  1724. list.Add((int)i);
  1725. Assert.AreEqual(i + 1, list.Length);
  1726. Assert.AreEqual(i, list[i]);
  1727. }
  1728. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1729. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  1730. #endif
  1731. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  1732. }
  1733. [Test]
  1734. public unsafe void FixedList32Int_GenericAddRange()
  1735. {
  1736. var list = new FixedList32Bytes<int>();
  1737. var capacity = list.Capacity;
  1738. var items = stackalloc int[capacity];
  1739. for (var i = 0; i < capacity; ++i)
  1740. items[i] = (int)i;
  1741. var half = capacity / 2;
  1742. list.AddRange(items, half);
  1743. Assert.AreEqual(half, list.Length);
  1744. for (var i = 0; i < half; ++i)
  1745. Assert.AreEqual(i, list[i]);
  1746. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1747. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  1748. #endif
  1749. Assert.AreEqual(half, list.Length); // Verify length didn't change
  1750. }
  1751. [Test]
  1752. public unsafe void FixedList32Int_GenericAddNoResize()
  1753. {
  1754. var list = new FixedList32Bytes<int>();
  1755. var capacity = list.Capacity;
  1756. for (var i = 0; i < capacity; ++i)
  1757. {
  1758. list.AddNoResize((int)i);
  1759. Assert.AreEqual(i + 1, list.Length);
  1760. Assert.AreEqual(i, list[i]);
  1761. }
  1762. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1763. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  1764. #endif
  1765. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  1766. }
  1767. [Test]
  1768. public unsafe void FixedList32Int_GenericAddRangeNoResize()
  1769. {
  1770. var list = new FixedList32Bytes<int>();
  1771. var capacity = list.Capacity;
  1772. var items = stackalloc int[capacity];
  1773. for (var i = 0; i < capacity; ++i)
  1774. items[i] = (int)i;
  1775. var half = capacity / 2;
  1776. list.AddRangeNoResize(items, half);
  1777. Assert.AreEqual(half, list.Length);
  1778. for (var i = 0; i < half; ++i)
  1779. Assert.AreEqual(i, list[i]);
  1780. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1781. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  1782. #endif
  1783. Assert.AreEqual(half, list.Length); // Verify length didn't change
  1784. }
  1785. [Test]
  1786. public void FixedList32Int_GenericAddReplicate()
  1787. {
  1788. var list = new FixedList32Bytes<int>();
  1789. list.AddReplicate(42, 2);
  1790. Assert.AreEqual(2, list.Length);
  1791. foreach (var item in list)
  1792. Assert.AreEqual(42, item);
  1793. list.AddReplicate(42, 3);
  1794. Assert.AreEqual(5, list.Length);
  1795. foreach (var item in list)
  1796. Assert.AreEqual(42, item);
  1797. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  1798. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  1799. #endif
  1800. Assert.AreEqual(5, list.Length); // Verify length didn't change
  1801. }
  1802. [Test]
  1803. public void FixedList32Int_GenericInsertRangeWithBeginEnd()
  1804. {
  1805. var list = new FixedList32Bytes<int>() { 0, 3, 4 };
  1806. list.InsertRangeWithBeginEnd(1,3);
  1807. list[1] = 1;
  1808. list[2] = 2;
  1809. for(var i = 0; i < 5; ++i)
  1810. Assert.AreEqual(i, list[i]);
  1811. }
  1812. [Test]
  1813. public void FixedList32Int_GenericInsertRange()
  1814. {
  1815. var list = new FixedList32Bytes<int>() { 0, 3, 4 };
  1816. Assert.AreEqual(3, list.Length);
  1817. list.InsertRange(1, 2);
  1818. Assert.AreEqual(5, list.Length);
  1819. list[1] = 1;
  1820. list[2] = 2;
  1821. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  1822. Assert.AreEqual(5, list.Length);
  1823. for(var i = 0; i < 5; ++i)
  1824. Assert.AreEqual(i, list[i]);
  1825. }
  1826. [Test]
  1827. public void FixedList32Int_GenericRemoveRange()
  1828. {
  1829. var list = new FixedList32Bytes<int>() { 0, 3, 3, 1, 2 };
  1830. list.RemoveRange(1, 2);
  1831. for(var i = 0; i < 3; ++i)
  1832. Assert.AreEqual(i, list[i]);
  1833. }
  1834. [Test]
  1835. public void FixedList32Int_GenericInsert()
  1836. {
  1837. var list = new FixedList32Bytes<int>() { 0, 3, 4 };
  1838. list.Insert(1,1);
  1839. list.Insert(2,2);
  1840. for(var i = 0; i < 5; ++i)
  1841. Assert.AreEqual(i, list[i]);
  1842. }
  1843. [Test]
  1844. public void FixedList32Int_GenericRemoveAt()
  1845. {
  1846. var list = new FixedList32Bytes<int>() { 0, 3, 3, 1, 2 };
  1847. list.RemoveAt(1);
  1848. list.RemoveAt(1);
  1849. for(var i = 0; i < 3; ++i)
  1850. Assert.AreEqual(i, list[i]);
  1851. }
  1852. [Test]
  1853. public void FixedList32Int_GenericRemove()
  1854. {
  1855. var list = new FixedList32Bytes<int>() { 0, 3, 3, 1, 2 };
  1856. Assert.True(list.Remove((int)3));
  1857. Assert.True(list.Remove((int)3));
  1858. for(var i = 0; i < 3; ++i)
  1859. Assert.AreEqual(i, list[i]);
  1860. }
  1861. [Test]
  1862. public void FixedList32Int_GenericRemoveSwapBack()
  1863. {
  1864. var list = new FixedList32Bytes<int>() { 0, 3, 3, 2, 1 };
  1865. Assert.True(list.RemoveSwapBack((int)3));
  1866. Assert.True(list.RemoveSwapBack((int)3));
  1867. for(var i = 0; i < 3; ++i)
  1868. Assert.AreEqual(i, list[i]);
  1869. }
  1870. [Test]
  1871. public void FixedList32Int_GenericSort()
  1872. {
  1873. var list = new FixedList32Bytes<int>();
  1874. for(var i = 0; i < 5; ++i)
  1875. list.Add((int)(4-i));
  1876. list.Sort();
  1877. for(var i = 0; i < 5; ++i)
  1878. Assert.AreEqual(i, list[i]);
  1879. }
  1880. [Test]
  1881. public void FixedList32Int_GenericSortCustomComparer()
  1882. {
  1883. var list = new FixedList32Bytes<int>();
  1884. for(var i = 0; i < 5; ++i)
  1885. list.Add((int)(i));
  1886. list.Sort(new DescendingComparer<int>());
  1887. for(var i = 0; i < 5; ++i)
  1888. Assert.AreEqual(4-i, list[i]);
  1889. }
  1890. [Test]
  1891. public unsafe void FixedList32Int_IndexOf()
  1892. {
  1893. var list = new FixedList32Bytes<int>() { 123, 178 };
  1894. bool r0 = false, r1 = false, r2 = false;
  1895. GCAllocRecorder.ValidateNoGCAllocs(() =>
  1896. {
  1897. r0 = -1 != list.IndexOf((int)145);
  1898. r1 = list.Contains((int)123);
  1899. r2 = list.Contains((int)178);
  1900. });
  1901. Assert.False(r0);
  1902. Assert.True(r1);
  1903. Assert.True(r2);
  1904. }
  1905. public readonly unsafe struct FixedList64Int_Wrapper
  1906. {
  1907. readonly FixedList64Bytes<int> _values;
  1908. internal int* Values => (int*)_values.Buffer;
  1909. public FixedList64Int_Wrapper(int x, int y)
  1910. {
  1911. _values = new FixedList64Bytes<int>();
  1912. _values.Add(x);
  1913. _values.Add(y);
  1914. _values.Sort();
  1915. }
  1916. }
  1917. unsafe void FixedList64Int_ReadonlyWorksFunc1(in FixedList64Int_Wrapper list, int* originalValues)
  1918. {
  1919. int* values = list.Values;
  1920. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  1921. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  1922. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  1923. }
  1924. [Test]
  1925. public unsafe void FixedList64Int_ReadonlyWorks()
  1926. {
  1927. var list = new FixedList64Int_Wrapper(17, 23);
  1928. int* values = list.Values;
  1929. FixedList64Int_ReadonlyWorksFunc1(list, values);
  1930. }
  1931. [Test]
  1932. public unsafe void FixedList64Int_FixedBytes64Align8IsAlignedGeneric()
  1933. {
  1934. var list = new FixedList<int,FixedBytes64Align8>();
  1935. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(int) - 1)) == 0);
  1936. }
  1937. [Test]
  1938. public void FixedList64Int_FixedBytes64Align8ToNativeArrayWorksGeneric()
  1939. {
  1940. var list = new FixedList<int,FixedBytes64Align8>();
  1941. for(var i = 0; i < 15; ++i)
  1942. list.Add((int)(i * 123 + 234));
  1943. using(var array = list.ToNativeArray(Allocator.Temp))
  1944. {
  1945. for(var i = 0; i < 15; ++i)
  1946. Assert.AreEqual((int)(i * 123 + 234), array[i]);
  1947. }
  1948. }
  1949. [Test]
  1950. public void FixedList64Int_ToNativeArrayWorks()
  1951. {
  1952. var list = new FixedList64Bytes<int>();
  1953. for(var i = 0; i < 15; ++i)
  1954. list.Add((int)(i * 123 + 234));
  1955. using(var array = list.ToNativeArray(Allocator.Temp))
  1956. {
  1957. for(var i = 0; i < 15; ++i)
  1958. Assert.AreEqual((int)(i * 123 + 234), array[i]);
  1959. }
  1960. }
  1961. [Test]
  1962. public void FixedList64Int_GenericHasExpectedLayout()
  1963. {
  1964. var actual = new FixedList64Bytes<int>();
  1965. for(var i = 0; i < 15; ++i)
  1966. actual.Add((int)i);
  1967. unsafe
  1968. {
  1969. var e = stackalloc byte[64];
  1970. e[0] = (byte)((15 >> 0) & 0xFF);
  1971. e[1] = (byte)((15 >> 8) & 0xFF);
  1972. for(var i = 0; i < 15; ++i)
  1973. {
  1974. var s = (int)i;
  1975. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<int>() + sizeof(int) * i, &s, sizeof(int));
  1976. }
  1977. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 64));
  1978. }
  1979. }
  1980. [Test]
  1981. [TestRequiresDotsDebugOrCollectionChecks]
  1982. public void FixedList64Int_GenericHasExpectedCapacity()
  1983. {
  1984. var list = new FixedList64Bytes<int>();
  1985. var expectedCapacity = list.Capacity;
  1986. for(int i = 0; i < expectedCapacity; ++i)
  1987. list.Add((int)i);
  1988. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((int)expectedCapacity); });
  1989. }
  1990. [Test]
  1991. public unsafe void FixedList64Int_GenericAdd()
  1992. {
  1993. var list = new FixedList64Bytes<int>();
  1994. var capacity = list.Capacity;
  1995. for (var i = 0; i < capacity; ++i)
  1996. {
  1997. list.Add((int)i);
  1998. Assert.AreEqual(i + 1, list.Length);
  1999. Assert.AreEqual(i, list[i]);
  2000. }
  2001. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2002. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  2003. #endif
  2004. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2005. }
  2006. [Test]
  2007. public unsafe void FixedList64Int_GenericAddRange()
  2008. {
  2009. var list = new FixedList64Bytes<int>();
  2010. var capacity = list.Capacity;
  2011. var items = stackalloc int[capacity];
  2012. for (var i = 0; i < capacity; ++i)
  2013. items[i] = (int)i;
  2014. var half = capacity / 2;
  2015. list.AddRange(items, half);
  2016. Assert.AreEqual(half, list.Length);
  2017. for (var i = 0; i < half; ++i)
  2018. Assert.AreEqual(i, list[i]);
  2019. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2020. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  2021. #endif
  2022. Assert.AreEqual(half, list.Length); // Verify length didn't change
  2023. }
  2024. [Test]
  2025. public unsafe void FixedList64Int_GenericAddNoResize()
  2026. {
  2027. var list = new FixedList64Bytes<int>();
  2028. var capacity = list.Capacity;
  2029. for (var i = 0; i < capacity; ++i)
  2030. {
  2031. list.AddNoResize((int)i);
  2032. Assert.AreEqual(i + 1, list.Length);
  2033. Assert.AreEqual(i, list[i]);
  2034. }
  2035. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2036. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  2037. #endif
  2038. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2039. }
  2040. [Test]
  2041. public unsafe void FixedList64Int_GenericAddRangeNoResize()
  2042. {
  2043. var list = new FixedList64Bytes<int>();
  2044. var capacity = list.Capacity;
  2045. var items = stackalloc int[capacity];
  2046. for (var i = 0; i < capacity; ++i)
  2047. items[i] = (int)i;
  2048. var half = capacity / 2;
  2049. list.AddRangeNoResize(items, half);
  2050. Assert.AreEqual(half, list.Length);
  2051. for (var i = 0; i < half; ++i)
  2052. Assert.AreEqual(i, list[i]);
  2053. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2054. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  2055. #endif
  2056. Assert.AreEqual(half, list.Length); // Verify length didn't change
  2057. }
  2058. [Test]
  2059. public void FixedList64Int_GenericAddReplicate()
  2060. {
  2061. var list = new FixedList64Bytes<int>();
  2062. list.AddReplicate(42, 2);
  2063. Assert.AreEqual(2, list.Length);
  2064. foreach (var item in list)
  2065. Assert.AreEqual(42, item);
  2066. list.AddReplicate(42, 3);
  2067. Assert.AreEqual(5, list.Length);
  2068. foreach (var item in list)
  2069. Assert.AreEqual(42, item);
  2070. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2071. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  2072. #endif
  2073. Assert.AreEqual(5, list.Length); // Verify length didn't change
  2074. }
  2075. [Test]
  2076. public void FixedList64Int_GenericInsertRangeWithBeginEnd()
  2077. {
  2078. var list = new FixedList64Bytes<int>() { 0, 3, 4 };
  2079. list.InsertRangeWithBeginEnd(1,3);
  2080. list[1] = 1;
  2081. list[2] = 2;
  2082. for(var i = 0; i < 5; ++i)
  2083. Assert.AreEqual(i, list[i]);
  2084. }
  2085. [Test]
  2086. public void FixedList64Int_GenericInsertRange()
  2087. {
  2088. var list = new FixedList64Bytes<int>() { 0, 3, 4 };
  2089. Assert.AreEqual(3, list.Length);
  2090. list.InsertRange(1, 2);
  2091. Assert.AreEqual(5, list.Length);
  2092. list[1] = 1;
  2093. list[2] = 2;
  2094. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  2095. Assert.AreEqual(5, list.Length);
  2096. for(var i = 0; i < 5; ++i)
  2097. Assert.AreEqual(i, list[i]);
  2098. }
  2099. [Test]
  2100. public void FixedList64Int_GenericRemoveRange()
  2101. {
  2102. var list = new FixedList64Bytes<int>() { 0, 3, 3, 1, 2 };
  2103. list.RemoveRange(1, 2);
  2104. for(var i = 0; i < 3; ++i)
  2105. Assert.AreEqual(i, list[i]);
  2106. }
  2107. [Test]
  2108. public void FixedList64Int_GenericInsert()
  2109. {
  2110. var list = new FixedList64Bytes<int>() { 0, 3, 4 };
  2111. list.Insert(1,1);
  2112. list.Insert(2,2);
  2113. for(var i = 0; i < 5; ++i)
  2114. Assert.AreEqual(i, list[i]);
  2115. }
  2116. [Test]
  2117. public void FixedList64Int_GenericRemoveAt()
  2118. {
  2119. var list = new FixedList64Bytes<int>() { 0, 3, 3, 1, 2 };
  2120. list.RemoveAt(1);
  2121. list.RemoveAt(1);
  2122. for(var i = 0; i < 3; ++i)
  2123. Assert.AreEqual(i, list[i]);
  2124. }
  2125. [Test]
  2126. public void FixedList64Int_GenericRemove()
  2127. {
  2128. var list = new FixedList64Bytes<int>() { 0, 3, 3, 1, 2 };
  2129. Assert.True(list.Remove((int)3));
  2130. Assert.True(list.Remove((int)3));
  2131. for(var i = 0; i < 3; ++i)
  2132. Assert.AreEqual(i, list[i]);
  2133. }
  2134. [Test]
  2135. public void FixedList64Int_GenericRemoveSwapBack()
  2136. {
  2137. var list = new FixedList64Bytes<int>() { 0, 3, 3, 2, 1 };
  2138. Assert.True(list.RemoveSwapBack((int)3));
  2139. Assert.True(list.RemoveSwapBack((int)3));
  2140. for(var i = 0; i < 3; ++i)
  2141. Assert.AreEqual(i, list[i]);
  2142. }
  2143. [Test]
  2144. public void FixedList64Int_GenericSort()
  2145. {
  2146. var list = new FixedList64Bytes<int>();
  2147. for(var i = 0; i < 5; ++i)
  2148. list.Add((int)(4-i));
  2149. list.Sort();
  2150. for(var i = 0; i < 5; ++i)
  2151. Assert.AreEqual(i, list[i]);
  2152. }
  2153. [Test]
  2154. public void FixedList64Int_GenericSortCustomComparer()
  2155. {
  2156. var list = new FixedList64Bytes<int>();
  2157. for(var i = 0; i < 5; ++i)
  2158. list.Add((int)(i));
  2159. list.Sort(new DescendingComparer<int>());
  2160. for(var i = 0; i < 5; ++i)
  2161. Assert.AreEqual(4-i, list[i]);
  2162. }
  2163. [Test]
  2164. public unsafe void FixedList64Int_IndexOf()
  2165. {
  2166. var list = new FixedList64Bytes<int>() { 123, 178 };
  2167. bool r0 = false, r1 = false, r2 = false;
  2168. GCAllocRecorder.ValidateNoGCAllocs(() =>
  2169. {
  2170. r0 = -1 != list.IndexOf((int)145);
  2171. r1 = list.Contains((int)123);
  2172. r2 = list.Contains((int)178);
  2173. });
  2174. Assert.False(r0);
  2175. Assert.True(r1);
  2176. Assert.True(r2);
  2177. }
  2178. public readonly unsafe struct FixedList128Int_Wrapper
  2179. {
  2180. readonly FixedList128Bytes<int> _values;
  2181. internal int* Values => (int*)_values.Buffer;
  2182. public FixedList128Int_Wrapper(int x, int y)
  2183. {
  2184. _values = new FixedList128Bytes<int>();
  2185. _values.Add(x);
  2186. _values.Add(y);
  2187. _values.Sort();
  2188. }
  2189. }
  2190. unsafe void FixedList128Int_ReadonlyWorksFunc1(in FixedList128Int_Wrapper list, int* originalValues)
  2191. {
  2192. int* values = list.Values;
  2193. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  2194. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  2195. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  2196. }
  2197. [Test]
  2198. public unsafe void FixedList128Int_ReadonlyWorks()
  2199. {
  2200. var list = new FixedList128Int_Wrapper(17, 23);
  2201. int* values = list.Values;
  2202. FixedList128Int_ReadonlyWorksFunc1(list, values);
  2203. }
  2204. [Test]
  2205. public unsafe void FixedList128Int_FixedBytes128Align8IsAlignedGeneric()
  2206. {
  2207. var list = new FixedList<int,FixedBytes128Align8>();
  2208. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(int) - 1)) == 0);
  2209. }
  2210. [Test]
  2211. public void FixedList128Int_FixedBytes128Align8ToNativeArrayWorksGeneric()
  2212. {
  2213. var list = new FixedList<int,FixedBytes128Align8>();
  2214. for(var i = 0; i < 31; ++i)
  2215. list.Add((int)(i * 123 + 234));
  2216. using(var array = list.ToNativeArray(Allocator.Temp))
  2217. {
  2218. for(var i = 0; i < 31; ++i)
  2219. Assert.AreEqual((int)(i * 123 + 234), array[i]);
  2220. }
  2221. }
  2222. [Test]
  2223. public void FixedList128Int_ToNativeArrayWorks()
  2224. {
  2225. var list = new FixedList128Bytes<int>();
  2226. for(var i = 0; i < 31; ++i)
  2227. list.Add((int)(i * 123 + 234));
  2228. using(var array = list.ToNativeArray(Allocator.Temp))
  2229. {
  2230. for(var i = 0; i < 31; ++i)
  2231. Assert.AreEqual((int)(i * 123 + 234), array[i]);
  2232. }
  2233. }
  2234. [Test]
  2235. public void FixedList128Int_GenericHasExpectedLayout()
  2236. {
  2237. var actual = new FixedList128Bytes<int>();
  2238. for(var i = 0; i < 31; ++i)
  2239. actual.Add((int)i);
  2240. unsafe
  2241. {
  2242. var e = stackalloc byte[128];
  2243. e[0] = (byte)((31 >> 0) & 0xFF);
  2244. e[1] = (byte)((31 >> 8) & 0xFF);
  2245. for(var i = 0; i < 31; ++i)
  2246. {
  2247. var s = (int)i;
  2248. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<int>() + sizeof(int) * i, &s, sizeof(int));
  2249. }
  2250. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 128));
  2251. }
  2252. }
  2253. [Test]
  2254. [TestRequiresDotsDebugOrCollectionChecks]
  2255. public void FixedList128Int_GenericHasExpectedCapacity()
  2256. {
  2257. var list = new FixedList128Bytes<int>();
  2258. var expectedCapacity = list.Capacity;
  2259. for(int i = 0; i < expectedCapacity; ++i)
  2260. list.Add((int)i);
  2261. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((int)expectedCapacity); });
  2262. }
  2263. [Test]
  2264. public unsafe void FixedList128Int_GenericAdd()
  2265. {
  2266. var list = new FixedList128Bytes<int>();
  2267. var capacity = list.Capacity;
  2268. for (var i = 0; i < capacity; ++i)
  2269. {
  2270. list.Add((int)i);
  2271. Assert.AreEqual(i + 1, list.Length);
  2272. Assert.AreEqual(i, list[i]);
  2273. }
  2274. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2275. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  2276. #endif
  2277. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2278. }
  2279. [Test]
  2280. public unsafe void FixedList128Int_GenericAddRange()
  2281. {
  2282. var list = new FixedList128Bytes<int>();
  2283. var capacity = list.Capacity;
  2284. var items = stackalloc int[capacity];
  2285. for (var i = 0; i < capacity; ++i)
  2286. items[i] = (int)i;
  2287. var half = capacity / 2;
  2288. list.AddRange(items, half);
  2289. Assert.AreEqual(half, list.Length);
  2290. for (var i = 0; i < half; ++i)
  2291. Assert.AreEqual(i, list[i]);
  2292. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2293. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  2294. #endif
  2295. Assert.AreEqual(half, list.Length); // Verify length didn't change
  2296. }
  2297. [Test]
  2298. public unsafe void FixedList128Int_GenericAddNoResize()
  2299. {
  2300. var list = new FixedList128Bytes<int>();
  2301. var capacity = list.Capacity;
  2302. for (var i = 0; i < capacity; ++i)
  2303. {
  2304. list.AddNoResize((int)i);
  2305. Assert.AreEqual(i + 1, list.Length);
  2306. Assert.AreEqual(i, list[i]);
  2307. }
  2308. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2309. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  2310. #endif
  2311. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2312. }
  2313. [Test]
  2314. public unsafe void FixedList128Int_GenericAddRangeNoResize()
  2315. {
  2316. var list = new FixedList128Bytes<int>();
  2317. var capacity = list.Capacity;
  2318. var items = stackalloc int[capacity];
  2319. for (var i = 0; i < capacity; ++i)
  2320. items[i] = (int)i;
  2321. var half = capacity / 2;
  2322. list.AddRangeNoResize(items, half);
  2323. Assert.AreEqual(half, list.Length);
  2324. for (var i = 0; i < half; ++i)
  2325. Assert.AreEqual(i, list[i]);
  2326. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2327. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  2328. #endif
  2329. Assert.AreEqual(half, list.Length); // Verify length didn't change
  2330. }
  2331. [Test]
  2332. public void FixedList128Int_GenericAddReplicate()
  2333. {
  2334. var list = new FixedList128Bytes<int>();
  2335. list.AddReplicate(42, 2);
  2336. Assert.AreEqual(2, list.Length);
  2337. foreach (var item in list)
  2338. Assert.AreEqual(42, item);
  2339. list.AddReplicate(42, 3);
  2340. Assert.AreEqual(5, list.Length);
  2341. foreach (var item in list)
  2342. Assert.AreEqual(42, item);
  2343. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2344. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  2345. #endif
  2346. Assert.AreEqual(5, list.Length); // Verify length didn't change
  2347. }
  2348. [Test]
  2349. public void FixedList128Int_GenericInsertRangeWithBeginEnd()
  2350. {
  2351. var list = new FixedList128Bytes<int>() { 0, 3, 4 };
  2352. list.InsertRangeWithBeginEnd(1,3);
  2353. list[1] = 1;
  2354. list[2] = 2;
  2355. for(var i = 0; i < 5; ++i)
  2356. Assert.AreEqual(i, list[i]);
  2357. }
  2358. [Test]
  2359. public void FixedList128Int_GenericInsertRange()
  2360. {
  2361. var list = new FixedList128Bytes<int>() { 0, 3, 4 };
  2362. Assert.AreEqual(3, list.Length);
  2363. list.InsertRange(1, 2);
  2364. Assert.AreEqual(5, list.Length);
  2365. list[1] = 1;
  2366. list[2] = 2;
  2367. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  2368. Assert.AreEqual(5, list.Length);
  2369. for(var i = 0; i < 5; ++i)
  2370. Assert.AreEqual(i, list[i]);
  2371. }
  2372. [Test]
  2373. public void FixedList128Int_GenericRemoveRange()
  2374. {
  2375. var list = new FixedList128Bytes<int>() { 0, 3, 3, 1, 2 };
  2376. list.RemoveRange(1, 2);
  2377. for(var i = 0; i < 3; ++i)
  2378. Assert.AreEqual(i, list[i]);
  2379. }
  2380. [Test]
  2381. public void FixedList128Int_GenericInsert()
  2382. {
  2383. var list = new FixedList128Bytes<int>() { 0, 3, 4 };
  2384. list.Insert(1,1);
  2385. list.Insert(2,2);
  2386. for(var i = 0; i < 5; ++i)
  2387. Assert.AreEqual(i, list[i]);
  2388. }
  2389. [Test]
  2390. public void FixedList128Int_GenericRemoveAt()
  2391. {
  2392. var list = new FixedList128Bytes<int>() { 0, 3, 3, 1, 2 };
  2393. list.RemoveAt(1);
  2394. list.RemoveAt(1);
  2395. for(var i = 0; i < 3; ++i)
  2396. Assert.AreEqual(i, list[i]);
  2397. }
  2398. [Test]
  2399. public void FixedList128Int_GenericRemove()
  2400. {
  2401. var list = new FixedList128Bytes<int>() { 0, 3, 3, 1, 2 };
  2402. Assert.True(list.Remove((int)3));
  2403. Assert.True(list.Remove((int)3));
  2404. for(var i = 0; i < 3; ++i)
  2405. Assert.AreEqual(i, list[i]);
  2406. }
  2407. [Test]
  2408. public void FixedList128Int_GenericRemoveSwapBack()
  2409. {
  2410. var list = new FixedList128Bytes<int>() { 0, 3, 3, 2, 1 };
  2411. Assert.True(list.RemoveSwapBack((int)3));
  2412. Assert.True(list.RemoveSwapBack((int)3));
  2413. for(var i = 0; i < 3; ++i)
  2414. Assert.AreEqual(i, list[i]);
  2415. }
  2416. [Test]
  2417. public void FixedList128Int_GenericSort()
  2418. {
  2419. var list = new FixedList128Bytes<int>();
  2420. for(var i = 0; i < 5; ++i)
  2421. list.Add((int)(4-i));
  2422. list.Sort();
  2423. for(var i = 0; i < 5; ++i)
  2424. Assert.AreEqual(i, list[i]);
  2425. }
  2426. [Test]
  2427. public void FixedList128Int_GenericSortCustomComparer()
  2428. {
  2429. var list = new FixedList128Bytes<int>();
  2430. for(var i = 0; i < 5; ++i)
  2431. list.Add((int)(i));
  2432. list.Sort(new DescendingComparer<int>());
  2433. for(var i = 0; i < 5; ++i)
  2434. Assert.AreEqual(4-i, list[i]);
  2435. }
  2436. [Test]
  2437. public unsafe void FixedList128Int_IndexOf()
  2438. {
  2439. var list = new FixedList128Bytes<int>() { 123, 178 };
  2440. bool r0 = false, r1 = false, r2 = false;
  2441. GCAllocRecorder.ValidateNoGCAllocs(() =>
  2442. {
  2443. r0 = -1 != list.IndexOf((int)145);
  2444. r1 = list.Contains((int)123);
  2445. r2 = list.Contains((int)178);
  2446. });
  2447. Assert.False(r0);
  2448. Assert.True(r1);
  2449. Assert.True(r2);
  2450. }
  2451. [Test]
  2452. public void FixedList32Int_HasExpectedLayout()
  2453. {
  2454. var actual = new FixedList32Bytes<int>();
  2455. for(var i = 0; i < 7; ++i)
  2456. actual.Add((int)i);
  2457. unsafe
  2458. {
  2459. var e = stackalloc byte[32];
  2460. e[0] = (byte)((7 >> 0) & 0xFF);
  2461. e[1] = (byte)((7 >> 8) & 0xFF);
  2462. for(var i = 0; i < 7; ++i)
  2463. {
  2464. var s = (int)i;
  2465. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<int>() + sizeof(int) * i, &s, sizeof(int));
  2466. }
  2467. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 32));
  2468. }
  2469. }
  2470. [Test]
  2471. [TestRequiresDotsDebugOrCollectionChecks]
  2472. public void FixedList32Int_HasExpectedCapacity()
  2473. {
  2474. var list = new FixedList32Bytes<int>();
  2475. var expectedCapacity = list.Capacity;
  2476. for(int i = 0; i < expectedCapacity; ++i)
  2477. list.Add((int)i);
  2478. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((int)expectedCapacity); });
  2479. }
  2480. class ScriptableObjectFixedList32Int_ : UnityEngine.ScriptableObject
  2481. {
  2482. public FixedList32Bytes<int> List;
  2483. }
  2484. [Test]
  2485. public void FixedList32Int_Serializes()
  2486. {
  2487. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList32Int_ >();
  2488. for(int i = 0; i < a.List.Capacity; ++i)
  2489. a.List.Add((int)i);
  2490. var b = UnityEngine.Object.Instantiate(a);
  2491. CollectionAssert.AreEqual(a.List, b.List);
  2492. }
  2493. [Test]
  2494. public unsafe void FixedList32Int_Add()
  2495. {
  2496. var list = new FixedList32Bytes<int>();
  2497. var capacity = list.Capacity;
  2498. for (var i = 0; i < capacity; ++i)
  2499. {
  2500. list.Add((int)i);
  2501. Assert.AreEqual(i + 1, list.Length);
  2502. Assert.AreEqual(i, list[i]);
  2503. }
  2504. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2505. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  2506. #endif
  2507. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2508. }
  2509. [Test]
  2510. public unsafe void FixedList32Int_AddRange()
  2511. {
  2512. var list = new FixedList32Bytes<int>();
  2513. var capacity = list.Capacity;
  2514. var items = stackalloc int[capacity];
  2515. for (var i = 0; i < capacity; ++i)
  2516. items[i] = (int)i;
  2517. var half = capacity / 2;
  2518. list.AddRange(items, half);
  2519. Assert.AreEqual(half, list.Length);
  2520. for (var i = 0; i < half; ++i)
  2521. Assert.AreEqual(i, list[i]);
  2522. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2523. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  2524. #endif
  2525. Assert.AreEqual(half, list.Length); // Verify length didn't change
  2526. }
  2527. [Test]
  2528. public unsafe void FixedList32Int_AddNoResize()
  2529. {
  2530. var list = new FixedList32Bytes<int>();
  2531. var capacity = list.Capacity;
  2532. for (var i = 0; i < capacity; ++i)
  2533. {
  2534. list.AddNoResize((int)i);
  2535. Assert.AreEqual(i + 1, list.Length);
  2536. Assert.AreEqual(i, list[i]);
  2537. }
  2538. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2539. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  2540. #endif
  2541. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2542. }
  2543. [Test]
  2544. public unsafe void FixedList32Int_AddRangeNoResize()
  2545. {
  2546. var list = new FixedList32Bytes<int>();
  2547. var capacity = list.Capacity;
  2548. var items = stackalloc int[capacity];
  2549. for (var i = 0; i < capacity; ++i)
  2550. items[i] = (int)i;
  2551. var half = capacity / 2;
  2552. list.AddRangeNoResize(items, half);
  2553. Assert.AreEqual(half, list.Length);
  2554. for (var i = 0; i < half; ++i)
  2555. Assert.AreEqual(i, list[i]);
  2556. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2557. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  2558. #endif
  2559. Assert.AreEqual(half, list.Length); // Verify length didn't change
  2560. }
  2561. [Test]
  2562. public void FixedList32Int_AddReplicate()
  2563. {
  2564. var list = new FixedList32Bytes<int>();
  2565. list.AddReplicate(42, 2);
  2566. Assert.AreEqual(2, list.Length);
  2567. foreach (var item in list)
  2568. Assert.AreEqual(42, item);
  2569. list.AddReplicate(42, 3);
  2570. Assert.AreEqual(5, list.Length);
  2571. foreach (var item in list)
  2572. Assert.AreEqual(42, item);
  2573. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2574. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  2575. #endif
  2576. Assert.AreEqual(5, list.Length); // Verify length didn't change
  2577. }
  2578. [Test]
  2579. public void FixedList32Int_InsertRangeWithBeginEnd()
  2580. {
  2581. var list = new FixedList32Bytes<int>() { 0, 3, 4 };
  2582. list.InsertRangeWithBeginEnd(1,3);
  2583. list[1] = 1;
  2584. list[2] = 2;
  2585. for(var i = 0; i < 5; ++i)
  2586. Assert.AreEqual(i, list[i]);
  2587. }
  2588. [Test]
  2589. public void FixedList32Int_InsertRange()
  2590. {
  2591. var list = new FixedList32Bytes<int>() { 0, 3, 4 };
  2592. Assert.AreEqual(3, list.Length);
  2593. list.InsertRange(1, 2);
  2594. Assert.AreEqual(5, list.Length);
  2595. list[1] = 1;
  2596. list[2] = 2;
  2597. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  2598. Assert.AreEqual(5, list.Length);
  2599. for(var i = 0; i < 5; ++i)
  2600. Assert.AreEqual(i, list[i]);
  2601. }
  2602. private static void Expected(ref FixedList32Bytes<int> container, int expectedLength, int[] expected)
  2603. {
  2604. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  2605. Assert.AreEqual(container.Length, expectedLength);
  2606. for (var i = 0; i < container.Length; ++i)
  2607. {
  2608. Assert.AreEqual(expected[i], container[i]);
  2609. }
  2610. }
  2611. [Test]
  2612. public void FixedList32Int_RemoveAt()
  2613. {
  2614. var list = new FixedList32Bytes<int>() { 0, 3, 3, 1, 2 };
  2615. list.RemoveAt(1);
  2616. list.RemoveAt(1);
  2617. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2618. }
  2619. [Test]
  2620. public void FixedList32Int_Remove()
  2621. {
  2622. var list = new FixedList32Bytes<int>() { 0, 3, 3, 1, 2 };
  2623. Assert.True(list.Remove((int)3));
  2624. Assert.True(list.Remove((int)3));
  2625. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2626. }
  2627. [Test]
  2628. public void FixedList32Int_RemoveSwapBack()
  2629. {
  2630. var list = new FixedList32Bytes<int>() { 0, 3, 3, 2, 1 };
  2631. Assert.True(list.RemoveSwapBack((int)3));
  2632. Assert.True(list.RemoveSwapBack((int)3));
  2633. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2634. }
  2635. [Test]
  2636. public void FixedList32Int_RemoveRange()
  2637. {
  2638. var list = new FixedList32Bytes<int>() { 0, 3, 3, 1, 2 };
  2639. list.RemoveRange(1, 2);
  2640. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2641. }
  2642. [Test]
  2643. public void FixedList32Int_RemoveAtSwapBack()
  2644. {
  2645. var list = new FixedList32Bytes<int>() { 0, 3, 3, 1, 2 };
  2646. list.RemoveAtSwapBack(1);
  2647. list.RemoveAtSwapBack(1);
  2648. Expected(ref list, 3, new int[] { 0, 1, 3 });
  2649. }
  2650. [Test]
  2651. public void FixedList32Int_RemoveRangeSwapBack()
  2652. {
  2653. var list = new FixedList32Bytes<int>() { 0, 3, 3, 1, 2 };
  2654. list.RemoveRangeSwapBack(1, 2);
  2655. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2656. }
  2657. [Test]
  2658. public void FixedList32Int_Insert()
  2659. {
  2660. var list = new FixedList32Bytes<int>() { 0, 3, 4 };
  2661. list.Insert(1,1);
  2662. list.Insert(2,2);
  2663. for(var i = 0; i < 5; ++i)
  2664. Assert.AreEqual(i, list[i]);
  2665. }
  2666. [Test]
  2667. public void FixedList32Int_Sort()
  2668. {
  2669. var list = new FixedList32Bytes<int>();
  2670. for(var i = 0; i < 5; ++i)
  2671. list.Add((int)(4-i));
  2672. list.Sort();
  2673. for(var i = 0; i < 5; ++i)
  2674. Assert.AreEqual(i, list[i]);
  2675. }
  2676. [Test]
  2677. public void FixedList32Int_To_FixedList64Int()
  2678. {
  2679. var a = new FixedList32Bytes<int>();
  2680. for(var i = 0; i < 7; ++i)
  2681. a.Add((int)i);
  2682. var b = new FixedList64Bytes<int>(a);
  2683. for(var i = 0; i < 7; ++i)
  2684. Assert.AreEqual((int)i, b[i]);
  2685. }
  2686. [Test]
  2687. public void FixedList32Int_To_FixedList128Int()
  2688. {
  2689. var a = new FixedList32Bytes<int>();
  2690. for(var i = 0; i < 7; ++i)
  2691. a.Add((int)i);
  2692. var b = new FixedList128Bytes<int>(a);
  2693. for(var i = 0; i < 7; ++i)
  2694. Assert.AreEqual((int)i, b[i]);
  2695. }
  2696. [Test]
  2697. public void FixedList64Int_HasExpectedLayout()
  2698. {
  2699. var actual = new FixedList64Bytes<int>();
  2700. for(var i = 0; i < 15; ++i)
  2701. actual.Add((int)i);
  2702. unsafe
  2703. {
  2704. var e = stackalloc byte[64];
  2705. e[0] = (byte)((15 >> 0) & 0xFF);
  2706. e[1] = (byte)((15 >> 8) & 0xFF);
  2707. for(var i = 0; i < 15; ++i)
  2708. {
  2709. var s = (int)i;
  2710. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<int>() + sizeof(int) * i, &s, sizeof(int));
  2711. }
  2712. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 64));
  2713. }
  2714. }
  2715. [Test]
  2716. [TestRequiresDotsDebugOrCollectionChecks]
  2717. public void FixedList64Int_HasExpectedCapacity()
  2718. {
  2719. var list = new FixedList64Bytes<int>();
  2720. var expectedCapacity = list.Capacity;
  2721. for(int i = 0; i < expectedCapacity; ++i)
  2722. list.Add((int)i);
  2723. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((int)expectedCapacity); });
  2724. }
  2725. class ScriptableObjectFixedList64Int_ : UnityEngine.ScriptableObject
  2726. {
  2727. public FixedList64Bytes<int> List;
  2728. }
  2729. [Test]
  2730. public void FixedList64Int_Serializes()
  2731. {
  2732. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList64Int_ >();
  2733. for(int i = 0; i < a.List.Capacity; ++i)
  2734. a.List.Add((int)i);
  2735. var b = UnityEngine.Object.Instantiate(a);
  2736. CollectionAssert.AreEqual(a.List, b.List);
  2737. }
  2738. [Test]
  2739. public unsafe void FixedList64Int_Add()
  2740. {
  2741. var list = new FixedList64Bytes<int>();
  2742. var capacity = list.Capacity;
  2743. for (var i = 0; i < capacity; ++i)
  2744. {
  2745. list.Add((int)i);
  2746. Assert.AreEqual(i + 1, list.Length);
  2747. Assert.AreEqual(i, list[i]);
  2748. }
  2749. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2750. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  2751. #endif
  2752. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2753. }
  2754. [Test]
  2755. public unsafe void FixedList64Int_AddRange()
  2756. {
  2757. var list = new FixedList64Bytes<int>();
  2758. var capacity = list.Capacity;
  2759. var items = stackalloc int[capacity];
  2760. for (var i = 0; i < capacity; ++i)
  2761. items[i] = (int)i;
  2762. var half = capacity / 2;
  2763. list.AddRange(items, half);
  2764. Assert.AreEqual(half, list.Length);
  2765. for (var i = 0; i < half; ++i)
  2766. Assert.AreEqual(i, list[i]);
  2767. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2768. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  2769. #endif
  2770. Assert.AreEqual(half, list.Length); // Verify length didn't change
  2771. }
  2772. [Test]
  2773. public unsafe void FixedList64Int_AddNoResize()
  2774. {
  2775. var list = new FixedList64Bytes<int>();
  2776. var capacity = list.Capacity;
  2777. for (var i = 0; i < capacity; ++i)
  2778. {
  2779. list.AddNoResize((int)i);
  2780. Assert.AreEqual(i + 1, list.Length);
  2781. Assert.AreEqual(i, list[i]);
  2782. }
  2783. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2784. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  2785. #endif
  2786. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2787. }
  2788. [Test]
  2789. public unsafe void FixedList64Int_AddRangeNoResize()
  2790. {
  2791. var list = new FixedList64Bytes<int>();
  2792. var capacity = list.Capacity;
  2793. var items = stackalloc int[capacity];
  2794. for (var i = 0; i < capacity; ++i)
  2795. items[i] = (int)i;
  2796. var half = capacity / 2;
  2797. list.AddRangeNoResize(items, half);
  2798. Assert.AreEqual(half, list.Length);
  2799. for (var i = 0; i < half; ++i)
  2800. Assert.AreEqual(i, list[i]);
  2801. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2802. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  2803. #endif
  2804. Assert.AreEqual(half, list.Length); // Verify length didn't change
  2805. }
  2806. [Test]
  2807. public void FixedList64Int_AddReplicate()
  2808. {
  2809. var list = new FixedList64Bytes<int>();
  2810. list.AddReplicate(42, 2);
  2811. Assert.AreEqual(2, list.Length);
  2812. foreach (var item in list)
  2813. Assert.AreEqual(42, item);
  2814. list.AddReplicate(42, 3);
  2815. Assert.AreEqual(5, list.Length);
  2816. foreach (var item in list)
  2817. Assert.AreEqual(42, item);
  2818. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2819. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  2820. #endif
  2821. Assert.AreEqual(5, list.Length); // Verify length didn't change
  2822. }
  2823. [Test]
  2824. public void FixedList64Int_InsertRangeWithBeginEnd()
  2825. {
  2826. var list = new FixedList64Bytes<int>() { 0, 3, 4 };
  2827. list.InsertRangeWithBeginEnd(1,3);
  2828. list[1] = 1;
  2829. list[2] = 2;
  2830. for(var i = 0; i < 5; ++i)
  2831. Assert.AreEqual(i, list[i]);
  2832. }
  2833. [Test]
  2834. public void FixedList64Int_InsertRange()
  2835. {
  2836. var list = new FixedList64Bytes<int>() { 0, 3, 4 };
  2837. Assert.AreEqual(3, list.Length);
  2838. list.InsertRange(1, 2);
  2839. Assert.AreEqual(5, list.Length);
  2840. list[1] = 1;
  2841. list[2] = 2;
  2842. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  2843. Assert.AreEqual(5, list.Length);
  2844. for(var i = 0; i < 5; ++i)
  2845. Assert.AreEqual(i, list[i]);
  2846. }
  2847. private static void Expected(ref FixedList64Bytes<int> container, int expectedLength, int[] expected)
  2848. {
  2849. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  2850. Assert.AreEqual(container.Length, expectedLength);
  2851. for (var i = 0; i < container.Length; ++i)
  2852. {
  2853. Assert.AreEqual(expected[i], container[i]);
  2854. }
  2855. }
  2856. [Test]
  2857. public void FixedList64Int_RemoveAt()
  2858. {
  2859. var list = new FixedList64Bytes<int>() { 0, 3, 3, 1, 2 };
  2860. list.RemoveAt(1);
  2861. list.RemoveAt(1);
  2862. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2863. }
  2864. [Test]
  2865. public void FixedList64Int_Remove()
  2866. {
  2867. var list = new FixedList64Bytes<int>() { 0, 3, 3, 1, 2 };
  2868. Assert.True(list.Remove((int)3));
  2869. Assert.True(list.Remove((int)3));
  2870. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2871. }
  2872. [Test]
  2873. public void FixedList64Int_RemoveSwapBack()
  2874. {
  2875. var list = new FixedList64Bytes<int>() { 0, 3, 3, 2, 1 };
  2876. Assert.True(list.RemoveSwapBack((int)3));
  2877. Assert.True(list.RemoveSwapBack((int)3));
  2878. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2879. }
  2880. [Test]
  2881. public void FixedList64Int_RemoveRange()
  2882. {
  2883. var list = new FixedList64Bytes<int>() { 0, 3, 3, 1, 2 };
  2884. list.RemoveRange(1, 2);
  2885. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2886. }
  2887. [Test]
  2888. public void FixedList64Int_RemoveAtSwapBack()
  2889. {
  2890. var list = new FixedList64Bytes<int>() { 0, 3, 3, 1, 2 };
  2891. list.RemoveAtSwapBack(1);
  2892. list.RemoveAtSwapBack(1);
  2893. Expected(ref list, 3, new int[] { 0, 1, 3 });
  2894. }
  2895. [Test]
  2896. public void FixedList64Int_RemoveRangeSwapBack()
  2897. {
  2898. var list = new FixedList64Bytes<int>() { 0, 3, 3, 1, 2 };
  2899. list.RemoveRangeSwapBack(1, 2);
  2900. Expected(ref list, 3, new int[] { 0, 1, 2 });
  2901. }
  2902. [Test]
  2903. public void FixedList64Int_Insert()
  2904. {
  2905. var list = new FixedList64Bytes<int>() { 0, 3, 4 };
  2906. list.Insert(1,1);
  2907. list.Insert(2,2);
  2908. for(var i = 0; i < 5; ++i)
  2909. Assert.AreEqual(i, list[i]);
  2910. }
  2911. [Test]
  2912. public void FixedList64Int_Sort()
  2913. {
  2914. var list = new FixedList64Bytes<int>();
  2915. for(var i = 0; i < 5; ++i)
  2916. list.Add((int)(4-i));
  2917. list.Sort();
  2918. for(var i = 0; i < 5; ++i)
  2919. Assert.AreEqual(i, list[i]);
  2920. }
  2921. [Test]
  2922. public void FixedList64Int_To_FixedList32Int()
  2923. {
  2924. var a = new FixedList64Bytes<int>();
  2925. for(var i = 0; i < 15; ++i)
  2926. a.Add((int)i);
  2927. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2928. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList32Bytes<int>(a); } );
  2929. #endif
  2930. }
  2931. [Test]
  2932. public void FixedList64Int_To_FixedList128Int()
  2933. {
  2934. var a = new FixedList64Bytes<int>();
  2935. for(var i = 0; i < 15; ++i)
  2936. a.Add((int)i);
  2937. var b = new FixedList128Bytes<int>(a);
  2938. for(var i = 0; i < 15; ++i)
  2939. Assert.AreEqual((int)i, b[i]);
  2940. }
  2941. [Test]
  2942. public void FixedList128Int_HasExpectedLayout()
  2943. {
  2944. var actual = new FixedList128Bytes<int>();
  2945. for(var i = 0; i < 31; ++i)
  2946. actual.Add((int)i);
  2947. unsafe
  2948. {
  2949. var e = stackalloc byte[128];
  2950. e[0] = (byte)((31 >> 0) & 0xFF);
  2951. e[1] = (byte)((31 >> 8) & 0xFF);
  2952. for(var i = 0; i < 31; ++i)
  2953. {
  2954. var s = (int)i;
  2955. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<int>() + sizeof(int) * i, &s, sizeof(int));
  2956. }
  2957. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 128));
  2958. }
  2959. }
  2960. [Test]
  2961. [TestRequiresDotsDebugOrCollectionChecks]
  2962. public void FixedList128Int_HasExpectedCapacity()
  2963. {
  2964. var list = new FixedList128Bytes<int>();
  2965. var expectedCapacity = list.Capacity;
  2966. for(int i = 0; i < expectedCapacity; ++i)
  2967. list.Add((int)i);
  2968. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((int)expectedCapacity); });
  2969. }
  2970. class ScriptableObjectFixedList128Int_ : UnityEngine.ScriptableObject
  2971. {
  2972. public FixedList128Bytes<int> List;
  2973. }
  2974. [Test]
  2975. public void FixedList128Int_Serializes()
  2976. {
  2977. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList128Int_ >();
  2978. for(int i = 0; i < a.List.Capacity; ++i)
  2979. a.List.Add((int)i);
  2980. var b = UnityEngine.Object.Instantiate(a);
  2981. CollectionAssert.AreEqual(a.List, b.List);
  2982. }
  2983. [Test]
  2984. public unsafe void FixedList128Int_Add()
  2985. {
  2986. var list = new FixedList128Bytes<int>();
  2987. var capacity = list.Capacity;
  2988. for (var i = 0; i < capacity; ++i)
  2989. {
  2990. list.Add((int)i);
  2991. Assert.AreEqual(i + 1, list.Length);
  2992. Assert.AreEqual(i, list[i]);
  2993. }
  2994. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  2995. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  2996. #endif
  2997. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  2998. }
  2999. [Test]
  3000. public unsafe void FixedList128Int_AddRange()
  3001. {
  3002. var list = new FixedList128Bytes<int>();
  3003. var capacity = list.Capacity;
  3004. var items = stackalloc int[capacity];
  3005. for (var i = 0; i < capacity; ++i)
  3006. items[i] = (int)i;
  3007. var half = capacity / 2;
  3008. list.AddRange(items, half);
  3009. Assert.AreEqual(half, list.Length);
  3010. for (var i = 0; i < half; ++i)
  3011. Assert.AreEqual(i, list[i]);
  3012. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3013. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  3014. #endif
  3015. Assert.AreEqual(half, list.Length); // Verify length didn't change
  3016. }
  3017. [Test]
  3018. public unsafe void FixedList128Int_AddNoResize()
  3019. {
  3020. var list = new FixedList128Bytes<int>();
  3021. var capacity = list.Capacity;
  3022. for (var i = 0; i < capacity; ++i)
  3023. {
  3024. list.AddNoResize((int)i);
  3025. Assert.AreEqual(i + 1, list.Length);
  3026. Assert.AreEqual(i, list[i]);
  3027. }
  3028. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3029. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  3030. #endif
  3031. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  3032. }
  3033. [Test]
  3034. public unsafe void FixedList128Int_AddRangeNoResize()
  3035. {
  3036. var list = new FixedList128Bytes<int>();
  3037. var capacity = list.Capacity;
  3038. var items = stackalloc int[capacity];
  3039. for (var i = 0; i < capacity; ++i)
  3040. items[i] = (int)i;
  3041. var half = capacity / 2;
  3042. list.AddRangeNoResize(items, half);
  3043. Assert.AreEqual(half, list.Length);
  3044. for (var i = 0; i < half; ++i)
  3045. Assert.AreEqual(i, list[i]);
  3046. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3047. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  3048. #endif
  3049. Assert.AreEqual(half, list.Length); // Verify length didn't change
  3050. }
  3051. [Test]
  3052. public void FixedList128Int_AddReplicate()
  3053. {
  3054. var list = new FixedList128Bytes<int>();
  3055. list.AddReplicate(42, 2);
  3056. Assert.AreEqual(2, list.Length);
  3057. foreach (var item in list)
  3058. Assert.AreEqual(42, item);
  3059. list.AddReplicate(42, 3);
  3060. Assert.AreEqual(5, list.Length);
  3061. foreach (var item in list)
  3062. Assert.AreEqual(42, item);
  3063. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3064. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  3065. #endif
  3066. Assert.AreEqual(5, list.Length); // Verify length didn't change
  3067. }
  3068. [Test]
  3069. public void FixedList128Int_InsertRangeWithBeginEnd()
  3070. {
  3071. var list = new FixedList128Bytes<int>() { 0, 3, 4 };
  3072. list.InsertRangeWithBeginEnd(1,3);
  3073. list[1] = 1;
  3074. list[2] = 2;
  3075. for(var i = 0; i < 5; ++i)
  3076. Assert.AreEqual(i, list[i]);
  3077. }
  3078. [Test]
  3079. public void FixedList128Int_InsertRange()
  3080. {
  3081. var list = new FixedList128Bytes<int>() { 0, 3, 4 };
  3082. Assert.AreEqual(3, list.Length);
  3083. list.InsertRange(1, 2);
  3084. Assert.AreEqual(5, list.Length);
  3085. list[1] = 1;
  3086. list[2] = 2;
  3087. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  3088. Assert.AreEqual(5, list.Length);
  3089. for(var i = 0; i < 5; ++i)
  3090. Assert.AreEqual(i, list[i]);
  3091. }
  3092. private static void Expected(ref FixedList128Bytes<int> container, int expectedLength, int[] expected)
  3093. {
  3094. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  3095. Assert.AreEqual(container.Length, expectedLength);
  3096. for (var i = 0; i < container.Length; ++i)
  3097. {
  3098. Assert.AreEqual(expected[i], container[i]);
  3099. }
  3100. }
  3101. [Test]
  3102. public void FixedList128Int_RemoveAt()
  3103. {
  3104. var list = new FixedList128Bytes<int>() { 0, 3, 3, 1, 2 };
  3105. list.RemoveAt(1);
  3106. list.RemoveAt(1);
  3107. Expected(ref list, 3, new int[] { 0, 1, 2 });
  3108. }
  3109. [Test]
  3110. public void FixedList128Int_Remove()
  3111. {
  3112. var list = new FixedList128Bytes<int>() { 0, 3, 3, 1, 2 };
  3113. Assert.True(list.Remove((int)3));
  3114. Assert.True(list.Remove((int)3));
  3115. Expected(ref list, 3, new int[] { 0, 1, 2 });
  3116. }
  3117. [Test]
  3118. public void FixedList128Int_RemoveSwapBack()
  3119. {
  3120. var list = new FixedList128Bytes<int>() { 0, 3, 3, 2, 1 };
  3121. Assert.True(list.RemoveSwapBack((int)3));
  3122. Assert.True(list.RemoveSwapBack((int)3));
  3123. Expected(ref list, 3, new int[] { 0, 1, 2 });
  3124. }
  3125. [Test]
  3126. public void FixedList128Int_RemoveRange()
  3127. {
  3128. var list = new FixedList128Bytes<int>() { 0, 3, 3, 1, 2 };
  3129. list.RemoveRange(1, 2);
  3130. Expected(ref list, 3, new int[] { 0, 1, 2 });
  3131. }
  3132. [Test]
  3133. public void FixedList128Int_RemoveAtSwapBack()
  3134. {
  3135. var list = new FixedList128Bytes<int>() { 0, 3, 3, 1, 2 };
  3136. list.RemoveAtSwapBack(1);
  3137. list.RemoveAtSwapBack(1);
  3138. Expected(ref list, 3, new int[] { 0, 1, 3 });
  3139. }
  3140. [Test]
  3141. public void FixedList128Int_RemoveRangeSwapBack()
  3142. {
  3143. var list = new FixedList128Bytes<int>() { 0, 3, 3, 1, 2 };
  3144. list.RemoveRangeSwapBack(1, 2);
  3145. Expected(ref list, 3, new int[] { 0, 1, 2 });
  3146. }
  3147. [Test]
  3148. public void FixedList128Int_Insert()
  3149. {
  3150. var list = new FixedList128Bytes<int>() { 0, 3, 4 };
  3151. list.Insert(1,1);
  3152. list.Insert(2,2);
  3153. for(var i = 0; i < 5; ++i)
  3154. Assert.AreEqual(i, list[i]);
  3155. }
  3156. [Test]
  3157. public void FixedList128Int_Sort()
  3158. {
  3159. var list = new FixedList128Bytes<int>();
  3160. for(var i = 0; i < 5; ++i)
  3161. list.Add((int)(4-i));
  3162. list.Sort();
  3163. for(var i = 0; i < 5; ++i)
  3164. Assert.AreEqual(i, list[i]);
  3165. }
  3166. [Test]
  3167. public void FixedList128Int_To_FixedList32Int()
  3168. {
  3169. var a = new FixedList128Bytes<int>();
  3170. for(var i = 0; i < 31; ++i)
  3171. a.Add((int)i);
  3172. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3173. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList32Bytes<int>(a); } );
  3174. #endif
  3175. }
  3176. [Test]
  3177. public void FixedList128Int_To_FixedList64Int()
  3178. {
  3179. var a = new FixedList128Bytes<int>();
  3180. for(var i = 0; i < 31; ++i)
  3181. a.Add((int)i);
  3182. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3183. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList64Bytes<int>(a); } );
  3184. #endif
  3185. }
  3186. public readonly unsafe struct FixedList32Float_Wrapper
  3187. {
  3188. readonly FixedList32Bytes<float> _values;
  3189. internal float* Values => (float*)_values.Buffer;
  3190. public FixedList32Float_Wrapper(float x, float y)
  3191. {
  3192. _values = new FixedList32Bytes<float>();
  3193. _values.Add(x);
  3194. _values.Add(y);
  3195. _values.Sort();
  3196. }
  3197. }
  3198. unsafe void FixedList32Float_ReadonlyWorksFunc1(in FixedList32Float_Wrapper list, float* originalValues)
  3199. {
  3200. float* values = list.Values;
  3201. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  3202. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  3203. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  3204. }
  3205. [Test]
  3206. public unsafe void FixedList32Float_ReadonlyWorks()
  3207. {
  3208. var list = new FixedList32Float_Wrapper(17, 23);
  3209. float* values = list.Values;
  3210. FixedList32Float_ReadonlyWorksFunc1(list, values);
  3211. }
  3212. [Test]
  3213. public unsafe void FixedList32Float_FixedBytes32Align8IsAlignedGeneric()
  3214. {
  3215. var list = new FixedList<float,FixedBytes32Align8>();
  3216. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(float) - 1)) == 0);
  3217. }
  3218. [Test]
  3219. public void FixedList32Float_FixedBytes32Align8ToNativeArrayWorksGeneric()
  3220. {
  3221. var list = new FixedList<float,FixedBytes32Align8>();
  3222. for(var i = 0; i < 7; ++i)
  3223. list.Add((float)(i * 123 + 234));
  3224. using(var array = list.ToNativeArray(Allocator.Temp))
  3225. {
  3226. for(var i = 0; i < 7; ++i)
  3227. Assert.AreEqual((float)(i * 123 + 234), array[i]);
  3228. }
  3229. }
  3230. [Test]
  3231. public void FixedList32Float_ToNativeArrayWorks()
  3232. {
  3233. var list = new FixedList32Bytes<float>();
  3234. for(var i = 0; i < 7; ++i)
  3235. list.Add((float)(i * 123 + 234));
  3236. using(var array = list.ToNativeArray(Allocator.Temp))
  3237. {
  3238. for(var i = 0; i < 7; ++i)
  3239. Assert.AreEqual((float)(i * 123 + 234), array[i]);
  3240. }
  3241. }
  3242. [Test]
  3243. public void FixedList32Float_GenericHasExpectedLayout()
  3244. {
  3245. var actual = new FixedList32Bytes<float>();
  3246. for(var i = 0; i < 7; ++i)
  3247. actual.Add((float)i);
  3248. unsafe
  3249. {
  3250. var e = stackalloc byte[32];
  3251. e[0] = (byte)((7 >> 0) & 0xFF);
  3252. e[1] = (byte)((7 >> 8) & 0xFF);
  3253. for(var i = 0; i < 7; ++i)
  3254. {
  3255. var s = (float)i;
  3256. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<float>() + sizeof(float) * i, &s, sizeof(float));
  3257. }
  3258. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 32));
  3259. }
  3260. }
  3261. [Test]
  3262. [TestRequiresDotsDebugOrCollectionChecks]
  3263. public void FixedList32Float_GenericHasExpectedCapacity()
  3264. {
  3265. var list = new FixedList32Bytes<float>();
  3266. var expectedCapacity = list.Capacity;
  3267. for(int i = 0; i < expectedCapacity; ++i)
  3268. list.Add((float)i);
  3269. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((float)expectedCapacity); });
  3270. }
  3271. [Test]
  3272. public unsafe void FixedList32Float_GenericAdd()
  3273. {
  3274. var list = new FixedList32Bytes<float>();
  3275. var capacity = list.Capacity;
  3276. for (var i = 0; i < capacity; ++i)
  3277. {
  3278. list.Add((float)i);
  3279. Assert.AreEqual(i + 1, list.Length);
  3280. Assert.AreEqual(i, list[i]);
  3281. }
  3282. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3283. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  3284. #endif
  3285. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  3286. }
  3287. [Test]
  3288. public unsafe void FixedList32Float_GenericAddRange()
  3289. {
  3290. var list = new FixedList32Bytes<float>();
  3291. var capacity = list.Capacity;
  3292. var items = stackalloc float[capacity];
  3293. for (var i = 0; i < capacity; ++i)
  3294. items[i] = (float)i;
  3295. var half = capacity / 2;
  3296. list.AddRange(items, half);
  3297. Assert.AreEqual(half, list.Length);
  3298. for (var i = 0; i < half; ++i)
  3299. Assert.AreEqual(i, list[i]);
  3300. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3301. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  3302. #endif
  3303. Assert.AreEqual(half, list.Length); // Verify length didn't change
  3304. }
  3305. [Test]
  3306. public unsafe void FixedList32Float_GenericAddNoResize()
  3307. {
  3308. var list = new FixedList32Bytes<float>();
  3309. var capacity = list.Capacity;
  3310. for (var i = 0; i < capacity; ++i)
  3311. {
  3312. list.AddNoResize((float)i);
  3313. Assert.AreEqual(i + 1, list.Length);
  3314. Assert.AreEqual(i, list[i]);
  3315. }
  3316. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3317. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  3318. #endif
  3319. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  3320. }
  3321. [Test]
  3322. public unsafe void FixedList32Float_GenericAddRangeNoResize()
  3323. {
  3324. var list = new FixedList32Bytes<float>();
  3325. var capacity = list.Capacity;
  3326. var items = stackalloc float[capacity];
  3327. for (var i = 0; i < capacity; ++i)
  3328. items[i] = (float)i;
  3329. var half = capacity / 2;
  3330. list.AddRangeNoResize(items, half);
  3331. Assert.AreEqual(half, list.Length);
  3332. for (var i = 0; i < half; ++i)
  3333. Assert.AreEqual(i, list[i]);
  3334. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3335. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  3336. #endif
  3337. Assert.AreEqual(half, list.Length); // Verify length didn't change
  3338. }
  3339. [Test]
  3340. public void FixedList32Float_GenericAddReplicate()
  3341. {
  3342. var list = new FixedList32Bytes<float>();
  3343. list.AddReplicate(42, 2);
  3344. Assert.AreEqual(2, list.Length);
  3345. foreach (var item in list)
  3346. Assert.AreEqual(42, item);
  3347. list.AddReplicate(42, 3);
  3348. Assert.AreEqual(5, list.Length);
  3349. foreach (var item in list)
  3350. Assert.AreEqual(42, item);
  3351. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3352. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  3353. #endif
  3354. Assert.AreEqual(5, list.Length); // Verify length didn't change
  3355. }
  3356. [Test]
  3357. public void FixedList32Float_GenericInsertRangeWithBeginEnd()
  3358. {
  3359. var list = new FixedList32Bytes<float>() { 0, 3, 4 };
  3360. list.InsertRangeWithBeginEnd(1,3);
  3361. list[1] = 1;
  3362. list[2] = 2;
  3363. for(var i = 0; i < 5; ++i)
  3364. Assert.AreEqual(i, list[i]);
  3365. }
  3366. [Test]
  3367. public void FixedList32Float_GenericInsertRange()
  3368. {
  3369. var list = new FixedList32Bytes<float>() { 0, 3, 4 };
  3370. Assert.AreEqual(3, list.Length);
  3371. list.InsertRange(1, 2);
  3372. Assert.AreEqual(5, list.Length);
  3373. list[1] = 1;
  3374. list[2] = 2;
  3375. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  3376. Assert.AreEqual(5, list.Length);
  3377. for(var i = 0; i < 5; ++i)
  3378. Assert.AreEqual(i, list[i]);
  3379. }
  3380. [Test]
  3381. public void FixedList32Float_GenericRemoveRange()
  3382. {
  3383. var list = new FixedList32Bytes<float>() { 0, 3, 3, 1, 2 };
  3384. list.RemoveRange(1, 2);
  3385. for(var i = 0; i < 3; ++i)
  3386. Assert.AreEqual(i, list[i]);
  3387. }
  3388. [Test]
  3389. public void FixedList32Float_GenericInsert()
  3390. {
  3391. var list = new FixedList32Bytes<float>() { 0, 3, 4 };
  3392. list.Insert(1,1);
  3393. list.Insert(2,2);
  3394. for(var i = 0; i < 5; ++i)
  3395. Assert.AreEqual(i, list[i]);
  3396. }
  3397. [Test]
  3398. public void FixedList32Float_GenericRemoveAt()
  3399. {
  3400. var list = new FixedList32Bytes<float>() { 0, 3, 3, 1, 2 };
  3401. list.RemoveAt(1);
  3402. list.RemoveAt(1);
  3403. for(var i = 0; i < 3; ++i)
  3404. Assert.AreEqual(i, list[i]);
  3405. }
  3406. [Test]
  3407. public void FixedList32Float_GenericRemove()
  3408. {
  3409. var list = new FixedList32Bytes<float>() { 0, 3, 3, 1, 2 };
  3410. Assert.True(list.Remove((float)3));
  3411. Assert.True(list.Remove((float)3));
  3412. for(var i = 0; i < 3; ++i)
  3413. Assert.AreEqual(i, list[i]);
  3414. }
  3415. [Test]
  3416. public void FixedList32Float_GenericRemoveSwapBack()
  3417. {
  3418. var list = new FixedList32Bytes<float>() { 0, 3, 3, 2, 1 };
  3419. Assert.True(list.RemoveSwapBack((float)3));
  3420. Assert.True(list.RemoveSwapBack((float)3));
  3421. for(var i = 0; i < 3; ++i)
  3422. Assert.AreEqual(i, list[i]);
  3423. }
  3424. [Test]
  3425. public void FixedList32Float_GenericSort()
  3426. {
  3427. var list = new FixedList32Bytes<float>();
  3428. for(var i = 0; i < 5; ++i)
  3429. list.Add((float)(4-i));
  3430. list.Sort();
  3431. for(var i = 0; i < 5; ++i)
  3432. Assert.AreEqual(i, list[i]);
  3433. }
  3434. [Test]
  3435. public void FixedList32Float_GenericSortCustomComparer()
  3436. {
  3437. var list = new FixedList32Bytes<float>();
  3438. for(var i = 0; i < 5; ++i)
  3439. list.Add((float)(i));
  3440. list.Sort(new DescendingComparer<float>());
  3441. for(var i = 0; i < 5; ++i)
  3442. Assert.AreEqual(4-i, list[i]);
  3443. }
  3444. [Test]
  3445. public unsafe void FixedList32Float_IndexOf()
  3446. {
  3447. var list = new FixedList32Bytes<float>() { 123, 178 };
  3448. bool r0 = false, r1 = false, r2 = false;
  3449. GCAllocRecorder.ValidateNoGCAllocs(() =>
  3450. {
  3451. r0 = -1 != list.IndexOf((float)145);
  3452. r1 = list.Contains((float)123);
  3453. r2 = list.Contains((float)178);
  3454. });
  3455. Assert.False(r0);
  3456. Assert.True(r1);
  3457. Assert.True(r2);
  3458. }
  3459. public readonly unsafe struct FixedList64Float_Wrapper
  3460. {
  3461. readonly FixedList64Bytes<float> _values;
  3462. internal float* Values => (float*)_values.Buffer;
  3463. public FixedList64Float_Wrapper(float x, float y)
  3464. {
  3465. _values = new FixedList64Bytes<float>();
  3466. _values.Add(x);
  3467. _values.Add(y);
  3468. _values.Sort();
  3469. }
  3470. }
  3471. unsafe void FixedList64Float_ReadonlyWorksFunc1(in FixedList64Float_Wrapper list, float* originalValues)
  3472. {
  3473. float* values = list.Values;
  3474. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  3475. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  3476. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  3477. }
  3478. [Test]
  3479. public unsafe void FixedList64Float_ReadonlyWorks()
  3480. {
  3481. var list = new FixedList64Float_Wrapper(17, 23);
  3482. float* values = list.Values;
  3483. FixedList64Float_ReadonlyWorksFunc1(list, values);
  3484. }
  3485. [Test]
  3486. public unsafe void FixedList64Float_FixedBytes64Align8IsAlignedGeneric()
  3487. {
  3488. var list = new FixedList<float,FixedBytes64Align8>();
  3489. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(float) - 1)) == 0);
  3490. }
  3491. [Test]
  3492. public void FixedList64Float_FixedBytes64Align8ToNativeArrayWorksGeneric()
  3493. {
  3494. var list = new FixedList<float,FixedBytes64Align8>();
  3495. for(var i = 0; i < 15; ++i)
  3496. list.Add((float)(i * 123 + 234));
  3497. using(var array = list.ToNativeArray(Allocator.Temp))
  3498. {
  3499. for(var i = 0; i < 15; ++i)
  3500. Assert.AreEqual((float)(i * 123 + 234), array[i]);
  3501. }
  3502. }
  3503. [Test]
  3504. public void FixedList64Float_ToNativeArrayWorks()
  3505. {
  3506. var list = new FixedList64Bytes<float>();
  3507. for(var i = 0; i < 15; ++i)
  3508. list.Add((float)(i * 123 + 234));
  3509. using(var array = list.ToNativeArray(Allocator.Temp))
  3510. {
  3511. for(var i = 0; i < 15; ++i)
  3512. Assert.AreEqual((float)(i * 123 + 234), array[i]);
  3513. }
  3514. }
  3515. [Test]
  3516. public void FixedList64Float_GenericHasExpectedLayout()
  3517. {
  3518. var actual = new FixedList64Bytes<float>();
  3519. for(var i = 0; i < 15; ++i)
  3520. actual.Add((float)i);
  3521. unsafe
  3522. {
  3523. var e = stackalloc byte[64];
  3524. e[0] = (byte)((15 >> 0) & 0xFF);
  3525. e[1] = (byte)((15 >> 8) & 0xFF);
  3526. for(var i = 0; i < 15; ++i)
  3527. {
  3528. var s = (float)i;
  3529. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<float>() + sizeof(float) * i, &s, sizeof(float));
  3530. }
  3531. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 64));
  3532. }
  3533. }
  3534. [Test]
  3535. [TestRequiresDotsDebugOrCollectionChecks]
  3536. public void FixedList64Float_GenericHasExpectedCapacity()
  3537. {
  3538. var list = new FixedList64Bytes<float>();
  3539. var expectedCapacity = list.Capacity;
  3540. for(int i = 0; i < expectedCapacity; ++i)
  3541. list.Add((float)i);
  3542. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((float)expectedCapacity); });
  3543. }
  3544. [Test]
  3545. public unsafe void FixedList64Float_GenericAdd()
  3546. {
  3547. var list = new FixedList64Bytes<float>();
  3548. var capacity = list.Capacity;
  3549. for (var i = 0; i < capacity; ++i)
  3550. {
  3551. list.Add((float)i);
  3552. Assert.AreEqual(i + 1, list.Length);
  3553. Assert.AreEqual(i, list[i]);
  3554. }
  3555. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3556. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  3557. #endif
  3558. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  3559. }
  3560. [Test]
  3561. public unsafe void FixedList64Float_GenericAddRange()
  3562. {
  3563. var list = new FixedList64Bytes<float>();
  3564. var capacity = list.Capacity;
  3565. var items = stackalloc float[capacity];
  3566. for (var i = 0; i < capacity; ++i)
  3567. items[i] = (float)i;
  3568. var half = capacity / 2;
  3569. list.AddRange(items, half);
  3570. Assert.AreEqual(half, list.Length);
  3571. for (var i = 0; i < half; ++i)
  3572. Assert.AreEqual(i, list[i]);
  3573. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3574. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  3575. #endif
  3576. Assert.AreEqual(half, list.Length); // Verify length didn't change
  3577. }
  3578. [Test]
  3579. public unsafe void FixedList64Float_GenericAddNoResize()
  3580. {
  3581. var list = new FixedList64Bytes<float>();
  3582. var capacity = list.Capacity;
  3583. for (var i = 0; i < capacity; ++i)
  3584. {
  3585. list.AddNoResize((float)i);
  3586. Assert.AreEqual(i + 1, list.Length);
  3587. Assert.AreEqual(i, list[i]);
  3588. }
  3589. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3590. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  3591. #endif
  3592. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  3593. }
  3594. [Test]
  3595. public unsafe void FixedList64Float_GenericAddRangeNoResize()
  3596. {
  3597. var list = new FixedList64Bytes<float>();
  3598. var capacity = list.Capacity;
  3599. var items = stackalloc float[capacity];
  3600. for (var i = 0; i < capacity; ++i)
  3601. items[i] = (float)i;
  3602. var half = capacity / 2;
  3603. list.AddRangeNoResize(items, half);
  3604. Assert.AreEqual(half, list.Length);
  3605. for (var i = 0; i < half; ++i)
  3606. Assert.AreEqual(i, list[i]);
  3607. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3608. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  3609. #endif
  3610. Assert.AreEqual(half, list.Length); // Verify length didn't change
  3611. }
  3612. [Test]
  3613. public void FixedList64Float_GenericAddReplicate()
  3614. {
  3615. var list = new FixedList64Bytes<float>();
  3616. list.AddReplicate(42, 2);
  3617. Assert.AreEqual(2, list.Length);
  3618. foreach (var item in list)
  3619. Assert.AreEqual(42, item);
  3620. list.AddReplicate(42, 3);
  3621. Assert.AreEqual(5, list.Length);
  3622. foreach (var item in list)
  3623. Assert.AreEqual(42, item);
  3624. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3625. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  3626. #endif
  3627. Assert.AreEqual(5, list.Length); // Verify length didn't change
  3628. }
  3629. [Test]
  3630. public void FixedList64Float_GenericInsertRangeWithBeginEnd()
  3631. {
  3632. var list = new FixedList64Bytes<float>() { 0, 3, 4 };
  3633. list.InsertRangeWithBeginEnd(1,3);
  3634. list[1] = 1;
  3635. list[2] = 2;
  3636. for(var i = 0; i < 5; ++i)
  3637. Assert.AreEqual(i, list[i]);
  3638. }
  3639. [Test]
  3640. public void FixedList64Float_GenericInsertRange()
  3641. {
  3642. var list = new FixedList64Bytes<float>() { 0, 3, 4 };
  3643. Assert.AreEqual(3, list.Length);
  3644. list.InsertRange(1, 2);
  3645. Assert.AreEqual(5, list.Length);
  3646. list[1] = 1;
  3647. list[2] = 2;
  3648. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  3649. Assert.AreEqual(5, list.Length);
  3650. for(var i = 0; i < 5; ++i)
  3651. Assert.AreEqual(i, list[i]);
  3652. }
  3653. [Test]
  3654. public void FixedList64Float_GenericRemoveRange()
  3655. {
  3656. var list = new FixedList64Bytes<float>() { 0, 3, 3, 1, 2 };
  3657. list.RemoveRange(1, 2);
  3658. for(var i = 0; i < 3; ++i)
  3659. Assert.AreEqual(i, list[i]);
  3660. }
  3661. [Test]
  3662. public void FixedList64Float_GenericInsert()
  3663. {
  3664. var list = new FixedList64Bytes<float>() { 0, 3, 4 };
  3665. list.Insert(1,1);
  3666. list.Insert(2,2);
  3667. for(var i = 0; i < 5; ++i)
  3668. Assert.AreEqual(i, list[i]);
  3669. }
  3670. [Test]
  3671. public void FixedList64Float_GenericRemoveAt()
  3672. {
  3673. var list = new FixedList64Bytes<float>() { 0, 3, 3, 1, 2 };
  3674. list.RemoveAt(1);
  3675. list.RemoveAt(1);
  3676. for(var i = 0; i < 3; ++i)
  3677. Assert.AreEqual(i, list[i]);
  3678. }
  3679. [Test]
  3680. public void FixedList64Float_GenericRemove()
  3681. {
  3682. var list = new FixedList64Bytes<float>() { 0, 3, 3, 1, 2 };
  3683. Assert.True(list.Remove((float)3));
  3684. Assert.True(list.Remove((float)3));
  3685. for(var i = 0; i < 3; ++i)
  3686. Assert.AreEqual(i, list[i]);
  3687. }
  3688. [Test]
  3689. public void FixedList64Float_GenericRemoveSwapBack()
  3690. {
  3691. var list = new FixedList64Bytes<float>() { 0, 3, 3, 2, 1 };
  3692. Assert.True(list.RemoveSwapBack((float)3));
  3693. Assert.True(list.RemoveSwapBack((float)3));
  3694. for(var i = 0; i < 3; ++i)
  3695. Assert.AreEqual(i, list[i]);
  3696. }
  3697. [Test]
  3698. public void FixedList64Float_GenericSort()
  3699. {
  3700. var list = new FixedList64Bytes<float>();
  3701. for(var i = 0; i < 5; ++i)
  3702. list.Add((float)(4-i));
  3703. list.Sort();
  3704. for(var i = 0; i < 5; ++i)
  3705. Assert.AreEqual(i, list[i]);
  3706. }
  3707. [Test]
  3708. public void FixedList64Float_GenericSortCustomComparer()
  3709. {
  3710. var list = new FixedList64Bytes<float>();
  3711. for(var i = 0; i < 5; ++i)
  3712. list.Add((float)(i));
  3713. list.Sort(new DescendingComparer<float>());
  3714. for(var i = 0; i < 5; ++i)
  3715. Assert.AreEqual(4-i, list[i]);
  3716. }
  3717. [Test]
  3718. public unsafe void FixedList64Float_IndexOf()
  3719. {
  3720. var list = new FixedList64Bytes<float>() { 123, 178 };
  3721. bool r0 = false, r1 = false, r2 = false;
  3722. GCAllocRecorder.ValidateNoGCAllocs(() =>
  3723. {
  3724. r0 = -1 != list.IndexOf((float)145);
  3725. r1 = list.Contains((float)123);
  3726. r2 = list.Contains((float)178);
  3727. });
  3728. Assert.False(r0);
  3729. Assert.True(r1);
  3730. Assert.True(r2);
  3731. }
  3732. public readonly unsafe struct FixedList128Float_Wrapper
  3733. {
  3734. readonly FixedList128Bytes<float> _values;
  3735. internal float* Values => (float*)_values.Buffer;
  3736. public FixedList128Float_Wrapper(float x, float y)
  3737. {
  3738. _values = new FixedList128Bytes<float>();
  3739. _values.Add(x);
  3740. _values.Add(y);
  3741. _values.Sort();
  3742. }
  3743. }
  3744. unsafe void FixedList128Float_ReadonlyWorksFunc1(in FixedList128Float_Wrapper list, float* originalValues)
  3745. {
  3746. float* values = list.Values;
  3747. Assert.AreEqual(originalValues[0], values[0], "Values[0] mismatch in func1");
  3748. Assert.AreEqual(originalValues[1], values[1], "Values[1] mismatch in func1");
  3749. Assert.AreEqual((ulong)originalValues, (ulong)values, "Buffer pointer mismatch in func1");
  3750. }
  3751. [Test]
  3752. public unsafe void FixedList128Float_ReadonlyWorks()
  3753. {
  3754. var list = new FixedList128Float_Wrapper(17, 23);
  3755. float* values = list.Values;
  3756. FixedList128Float_ReadonlyWorksFunc1(list, values);
  3757. }
  3758. [Test]
  3759. public unsafe void FixedList128Float_FixedBytes128Align8IsAlignedGeneric()
  3760. {
  3761. var list = new FixedList<float,FixedBytes128Align8>();
  3762. Assert.IsTrue((((ulong)list.Buffer) & (sizeof(float) - 1)) == 0);
  3763. }
  3764. [Test]
  3765. public void FixedList128Float_FixedBytes128Align8ToNativeArrayWorksGeneric()
  3766. {
  3767. var list = new FixedList<float,FixedBytes128Align8>();
  3768. for(var i = 0; i < 31; ++i)
  3769. list.Add((float)(i * 123 + 234));
  3770. using(var array = list.ToNativeArray(Allocator.Temp))
  3771. {
  3772. for(var i = 0; i < 31; ++i)
  3773. Assert.AreEqual((float)(i * 123 + 234), array[i]);
  3774. }
  3775. }
  3776. [Test]
  3777. public void FixedList128Float_ToNativeArrayWorks()
  3778. {
  3779. var list = new FixedList128Bytes<float>();
  3780. for(var i = 0; i < 31; ++i)
  3781. list.Add((float)(i * 123 + 234));
  3782. using(var array = list.ToNativeArray(Allocator.Temp))
  3783. {
  3784. for(var i = 0; i < 31; ++i)
  3785. Assert.AreEqual((float)(i * 123 + 234), array[i]);
  3786. }
  3787. }
  3788. [Test]
  3789. public void FixedList128Float_GenericHasExpectedLayout()
  3790. {
  3791. var actual = new FixedList128Bytes<float>();
  3792. for(var i = 0; i < 31; ++i)
  3793. actual.Add((float)i);
  3794. unsafe
  3795. {
  3796. var e = stackalloc byte[128];
  3797. e[0] = (byte)((31 >> 0) & 0xFF);
  3798. e[1] = (byte)((31 >> 8) & 0xFF);
  3799. for(var i = 0; i < 31; ++i)
  3800. {
  3801. var s = (float)i;
  3802. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<float>() + sizeof(float) * i, &s, sizeof(float));
  3803. }
  3804. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 128));
  3805. }
  3806. }
  3807. [Test]
  3808. [TestRequiresDotsDebugOrCollectionChecks]
  3809. public void FixedList128Float_GenericHasExpectedCapacity()
  3810. {
  3811. var list = new FixedList128Bytes<float>();
  3812. var expectedCapacity = list.Capacity;
  3813. for(int i = 0; i < expectedCapacity; ++i)
  3814. list.Add((float)i);
  3815. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((float)expectedCapacity); });
  3816. }
  3817. [Test]
  3818. public unsafe void FixedList128Float_GenericAdd()
  3819. {
  3820. var list = new FixedList128Bytes<float>();
  3821. var capacity = list.Capacity;
  3822. for (var i = 0; i < capacity; ++i)
  3823. {
  3824. list.Add((float)i);
  3825. Assert.AreEqual(i + 1, list.Length);
  3826. Assert.AreEqual(i, list[i]);
  3827. }
  3828. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3829. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  3830. #endif
  3831. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  3832. }
  3833. [Test]
  3834. public unsafe void FixedList128Float_GenericAddRange()
  3835. {
  3836. var list = new FixedList128Bytes<float>();
  3837. var capacity = list.Capacity;
  3838. var items = stackalloc float[capacity];
  3839. for (var i = 0; i < capacity; ++i)
  3840. items[i] = (float)i;
  3841. var half = capacity / 2;
  3842. list.AddRange(items, half);
  3843. Assert.AreEqual(half, list.Length);
  3844. for (var i = 0; i < half; ++i)
  3845. Assert.AreEqual(i, list[i]);
  3846. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3847. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  3848. #endif
  3849. Assert.AreEqual(half, list.Length); // Verify length didn't change
  3850. }
  3851. [Test]
  3852. public unsafe void FixedList128Float_GenericAddNoResize()
  3853. {
  3854. var list = new FixedList128Bytes<float>();
  3855. var capacity = list.Capacity;
  3856. for (var i = 0; i < capacity; ++i)
  3857. {
  3858. list.AddNoResize((float)i);
  3859. Assert.AreEqual(i + 1, list.Length);
  3860. Assert.AreEqual(i, list[i]);
  3861. }
  3862. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3863. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  3864. #endif
  3865. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  3866. }
  3867. [Test]
  3868. public unsafe void FixedList128Float_GenericAddRangeNoResize()
  3869. {
  3870. var list = new FixedList128Bytes<float>();
  3871. var capacity = list.Capacity;
  3872. var items = stackalloc float[capacity];
  3873. for (var i = 0; i < capacity; ++i)
  3874. items[i] = (float)i;
  3875. var half = capacity / 2;
  3876. list.AddRangeNoResize(items, half);
  3877. Assert.AreEqual(half, list.Length);
  3878. for (var i = 0; i < half; ++i)
  3879. Assert.AreEqual(i, list[i]);
  3880. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3881. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  3882. #endif
  3883. Assert.AreEqual(half, list.Length); // Verify length didn't change
  3884. }
  3885. [Test]
  3886. public void FixedList128Float_GenericAddReplicate()
  3887. {
  3888. var list = new FixedList128Bytes<float>();
  3889. list.AddReplicate(42, 2);
  3890. Assert.AreEqual(2, list.Length);
  3891. foreach (var item in list)
  3892. Assert.AreEqual(42, item);
  3893. list.AddReplicate(42, 3);
  3894. Assert.AreEqual(5, list.Length);
  3895. foreach (var item in list)
  3896. Assert.AreEqual(42, item);
  3897. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  3898. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  3899. #endif
  3900. Assert.AreEqual(5, list.Length); // Verify length didn't change
  3901. }
  3902. [Test]
  3903. public void FixedList128Float_GenericInsertRangeWithBeginEnd()
  3904. {
  3905. var list = new FixedList128Bytes<float>() { 0, 3, 4 };
  3906. list.InsertRangeWithBeginEnd(1,3);
  3907. list[1] = 1;
  3908. list[2] = 2;
  3909. for(var i = 0; i < 5; ++i)
  3910. Assert.AreEqual(i, list[i]);
  3911. }
  3912. [Test]
  3913. public void FixedList128Float_GenericInsertRange()
  3914. {
  3915. var list = new FixedList128Bytes<float>() { 0, 3, 4 };
  3916. Assert.AreEqual(3, list.Length);
  3917. list.InsertRange(1, 2);
  3918. Assert.AreEqual(5, list.Length);
  3919. list[1] = 1;
  3920. list[2] = 2;
  3921. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  3922. Assert.AreEqual(5, list.Length);
  3923. for(var i = 0; i < 5; ++i)
  3924. Assert.AreEqual(i, list[i]);
  3925. }
  3926. [Test]
  3927. public void FixedList128Float_GenericRemoveRange()
  3928. {
  3929. var list = new FixedList128Bytes<float>() { 0, 3, 3, 1, 2 };
  3930. list.RemoveRange(1, 2);
  3931. for(var i = 0; i < 3; ++i)
  3932. Assert.AreEqual(i, list[i]);
  3933. }
  3934. [Test]
  3935. public void FixedList128Float_GenericInsert()
  3936. {
  3937. var list = new FixedList128Bytes<float>() { 0, 3, 4 };
  3938. list.Insert(1,1);
  3939. list.Insert(2,2);
  3940. for(var i = 0; i < 5; ++i)
  3941. Assert.AreEqual(i, list[i]);
  3942. }
  3943. [Test]
  3944. public void FixedList128Float_GenericRemoveAt()
  3945. {
  3946. var list = new FixedList128Bytes<float>() { 0, 3, 3, 1, 2 };
  3947. list.RemoveAt(1);
  3948. list.RemoveAt(1);
  3949. for(var i = 0; i < 3; ++i)
  3950. Assert.AreEqual(i, list[i]);
  3951. }
  3952. [Test]
  3953. public void FixedList128Float_GenericRemove()
  3954. {
  3955. var list = new FixedList128Bytes<float>() { 0, 3, 3, 1, 2 };
  3956. Assert.True(list.Remove((float)3));
  3957. Assert.True(list.Remove((float)3));
  3958. for(var i = 0; i < 3; ++i)
  3959. Assert.AreEqual(i, list[i]);
  3960. }
  3961. [Test]
  3962. public void FixedList128Float_GenericRemoveSwapBack()
  3963. {
  3964. var list = new FixedList128Bytes<float>() { 0, 3, 3, 2, 1 };
  3965. Assert.True(list.RemoveSwapBack((float)3));
  3966. Assert.True(list.RemoveSwapBack((float)3));
  3967. for(var i = 0; i < 3; ++i)
  3968. Assert.AreEqual(i, list[i]);
  3969. }
  3970. [Test]
  3971. public void FixedList128Float_GenericSort()
  3972. {
  3973. var list = new FixedList128Bytes<float>();
  3974. for(var i = 0; i < 5; ++i)
  3975. list.Add((float)(4-i));
  3976. list.Sort();
  3977. for(var i = 0; i < 5; ++i)
  3978. Assert.AreEqual(i, list[i]);
  3979. }
  3980. [Test]
  3981. public void FixedList128Float_GenericSortCustomComparer()
  3982. {
  3983. var list = new FixedList128Bytes<float>();
  3984. for(var i = 0; i < 5; ++i)
  3985. list.Add((float)(i));
  3986. list.Sort(new DescendingComparer<float>());
  3987. for(var i = 0; i < 5; ++i)
  3988. Assert.AreEqual(4-i, list[i]);
  3989. }
  3990. [Test]
  3991. public unsafe void FixedList128Float_IndexOf()
  3992. {
  3993. var list = new FixedList128Bytes<float>() { 123, 178 };
  3994. bool r0 = false, r1 = false, r2 = false;
  3995. GCAllocRecorder.ValidateNoGCAllocs(() =>
  3996. {
  3997. r0 = -1 != list.IndexOf((float)145);
  3998. r1 = list.Contains((float)123);
  3999. r2 = list.Contains((float)178);
  4000. });
  4001. Assert.False(r0);
  4002. Assert.True(r1);
  4003. Assert.True(r2);
  4004. }
  4005. [Test]
  4006. public void FixedList32Float_HasExpectedLayout()
  4007. {
  4008. var actual = new FixedList32Bytes<float>();
  4009. for(var i = 0; i < 7; ++i)
  4010. actual.Add((float)i);
  4011. unsafe
  4012. {
  4013. var e = stackalloc byte[32];
  4014. e[0] = (byte)((7 >> 0) & 0xFF);
  4015. e[1] = (byte)((7 >> 8) & 0xFF);
  4016. for(var i = 0; i < 7; ++i)
  4017. {
  4018. var s = (float)i;
  4019. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<float>() + sizeof(float) * i, &s, sizeof(float));
  4020. }
  4021. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 32));
  4022. }
  4023. }
  4024. [Test]
  4025. [TestRequiresDotsDebugOrCollectionChecks]
  4026. public void FixedList32Float_HasExpectedCapacity()
  4027. {
  4028. var list = new FixedList32Bytes<float>();
  4029. var expectedCapacity = list.Capacity;
  4030. for(int i = 0; i < expectedCapacity; ++i)
  4031. list.Add((float)i);
  4032. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((float)expectedCapacity); });
  4033. }
  4034. class ScriptableObjectFixedList32Float_ : UnityEngine.ScriptableObject
  4035. {
  4036. public FixedList32Bytes<float> List;
  4037. }
  4038. [Test]
  4039. public void FixedList32Float_Serializes()
  4040. {
  4041. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList32Float_ >();
  4042. for(int i = 0; i < a.List.Capacity; ++i)
  4043. a.List.Add((float)i);
  4044. var b = UnityEngine.Object.Instantiate(a);
  4045. CollectionAssert.AreEqual(a.List, b.List);
  4046. }
  4047. [Test]
  4048. public unsafe void FixedList32Float_Add()
  4049. {
  4050. var list = new FixedList32Bytes<float>();
  4051. var capacity = list.Capacity;
  4052. for (var i = 0; i < capacity; ++i)
  4053. {
  4054. list.Add((float)i);
  4055. Assert.AreEqual(i + 1, list.Length);
  4056. Assert.AreEqual(i, list[i]);
  4057. }
  4058. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4059. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  4060. #endif
  4061. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  4062. }
  4063. [Test]
  4064. public unsafe void FixedList32Float_AddRange()
  4065. {
  4066. var list = new FixedList32Bytes<float>();
  4067. var capacity = list.Capacity;
  4068. var items = stackalloc float[capacity];
  4069. for (var i = 0; i < capacity; ++i)
  4070. items[i] = (float)i;
  4071. var half = capacity / 2;
  4072. list.AddRange(items, half);
  4073. Assert.AreEqual(half, list.Length);
  4074. for (var i = 0; i < half; ++i)
  4075. Assert.AreEqual(i, list[i]);
  4076. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4077. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  4078. #endif
  4079. Assert.AreEqual(half, list.Length); // Verify length didn't change
  4080. }
  4081. [Test]
  4082. public unsafe void FixedList32Float_AddNoResize()
  4083. {
  4084. var list = new FixedList32Bytes<float>();
  4085. var capacity = list.Capacity;
  4086. for (var i = 0; i < capacity; ++i)
  4087. {
  4088. list.AddNoResize((float)i);
  4089. Assert.AreEqual(i + 1, list.Length);
  4090. Assert.AreEqual(i, list[i]);
  4091. }
  4092. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4093. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  4094. #endif
  4095. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  4096. }
  4097. [Test]
  4098. public unsafe void FixedList32Float_AddRangeNoResize()
  4099. {
  4100. var list = new FixedList32Bytes<float>();
  4101. var capacity = list.Capacity;
  4102. var items = stackalloc float[capacity];
  4103. for (var i = 0; i < capacity; ++i)
  4104. items[i] = (float)i;
  4105. var half = capacity / 2;
  4106. list.AddRangeNoResize(items, half);
  4107. Assert.AreEqual(half, list.Length);
  4108. for (var i = 0; i < half; ++i)
  4109. Assert.AreEqual(i, list[i]);
  4110. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4111. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  4112. #endif
  4113. Assert.AreEqual(half, list.Length); // Verify length didn't change
  4114. }
  4115. [Test]
  4116. public void FixedList32Float_AddReplicate()
  4117. {
  4118. var list = new FixedList32Bytes<float>();
  4119. list.AddReplicate(42, 2);
  4120. Assert.AreEqual(2, list.Length);
  4121. foreach (var item in list)
  4122. Assert.AreEqual(42, item);
  4123. list.AddReplicate(42, 3);
  4124. Assert.AreEqual(5, list.Length);
  4125. foreach (var item in list)
  4126. Assert.AreEqual(42, item);
  4127. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4128. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  4129. #endif
  4130. Assert.AreEqual(5, list.Length); // Verify length didn't change
  4131. }
  4132. [Test]
  4133. public void FixedList32Float_InsertRangeWithBeginEnd()
  4134. {
  4135. var list = new FixedList32Bytes<float>() { 0, 3, 4 };
  4136. list.InsertRangeWithBeginEnd(1,3);
  4137. list[1] = 1;
  4138. list[2] = 2;
  4139. for(var i = 0; i < 5; ++i)
  4140. Assert.AreEqual(i, list[i]);
  4141. }
  4142. [Test]
  4143. public void FixedList32Float_InsertRange()
  4144. {
  4145. var list = new FixedList32Bytes<float>() { 0, 3, 4 };
  4146. Assert.AreEqual(3, list.Length);
  4147. list.InsertRange(1, 2);
  4148. Assert.AreEqual(5, list.Length);
  4149. list[1] = 1;
  4150. list[2] = 2;
  4151. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  4152. Assert.AreEqual(5, list.Length);
  4153. for(var i = 0; i < 5; ++i)
  4154. Assert.AreEqual(i, list[i]);
  4155. }
  4156. private static void Expected(ref FixedList32Bytes<float> container, int expectedLength, int[] expected)
  4157. {
  4158. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  4159. Assert.AreEqual(container.Length, expectedLength);
  4160. for (var i = 0; i < container.Length; ++i)
  4161. {
  4162. Assert.AreEqual(expected[i], container[i]);
  4163. }
  4164. }
  4165. [Test]
  4166. public void FixedList32Float_RemoveAt()
  4167. {
  4168. var list = new FixedList32Bytes<float>() { 0, 3, 3, 1, 2 };
  4169. list.RemoveAt(1);
  4170. list.RemoveAt(1);
  4171. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4172. }
  4173. [Test]
  4174. public void FixedList32Float_Remove()
  4175. {
  4176. var list = new FixedList32Bytes<float>() { 0, 3, 3, 1, 2 };
  4177. Assert.True(list.Remove((float)3));
  4178. Assert.True(list.Remove((float)3));
  4179. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4180. }
  4181. [Test]
  4182. public void FixedList32Float_RemoveSwapBack()
  4183. {
  4184. var list = new FixedList32Bytes<float>() { 0, 3, 3, 2, 1 };
  4185. Assert.True(list.RemoveSwapBack((float)3));
  4186. Assert.True(list.RemoveSwapBack((float)3));
  4187. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4188. }
  4189. [Test]
  4190. public void FixedList32Float_RemoveRange()
  4191. {
  4192. var list = new FixedList32Bytes<float>() { 0, 3, 3, 1, 2 };
  4193. list.RemoveRange(1, 2);
  4194. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4195. }
  4196. [Test]
  4197. public void FixedList32Float_RemoveAtSwapBack()
  4198. {
  4199. var list = new FixedList32Bytes<float>() { 0, 3, 3, 1, 2 };
  4200. list.RemoveAtSwapBack(1);
  4201. list.RemoveAtSwapBack(1);
  4202. Expected(ref list, 3, new int[] { 0, 1, 3 });
  4203. }
  4204. [Test]
  4205. public void FixedList32Float_RemoveRangeSwapBack()
  4206. {
  4207. var list = new FixedList32Bytes<float>() { 0, 3, 3, 1, 2 };
  4208. list.RemoveRangeSwapBack(1, 2);
  4209. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4210. }
  4211. [Test]
  4212. public void FixedList32Float_Insert()
  4213. {
  4214. var list = new FixedList32Bytes<float>() { 0, 3, 4 };
  4215. list.Insert(1,1);
  4216. list.Insert(2,2);
  4217. for(var i = 0; i < 5; ++i)
  4218. Assert.AreEqual(i, list[i]);
  4219. }
  4220. [Test]
  4221. public void FixedList32Float_Sort()
  4222. {
  4223. var list = new FixedList32Bytes<float>();
  4224. for(var i = 0; i < 5; ++i)
  4225. list.Add((float)(4-i));
  4226. list.Sort();
  4227. for(var i = 0; i < 5; ++i)
  4228. Assert.AreEqual(i, list[i]);
  4229. }
  4230. [Test]
  4231. public void FixedList32Float_To_FixedList64Float()
  4232. {
  4233. var a = new FixedList32Bytes<float>();
  4234. for(var i = 0; i < 7; ++i)
  4235. a.Add((float)i);
  4236. var b = new FixedList64Bytes<float>(a);
  4237. for(var i = 0; i < 7; ++i)
  4238. Assert.AreEqual((float)i, b[i]);
  4239. }
  4240. [Test]
  4241. public void FixedList32Float_To_FixedList128Float()
  4242. {
  4243. var a = new FixedList32Bytes<float>();
  4244. for(var i = 0; i < 7; ++i)
  4245. a.Add((float)i);
  4246. var b = new FixedList128Bytes<float>(a);
  4247. for(var i = 0; i < 7; ++i)
  4248. Assert.AreEqual((float)i, b[i]);
  4249. }
  4250. [Test]
  4251. public void FixedList64Float_HasExpectedLayout()
  4252. {
  4253. var actual = new FixedList64Bytes<float>();
  4254. for(var i = 0; i < 15; ++i)
  4255. actual.Add((float)i);
  4256. unsafe
  4257. {
  4258. var e = stackalloc byte[64];
  4259. e[0] = (byte)((15 >> 0) & 0xFF);
  4260. e[1] = (byte)((15 >> 8) & 0xFF);
  4261. for(var i = 0; i < 15; ++i)
  4262. {
  4263. var s = (float)i;
  4264. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<float>() + sizeof(float) * i, &s, sizeof(float));
  4265. }
  4266. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 64));
  4267. }
  4268. }
  4269. [Test]
  4270. [TestRequiresDotsDebugOrCollectionChecks]
  4271. public void FixedList64Float_HasExpectedCapacity()
  4272. {
  4273. var list = new FixedList64Bytes<float>();
  4274. var expectedCapacity = list.Capacity;
  4275. for(int i = 0; i < expectedCapacity; ++i)
  4276. list.Add((float)i);
  4277. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((float)expectedCapacity); });
  4278. }
  4279. class ScriptableObjectFixedList64Float_ : UnityEngine.ScriptableObject
  4280. {
  4281. public FixedList64Bytes<float> List;
  4282. }
  4283. [Test]
  4284. public void FixedList64Float_Serializes()
  4285. {
  4286. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList64Float_ >();
  4287. for(int i = 0; i < a.List.Capacity; ++i)
  4288. a.List.Add((float)i);
  4289. var b = UnityEngine.Object.Instantiate(a);
  4290. CollectionAssert.AreEqual(a.List, b.List);
  4291. }
  4292. [Test]
  4293. public unsafe void FixedList64Float_Add()
  4294. {
  4295. var list = new FixedList64Bytes<float>();
  4296. var capacity = list.Capacity;
  4297. for (var i = 0; i < capacity; ++i)
  4298. {
  4299. list.Add((float)i);
  4300. Assert.AreEqual(i + 1, list.Length);
  4301. Assert.AreEqual(i, list[i]);
  4302. }
  4303. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4304. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  4305. #endif
  4306. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  4307. }
  4308. [Test]
  4309. public unsafe void FixedList64Float_AddRange()
  4310. {
  4311. var list = new FixedList64Bytes<float>();
  4312. var capacity = list.Capacity;
  4313. var items = stackalloc float[capacity];
  4314. for (var i = 0; i < capacity; ++i)
  4315. items[i] = (float)i;
  4316. var half = capacity / 2;
  4317. list.AddRange(items, half);
  4318. Assert.AreEqual(half, list.Length);
  4319. for (var i = 0; i < half; ++i)
  4320. Assert.AreEqual(i, list[i]);
  4321. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4322. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  4323. #endif
  4324. Assert.AreEqual(half, list.Length); // Verify length didn't change
  4325. }
  4326. [Test]
  4327. public unsafe void FixedList64Float_AddNoResize()
  4328. {
  4329. var list = new FixedList64Bytes<float>();
  4330. var capacity = list.Capacity;
  4331. for (var i = 0; i < capacity; ++i)
  4332. {
  4333. list.AddNoResize((float)i);
  4334. Assert.AreEqual(i + 1, list.Length);
  4335. Assert.AreEqual(i, list[i]);
  4336. }
  4337. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4338. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  4339. #endif
  4340. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  4341. }
  4342. [Test]
  4343. public unsafe void FixedList64Float_AddRangeNoResize()
  4344. {
  4345. var list = new FixedList64Bytes<float>();
  4346. var capacity = list.Capacity;
  4347. var items = stackalloc float[capacity];
  4348. for (var i = 0; i < capacity; ++i)
  4349. items[i] = (float)i;
  4350. var half = capacity / 2;
  4351. list.AddRangeNoResize(items, half);
  4352. Assert.AreEqual(half, list.Length);
  4353. for (var i = 0; i < half; ++i)
  4354. Assert.AreEqual(i, list[i]);
  4355. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4356. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  4357. #endif
  4358. Assert.AreEqual(half, list.Length); // Verify length didn't change
  4359. }
  4360. [Test]
  4361. public void FixedList64Float_AddReplicate()
  4362. {
  4363. var list = new FixedList64Bytes<float>();
  4364. list.AddReplicate(42, 2);
  4365. Assert.AreEqual(2, list.Length);
  4366. foreach (var item in list)
  4367. Assert.AreEqual(42, item);
  4368. list.AddReplicate(42, 3);
  4369. Assert.AreEqual(5, list.Length);
  4370. foreach (var item in list)
  4371. Assert.AreEqual(42, item);
  4372. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4373. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  4374. #endif
  4375. Assert.AreEqual(5, list.Length); // Verify length didn't change
  4376. }
  4377. [Test]
  4378. public void FixedList64Float_InsertRangeWithBeginEnd()
  4379. {
  4380. var list = new FixedList64Bytes<float>() { 0, 3, 4 };
  4381. list.InsertRangeWithBeginEnd(1,3);
  4382. list[1] = 1;
  4383. list[2] = 2;
  4384. for(var i = 0; i < 5; ++i)
  4385. Assert.AreEqual(i, list[i]);
  4386. }
  4387. [Test]
  4388. public void FixedList64Float_InsertRange()
  4389. {
  4390. var list = new FixedList64Bytes<float>() { 0, 3, 4 };
  4391. Assert.AreEqual(3, list.Length);
  4392. list.InsertRange(1, 2);
  4393. Assert.AreEqual(5, list.Length);
  4394. list[1] = 1;
  4395. list[2] = 2;
  4396. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  4397. Assert.AreEqual(5, list.Length);
  4398. for(var i = 0; i < 5; ++i)
  4399. Assert.AreEqual(i, list[i]);
  4400. }
  4401. private static void Expected(ref FixedList64Bytes<float> container, int expectedLength, int[] expected)
  4402. {
  4403. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  4404. Assert.AreEqual(container.Length, expectedLength);
  4405. for (var i = 0; i < container.Length; ++i)
  4406. {
  4407. Assert.AreEqual(expected[i], container[i]);
  4408. }
  4409. }
  4410. [Test]
  4411. public void FixedList64Float_RemoveAt()
  4412. {
  4413. var list = new FixedList64Bytes<float>() { 0, 3, 3, 1, 2 };
  4414. list.RemoveAt(1);
  4415. list.RemoveAt(1);
  4416. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4417. }
  4418. [Test]
  4419. public void FixedList64Float_Remove()
  4420. {
  4421. var list = new FixedList64Bytes<float>() { 0, 3, 3, 1, 2 };
  4422. Assert.True(list.Remove((float)3));
  4423. Assert.True(list.Remove((float)3));
  4424. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4425. }
  4426. [Test]
  4427. public void FixedList64Float_RemoveSwapBack()
  4428. {
  4429. var list = new FixedList64Bytes<float>() { 0, 3, 3, 2, 1 };
  4430. Assert.True(list.RemoveSwapBack((float)3));
  4431. Assert.True(list.RemoveSwapBack((float)3));
  4432. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4433. }
  4434. [Test]
  4435. public void FixedList64Float_RemoveRange()
  4436. {
  4437. var list = new FixedList64Bytes<float>() { 0, 3, 3, 1, 2 };
  4438. list.RemoveRange(1, 2);
  4439. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4440. }
  4441. [Test]
  4442. public void FixedList64Float_RemoveAtSwapBack()
  4443. {
  4444. var list = new FixedList64Bytes<float>() { 0, 3, 3, 1, 2 };
  4445. list.RemoveAtSwapBack(1);
  4446. list.RemoveAtSwapBack(1);
  4447. Expected(ref list, 3, new int[] { 0, 1, 3 });
  4448. }
  4449. [Test]
  4450. public void FixedList64Float_RemoveRangeSwapBack()
  4451. {
  4452. var list = new FixedList64Bytes<float>() { 0, 3, 3, 1, 2 };
  4453. list.RemoveRangeSwapBack(1, 2);
  4454. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4455. }
  4456. [Test]
  4457. public void FixedList64Float_Insert()
  4458. {
  4459. var list = new FixedList64Bytes<float>() { 0, 3, 4 };
  4460. list.Insert(1,1);
  4461. list.Insert(2,2);
  4462. for(var i = 0; i < 5; ++i)
  4463. Assert.AreEqual(i, list[i]);
  4464. }
  4465. [Test]
  4466. public void FixedList64Float_Sort()
  4467. {
  4468. var list = new FixedList64Bytes<float>();
  4469. for(var i = 0; i < 5; ++i)
  4470. list.Add((float)(4-i));
  4471. list.Sort();
  4472. for(var i = 0; i < 5; ++i)
  4473. Assert.AreEqual(i, list[i]);
  4474. }
  4475. [Test]
  4476. public void FixedList64Float_To_FixedList32Float()
  4477. {
  4478. var a = new FixedList64Bytes<float>();
  4479. for(var i = 0; i < 15; ++i)
  4480. a.Add((float)i);
  4481. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4482. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList32Bytes<float>(a); } );
  4483. #endif
  4484. }
  4485. [Test]
  4486. public void FixedList64Float_To_FixedList128Float()
  4487. {
  4488. var a = new FixedList64Bytes<float>();
  4489. for(var i = 0; i < 15; ++i)
  4490. a.Add((float)i);
  4491. var b = new FixedList128Bytes<float>(a);
  4492. for(var i = 0; i < 15; ++i)
  4493. Assert.AreEqual((float)i, b[i]);
  4494. }
  4495. [Test]
  4496. public void FixedList128Float_HasExpectedLayout()
  4497. {
  4498. var actual = new FixedList128Bytes<float>();
  4499. for(var i = 0; i < 31; ++i)
  4500. actual.Add((float)i);
  4501. unsafe
  4502. {
  4503. var e = stackalloc byte[128];
  4504. e[0] = (byte)((31 >> 0) & 0xFF);
  4505. e[1] = (byte)((31 >> 8) & 0xFF);
  4506. for(var i = 0; i < 31; ++i)
  4507. {
  4508. var s = (float)i;
  4509. UnsafeUtility.MemCpy(e + 2 + FixedList.PaddingBytes<float>() + sizeof(float) * i, &s, sizeof(float));
  4510. }
  4511. Assert.AreEqual(0, UnsafeUtility.MemCmp(e, &actual.data, 128));
  4512. }
  4513. }
  4514. [Test]
  4515. [TestRequiresDotsDebugOrCollectionChecks]
  4516. public void FixedList128Float_HasExpectedCapacity()
  4517. {
  4518. var list = new FixedList128Bytes<float>();
  4519. var expectedCapacity = list.Capacity;
  4520. for(int i = 0; i < expectedCapacity; ++i)
  4521. list.Add((float)i);
  4522. Assert.Throws<IndexOutOfRangeException> (() => { list.Add((float)expectedCapacity); });
  4523. }
  4524. class ScriptableObjectFixedList128Float_ : UnityEngine.ScriptableObject
  4525. {
  4526. public FixedList128Bytes<float> List;
  4527. }
  4528. [Test]
  4529. public void FixedList128Float_Serializes()
  4530. {
  4531. var a = UnityEngine.ScriptableObject.CreateInstance<ScriptableObjectFixedList128Float_ >();
  4532. for(int i = 0; i < a.List.Capacity; ++i)
  4533. a.List.Add((float)i);
  4534. var b = UnityEngine.Object.Instantiate(a);
  4535. CollectionAssert.AreEqual(a.List, b.List);
  4536. }
  4537. [Test]
  4538. public unsafe void FixedList128Float_Add()
  4539. {
  4540. var list = new FixedList128Bytes<float>();
  4541. var capacity = list.Capacity;
  4542. for (var i = 0; i < capacity; ++i)
  4543. {
  4544. list.Add((float)i);
  4545. Assert.AreEqual(i + 1, list.Length);
  4546. Assert.AreEqual(i, list[i]);
  4547. }
  4548. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4549. Assert.Throws<IndexOutOfRangeException>(() => { list.Add(0); });
  4550. #endif
  4551. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  4552. }
  4553. [Test]
  4554. public unsafe void FixedList128Float_AddRange()
  4555. {
  4556. var list = new FixedList128Bytes<float>();
  4557. var capacity = list.Capacity;
  4558. var items = stackalloc float[capacity];
  4559. for (var i = 0; i < capacity; ++i)
  4560. items[i] = (float)i;
  4561. var half = capacity / 2;
  4562. list.AddRange(items, half);
  4563. Assert.AreEqual(half, list.Length);
  4564. for (var i = 0; i < half; ++i)
  4565. Assert.AreEqual(i, list[i]);
  4566. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4567. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRange(items, capacity); });
  4568. #endif
  4569. Assert.AreEqual(half, list.Length); // Verify length didn't change
  4570. }
  4571. [Test]
  4572. public unsafe void FixedList128Float_AddNoResize()
  4573. {
  4574. var list = new FixedList128Bytes<float>();
  4575. var capacity = list.Capacity;
  4576. for (var i = 0; i < capacity; ++i)
  4577. {
  4578. list.AddNoResize((float)i);
  4579. Assert.AreEqual(i + 1, list.Length);
  4580. Assert.AreEqual(i, list[i]);
  4581. }
  4582. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4583. Assert.Throws<IndexOutOfRangeException>(() => { list.AddNoResize(0); });
  4584. #endif
  4585. Assert.AreEqual(capacity, list.Length); // Verify length didn't change
  4586. }
  4587. [Test]
  4588. public unsafe void FixedList128Float_AddRangeNoResize()
  4589. {
  4590. var list = new FixedList128Bytes<float>();
  4591. var capacity = list.Capacity;
  4592. var items = stackalloc float[capacity];
  4593. for (var i = 0; i < capacity; ++i)
  4594. items[i] = (float)i;
  4595. var half = capacity / 2;
  4596. list.AddRangeNoResize(items, half);
  4597. Assert.AreEqual(half, list.Length);
  4598. for (var i = 0; i < half; ++i)
  4599. Assert.AreEqual(i, list[i]);
  4600. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4601. Assert.Throws<IndexOutOfRangeException>(() => { list.AddRangeNoResize(items, capacity); });
  4602. #endif
  4603. Assert.AreEqual(half, list.Length); // Verify length didn't change
  4604. }
  4605. [Test]
  4606. public void FixedList128Float_AddReplicate()
  4607. {
  4608. var list = new FixedList128Bytes<float>();
  4609. list.AddReplicate(42, 2);
  4610. Assert.AreEqual(2, list.Length);
  4611. foreach (var item in list)
  4612. Assert.AreEqual(42, item);
  4613. list.AddReplicate(42, 3);
  4614. Assert.AreEqual(5, list.Length);
  4615. foreach (var item in list)
  4616. Assert.AreEqual(42, item);
  4617. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4618. Assert.Throws<IndexOutOfRangeException>(() => { list.AddReplicate(42, 1000); });
  4619. #endif
  4620. Assert.AreEqual(5, list.Length); // Verify length didn't change
  4621. }
  4622. [Test]
  4623. public void FixedList128Float_InsertRangeWithBeginEnd()
  4624. {
  4625. var list = new FixedList128Bytes<float>() { 0, 3, 4 };
  4626. list.InsertRangeWithBeginEnd(1,3);
  4627. list[1] = 1;
  4628. list[2] = 2;
  4629. for(var i = 0; i < 5; ++i)
  4630. Assert.AreEqual(i, list[i]);
  4631. }
  4632. [Test]
  4633. public void FixedList128Float_InsertRange()
  4634. {
  4635. var list = new FixedList128Bytes<float>() { 0, 3, 4 };
  4636. Assert.AreEqual(3, list.Length);
  4637. list.InsertRange(1, 2);
  4638. Assert.AreEqual(5, list.Length);
  4639. list[1] = 1;
  4640. list[2] = 2;
  4641. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  4642. Assert.AreEqual(5, list.Length);
  4643. for(var i = 0; i < 5; ++i)
  4644. Assert.AreEqual(i, list[i]);
  4645. }
  4646. private static void Expected(ref FixedList128Bytes<float> container, int expectedLength, int[] expected)
  4647. {
  4648. Assert.AreEqual(expectedLength == 0, container.IsEmpty);
  4649. Assert.AreEqual(container.Length, expectedLength);
  4650. for (var i = 0; i < container.Length; ++i)
  4651. {
  4652. Assert.AreEqual(expected[i], container[i]);
  4653. }
  4654. }
  4655. [Test]
  4656. public void FixedList128Float_RemoveAt()
  4657. {
  4658. var list = new FixedList128Bytes<float>() { 0, 3, 3, 1, 2 };
  4659. list.RemoveAt(1);
  4660. list.RemoveAt(1);
  4661. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4662. }
  4663. [Test]
  4664. public void FixedList128Float_Remove()
  4665. {
  4666. var list = new FixedList128Bytes<float>() { 0, 3, 3, 1, 2 };
  4667. Assert.True(list.Remove((float)3));
  4668. Assert.True(list.Remove((float)3));
  4669. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4670. }
  4671. [Test]
  4672. public void FixedList128Float_RemoveSwapBack()
  4673. {
  4674. var list = new FixedList128Bytes<float>() { 0, 3, 3, 2, 1 };
  4675. Assert.True(list.RemoveSwapBack((float)3));
  4676. Assert.True(list.RemoveSwapBack((float)3));
  4677. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4678. }
  4679. [Test]
  4680. public void FixedList128Float_RemoveRange()
  4681. {
  4682. var list = new FixedList128Bytes<float>() { 0, 3, 3, 1, 2 };
  4683. list.RemoveRange(1, 2);
  4684. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4685. }
  4686. [Test]
  4687. public void FixedList128Float_RemoveAtSwapBack()
  4688. {
  4689. var list = new FixedList128Bytes<float>() { 0, 3, 3, 1, 2 };
  4690. list.RemoveAtSwapBack(1);
  4691. list.RemoveAtSwapBack(1);
  4692. Expected(ref list, 3, new int[] { 0, 1, 3 });
  4693. }
  4694. [Test]
  4695. public void FixedList128Float_RemoveRangeSwapBack()
  4696. {
  4697. var list = new FixedList128Bytes<float>() { 0, 3, 3, 1, 2 };
  4698. list.RemoveRangeSwapBack(1, 2);
  4699. Expected(ref list, 3, new int[] { 0, 1, 2 });
  4700. }
  4701. [Test]
  4702. public void FixedList128Float_Insert()
  4703. {
  4704. var list = new FixedList128Bytes<float>() { 0, 3, 4 };
  4705. list.Insert(1,1);
  4706. list.Insert(2,2);
  4707. for(var i = 0; i < 5; ++i)
  4708. Assert.AreEqual(i, list[i]);
  4709. }
  4710. [Test]
  4711. public void FixedList128Float_Sort()
  4712. {
  4713. var list = new FixedList128Bytes<float>();
  4714. for(var i = 0; i < 5; ++i)
  4715. list.Add((float)(4-i));
  4716. list.Sort();
  4717. for(var i = 0; i < 5; ++i)
  4718. Assert.AreEqual(i, list[i]);
  4719. }
  4720. [Test]
  4721. public void FixedList128Float_To_FixedList32Float()
  4722. {
  4723. var a = new FixedList128Bytes<float>();
  4724. for(var i = 0; i < 31; ++i)
  4725. a.Add((float)i);
  4726. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4727. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList32Bytes<float>(a); } );
  4728. #endif
  4729. }
  4730. [Test]
  4731. public void FixedList128Float_To_FixedList64Float()
  4732. {
  4733. var a = new FixedList128Bytes<float>();
  4734. for(var i = 0; i < 31; ++i)
  4735. a.Add((float)i);
  4736. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  4737. Assert.Throws<IndexOutOfRangeException> (() => { var b = new FixedList64Bytes<float>(a); } );
  4738. #endif
  4739. }
  4740. }