GPBDictionaryTests+UInt64.m 136 KB

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