GPBDictionaryTests+String.m 127 KB

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