GPBDictionaryTests+String.m 127 KB

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