GPBDictionaryTests+Int64.m 135 KB

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