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