GPBDictionaryTests+String.m 127 KB

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