GPBDictionaryTests+UInt64.m 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import <Foundation/Foundation.h>
  31. #import <XCTest/XCTest.h>
  32. #import "GPBDictionary.h"
  33. #import "GPBTestUtilities.h"
  34. #import "google/protobuf/UnittestRuntimeProto2.pbobjc.h"
  35. // Pull in the macros (using an external file because expanding all tests
  36. // in a single file makes a file that is failing to work with within Xcode.
  37. //%PDDM-IMPORT-DEFINES GPBDictionaryTests.pddm
  38. //%PDDM-EXPAND TEST_FOR_POD_KEY(UInt64, uint64_t, 31ULL, 32ULL, 33ULL, 34ULL)
  39. // This block of code is generated, do not edit it directly.
  40. // To let the testing macros work, add some extra methods to simplify things.
  41. @interface GPBUInt64EnumDictionary (TestingTweak)
  42. - (instancetype)initWithEnums:(const int32_t [])values
  43. forKeys:(const uint64_t [])keys
  44. count:(NSUInteger)count;
  45. @end
  46. static BOOL TestingEnum_IsValidValue(int32_t value) {
  47. switch (value) {
  48. case 700:
  49. case 701:
  50. case 702:
  51. case 703:
  52. return YES;
  53. default:
  54. return NO;
  55. }
  56. }
  57. @implementation GPBUInt64EnumDictionary (TestingTweak)
  58. - (instancetype)initWithEnums:(const int32_t [])values
  59. forKeys:(const uint64_t [])keys
  60. count:(NSUInteger)count {
  61. return [self initWithValidationFunction:TestingEnum_IsValidValue
  62. rawValues:values
  63. forKeys:keys
  64. count:count];
  65. }
  66. @end
  67. #pragma mark - UInt64 -> UInt32
  68. @interface GPBUInt64UInt32DictionaryTests : XCTestCase
  69. @end
  70. @implementation GPBUInt64UInt32DictionaryTests
  71. - (void)testEmpty {
  72. GPBUInt64UInt32Dictionary *dict = [[GPBUInt64UInt32Dictionary alloc] init];
  73. XCTAssertNotNil(dict);
  74. XCTAssertEqual(dict.count, 0U);
  75. XCTAssertFalse([dict getUInt32:NULL forKey:31ULL]);
  76. [dict enumerateKeysAndUInt32sUsingBlock:^(uint64_t aKey, uint32_t aValue, BOOL *stop) {
  77. #pragma unused(aKey, aValue, stop)
  78. XCTFail(@"Shouldn't get here!");
  79. }];
  80. [dict release];
  81. }
  82. - (void)testOne {
  83. GPBUInt64UInt32Dictionary *dict = [[GPBUInt64UInt32Dictionary alloc] init];
  84. [dict setUInt32:100U forKey:31ULL];
  85. XCTAssertNotNil(dict);
  86. XCTAssertEqual(dict.count, 1U);
  87. uint32_t value;
  88. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  89. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  90. XCTAssertEqual(value, 100U);
  91. XCTAssertFalse([dict getUInt32:NULL forKey:32ULL]);
  92. [dict enumerateKeysAndUInt32sUsingBlock:^(uint64_t aKey, uint32_t aValue, BOOL *stop) {
  93. XCTAssertEqual(aKey, 31ULL);
  94. XCTAssertEqual(aValue, 100U);
  95. XCTAssertNotEqual(stop, NULL);
  96. }];
  97. [dict release];
  98. }
  99. - (void)testBasics {
  100. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  101. const uint32_t kValues[] = { 100U, 101U, 102U };
  102. GPBUInt64UInt32Dictionary *dict =
  103. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues
  104. forKeys:kKeys
  105. count:GPBARRAYSIZE(kValues)];
  106. XCTAssertNotNil(dict);
  107. XCTAssertEqual(dict.count, 3U);
  108. uint32_t value;
  109. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  110. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  111. XCTAssertEqual(value, 100U);
  112. XCTAssertTrue([dict getUInt32:NULL forKey:32ULL]);
  113. XCTAssertTrue([dict getUInt32:&value forKey:32ULL]);
  114. XCTAssertEqual(value, 101U);
  115. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  116. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  117. XCTAssertEqual(value, 102U);
  118. XCTAssertFalse([dict getUInt32:NULL forKey:34ULL]);
  119. __block NSUInteger idx = 0;
  120. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  121. uint32_t *seenValues = malloc(3 * sizeof(uint32_t));
  122. [dict enumerateKeysAndUInt32sUsingBlock:^(uint64_t aKey, uint32_t aValue, BOOL *stop) {
  123. XCTAssertLessThan(idx, 3U);
  124. seenKeys[idx] = aKey;
  125. seenValues[idx] = aValue;
  126. XCTAssertNotEqual(stop, NULL);
  127. ++idx;
  128. }];
  129. for (int i = 0; i < 3; ++i) {
  130. BOOL foundKey = NO;
  131. for (int j = 0; (j < 3) && !foundKey; ++j) {
  132. if (kKeys[i] == seenKeys[j]) {
  133. foundKey = YES;
  134. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  135. }
  136. }
  137. XCTAssertTrue(foundKey, @"i = %d", i);
  138. }
  139. free(seenKeys);
  140. free(seenValues);
  141. // Stopping the enumeration.
  142. idx = 0;
  143. [dict enumerateKeysAndUInt32sUsingBlock:^(uint64_t aKey, uint32_t aValue, BOOL *stop) {
  144. #pragma unused(aKey, aValue)
  145. if (idx == 1) *stop = YES;
  146. XCTAssertNotEqual(idx, 2U);
  147. ++idx;
  148. }];
  149. [dict release];
  150. }
  151. - (void)testEquality {
  152. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  153. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  154. const uint32_t kValues1[] = { 100U, 101U, 102U };
  155. const uint32_t kValues2[] = { 100U, 103U, 102U };
  156. const uint32_t kValues3[] = { 100U, 101U, 102U, 103U };
  157. GPBUInt64UInt32Dictionary *dict1 =
  158. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues1
  159. forKeys:kKeys1
  160. count:GPBARRAYSIZE(kValues1)];
  161. XCTAssertNotNil(dict1);
  162. GPBUInt64UInt32Dictionary *dict1prime =
  163. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues1
  164. forKeys:kKeys1
  165. count:GPBARRAYSIZE(kValues1)];
  166. XCTAssertNotNil(dict1prime);
  167. GPBUInt64UInt32Dictionary *dict2 =
  168. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues2
  169. forKeys:kKeys1
  170. count:GPBARRAYSIZE(kValues2)];
  171. XCTAssertNotNil(dict2);
  172. GPBUInt64UInt32Dictionary *dict3 =
  173. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues1
  174. forKeys:kKeys2
  175. count:GPBARRAYSIZE(kValues1)];
  176. XCTAssertNotNil(dict3);
  177. GPBUInt64UInt32Dictionary *dict4 =
  178. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues3
  179. forKeys:kKeys1
  180. count:GPBARRAYSIZE(kValues3)];
  181. XCTAssertNotNil(dict4);
  182. // 1/1Prime should be different objects, but equal.
  183. XCTAssertNotEqual(dict1, dict1prime);
  184. XCTAssertEqualObjects(dict1, dict1prime);
  185. // Equal, so they must have same hash.
  186. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  187. // 2 is same keys, different values; not equal.
  188. XCTAssertNotEqualObjects(dict1, dict2);
  189. // 3 is different keys, same values; not equal.
  190. XCTAssertNotEqualObjects(dict1, dict3);
  191. // 4 extra pair; not equal
  192. XCTAssertNotEqualObjects(dict1, dict4);
  193. [dict1 release];
  194. [dict1prime release];
  195. [dict2 release];
  196. [dict3 release];
  197. [dict4 release];
  198. }
  199. - (void)testCopy {
  200. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  201. const uint32_t kValues[] = { 100U, 101U, 102U, 103U };
  202. GPBUInt64UInt32Dictionary *dict =
  203. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues
  204. forKeys:kKeys
  205. count:GPBARRAYSIZE(kValues)];
  206. XCTAssertNotNil(dict);
  207. GPBUInt64UInt32Dictionary *dict2 = [dict copy];
  208. XCTAssertNotNil(dict2);
  209. // Should be new object but equal.
  210. XCTAssertNotEqual(dict, dict2);
  211. XCTAssertEqualObjects(dict, dict2);
  212. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64UInt32Dictionary class]]);
  213. [dict2 release];
  214. [dict release];
  215. }
  216. - (void)testDictionaryFromDictionary {
  217. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  218. const uint32_t kValues[] = { 100U, 101U, 102U, 103U };
  219. GPBUInt64UInt32Dictionary *dict =
  220. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues
  221. forKeys:kKeys
  222. count:GPBARRAYSIZE(kValues)];
  223. XCTAssertNotNil(dict);
  224. GPBUInt64UInt32Dictionary *dict2 =
  225. [[GPBUInt64UInt32Dictionary alloc] initWithDictionary:dict];
  226. XCTAssertNotNil(dict2);
  227. // Should be new pointer, but equal objects.
  228. XCTAssertNotEqual(dict, dict2);
  229. XCTAssertEqualObjects(dict, dict2);
  230. [dict2 release];
  231. [dict release];
  232. }
  233. - (void)testAdds {
  234. GPBUInt64UInt32Dictionary *dict = [[GPBUInt64UInt32Dictionary alloc] init];
  235. XCTAssertNotNil(dict);
  236. XCTAssertEqual(dict.count, 0U);
  237. [dict setUInt32:100U forKey:31ULL];
  238. XCTAssertEqual(dict.count, 1U);
  239. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  240. const uint32_t kValues[] = { 101U, 102U, 103U };
  241. GPBUInt64UInt32Dictionary *dict2 =
  242. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues
  243. forKeys:kKeys
  244. count:GPBARRAYSIZE(kValues)];
  245. XCTAssertNotNil(dict2);
  246. [dict addEntriesFromDictionary:dict2];
  247. XCTAssertEqual(dict.count, 4U);
  248. uint32_t value;
  249. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  250. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  251. XCTAssertEqual(value, 100U);
  252. XCTAssertTrue([dict getUInt32:NULL forKey:32ULL]);
  253. XCTAssertTrue([dict getUInt32:&value forKey:32ULL]);
  254. XCTAssertEqual(value, 101U);
  255. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  256. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  257. XCTAssertEqual(value, 102U);
  258. XCTAssertTrue([dict getUInt32:NULL forKey:34ULL]);
  259. XCTAssertTrue([dict getUInt32:&value forKey:34ULL]);
  260. XCTAssertEqual(value, 103U);
  261. [dict2 release];
  262. [dict release];
  263. }
  264. - (void)testRemove {
  265. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  266. const uint32_t kValues[] = { 100U, 101U, 102U, 103U };
  267. GPBUInt64UInt32Dictionary *dict =
  268. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues
  269. forKeys:kKeys
  270. count:GPBARRAYSIZE(kValues)];
  271. XCTAssertNotNil(dict);
  272. XCTAssertEqual(dict.count, 4U);
  273. [dict removeUInt32ForKey:32ULL];
  274. XCTAssertEqual(dict.count, 3U);
  275. uint32_t value;
  276. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  277. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  278. XCTAssertEqual(value, 100U);
  279. XCTAssertFalse([dict getUInt32:NULL forKey:32ULL]);
  280. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  281. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  282. XCTAssertEqual(value, 102U);
  283. XCTAssertTrue([dict getUInt32:NULL forKey:34ULL]);
  284. XCTAssertTrue([dict getUInt32:&value forKey:34ULL]);
  285. XCTAssertEqual(value, 103U);
  286. // Remove again does nothing.
  287. [dict removeUInt32ForKey:32ULL];
  288. XCTAssertEqual(dict.count, 3U);
  289. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  290. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  291. XCTAssertEqual(value, 100U);
  292. XCTAssertFalse([dict getUInt32:NULL forKey:32ULL]);
  293. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  294. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  295. XCTAssertEqual(value, 102U);
  296. XCTAssertTrue([dict getUInt32:NULL forKey:34ULL]);
  297. XCTAssertTrue([dict getUInt32:&value forKey:34ULL]);
  298. XCTAssertEqual(value, 103U);
  299. [dict removeUInt32ForKey:34ULL];
  300. XCTAssertEqual(dict.count, 2U);
  301. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  302. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  303. XCTAssertEqual(value, 100U);
  304. XCTAssertFalse([dict getUInt32:NULL forKey:32ULL]);
  305. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  306. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  307. XCTAssertEqual(value, 102U);
  308. XCTAssertFalse([dict getUInt32:NULL forKey:34ULL]);
  309. [dict removeAll];
  310. XCTAssertEqual(dict.count, 0U);
  311. XCTAssertFalse([dict getUInt32:NULL forKey:31ULL]);
  312. XCTAssertFalse([dict getUInt32:NULL forKey:32ULL]);
  313. XCTAssertFalse([dict getUInt32:NULL forKey:33ULL]);
  314. XCTAssertFalse([dict getUInt32:NULL forKey:34ULL]);
  315. [dict release];
  316. }
  317. - (void)testInplaceMutation {
  318. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  319. const uint32_t kValues[] = { 100U, 101U, 102U, 103U };
  320. GPBUInt64UInt32Dictionary *dict =
  321. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues
  322. forKeys:kKeys
  323. count:GPBARRAYSIZE(kValues)];
  324. XCTAssertNotNil(dict);
  325. XCTAssertEqual(dict.count, 4U);
  326. uint32_t value;
  327. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  328. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  329. XCTAssertEqual(value, 100U);
  330. XCTAssertTrue([dict getUInt32:NULL forKey:32ULL]);
  331. XCTAssertTrue([dict getUInt32:&value forKey:32ULL]);
  332. XCTAssertEqual(value, 101U);
  333. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  334. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  335. XCTAssertEqual(value, 102U);
  336. XCTAssertTrue([dict getUInt32:NULL forKey:34ULL]);
  337. XCTAssertTrue([dict getUInt32:&value forKey:34ULL]);
  338. XCTAssertEqual(value, 103U);
  339. [dict setUInt32:103U forKey:31ULL];
  340. XCTAssertEqual(dict.count, 4U);
  341. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  342. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  343. XCTAssertEqual(value, 103U);
  344. XCTAssertTrue([dict getUInt32:NULL forKey:32ULL]);
  345. XCTAssertTrue([dict getUInt32:&value forKey:32ULL]);
  346. XCTAssertEqual(value, 101U);
  347. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  348. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  349. XCTAssertEqual(value, 102U);
  350. XCTAssertTrue([dict getUInt32:NULL forKey:34ULL]);
  351. XCTAssertTrue([dict getUInt32:&value forKey:34ULL]);
  352. XCTAssertEqual(value, 103U);
  353. [dict setUInt32:101U forKey:34ULL];
  354. XCTAssertEqual(dict.count, 4U);
  355. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  356. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  357. XCTAssertEqual(value, 103U);
  358. XCTAssertTrue([dict getUInt32:NULL forKey:32ULL]);
  359. XCTAssertTrue([dict getUInt32:&value forKey:32ULL]);
  360. XCTAssertEqual(value, 101U);
  361. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  362. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  363. XCTAssertEqual(value, 102U);
  364. XCTAssertTrue([dict getUInt32:NULL forKey:34ULL]);
  365. XCTAssertTrue([dict getUInt32:&value forKey:34ULL]);
  366. XCTAssertEqual(value, 101U);
  367. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  368. const uint32_t kValues2[] = { 102U, 100U };
  369. GPBUInt64UInt32Dictionary *dict2 =
  370. [[GPBUInt64UInt32Dictionary alloc] initWithUInt32s:kValues2
  371. forKeys:kKeys2
  372. count:GPBARRAYSIZE(kValues2)];
  373. XCTAssertNotNil(dict2);
  374. [dict addEntriesFromDictionary:dict2];
  375. XCTAssertEqual(dict.count, 4U);
  376. XCTAssertTrue([dict getUInt32:NULL forKey:31ULL]);
  377. XCTAssertTrue([dict getUInt32:&value forKey:31ULL]);
  378. XCTAssertEqual(value, 103U);
  379. XCTAssertTrue([dict getUInt32:NULL forKey:32ULL]);
  380. XCTAssertTrue([dict getUInt32:&value forKey:32ULL]);
  381. XCTAssertEqual(value, 102U);
  382. XCTAssertTrue([dict getUInt32:NULL forKey:33ULL]);
  383. XCTAssertTrue([dict getUInt32:&value forKey:33ULL]);
  384. XCTAssertEqual(value, 100U);
  385. XCTAssertTrue([dict getUInt32:NULL forKey:34ULL]);
  386. XCTAssertTrue([dict getUInt32:&value forKey:34ULL]);
  387. XCTAssertEqual(value, 101U);
  388. [dict2 release];
  389. [dict release];
  390. }
  391. @end
  392. #pragma mark - UInt64 -> Int32
  393. @interface GPBUInt64Int32DictionaryTests : XCTestCase
  394. @end
  395. @implementation GPBUInt64Int32DictionaryTests
  396. - (void)testEmpty {
  397. GPBUInt64Int32Dictionary *dict = [[GPBUInt64Int32Dictionary alloc] init];
  398. XCTAssertNotNil(dict);
  399. XCTAssertEqual(dict.count, 0U);
  400. XCTAssertFalse([dict getInt32:NULL forKey:31ULL]);
  401. [dict enumerateKeysAndInt32sUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  402. #pragma unused(aKey, aValue, stop)
  403. XCTFail(@"Shouldn't get here!");
  404. }];
  405. [dict release];
  406. }
  407. - (void)testOne {
  408. GPBUInt64Int32Dictionary *dict = [[GPBUInt64Int32Dictionary alloc] init];
  409. [dict setInt32:200 forKey:31ULL];
  410. XCTAssertNotNil(dict);
  411. XCTAssertEqual(dict.count, 1U);
  412. int32_t value;
  413. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  414. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  415. XCTAssertEqual(value, 200);
  416. XCTAssertFalse([dict getInt32:NULL forKey:32ULL]);
  417. [dict enumerateKeysAndInt32sUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  418. XCTAssertEqual(aKey, 31ULL);
  419. XCTAssertEqual(aValue, 200);
  420. XCTAssertNotEqual(stop, NULL);
  421. }];
  422. [dict release];
  423. }
  424. - (void)testBasics {
  425. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  426. const int32_t kValues[] = { 200, 201, 202 };
  427. GPBUInt64Int32Dictionary *dict =
  428. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues
  429. forKeys:kKeys
  430. count:GPBARRAYSIZE(kValues)];
  431. XCTAssertNotNil(dict);
  432. XCTAssertEqual(dict.count, 3U);
  433. int32_t value;
  434. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  435. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  436. XCTAssertEqual(value, 200);
  437. XCTAssertTrue([dict getInt32:NULL forKey:32ULL]);
  438. XCTAssertTrue([dict getInt32:&value forKey:32ULL]);
  439. XCTAssertEqual(value, 201);
  440. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  441. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  442. XCTAssertEqual(value, 202);
  443. XCTAssertFalse([dict getInt32:NULL forKey:34ULL]);
  444. __block NSUInteger idx = 0;
  445. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  446. int32_t *seenValues = malloc(3 * sizeof(int32_t));
  447. [dict enumerateKeysAndInt32sUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  448. XCTAssertLessThan(idx, 3U);
  449. seenKeys[idx] = aKey;
  450. seenValues[idx] = aValue;
  451. XCTAssertNotEqual(stop, NULL);
  452. ++idx;
  453. }];
  454. for (int i = 0; i < 3; ++i) {
  455. BOOL foundKey = NO;
  456. for (int j = 0; (j < 3) && !foundKey; ++j) {
  457. if (kKeys[i] == seenKeys[j]) {
  458. foundKey = YES;
  459. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  460. }
  461. }
  462. XCTAssertTrue(foundKey, @"i = %d", i);
  463. }
  464. free(seenKeys);
  465. free(seenValues);
  466. // Stopping the enumeration.
  467. idx = 0;
  468. [dict enumerateKeysAndInt32sUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  469. #pragma unused(aKey, aValue)
  470. if (idx == 1) *stop = YES;
  471. XCTAssertNotEqual(idx, 2U);
  472. ++idx;
  473. }];
  474. [dict release];
  475. }
  476. - (void)testEquality {
  477. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  478. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  479. const int32_t kValues1[] = { 200, 201, 202 };
  480. const int32_t kValues2[] = { 200, 203, 202 };
  481. const int32_t kValues3[] = { 200, 201, 202, 203 };
  482. GPBUInt64Int32Dictionary *dict1 =
  483. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues1
  484. forKeys:kKeys1
  485. count:GPBARRAYSIZE(kValues1)];
  486. XCTAssertNotNil(dict1);
  487. GPBUInt64Int32Dictionary *dict1prime =
  488. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues1
  489. forKeys:kKeys1
  490. count:GPBARRAYSIZE(kValues1)];
  491. XCTAssertNotNil(dict1prime);
  492. GPBUInt64Int32Dictionary *dict2 =
  493. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues2
  494. forKeys:kKeys1
  495. count:GPBARRAYSIZE(kValues2)];
  496. XCTAssertNotNil(dict2);
  497. GPBUInt64Int32Dictionary *dict3 =
  498. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues1
  499. forKeys:kKeys2
  500. count:GPBARRAYSIZE(kValues1)];
  501. XCTAssertNotNil(dict3);
  502. GPBUInt64Int32Dictionary *dict4 =
  503. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues3
  504. forKeys:kKeys1
  505. count:GPBARRAYSIZE(kValues3)];
  506. XCTAssertNotNil(dict4);
  507. // 1/1Prime should be different objects, but equal.
  508. XCTAssertNotEqual(dict1, dict1prime);
  509. XCTAssertEqualObjects(dict1, dict1prime);
  510. // Equal, so they must have same hash.
  511. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  512. // 2 is same keys, different values; not equal.
  513. XCTAssertNotEqualObjects(dict1, dict2);
  514. // 3 is different keys, same values; not equal.
  515. XCTAssertNotEqualObjects(dict1, dict3);
  516. // 4 extra pair; not equal
  517. XCTAssertNotEqualObjects(dict1, dict4);
  518. [dict1 release];
  519. [dict1prime release];
  520. [dict2 release];
  521. [dict3 release];
  522. [dict4 release];
  523. }
  524. - (void)testCopy {
  525. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  526. const int32_t kValues[] = { 200, 201, 202, 203 };
  527. GPBUInt64Int32Dictionary *dict =
  528. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues
  529. forKeys:kKeys
  530. count:GPBARRAYSIZE(kValues)];
  531. XCTAssertNotNil(dict);
  532. GPBUInt64Int32Dictionary *dict2 = [dict copy];
  533. XCTAssertNotNil(dict2);
  534. // Should be new object but equal.
  535. XCTAssertNotEqual(dict, dict2);
  536. XCTAssertEqualObjects(dict, dict2);
  537. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64Int32Dictionary class]]);
  538. [dict2 release];
  539. [dict release];
  540. }
  541. - (void)testDictionaryFromDictionary {
  542. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  543. const int32_t kValues[] = { 200, 201, 202, 203 };
  544. GPBUInt64Int32Dictionary *dict =
  545. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues
  546. forKeys:kKeys
  547. count:GPBARRAYSIZE(kValues)];
  548. XCTAssertNotNil(dict);
  549. GPBUInt64Int32Dictionary *dict2 =
  550. [[GPBUInt64Int32Dictionary alloc] initWithDictionary:dict];
  551. XCTAssertNotNil(dict2);
  552. // Should be new pointer, but equal objects.
  553. XCTAssertNotEqual(dict, dict2);
  554. XCTAssertEqualObjects(dict, dict2);
  555. [dict2 release];
  556. [dict release];
  557. }
  558. - (void)testAdds {
  559. GPBUInt64Int32Dictionary *dict = [[GPBUInt64Int32Dictionary alloc] init];
  560. XCTAssertNotNil(dict);
  561. XCTAssertEqual(dict.count, 0U);
  562. [dict setInt32:200 forKey:31ULL];
  563. XCTAssertEqual(dict.count, 1U);
  564. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  565. const int32_t kValues[] = { 201, 202, 203 };
  566. GPBUInt64Int32Dictionary *dict2 =
  567. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues
  568. forKeys:kKeys
  569. count:GPBARRAYSIZE(kValues)];
  570. XCTAssertNotNil(dict2);
  571. [dict addEntriesFromDictionary:dict2];
  572. XCTAssertEqual(dict.count, 4U);
  573. int32_t value;
  574. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  575. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  576. XCTAssertEqual(value, 200);
  577. XCTAssertTrue([dict getInt32:NULL forKey:32ULL]);
  578. XCTAssertTrue([dict getInt32:&value forKey:32ULL]);
  579. XCTAssertEqual(value, 201);
  580. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  581. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  582. XCTAssertEqual(value, 202);
  583. XCTAssertTrue([dict getInt32:NULL forKey:34ULL]);
  584. XCTAssertTrue([dict getInt32:&value forKey:34ULL]);
  585. XCTAssertEqual(value, 203);
  586. [dict2 release];
  587. [dict release];
  588. }
  589. - (void)testRemove {
  590. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  591. const int32_t kValues[] = { 200, 201, 202, 203 };
  592. GPBUInt64Int32Dictionary *dict =
  593. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues
  594. forKeys:kKeys
  595. count:GPBARRAYSIZE(kValues)];
  596. XCTAssertNotNil(dict);
  597. XCTAssertEqual(dict.count, 4U);
  598. [dict removeInt32ForKey:32ULL];
  599. XCTAssertEqual(dict.count, 3U);
  600. int32_t value;
  601. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  602. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  603. XCTAssertEqual(value, 200);
  604. XCTAssertFalse([dict getInt32:NULL forKey:32ULL]);
  605. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  606. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  607. XCTAssertEqual(value, 202);
  608. XCTAssertTrue([dict getInt32:NULL forKey:34ULL]);
  609. XCTAssertTrue([dict getInt32:&value forKey:34ULL]);
  610. XCTAssertEqual(value, 203);
  611. // Remove again does nothing.
  612. [dict removeInt32ForKey:32ULL];
  613. XCTAssertEqual(dict.count, 3U);
  614. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  615. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  616. XCTAssertEqual(value, 200);
  617. XCTAssertFalse([dict getInt32:NULL forKey:32ULL]);
  618. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  619. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  620. XCTAssertEqual(value, 202);
  621. XCTAssertTrue([dict getInt32:NULL forKey:34ULL]);
  622. XCTAssertTrue([dict getInt32:&value forKey:34ULL]);
  623. XCTAssertEqual(value, 203);
  624. [dict removeInt32ForKey:34ULL];
  625. XCTAssertEqual(dict.count, 2U);
  626. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  627. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  628. XCTAssertEqual(value, 200);
  629. XCTAssertFalse([dict getInt32:NULL forKey:32ULL]);
  630. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  631. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  632. XCTAssertEqual(value, 202);
  633. XCTAssertFalse([dict getInt32:NULL forKey:34ULL]);
  634. [dict removeAll];
  635. XCTAssertEqual(dict.count, 0U);
  636. XCTAssertFalse([dict getInt32:NULL forKey:31ULL]);
  637. XCTAssertFalse([dict getInt32:NULL forKey:32ULL]);
  638. XCTAssertFalse([dict getInt32:NULL forKey:33ULL]);
  639. XCTAssertFalse([dict getInt32:NULL forKey:34ULL]);
  640. [dict release];
  641. }
  642. - (void)testInplaceMutation {
  643. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  644. const int32_t kValues[] = { 200, 201, 202, 203 };
  645. GPBUInt64Int32Dictionary *dict =
  646. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues
  647. forKeys:kKeys
  648. count:GPBARRAYSIZE(kValues)];
  649. XCTAssertNotNil(dict);
  650. XCTAssertEqual(dict.count, 4U);
  651. int32_t value;
  652. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  653. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  654. XCTAssertEqual(value, 200);
  655. XCTAssertTrue([dict getInt32:NULL forKey:32ULL]);
  656. XCTAssertTrue([dict getInt32:&value forKey:32ULL]);
  657. XCTAssertEqual(value, 201);
  658. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  659. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  660. XCTAssertEqual(value, 202);
  661. XCTAssertTrue([dict getInt32:NULL forKey:34ULL]);
  662. XCTAssertTrue([dict getInt32:&value forKey:34ULL]);
  663. XCTAssertEqual(value, 203);
  664. [dict setInt32:203 forKey:31ULL];
  665. XCTAssertEqual(dict.count, 4U);
  666. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  667. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  668. XCTAssertEqual(value, 203);
  669. XCTAssertTrue([dict getInt32:NULL forKey:32ULL]);
  670. XCTAssertTrue([dict getInt32:&value forKey:32ULL]);
  671. XCTAssertEqual(value, 201);
  672. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  673. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  674. XCTAssertEqual(value, 202);
  675. XCTAssertTrue([dict getInt32:NULL forKey:34ULL]);
  676. XCTAssertTrue([dict getInt32:&value forKey:34ULL]);
  677. XCTAssertEqual(value, 203);
  678. [dict setInt32:201 forKey:34ULL];
  679. XCTAssertEqual(dict.count, 4U);
  680. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  681. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  682. XCTAssertEqual(value, 203);
  683. XCTAssertTrue([dict getInt32:NULL forKey:32ULL]);
  684. XCTAssertTrue([dict getInt32:&value forKey:32ULL]);
  685. XCTAssertEqual(value, 201);
  686. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  687. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  688. XCTAssertEqual(value, 202);
  689. XCTAssertTrue([dict getInt32:NULL forKey:34ULL]);
  690. XCTAssertTrue([dict getInt32:&value forKey:34ULL]);
  691. XCTAssertEqual(value, 201);
  692. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  693. const int32_t kValues2[] = { 202, 200 };
  694. GPBUInt64Int32Dictionary *dict2 =
  695. [[GPBUInt64Int32Dictionary alloc] initWithInt32s:kValues2
  696. forKeys:kKeys2
  697. count:GPBARRAYSIZE(kValues2)];
  698. XCTAssertNotNil(dict2);
  699. [dict addEntriesFromDictionary:dict2];
  700. XCTAssertEqual(dict.count, 4U);
  701. XCTAssertTrue([dict getInt32:NULL forKey:31ULL]);
  702. XCTAssertTrue([dict getInt32:&value forKey:31ULL]);
  703. XCTAssertEqual(value, 203);
  704. XCTAssertTrue([dict getInt32:NULL forKey:32ULL]);
  705. XCTAssertTrue([dict getInt32:&value forKey:32ULL]);
  706. XCTAssertEqual(value, 202);
  707. XCTAssertTrue([dict getInt32:NULL forKey:33ULL]);
  708. XCTAssertTrue([dict getInt32:&value forKey:33ULL]);
  709. XCTAssertEqual(value, 200);
  710. XCTAssertTrue([dict getInt32:NULL forKey:34ULL]);
  711. XCTAssertTrue([dict getInt32:&value forKey:34ULL]);
  712. XCTAssertEqual(value, 201);
  713. [dict2 release];
  714. [dict release];
  715. }
  716. @end
  717. #pragma mark - UInt64 -> UInt64
  718. @interface GPBUInt64UInt64DictionaryTests : XCTestCase
  719. @end
  720. @implementation GPBUInt64UInt64DictionaryTests
  721. - (void)testEmpty {
  722. GPBUInt64UInt64Dictionary *dict = [[GPBUInt64UInt64Dictionary alloc] init];
  723. XCTAssertNotNil(dict);
  724. XCTAssertEqual(dict.count, 0U);
  725. XCTAssertFalse([dict getUInt64:NULL forKey:31ULL]);
  726. [dict enumerateKeysAndUInt64sUsingBlock:^(uint64_t aKey, uint64_t aValue, BOOL *stop) {
  727. #pragma unused(aKey, aValue, stop)
  728. XCTFail(@"Shouldn't get here!");
  729. }];
  730. [dict release];
  731. }
  732. - (void)testOne {
  733. GPBUInt64UInt64Dictionary *dict = [[GPBUInt64UInt64Dictionary alloc] init];
  734. [dict setUInt64:300U forKey:31ULL];
  735. XCTAssertNotNil(dict);
  736. XCTAssertEqual(dict.count, 1U);
  737. uint64_t value;
  738. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  739. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  740. XCTAssertEqual(value, 300U);
  741. XCTAssertFalse([dict getUInt64:NULL forKey:32ULL]);
  742. [dict enumerateKeysAndUInt64sUsingBlock:^(uint64_t aKey, uint64_t aValue, BOOL *stop) {
  743. XCTAssertEqual(aKey, 31ULL);
  744. XCTAssertEqual(aValue, 300U);
  745. XCTAssertNotEqual(stop, NULL);
  746. }];
  747. [dict release];
  748. }
  749. - (void)testBasics {
  750. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  751. const uint64_t kValues[] = { 300U, 301U, 302U };
  752. GPBUInt64UInt64Dictionary *dict =
  753. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues
  754. forKeys:kKeys
  755. count:GPBARRAYSIZE(kValues)];
  756. XCTAssertNotNil(dict);
  757. XCTAssertEqual(dict.count, 3U);
  758. uint64_t value;
  759. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  760. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  761. XCTAssertEqual(value, 300U);
  762. XCTAssertTrue([dict getUInt64:NULL forKey:32ULL]);
  763. XCTAssertTrue([dict getUInt64:&value forKey:32ULL]);
  764. XCTAssertEqual(value, 301U);
  765. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  766. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  767. XCTAssertEqual(value, 302U);
  768. XCTAssertFalse([dict getUInt64:NULL forKey:34ULL]);
  769. __block NSUInteger idx = 0;
  770. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  771. uint64_t *seenValues = malloc(3 * sizeof(uint64_t));
  772. [dict enumerateKeysAndUInt64sUsingBlock:^(uint64_t aKey, uint64_t aValue, BOOL *stop) {
  773. XCTAssertLessThan(idx, 3U);
  774. seenKeys[idx] = aKey;
  775. seenValues[idx] = aValue;
  776. XCTAssertNotEqual(stop, NULL);
  777. ++idx;
  778. }];
  779. for (int i = 0; i < 3; ++i) {
  780. BOOL foundKey = NO;
  781. for (int j = 0; (j < 3) && !foundKey; ++j) {
  782. if (kKeys[i] == seenKeys[j]) {
  783. foundKey = YES;
  784. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  785. }
  786. }
  787. XCTAssertTrue(foundKey, @"i = %d", i);
  788. }
  789. free(seenKeys);
  790. free(seenValues);
  791. // Stopping the enumeration.
  792. idx = 0;
  793. [dict enumerateKeysAndUInt64sUsingBlock:^(uint64_t aKey, uint64_t aValue, BOOL *stop) {
  794. #pragma unused(aKey, aValue)
  795. if (idx == 1) *stop = YES;
  796. XCTAssertNotEqual(idx, 2U);
  797. ++idx;
  798. }];
  799. [dict release];
  800. }
  801. - (void)testEquality {
  802. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  803. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  804. const uint64_t kValues1[] = { 300U, 301U, 302U };
  805. const uint64_t kValues2[] = { 300U, 303U, 302U };
  806. const uint64_t kValues3[] = { 300U, 301U, 302U, 303U };
  807. GPBUInt64UInt64Dictionary *dict1 =
  808. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues1
  809. forKeys:kKeys1
  810. count:GPBARRAYSIZE(kValues1)];
  811. XCTAssertNotNil(dict1);
  812. GPBUInt64UInt64Dictionary *dict1prime =
  813. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues1
  814. forKeys:kKeys1
  815. count:GPBARRAYSIZE(kValues1)];
  816. XCTAssertNotNil(dict1prime);
  817. GPBUInt64UInt64Dictionary *dict2 =
  818. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues2
  819. forKeys:kKeys1
  820. count:GPBARRAYSIZE(kValues2)];
  821. XCTAssertNotNil(dict2);
  822. GPBUInt64UInt64Dictionary *dict3 =
  823. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues1
  824. forKeys:kKeys2
  825. count:GPBARRAYSIZE(kValues1)];
  826. XCTAssertNotNil(dict3);
  827. GPBUInt64UInt64Dictionary *dict4 =
  828. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues3
  829. forKeys:kKeys1
  830. count:GPBARRAYSIZE(kValues3)];
  831. XCTAssertNotNil(dict4);
  832. // 1/1Prime should be different objects, but equal.
  833. XCTAssertNotEqual(dict1, dict1prime);
  834. XCTAssertEqualObjects(dict1, dict1prime);
  835. // Equal, so they must have same hash.
  836. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  837. // 2 is same keys, different values; not equal.
  838. XCTAssertNotEqualObjects(dict1, dict2);
  839. // 3 is different keys, same values; not equal.
  840. XCTAssertNotEqualObjects(dict1, dict3);
  841. // 4 extra pair; not equal
  842. XCTAssertNotEqualObjects(dict1, dict4);
  843. [dict1 release];
  844. [dict1prime release];
  845. [dict2 release];
  846. [dict3 release];
  847. [dict4 release];
  848. }
  849. - (void)testCopy {
  850. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  851. const uint64_t kValues[] = { 300U, 301U, 302U, 303U };
  852. GPBUInt64UInt64Dictionary *dict =
  853. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues
  854. forKeys:kKeys
  855. count:GPBARRAYSIZE(kValues)];
  856. XCTAssertNotNil(dict);
  857. GPBUInt64UInt64Dictionary *dict2 = [dict copy];
  858. XCTAssertNotNil(dict2);
  859. // Should be new object but equal.
  860. XCTAssertNotEqual(dict, dict2);
  861. XCTAssertEqualObjects(dict, dict2);
  862. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64UInt64Dictionary class]]);
  863. [dict2 release];
  864. [dict release];
  865. }
  866. - (void)testDictionaryFromDictionary {
  867. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  868. const uint64_t kValues[] = { 300U, 301U, 302U, 303U };
  869. GPBUInt64UInt64Dictionary *dict =
  870. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues
  871. forKeys:kKeys
  872. count:GPBARRAYSIZE(kValues)];
  873. XCTAssertNotNil(dict);
  874. GPBUInt64UInt64Dictionary *dict2 =
  875. [[GPBUInt64UInt64Dictionary alloc] initWithDictionary:dict];
  876. XCTAssertNotNil(dict2);
  877. // Should be new pointer, but equal objects.
  878. XCTAssertNotEqual(dict, dict2);
  879. XCTAssertEqualObjects(dict, dict2);
  880. [dict2 release];
  881. [dict release];
  882. }
  883. - (void)testAdds {
  884. GPBUInt64UInt64Dictionary *dict = [[GPBUInt64UInt64Dictionary alloc] init];
  885. XCTAssertNotNil(dict);
  886. XCTAssertEqual(dict.count, 0U);
  887. [dict setUInt64:300U forKey:31ULL];
  888. XCTAssertEqual(dict.count, 1U);
  889. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  890. const uint64_t kValues[] = { 301U, 302U, 303U };
  891. GPBUInt64UInt64Dictionary *dict2 =
  892. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues
  893. forKeys:kKeys
  894. count:GPBARRAYSIZE(kValues)];
  895. XCTAssertNotNil(dict2);
  896. [dict addEntriesFromDictionary:dict2];
  897. XCTAssertEqual(dict.count, 4U);
  898. uint64_t value;
  899. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  900. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  901. XCTAssertEqual(value, 300U);
  902. XCTAssertTrue([dict getUInt64:NULL forKey:32ULL]);
  903. XCTAssertTrue([dict getUInt64:&value forKey:32ULL]);
  904. XCTAssertEqual(value, 301U);
  905. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  906. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  907. XCTAssertEqual(value, 302U);
  908. XCTAssertTrue([dict getUInt64:NULL forKey:34ULL]);
  909. XCTAssertTrue([dict getUInt64:&value forKey:34ULL]);
  910. XCTAssertEqual(value, 303U);
  911. [dict2 release];
  912. [dict release];
  913. }
  914. - (void)testRemove {
  915. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  916. const uint64_t kValues[] = { 300U, 301U, 302U, 303U };
  917. GPBUInt64UInt64Dictionary *dict =
  918. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues
  919. forKeys:kKeys
  920. count:GPBARRAYSIZE(kValues)];
  921. XCTAssertNotNil(dict);
  922. XCTAssertEqual(dict.count, 4U);
  923. [dict removeUInt64ForKey:32ULL];
  924. XCTAssertEqual(dict.count, 3U);
  925. uint64_t value;
  926. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  927. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  928. XCTAssertEqual(value, 300U);
  929. XCTAssertFalse([dict getUInt64:NULL forKey:32ULL]);
  930. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  931. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  932. XCTAssertEqual(value, 302U);
  933. XCTAssertTrue([dict getUInt64:NULL forKey:34ULL]);
  934. XCTAssertTrue([dict getUInt64:&value forKey:34ULL]);
  935. XCTAssertEqual(value, 303U);
  936. // Remove again does nothing.
  937. [dict removeUInt64ForKey:32ULL];
  938. XCTAssertEqual(dict.count, 3U);
  939. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  940. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  941. XCTAssertEqual(value, 300U);
  942. XCTAssertFalse([dict getUInt64:NULL forKey:32ULL]);
  943. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  944. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  945. XCTAssertEqual(value, 302U);
  946. XCTAssertTrue([dict getUInt64:NULL forKey:34ULL]);
  947. XCTAssertTrue([dict getUInt64:&value forKey:34ULL]);
  948. XCTAssertEqual(value, 303U);
  949. [dict removeUInt64ForKey:34ULL];
  950. XCTAssertEqual(dict.count, 2U);
  951. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  952. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  953. XCTAssertEqual(value, 300U);
  954. XCTAssertFalse([dict getUInt64:NULL forKey:32ULL]);
  955. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  956. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  957. XCTAssertEqual(value, 302U);
  958. XCTAssertFalse([dict getUInt64:NULL forKey:34ULL]);
  959. [dict removeAll];
  960. XCTAssertEqual(dict.count, 0U);
  961. XCTAssertFalse([dict getUInt64:NULL forKey:31ULL]);
  962. XCTAssertFalse([dict getUInt64:NULL forKey:32ULL]);
  963. XCTAssertFalse([dict getUInt64:NULL forKey:33ULL]);
  964. XCTAssertFalse([dict getUInt64:NULL forKey:34ULL]);
  965. [dict release];
  966. }
  967. - (void)testInplaceMutation {
  968. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  969. const uint64_t kValues[] = { 300U, 301U, 302U, 303U };
  970. GPBUInt64UInt64Dictionary *dict =
  971. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues
  972. forKeys:kKeys
  973. count:GPBARRAYSIZE(kValues)];
  974. XCTAssertNotNil(dict);
  975. XCTAssertEqual(dict.count, 4U);
  976. uint64_t value;
  977. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  978. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  979. XCTAssertEqual(value, 300U);
  980. XCTAssertTrue([dict getUInt64:NULL forKey:32ULL]);
  981. XCTAssertTrue([dict getUInt64:&value forKey:32ULL]);
  982. XCTAssertEqual(value, 301U);
  983. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  984. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  985. XCTAssertEqual(value, 302U);
  986. XCTAssertTrue([dict getUInt64:NULL forKey:34ULL]);
  987. XCTAssertTrue([dict getUInt64:&value forKey:34ULL]);
  988. XCTAssertEqual(value, 303U);
  989. [dict setUInt64:303U forKey:31ULL];
  990. XCTAssertEqual(dict.count, 4U);
  991. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  992. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  993. XCTAssertEqual(value, 303U);
  994. XCTAssertTrue([dict getUInt64:NULL forKey:32ULL]);
  995. XCTAssertTrue([dict getUInt64:&value forKey:32ULL]);
  996. XCTAssertEqual(value, 301U);
  997. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  998. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  999. XCTAssertEqual(value, 302U);
  1000. XCTAssertTrue([dict getUInt64:NULL forKey:34ULL]);
  1001. XCTAssertTrue([dict getUInt64:&value forKey:34ULL]);
  1002. XCTAssertEqual(value, 303U);
  1003. [dict setUInt64:301U forKey:34ULL];
  1004. XCTAssertEqual(dict.count, 4U);
  1005. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  1006. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  1007. XCTAssertEqual(value, 303U);
  1008. XCTAssertTrue([dict getUInt64:NULL forKey:32ULL]);
  1009. XCTAssertTrue([dict getUInt64:&value forKey:32ULL]);
  1010. XCTAssertEqual(value, 301U);
  1011. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  1012. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  1013. XCTAssertEqual(value, 302U);
  1014. XCTAssertTrue([dict getUInt64:NULL forKey:34ULL]);
  1015. XCTAssertTrue([dict getUInt64:&value forKey:34ULL]);
  1016. XCTAssertEqual(value, 301U);
  1017. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  1018. const uint64_t kValues2[] = { 302U, 300U };
  1019. GPBUInt64UInt64Dictionary *dict2 =
  1020. [[GPBUInt64UInt64Dictionary alloc] initWithUInt64s:kValues2
  1021. forKeys:kKeys2
  1022. count:GPBARRAYSIZE(kValues2)];
  1023. XCTAssertNotNil(dict2);
  1024. [dict addEntriesFromDictionary:dict2];
  1025. XCTAssertEqual(dict.count, 4U);
  1026. XCTAssertTrue([dict getUInt64:NULL forKey:31ULL]);
  1027. XCTAssertTrue([dict getUInt64:&value forKey:31ULL]);
  1028. XCTAssertEqual(value, 303U);
  1029. XCTAssertTrue([dict getUInt64:NULL forKey:32ULL]);
  1030. XCTAssertTrue([dict getUInt64:&value forKey:32ULL]);
  1031. XCTAssertEqual(value, 302U);
  1032. XCTAssertTrue([dict getUInt64:NULL forKey:33ULL]);
  1033. XCTAssertTrue([dict getUInt64:&value forKey:33ULL]);
  1034. XCTAssertEqual(value, 300U);
  1035. XCTAssertTrue([dict getUInt64:NULL forKey:34ULL]);
  1036. XCTAssertTrue([dict getUInt64:&value forKey:34ULL]);
  1037. XCTAssertEqual(value, 301U);
  1038. [dict2 release];
  1039. [dict release];
  1040. }
  1041. @end
  1042. #pragma mark - UInt64 -> Int64
  1043. @interface GPBUInt64Int64DictionaryTests : XCTestCase
  1044. @end
  1045. @implementation GPBUInt64Int64DictionaryTests
  1046. - (void)testEmpty {
  1047. GPBUInt64Int64Dictionary *dict = [[GPBUInt64Int64Dictionary alloc] init];
  1048. XCTAssertNotNil(dict);
  1049. XCTAssertEqual(dict.count, 0U);
  1050. XCTAssertFalse([dict getInt64:NULL forKey:31ULL]);
  1051. [dict enumerateKeysAndInt64sUsingBlock:^(uint64_t aKey, int64_t aValue, BOOL *stop) {
  1052. #pragma unused(aKey, aValue, stop)
  1053. XCTFail(@"Shouldn't get here!");
  1054. }];
  1055. [dict release];
  1056. }
  1057. - (void)testOne {
  1058. GPBUInt64Int64Dictionary *dict = [[GPBUInt64Int64Dictionary alloc] init];
  1059. [dict setInt64:400 forKey:31ULL];
  1060. XCTAssertNotNil(dict);
  1061. XCTAssertEqual(dict.count, 1U);
  1062. int64_t value;
  1063. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1064. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1065. XCTAssertEqual(value, 400);
  1066. XCTAssertFalse([dict getInt64:NULL forKey:32ULL]);
  1067. [dict enumerateKeysAndInt64sUsingBlock:^(uint64_t aKey, int64_t aValue, BOOL *stop) {
  1068. XCTAssertEqual(aKey, 31ULL);
  1069. XCTAssertEqual(aValue, 400);
  1070. XCTAssertNotEqual(stop, NULL);
  1071. }];
  1072. [dict release];
  1073. }
  1074. - (void)testBasics {
  1075. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  1076. const int64_t kValues[] = { 400, 401, 402 };
  1077. GPBUInt64Int64Dictionary *dict =
  1078. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues
  1079. forKeys:kKeys
  1080. count:GPBARRAYSIZE(kValues)];
  1081. XCTAssertNotNil(dict);
  1082. XCTAssertEqual(dict.count, 3U);
  1083. int64_t value;
  1084. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1085. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1086. XCTAssertEqual(value, 400);
  1087. XCTAssertTrue([dict getInt64:NULL forKey:32ULL]);
  1088. XCTAssertTrue([dict getInt64:&value forKey:32ULL]);
  1089. XCTAssertEqual(value, 401);
  1090. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1091. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1092. XCTAssertEqual(value, 402);
  1093. XCTAssertFalse([dict getInt64:NULL forKey:34ULL]);
  1094. __block NSUInteger idx = 0;
  1095. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  1096. int64_t *seenValues = malloc(3 * sizeof(int64_t));
  1097. [dict enumerateKeysAndInt64sUsingBlock:^(uint64_t aKey, int64_t aValue, BOOL *stop) {
  1098. XCTAssertLessThan(idx, 3U);
  1099. seenKeys[idx] = aKey;
  1100. seenValues[idx] = aValue;
  1101. XCTAssertNotEqual(stop, NULL);
  1102. ++idx;
  1103. }];
  1104. for (int i = 0; i < 3; ++i) {
  1105. BOOL foundKey = NO;
  1106. for (int j = 0; (j < 3) && !foundKey; ++j) {
  1107. if (kKeys[i] == seenKeys[j]) {
  1108. foundKey = YES;
  1109. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  1110. }
  1111. }
  1112. XCTAssertTrue(foundKey, @"i = %d", i);
  1113. }
  1114. free(seenKeys);
  1115. free(seenValues);
  1116. // Stopping the enumeration.
  1117. idx = 0;
  1118. [dict enumerateKeysAndInt64sUsingBlock:^(uint64_t aKey, int64_t aValue, BOOL *stop) {
  1119. #pragma unused(aKey, aValue)
  1120. if (idx == 1) *stop = YES;
  1121. XCTAssertNotEqual(idx, 2U);
  1122. ++idx;
  1123. }];
  1124. [dict release];
  1125. }
  1126. - (void)testEquality {
  1127. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1128. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  1129. const int64_t kValues1[] = { 400, 401, 402 };
  1130. const int64_t kValues2[] = { 400, 403, 402 };
  1131. const int64_t kValues3[] = { 400, 401, 402, 403 };
  1132. GPBUInt64Int64Dictionary *dict1 =
  1133. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues1
  1134. forKeys:kKeys1
  1135. count:GPBARRAYSIZE(kValues1)];
  1136. XCTAssertNotNil(dict1);
  1137. GPBUInt64Int64Dictionary *dict1prime =
  1138. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues1
  1139. forKeys:kKeys1
  1140. count:GPBARRAYSIZE(kValues1)];
  1141. XCTAssertNotNil(dict1prime);
  1142. GPBUInt64Int64Dictionary *dict2 =
  1143. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues2
  1144. forKeys:kKeys1
  1145. count:GPBARRAYSIZE(kValues2)];
  1146. XCTAssertNotNil(dict2);
  1147. GPBUInt64Int64Dictionary *dict3 =
  1148. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues1
  1149. forKeys:kKeys2
  1150. count:GPBARRAYSIZE(kValues1)];
  1151. XCTAssertNotNil(dict3);
  1152. GPBUInt64Int64Dictionary *dict4 =
  1153. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues3
  1154. forKeys:kKeys1
  1155. count:GPBARRAYSIZE(kValues3)];
  1156. XCTAssertNotNil(dict4);
  1157. // 1/1Prime should be different objects, but equal.
  1158. XCTAssertNotEqual(dict1, dict1prime);
  1159. XCTAssertEqualObjects(dict1, dict1prime);
  1160. // Equal, so they must have same hash.
  1161. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  1162. // 2 is same keys, different values; not equal.
  1163. XCTAssertNotEqualObjects(dict1, dict2);
  1164. // 3 is different keys, same values; not equal.
  1165. XCTAssertNotEqualObjects(dict1, dict3);
  1166. // 4 extra pair; not equal
  1167. XCTAssertNotEqualObjects(dict1, dict4);
  1168. [dict1 release];
  1169. [dict1prime release];
  1170. [dict2 release];
  1171. [dict3 release];
  1172. [dict4 release];
  1173. }
  1174. - (void)testCopy {
  1175. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1176. const int64_t kValues[] = { 400, 401, 402, 403 };
  1177. GPBUInt64Int64Dictionary *dict =
  1178. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues
  1179. forKeys:kKeys
  1180. count:GPBARRAYSIZE(kValues)];
  1181. XCTAssertNotNil(dict);
  1182. GPBUInt64Int64Dictionary *dict2 = [dict copy];
  1183. XCTAssertNotNil(dict2);
  1184. // Should be new object but equal.
  1185. XCTAssertNotEqual(dict, dict2);
  1186. XCTAssertEqualObjects(dict, dict2);
  1187. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64Int64Dictionary class]]);
  1188. [dict2 release];
  1189. [dict release];
  1190. }
  1191. - (void)testDictionaryFromDictionary {
  1192. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1193. const int64_t kValues[] = { 400, 401, 402, 403 };
  1194. GPBUInt64Int64Dictionary *dict =
  1195. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues
  1196. forKeys:kKeys
  1197. count:GPBARRAYSIZE(kValues)];
  1198. XCTAssertNotNil(dict);
  1199. GPBUInt64Int64Dictionary *dict2 =
  1200. [[GPBUInt64Int64Dictionary alloc] initWithDictionary:dict];
  1201. XCTAssertNotNil(dict2);
  1202. // Should be new pointer, but equal objects.
  1203. XCTAssertNotEqual(dict, dict2);
  1204. XCTAssertEqualObjects(dict, dict2);
  1205. [dict2 release];
  1206. [dict release];
  1207. }
  1208. - (void)testAdds {
  1209. GPBUInt64Int64Dictionary *dict = [[GPBUInt64Int64Dictionary alloc] init];
  1210. XCTAssertNotNil(dict);
  1211. XCTAssertEqual(dict.count, 0U);
  1212. [dict setInt64:400 forKey:31ULL];
  1213. XCTAssertEqual(dict.count, 1U);
  1214. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  1215. const int64_t kValues[] = { 401, 402, 403 };
  1216. GPBUInt64Int64Dictionary *dict2 =
  1217. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues
  1218. forKeys:kKeys
  1219. count:GPBARRAYSIZE(kValues)];
  1220. XCTAssertNotNil(dict2);
  1221. [dict addEntriesFromDictionary:dict2];
  1222. XCTAssertEqual(dict.count, 4U);
  1223. int64_t value;
  1224. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1225. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1226. XCTAssertEqual(value, 400);
  1227. XCTAssertTrue([dict getInt64:NULL forKey:32ULL]);
  1228. XCTAssertTrue([dict getInt64:&value forKey:32ULL]);
  1229. XCTAssertEqual(value, 401);
  1230. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1231. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1232. XCTAssertEqual(value, 402);
  1233. XCTAssertTrue([dict getInt64:NULL forKey:34ULL]);
  1234. XCTAssertTrue([dict getInt64:&value forKey:34ULL]);
  1235. XCTAssertEqual(value, 403);
  1236. [dict2 release];
  1237. [dict release];
  1238. }
  1239. - (void)testRemove {
  1240. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1241. const int64_t kValues[] = { 400, 401, 402, 403 };
  1242. GPBUInt64Int64Dictionary *dict =
  1243. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues
  1244. forKeys:kKeys
  1245. count:GPBARRAYSIZE(kValues)];
  1246. XCTAssertNotNil(dict);
  1247. XCTAssertEqual(dict.count, 4U);
  1248. [dict removeInt64ForKey:32ULL];
  1249. XCTAssertEqual(dict.count, 3U);
  1250. int64_t value;
  1251. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1252. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1253. XCTAssertEqual(value, 400);
  1254. XCTAssertFalse([dict getInt64:NULL forKey:32ULL]);
  1255. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1256. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1257. XCTAssertEqual(value, 402);
  1258. XCTAssertTrue([dict getInt64:NULL forKey:34ULL]);
  1259. XCTAssertTrue([dict getInt64:&value forKey:34ULL]);
  1260. XCTAssertEqual(value, 403);
  1261. // Remove again does nothing.
  1262. [dict removeInt64ForKey:32ULL];
  1263. XCTAssertEqual(dict.count, 3U);
  1264. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1265. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1266. XCTAssertEqual(value, 400);
  1267. XCTAssertFalse([dict getInt64:NULL forKey:32ULL]);
  1268. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1269. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1270. XCTAssertEqual(value, 402);
  1271. XCTAssertTrue([dict getInt64:NULL forKey:34ULL]);
  1272. XCTAssertTrue([dict getInt64:&value forKey:34ULL]);
  1273. XCTAssertEqual(value, 403);
  1274. [dict removeInt64ForKey:34ULL];
  1275. XCTAssertEqual(dict.count, 2U);
  1276. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1277. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1278. XCTAssertEqual(value, 400);
  1279. XCTAssertFalse([dict getInt64:NULL forKey:32ULL]);
  1280. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1281. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1282. XCTAssertEqual(value, 402);
  1283. XCTAssertFalse([dict getInt64:NULL forKey:34ULL]);
  1284. [dict removeAll];
  1285. XCTAssertEqual(dict.count, 0U);
  1286. XCTAssertFalse([dict getInt64:NULL forKey:31ULL]);
  1287. XCTAssertFalse([dict getInt64:NULL forKey:32ULL]);
  1288. XCTAssertFalse([dict getInt64:NULL forKey:33ULL]);
  1289. XCTAssertFalse([dict getInt64:NULL forKey:34ULL]);
  1290. [dict release];
  1291. }
  1292. - (void)testInplaceMutation {
  1293. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1294. const int64_t kValues[] = { 400, 401, 402, 403 };
  1295. GPBUInt64Int64Dictionary *dict =
  1296. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues
  1297. forKeys:kKeys
  1298. count:GPBARRAYSIZE(kValues)];
  1299. XCTAssertNotNil(dict);
  1300. XCTAssertEqual(dict.count, 4U);
  1301. int64_t value;
  1302. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1303. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1304. XCTAssertEqual(value, 400);
  1305. XCTAssertTrue([dict getInt64:NULL forKey:32ULL]);
  1306. XCTAssertTrue([dict getInt64:&value forKey:32ULL]);
  1307. XCTAssertEqual(value, 401);
  1308. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1309. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1310. XCTAssertEqual(value, 402);
  1311. XCTAssertTrue([dict getInt64:NULL forKey:34ULL]);
  1312. XCTAssertTrue([dict getInt64:&value forKey:34ULL]);
  1313. XCTAssertEqual(value, 403);
  1314. [dict setInt64:403 forKey:31ULL];
  1315. XCTAssertEqual(dict.count, 4U);
  1316. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1317. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1318. XCTAssertEqual(value, 403);
  1319. XCTAssertTrue([dict getInt64:NULL forKey:32ULL]);
  1320. XCTAssertTrue([dict getInt64:&value forKey:32ULL]);
  1321. XCTAssertEqual(value, 401);
  1322. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1323. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1324. XCTAssertEqual(value, 402);
  1325. XCTAssertTrue([dict getInt64:NULL forKey:34ULL]);
  1326. XCTAssertTrue([dict getInt64:&value forKey:34ULL]);
  1327. XCTAssertEqual(value, 403);
  1328. [dict setInt64:401 forKey:34ULL];
  1329. XCTAssertEqual(dict.count, 4U);
  1330. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1331. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1332. XCTAssertEqual(value, 403);
  1333. XCTAssertTrue([dict getInt64:NULL forKey:32ULL]);
  1334. XCTAssertTrue([dict getInt64:&value forKey:32ULL]);
  1335. XCTAssertEqual(value, 401);
  1336. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1337. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1338. XCTAssertEqual(value, 402);
  1339. XCTAssertTrue([dict getInt64:NULL forKey:34ULL]);
  1340. XCTAssertTrue([dict getInt64:&value forKey:34ULL]);
  1341. XCTAssertEqual(value, 401);
  1342. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  1343. const int64_t kValues2[] = { 402, 400 };
  1344. GPBUInt64Int64Dictionary *dict2 =
  1345. [[GPBUInt64Int64Dictionary alloc] initWithInt64s:kValues2
  1346. forKeys:kKeys2
  1347. count:GPBARRAYSIZE(kValues2)];
  1348. XCTAssertNotNil(dict2);
  1349. [dict addEntriesFromDictionary:dict2];
  1350. XCTAssertEqual(dict.count, 4U);
  1351. XCTAssertTrue([dict getInt64:NULL forKey:31ULL]);
  1352. XCTAssertTrue([dict getInt64:&value forKey:31ULL]);
  1353. XCTAssertEqual(value, 403);
  1354. XCTAssertTrue([dict getInt64:NULL forKey:32ULL]);
  1355. XCTAssertTrue([dict getInt64:&value forKey:32ULL]);
  1356. XCTAssertEqual(value, 402);
  1357. XCTAssertTrue([dict getInt64:NULL forKey:33ULL]);
  1358. XCTAssertTrue([dict getInt64:&value forKey:33ULL]);
  1359. XCTAssertEqual(value, 400);
  1360. XCTAssertTrue([dict getInt64:NULL forKey:34ULL]);
  1361. XCTAssertTrue([dict getInt64:&value forKey:34ULL]);
  1362. XCTAssertEqual(value, 401);
  1363. [dict2 release];
  1364. [dict release];
  1365. }
  1366. @end
  1367. #pragma mark - UInt64 -> Bool
  1368. @interface GPBUInt64BoolDictionaryTests : XCTestCase
  1369. @end
  1370. @implementation GPBUInt64BoolDictionaryTests
  1371. - (void)testEmpty {
  1372. GPBUInt64BoolDictionary *dict = [[GPBUInt64BoolDictionary alloc] init];
  1373. XCTAssertNotNil(dict);
  1374. XCTAssertEqual(dict.count, 0U);
  1375. XCTAssertFalse([dict getBool:NULL forKey:31ULL]);
  1376. [dict enumerateKeysAndBoolsUsingBlock:^(uint64_t aKey, BOOL aValue, BOOL *stop) {
  1377. #pragma unused(aKey, aValue, stop)
  1378. XCTFail(@"Shouldn't get here!");
  1379. }];
  1380. [dict release];
  1381. }
  1382. - (void)testOne {
  1383. GPBUInt64BoolDictionary *dict = [[GPBUInt64BoolDictionary alloc] init];
  1384. [dict setBool:YES forKey:31ULL];
  1385. XCTAssertNotNil(dict);
  1386. XCTAssertEqual(dict.count, 1U);
  1387. BOOL value;
  1388. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1389. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1390. XCTAssertEqual(value, YES);
  1391. XCTAssertFalse([dict getBool:NULL forKey:32ULL]);
  1392. [dict enumerateKeysAndBoolsUsingBlock:^(uint64_t aKey, BOOL aValue, BOOL *stop) {
  1393. XCTAssertEqual(aKey, 31ULL);
  1394. XCTAssertEqual(aValue, YES);
  1395. XCTAssertNotEqual(stop, NULL);
  1396. }];
  1397. [dict release];
  1398. }
  1399. - (void)testBasics {
  1400. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  1401. const BOOL kValues[] = { YES, YES, NO };
  1402. GPBUInt64BoolDictionary *dict =
  1403. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues
  1404. forKeys:kKeys
  1405. count:GPBARRAYSIZE(kValues)];
  1406. XCTAssertNotNil(dict);
  1407. XCTAssertEqual(dict.count, 3U);
  1408. BOOL value;
  1409. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1410. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1411. XCTAssertEqual(value, YES);
  1412. XCTAssertTrue([dict getBool:NULL forKey:32ULL]);
  1413. XCTAssertTrue([dict getBool:&value forKey:32ULL]);
  1414. XCTAssertEqual(value, YES);
  1415. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1416. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1417. XCTAssertEqual(value, NO);
  1418. XCTAssertFalse([dict getBool:NULL forKey:34ULL]);
  1419. __block NSUInteger idx = 0;
  1420. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  1421. BOOL *seenValues = malloc(3 * sizeof(BOOL));
  1422. [dict enumerateKeysAndBoolsUsingBlock:^(uint64_t aKey, BOOL aValue, BOOL *stop) {
  1423. XCTAssertLessThan(idx, 3U);
  1424. seenKeys[idx] = aKey;
  1425. seenValues[idx] = aValue;
  1426. XCTAssertNotEqual(stop, NULL);
  1427. ++idx;
  1428. }];
  1429. for (int i = 0; i < 3; ++i) {
  1430. BOOL foundKey = NO;
  1431. for (int j = 0; (j < 3) && !foundKey; ++j) {
  1432. if (kKeys[i] == seenKeys[j]) {
  1433. foundKey = YES;
  1434. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  1435. }
  1436. }
  1437. XCTAssertTrue(foundKey, @"i = %d", i);
  1438. }
  1439. free(seenKeys);
  1440. free(seenValues);
  1441. // Stopping the enumeration.
  1442. idx = 0;
  1443. [dict enumerateKeysAndBoolsUsingBlock:^(uint64_t aKey, BOOL aValue, BOOL *stop) {
  1444. #pragma unused(aKey, aValue)
  1445. if (idx == 1) *stop = YES;
  1446. XCTAssertNotEqual(idx, 2U);
  1447. ++idx;
  1448. }];
  1449. [dict release];
  1450. }
  1451. - (void)testEquality {
  1452. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1453. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  1454. const BOOL kValues1[] = { YES, YES, NO };
  1455. const BOOL kValues2[] = { YES, NO, NO };
  1456. const BOOL kValues3[] = { YES, YES, NO, NO };
  1457. GPBUInt64BoolDictionary *dict1 =
  1458. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues1
  1459. forKeys:kKeys1
  1460. count:GPBARRAYSIZE(kValues1)];
  1461. XCTAssertNotNil(dict1);
  1462. GPBUInt64BoolDictionary *dict1prime =
  1463. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues1
  1464. forKeys:kKeys1
  1465. count:GPBARRAYSIZE(kValues1)];
  1466. XCTAssertNotNil(dict1prime);
  1467. GPBUInt64BoolDictionary *dict2 =
  1468. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues2
  1469. forKeys:kKeys1
  1470. count:GPBARRAYSIZE(kValues2)];
  1471. XCTAssertNotNil(dict2);
  1472. GPBUInt64BoolDictionary *dict3 =
  1473. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues1
  1474. forKeys:kKeys2
  1475. count:GPBARRAYSIZE(kValues1)];
  1476. XCTAssertNotNil(dict3);
  1477. GPBUInt64BoolDictionary *dict4 =
  1478. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues3
  1479. forKeys:kKeys1
  1480. count:GPBARRAYSIZE(kValues3)];
  1481. XCTAssertNotNil(dict4);
  1482. // 1/1Prime should be different objects, but equal.
  1483. XCTAssertNotEqual(dict1, dict1prime);
  1484. XCTAssertEqualObjects(dict1, dict1prime);
  1485. // Equal, so they must have same hash.
  1486. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  1487. // 2 is same keys, different values; not equal.
  1488. XCTAssertNotEqualObjects(dict1, dict2);
  1489. // 3 is different keys, same values; not equal.
  1490. XCTAssertNotEqualObjects(dict1, dict3);
  1491. // 4 extra pair; not equal
  1492. XCTAssertNotEqualObjects(dict1, dict4);
  1493. [dict1 release];
  1494. [dict1prime release];
  1495. [dict2 release];
  1496. [dict3 release];
  1497. [dict4 release];
  1498. }
  1499. - (void)testCopy {
  1500. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1501. const BOOL kValues[] = { YES, YES, NO, NO };
  1502. GPBUInt64BoolDictionary *dict =
  1503. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues
  1504. forKeys:kKeys
  1505. count:GPBARRAYSIZE(kValues)];
  1506. XCTAssertNotNil(dict);
  1507. GPBUInt64BoolDictionary *dict2 = [dict copy];
  1508. XCTAssertNotNil(dict2);
  1509. // Should be new object but equal.
  1510. XCTAssertNotEqual(dict, dict2);
  1511. XCTAssertEqualObjects(dict, dict2);
  1512. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64BoolDictionary class]]);
  1513. [dict2 release];
  1514. [dict release];
  1515. }
  1516. - (void)testDictionaryFromDictionary {
  1517. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1518. const BOOL kValues[] = { YES, YES, NO, NO };
  1519. GPBUInt64BoolDictionary *dict =
  1520. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues
  1521. forKeys:kKeys
  1522. count:GPBARRAYSIZE(kValues)];
  1523. XCTAssertNotNil(dict);
  1524. GPBUInt64BoolDictionary *dict2 =
  1525. [[GPBUInt64BoolDictionary alloc] initWithDictionary:dict];
  1526. XCTAssertNotNil(dict2);
  1527. // Should be new pointer, but equal objects.
  1528. XCTAssertNotEqual(dict, dict2);
  1529. XCTAssertEqualObjects(dict, dict2);
  1530. [dict2 release];
  1531. [dict release];
  1532. }
  1533. - (void)testAdds {
  1534. GPBUInt64BoolDictionary *dict = [[GPBUInt64BoolDictionary alloc] init];
  1535. XCTAssertNotNil(dict);
  1536. XCTAssertEqual(dict.count, 0U);
  1537. [dict setBool:YES forKey:31ULL];
  1538. XCTAssertEqual(dict.count, 1U);
  1539. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  1540. const BOOL kValues[] = { YES, NO, NO };
  1541. GPBUInt64BoolDictionary *dict2 =
  1542. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues
  1543. forKeys:kKeys
  1544. count:GPBARRAYSIZE(kValues)];
  1545. XCTAssertNotNil(dict2);
  1546. [dict addEntriesFromDictionary:dict2];
  1547. XCTAssertEqual(dict.count, 4U);
  1548. BOOL value;
  1549. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1550. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1551. XCTAssertEqual(value, YES);
  1552. XCTAssertTrue([dict getBool:NULL forKey:32ULL]);
  1553. XCTAssertTrue([dict getBool:&value forKey:32ULL]);
  1554. XCTAssertEqual(value, YES);
  1555. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1556. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1557. XCTAssertEqual(value, NO);
  1558. XCTAssertTrue([dict getBool:NULL forKey:34ULL]);
  1559. XCTAssertTrue([dict getBool:&value forKey:34ULL]);
  1560. XCTAssertEqual(value, NO);
  1561. [dict2 release];
  1562. [dict release];
  1563. }
  1564. - (void)testRemove {
  1565. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1566. const BOOL kValues[] = { YES, YES, NO, NO };
  1567. GPBUInt64BoolDictionary *dict =
  1568. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues
  1569. forKeys:kKeys
  1570. count:GPBARRAYSIZE(kValues)];
  1571. XCTAssertNotNil(dict);
  1572. XCTAssertEqual(dict.count, 4U);
  1573. [dict removeBoolForKey:32ULL];
  1574. XCTAssertEqual(dict.count, 3U);
  1575. BOOL value;
  1576. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1577. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1578. XCTAssertEqual(value, YES);
  1579. XCTAssertFalse([dict getBool:NULL forKey:32ULL]);
  1580. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1581. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1582. XCTAssertEqual(value, NO);
  1583. XCTAssertTrue([dict getBool:NULL forKey:34ULL]);
  1584. XCTAssertTrue([dict getBool:&value forKey:34ULL]);
  1585. XCTAssertEqual(value, NO);
  1586. // Remove again does nothing.
  1587. [dict removeBoolForKey:32ULL];
  1588. XCTAssertEqual(dict.count, 3U);
  1589. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1590. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1591. XCTAssertEqual(value, YES);
  1592. XCTAssertFalse([dict getBool:NULL forKey:32ULL]);
  1593. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1594. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1595. XCTAssertEqual(value, NO);
  1596. XCTAssertTrue([dict getBool:NULL forKey:34ULL]);
  1597. XCTAssertTrue([dict getBool:&value forKey:34ULL]);
  1598. XCTAssertEqual(value, NO);
  1599. [dict removeBoolForKey:34ULL];
  1600. XCTAssertEqual(dict.count, 2U);
  1601. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1602. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1603. XCTAssertEqual(value, YES);
  1604. XCTAssertFalse([dict getBool:NULL forKey:32ULL]);
  1605. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1606. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1607. XCTAssertEqual(value, NO);
  1608. XCTAssertFalse([dict getBool:NULL forKey:34ULL]);
  1609. [dict removeAll];
  1610. XCTAssertEqual(dict.count, 0U);
  1611. XCTAssertFalse([dict getBool:NULL forKey:31ULL]);
  1612. XCTAssertFalse([dict getBool:NULL forKey:32ULL]);
  1613. XCTAssertFalse([dict getBool:NULL forKey:33ULL]);
  1614. XCTAssertFalse([dict getBool:NULL forKey:34ULL]);
  1615. [dict release];
  1616. }
  1617. - (void)testInplaceMutation {
  1618. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1619. const BOOL kValues[] = { YES, YES, NO, NO };
  1620. GPBUInt64BoolDictionary *dict =
  1621. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues
  1622. forKeys:kKeys
  1623. count:GPBARRAYSIZE(kValues)];
  1624. XCTAssertNotNil(dict);
  1625. XCTAssertEqual(dict.count, 4U);
  1626. BOOL value;
  1627. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1628. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1629. XCTAssertEqual(value, YES);
  1630. XCTAssertTrue([dict getBool:NULL forKey:32ULL]);
  1631. XCTAssertTrue([dict getBool:&value forKey:32ULL]);
  1632. XCTAssertEqual(value, YES);
  1633. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1634. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1635. XCTAssertEqual(value, NO);
  1636. XCTAssertTrue([dict getBool:NULL forKey:34ULL]);
  1637. XCTAssertTrue([dict getBool:&value forKey:34ULL]);
  1638. XCTAssertEqual(value, NO);
  1639. [dict setBool:NO forKey:31ULL];
  1640. XCTAssertEqual(dict.count, 4U);
  1641. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1642. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1643. XCTAssertEqual(value, NO);
  1644. XCTAssertTrue([dict getBool:NULL forKey:32ULL]);
  1645. XCTAssertTrue([dict getBool:&value forKey:32ULL]);
  1646. XCTAssertEqual(value, YES);
  1647. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1648. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1649. XCTAssertEqual(value, NO);
  1650. XCTAssertTrue([dict getBool:NULL forKey:34ULL]);
  1651. XCTAssertTrue([dict getBool:&value forKey:34ULL]);
  1652. XCTAssertEqual(value, NO);
  1653. [dict setBool:YES forKey:34ULL];
  1654. XCTAssertEqual(dict.count, 4U);
  1655. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1656. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1657. XCTAssertEqual(value, NO);
  1658. XCTAssertTrue([dict getBool:NULL forKey:32ULL]);
  1659. XCTAssertTrue([dict getBool:&value forKey:32ULL]);
  1660. XCTAssertEqual(value, YES);
  1661. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1662. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1663. XCTAssertEqual(value, NO);
  1664. XCTAssertTrue([dict getBool:NULL forKey:34ULL]);
  1665. XCTAssertTrue([dict getBool:&value forKey:34ULL]);
  1666. XCTAssertEqual(value, YES);
  1667. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  1668. const BOOL kValues2[] = { NO, YES };
  1669. GPBUInt64BoolDictionary *dict2 =
  1670. [[GPBUInt64BoolDictionary alloc] initWithBools:kValues2
  1671. forKeys:kKeys2
  1672. count:GPBARRAYSIZE(kValues2)];
  1673. XCTAssertNotNil(dict2);
  1674. [dict addEntriesFromDictionary:dict2];
  1675. XCTAssertEqual(dict.count, 4U);
  1676. XCTAssertTrue([dict getBool:NULL forKey:31ULL]);
  1677. XCTAssertTrue([dict getBool:&value forKey:31ULL]);
  1678. XCTAssertEqual(value, NO);
  1679. XCTAssertTrue([dict getBool:NULL forKey:32ULL]);
  1680. XCTAssertTrue([dict getBool:&value forKey:32ULL]);
  1681. XCTAssertEqual(value, NO);
  1682. XCTAssertTrue([dict getBool:NULL forKey:33ULL]);
  1683. XCTAssertTrue([dict getBool:&value forKey:33ULL]);
  1684. XCTAssertEqual(value, YES);
  1685. XCTAssertTrue([dict getBool:NULL forKey:34ULL]);
  1686. XCTAssertTrue([dict getBool:&value forKey:34ULL]);
  1687. XCTAssertEqual(value, YES);
  1688. [dict2 release];
  1689. [dict release];
  1690. }
  1691. @end
  1692. #pragma mark - UInt64 -> Float
  1693. @interface GPBUInt64FloatDictionaryTests : XCTestCase
  1694. @end
  1695. @implementation GPBUInt64FloatDictionaryTests
  1696. - (void)testEmpty {
  1697. GPBUInt64FloatDictionary *dict = [[GPBUInt64FloatDictionary alloc] init];
  1698. XCTAssertNotNil(dict);
  1699. XCTAssertEqual(dict.count, 0U);
  1700. XCTAssertFalse([dict getFloat:NULL forKey:31ULL]);
  1701. [dict enumerateKeysAndFloatsUsingBlock:^(uint64_t aKey, float aValue, BOOL *stop) {
  1702. #pragma unused(aKey, aValue, stop)
  1703. XCTFail(@"Shouldn't get here!");
  1704. }];
  1705. [dict release];
  1706. }
  1707. - (void)testOne {
  1708. GPBUInt64FloatDictionary *dict = [[GPBUInt64FloatDictionary alloc] init];
  1709. [dict setFloat:500.f forKey:31ULL];
  1710. XCTAssertNotNil(dict);
  1711. XCTAssertEqual(dict.count, 1U);
  1712. float value;
  1713. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1714. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1715. XCTAssertEqual(value, 500.f);
  1716. XCTAssertFalse([dict getFloat:NULL forKey:32ULL]);
  1717. [dict enumerateKeysAndFloatsUsingBlock:^(uint64_t aKey, float aValue, BOOL *stop) {
  1718. XCTAssertEqual(aKey, 31ULL);
  1719. XCTAssertEqual(aValue, 500.f);
  1720. XCTAssertNotEqual(stop, NULL);
  1721. }];
  1722. [dict release];
  1723. }
  1724. - (void)testBasics {
  1725. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  1726. const float kValues[] = { 500.f, 501.f, 502.f };
  1727. GPBUInt64FloatDictionary *dict =
  1728. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues
  1729. forKeys:kKeys
  1730. count:GPBARRAYSIZE(kValues)];
  1731. XCTAssertNotNil(dict);
  1732. XCTAssertEqual(dict.count, 3U);
  1733. float value;
  1734. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1735. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1736. XCTAssertEqual(value, 500.f);
  1737. XCTAssertTrue([dict getFloat:NULL forKey:32ULL]);
  1738. XCTAssertTrue([dict getFloat:&value forKey:32ULL]);
  1739. XCTAssertEqual(value, 501.f);
  1740. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  1741. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  1742. XCTAssertEqual(value, 502.f);
  1743. XCTAssertFalse([dict getFloat:NULL forKey:34ULL]);
  1744. __block NSUInteger idx = 0;
  1745. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  1746. float *seenValues = malloc(3 * sizeof(float));
  1747. [dict enumerateKeysAndFloatsUsingBlock:^(uint64_t aKey, float aValue, BOOL *stop) {
  1748. XCTAssertLessThan(idx, 3U);
  1749. seenKeys[idx] = aKey;
  1750. seenValues[idx] = aValue;
  1751. XCTAssertNotEqual(stop, NULL);
  1752. ++idx;
  1753. }];
  1754. for (int i = 0; i < 3; ++i) {
  1755. BOOL foundKey = NO;
  1756. for (int j = 0; (j < 3) && !foundKey; ++j) {
  1757. if (kKeys[i] == seenKeys[j]) {
  1758. foundKey = YES;
  1759. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  1760. }
  1761. }
  1762. XCTAssertTrue(foundKey, @"i = %d", i);
  1763. }
  1764. free(seenKeys);
  1765. free(seenValues);
  1766. // Stopping the enumeration.
  1767. idx = 0;
  1768. [dict enumerateKeysAndFloatsUsingBlock:^(uint64_t aKey, float aValue, BOOL *stop) {
  1769. #pragma unused(aKey, aValue)
  1770. if (idx == 1) *stop = YES;
  1771. XCTAssertNotEqual(idx, 2U);
  1772. ++idx;
  1773. }];
  1774. [dict release];
  1775. }
  1776. - (void)testEquality {
  1777. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1778. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  1779. const float kValues1[] = { 500.f, 501.f, 502.f };
  1780. const float kValues2[] = { 500.f, 503.f, 502.f };
  1781. const float kValues3[] = { 500.f, 501.f, 502.f, 503.f };
  1782. GPBUInt64FloatDictionary *dict1 =
  1783. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues1
  1784. forKeys:kKeys1
  1785. count:GPBARRAYSIZE(kValues1)];
  1786. XCTAssertNotNil(dict1);
  1787. GPBUInt64FloatDictionary *dict1prime =
  1788. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues1
  1789. forKeys:kKeys1
  1790. count:GPBARRAYSIZE(kValues1)];
  1791. XCTAssertNotNil(dict1prime);
  1792. GPBUInt64FloatDictionary *dict2 =
  1793. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues2
  1794. forKeys:kKeys1
  1795. count:GPBARRAYSIZE(kValues2)];
  1796. XCTAssertNotNil(dict2);
  1797. GPBUInt64FloatDictionary *dict3 =
  1798. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues1
  1799. forKeys:kKeys2
  1800. count:GPBARRAYSIZE(kValues1)];
  1801. XCTAssertNotNil(dict3);
  1802. GPBUInt64FloatDictionary *dict4 =
  1803. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues3
  1804. forKeys:kKeys1
  1805. count:GPBARRAYSIZE(kValues3)];
  1806. XCTAssertNotNil(dict4);
  1807. // 1/1Prime should be different objects, but equal.
  1808. XCTAssertNotEqual(dict1, dict1prime);
  1809. XCTAssertEqualObjects(dict1, dict1prime);
  1810. // Equal, so they must have same hash.
  1811. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  1812. // 2 is same keys, different values; not equal.
  1813. XCTAssertNotEqualObjects(dict1, dict2);
  1814. // 3 is different keys, same values; not equal.
  1815. XCTAssertNotEqualObjects(dict1, dict3);
  1816. // 4 extra pair; not equal
  1817. XCTAssertNotEqualObjects(dict1, dict4);
  1818. [dict1 release];
  1819. [dict1prime release];
  1820. [dict2 release];
  1821. [dict3 release];
  1822. [dict4 release];
  1823. }
  1824. - (void)testCopy {
  1825. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1826. const float kValues[] = { 500.f, 501.f, 502.f, 503.f };
  1827. GPBUInt64FloatDictionary *dict =
  1828. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues
  1829. forKeys:kKeys
  1830. count:GPBARRAYSIZE(kValues)];
  1831. XCTAssertNotNil(dict);
  1832. GPBUInt64FloatDictionary *dict2 = [dict copy];
  1833. XCTAssertNotNil(dict2);
  1834. // Should be new object but equal.
  1835. XCTAssertNotEqual(dict, dict2);
  1836. XCTAssertEqualObjects(dict, dict2);
  1837. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64FloatDictionary class]]);
  1838. [dict2 release];
  1839. [dict release];
  1840. }
  1841. - (void)testDictionaryFromDictionary {
  1842. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1843. const float kValues[] = { 500.f, 501.f, 502.f, 503.f };
  1844. GPBUInt64FloatDictionary *dict =
  1845. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues
  1846. forKeys:kKeys
  1847. count:GPBARRAYSIZE(kValues)];
  1848. XCTAssertNotNil(dict);
  1849. GPBUInt64FloatDictionary *dict2 =
  1850. [[GPBUInt64FloatDictionary alloc] initWithDictionary:dict];
  1851. XCTAssertNotNil(dict2);
  1852. // Should be new pointer, but equal objects.
  1853. XCTAssertNotEqual(dict, dict2);
  1854. XCTAssertEqualObjects(dict, dict2);
  1855. [dict2 release];
  1856. [dict release];
  1857. }
  1858. - (void)testAdds {
  1859. GPBUInt64FloatDictionary *dict = [[GPBUInt64FloatDictionary alloc] init];
  1860. XCTAssertNotNil(dict);
  1861. XCTAssertEqual(dict.count, 0U);
  1862. [dict setFloat:500.f forKey:31ULL];
  1863. XCTAssertEqual(dict.count, 1U);
  1864. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  1865. const float kValues[] = { 501.f, 502.f, 503.f };
  1866. GPBUInt64FloatDictionary *dict2 =
  1867. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues
  1868. forKeys:kKeys
  1869. count:GPBARRAYSIZE(kValues)];
  1870. XCTAssertNotNil(dict2);
  1871. [dict addEntriesFromDictionary:dict2];
  1872. XCTAssertEqual(dict.count, 4U);
  1873. float value;
  1874. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1875. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1876. XCTAssertEqual(value, 500.f);
  1877. XCTAssertTrue([dict getFloat:NULL forKey:32ULL]);
  1878. XCTAssertTrue([dict getFloat:&value forKey:32ULL]);
  1879. XCTAssertEqual(value, 501.f);
  1880. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  1881. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  1882. XCTAssertEqual(value, 502.f);
  1883. XCTAssertTrue([dict getFloat:NULL forKey:34ULL]);
  1884. XCTAssertTrue([dict getFloat:&value forKey:34ULL]);
  1885. XCTAssertEqual(value, 503.f);
  1886. [dict2 release];
  1887. [dict release];
  1888. }
  1889. - (void)testRemove {
  1890. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1891. const float kValues[] = { 500.f, 501.f, 502.f, 503.f };
  1892. GPBUInt64FloatDictionary *dict =
  1893. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues
  1894. forKeys:kKeys
  1895. count:GPBARRAYSIZE(kValues)];
  1896. XCTAssertNotNil(dict);
  1897. XCTAssertEqual(dict.count, 4U);
  1898. [dict removeFloatForKey:32ULL];
  1899. XCTAssertEqual(dict.count, 3U);
  1900. float value;
  1901. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1902. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1903. XCTAssertEqual(value, 500.f);
  1904. XCTAssertFalse([dict getFloat:NULL forKey:32ULL]);
  1905. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  1906. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  1907. XCTAssertEqual(value, 502.f);
  1908. XCTAssertTrue([dict getFloat:NULL forKey:34ULL]);
  1909. XCTAssertTrue([dict getFloat:&value forKey:34ULL]);
  1910. XCTAssertEqual(value, 503.f);
  1911. // Remove again does nothing.
  1912. [dict removeFloatForKey:32ULL];
  1913. XCTAssertEqual(dict.count, 3U);
  1914. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1915. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1916. XCTAssertEqual(value, 500.f);
  1917. XCTAssertFalse([dict getFloat:NULL forKey:32ULL]);
  1918. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  1919. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  1920. XCTAssertEqual(value, 502.f);
  1921. XCTAssertTrue([dict getFloat:NULL forKey:34ULL]);
  1922. XCTAssertTrue([dict getFloat:&value forKey:34ULL]);
  1923. XCTAssertEqual(value, 503.f);
  1924. [dict removeFloatForKey:34ULL];
  1925. XCTAssertEqual(dict.count, 2U);
  1926. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1927. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1928. XCTAssertEqual(value, 500.f);
  1929. XCTAssertFalse([dict getFloat:NULL forKey:32ULL]);
  1930. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  1931. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  1932. XCTAssertEqual(value, 502.f);
  1933. XCTAssertFalse([dict getFloat:NULL forKey:34ULL]);
  1934. [dict removeAll];
  1935. XCTAssertEqual(dict.count, 0U);
  1936. XCTAssertFalse([dict getFloat:NULL forKey:31ULL]);
  1937. XCTAssertFalse([dict getFloat:NULL forKey:32ULL]);
  1938. XCTAssertFalse([dict getFloat:NULL forKey:33ULL]);
  1939. XCTAssertFalse([dict getFloat:NULL forKey:34ULL]);
  1940. [dict release];
  1941. }
  1942. - (void)testInplaceMutation {
  1943. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  1944. const float kValues[] = { 500.f, 501.f, 502.f, 503.f };
  1945. GPBUInt64FloatDictionary *dict =
  1946. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues
  1947. forKeys:kKeys
  1948. count:GPBARRAYSIZE(kValues)];
  1949. XCTAssertNotNil(dict);
  1950. XCTAssertEqual(dict.count, 4U);
  1951. float value;
  1952. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1953. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1954. XCTAssertEqual(value, 500.f);
  1955. XCTAssertTrue([dict getFloat:NULL forKey:32ULL]);
  1956. XCTAssertTrue([dict getFloat:&value forKey:32ULL]);
  1957. XCTAssertEqual(value, 501.f);
  1958. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  1959. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  1960. XCTAssertEqual(value, 502.f);
  1961. XCTAssertTrue([dict getFloat:NULL forKey:34ULL]);
  1962. XCTAssertTrue([dict getFloat:&value forKey:34ULL]);
  1963. XCTAssertEqual(value, 503.f);
  1964. [dict setFloat:503.f forKey:31ULL];
  1965. XCTAssertEqual(dict.count, 4U);
  1966. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1967. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1968. XCTAssertEqual(value, 503.f);
  1969. XCTAssertTrue([dict getFloat:NULL forKey:32ULL]);
  1970. XCTAssertTrue([dict getFloat:&value forKey:32ULL]);
  1971. XCTAssertEqual(value, 501.f);
  1972. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  1973. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  1974. XCTAssertEqual(value, 502.f);
  1975. XCTAssertTrue([dict getFloat:NULL forKey:34ULL]);
  1976. XCTAssertTrue([dict getFloat:&value forKey:34ULL]);
  1977. XCTAssertEqual(value, 503.f);
  1978. [dict setFloat:501.f forKey:34ULL];
  1979. XCTAssertEqual(dict.count, 4U);
  1980. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  1981. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  1982. XCTAssertEqual(value, 503.f);
  1983. XCTAssertTrue([dict getFloat:NULL forKey:32ULL]);
  1984. XCTAssertTrue([dict getFloat:&value forKey:32ULL]);
  1985. XCTAssertEqual(value, 501.f);
  1986. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  1987. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  1988. XCTAssertEqual(value, 502.f);
  1989. XCTAssertTrue([dict getFloat:NULL forKey:34ULL]);
  1990. XCTAssertTrue([dict getFloat:&value forKey:34ULL]);
  1991. XCTAssertEqual(value, 501.f);
  1992. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  1993. const float kValues2[] = { 502.f, 500.f };
  1994. GPBUInt64FloatDictionary *dict2 =
  1995. [[GPBUInt64FloatDictionary alloc] initWithFloats:kValues2
  1996. forKeys:kKeys2
  1997. count:GPBARRAYSIZE(kValues2)];
  1998. XCTAssertNotNil(dict2);
  1999. [dict addEntriesFromDictionary:dict2];
  2000. XCTAssertEqual(dict.count, 4U);
  2001. XCTAssertTrue([dict getFloat:NULL forKey:31ULL]);
  2002. XCTAssertTrue([dict getFloat:&value forKey:31ULL]);
  2003. XCTAssertEqual(value, 503.f);
  2004. XCTAssertTrue([dict getFloat:NULL forKey:32ULL]);
  2005. XCTAssertTrue([dict getFloat:&value forKey:32ULL]);
  2006. XCTAssertEqual(value, 502.f);
  2007. XCTAssertTrue([dict getFloat:NULL forKey:33ULL]);
  2008. XCTAssertTrue([dict getFloat:&value forKey:33ULL]);
  2009. XCTAssertEqual(value, 500.f);
  2010. XCTAssertTrue([dict getFloat:NULL forKey:34ULL]);
  2011. XCTAssertTrue([dict getFloat:&value forKey:34ULL]);
  2012. XCTAssertEqual(value, 501.f);
  2013. [dict2 release];
  2014. [dict release];
  2015. }
  2016. @end
  2017. #pragma mark - UInt64 -> Double
  2018. @interface GPBUInt64DoubleDictionaryTests : XCTestCase
  2019. @end
  2020. @implementation GPBUInt64DoubleDictionaryTests
  2021. - (void)testEmpty {
  2022. GPBUInt64DoubleDictionary *dict = [[GPBUInt64DoubleDictionary alloc] init];
  2023. XCTAssertNotNil(dict);
  2024. XCTAssertEqual(dict.count, 0U);
  2025. XCTAssertFalse([dict getDouble:NULL forKey:31ULL]);
  2026. [dict enumerateKeysAndDoublesUsingBlock:^(uint64_t aKey, double aValue, BOOL *stop) {
  2027. #pragma unused(aKey, aValue, stop)
  2028. XCTFail(@"Shouldn't get here!");
  2029. }];
  2030. [dict release];
  2031. }
  2032. - (void)testOne {
  2033. GPBUInt64DoubleDictionary *dict = [[GPBUInt64DoubleDictionary alloc] init];
  2034. [dict setDouble:600. forKey:31ULL];
  2035. XCTAssertNotNil(dict);
  2036. XCTAssertEqual(dict.count, 1U);
  2037. double value;
  2038. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2039. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2040. XCTAssertEqual(value, 600.);
  2041. XCTAssertFalse([dict getDouble:NULL forKey:32ULL]);
  2042. [dict enumerateKeysAndDoublesUsingBlock:^(uint64_t aKey, double aValue, BOOL *stop) {
  2043. XCTAssertEqual(aKey, 31ULL);
  2044. XCTAssertEqual(aValue, 600.);
  2045. XCTAssertNotEqual(stop, NULL);
  2046. }];
  2047. [dict release];
  2048. }
  2049. - (void)testBasics {
  2050. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  2051. const double kValues[] = { 600., 601., 602. };
  2052. GPBUInt64DoubleDictionary *dict =
  2053. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues
  2054. forKeys:kKeys
  2055. count:GPBARRAYSIZE(kValues)];
  2056. XCTAssertNotNil(dict);
  2057. XCTAssertEqual(dict.count, 3U);
  2058. double value;
  2059. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2060. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2061. XCTAssertEqual(value, 600.);
  2062. XCTAssertTrue([dict getDouble:NULL forKey:32ULL]);
  2063. XCTAssertTrue([dict getDouble:&value forKey:32ULL]);
  2064. XCTAssertEqual(value, 601.);
  2065. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2066. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2067. XCTAssertEqual(value, 602.);
  2068. XCTAssertFalse([dict getDouble:NULL forKey:34ULL]);
  2069. __block NSUInteger idx = 0;
  2070. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  2071. double *seenValues = malloc(3 * sizeof(double));
  2072. [dict enumerateKeysAndDoublesUsingBlock:^(uint64_t aKey, double aValue, BOOL *stop) {
  2073. XCTAssertLessThan(idx, 3U);
  2074. seenKeys[idx] = aKey;
  2075. seenValues[idx] = aValue;
  2076. XCTAssertNotEqual(stop, NULL);
  2077. ++idx;
  2078. }];
  2079. for (int i = 0; i < 3; ++i) {
  2080. BOOL foundKey = NO;
  2081. for (int j = 0; (j < 3) && !foundKey; ++j) {
  2082. if (kKeys[i] == seenKeys[j]) {
  2083. foundKey = YES;
  2084. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  2085. }
  2086. }
  2087. XCTAssertTrue(foundKey, @"i = %d", i);
  2088. }
  2089. free(seenKeys);
  2090. free(seenValues);
  2091. // Stopping the enumeration.
  2092. idx = 0;
  2093. [dict enumerateKeysAndDoublesUsingBlock:^(uint64_t aKey, double aValue, BOOL *stop) {
  2094. #pragma unused(aKey, aValue)
  2095. if (idx == 1) *stop = YES;
  2096. XCTAssertNotEqual(idx, 2U);
  2097. ++idx;
  2098. }];
  2099. [dict release];
  2100. }
  2101. - (void)testEquality {
  2102. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2103. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  2104. const double kValues1[] = { 600., 601., 602. };
  2105. const double kValues2[] = { 600., 603., 602. };
  2106. const double kValues3[] = { 600., 601., 602., 603. };
  2107. GPBUInt64DoubleDictionary *dict1 =
  2108. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues1
  2109. forKeys:kKeys1
  2110. count:GPBARRAYSIZE(kValues1)];
  2111. XCTAssertNotNil(dict1);
  2112. GPBUInt64DoubleDictionary *dict1prime =
  2113. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues1
  2114. forKeys:kKeys1
  2115. count:GPBARRAYSIZE(kValues1)];
  2116. XCTAssertNotNil(dict1prime);
  2117. GPBUInt64DoubleDictionary *dict2 =
  2118. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues2
  2119. forKeys:kKeys1
  2120. count:GPBARRAYSIZE(kValues2)];
  2121. XCTAssertNotNil(dict2);
  2122. GPBUInt64DoubleDictionary *dict3 =
  2123. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues1
  2124. forKeys:kKeys2
  2125. count:GPBARRAYSIZE(kValues1)];
  2126. XCTAssertNotNil(dict3);
  2127. GPBUInt64DoubleDictionary *dict4 =
  2128. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues3
  2129. forKeys:kKeys1
  2130. count:GPBARRAYSIZE(kValues3)];
  2131. XCTAssertNotNil(dict4);
  2132. // 1/1Prime should be different objects, but equal.
  2133. XCTAssertNotEqual(dict1, dict1prime);
  2134. XCTAssertEqualObjects(dict1, dict1prime);
  2135. // Equal, so they must have same hash.
  2136. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  2137. // 2 is same keys, different values; not equal.
  2138. XCTAssertNotEqualObjects(dict1, dict2);
  2139. // 3 is different keys, same values; not equal.
  2140. XCTAssertNotEqualObjects(dict1, dict3);
  2141. // 4 extra pair; not equal
  2142. XCTAssertNotEqualObjects(dict1, dict4);
  2143. [dict1 release];
  2144. [dict1prime release];
  2145. [dict2 release];
  2146. [dict3 release];
  2147. [dict4 release];
  2148. }
  2149. - (void)testCopy {
  2150. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2151. const double kValues[] = { 600., 601., 602., 603. };
  2152. GPBUInt64DoubleDictionary *dict =
  2153. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues
  2154. forKeys:kKeys
  2155. count:GPBARRAYSIZE(kValues)];
  2156. XCTAssertNotNil(dict);
  2157. GPBUInt64DoubleDictionary *dict2 = [dict copy];
  2158. XCTAssertNotNil(dict2);
  2159. // Should be new object but equal.
  2160. XCTAssertNotEqual(dict, dict2);
  2161. XCTAssertEqualObjects(dict, dict2);
  2162. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64DoubleDictionary class]]);
  2163. [dict2 release];
  2164. [dict release];
  2165. }
  2166. - (void)testDictionaryFromDictionary {
  2167. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2168. const double kValues[] = { 600., 601., 602., 603. };
  2169. GPBUInt64DoubleDictionary *dict =
  2170. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues
  2171. forKeys:kKeys
  2172. count:GPBARRAYSIZE(kValues)];
  2173. XCTAssertNotNil(dict);
  2174. GPBUInt64DoubleDictionary *dict2 =
  2175. [[GPBUInt64DoubleDictionary alloc] initWithDictionary:dict];
  2176. XCTAssertNotNil(dict2);
  2177. // Should be new pointer, but equal objects.
  2178. XCTAssertNotEqual(dict, dict2);
  2179. XCTAssertEqualObjects(dict, dict2);
  2180. [dict2 release];
  2181. [dict release];
  2182. }
  2183. - (void)testAdds {
  2184. GPBUInt64DoubleDictionary *dict = [[GPBUInt64DoubleDictionary alloc] init];
  2185. XCTAssertNotNil(dict);
  2186. XCTAssertEqual(dict.count, 0U);
  2187. [dict setDouble:600. forKey:31ULL];
  2188. XCTAssertEqual(dict.count, 1U);
  2189. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  2190. const double kValues[] = { 601., 602., 603. };
  2191. GPBUInt64DoubleDictionary *dict2 =
  2192. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues
  2193. forKeys:kKeys
  2194. count:GPBARRAYSIZE(kValues)];
  2195. XCTAssertNotNil(dict2);
  2196. [dict addEntriesFromDictionary:dict2];
  2197. XCTAssertEqual(dict.count, 4U);
  2198. double value;
  2199. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2200. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2201. XCTAssertEqual(value, 600.);
  2202. XCTAssertTrue([dict getDouble:NULL forKey:32ULL]);
  2203. XCTAssertTrue([dict getDouble:&value forKey:32ULL]);
  2204. XCTAssertEqual(value, 601.);
  2205. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2206. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2207. XCTAssertEqual(value, 602.);
  2208. XCTAssertTrue([dict getDouble:NULL forKey:34ULL]);
  2209. XCTAssertTrue([dict getDouble:&value forKey:34ULL]);
  2210. XCTAssertEqual(value, 603.);
  2211. [dict2 release];
  2212. [dict release];
  2213. }
  2214. - (void)testRemove {
  2215. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2216. const double kValues[] = { 600., 601., 602., 603. };
  2217. GPBUInt64DoubleDictionary *dict =
  2218. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues
  2219. forKeys:kKeys
  2220. count:GPBARRAYSIZE(kValues)];
  2221. XCTAssertNotNil(dict);
  2222. XCTAssertEqual(dict.count, 4U);
  2223. [dict removeDoubleForKey:32ULL];
  2224. XCTAssertEqual(dict.count, 3U);
  2225. double value;
  2226. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2227. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2228. XCTAssertEqual(value, 600.);
  2229. XCTAssertFalse([dict getDouble:NULL forKey:32ULL]);
  2230. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2231. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2232. XCTAssertEqual(value, 602.);
  2233. XCTAssertTrue([dict getDouble:NULL forKey:34ULL]);
  2234. XCTAssertTrue([dict getDouble:&value forKey:34ULL]);
  2235. XCTAssertEqual(value, 603.);
  2236. // Remove again does nothing.
  2237. [dict removeDoubleForKey:32ULL];
  2238. XCTAssertEqual(dict.count, 3U);
  2239. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2240. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2241. XCTAssertEqual(value, 600.);
  2242. XCTAssertFalse([dict getDouble:NULL forKey:32ULL]);
  2243. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2244. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2245. XCTAssertEqual(value, 602.);
  2246. XCTAssertTrue([dict getDouble:NULL forKey:34ULL]);
  2247. XCTAssertTrue([dict getDouble:&value forKey:34ULL]);
  2248. XCTAssertEqual(value, 603.);
  2249. [dict removeDoubleForKey:34ULL];
  2250. XCTAssertEqual(dict.count, 2U);
  2251. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2252. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2253. XCTAssertEqual(value, 600.);
  2254. XCTAssertFalse([dict getDouble:NULL forKey:32ULL]);
  2255. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2256. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2257. XCTAssertEqual(value, 602.);
  2258. XCTAssertFalse([dict getDouble:NULL forKey:34ULL]);
  2259. [dict removeAll];
  2260. XCTAssertEqual(dict.count, 0U);
  2261. XCTAssertFalse([dict getDouble:NULL forKey:31ULL]);
  2262. XCTAssertFalse([dict getDouble:NULL forKey:32ULL]);
  2263. XCTAssertFalse([dict getDouble:NULL forKey:33ULL]);
  2264. XCTAssertFalse([dict getDouble:NULL forKey:34ULL]);
  2265. [dict release];
  2266. }
  2267. - (void)testInplaceMutation {
  2268. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2269. const double kValues[] = { 600., 601., 602., 603. };
  2270. GPBUInt64DoubleDictionary *dict =
  2271. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues
  2272. forKeys:kKeys
  2273. count:GPBARRAYSIZE(kValues)];
  2274. XCTAssertNotNil(dict);
  2275. XCTAssertEqual(dict.count, 4U);
  2276. double value;
  2277. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2278. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2279. XCTAssertEqual(value, 600.);
  2280. XCTAssertTrue([dict getDouble:NULL forKey:32ULL]);
  2281. XCTAssertTrue([dict getDouble:&value forKey:32ULL]);
  2282. XCTAssertEqual(value, 601.);
  2283. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2284. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2285. XCTAssertEqual(value, 602.);
  2286. XCTAssertTrue([dict getDouble:NULL forKey:34ULL]);
  2287. XCTAssertTrue([dict getDouble:&value forKey:34ULL]);
  2288. XCTAssertEqual(value, 603.);
  2289. [dict setDouble:603. forKey:31ULL];
  2290. XCTAssertEqual(dict.count, 4U);
  2291. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2292. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2293. XCTAssertEqual(value, 603.);
  2294. XCTAssertTrue([dict getDouble:NULL forKey:32ULL]);
  2295. XCTAssertTrue([dict getDouble:&value forKey:32ULL]);
  2296. XCTAssertEqual(value, 601.);
  2297. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2298. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2299. XCTAssertEqual(value, 602.);
  2300. XCTAssertTrue([dict getDouble:NULL forKey:34ULL]);
  2301. XCTAssertTrue([dict getDouble:&value forKey:34ULL]);
  2302. XCTAssertEqual(value, 603.);
  2303. [dict setDouble:601. forKey:34ULL];
  2304. XCTAssertEqual(dict.count, 4U);
  2305. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2306. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2307. XCTAssertEqual(value, 603.);
  2308. XCTAssertTrue([dict getDouble:NULL forKey:32ULL]);
  2309. XCTAssertTrue([dict getDouble:&value forKey:32ULL]);
  2310. XCTAssertEqual(value, 601.);
  2311. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2312. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2313. XCTAssertEqual(value, 602.);
  2314. XCTAssertTrue([dict getDouble:NULL forKey:34ULL]);
  2315. XCTAssertTrue([dict getDouble:&value forKey:34ULL]);
  2316. XCTAssertEqual(value, 601.);
  2317. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  2318. const double kValues2[] = { 602., 600. };
  2319. GPBUInt64DoubleDictionary *dict2 =
  2320. [[GPBUInt64DoubleDictionary alloc] initWithDoubles:kValues2
  2321. forKeys:kKeys2
  2322. count:GPBARRAYSIZE(kValues2)];
  2323. XCTAssertNotNil(dict2);
  2324. [dict addEntriesFromDictionary:dict2];
  2325. XCTAssertEqual(dict.count, 4U);
  2326. XCTAssertTrue([dict getDouble:NULL forKey:31ULL]);
  2327. XCTAssertTrue([dict getDouble:&value forKey:31ULL]);
  2328. XCTAssertEqual(value, 603.);
  2329. XCTAssertTrue([dict getDouble:NULL forKey:32ULL]);
  2330. XCTAssertTrue([dict getDouble:&value forKey:32ULL]);
  2331. XCTAssertEqual(value, 602.);
  2332. XCTAssertTrue([dict getDouble:NULL forKey:33ULL]);
  2333. XCTAssertTrue([dict getDouble:&value forKey:33ULL]);
  2334. XCTAssertEqual(value, 600.);
  2335. XCTAssertTrue([dict getDouble:NULL forKey:34ULL]);
  2336. XCTAssertTrue([dict getDouble:&value forKey:34ULL]);
  2337. XCTAssertEqual(value, 601.);
  2338. [dict2 release];
  2339. [dict release];
  2340. }
  2341. @end
  2342. #pragma mark - UInt64 -> Enum
  2343. @interface GPBUInt64EnumDictionaryTests : XCTestCase
  2344. @end
  2345. @implementation GPBUInt64EnumDictionaryTests
  2346. - (void)testEmpty {
  2347. GPBUInt64EnumDictionary *dict = [[GPBUInt64EnumDictionary alloc] init];
  2348. XCTAssertNotNil(dict);
  2349. XCTAssertEqual(dict.count, 0U);
  2350. XCTAssertFalse([dict getEnum:NULL forKey:31ULL]);
  2351. [dict enumerateKeysAndEnumsUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  2352. #pragma unused(aKey, aValue, stop)
  2353. XCTFail(@"Shouldn't get here!");
  2354. }];
  2355. [dict release];
  2356. }
  2357. - (void)testOne {
  2358. GPBUInt64EnumDictionary *dict = [[GPBUInt64EnumDictionary alloc] init];
  2359. [dict setEnum:700 forKey:31ULL];
  2360. XCTAssertNotNil(dict);
  2361. XCTAssertEqual(dict.count, 1U);
  2362. int32_t value;
  2363. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2364. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2365. XCTAssertEqual(value, 700);
  2366. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2367. [dict enumerateKeysAndEnumsUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  2368. XCTAssertEqual(aKey, 31ULL);
  2369. XCTAssertEqual(aValue, 700);
  2370. XCTAssertNotEqual(stop, NULL);
  2371. }];
  2372. [dict release];
  2373. }
  2374. - (void)testBasics {
  2375. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  2376. const int32_t kValues[] = { 700, 701, 702 };
  2377. GPBUInt64EnumDictionary *dict =
  2378. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues
  2379. forKeys:kKeys
  2380. count:GPBARRAYSIZE(kValues)];
  2381. XCTAssertNotNil(dict);
  2382. XCTAssertEqual(dict.count, 3U);
  2383. int32_t value;
  2384. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2385. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2386. XCTAssertEqual(value, 700);
  2387. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  2388. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  2389. XCTAssertEqual(value, 701);
  2390. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2391. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2392. XCTAssertEqual(value, 702);
  2393. XCTAssertFalse([dict getEnum:NULL forKey:34ULL]);
  2394. __block NSUInteger idx = 0;
  2395. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  2396. int32_t *seenValues = malloc(3 * sizeof(int32_t));
  2397. [dict enumerateKeysAndEnumsUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  2398. XCTAssertLessThan(idx, 3U);
  2399. seenKeys[idx] = aKey;
  2400. seenValues[idx] = aValue;
  2401. XCTAssertNotEqual(stop, NULL);
  2402. ++idx;
  2403. }];
  2404. for (int i = 0; i < 3; ++i) {
  2405. BOOL foundKey = NO;
  2406. for (int j = 0; (j < 3) && !foundKey; ++j) {
  2407. if (kKeys[i] == seenKeys[j]) {
  2408. foundKey = YES;
  2409. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  2410. }
  2411. }
  2412. XCTAssertTrue(foundKey, @"i = %d", i);
  2413. }
  2414. free(seenKeys);
  2415. free(seenValues);
  2416. // Stopping the enumeration.
  2417. idx = 0;
  2418. [dict enumerateKeysAndEnumsUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  2419. #pragma unused(aKey, aValue)
  2420. if (idx == 1) *stop = YES;
  2421. XCTAssertNotEqual(idx, 2U);
  2422. ++idx;
  2423. }];
  2424. [dict release];
  2425. }
  2426. - (void)testEquality {
  2427. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2428. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  2429. const int32_t kValues1[] = { 700, 701, 702 };
  2430. const int32_t kValues2[] = { 700, 703, 702 };
  2431. const int32_t kValues3[] = { 700, 701, 702, 703 };
  2432. GPBUInt64EnumDictionary *dict1 =
  2433. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues1
  2434. forKeys:kKeys1
  2435. count:GPBARRAYSIZE(kValues1)];
  2436. XCTAssertNotNil(dict1);
  2437. GPBUInt64EnumDictionary *dict1prime =
  2438. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues1
  2439. forKeys:kKeys1
  2440. count:GPBARRAYSIZE(kValues1)];
  2441. XCTAssertNotNil(dict1prime);
  2442. GPBUInt64EnumDictionary *dict2 =
  2443. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues2
  2444. forKeys:kKeys1
  2445. count:GPBARRAYSIZE(kValues2)];
  2446. XCTAssertNotNil(dict2);
  2447. GPBUInt64EnumDictionary *dict3 =
  2448. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues1
  2449. forKeys:kKeys2
  2450. count:GPBARRAYSIZE(kValues1)];
  2451. XCTAssertNotNil(dict3);
  2452. GPBUInt64EnumDictionary *dict4 =
  2453. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues3
  2454. forKeys:kKeys1
  2455. count:GPBARRAYSIZE(kValues3)];
  2456. XCTAssertNotNil(dict4);
  2457. // 1/1Prime should be different objects, but equal.
  2458. XCTAssertNotEqual(dict1, dict1prime);
  2459. XCTAssertEqualObjects(dict1, dict1prime);
  2460. // Equal, so they must have same hash.
  2461. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  2462. // 2 is same keys, different values; not equal.
  2463. XCTAssertNotEqualObjects(dict1, dict2);
  2464. // 3 is different keys, same values; not equal.
  2465. XCTAssertNotEqualObjects(dict1, dict3);
  2466. // 4 extra pair; not equal
  2467. XCTAssertNotEqualObjects(dict1, dict4);
  2468. [dict1 release];
  2469. [dict1prime release];
  2470. [dict2 release];
  2471. [dict3 release];
  2472. [dict4 release];
  2473. }
  2474. - (void)testCopy {
  2475. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2476. const int32_t kValues[] = { 700, 701, 702, 703 };
  2477. GPBUInt64EnumDictionary *dict =
  2478. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues
  2479. forKeys:kKeys
  2480. count:GPBARRAYSIZE(kValues)];
  2481. XCTAssertNotNil(dict);
  2482. GPBUInt64EnumDictionary *dict2 = [dict copy];
  2483. XCTAssertNotNil(dict2);
  2484. // Should be new object but equal.
  2485. XCTAssertNotEqual(dict, dict2);
  2486. XCTAssertEqualObjects(dict, dict2);
  2487. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64EnumDictionary class]]);
  2488. [dict2 release];
  2489. [dict release];
  2490. }
  2491. - (void)testDictionaryFromDictionary {
  2492. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2493. const int32_t kValues[] = { 700, 701, 702, 703 };
  2494. GPBUInt64EnumDictionary *dict =
  2495. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues
  2496. forKeys:kKeys
  2497. count:GPBARRAYSIZE(kValues)];
  2498. XCTAssertNotNil(dict);
  2499. GPBUInt64EnumDictionary *dict2 =
  2500. [[GPBUInt64EnumDictionary alloc] initWithDictionary:dict];
  2501. XCTAssertNotNil(dict2);
  2502. // Should be new pointer, but equal objects.
  2503. XCTAssertNotEqual(dict, dict2);
  2504. XCTAssertEqualObjects(dict, dict2);
  2505. [dict2 release];
  2506. [dict release];
  2507. }
  2508. - (void)testAdds {
  2509. GPBUInt64EnumDictionary *dict = [[GPBUInt64EnumDictionary alloc] init];
  2510. XCTAssertNotNil(dict);
  2511. XCTAssertEqual(dict.count, 0U);
  2512. [dict setEnum:700 forKey:31ULL];
  2513. XCTAssertEqual(dict.count, 1U);
  2514. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  2515. const int32_t kValues[] = { 701, 702, 703 };
  2516. GPBUInt64EnumDictionary *dict2 =
  2517. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues
  2518. forKeys:kKeys
  2519. count:GPBARRAYSIZE(kValues)];
  2520. XCTAssertNotNil(dict2);
  2521. [dict addRawEntriesFromDictionary:dict2];
  2522. XCTAssertEqual(dict.count, 4U);
  2523. int32_t value;
  2524. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2525. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2526. XCTAssertEqual(value, 700);
  2527. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  2528. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  2529. XCTAssertEqual(value, 701);
  2530. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2531. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2532. XCTAssertEqual(value, 702);
  2533. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2534. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  2535. XCTAssertEqual(value, 703);
  2536. [dict2 release];
  2537. [dict release];
  2538. }
  2539. - (void)testRemove {
  2540. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2541. const int32_t kValues[] = { 700, 701, 702, 703 };
  2542. GPBUInt64EnumDictionary *dict =
  2543. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues
  2544. forKeys:kKeys
  2545. count:GPBARRAYSIZE(kValues)];
  2546. XCTAssertNotNil(dict);
  2547. XCTAssertEqual(dict.count, 4U);
  2548. [dict removeEnumForKey:32ULL];
  2549. XCTAssertEqual(dict.count, 3U);
  2550. int32_t value;
  2551. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2552. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2553. XCTAssertEqual(value, 700);
  2554. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2555. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2556. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2557. XCTAssertEqual(value, 702);
  2558. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2559. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  2560. XCTAssertEqual(value, 703);
  2561. // Remove again does nothing.
  2562. [dict removeEnumForKey:32ULL];
  2563. XCTAssertEqual(dict.count, 3U);
  2564. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2565. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2566. XCTAssertEqual(value, 700);
  2567. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2568. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2569. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2570. XCTAssertEqual(value, 702);
  2571. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2572. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  2573. XCTAssertEqual(value, 703);
  2574. [dict removeEnumForKey:34ULL];
  2575. XCTAssertEqual(dict.count, 2U);
  2576. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2577. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2578. XCTAssertEqual(value, 700);
  2579. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2580. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2581. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2582. XCTAssertEqual(value, 702);
  2583. XCTAssertFalse([dict getEnum:NULL forKey:34ULL]);
  2584. [dict removeAll];
  2585. XCTAssertEqual(dict.count, 0U);
  2586. XCTAssertFalse([dict getEnum:NULL forKey:31ULL]);
  2587. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2588. XCTAssertFalse([dict getEnum:NULL forKey:33ULL]);
  2589. XCTAssertFalse([dict getEnum:NULL forKey:34ULL]);
  2590. [dict release];
  2591. }
  2592. - (void)testInplaceMutation {
  2593. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2594. const int32_t kValues[] = { 700, 701, 702, 703 };
  2595. GPBUInt64EnumDictionary *dict =
  2596. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues
  2597. forKeys:kKeys
  2598. count:GPBARRAYSIZE(kValues)];
  2599. XCTAssertNotNil(dict);
  2600. XCTAssertEqual(dict.count, 4U);
  2601. int32_t value;
  2602. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2603. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2604. XCTAssertEqual(value, 700);
  2605. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  2606. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  2607. XCTAssertEqual(value, 701);
  2608. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2609. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2610. XCTAssertEqual(value, 702);
  2611. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2612. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  2613. XCTAssertEqual(value, 703);
  2614. [dict setEnum:703 forKey:31ULL];
  2615. XCTAssertEqual(dict.count, 4U);
  2616. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2617. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2618. XCTAssertEqual(value, 703);
  2619. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  2620. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  2621. XCTAssertEqual(value, 701);
  2622. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2623. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2624. XCTAssertEqual(value, 702);
  2625. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2626. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  2627. XCTAssertEqual(value, 703);
  2628. [dict setEnum:701 forKey:34ULL];
  2629. XCTAssertEqual(dict.count, 4U);
  2630. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2631. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2632. XCTAssertEqual(value, 703);
  2633. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  2634. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  2635. XCTAssertEqual(value, 701);
  2636. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2637. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2638. XCTAssertEqual(value, 702);
  2639. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2640. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  2641. XCTAssertEqual(value, 701);
  2642. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  2643. const int32_t kValues2[] = { 702, 700 };
  2644. GPBUInt64EnumDictionary *dict2 =
  2645. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues2
  2646. forKeys:kKeys2
  2647. count:GPBARRAYSIZE(kValues2)];
  2648. XCTAssertNotNil(dict2);
  2649. [dict addRawEntriesFromDictionary:dict2];
  2650. XCTAssertEqual(dict.count, 4U);
  2651. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2652. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2653. XCTAssertEqual(value, 703);
  2654. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  2655. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  2656. XCTAssertEqual(value, 702);
  2657. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2658. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2659. XCTAssertEqual(value, 700);
  2660. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2661. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  2662. XCTAssertEqual(value, 701);
  2663. [dict2 release];
  2664. [dict release];
  2665. }
  2666. @end
  2667. #pragma mark - UInt64 -> Enum (Unknown Enums)
  2668. @interface GPBUInt64EnumDictionaryUnknownEnumTests : XCTestCase
  2669. @end
  2670. @implementation GPBUInt64EnumDictionaryUnknownEnumTests
  2671. - (void)testRawBasics {
  2672. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  2673. const int32_t kValues[] = { 700, 801, 702 };
  2674. GPBUInt64EnumDictionary *dict =
  2675. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2676. rawValues:kValues
  2677. forKeys:kKeys
  2678. count:GPBARRAYSIZE(kValues)];
  2679. XCTAssertNotNil(dict);
  2680. XCTAssertEqual(dict.count, 3U);
  2681. XCTAssertTrue(dict.validationFunc == TestingEnum_IsValidValue); // Pointer comparison
  2682. int32_t value;
  2683. XCTAssertTrue([dict getRawValue:NULL forKey:31ULL]);
  2684. XCTAssertTrue([dict getRawValue:&value forKey:31ULL]);
  2685. XCTAssertEqual(value, 700);
  2686. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  2687. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  2688. XCTAssertEqual(value, kGPBUnrecognizedEnumeratorValue);
  2689. XCTAssertTrue([dict getRawValue:NULL forKey:32ULL]);
  2690. XCTAssertTrue([dict getRawValue:&value forKey:32ULL]);
  2691. XCTAssertEqual(value, 801);
  2692. XCTAssertTrue([dict getRawValue:NULL forKey:33ULL]);
  2693. XCTAssertTrue([dict getRawValue:&value forKey:33ULL]);
  2694. XCTAssertEqual(value, 702);
  2695. XCTAssertFalse([dict getRawValue:NULL forKey:34ULL]);
  2696. __block NSUInteger idx = 0;
  2697. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  2698. int32_t *seenValues = malloc(3 * sizeof(int32_t));
  2699. [dict enumerateKeysAndEnumsUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  2700. XCTAssertLessThan(idx, 3U);
  2701. seenKeys[idx] = aKey;
  2702. seenValues[idx] = aValue;
  2703. XCTAssertNotEqual(stop, NULL);
  2704. ++idx;
  2705. }];
  2706. for (int i = 0; i < 3; ++i) {
  2707. BOOL foundKey = NO;
  2708. for (int j = 0; (j < 3) && !foundKey; ++j) {
  2709. if (kKeys[i] == seenKeys[j]) {
  2710. foundKey = YES;
  2711. if (i == 1) {
  2712. XCTAssertEqual(kGPBUnrecognizedEnumeratorValue, seenValues[j], @"i = %d, j = %d", i, j);
  2713. } else {
  2714. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  2715. }
  2716. }
  2717. }
  2718. XCTAssertTrue(foundKey, @"i = %d", i);
  2719. }
  2720. idx = 0;
  2721. [dict enumerateKeysAndRawValuesUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  2722. XCTAssertLessThan(idx, 3U);
  2723. seenKeys[idx] = aKey;
  2724. seenValues[idx] = aValue;
  2725. XCTAssertNotEqual(stop, NULL);
  2726. ++idx;
  2727. }];
  2728. for (int i = 0; i < 3; ++i) {
  2729. BOOL foundKey = NO;
  2730. for (int j = 0; (j < 3) && !foundKey; ++j) {
  2731. if (kKeys[i] == seenKeys[j]) {
  2732. foundKey = YES;
  2733. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  2734. }
  2735. }
  2736. XCTAssertTrue(foundKey, @"i = %d", i);
  2737. }
  2738. free(seenKeys);
  2739. free(seenValues);
  2740. // Stopping the enumeration.
  2741. idx = 0;
  2742. [dict enumerateKeysAndRawValuesUsingBlock:^(uint64_t aKey, int32_t aValue, BOOL *stop) {
  2743. #pragma unused(aKey, aValue)
  2744. if (idx == 1) *stop = YES;
  2745. XCTAssertNotEqual(idx, 2U);
  2746. ++idx;
  2747. }];
  2748. [dict release];
  2749. }
  2750. - (void)testEqualityWithUnknowns {
  2751. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2752. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  2753. const int32_t kValues1[] = { 700, 801, 702 }; // Unknown
  2754. const int32_t kValues2[] = { 700, 803, 702 }; // Unknown
  2755. const int32_t kValues3[] = { 700, 801, 702, 803 }; // Unknowns
  2756. GPBUInt64EnumDictionary *dict1 =
  2757. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2758. rawValues:kValues1
  2759. forKeys:kKeys1
  2760. count:GPBARRAYSIZE(kValues1)];
  2761. XCTAssertNotNil(dict1);
  2762. GPBUInt64EnumDictionary *dict1prime =
  2763. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2764. rawValues:kValues1
  2765. forKeys:kKeys1
  2766. count:GPBARRAYSIZE(kValues1)];
  2767. XCTAssertNotNil(dict1prime);
  2768. GPBUInt64EnumDictionary *dict2 =
  2769. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2770. rawValues:kValues2
  2771. forKeys:kKeys1
  2772. count:GPBARRAYSIZE(kValues2)];
  2773. XCTAssertNotNil(dict2);
  2774. GPBUInt64EnumDictionary *dict3 =
  2775. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2776. rawValues:kValues1
  2777. forKeys:kKeys2
  2778. count:GPBARRAYSIZE(kValues1)];
  2779. XCTAssertNotNil(dict3);
  2780. GPBUInt64EnumDictionary *dict4 =
  2781. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2782. rawValues:kValues3
  2783. forKeys:kKeys1
  2784. count:GPBARRAYSIZE(kValues3)];
  2785. XCTAssertNotNil(dict4);
  2786. // 1/1Prime should be different objects, but equal.
  2787. XCTAssertNotEqual(dict1, dict1prime);
  2788. XCTAssertEqualObjects(dict1, dict1prime);
  2789. // Equal, so they must have same hash.
  2790. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  2791. // 2 is same keys, different values; not equal.
  2792. XCTAssertNotEqualObjects(dict1, dict2);
  2793. // 3 is different keys, same values; not equal.
  2794. XCTAssertNotEqualObjects(dict1, dict3);
  2795. // 4 extra pair; not equal
  2796. XCTAssertNotEqualObjects(dict1, dict4);
  2797. [dict1 release];
  2798. [dict1prime release];
  2799. [dict2 release];
  2800. [dict3 release];
  2801. [dict4 release];
  2802. }
  2803. - (void)testCopyWithUnknowns {
  2804. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2805. const int32_t kValues[] = { 700, 801, 702, 803 }; // Unknown
  2806. GPBUInt64EnumDictionary *dict =
  2807. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2808. rawValues:kValues
  2809. forKeys:kKeys
  2810. count:GPBARRAYSIZE(kValues)];
  2811. XCTAssertNotNil(dict);
  2812. GPBUInt64EnumDictionary *dict2 = [dict copy];
  2813. XCTAssertNotNil(dict2);
  2814. // Should be new pointer, but equal objects.
  2815. XCTAssertNotEqual(dict, dict2);
  2816. XCTAssertEqual(dict.validationFunc, dict2.validationFunc); // Pointer comparison
  2817. XCTAssertEqualObjects(dict, dict2);
  2818. [dict2 release];
  2819. [dict release];
  2820. }
  2821. - (void)testDictionaryFromDictionary {
  2822. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2823. const int32_t kValues[] = { 700, 801, 702, 803 }; // Unknowns
  2824. GPBUInt64EnumDictionary *dict =
  2825. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2826. rawValues:kValues
  2827. forKeys:kKeys
  2828. count:GPBARRAYSIZE(kValues)];
  2829. XCTAssertNotNil(dict);
  2830. GPBUInt64EnumDictionary *dict2 =
  2831. [[GPBUInt64EnumDictionary alloc] initWithDictionary:dict];
  2832. XCTAssertNotNil(dict2);
  2833. // Should be new pointer, but equal objects.
  2834. XCTAssertNotEqual(dict, dict2);
  2835. XCTAssertEqualObjects(dict, dict2);
  2836. XCTAssertEqual(dict.validationFunc, dict2.validationFunc); // Pointer comparison
  2837. [dict2 release];
  2838. [dict release];
  2839. }
  2840. - (void)testUnknownAdds {
  2841. GPBUInt64EnumDictionary *dict =
  2842. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue];
  2843. XCTAssertNotNil(dict);
  2844. XCTAssertEqual(dict.count, 0U);
  2845. XCTAssertThrowsSpecificNamed([dict setEnum:801 forKey:32ULL], // Unknown
  2846. NSException, NSInvalidArgumentException);
  2847. XCTAssertEqual(dict.count, 0U);
  2848. [dict setRawValue:801 forKey:32ULL]; // Unknown
  2849. XCTAssertEqual(dict.count, 1U);
  2850. const uint64_t kKeys[] = { 31ULL, 33ULL, 34ULL };
  2851. const int32_t kValues[] = { 700, 702, 803 }; // Unknown
  2852. GPBUInt64EnumDictionary *dict2 =
  2853. [[GPBUInt64EnumDictionary alloc] initWithEnums:kValues
  2854. forKeys:kKeys
  2855. count:GPBARRAYSIZE(kValues)];
  2856. XCTAssertNotNil(dict2);
  2857. [dict addRawEntriesFromDictionary:dict2];
  2858. XCTAssertEqual(dict.count, 4U);
  2859. int32_t value;
  2860. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2861. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2862. XCTAssertEqual(value, 700);
  2863. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  2864. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  2865. XCTAssertEqual(value, kGPBUnrecognizedEnumeratorValue);
  2866. XCTAssertTrue([dict getRawValue:NULL forKey:32ULL]);
  2867. XCTAssertTrue([dict getRawValue:&value forKey:32ULL]);
  2868. XCTAssertEqual(value, 801);
  2869. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2870. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2871. XCTAssertEqual(value, 702);
  2872. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2873. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  2874. XCTAssertEqual(value, kGPBUnrecognizedEnumeratorValue);
  2875. XCTAssertTrue([dict getRawValue:NULL forKey:34ULL]);
  2876. XCTAssertTrue([dict getRawValue:&value forKey:34ULL]);
  2877. XCTAssertEqual(value, 803);
  2878. [dict2 release];
  2879. [dict release];
  2880. }
  2881. - (void)testUnknownRemove {
  2882. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2883. const int32_t kValues[] = { 700, 801, 702, 803 }; // Unknowns
  2884. GPBUInt64EnumDictionary *dict =
  2885. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2886. rawValues:kValues
  2887. forKeys:kKeys
  2888. count:GPBARRAYSIZE(kValues)];
  2889. XCTAssertNotNil(dict);
  2890. XCTAssertEqual(dict.count, 4U);
  2891. [dict removeEnumForKey:32ULL];
  2892. XCTAssertEqual(dict.count, 3U);
  2893. int32_t value;
  2894. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2895. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2896. XCTAssertEqual(value, 700);
  2897. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2898. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2899. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2900. XCTAssertEqual(value, 702);
  2901. XCTAssertTrue([dict getRawValue:NULL forKey:34ULL]);
  2902. XCTAssertTrue([dict getRawValue:&value forKey:34ULL]);
  2903. XCTAssertEqual(value, 803);
  2904. // Remove again does nothing.
  2905. [dict removeEnumForKey:32ULL];
  2906. XCTAssertEqual(dict.count, 3U);
  2907. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2908. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2909. XCTAssertEqual(value, 700);
  2910. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2911. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2912. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2913. XCTAssertEqual(value, 702);
  2914. XCTAssertTrue([dict getRawValue:NULL forKey:34ULL]);
  2915. XCTAssertTrue([dict getRawValue:&value forKey:34ULL]);
  2916. XCTAssertEqual(value, 803);
  2917. [dict removeEnumForKey:34ULL];
  2918. XCTAssertEqual(dict.count, 2U);
  2919. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2920. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2921. XCTAssertEqual(value, 700);
  2922. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2923. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2924. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2925. XCTAssertEqual(value, 702);
  2926. XCTAssertFalse([dict getEnum:NULL forKey:34ULL]);
  2927. [dict removeAll];
  2928. XCTAssertEqual(dict.count, 0U);
  2929. XCTAssertFalse([dict getEnum:NULL forKey:31ULL]);
  2930. XCTAssertFalse([dict getEnum:NULL forKey:32ULL]);
  2931. XCTAssertFalse([dict getEnum:NULL forKey:33ULL]);
  2932. XCTAssertFalse([dict getEnum:NULL forKey:34ULL]);
  2933. [dict release];
  2934. }
  2935. - (void)testInplaceMutationUnknowns {
  2936. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  2937. const int32_t kValues[] = { 700, 801, 702, 803 }; // Unknowns
  2938. GPBUInt64EnumDictionary *dict =
  2939. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  2940. rawValues:kValues
  2941. forKeys:kKeys
  2942. count:GPBARRAYSIZE(kValues)];
  2943. XCTAssertNotNil(dict);
  2944. XCTAssertEqual(dict.count, 4U);
  2945. int32_t value;
  2946. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2947. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2948. XCTAssertEqual(value, 700);
  2949. XCTAssertTrue([dict getRawValue:NULL forKey:32ULL]);
  2950. XCTAssertTrue([dict getRawValue:&value forKey:32ULL]);
  2951. XCTAssertEqual(value, 801);
  2952. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2953. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2954. XCTAssertEqual(value, 702);
  2955. XCTAssertTrue([dict getRawValue:NULL forKey:34ULL]);
  2956. XCTAssertTrue([dict getRawValue:&value forKey:34ULL]);
  2957. XCTAssertEqual(value, 803);
  2958. XCTAssertThrowsSpecificNamed([dict setEnum:803 forKey:31ULL], // Unknown
  2959. NSException, NSInvalidArgumentException);
  2960. XCTAssertEqual(dict.count, 4U);
  2961. XCTAssertTrue([dict getEnum:NULL forKey:31ULL]);
  2962. XCTAssertTrue([dict getEnum:&value forKey:31ULL]);
  2963. XCTAssertEqual(value, 700);
  2964. XCTAssertTrue([dict getRawValue:NULL forKey:32ULL]);
  2965. XCTAssertTrue([dict getRawValue:&value forKey:32ULL]);
  2966. XCTAssertEqual(value, 801);
  2967. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2968. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2969. XCTAssertEqual(value, 702);
  2970. XCTAssertTrue([dict getRawValue:NULL forKey:34ULL]);
  2971. XCTAssertTrue([dict getRawValue:&value forKey:34ULL]);
  2972. XCTAssertEqual(value, 803);
  2973. [dict setRawValue:803 forKey:31ULL]; // Unknown
  2974. XCTAssertEqual(dict.count, 4U);
  2975. XCTAssertTrue([dict getRawValue:NULL forKey:31ULL]);
  2976. XCTAssertTrue([dict getRawValue:&value forKey:31ULL]);
  2977. XCTAssertEqual(value, 803);
  2978. XCTAssertTrue([dict getRawValue:NULL forKey:32ULL]);
  2979. XCTAssertTrue([dict getRawValue:&value forKey:32ULL]);
  2980. XCTAssertEqual(value, 801);
  2981. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2982. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2983. XCTAssertEqual(value, 702);
  2984. XCTAssertTrue([dict getRawValue:NULL forKey:34ULL]);
  2985. XCTAssertTrue([dict getRawValue:&value forKey:34ULL]);
  2986. XCTAssertEqual(value, 803);
  2987. [dict setRawValue:700 forKey:34ULL];
  2988. XCTAssertEqual(dict.count, 4U);
  2989. XCTAssertTrue([dict getRawValue:NULL forKey:31ULL]);
  2990. XCTAssertTrue([dict getRawValue:&value forKey:31ULL]);
  2991. XCTAssertEqual(value, 803);
  2992. XCTAssertTrue([dict getRawValue:NULL forKey:32ULL]);
  2993. XCTAssertTrue([dict getRawValue:&value forKey:32ULL]);
  2994. XCTAssertEqual(value, 801);
  2995. XCTAssertTrue([dict getEnum:NULL forKey:33ULL]);
  2996. XCTAssertTrue([dict getEnum:&value forKey:33ULL]);
  2997. XCTAssertEqual(value, 702);
  2998. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  2999. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  3000. XCTAssertEqual(value, 700);
  3001. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  3002. const int32_t kValues2[] = { 702, 801 }; // Unknown
  3003. GPBUInt64EnumDictionary *dict2 =
  3004. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  3005. rawValues:kValues2
  3006. forKeys:kKeys2
  3007. count:GPBARRAYSIZE(kValues2)];
  3008. XCTAssertNotNil(dict2);
  3009. [dict addRawEntriesFromDictionary:dict2];
  3010. XCTAssertEqual(dict.count, 4U);
  3011. XCTAssertTrue([dict getRawValue:NULL forKey:31ULL]);
  3012. XCTAssertTrue([dict getRawValue:&value forKey:31ULL]);
  3013. XCTAssertEqual(value, 803);
  3014. XCTAssertTrue([dict getEnum:NULL forKey:32ULL]);
  3015. XCTAssertTrue([dict getEnum:&value forKey:32ULL]);
  3016. XCTAssertEqual(value, 702);
  3017. XCTAssertTrue([dict getRawValue:NULL forKey:33ULL]);
  3018. XCTAssertTrue([dict getRawValue:&value forKey:33ULL]);
  3019. XCTAssertEqual(value, 801);
  3020. XCTAssertTrue([dict getEnum:NULL forKey:34ULL]);
  3021. XCTAssertTrue([dict getEnum:&value forKey:34ULL]);
  3022. XCTAssertEqual(value, 700);
  3023. [dict2 release];
  3024. [dict release];
  3025. }
  3026. - (void)testCopyUnknowns {
  3027. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  3028. const int32_t kValues[] = { 700, 801, 702, 803 };
  3029. GPBUInt64EnumDictionary *dict =
  3030. [[GPBUInt64EnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue
  3031. rawValues:kValues
  3032. forKeys:kKeys
  3033. count:GPBARRAYSIZE(kValues)];
  3034. XCTAssertNotNil(dict);
  3035. GPBUInt64EnumDictionary *dict2 = [dict copy];
  3036. XCTAssertNotNil(dict2);
  3037. // Should be new pointer, but equal objects.
  3038. XCTAssertNotEqual(dict, dict2);
  3039. XCTAssertEqualObjects(dict, dict2);
  3040. XCTAssertEqual(dict.validationFunc, dict2.validationFunc); // Pointer comparison
  3041. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64EnumDictionary class]]);
  3042. [dict2 release];
  3043. [dict release];
  3044. }
  3045. @end
  3046. #pragma mark - UInt64 -> Object
  3047. @interface GPBUInt64ObjectDictionaryTests : XCTestCase
  3048. @end
  3049. @implementation GPBUInt64ObjectDictionaryTests
  3050. - (void)testEmpty {
  3051. GPBUInt64ObjectDictionary<NSString*> *dict = [[GPBUInt64ObjectDictionary alloc] init];
  3052. XCTAssertNotNil(dict);
  3053. XCTAssertEqual(dict.count, 0U);
  3054. XCTAssertNil([dict objectForKey:31ULL]);
  3055. [dict enumerateKeysAndObjectsUsingBlock:^(uint64_t aKey, NSString* aObject, BOOL *stop) {
  3056. #pragma unused(aKey, aObject, stop)
  3057. XCTFail(@"Shouldn't get here!");
  3058. }];
  3059. [dict release];
  3060. }
  3061. - (void)testOne {
  3062. GPBUInt64ObjectDictionary<NSString*> *dict = [[GPBUInt64ObjectDictionary alloc] init];
  3063. [dict setObject:@"abc" forKey:31ULL];
  3064. XCTAssertNotNil(dict);
  3065. XCTAssertEqual(dict.count, 1U);
  3066. XCTAssertEqualObjects([dict objectForKey:31ULL], @"abc");
  3067. XCTAssertNil([dict objectForKey:32ULL]);
  3068. [dict enumerateKeysAndObjectsUsingBlock:^(uint64_t aKey, NSString* aObject, BOOL *stop) {
  3069. XCTAssertEqual(aKey, 31ULL);
  3070. XCTAssertEqualObjects(aObject, @"abc");
  3071. XCTAssertNotEqual(stop, NULL);
  3072. }];
  3073. [dict release];
  3074. }
  3075. - (void)testBasics {
  3076. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL };
  3077. const NSString* kObjects[] = { @"abc", @"def", @"ghi" };
  3078. GPBUInt64ObjectDictionary<NSString*> *dict =
  3079. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects
  3080. forKeys:kKeys
  3081. count:GPBARRAYSIZE(kObjects)];
  3082. XCTAssertNotNil(dict);
  3083. XCTAssertEqual(dict.count, 3U);
  3084. XCTAssertEqualObjects([dict objectForKey:31ULL], @"abc");
  3085. XCTAssertEqualObjects([dict objectForKey:32ULL], @"def");
  3086. XCTAssertEqualObjects([dict objectForKey:33ULL], @"ghi");
  3087. XCTAssertNil([dict objectForKey:34ULL]);
  3088. __block NSUInteger idx = 0;
  3089. uint64_t *seenKeys = malloc(3 * sizeof(uint64_t));
  3090. NSString* *seenObjects = malloc(3 * sizeof(NSString*));
  3091. [dict enumerateKeysAndObjectsUsingBlock:^(uint64_t aKey, NSString* aObject, BOOL *stop) {
  3092. XCTAssertLessThan(idx, 3U);
  3093. seenKeys[idx] = aKey;
  3094. seenObjects[idx] = aObject;
  3095. XCTAssertNotEqual(stop, NULL);
  3096. ++idx;
  3097. }];
  3098. for (int i = 0; i < 3; ++i) {
  3099. BOOL foundKey = NO;
  3100. for (int j = 0; (j < 3) && !foundKey; ++j) {
  3101. if (kKeys[i] == seenKeys[j]) {
  3102. foundKey = YES;
  3103. XCTAssertEqualObjects(kObjects[i], seenObjects[j], @"i = %d, j = %d", i, j);
  3104. }
  3105. }
  3106. XCTAssertTrue(foundKey, @"i = %d", i);
  3107. }
  3108. free(seenKeys);
  3109. free(seenObjects);
  3110. // Stopping the enumeration.
  3111. idx = 0;
  3112. [dict enumerateKeysAndObjectsUsingBlock:^(uint64_t aKey, NSString* aObject, BOOL *stop) {
  3113. #pragma unused(aKey, aObject)
  3114. if (idx == 1) *stop = YES;
  3115. XCTAssertNotEqual(idx, 2U);
  3116. ++idx;
  3117. }];
  3118. [dict release];
  3119. }
  3120. - (void)testEquality {
  3121. const uint64_t kKeys1[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  3122. const uint64_t kKeys2[] = { 32ULL, 31ULL, 34ULL };
  3123. const NSString* kObjects1[] = { @"abc", @"def", @"ghi" };
  3124. const NSString* kObjects2[] = { @"abc", @"jkl", @"ghi" };
  3125. const NSString* kObjects3[] = { @"abc", @"def", @"ghi", @"jkl" };
  3126. GPBUInt64ObjectDictionary<NSString*> *dict1 =
  3127. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects1
  3128. forKeys:kKeys1
  3129. count:GPBARRAYSIZE(kObjects1)];
  3130. XCTAssertNotNil(dict1);
  3131. GPBUInt64ObjectDictionary<NSString*> *dict1prime =
  3132. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects1
  3133. forKeys:kKeys1
  3134. count:GPBARRAYSIZE(kObjects1)];
  3135. XCTAssertNotNil(dict1prime);
  3136. GPBUInt64ObjectDictionary<NSString*> *dict2 =
  3137. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects2
  3138. forKeys:kKeys1
  3139. count:GPBARRAYSIZE(kObjects2)];
  3140. XCTAssertNotNil(dict2);
  3141. GPBUInt64ObjectDictionary<NSString*> *dict3 =
  3142. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects1
  3143. forKeys:kKeys2
  3144. count:GPBARRAYSIZE(kObjects1)];
  3145. XCTAssertNotNil(dict3);
  3146. GPBUInt64ObjectDictionary<NSString*> *dict4 =
  3147. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects3
  3148. forKeys:kKeys1
  3149. count:GPBARRAYSIZE(kObjects3)];
  3150. XCTAssertNotNil(dict4);
  3151. // 1/1Prime should be different objects, but equal.
  3152. XCTAssertNotEqual(dict1, dict1prime);
  3153. XCTAssertEqualObjects(dict1, dict1prime);
  3154. // Equal, so they must have same hash.
  3155. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  3156. // 2 is same keys, different objects; not equal.
  3157. XCTAssertNotEqualObjects(dict1, dict2);
  3158. // 3 is different keys, same objects; not equal.
  3159. XCTAssertNotEqualObjects(dict1, dict3);
  3160. // 4 extra pair; not equal
  3161. XCTAssertNotEqualObjects(dict1, dict4);
  3162. [dict1 release];
  3163. [dict1prime release];
  3164. [dict2 release];
  3165. [dict3 release];
  3166. [dict4 release];
  3167. }
  3168. - (void)testCopy {
  3169. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  3170. const NSString* kObjects[] = { @"abc", @"def", @"ghi", @"jkl" };
  3171. GPBUInt64ObjectDictionary<NSString*> *dict =
  3172. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects
  3173. forKeys:kKeys
  3174. count:GPBARRAYSIZE(kObjects)];
  3175. XCTAssertNotNil(dict);
  3176. GPBUInt64ObjectDictionary<NSString*> *dict2 = [dict copy];
  3177. XCTAssertNotNil(dict2);
  3178. // Should be new object but equal.
  3179. XCTAssertNotEqual(dict, dict2);
  3180. XCTAssertEqualObjects(dict, dict2);
  3181. XCTAssertTrue([dict2 isKindOfClass:[GPBUInt64ObjectDictionary class]]);
  3182. [dict2 release];
  3183. [dict release];
  3184. }
  3185. - (void)testDictionaryFromDictionary {
  3186. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  3187. const NSString* kObjects[] = { @"abc", @"def", @"ghi", @"jkl" };
  3188. GPBUInt64ObjectDictionary<NSString*> *dict =
  3189. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects
  3190. forKeys:kKeys
  3191. count:GPBARRAYSIZE(kObjects)];
  3192. XCTAssertNotNil(dict);
  3193. GPBUInt64ObjectDictionary<NSString*> *dict2 =
  3194. [[GPBUInt64ObjectDictionary alloc] initWithDictionary:dict];
  3195. XCTAssertNotNil(dict2);
  3196. // Should be new pointer, but equal objects.
  3197. XCTAssertNotEqual(dict, dict2);
  3198. XCTAssertEqualObjects(dict, dict2);
  3199. [dict2 release];
  3200. [dict release];
  3201. }
  3202. - (void)testAdds {
  3203. GPBUInt64ObjectDictionary<NSString*> *dict = [[GPBUInt64ObjectDictionary alloc] init];
  3204. XCTAssertNotNil(dict);
  3205. XCTAssertEqual(dict.count, 0U);
  3206. [dict setObject:@"abc" forKey:31ULL];
  3207. XCTAssertEqual(dict.count, 1U);
  3208. const uint64_t kKeys[] = { 32ULL, 33ULL, 34ULL };
  3209. const NSString* kObjects[] = { @"def", @"ghi", @"jkl" };
  3210. GPBUInt64ObjectDictionary<NSString*> *dict2 =
  3211. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects
  3212. forKeys:kKeys
  3213. count:GPBARRAYSIZE(kObjects)];
  3214. XCTAssertNotNil(dict2);
  3215. [dict addEntriesFromDictionary:dict2];
  3216. XCTAssertEqual(dict.count, 4U);
  3217. XCTAssertEqualObjects([dict objectForKey:31ULL], @"abc");
  3218. XCTAssertEqualObjects([dict objectForKey:32ULL], @"def");
  3219. XCTAssertEqualObjects([dict objectForKey:33ULL], @"ghi");
  3220. XCTAssertEqualObjects([dict objectForKey:34ULL], @"jkl");
  3221. [dict2 release];
  3222. [dict release];
  3223. }
  3224. - (void)testRemove {
  3225. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  3226. const NSString* kObjects[] = { @"abc", @"def", @"ghi", @"jkl" };
  3227. GPBUInt64ObjectDictionary<NSString*> *dict =
  3228. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects
  3229. forKeys:kKeys
  3230. count:GPBARRAYSIZE(kObjects)];
  3231. XCTAssertNotNil(dict);
  3232. XCTAssertEqual(dict.count, 4U);
  3233. [dict removeObjectForKey:32ULL];
  3234. XCTAssertEqual(dict.count, 3U);
  3235. XCTAssertEqualObjects([dict objectForKey:31ULL], @"abc");
  3236. XCTAssertNil([dict objectForKey:32ULL]);
  3237. XCTAssertEqualObjects([dict objectForKey:33ULL], @"ghi");
  3238. XCTAssertEqualObjects([dict objectForKey:34ULL], @"jkl");
  3239. // Remove again does nothing.
  3240. [dict removeObjectForKey:32ULL];
  3241. XCTAssertEqual(dict.count, 3U);
  3242. XCTAssertEqualObjects([dict objectForKey:31ULL], @"abc");
  3243. XCTAssertNil([dict objectForKey:32ULL]);
  3244. XCTAssertEqualObjects([dict objectForKey:33ULL], @"ghi");
  3245. XCTAssertEqualObjects([dict objectForKey:34ULL], @"jkl");
  3246. [dict removeObjectForKey:34ULL];
  3247. XCTAssertEqual(dict.count, 2U);
  3248. XCTAssertEqualObjects([dict objectForKey:31ULL], @"abc");
  3249. XCTAssertNil([dict objectForKey:32ULL]);
  3250. XCTAssertEqualObjects([dict objectForKey:33ULL], @"ghi");
  3251. XCTAssertNil([dict objectForKey:34ULL]);
  3252. [dict removeAll];
  3253. XCTAssertEqual(dict.count, 0U);
  3254. XCTAssertNil([dict objectForKey:31ULL]);
  3255. XCTAssertNil([dict objectForKey:32ULL]);
  3256. XCTAssertNil([dict objectForKey:33ULL]);
  3257. XCTAssertNil([dict objectForKey:34ULL]);
  3258. [dict release];
  3259. }
  3260. - (void)testInplaceMutation {
  3261. const uint64_t kKeys[] = { 31ULL, 32ULL, 33ULL, 34ULL };
  3262. const NSString* kObjects[] = { @"abc", @"def", @"ghi", @"jkl" };
  3263. GPBUInt64ObjectDictionary<NSString*> *dict =
  3264. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects
  3265. forKeys:kKeys
  3266. count:GPBARRAYSIZE(kObjects)];
  3267. XCTAssertNotNil(dict);
  3268. XCTAssertEqual(dict.count, 4U);
  3269. XCTAssertEqualObjects([dict objectForKey:31ULL], @"abc");
  3270. XCTAssertEqualObjects([dict objectForKey:32ULL], @"def");
  3271. XCTAssertEqualObjects([dict objectForKey:33ULL], @"ghi");
  3272. XCTAssertEqualObjects([dict objectForKey:34ULL], @"jkl");
  3273. [dict setObject:@"jkl" forKey:31ULL];
  3274. XCTAssertEqual(dict.count, 4U);
  3275. XCTAssertEqualObjects([dict objectForKey:31ULL], @"jkl");
  3276. XCTAssertEqualObjects([dict objectForKey:32ULL], @"def");
  3277. XCTAssertEqualObjects([dict objectForKey:33ULL], @"ghi");
  3278. XCTAssertEqualObjects([dict objectForKey:34ULL], @"jkl");
  3279. [dict setObject:@"def" forKey:34ULL];
  3280. XCTAssertEqual(dict.count, 4U);
  3281. XCTAssertEqualObjects([dict objectForKey:31ULL], @"jkl");
  3282. XCTAssertEqualObjects([dict objectForKey:32ULL], @"def");
  3283. XCTAssertEqualObjects([dict objectForKey:33ULL], @"ghi");
  3284. XCTAssertEqualObjects([dict objectForKey:34ULL], @"def");
  3285. const uint64_t kKeys2[] = { 32ULL, 33ULL };
  3286. const NSString* kObjects2[] = { @"ghi", @"abc" };
  3287. GPBUInt64ObjectDictionary<NSString*> *dict2 =
  3288. [[GPBUInt64ObjectDictionary alloc] initWithObjects:kObjects2
  3289. forKeys:kKeys2
  3290. count:GPBARRAYSIZE(kObjects2)];
  3291. XCTAssertNotNil(dict2);
  3292. [dict addEntriesFromDictionary:dict2];
  3293. XCTAssertEqual(dict.count, 4U);
  3294. XCTAssertEqualObjects([dict objectForKey:31ULL], @"jkl");
  3295. XCTAssertEqualObjects([dict objectForKey:32ULL], @"ghi");
  3296. XCTAssertEqualObjects([dict objectForKey:33ULL], @"abc");
  3297. XCTAssertEqualObjects([dict objectForKey:34ULL], @"def");
  3298. [dict2 release];
  3299. [dict release];
  3300. }
  3301. @end
  3302. //%PDDM-EXPAND-END TEST_FOR_POD_KEY(UInt64, uint64_t, 31ULL, 32ULL, 33ULL, 34ULL)