GPBDictionaryTests+UInt64.m 136 KB

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