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