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