GPBDictionaryTests+Int32.m 132 KB

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