GPBDictionaryTests+Bool.m 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import <Foundation/Foundation.h>
  31. #import <XCTest/XCTest.h>
  32. #import "GPBDictionary.h"
  33. #import "google/protobuf/UnittestRuntimeProto2.pbobjc.h"
  34. #ifndef GPBARRAYSIZE
  35. #define GPBARRAYSIZE(a) ((sizeof(a) / sizeof((a[0]))))
  36. #endif // GPBARRAYSIZE
  37. // Pull in the macros (using an external file because expanding all tests
  38. // in a single file makes a file that is failing to work with within Xcode.
  39. //%PDDM-IMPORT-DEFINES GPBDictionaryTests.pddm
  40. //%PDDM-EXPAND BOOL_TESTS_FOR_POD_VALUE(UInt32, uint32_t, 100U, 101U)
  41. // This block of code is generated, do not edit it directly.
  42. #pragma mark - Bool -> UInt32
  43. @interface GPBBoolUInt32DictionaryTests : XCTestCase
  44. @end
  45. @implementation GPBBoolUInt32DictionaryTests
  46. - (void)testEmpty {
  47. GPBBoolUInt32Dictionary *dict = [[GPBBoolUInt32Dictionary alloc] init];
  48. XCTAssertNotNil(dict);
  49. XCTAssertEqual(dict.count, 0U);
  50. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  51. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, uint32_t aValue, BOOL *stop) {
  52. #pragma unused(aKey, aValue, stop)
  53. XCTFail(@"Shouldn't get here!");
  54. }];
  55. [dict release];
  56. }
  57. - (void)testOne {
  58. GPBBoolUInt32Dictionary *dict = [GPBBoolUInt32Dictionary dictionaryWithValue:100U forKey:YES];
  59. XCTAssertNotNil(dict);
  60. XCTAssertEqual(dict.count, 1U);
  61. uint32_t value;
  62. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  63. XCTAssertTrue([dict valueForKey:YES value:&value]);
  64. XCTAssertEqual(value, 100U);
  65. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  66. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, uint32_t aValue, BOOL *stop) {
  67. XCTAssertEqual(aKey, YES);
  68. XCTAssertEqual(aValue, 100U);
  69. XCTAssertNotEqual(stop, NULL);
  70. }];
  71. }
  72. - (void)testBasics {
  73. const BOOL kKeys[] = { YES, NO };
  74. const uint32_t kValues[] = { 100U, 101U };
  75. GPBBoolUInt32Dictionary *dict =
  76. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues
  77. forKeys:kKeys
  78. count:GPBARRAYSIZE(kValues)];
  79. XCTAssertNotNil(dict);
  80. XCTAssertEqual(dict.count, 2U);
  81. uint32_t value;
  82. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  83. XCTAssertTrue([dict valueForKey:YES value:&value]);
  84. XCTAssertEqual(value, 100U);
  85. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  86. XCTAssertTrue([dict valueForKey:NO value:&value]);
  87. XCTAssertEqual(value, 101U);
  88. __block NSUInteger idx = 0;
  89. BOOL *seenKeys = malloc(2 * sizeof(BOOL));
  90. uint32_t *seenValues = malloc(2 * sizeof(uint32_t));
  91. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, uint32_t aValue, BOOL *stop) {
  92. XCTAssertLessThan(idx, 2U);
  93. seenKeys[idx] = aKey;
  94. seenValues[idx] = aValue;
  95. XCTAssertNotEqual(stop, NULL);
  96. ++idx;
  97. }];
  98. for (int i = 0; i < 2; ++i) {
  99. BOOL foundKey = NO;
  100. for (int j = 0; (j < 2) && !foundKey; ++j) {
  101. if (kKeys[i] == seenKeys[j]) {
  102. foundKey = YES;
  103. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  104. }
  105. }
  106. XCTAssertTrue(foundKey, @"i = %d", i);
  107. }
  108. free(seenKeys);
  109. free(seenValues);
  110. // Stopping the enumeration.
  111. idx = 0;
  112. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, uint32_t aValue, BOOL *stop) {
  113. #pragma unused(aKey, aValue)
  114. if (idx == 0) *stop = YES;
  115. XCTAssertNotEqual(idx, 2U);
  116. ++idx;
  117. }];
  118. [dict release];
  119. }
  120. - (void)testEquality {
  121. const BOOL kKeys1[] = { YES, NO };
  122. const BOOL kKeys2[] = { NO, YES };
  123. const uint32_t kValues1[] = { 100U, 101U };
  124. const uint32_t kValues2[] = { 101U, 100U };
  125. const uint32_t kValues3[] = { 101U };
  126. GPBBoolUInt32Dictionary *dict1 =
  127. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues1
  128. forKeys:kKeys1
  129. count:GPBARRAYSIZE(kValues1)];
  130. XCTAssertNotNil(dict1);
  131. GPBBoolUInt32Dictionary *dict1prime =
  132. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues1
  133. forKeys:kKeys1
  134. count:GPBARRAYSIZE(kValues1)];
  135. XCTAssertNotNil(dict1prime);
  136. GPBBoolUInt32Dictionary *dict2 =
  137. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues2
  138. forKeys:kKeys1
  139. count:GPBARRAYSIZE(kValues2)];
  140. XCTAssertNotNil(dict2);
  141. GPBBoolUInt32Dictionary *dict3 =
  142. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues1
  143. forKeys:kKeys2
  144. count:GPBARRAYSIZE(kValues1)];
  145. XCTAssertNotNil(dict3);
  146. GPBBoolUInt32Dictionary *dict4 =
  147. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues3
  148. forKeys:kKeys1
  149. count:GPBARRAYSIZE(kValues3)];
  150. XCTAssertNotNil(dict4);
  151. // 1/1Prime should be different objects, but equal.
  152. XCTAssertNotEqual(dict1, dict1prime);
  153. XCTAssertEqualObjects(dict1, dict1prime);
  154. // Equal, so they must have same hash.
  155. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  156. // 2 is save keys, different values; not equal.
  157. XCTAssertNotEqualObjects(dict1, dict2);
  158. // 3 is different keys, samae values; not equal.
  159. XCTAssertNotEqualObjects(dict1, dict3);
  160. // 4 Fewer pairs; not equal
  161. XCTAssertNotEqualObjects(dict1, dict4);
  162. [dict1 release];
  163. [dict1prime release];
  164. [dict2 release];
  165. [dict3 release];
  166. [dict4 release];
  167. }
  168. - (void)testCopy {
  169. const BOOL kKeys[] = { YES, NO };
  170. const uint32_t kValues[] = { 100U, 101U };
  171. GPBBoolUInt32Dictionary *dict =
  172. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues
  173. forKeys:kKeys
  174. count:GPBARRAYSIZE(kValues)];
  175. XCTAssertNotNil(dict);
  176. GPBBoolUInt32Dictionary *dict2 = [dict copy];
  177. XCTAssertNotNil(dict2);
  178. // Should be new object but equal.
  179. XCTAssertNotEqual(dict, dict2);
  180. XCTAssertEqualObjects(dict, dict2);
  181. XCTAssertTrue([dict2 isKindOfClass:[GPBBoolUInt32Dictionary class]]);
  182. [dict2 release];
  183. [dict release];
  184. }
  185. - (void)testDictionaryFromDictionary {
  186. const BOOL kKeys[] = { YES, NO };
  187. const uint32_t kValues[] = { 100U, 101U };
  188. GPBBoolUInt32Dictionary *dict =
  189. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues
  190. forKeys:kKeys
  191. count:GPBARRAYSIZE(kValues)];
  192. XCTAssertNotNil(dict);
  193. GPBBoolUInt32Dictionary *dict2 =
  194. [GPBBoolUInt32Dictionary dictionaryWithDictionary:dict];
  195. XCTAssertNotNil(dict2);
  196. // Should be new pointer, but equal objects.
  197. XCTAssertNotEqual(dict, dict2);
  198. XCTAssertEqualObjects(dict, dict2);
  199. [dict release];
  200. }
  201. - (void)testAdds {
  202. GPBBoolUInt32Dictionary *dict = [GPBBoolUInt32Dictionary dictionary];
  203. XCTAssertNotNil(dict);
  204. XCTAssertEqual(dict.count, 0U);
  205. [dict setValue:100U forKey:YES];
  206. XCTAssertEqual(dict.count, 1U);
  207. const BOOL kKeys[] = { NO };
  208. const uint32_t kValues[] = { 101U };
  209. GPBBoolUInt32Dictionary *dict2 =
  210. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues
  211. forKeys:kKeys
  212. count:GPBARRAYSIZE(kValues)];
  213. XCTAssertNotNil(dict2);
  214. [dict addEntriesFromDictionary:dict2];
  215. XCTAssertEqual(dict.count, 2U);
  216. uint32_t value;
  217. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  218. XCTAssertTrue([dict valueForKey:YES value:&value]);
  219. XCTAssertEqual(value, 100U);
  220. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  221. XCTAssertTrue([dict valueForKey:NO value:&value]);
  222. XCTAssertEqual(value, 101U);
  223. [dict2 release];
  224. }
  225. - (void)testRemove {
  226. const BOOL kKeys[] = { YES, NO};
  227. const uint32_t kValues[] = { 100U, 101U };
  228. GPBBoolUInt32Dictionary *dict =
  229. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues
  230. forKeys:kKeys
  231. count:GPBARRAYSIZE(kValues)];
  232. XCTAssertNotNil(dict);
  233. XCTAssertEqual(dict.count, 2U);
  234. [dict removeValueForKey:NO];
  235. XCTAssertEqual(dict.count, 1U);
  236. uint32_t value;
  237. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  238. XCTAssertTrue([dict valueForKey:YES value:&value]);
  239. XCTAssertEqual(value, 100U);
  240. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  241. // Remove again does nothing.
  242. [dict removeValueForKey:NO];
  243. XCTAssertEqual(dict.count, 1U);
  244. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  245. XCTAssertTrue([dict valueForKey:YES value:&value]);
  246. XCTAssertEqual(value, 100U);
  247. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  248. [dict removeAll];
  249. XCTAssertEqual(dict.count, 0U);
  250. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  251. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  252. [dict release];
  253. }
  254. - (void)testInplaceMutation {
  255. const BOOL kKeys[] = { YES, NO };
  256. const uint32_t kValues[] = { 100U, 101U };
  257. GPBBoolUInt32Dictionary *dict =
  258. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues
  259. forKeys:kKeys
  260. count:GPBARRAYSIZE(kValues)];
  261. XCTAssertNotNil(dict);
  262. XCTAssertEqual(dict.count, 2U);
  263. uint32_t value;
  264. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  265. XCTAssertTrue([dict valueForKey:YES value:&value]);
  266. XCTAssertEqual(value, 100U);
  267. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  268. XCTAssertTrue([dict valueForKey:NO value:&value]);
  269. XCTAssertEqual(value, 101U);
  270. [dict setValue:101U forKey:YES];
  271. XCTAssertEqual(dict.count, 2U);
  272. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  273. XCTAssertTrue([dict valueForKey:YES value:&value]);
  274. XCTAssertEqual(value, 101U);
  275. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  276. XCTAssertTrue([dict valueForKey:NO value:&value]);
  277. XCTAssertEqual(value, 101U);
  278. [dict setValue:100U forKey:NO];
  279. XCTAssertEqual(dict.count, 2U);
  280. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  281. XCTAssertTrue([dict valueForKey:YES value:&value]);
  282. XCTAssertEqual(value, 101U);
  283. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  284. XCTAssertTrue([dict valueForKey:NO value:&value]);
  285. XCTAssertEqual(value, 100U);
  286. const BOOL kKeys2[] = { NO, YES };
  287. const uint32_t kValues2[] = { 101U, 100U };
  288. GPBBoolUInt32Dictionary *dict2 =
  289. [[GPBBoolUInt32Dictionary alloc] initWithValues:kValues2
  290. forKeys:kKeys2
  291. count:GPBARRAYSIZE(kValues2)];
  292. XCTAssertNotNil(dict2);
  293. [dict addEntriesFromDictionary:dict2];
  294. XCTAssertEqual(dict.count, 2U);
  295. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  296. XCTAssertTrue([dict valueForKey:YES value:&value]);
  297. XCTAssertEqual(value, 100U);
  298. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  299. XCTAssertTrue([dict valueForKey:NO value:&value]);
  300. XCTAssertEqual(value, 101U);
  301. [dict2 release];
  302. [dict release];
  303. }
  304. @end
  305. //%PDDM-EXPAND BOOL_TESTS_FOR_POD_VALUE(Int32, int32_t, 200, 201)
  306. // This block of code is generated, do not edit it directly.
  307. #pragma mark - Bool -> Int32
  308. @interface GPBBoolInt32DictionaryTests : XCTestCase
  309. @end
  310. @implementation GPBBoolInt32DictionaryTests
  311. - (void)testEmpty {
  312. GPBBoolInt32Dictionary *dict = [[GPBBoolInt32Dictionary alloc] init];
  313. XCTAssertNotNil(dict);
  314. XCTAssertEqual(dict.count, 0U);
  315. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  316. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, int32_t aValue, BOOL *stop) {
  317. #pragma unused(aKey, aValue, stop)
  318. XCTFail(@"Shouldn't get here!");
  319. }];
  320. [dict release];
  321. }
  322. - (void)testOne {
  323. GPBBoolInt32Dictionary *dict = [GPBBoolInt32Dictionary dictionaryWithValue:200 forKey:YES];
  324. XCTAssertNotNil(dict);
  325. XCTAssertEqual(dict.count, 1U);
  326. int32_t value;
  327. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  328. XCTAssertTrue([dict valueForKey:YES value:&value]);
  329. XCTAssertEqual(value, 200);
  330. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  331. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, int32_t aValue, BOOL *stop) {
  332. XCTAssertEqual(aKey, YES);
  333. XCTAssertEqual(aValue, 200);
  334. XCTAssertNotEqual(stop, NULL);
  335. }];
  336. }
  337. - (void)testBasics {
  338. const BOOL kKeys[] = { YES, NO };
  339. const int32_t kValues[] = { 200, 201 };
  340. GPBBoolInt32Dictionary *dict =
  341. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues
  342. forKeys:kKeys
  343. count:GPBARRAYSIZE(kValues)];
  344. XCTAssertNotNil(dict);
  345. XCTAssertEqual(dict.count, 2U);
  346. int32_t value;
  347. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  348. XCTAssertTrue([dict valueForKey:YES value:&value]);
  349. XCTAssertEqual(value, 200);
  350. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  351. XCTAssertTrue([dict valueForKey:NO value:&value]);
  352. XCTAssertEqual(value, 201);
  353. __block NSUInteger idx = 0;
  354. BOOL *seenKeys = malloc(2 * sizeof(BOOL));
  355. int32_t *seenValues = malloc(2 * sizeof(int32_t));
  356. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, int32_t aValue, BOOL *stop) {
  357. XCTAssertLessThan(idx, 2U);
  358. seenKeys[idx] = aKey;
  359. seenValues[idx] = aValue;
  360. XCTAssertNotEqual(stop, NULL);
  361. ++idx;
  362. }];
  363. for (int i = 0; i < 2; ++i) {
  364. BOOL foundKey = NO;
  365. for (int j = 0; (j < 2) && !foundKey; ++j) {
  366. if (kKeys[i] == seenKeys[j]) {
  367. foundKey = YES;
  368. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  369. }
  370. }
  371. XCTAssertTrue(foundKey, @"i = %d", i);
  372. }
  373. free(seenKeys);
  374. free(seenValues);
  375. // Stopping the enumeration.
  376. idx = 0;
  377. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, int32_t aValue, BOOL *stop) {
  378. #pragma unused(aKey, aValue)
  379. if (idx == 0) *stop = YES;
  380. XCTAssertNotEqual(idx, 2U);
  381. ++idx;
  382. }];
  383. [dict release];
  384. }
  385. - (void)testEquality {
  386. const BOOL kKeys1[] = { YES, NO };
  387. const BOOL kKeys2[] = { NO, YES };
  388. const int32_t kValues1[] = { 200, 201 };
  389. const int32_t kValues2[] = { 201, 200 };
  390. const int32_t kValues3[] = { 201 };
  391. GPBBoolInt32Dictionary *dict1 =
  392. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues1
  393. forKeys:kKeys1
  394. count:GPBARRAYSIZE(kValues1)];
  395. XCTAssertNotNil(dict1);
  396. GPBBoolInt32Dictionary *dict1prime =
  397. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues1
  398. forKeys:kKeys1
  399. count:GPBARRAYSIZE(kValues1)];
  400. XCTAssertNotNil(dict1prime);
  401. GPBBoolInt32Dictionary *dict2 =
  402. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues2
  403. forKeys:kKeys1
  404. count:GPBARRAYSIZE(kValues2)];
  405. XCTAssertNotNil(dict2);
  406. GPBBoolInt32Dictionary *dict3 =
  407. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues1
  408. forKeys:kKeys2
  409. count:GPBARRAYSIZE(kValues1)];
  410. XCTAssertNotNil(dict3);
  411. GPBBoolInt32Dictionary *dict4 =
  412. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues3
  413. forKeys:kKeys1
  414. count:GPBARRAYSIZE(kValues3)];
  415. XCTAssertNotNil(dict4);
  416. // 1/1Prime should be different objects, but equal.
  417. XCTAssertNotEqual(dict1, dict1prime);
  418. XCTAssertEqualObjects(dict1, dict1prime);
  419. // Equal, so they must have same hash.
  420. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  421. // 2 is save keys, different values; not equal.
  422. XCTAssertNotEqualObjects(dict1, dict2);
  423. // 3 is different keys, samae values; not equal.
  424. XCTAssertNotEqualObjects(dict1, dict3);
  425. // 4 Fewer pairs; not equal
  426. XCTAssertNotEqualObjects(dict1, dict4);
  427. [dict1 release];
  428. [dict1prime release];
  429. [dict2 release];
  430. [dict3 release];
  431. [dict4 release];
  432. }
  433. - (void)testCopy {
  434. const BOOL kKeys[] = { YES, NO };
  435. const int32_t kValues[] = { 200, 201 };
  436. GPBBoolInt32Dictionary *dict =
  437. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues
  438. forKeys:kKeys
  439. count:GPBARRAYSIZE(kValues)];
  440. XCTAssertNotNil(dict);
  441. GPBBoolInt32Dictionary *dict2 = [dict copy];
  442. XCTAssertNotNil(dict2);
  443. // Should be new object but equal.
  444. XCTAssertNotEqual(dict, dict2);
  445. XCTAssertEqualObjects(dict, dict2);
  446. XCTAssertTrue([dict2 isKindOfClass:[GPBBoolInt32Dictionary class]]);
  447. [dict2 release];
  448. [dict release];
  449. }
  450. - (void)testDictionaryFromDictionary {
  451. const BOOL kKeys[] = { YES, NO };
  452. const int32_t kValues[] = { 200, 201 };
  453. GPBBoolInt32Dictionary *dict =
  454. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues
  455. forKeys:kKeys
  456. count:GPBARRAYSIZE(kValues)];
  457. XCTAssertNotNil(dict);
  458. GPBBoolInt32Dictionary *dict2 =
  459. [GPBBoolInt32Dictionary dictionaryWithDictionary:dict];
  460. XCTAssertNotNil(dict2);
  461. // Should be new pointer, but equal objects.
  462. XCTAssertNotEqual(dict, dict2);
  463. XCTAssertEqualObjects(dict, dict2);
  464. [dict release];
  465. }
  466. - (void)testAdds {
  467. GPBBoolInt32Dictionary *dict = [GPBBoolInt32Dictionary dictionary];
  468. XCTAssertNotNil(dict);
  469. XCTAssertEqual(dict.count, 0U);
  470. [dict setValue:200 forKey:YES];
  471. XCTAssertEqual(dict.count, 1U);
  472. const BOOL kKeys[] = { NO };
  473. const int32_t kValues[] = { 201 };
  474. GPBBoolInt32Dictionary *dict2 =
  475. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues
  476. forKeys:kKeys
  477. count:GPBARRAYSIZE(kValues)];
  478. XCTAssertNotNil(dict2);
  479. [dict addEntriesFromDictionary:dict2];
  480. XCTAssertEqual(dict.count, 2U);
  481. int32_t value;
  482. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  483. XCTAssertTrue([dict valueForKey:YES value:&value]);
  484. XCTAssertEqual(value, 200);
  485. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  486. XCTAssertTrue([dict valueForKey:NO value:&value]);
  487. XCTAssertEqual(value, 201);
  488. [dict2 release];
  489. }
  490. - (void)testRemove {
  491. const BOOL kKeys[] = { YES, NO};
  492. const int32_t kValues[] = { 200, 201 };
  493. GPBBoolInt32Dictionary *dict =
  494. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues
  495. forKeys:kKeys
  496. count:GPBARRAYSIZE(kValues)];
  497. XCTAssertNotNil(dict);
  498. XCTAssertEqual(dict.count, 2U);
  499. [dict removeValueForKey:NO];
  500. XCTAssertEqual(dict.count, 1U);
  501. int32_t value;
  502. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  503. XCTAssertTrue([dict valueForKey:YES value:&value]);
  504. XCTAssertEqual(value, 200);
  505. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  506. // Remove again does nothing.
  507. [dict removeValueForKey:NO];
  508. XCTAssertEqual(dict.count, 1U);
  509. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  510. XCTAssertTrue([dict valueForKey:YES value:&value]);
  511. XCTAssertEqual(value, 200);
  512. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  513. [dict removeAll];
  514. XCTAssertEqual(dict.count, 0U);
  515. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  516. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  517. [dict release];
  518. }
  519. - (void)testInplaceMutation {
  520. const BOOL kKeys[] = { YES, NO };
  521. const int32_t kValues[] = { 200, 201 };
  522. GPBBoolInt32Dictionary *dict =
  523. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues
  524. forKeys:kKeys
  525. count:GPBARRAYSIZE(kValues)];
  526. XCTAssertNotNil(dict);
  527. XCTAssertEqual(dict.count, 2U);
  528. int32_t value;
  529. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  530. XCTAssertTrue([dict valueForKey:YES value:&value]);
  531. XCTAssertEqual(value, 200);
  532. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  533. XCTAssertTrue([dict valueForKey:NO value:&value]);
  534. XCTAssertEqual(value, 201);
  535. [dict setValue:201 forKey:YES];
  536. XCTAssertEqual(dict.count, 2U);
  537. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  538. XCTAssertTrue([dict valueForKey:YES value:&value]);
  539. XCTAssertEqual(value, 201);
  540. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  541. XCTAssertTrue([dict valueForKey:NO value:&value]);
  542. XCTAssertEqual(value, 201);
  543. [dict setValue:200 forKey:NO];
  544. XCTAssertEqual(dict.count, 2U);
  545. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  546. XCTAssertTrue([dict valueForKey:YES value:&value]);
  547. XCTAssertEqual(value, 201);
  548. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  549. XCTAssertTrue([dict valueForKey:NO value:&value]);
  550. XCTAssertEqual(value, 200);
  551. const BOOL kKeys2[] = { NO, YES };
  552. const int32_t kValues2[] = { 201, 200 };
  553. GPBBoolInt32Dictionary *dict2 =
  554. [[GPBBoolInt32Dictionary alloc] initWithValues:kValues2
  555. forKeys:kKeys2
  556. count:GPBARRAYSIZE(kValues2)];
  557. XCTAssertNotNil(dict2);
  558. [dict addEntriesFromDictionary:dict2];
  559. XCTAssertEqual(dict.count, 2U);
  560. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  561. XCTAssertTrue([dict valueForKey:YES value:&value]);
  562. XCTAssertEqual(value, 200);
  563. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  564. XCTAssertTrue([dict valueForKey:NO value:&value]);
  565. XCTAssertEqual(value, 201);
  566. [dict2 release];
  567. [dict release];
  568. }
  569. @end
  570. //%PDDM-EXPAND BOOL_TESTS_FOR_POD_VALUE(UInt64, uint64_t, 300U, 301U)
  571. // This block of code is generated, do not edit it directly.
  572. #pragma mark - Bool -> UInt64
  573. @interface GPBBoolUInt64DictionaryTests : XCTestCase
  574. @end
  575. @implementation GPBBoolUInt64DictionaryTests
  576. - (void)testEmpty {
  577. GPBBoolUInt64Dictionary *dict = [[GPBBoolUInt64Dictionary alloc] init];
  578. XCTAssertNotNil(dict);
  579. XCTAssertEqual(dict.count, 0U);
  580. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  581. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, uint64_t aValue, BOOL *stop) {
  582. #pragma unused(aKey, aValue, stop)
  583. XCTFail(@"Shouldn't get here!");
  584. }];
  585. [dict release];
  586. }
  587. - (void)testOne {
  588. GPBBoolUInt64Dictionary *dict = [GPBBoolUInt64Dictionary dictionaryWithValue:300U forKey:YES];
  589. XCTAssertNotNil(dict);
  590. XCTAssertEqual(dict.count, 1U);
  591. uint64_t value;
  592. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  593. XCTAssertTrue([dict valueForKey:YES value:&value]);
  594. XCTAssertEqual(value, 300U);
  595. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  596. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, uint64_t aValue, BOOL *stop) {
  597. XCTAssertEqual(aKey, YES);
  598. XCTAssertEqual(aValue, 300U);
  599. XCTAssertNotEqual(stop, NULL);
  600. }];
  601. }
  602. - (void)testBasics {
  603. const BOOL kKeys[] = { YES, NO };
  604. const uint64_t kValues[] = { 300U, 301U };
  605. GPBBoolUInt64Dictionary *dict =
  606. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues
  607. forKeys:kKeys
  608. count:GPBARRAYSIZE(kValues)];
  609. XCTAssertNotNil(dict);
  610. XCTAssertEqual(dict.count, 2U);
  611. uint64_t value;
  612. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  613. XCTAssertTrue([dict valueForKey:YES value:&value]);
  614. XCTAssertEqual(value, 300U);
  615. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  616. XCTAssertTrue([dict valueForKey:NO value:&value]);
  617. XCTAssertEqual(value, 301U);
  618. __block NSUInteger idx = 0;
  619. BOOL *seenKeys = malloc(2 * sizeof(BOOL));
  620. uint64_t *seenValues = malloc(2 * sizeof(uint64_t));
  621. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, uint64_t aValue, BOOL *stop) {
  622. XCTAssertLessThan(idx, 2U);
  623. seenKeys[idx] = aKey;
  624. seenValues[idx] = aValue;
  625. XCTAssertNotEqual(stop, NULL);
  626. ++idx;
  627. }];
  628. for (int i = 0; i < 2; ++i) {
  629. BOOL foundKey = NO;
  630. for (int j = 0; (j < 2) && !foundKey; ++j) {
  631. if (kKeys[i] == seenKeys[j]) {
  632. foundKey = YES;
  633. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  634. }
  635. }
  636. XCTAssertTrue(foundKey, @"i = %d", i);
  637. }
  638. free(seenKeys);
  639. free(seenValues);
  640. // Stopping the enumeration.
  641. idx = 0;
  642. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, uint64_t aValue, BOOL *stop) {
  643. #pragma unused(aKey, aValue)
  644. if (idx == 0) *stop = YES;
  645. XCTAssertNotEqual(idx, 2U);
  646. ++idx;
  647. }];
  648. [dict release];
  649. }
  650. - (void)testEquality {
  651. const BOOL kKeys1[] = { YES, NO };
  652. const BOOL kKeys2[] = { NO, YES };
  653. const uint64_t kValues1[] = { 300U, 301U };
  654. const uint64_t kValues2[] = { 301U, 300U };
  655. const uint64_t kValues3[] = { 301U };
  656. GPBBoolUInt64Dictionary *dict1 =
  657. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues1
  658. forKeys:kKeys1
  659. count:GPBARRAYSIZE(kValues1)];
  660. XCTAssertNotNil(dict1);
  661. GPBBoolUInt64Dictionary *dict1prime =
  662. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues1
  663. forKeys:kKeys1
  664. count:GPBARRAYSIZE(kValues1)];
  665. XCTAssertNotNil(dict1prime);
  666. GPBBoolUInt64Dictionary *dict2 =
  667. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues2
  668. forKeys:kKeys1
  669. count:GPBARRAYSIZE(kValues2)];
  670. XCTAssertNotNil(dict2);
  671. GPBBoolUInt64Dictionary *dict3 =
  672. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues1
  673. forKeys:kKeys2
  674. count:GPBARRAYSIZE(kValues1)];
  675. XCTAssertNotNil(dict3);
  676. GPBBoolUInt64Dictionary *dict4 =
  677. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues3
  678. forKeys:kKeys1
  679. count:GPBARRAYSIZE(kValues3)];
  680. XCTAssertNotNil(dict4);
  681. // 1/1Prime should be different objects, but equal.
  682. XCTAssertNotEqual(dict1, dict1prime);
  683. XCTAssertEqualObjects(dict1, dict1prime);
  684. // Equal, so they must have same hash.
  685. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  686. // 2 is save keys, different values; not equal.
  687. XCTAssertNotEqualObjects(dict1, dict2);
  688. // 3 is different keys, samae values; not equal.
  689. XCTAssertNotEqualObjects(dict1, dict3);
  690. // 4 Fewer pairs; not equal
  691. XCTAssertNotEqualObjects(dict1, dict4);
  692. [dict1 release];
  693. [dict1prime release];
  694. [dict2 release];
  695. [dict3 release];
  696. [dict4 release];
  697. }
  698. - (void)testCopy {
  699. const BOOL kKeys[] = { YES, NO };
  700. const uint64_t kValues[] = { 300U, 301U };
  701. GPBBoolUInt64Dictionary *dict =
  702. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues
  703. forKeys:kKeys
  704. count:GPBARRAYSIZE(kValues)];
  705. XCTAssertNotNil(dict);
  706. GPBBoolUInt64Dictionary *dict2 = [dict copy];
  707. XCTAssertNotNil(dict2);
  708. // Should be new object but equal.
  709. XCTAssertNotEqual(dict, dict2);
  710. XCTAssertEqualObjects(dict, dict2);
  711. XCTAssertTrue([dict2 isKindOfClass:[GPBBoolUInt64Dictionary class]]);
  712. [dict2 release];
  713. [dict release];
  714. }
  715. - (void)testDictionaryFromDictionary {
  716. const BOOL kKeys[] = { YES, NO };
  717. const uint64_t kValues[] = { 300U, 301U };
  718. GPBBoolUInt64Dictionary *dict =
  719. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues
  720. forKeys:kKeys
  721. count:GPBARRAYSIZE(kValues)];
  722. XCTAssertNotNil(dict);
  723. GPBBoolUInt64Dictionary *dict2 =
  724. [GPBBoolUInt64Dictionary dictionaryWithDictionary:dict];
  725. XCTAssertNotNil(dict2);
  726. // Should be new pointer, but equal objects.
  727. XCTAssertNotEqual(dict, dict2);
  728. XCTAssertEqualObjects(dict, dict2);
  729. [dict release];
  730. }
  731. - (void)testAdds {
  732. GPBBoolUInt64Dictionary *dict = [GPBBoolUInt64Dictionary dictionary];
  733. XCTAssertNotNil(dict);
  734. XCTAssertEqual(dict.count, 0U);
  735. [dict setValue:300U forKey:YES];
  736. XCTAssertEqual(dict.count, 1U);
  737. const BOOL kKeys[] = { NO };
  738. const uint64_t kValues[] = { 301U };
  739. GPBBoolUInt64Dictionary *dict2 =
  740. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues
  741. forKeys:kKeys
  742. count:GPBARRAYSIZE(kValues)];
  743. XCTAssertNotNil(dict2);
  744. [dict addEntriesFromDictionary:dict2];
  745. XCTAssertEqual(dict.count, 2U);
  746. uint64_t value;
  747. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  748. XCTAssertTrue([dict valueForKey:YES value:&value]);
  749. XCTAssertEqual(value, 300U);
  750. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  751. XCTAssertTrue([dict valueForKey:NO value:&value]);
  752. XCTAssertEqual(value, 301U);
  753. [dict2 release];
  754. }
  755. - (void)testRemove {
  756. const BOOL kKeys[] = { YES, NO};
  757. const uint64_t kValues[] = { 300U, 301U };
  758. GPBBoolUInt64Dictionary *dict =
  759. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues
  760. forKeys:kKeys
  761. count:GPBARRAYSIZE(kValues)];
  762. XCTAssertNotNil(dict);
  763. XCTAssertEqual(dict.count, 2U);
  764. [dict removeValueForKey:NO];
  765. XCTAssertEqual(dict.count, 1U);
  766. uint64_t value;
  767. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  768. XCTAssertTrue([dict valueForKey:YES value:&value]);
  769. XCTAssertEqual(value, 300U);
  770. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  771. // Remove again does nothing.
  772. [dict removeValueForKey:NO];
  773. XCTAssertEqual(dict.count, 1U);
  774. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  775. XCTAssertTrue([dict valueForKey:YES value:&value]);
  776. XCTAssertEqual(value, 300U);
  777. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  778. [dict removeAll];
  779. XCTAssertEqual(dict.count, 0U);
  780. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  781. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  782. [dict release];
  783. }
  784. - (void)testInplaceMutation {
  785. const BOOL kKeys[] = { YES, NO };
  786. const uint64_t kValues[] = { 300U, 301U };
  787. GPBBoolUInt64Dictionary *dict =
  788. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues
  789. forKeys:kKeys
  790. count:GPBARRAYSIZE(kValues)];
  791. XCTAssertNotNil(dict);
  792. XCTAssertEqual(dict.count, 2U);
  793. uint64_t value;
  794. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  795. XCTAssertTrue([dict valueForKey:YES value:&value]);
  796. XCTAssertEqual(value, 300U);
  797. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  798. XCTAssertTrue([dict valueForKey:NO value:&value]);
  799. XCTAssertEqual(value, 301U);
  800. [dict setValue:301U forKey:YES];
  801. XCTAssertEqual(dict.count, 2U);
  802. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  803. XCTAssertTrue([dict valueForKey:YES value:&value]);
  804. XCTAssertEqual(value, 301U);
  805. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  806. XCTAssertTrue([dict valueForKey:NO value:&value]);
  807. XCTAssertEqual(value, 301U);
  808. [dict setValue:300U forKey:NO];
  809. XCTAssertEqual(dict.count, 2U);
  810. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  811. XCTAssertTrue([dict valueForKey:YES value:&value]);
  812. XCTAssertEqual(value, 301U);
  813. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  814. XCTAssertTrue([dict valueForKey:NO value:&value]);
  815. XCTAssertEqual(value, 300U);
  816. const BOOL kKeys2[] = { NO, YES };
  817. const uint64_t kValues2[] = { 301U, 300U };
  818. GPBBoolUInt64Dictionary *dict2 =
  819. [[GPBBoolUInt64Dictionary alloc] initWithValues:kValues2
  820. forKeys:kKeys2
  821. count:GPBARRAYSIZE(kValues2)];
  822. XCTAssertNotNil(dict2);
  823. [dict addEntriesFromDictionary:dict2];
  824. XCTAssertEqual(dict.count, 2U);
  825. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  826. XCTAssertTrue([dict valueForKey:YES value:&value]);
  827. XCTAssertEqual(value, 300U);
  828. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  829. XCTAssertTrue([dict valueForKey:NO value:&value]);
  830. XCTAssertEqual(value, 301U);
  831. [dict2 release];
  832. [dict release];
  833. }
  834. @end
  835. //%PDDM-EXPAND BOOL_TESTS_FOR_POD_VALUE(Int64, int64_t, 400, 401)
  836. // This block of code is generated, do not edit it directly.
  837. #pragma mark - Bool -> Int64
  838. @interface GPBBoolInt64DictionaryTests : XCTestCase
  839. @end
  840. @implementation GPBBoolInt64DictionaryTests
  841. - (void)testEmpty {
  842. GPBBoolInt64Dictionary *dict = [[GPBBoolInt64Dictionary alloc] init];
  843. XCTAssertNotNil(dict);
  844. XCTAssertEqual(dict.count, 0U);
  845. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  846. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, int64_t aValue, BOOL *stop) {
  847. #pragma unused(aKey, aValue, stop)
  848. XCTFail(@"Shouldn't get here!");
  849. }];
  850. [dict release];
  851. }
  852. - (void)testOne {
  853. GPBBoolInt64Dictionary *dict = [GPBBoolInt64Dictionary dictionaryWithValue:400 forKey:YES];
  854. XCTAssertNotNil(dict);
  855. XCTAssertEqual(dict.count, 1U);
  856. int64_t value;
  857. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  858. XCTAssertTrue([dict valueForKey:YES value:&value]);
  859. XCTAssertEqual(value, 400);
  860. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  861. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, int64_t aValue, BOOL *stop) {
  862. XCTAssertEqual(aKey, YES);
  863. XCTAssertEqual(aValue, 400);
  864. XCTAssertNotEqual(stop, NULL);
  865. }];
  866. }
  867. - (void)testBasics {
  868. const BOOL kKeys[] = { YES, NO };
  869. const int64_t kValues[] = { 400, 401 };
  870. GPBBoolInt64Dictionary *dict =
  871. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues
  872. forKeys:kKeys
  873. count:GPBARRAYSIZE(kValues)];
  874. XCTAssertNotNil(dict);
  875. XCTAssertEqual(dict.count, 2U);
  876. int64_t value;
  877. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  878. XCTAssertTrue([dict valueForKey:YES value:&value]);
  879. XCTAssertEqual(value, 400);
  880. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  881. XCTAssertTrue([dict valueForKey:NO value:&value]);
  882. XCTAssertEqual(value, 401);
  883. __block NSUInteger idx = 0;
  884. BOOL *seenKeys = malloc(2 * sizeof(BOOL));
  885. int64_t *seenValues = malloc(2 * sizeof(int64_t));
  886. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, int64_t aValue, BOOL *stop) {
  887. XCTAssertLessThan(idx, 2U);
  888. seenKeys[idx] = aKey;
  889. seenValues[idx] = aValue;
  890. XCTAssertNotEqual(stop, NULL);
  891. ++idx;
  892. }];
  893. for (int i = 0; i < 2; ++i) {
  894. BOOL foundKey = NO;
  895. for (int j = 0; (j < 2) && !foundKey; ++j) {
  896. if (kKeys[i] == seenKeys[j]) {
  897. foundKey = YES;
  898. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  899. }
  900. }
  901. XCTAssertTrue(foundKey, @"i = %d", i);
  902. }
  903. free(seenKeys);
  904. free(seenValues);
  905. // Stopping the enumeration.
  906. idx = 0;
  907. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, int64_t aValue, BOOL *stop) {
  908. #pragma unused(aKey, aValue)
  909. if (idx == 0) *stop = YES;
  910. XCTAssertNotEqual(idx, 2U);
  911. ++idx;
  912. }];
  913. [dict release];
  914. }
  915. - (void)testEquality {
  916. const BOOL kKeys1[] = { YES, NO };
  917. const BOOL kKeys2[] = { NO, YES };
  918. const int64_t kValues1[] = { 400, 401 };
  919. const int64_t kValues2[] = { 401, 400 };
  920. const int64_t kValues3[] = { 401 };
  921. GPBBoolInt64Dictionary *dict1 =
  922. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues1
  923. forKeys:kKeys1
  924. count:GPBARRAYSIZE(kValues1)];
  925. XCTAssertNotNil(dict1);
  926. GPBBoolInt64Dictionary *dict1prime =
  927. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues1
  928. forKeys:kKeys1
  929. count:GPBARRAYSIZE(kValues1)];
  930. XCTAssertNotNil(dict1prime);
  931. GPBBoolInt64Dictionary *dict2 =
  932. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues2
  933. forKeys:kKeys1
  934. count:GPBARRAYSIZE(kValues2)];
  935. XCTAssertNotNil(dict2);
  936. GPBBoolInt64Dictionary *dict3 =
  937. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues1
  938. forKeys:kKeys2
  939. count:GPBARRAYSIZE(kValues1)];
  940. XCTAssertNotNil(dict3);
  941. GPBBoolInt64Dictionary *dict4 =
  942. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues3
  943. forKeys:kKeys1
  944. count:GPBARRAYSIZE(kValues3)];
  945. XCTAssertNotNil(dict4);
  946. // 1/1Prime should be different objects, but equal.
  947. XCTAssertNotEqual(dict1, dict1prime);
  948. XCTAssertEqualObjects(dict1, dict1prime);
  949. // Equal, so they must have same hash.
  950. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  951. // 2 is save keys, different values; not equal.
  952. XCTAssertNotEqualObjects(dict1, dict2);
  953. // 3 is different keys, samae values; not equal.
  954. XCTAssertNotEqualObjects(dict1, dict3);
  955. // 4 Fewer pairs; not equal
  956. XCTAssertNotEqualObjects(dict1, dict4);
  957. [dict1 release];
  958. [dict1prime release];
  959. [dict2 release];
  960. [dict3 release];
  961. [dict4 release];
  962. }
  963. - (void)testCopy {
  964. const BOOL kKeys[] = { YES, NO };
  965. const int64_t kValues[] = { 400, 401 };
  966. GPBBoolInt64Dictionary *dict =
  967. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues
  968. forKeys:kKeys
  969. count:GPBARRAYSIZE(kValues)];
  970. XCTAssertNotNil(dict);
  971. GPBBoolInt64Dictionary *dict2 = [dict copy];
  972. XCTAssertNotNil(dict2);
  973. // Should be new object but equal.
  974. XCTAssertNotEqual(dict, dict2);
  975. XCTAssertEqualObjects(dict, dict2);
  976. XCTAssertTrue([dict2 isKindOfClass:[GPBBoolInt64Dictionary class]]);
  977. [dict2 release];
  978. [dict release];
  979. }
  980. - (void)testDictionaryFromDictionary {
  981. const BOOL kKeys[] = { YES, NO };
  982. const int64_t kValues[] = { 400, 401 };
  983. GPBBoolInt64Dictionary *dict =
  984. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues
  985. forKeys:kKeys
  986. count:GPBARRAYSIZE(kValues)];
  987. XCTAssertNotNil(dict);
  988. GPBBoolInt64Dictionary *dict2 =
  989. [GPBBoolInt64Dictionary dictionaryWithDictionary:dict];
  990. XCTAssertNotNil(dict2);
  991. // Should be new pointer, but equal objects.
  992. XCTAssertNotEqual(dict, dict2);
  993. XCTAssertEqualObjects(dict, dict2);
  994. [dict release];
  995. }
  996. - (void)testAdds {
  997. GPBBoolInt64Dictionary *dict = [GPBBoolInt64Dictionary dictionary];
  998. XCTAssertNotNil(dict);
  999. XCTAssertEqual(dict.count, 0U);
  1000. [dict setValue:400 forKey:YES];
  1001. XCTAssertEqual(dict.count, 1U);
  1002. const BOOL kKeys[] = { NO };
  1003. const int64_t kValues[] = { 401 };
  1004. GPBBoolInt64Dictionary *dict2 =
  1005. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues
  1006. forKeys:kKeys
  1007. count:GPBARRAYSIZE(kValues)];
  1008. XCTAssertNotNil(dict2);
  1009. [dict addEntriesFromDictionary:dict2];
  1010. XCTAssertEqual(dict.count, 2U);
  1011. int64_t value;
  1012. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1013. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1014. XCTAssertEqual(value, 400);
  1015. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1016. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1017. XCTAssertEqual(value, 401);
  1018. [dict2 release];
  1019. }
  1020. - (void)testRemove {
  1021. const BOOL kKeys[] = { YES, NO};
  1022. const int64_t kValues[] = { 400, 401 };
  1023. GPBBoolInt64Dictionary *dict =
  1024. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues
  1025. forKeys:kKeys
  1026. count:GPBARRAYSIZE(kValues)];
  1027. XCTAssertNotNil(dict);
  1028. XCTAssertEqual(dict.count, 2U);
  1029. [dict removeValueForKey:NO];
  1030. XCTAssertEqual(dict.count, 1U);
  1031. int64_t value;
  1032. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1033. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1034. XCTAssertEqual(value, 400);
  1035. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1036. // Remove again does nothing.
  1037. [dict removeValueForKey:NO];
  1038. XCTAssertEqual(dict.count, 1U);
  1039. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1040. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1041. XCTAssertEqual(value, 400);
  1042. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1043. [dict removeAll];
  1044. XCTAssertEqual(dict.count, 0U);
  1045. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  1046. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1047. [dict release];
  1048. }
  1049. - (void)testInplaceMutation {
  1050. const BOOL kKeys[] = { YES, NO };
  1051. const int64_t kValues[] = { 400, 401 };
  1052. GPBBoolInt64Dictionary *dict =
  1053. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues
  1054. forKeys:kKeys
  1055. count:GPBARRAYSIZE(kValues)];
  1056. XCTAssertNotNil(dict);
  1057. XCTAssertEqual(dict.count, 2U);
  1058. int64_t value;
  1059. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1060. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1061. XCTAssertEqual(value, 400);
  1062. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1063. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1064. XCTAssertEqual(value, 401);
  1065. [dict setValue:401 forKey:YES];
  1066. XCTAssertEqual(dict.count, 2U);
  1067. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1068. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1069. XCTAssertEqual(value, 401);
  1070. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1071. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1072. XCTAssertEqual(value, 401);
  1073. [dict setValue:400 forKey:NO];
  1074. XCTAssertEqual(dict.count, 2U);
  1075. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1076. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1077. XCTAssertEqual(value, 401);
  1078. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1079. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1080. XCTAssertEqual(value, 400);
  1081. const BOOL kKeys2[] = { NO, YES };
  1082. const int64_t kValues2[] = { 401, 400 };
  1083. GPBBoolInt64Dictionary *dict2 =
  1084. [[GPBBoolInt64Dictionary alloc] initWithValues:kValues2
  1085. forKeys:kKeys2
  1086. count:GPBARRAYSIZE(kValues2)];
  1087. XCTAssertNotNil(dict2);
  1088. [dict addEntriesFromDictionary:dict2];
  1089. XCTAssertEqual(dict.count, 2U);
  1090. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1091. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1092. XCTAssertEqual(value, 400);
  1093. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1094. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1095. XCTAssertEqual(value, 401);
  1096. [dict2 release];
  1097. [dict release];
  1098. }
  1099. @end
  1100. //%PDDM-EXPAND BOOL_TESTS_FOR_POD_VALUE(Bool, BOOL, NO, YES)
  1101. // This block of code is generated, do not edit it directly.
  1102. #pragma mark - Bool -> Bool
  1103. @interface GPBBoolBoolDictionaryTests : XCTestCase
  1104. @end
  1105. @implementation GPBBoolBoolDictionaryTests
  1106. - (void)testEmpty {
  1107. GPBBoolBoolDictionary *dict = [[GPBBoolBoolDictionary alloc] init];
  1108. XCTAssertNotNil(dict);
  1109. XCTAssertEqual(dict.count, 0U);
  1110. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  1111. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, BOOL aValue, BOOL *stop) {
  1112. #pragma unused(aKey, aValue, stop)
  1113. XCTFail(@"Shouldn't get here!");
  1114. }];
  1115. [dict release];
  1116. }
  1117. - (void)testOne {
  1118. GPBBoolBoolDictionary *dict = [GPBBoolBoolDictionary dictionaryWithValue:NO forKey:YES];
  1119. XCTAssertNotNil(dict);
  1120. XCTAssertEqual(dict.count, 1U);
  1121. BOOL value;
  1122. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1123. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1124. XCTAssertEqual(value, NO);
  1125. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1126. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, BOOL aValue, BOOL *stop) {
  1127. XCTAssertEqual(aKey, YES);
  1128. XCTAssertEqual(aValue, NO);
  1129. XCTAssertNotEqual(stop, NULL);
  1130. }];
  1131. }
  1132. - (void)testBasics {
  1133. const BOOL kKeys[] = { YES, NO };
  1134. const BOOL kValues[] = { NO, YES };
  1135. GPBBoolBoolDictionary *dict =
  1136. [[GPBBoolBoolDictionary alloc] initWithValues:kValues
  1137. forKeys:kKeys
  1138. count:GPBARRAYSIZE(kValues)];
  1139. XCTAssertNotNil(dict);
  1140. XCTAssertEqual(dict.count, 2U);
  1141. BOOL value;
  1142. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1143. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1144. XCTAssertEqual(value, NO);
  1145. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1146. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1147. XCTAssertEqual(value, YES);
  1148. __block NSUInteger idx = 0;
  1149. BOOL *seenKeys = malloc(2 * sizeof(BOOL));
  1150. BOOL *seenValues = malloc(2 * sizeof(BOOL));
  1151. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, BOOL aValue, BOOL *stop) {
  1152. XCTAssertLessThan(idx, 2U);
  1153. seenKeys[idx] = aKey;
  1154. seenValues[idx] = aValue;
  1155. XCTAssertNotEqual(stop, NULL);
  1156. ++idx;
  1157. }];
  1158. for (int i = 0; i < 2; ++i) {
  1159. BOOL foundKey = NO;
  1160. for (int j = 0; (j < 2) && !foundKey; ++j) {
  1161. if (kKeys[i] == seenKeys[j]) {
  1162. foundKey = YES;
  1163. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  1164. }
  1165. }
  1166. XCTAssertTrue(foundKey, @"i = %d", i);
  1167. }
  1168. free(seenKeys);
  1169. free(seenValues);
  1170. // Stopping the enumeration.
  1171. idx = 0;
  1172. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, BOOL aValue, BOOL *stop) {
  1173. #pragma unused(aKey, aValue)
  1174. if (idx == 0) *stop = YES;
  1175. XCTAssertNotEqual(idx, 2U);
  1176. ++idx;
  1177. }];
  1178. [dict release];
  1179. }
  1180. - (void)testEquality {
  1181. const BOOL kKeys1[] = { YES, NO };
  1182. const BOOL kKeys2[] = { NO, YES };
  1183. const BOOL kValues1[] = { NO, YES };
  1184. const BOOL kValues2[] = { YES, NO };
  1185. const BOOL kValues3[] = { YES };
  1186. GPBBoolBoolDictionary *dict1 =
  1187. [[GPBBoolBoolDictionary alloc] initWithValues:kValues1
  1188. forKeys:kKeys1
  1189. count:GPBARRAYSIZE(kValues1)];
  1190. XCTAssertNotNil(dict1);
  1191. GPBBoolBoolDictionary *dict1prime =
  1192. [[GPBBoolBoolDictionary alloc] initWithValues:kValues1
  1193. forKeys:kKeys1
  1194. count:GPBARRAYSIZE(kValues1)];
  1195. XCTAssertNotNil(dict1prime);
  1196. GPBBoolBoolDictionary *dict2 =
  1197. [[GPBBoolBoolDictionary alloc] initWithValues:kValues2
  1198. forKeys:kKeys1
  1199. count:GPBARRAYSIZE(kValues2)];
  1200. XCTAssertNotNil(dict2);
  1201. GPBBoolBoolDictionary *dict3 =
  1202. [[GPBBoolBoolDictionary alloc] initWithValues:kValues1
  1203. forKeys:kKeys2
  1204. count:GPBARRAYSIZE(kValues1)];
  1205. XCTAssertNotNil(dict3);
  1206. GPBBoolBoolDictionary *dict4 =
  1207. [[GPBBoolBoolDictionary alloc] initWithValues:kValues3
  1208. forKeys:kKeys1
  1209. count:GPBARRAYSIZE(kValues3)];
  1210. XCTAssertNotNil(dict4);
  1211. // 1/1Prime should be different objects, but equal.
  1212. XCTAssertNotEqual(dict1, dict1prime);
  1213. XCTAssertEqualObjects(dict1, dict1prime);
  1214. // Equal, so they must have same hash.
  1215. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  1216. // 2 is save keys, different values; not equal.
  1217. XCTAssertNotEqualObjects(dict1, dict2);
  1218. // 3 is different keys, samae values; not equal.
  1219. XCTAssertNotEqualObjects(dict1, dict3);
  1220. // 4 Fewer pairs; not equal
  1221. XCTAssertNotEqualObjects(dict1, dict4);
  1222. [dict1 release];
  1223. [dict1prime release];
  1224. [dict2 release];
  1225. [dict3 release];
  1226. [dict4 release];
  1227. }
  1228. - (void)testCopy {
  1229. const BOOL kKeys[] = { YES, NO };
  1230. const BOOL kValues[] = { NO, YES };
  1231. GPBBoolBoolDictionary *dict =
  1232. [[GPBBoolBoolDictionary alloc] initWithValues:kValues
  1233. forKeys:kKeys
  1234. count:GPBARRAYSIZE(kValues)];
  1235. XCTAssertNotNil(dict);
  1236. GPBBoolBoolDictionary *dict2 = [dict copy];
  1237. XCTAssertNotNil(dict2);
  1238. // Should be new object but equal.
  1239. XCTAssertNotEqual(dict, dict2);
  1240. XCTAssertEqualObjects(dict, dict2);
  1241. XCTAssertTrue([dict2 isKindOfClass:[GPBBoolBoolDictionary class]]);
  1242. [dict2 release];
  1243. [dict release];
  1244. }
  1245. - (void)testDictionaryFromDictionary {
  1246. const BOOL kKeys[] = { YES, NO };
  1247. const BOOL kValues[] = { NO, YES };
  1248. GPBBoolBoolDictionary *dict =
  1249. [[GPBBoolBoolDictionary alloc] initWithValues:kValues
  1250. forKeys:kKeys
  1251. count:GPBARRAYSIZE(kValues)];
  1252. XCTAssertNotNil(dict);
  1253. GPBBoolBoolDictionary *dict2 =
  1254. [GPBBoolBoolDictionary dictionaryWithDictionary:dict];
  1255. XCTAssertNotNil(dict2);
  1256. // Should be new pointer, but equal objects.
  1257. XCTAssertNotEqual(dict, dict2);
  1258. XCTAssertEqualObjects(dict, dict2);
  1259. [dict release];
  1260. }
  1261. - (void)testAdds {
  1262. GPBBoolBoolDictionary *dict = [GPBBoolBoolDictionary dictionary];
  1263. XCTAssertNotNil(dict);
  1264. XCTAssertEqual(dict.count, 0U);
  1265. [dict setValue:NO forKey:YES];
  1266. XCTAssertEqual(dict.count, 1U);
  1267. const BOOL kKeys[] = { NO };
  1268. const BOOL kValues[] = { YES };
  1269. GPBBoolBoolDictionary *dict2 =
  1270. [[GPBBoolBoolDictionary alloc] initWithValues:kValues
  1271. forKeys:kKeys
  1272. count:GPBARRAYSIZE(kValues)];
  1273. XCTAssertNotNil(dict2);
  1274. [dict addEntriesFromDictionary:dict2];
  1275. XCTAssertEqual(dict.count, 2U);
  1276. BOOL value;
  1277. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1278. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1279. XCTAssertEqual(value, NO);
  1280. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1281. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1282. XCTAssertEqual(value, YES);
  1283. [dict2 release];
  1284. }
  1285. - (void)testRemove {
  1286. const BOOL kKeys[] = { YES, NO};
  1287. const BOOL kValues[] = { NO, YES };
  1288. GPBBoolBoolDictionary *dict =
  1289. [[GPBBoolBoolDictionary alloc] initWithValues:kValues
  1290. forKeys:kKeys
  1291. count:GPBARRAYSIZE(kValues)];
  1292. XCTAssertNotNil(dict);
  1293. XCTAssertEqual(dict.count, 2U);
  1294. [dict removeValueForKey:NO];
  1295. XCTAssertEqual(dict.count, 1U);
  1296. BOOL value;
  1297. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1298. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1299. XCTAssertEqual(value, NO);
  1300. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1301. // Remove again does nothing.
  1302. [dict removeValueForKey:NO];
  1303. XCTAssertEqual(dict.count, 1U);
  1304. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1305. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1306. XCTAssertEqual(value, NO);
  1307. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1308. [dict removeAll];
  1309. XCTAssertEqual(dict.count, 0U);
  1310. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  1311. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1312. [dict release];
  1313. }
  1314. - (void)testInplaceMutation {
  1315. const BOOL kKeys[] = { YES, NO };
  1316. const BOOL kValues[] = { NO, YES };
  1317. GPBBoolBoolDictionary *dict =
  1318. [[GPBBoolBoolDictionary alloc] initWithValues:kValues
  1319. forKeys:kKeys
  1320. count:GPBARRAYSIZE(kValues)];
  1321. XCTAssertNotNil(dict);
  1322. XCTAssertEqual(dict.count, 2U);
  1323. BOOL value;
  1324. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1325. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1326. XCTAssertEqual(value, NO);
  1327. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1328. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1329. XCTAssertEqual(value, YES);
  1330. [dict setValue:YES forKey:YES];
  1331. XCTAssertEqual(dict.count, 2U);
  1332. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1333. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1334. XCTAssertEqual(value, YES);
  1335. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1336. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1337. XCTAssertEqual(value, YES);
  1338. [dict setValue:NO forKey:NO];
  1339. XCTAssertEqual(dict.count, 2U);
  1340. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1341. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1342. XCTAssertEqual(value, YES);
  1343. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1344. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1345. XCTAssertEqual(value, NO);
  1346. const BOOL kKeys2[] = { NO, YES };
  1347. const BOOL kValues2[] = { YES, NO };
  1348. GPBBoolBoolDictionary *dict2 =
  1349. [[GPBBoolBoolDictionary alloc] initWithValues:kValues2
  1350. forKeys:kKeys2
  1351. count:GPBARRAYSIZE(kValues2)];
  1352. XCTAssertNotNil(dict2);
  1353. [dict addEntriesFromDictionary:dict2];
  1354. XCTAssertEqual(dict.count, 2U);
  1355. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1356. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1357. XCTAssertEqual(value, NO);
  1358. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1359. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1360. XCTAssertEqual(value, YES);
  1361. [dict2 release];
  1362. [dict release];
  1363. }
  1364. @end
  1365. //%PDDM-EXPAND BOOL_TESTS_FOR_POD_VALUE(Float, float, 500.f, 501.f)
  1366. // This block of code is generated, do not edit it directly.
  1367. #pragma mark - Bool -> Float
  1368. @interface GPBBoolFloatDictionaryTests : XCTestCase
  1369. @end
  1370. @implementation GPBBoolFloatDictionaryTests
  1371. - (void)testEmpty {
  1372. GPBBoolFloatDictionary *dict = [[GPBBoolFloatDictionary alloc] init];
  1373. XCTAssertNotNil(dict);
  1374. XCTAssertEqual(dict.count, 0U);
  1375. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  1376. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, float aValue, BOOL *stop) {
  1377. #pragma unused(aKey, aValue, stop)
  1378. XCTFail(@"Shouldn't get here!");
  1379. }];
  1380. [dict release];
  1381. }
  1382. - (void)testOne {
  1383. GPBBoolFloatDictionary *dict = [GPBBoolFloatDictionary dictionaryWithValue:500.f forKey:YES];
  1384. XCTAssertNotNil(dict);
  1385. XCTAssertEqual(dict.count, 1U);
  1386. float value;
  1387. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1388. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1389. XCTAssertEqual(value, 500.f);
  1390. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1391. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, float aValue, BOOL *stop) {
  1392. XCTAssertEqual(aKey, YES);
  1393. XCTAssertEqual(aValue, 500.f);
  1394. XCTAssertNotEqual(stop, NULL);
  1395. }];
  1396. }
  1397. - (void)testBasics {
  1398. const BOOL kKeys[] = { YES, NO };
  1399. const float kValues[] = { 500.f, 501.f };
  1400. GPBBoolFloatDictionary *dict =
  1401. [[GPBBoolFloatDictionary alloc] initWithValues:kValues
  1402. forKeys:kKeys
  1403. count:GPBARRAYSIZE(kValues)];
  1404. XCTAssertNotNil(dict);
  1405. XCTAssertEqual(dict.count, 2U);
  1406. float value;
  1407. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1408. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1409. XCTAssertEqual(value, 500.f);
  1410. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1411. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1412. XCTAssertEqual(value, 501.f);
  1413. __block NSUInteger idx = 0;
  1414. BOOL *seenKeys = malloc(2 * sizeof(BOOL));
  1415. float *seenValues = malloc(2 * sizeof(float));
  1416. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, float aValue, BOOL *stop) {
  1417. XCTAssertLessThan(idx, 2U);
  1418. seenKeys[idx] = aKey;
  1419. seenValues[idx] = aValue;
  1420. XCTAssertNotEqual(stop, NULL);
  1421. ++idx;
  1422. }];
  1423. for (int i = 0; i < 2; ++i) {
  1424. BOOL foundKey = NO;
  1425. for (int j = 0; (j < 2) && !foundKey; ++j) {
  1426. if (kKeys[i] == seenKeys[j]) {
  1427. foundKey = YES;
  1428. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  1429. }
  1430. }
  1431. XCTAssertTrue(foundKey, @"i = %d", i);
  1432. }
  1433. free(seenKeys);
  1434. free(seenValues);
  1435. // Stopping the enumeration.
  1436. idx = 0;
  1437. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, float aValue, BOOL *stop) {
  1438. #pragma unused(aKey, aValue)
  1439. if (idx == 0) *stop = YES;
  1440. XCTAssertNotEqual(idx, 2U);
  1441. ++idx;
  1442. }];
  1443. [dict release];
  1444. }
  1445. - (void)testEquality {
  1446. const BOOL kKeys1[] = { YES, NO };
  1447. const BOOL kKeys2[] = { NO, YES };
  1448. const float kValues1[] = { 500.f, 501.f };
  1449. const float kValues2[] = { 501.f, 500.f };
  1450. const float kValues3[] = { 501.f };
  1451. GPBBoolFloatDictionary *dict1 =
  1452. [[GPBBoolFloatDictionary alloc] initWithValues:kValues1
  1453. forKeys:kKeys1
  1454. count:GPBARRAYSIZE(kValues1)];
  1455. XCTAssertNotNil(dict1);
  1456. GPBBoolFloatDictionary *dict1prime =
  1457. [[GPBBoolFloatDictionary alloc] initWithValues:kValues1
  1458. forKeys:kKeys1
  1459. count:GPBARRAYSIZE(kValues1)];
  1460. XCTAssertNotNil(dict1prime);
  1461. GPBBoolFloatDictionary *dict2 =
  1462. [[GPBBoolFloatDictionary alloc] initWithValues:kValues2
  1463. forKeys:kKeys1
  1464. count:GPBARRAYSIZE(kValues2)];
  1465. XCTAssertNotNil(dict2);
  1466. GPBBoolFloatDictionary *dict3 =
  1467. [[GPBBoolFloatDictionary alloc] initWithValues:kValues1
  1468. forKeys:kKeys2
  1469. count:GPBARRAYSIZE(kValues1)];
  1470. XCTAssertNotNil(dict3);
  1471. GPBBoolFloatDictionary *dict4 =
  1472. [[GPBBoolFloatDictionary alloc] initWithValues:kValues3
  1473. forKeys:kKeys1
  1474. count:GPBARRAYSIZE(kValues3)];
  1475. XCTAssertNotNil(dict4);
  1476. // 1/1Prime should be different objects, but equal.
  1477. XCTAssertNotEqual(dict1, dict1prime);
  1478. XCTAssertEqualObjects(dict1, dict1prime);
  1479. // Equal, so they must have same hash.
  1480. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  1481. // 2 is save keys, different values; not equal.
  1482. XCTAssertNotEqualObjects(dict1, dict2);
  1483. // 3 is different keys, samae values; not equal.
  1484. XCTAssertNotEqualObjects(dict1, dict3);
  1485. // 4 Fewer pairs; not equal
  1486. XCTAssertNotEqualObjects(dict1, dict4);
  1487. [dict1 release];
  1488. [dict1prime release];
  1489. [dict2 release];
  1490. [dict3 release];
  1491. [dict4 release];
  1492. }
  1493. - (void)testCopy {
  1494. const BOOL kKeys[] = { YES, NO };
  1495. const float kValues[] = { 500.f, 501.f };
  1496. GPBBoolFloatDictionary *dict =
  1497. [[GPBBoolFloatDictionary alloc] initWithValues:kValues
  1498. forKeys:kKeys
  1499. count:GPBARRAYSIZE(kValues)];
  1500. XCTAssertNotNil(dict);
  1501. GPBBoolFloatDictionary *dict2 = [dict copy];
  1502. XCTAssertNotNil(dict2);
  1503. // Should be new object but equal.
  1504. XCTAssertNotEqual(dict, dict2);
  1505. XCTAssertEqualObjects(dict, dict2);
  1506. XCTAssertTrue([dict2 isKindOfClass:[GPBBoolFloatDictionary class]]);
  1507. [dict2 release];
  1508. [dict release];
  1509. }
  1510. - (void)testDictionaryFromDictionary {
  1511. const BOOL kKeys[] = { YES, NO };
  1512. const float kValues[] = { 500.f, 501.f };
  1513. GPBBoolFloatDictionary *dict =
  1514. [[GPBBoolFloatDictionary alloc] initWithValues:kValues
  1515. forKeys:kKeys
  1516. count:GPBARRAYSIZE(kValues)];
  1517. XCTAssertNotNil(dict);
  1518. GPBBoolFloatDictionary *dict2 =
  1519. [GPBBoolFloatDictionary dictionaryWithDictionary:dict];
  1520. XCTAssertNotNil(dict2);
  1521. // Should be new pointer, but equal objects.
  1522. XCTAssertNotEqual(dict, dict2);
  1523. XCTAssertEqualObjects(dict, dict2);
  1524. [dict release];
  1525. }
  1526. - (void)testAdds {
  1527. GPBBoolFloatDictionary *dict = [GPBBoolFloatDictionary dictionary];
  1528. XCTAssertNotNil(dict);
  1529. XCTAssertEqual(dict.count, 0U);
  1530. [dict setValue:500.f forKey:YES];
  1531. XCTAssertEqual(dict.count, 1U);
  1532. const BOOL kKeys[] = { NO };
  1533. const float kValues[] = { 501.f };
  1534. GPBBoolFloatDictionary *dict2 =
  1535. [[GPBBoolFloatDictionary alloc] initWithValues:kValues
  1536. forKeys:kKeys
  1537. count:GPBARRAYSIZE(kValues)];
  1538. XCTAssertNotNil(dict2);
  1539. [dict addEntriesFromDictionary:dict2];
  1540. XCTAssertEqual(dict.count, 2U);
  1541. float value;
  1542. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1543. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1544. XCTAssertEqual(value, 500.f);
  1545. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1546. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1547. XCTAssertEqual(value, 501.f);
  1548. [dict2 release];
  1549. }
  1550. - (void)testRemove {
  1551. const BOOL kKeys[] = { YES, NO};
  1552. const float kValues[] = { 500.f, 501.f };
  1553. GPBBoolFloatDictionary *dict =
  1554. [[GPBBoolFloatDictionary alloc] initWithValues:kValues
  1555. forKeys:kKeys
  1556. count:GPBARRAYSIZE(kValues)];
  1557. XCTAssertNotNil(dict);
  1558. XCTAssertEqual(dict.count, 2U);
  1559. [dict removeValueForKey:NO];
  1560. XCTAssertEqual(dict.count, 1U);
  1561. float value;
  1562. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1563. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1564. XCTAssertEqual(value, 500.f);
  1565. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1566. // Remove again does nothing.
  1567. [dict removeValueForKey:NO];
  1568. XCTAssertEqual(dict.count, 1U);
  1569. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1570. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1571. XCTAssertEqual(value, 500.f);
  1572. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1573. [dict removeAll];
  1574. XCTAssertEqual(dict.count, 0U);
  1575. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  1576. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1577. [dict release];
  1578. }
  1579. - (void)testInplaceMutation {
  1580. const BOOL kKeys[] = { YES, NO };
  1581. const float kValues[] = { 500.f, 501.f };
  1582. GPBBoolFloatDictionary *dict =
  1583. [[GPBBoolFloatDictionary alloc] initWithValues:kValues
  1584. forKeys:kKeys
  1585. count:GPBARRAYSIZE(kValues)];
  1586. XCTAssertNotNil(dict);
  1587. XCTAssertEqual(dict.count, 2U);
  1588. float value;
  1589. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1590. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1591. XCTAssertEqual(value, 500.f);
  1592. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1593. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1594. XCTAssertEqual(value, 501.f);
  1595. [dict setValue:501.f forKey:YES];
  1596. XCTAssertEqual(dict.count, 2U);
  1597. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1598. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1599. XCTAssertEqual(value, 501.f);
  1600. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1601. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1602. XCTAssertEqual(value, 501.f);
  1603. [dict setValue:500.f forKey:NO];
  1604. XCTAssertEqual(dict.count, 2U);
  1605. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1606. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1607. XCTAssertEqual(value, 501.f);
  1608. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1609. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1610. XCTAssertEqual(value, 500.f);
  1611. const BOOL kKeys2[] = { NO, YES };
  1612. const float kValues2[] = { 501.f, 500.f };
  1613. GPBBoolFloatDictionary *dict2 =
  1614. [[GPBBoolFloatDictionary alloc] initWithValues:kValues2
  1615. forKeys:kKeys2
  1616. count:GPBARRAYSIZE(kValues2)];
  1617. XCTAssertNotNil(dict2);
  1618. [dict addEntriesFromDictionary:dict2];
  1619. XCTAssertEqual(dict.count, 2U);
  1620. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1621. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1622. XCTAssertEqual(value, 500.f);
  1623. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1624. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1625. XCTAssertEqual(value, 501.f);
  1626. [dict2 release];
  1627. [dict release];
  1628. }
  1629. @end
  1630. //%PDDM-EXPAND BOOL_TESTS_FOR_POD_VALUE(Double, double, 600., 601.)
  1631. // This block of code is generated, do not edit it directly.
  1632. #pragma mark - Bool -> Double
  1633. @interface GPBBoolDoubleDictionaryTests : XCTestCase
  1634. @end
  1635. @implementation GPBBoolDoubleDictionaryTests
  1636. - (void)testEmpty {
  1637. GPBBoolDoubleDictionary *dict = [[GPBBoolDoubleDictionary alloc] init];
  1638. XCTAssertNotNil(dict);
  1639. XCTAssertEqual(dict.count, 0U);
  1640. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  1641. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, double aValue, BOOL *stop) {
  1642. #pragma unused(aKey, aValue, stop)
  1643. XCTFail(@"Shouldn't get here!");
  1644. }];
  1645. [dict release];
  1646. }
  1647. - (void)testOne {
  1648. GPBBoolDoubleDictionary *dict = [GPBBoolDoubleDictionary dictionaryWithValue:600. forKey:YES];
  1649. XCTAssertNotNil(dict);
  1650. XCTAssertEqual(dict.count, 1U);
  1651. double value;
  1652. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1653. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1654. XCTAssertEqual(value, 600.);
  1655. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1656. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, double aValue, BOOL *stop) {
  1657. XCTAssertEqual(aKey, YES);
  1658. XCTAssertEqual(aValue, 600.);
  1659. XCTAssertNotEqual(stop, NULL);
  1660. }];
  1661. }
  1662. - (void)testBasics {
  1663. const BOOL kKeys[] = { YES, NO };
  1664. const double kValues[] = { 600., 601. };
  1665. GPBBoolDoubleDictionary *dict =
  1666. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues
  1667. forKeys:kKeys
  1668. count:GPBARRAYSIZE(kValues)];
  1669. XCTAssertNotNil(dict);
  1670. XCTAssertEqual(dict.count, 2U);
  1671. double value;
  1672. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1673. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1674. XCTAssertEqual(value, 600.);
  1675. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1676. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1677. XCTAssertEqual(value, 601.);
  1678. __block NSUInteger idx = 0;
  1679. BOOL *seenKeys = malloc(2 * sizeof(BOOL));
  1680. double *seenValues = malloc(2 * sizeof(double));
  1681. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, double aValue, BOOL *stop) {
  1682. XCTAssertLessThan(idx, 2U);
  1683. seenKeys[idx] = aKey;
  1684. seenValues[idx] = aValue;
  1685. XCTAssertNotEqual(stop, NULL);
  1686. ++idx;
  1687. }];
  1688. for (int i = 0; i < 2; ++i) {
  1689. BOOL foundKey = NO;
  1690. for (int j = 0; (j < 2) && !foundKey; ++j) {
  1691. if (kKeys[i] == seenKeys[j]) {
  1692. foundKey = YES;
  1693. XCTAssertEqual(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  1694. }
  1695. }
  1696. XCTAssertTrue(foundKey, @"i = %d", i);
  1697. }
  1698. free(seenKeys);
  1699. free(seenValues);
  1700. // Stopping the enumeration.
  1701. idx = 0;
  1702. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, double aValue, BOOL *stop) {
  1703. #pragma unused(aKey, aValue)
  1704. if (idx == 0) *stop = YES;
  1705. XCTAssertNotEqual(idx, 2U);
  1706. ++idx;
  1707. }];
  1708. [dict release];
  1709. }
  1710. - (void)testEquality {
  1711. const BOOL kKeys1[] = { YES, NO };
  1712. const BOOL kKeys2[] = { NO, YES };
  1713. const double kValues1[] = { 600., 601. };
  1714. const double kValues2[] = { 601., 600. };
  1715. const double kValues3[] = { 601. };
  1716. GPBBoolDoubleDictionary *dict1 =
  1717. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues1
  1718. forKeys:kKeys1
  1719. count:GPBARRAYSIZE(kValues1)];
  1720. XCTAssertNotNil(dict1);
  1721. GPBBoolDoubleDictionary *dict1prime =
  1722. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues1
  1723. forKeys:kKeys1
  1724. count:GPBARRAYSIZE(kValues1)];
  1725. XCTAssertNotNil(dict1prime);
  1726. GPBBoolDoubleDictionary *dict2 =
  1727. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues2
  1728. forKeys:kKeys1
  1729. count:GPBARRAYSIZE(kValues2)];
  1730. XCTAssertNotNil(dict2);
  1731. GPBBoolDoubleDictionary *dict3 =
  1732. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues1
  1733. forKeys:kKeys2
  1734. count:GPBARRAYSIZE(kValues1)];
  1735. XCTAssertNotNil(dict3);
  1736. GPBBoolDoubleDictionary *dict4 =
  1737. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues3
  1738. forKeys:kKeys1
  1739. count:GPBARRAYSIZE(kValues3)];
  1740. XCTAssertNotNil(dict4);
  1741. // 1/1Prime should be different objects, but equal.
  1742. XCTAssertNotEqual(dict1, dict1prime);
  1743. XCTAssertEqualObjects(dict1, dict1prime);
  1744. // Equal, so they must have same hash.
  1745. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  1746. // 2 is save keys, different values; not equal.
  1747. XCTAssertNotEqualObjects(dict1, dict2);
  1748. // 3 is different keys, samae values; not equal.
  1749. XCTAssertNotEqualObjects(dict1, dict3);
  1750. // 4 Fewer pairs; not equal
  1751. XCTAssertNotEqualObjects(dict1, dict4);
  1752. [dict1 release];
  1753. [dict1prime release];
  1754. [dict2 release];
  1755. [dict3 release];
  1756. [dict4 release];
  1757. }
  1758. - (void)testCopy {
  1759. const BOOL kKeys[] = { YES, NO };
  1760. const double kValues[] = { 600., 601. };
  1761. GPBBoolDoubleDictionary *dict =
  1762. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues
  1763. forKeys:kKeys
  1764. count:GPBARRAYSIZE(kValues)];
  1765. XCTAssertNotNil(dict);
  1766. GPBBoolDoubleDictionary *dict2 = [dict copy];
  1767. XCTAssertNotNil(dict2);
  1768. // Should be new object but equal.
  1769. XCTAssertNotEqual(dict, dict2);
  1770. XCTAssertEqualObjects(dict, dict2);
  1771. XCTAssertTrue([dict2 isKindOfClass:[GPBBoolDoubleDictionary class]]);
  1772. [dict2 release];
  1773. [dict release];
  1774. }
  1775. - (void)testDictionaryFromDictionary {
  1776. const BOOL kKeys[] = { YES, NO };
  1777. const double kValues[] = { 600., 601. };
  1778. GPBBoolDoubleDictionary *dict =
  1779. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues
  1780. forKeys:kKeys
  1781. count:GPBARRAYSIZE(kValues)];
  1782. XCTAssertNotNil(dict);
  1783. GPBBoolDoubleDictionary *dict2 =
  1784. [GPBBoolDoubleDictionary dictionaryWithDictionary:dict];
  1785. XCTAssertNotNil(dict2);
  1786. // Should be new pointer, but equal objects.
  1787. XCTAssertNotEqual(dict, dict2);
  1788. XCTAssertEqualObjects(dict, dict2);
  1789. [dict release];
  1790. }
  1791. - (void)testAdds {
  1792. GPBBoolDoubleDictionary *dict = [GPBBoolDoubleDictionary dictionary];
  1793. XCTAssertNotNil(dict);
  1794. XCTAssertEqual(dict.count, 0U);
  1795. [dict setValue:600. forKey:YES];
  1796. XCTAssertEqual(dict.count, 1U);
  1797. const BOOL kKeys[] = { NO };
  1798. const double kValues[] = { 601. };
  1799. GPBBoolDoubleDictionary *dict2 =
  1800. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues
  1801. forKeys:kKeys
  1802. count:GPBARRAYSIZE(kValues)];
  1803. XCTAssertNotNil(dict2);
  1804. [dict addEntriesFromDictionary:dict2];
  1805. XCTAssertEqual(dict.count, 2U);
  1806. double value;
  1807. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1808. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1809. XCTAssertEqual(value, 600.);
  1810. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1811. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1812. XCTAssertEqual(value, 601.);
  1813. [dict2 release];
  1814. }
  1815. - (void)testRemove {
  1816. const BOOL kKeys[] = { YES, NO};
  1817. const double kValues[] = { 600., 601. };
  1818. GPBBoolDoubleDictionary *dict =
  1819. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues
  1820. forKeys:kKeys
  1821. count:GPBARRAYSIZE(kValues)];
  1822. XCTAssertNotNil(dict);
  1823. XCTAssertEqual(dict.count, 2U);
  1824. [dict removeValueForKey:NO];
  1825. XCTAssertEqual(dict.count, 1U);
  1826. double value;
  1827. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1828. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1829. XCTAssertEqual(value, 600.);
  1830. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1831. // Remove again does nothing.
  1832. [dict removeValueForKey:NO];
  1833. XCTAssertEqual(dict.count, 1U);
  1834. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1835. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1836. XCTAssertEqual(value, 600.);
  1837. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1838. [dict removeAll];
  1839. XCTAssertEqual(dict.count, 0U);
  1840. XCTAssertFalse([dict valueForKey:YES value:NULL]);
  1841. XCTAssertFalse([dict valueForKey:NO value:NULL]);
  1842. [dict release];
  1843. }
  1844. - (void)testInplaceMutation {
  1845. const BOOL kKeys[] = { YES, NO };
  1846. const double kValues[] = { 600., 601. };
  1847. GPBBoolDoubleDictionary *dict =
  1848. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues
  1849. forKeys:kKeys
  1850. count:GPBARRAYSIZE(kValues)];
  1851. XCTAssertNotNil(dict);
  1852. XCTAssertEqual(dict.count, 2U);
  1853. double value;
  1854. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1855. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1856. XCTAssertEqual(value, 600.);
  1857. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1858. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1859. XCTAssertEqual(value, 601.);
  1860. [dict setValue:601. forKey:YES];
  1861. XCTAssertEqual(dict.count, 2U);
  1862. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1863. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1864. XCTAssertEqual(value, 601.);
  1865. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1866. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1867. XCTAssertEqual(value, 601.);
  1868. [dict setValue:600. forKey:NO];
  1869. XCTAssertEqual(dict.count, 2U);
  1870. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1871. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1872. XCTAssertEqual(value, 601.);
  1873. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1874. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1875. XCTAssertEqual(value, 600.);
  1876. const BOOL kKeys2[] = { NO, YES };
  1877. const double kValues2[] = { 601., 600. };
  1878. GPBBoolDoubleDictionary *dict2 =
  1879. [[GPBBoolDoubleDictionary alloc] initWithValues:kValues2
  1880. forKeys:kKeys2
  1881. count:GPBARRAYSIZE(kValues2)];
  1882. XCTAssertNotNil(dict2);
  1883. [dict addEntriesFromDictionary:dict2];
  1884. XCTAssertEqual(dict.count, 2U);
  1885. XCTAssertTrue([dict valueForKey:YES value:NULL]);
  1886. XCTAssertTrue([dict valueForKey:YES value:&value]);
  1887. XCTAssertEqual(value, 600.);
  1888. XCTAssertTrue([dict valueForKey:NO value:NULL]);
  1889. XCTAssertTrue([dict valueForKey:NO value:&value]);
  1890. XCTAssertEqual(value, 601.);
  1891. [dict2 release];
  1892. [dict release];
  1893. }
  1894. @end
  1895. //%PDDM-EXPAND TESTS_FOR_BOOL_KEY_OBJECT_VALUE(Object, id, @"abc", @"def")
  1896. // This block of code is generated, do not edit it directly.
  1897. #pragma mark - Bool -> Object
  1898. @interface GPBBoolObjectDictionaryTests : XCTestCase
  1899. @end
  1900. @implementation GPBBoolObjectDictionaryTests
  1901. - (void)testEmpty {
  1902. GPBBoolObjectDictionary *dict = [[GPBBoolObjectDictionary alloc] init];
  1903. XCTAssertNotNil(dict);
  1904. XCTAssertEqual(dict.count, 0U);
  1905. XCTAssertNil([dict valueForKey:YES]);
  1906. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, id aValue, BOOL *stop) {
  1907. #pragma unused(aKey, aValue, stop)
  1908. XCTFail(@"Shouldn't get here!");
  1909. }];
  1910. [dict release];
  1911. }
  1912. - (void)testOne {
  1913. GPBBoolObjectDictionary *dict = [GPBBoolObjectDictionary dictionaryWithValue:@"abc" forKey:YES];
  1914. XCTAssertNotNil(dict);
  1915. XCTAssertEqual(dict.count, 1U);
  1916. XCTAssertEqualObjects([dict valueForKey:YES], @"abc");
  1917. XCTAssertNil([dict valueForKey:NO]);
  1918. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, id aValue, BOOL *stop) {
  1919. XCTAssertEqual(aKey, YES);
  1920. XCTAssertEqualObjects(aValue, @"abc");
  1921. XCTAssertNotEqual(stop, NULL);
  1922. }];
  1923. }
  1924. - (void)testBasics {
  1925. const BOOL kKeys[] = { YES, NO };
  1926. const id kValues[] = { @"abc", @"def" };
  1927. GPBBoolObjectDictionary *dict =
  1928. [[GPBBoolObjectDictionary alloc] initWithValues:kValues
  1929. forKeys:kKeys
  1930. count:GPBARRAYSIZE(kValues)];
  1931. XCTAssertNotNil(dict);
  1932. XCTAssertEqual(dict.count, 2U);
  1933. XCTAssertEqualObjects([dict valueForKey:YES], @"abc");
  1934. XCTAssertEqualObjects([dict valueForKey:NO], @"def");
  1935. __block NSUInteger idx = 0;
  1936. BOOL *seenKeys = malloc(2 * sizeof(BOOL));
  1937. id *seenValues = malloc(2 * sizeof(id));
  1938. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, id aValue, BOOL *stop) {
  1939. XCTAssertLessThan(idx, 2U);
  1940. seenKeys[idx] = aKey;
  1941. seenValues[idx] = aValue;
  1942. XCTAssertNotEqual(stop, NULL);
  1943. ++idx;
  1944. }];
  1945. for (int i = 0; i < 2; ++i) {
  1946. BOOL foundKey = NO;
  1947. for (int j = 0; (j < 2) && !foundKey; ++j) {
  1948. if (kKeys[i] == seenKeys[j]) {
  1949. foundKey = YES;
  1950. XCTAssertEqualObjects(kValues[i], seenValues[j], @"i = %d, j = %d", i, j);
  1951. }
  1952. }
  1953. XCTAssertTrue(foundKey, @"i = %d", i);
  1954. }
  1955. free(seenKeys);
  1956. free(seenValues);
  1957. // Stopping the enumeration.
  1958. idx = 0;
  1959. [dict enumerateKeysAndValuesUsingBlock:^(BOOL aKey, id aValue, BOOL *stop) {
  1960. #pragma unused(aKey, aValue)
  1961. if (idx == 0) *stop = YES;
  1962. XCTAssertNotEqual(idx, 2U);
  1963. ++idx;
  1964. }];
  1965. [dict release];
  1966. }
  1967. - (void)testEquality {
  1968. const BOOL kKeys1[] = { YES, NO };
  1969. const BOOL kKeys2[] = { NO, YES };
  1970. const id kValues1[] = { @"abc", @"def" };
  1971. const id kValues2[] = { @"def", @"abc" };
  1972. const id kValues3[] = { @"def" };
  1973. GPBBoolObjectDictionary *dict1 =
  1974. [[GPBBoolObjectDictionary alloc] initWithValues:kValues1
  1975. forKeys:kKeys1
  1976. count:GPBARRAYSIZE(kValues1)];
  1977. XCTAssertNotNil(dict1);
  1978. GPBBoolObjectDictionary *dict1prime =
  1979. [[GPBBoolObjectDictionary alloc] initWithValues:kValues1
  1980. forKeys:kKeys1
  1981. count:GPBARRAYSIZE(kValues1)];
  1982. XCTAssertNotNil(dict1prime);
  1983. GPBBoolObjectDictionary *dict2 =
  1984. [[GPBBoolObjectDictionary alloc] initWithValues:kValues2
  1985. forKeys:kKeys1
  1986. count:GPBARRAYSIZE(kValues2)];
  1987. XCTAssertNotNil(dict2);
  1988. GPBBoolObjectDictionary *dict3 =
  1989. [[GPBBoolObjectDictionary alloc] initWithValues:kValues1
  1990. forKeys:kKeys2
  1991. count:GPBARRAYSIZE(kValues1)];
  1992. XCTAssertNotNil(dict3);
  1993. GPBBoolObjectDictionary *dict4 =
  1994. [[GPBBoolObjectDictionary alloc] initWithValues:kValues3
  1995. forKeys:kKeys1
  1996. count:GPBARRAYSIZE(kValues3)];
  1997. XCTAssertNotNil(dict4);
  1998. // 1/1Prime should be different objects, but equal.
  1999. XCTAssertNotEqual(dict1, dict1prime);
  2000. XCTAssertEqualObjects(dict1, dict1prime);
  2001. // Equal, so they must have same hash.
  2002. XCTAssertEqual([dict1 hash], [dict1prime hash]);
  2003. // 2 is save keys, different values; not equal.
  2004. XCTAssertNotEqualObjects(dict1, dict2);
  2005. // 3 is different keys, samae values; not equal.
  2006. XCTAssertNotEqualObjects(dict1, dict3);
  2007. // 4 Fewer pairs; not equal
  2008. XCTAssertNotEqualObjects(dict1, dict4);
  2009. [dict1 release];
  2010. [dict1prime release];
  2011. [dict2 release];
  2012. [dict3 release];
  2013. [dict4 release];
  2014. }
  2015. - (void)testCopy {
  2016. const BOOL kKeys[] = { YES, NO };
  2017. const id kValues[] = { @"abc", @"def" };
  2018. GPBBoolObjectDictionary *dict =
  2019. [[GPBBoolObjectDictionary alloc] initWithValues:kValues
  2020. forKeys:kKeys
  2021. count:GPBARRAYSIZE(kValues)];
  2022. XCTAssertNotNil(dict);
  2023. GPBBoolObjectDictionary *dict2 = [dict copy];
  2024. XCTAssertNotNil(dict2);
  2025. // Should be new object but equal.
  2026. XCTAssertNotEqual(dict, dict2);
  2027. XCTAssertEqualObjects(dict, dict2);
  2028. XCTAssertTrue([dict2 isKindOfClass:[GPBBoolObjectDictionary class]]);
  2029. [dict2 release];
  2030. [dict release];
  2031. }
  2032. - (void)testDictionaryFromDictionary {
  2033. const BOOL kKeys[] = { YES, NO };
  2034. const id kValues[] = { @"abc", @"def" };
  2035. GPBBoolObjectDictionary *dict =
  2036. [[GPBBoolObjectDictionary alloc] initWithValues:kValues
  2037. forKeys:kKeys
  2038. count:GPBARRAYSIZE(kValues)];
  2039. XCTAssertNotNil(dict);
  2040. GPBBoolObjectDictionary *dict2 =
  2041. [GPBBoolObjectDictionary dictionaryWithDictionary:dict];
  2042. XCTAssertNotNil(dict2);
  2043. // Should be new pointer, but equal objects.
  2044. XCTAssertNotEqual(dict, dict2);
  2045. XCTAssertEqualObjects(dict, dict2);
  2046. [dict release];
  2047. }
  2048. - (void)testAdds {
  2049. GPBBoolObjectDictionary *dict = [GPBBoolObjectDictionary dictionary];
  2050. XCTAssertNotNil(dict);
  2051. XCTAssertEqual(dict.count, 0U);
  2052. [dict setValue:@"abc" forKey:YES];
  2053. XCTAssertEqual(dict.count, 1U);
  2054. const BOOL kKeys[] = { NO };
  2055. const id kValues[] = { @"def" };
  2056. GPBBoolObjectDictionary *dict2 =
  2057. [[GPBBoolObjectDictionary alloc] initWithValues:kValues
  2058. forKeys:kKeys
  2059. count:GPBARRAYSIZE(kValues)];
  2060. XCTAssertNotNil(dict2);
  2061. [dict addEntriesFromDictionary:dict2];
  2062. XCTAssertEqual(dict.count, 2U);
  2063. XCTAssertEqualObjects([dict valueForKey:YES], @"abc");
  2064. XCTAssertEqualObjects([dict valueForKey:NO], @"def");
  2065. [dict2 release];
  2066. }
  2067. - (void)testRemove {
  2068. const BOOL kKeys[] = { YES, NO};
  2069. const id kValues[] = { @"abc", @"def" };
  2070. GPBBoolObjectDictionary *dict =
  2071. [[GPBBoolObjectDictionary alloc] initWithValues:kValues
  2072. forKeys:kKeys
  2073. count:GPBARRAYSIZE(kValues)];
  2074. XCTAssertNotNil(dict);
  2075. XCTAssertEqual(dict.count, 2U);
  2076. [dict removeValueForKey:NO];
  2077. XCTAssertEqual(dict.count, 1U);
  2078. XCTAssertEqualObjects([dict valueForKey:YES], @"abc");
  2079. XCTAssertNil([dict valueForKey:NO]);
  2080. // Remove again does nothing.
  2081. [dict removeValueForKey:NO];
  2082. XCTAssertEqual(dict.count, 1U);
  2083. XCTAssertEqualObjects([dict valueForKey:YES], @"abc");
  2084. XCTAssertNil([dict valueForKey:NO]);
  2085. [dict removeAll];
  2086. XCTAssertEqual(dict.count, 0U);
  2087. XCTAssertNil([dict valueForKey:YES]);
  2088. XCTAssertNil([dict valueForKey:NO]);
  2089. [dict release];
  2090. }
  2091. - (void)testInplaceMutation {
  2092. const BOOL kKeys[] = { YES, NO };
  2093. const id kValues[] = { @"abc", @"def" };
  2094. GPBBoolObjectDictionary *dict =
  2095. [[GPBBoolObjectDictionary alloc] initWithValues:kValues
  2096. forKeys:kKeys
  2097. count:GPBARRAYSIZE(kValues)];
  2098. XCTAssertNotNil(dict);
  2099. XCTAssertEqual(dict.count, 2U);
  2100. XCTAssertEqualObjects([dict valueForKey:YES], @"abc");
  2101. XCTAssertEqualObjects([dict valueForKey:NO], @"def");
  2102. [dict setValue:@"def" forKey:YES];
  2103. XCTAssertEqual(dict.count, 2U);
  2104. XCTAssertEqualObjects([dict valueForKey:YES], @"def");
  2105. XCTAssertEqualObjects([dict valueForKey:NO], @"def");
  2106. [dict setValue:@"abc" forKey:NO];
  2107. XCTAssertEqual(dict.count, 2U);
  2108. XCTAssertEqualObjects([dict valueForKey:YES], @"def");
  2109. XCTAssertEqualObjects([dict valueForKey:NO], @"abc");
  2110. const BOOL kKeys2[] = { NO, YES };
  2111. const id kValues2[] = { @"def", @"abc" };
  2112. GPBBoolObjectDictionary *dict2 =
  2113. [[GPBBoolObjectDictionary alloc] initWithValues:kValues2
  2114. forKeys:kKeys2
  2115. count:GPBARRAYSIZE(kValues2)];
  2116. XCTAssertNotNil(dict2);
  2117. [dict addEntriesFromDictionary:dict2];
  2118. XCTAssertEqual(dict.count, 2U);
  2119. XCTAssertEqualObjects([dict valueForKey:YES], @"abc");
  2120. XCTAssertEqualObjects([dict valueForKey:NO], @"def");
  2121. [dict2 release];
  2122. [dict release];
  2123. }
  2124. @end
  2125. //%PDDM-EXPAND-END (8 expansions)