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