GPBDictionaryTests+UInt32.m 134 KB

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