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 TESTS_FOR_POD_VALUES(String, NSString, *, Objects, @"foo", @"bar", @"baz", @"mumble") 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 GPBStringEnumDictionary (TestingTweak) 49ffe3c632Sopenharmony_ci- (instancetype)initWithEnums:(const int32_t [])values 50ffe3c632Sopenharmony_ci forKeys:(const NSString * [])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 GPBStringEnumDictionary (TestingTweak) 67ffe3c632Sopenharmony_ci- (instancetype)initWithEnums:(const int32_t [])values 68ffe3c632Sopenharmony_ci forKeys:(const NSString * [])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 - String -> UInt32 79ffe3c632Sopenharmony_ci 80ffe3c632Sopenharmony_ci@interface GPBStringUInt32DictionaryTests : XCTestCase 81ffe3c632Sopenharmony_ci@end 82ffe3c632Sopenharmony_ci 83ffe3c632Sopenharmony_ci@implementation GPBStringUInt32DictionaryTests 84ffe3c632Sopenharmony_ci 85ffe3c632Sopenharmony_ci- (void)testEmpty { 86ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict = [[GPBStringUInt32Dictionary alloc] init]; 87ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 88ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 89ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"foo"]); 90ffe3c632Sopenharmony_ci [dict enumerateKeysAndUInt32sUsingBlock:^(NSString *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 GPBStringUInt32Dictionary *dict = [[GPBStringUInt32Dictionary alloc] init]; 99ffe3c632Sopenharmony_ci [dict setUInt32:100U forKey:@"foo"]; 100ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 101ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 102ffe3c632Sopenharmony_ci uint32_t value; 103ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"foo"]); 104ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 105ffe3c632Sopenharmony_ci XCTAssertEqual(value, 100U); 106ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"bar"]); 107ffe3c632Sopenharmony_ci [dict enumerateKeysAndUInt32sUsingBlock:^(NSString *aKey, uint32_t aValue, BOOL *stop) { 108ffe3c632Sopenharmony_ci XCTAssertEqualObjects(aKey, @"foo"); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 117ffe3c632Sopenharmony_ci const uint32_t kValues[] = { 100U, 101U, 102U }; 118ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict = 119ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary 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:@"foo"]); 126ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 127ffe3c632Sopenharmony_ci XCTAssertEqual(value, 100U); 128ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"bar"]); 129ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"bar"]); 130ffe3c632Sopenharmony_ci XCTAssertEqual(value, 101U); 131ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 132ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 133ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 134ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"mumble"]); 135ffe3c632Sopenharmony_ci 136ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 137ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 138ffe3c632Sopenharmony_ci uint32_t *seenValues = malloc(3 * sizeof(uint32_t)); 139ffe3c632Sopenharmony_ci [dict enumerateKeysAndUInt32sUsingBlock:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 172ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringUInt32Dictionary *dict1 = 177ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary alloc] initWithUInt32s:kValues1 178ffe3c632Sopenharmony_ci forKeys:kKeys1 179ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 180ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 181ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict1prime = 182ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary alloc] initWithUInt32s:kValues1 183ffe3c632Sopenharmony_ci forKeys:kKeys1 184ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 185ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 186ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict2 = 187ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary alloc] initWithUInt32s:kValues2 188ffe3c632Sopenharmony_ci forKeys:kKeys1 189ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 190ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 191ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict3 = 192ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary alloc] initWithUInt32s:kValues1 193ffe3c632Sopenharmony_ci forKeys:kKeys2 194ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 195ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 196ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict4 = 197ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 226ffe3c632Sopenharmony_ci const uint32_t kValues[] = { 100U, 101U, 102U, 103U }; 227ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict = 228ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary alloc] initWithUInt32s:kValues 229ffe3c632Sopenharmony_ci forKeys:kKeys 230ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 231ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 232ffe3c632Sopenharmony_ci 233ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *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:[GPBStringUInt32Dictionary class]]); 240ffe3c632Sopenharmony_ci 241ffe3c632Sopenharmony_ci [dict2 release]; 242ffe3c632Sopenharmony_ci [dict release]; 243ffe3c632Sopenharmony_ci} 244ffe3c632Sopenharmony_ci 245ffe3c632Sopenharmony_ci- (void)testDictionaryFromDictionary { 246ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 247ffe3c632Sopenharmony_ci const uint32_t kValues[] = { 100U, 101U, 102U, 103U }; 248ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict = 249ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary alloc] initWithUInt32s:kValues 250ffe3c632Sopenharmony_ci forKeys:kKeys 251ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 252ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 253ffe3c632Sopenharmony_ci 254ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict2 = 255ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary 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 GPBStringUInt32Dictionary *dict = [[GPBStringUInt32Dictionary alloc] init]; 267ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 268ffe3c632Sopenharmony_ci 269ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 270ffe3c632Sopenharmony_ci [dict setUInt32:100U forKey:@"foo"]; 271ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 272ffe3c632Sopenharmony_ci 273ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"bar", @"baz", @"mumble" }; 274ffe3c632Sopenharmony_ci const uint32_t kValues[] = { 101U, 102U, 103U }; 275ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict2 = 276ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary 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:@"foo"]); 285ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 286ffe3c632Sopenharmony_ci XCTAssertEqual(value, 100U); 287ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"bar"]); 288ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"bar"]); 289ffe3c632Sopenharmony_ci XCTAssertEqual(value, 101U); 290ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 291ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 292ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 293ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"mumble"]); 294ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"mumble"]); 295ffe3c632Sopenharmony_ci XCTAssertEqual(value, 103U); 296ffe3c632Sopenharmony_ci [dict2 release]; 297ffe3c632Sopenharmony_ci [dict release]; 298ffe3c632Sopenharmony_ci} 299ffe3c632Sopenharmony_ci 300ffe3c632Sopenharmony_ci- (void)testRemove { 301ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 302ffe3c632Sopenharmony_ci const uint32_t kValues[] = { 100U, 101U, 102U, 103U }; 303ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict = 304ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary 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:@"bar"]; 311ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 312ffe3c632Sopenharmony_ci uint32_t value; 313ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"foo"]); 314ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 315ffe3c632Sopenharmony_ci XCTAssertEqual(value, 100U); 316ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"bar"]); 317ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 318ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 319ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 320ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"mumble"]); 321ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"mumble"]); 322ffe3c632Sopenharmony_ci XCTAssertEqual(value, 103U); 323ffe3c632Sopenharmony_ci 324ffe3c632Sopenharmony_ci // Remove again does nothing. 325ffe3c632Sopenharmony_ci [dict removeUInt32ForKey:@"bar"]; 326ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 327ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"foo"]); 328ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 329ffe3c632Sopenharmony_ci XCTAssertEqual(value, 100U); 330ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"bar"]); 331ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 332ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 333ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 334ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"mumble"]); 335ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"mumble"]); 336ffe3c632Sopenharmony_ci XCTAssertEqual(value, 103U); 337ffe3c632Sopenharmony_ci 338ffe3c632Sopenharmony_ci [dict removeUInt32ForKey:@"mumble"]; 339ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 340ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"foo"]); 341ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 342ffe3c632Sopenharmony_ci XCTAssertEqual(value, 100U); 343ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"bar"]); 344ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 345ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 346ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 347ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"mumble"]); 348ffe3c632Sopenharmony_ci 349ffe3c632Sopenharmony_ci [dict removeAll]; 350ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 351ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"foo"]); 352ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"bar"]); 353ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"baz"]); 354ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt32:NULL forKey:@"mumble"]); 355ffe3c632Sopenharmony_ci [dict release]; 356ffe3c632Sopenharmony_ci} 357ffe3c632Sopenharmony_ci 358ffe3c632Sopenharmony_ci- (void)testInplaceMutation { 359ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 360ffe3c632Sopenharmony_ci const uint32_t kValues[] = { 100U, 101U, 102U, 103U }; 361ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict = 362ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary 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:@"foo"]); 369ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 370ffe3c632Sopenharmony_ci XCTAssertEqual(value, 100U); 371ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"bar"]); 372ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"bar"]); 373ffe3c632Sopenharmony_ci XCTAssertEqual(value, 101U); 374ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 375ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 376ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 377ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"mumble"]); 378ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"mumble"]); 379ffe3c632Sopenharmony_ci XCTAssertEqual(value, 103U); 380ffe3c632Sopenharmony_ci 381ffe3c632Sopenharmony_ci [dict setUInt32:103U forKey:@"foo"]; 382ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 383ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"foo"]); 384ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 385ffe3c632Sopenharmony_ci XCTAssertEqual(value, 103U); 386ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"bar"]); 387ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"bar"]); 388ffe3c632Sopenharmony_ci XCTAssertEqual(value, 101U); 389ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 390ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 391ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 392ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"mumble"]); 393ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"mumble"]); 394ffe3c632Sopenharmony_ci XCTAssertEqual(value, 103U); 395ffe3c632Sopenharmony_ci 396ffe3c632Sopenharmony_ci [dict setUInt32:101U forKey:@"mumble"]; 397ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 398ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"foo"]); 399ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 400ffe3c632Sopenharmony_ci XCTAssertEqual(value, 103U); 401ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"bar"]); 402ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"bar"]); 403ffe3c632Sopenharmony_ci XCTAssertEqual(value, 101U); 404ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 405ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 406ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 407ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"mumble"]); 408ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"mumble"]); 409ffe3c632Sopenharmony_ci XCTAssertEqual(value, 101U); 410ffe3c632Sopenharmony_ci 411ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 412ffe3c632Sopenharmony_ci const uint32_t kValues2[] = { 102U, 100U }; 413ffe3c632Sopenharmony_ci GPBStringUInt32Dictionary *dict2 = 414ffe3c632Sopenharmony_ci [[GPBStringUInt32Dictionary 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:@"foo"]); 421ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"foo"]); 422ffe3c632Sopenharmony_ci XCTAssertEqual(value, 103U); 423ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"bar"]); 424ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"bar"]); 425ffe3c632Sopenharmony_ci XCTAssertEqual(value, 102U); 426ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"baz"]); 427ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"baz"]); 428ffe3c632Sopenharmony_ci XCTAssertEqual(value, 100U); 429ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:NULL forKey:@"mumble"]); 430ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt32:&value forKey:@"mumble"]); 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 - String -> Int32 440ffe3c632Sopenharmony_ci 441ffe3c632Sopenharmony_ci@interface GPBStringInt32DictionaryTests : XCTestCase 442ffe3c632Sopenharmony_ci@end 443ffe3c632Sopenharmony_ci 444ffe3c632Sopenharmony_ci@implementation GPBStringInt32DictionaryTests 445ffe3c632Sopenharmony_ci 446ffe3c632Sopenharmony_ci- (void)testEmpty { 447ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict = [[GPBStringInt32Dictionary alloc] init]; 448ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 449ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 450ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"foo"]); 451ffe3c632Sopenharmony_ci [dict enumerateKeysAndInt32sUsingBlock:^(NSString *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 GPBStringInt32Dictionary *dict = [[GPBStringInt32Dictionary alloc] init]; 460ffe3c632Sopenharmony_ci [dict setInt32:200 forKey:@"foo"]; 461ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 462ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 463ffe3c632Sopenharmony_ci int32_t value; 464ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"foo"]); 465ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 466ffe3c632Sopenharmony_ci XCTAssertEqual(value, 200); 467ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"bar"]); 468ffe3c632Sopenharmony_ci [dict enumerateKeysAndInt32sUsingBlock:^(NSString *aKey, int32_t aValue, BOOL *stop) { 469ffe3c632Sopenharmony_ci XCTAssertEqualObjects(aKey, @"foo"); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 478ffe3c632Sopenharmony_ci const int32_t kValues[] = { 200, 201, 202 }; 479ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict = 480ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary 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:@"foo"]); 487ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 488ffe3c632Sopenharmony_ci XCTAssertEqual(value, 200); 489ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"bar"]); 490ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"bar"]); 491ffe3c632Sopenharmony_ci XCTAssertEqual(value, 201); 492ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 493ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 494ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 495ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"mumble"]); 496ffe3c632Sopenharmony_ci 497ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 498ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 499ffe3c632Sopenharmony_ci int32_t *seenValues = malloc(3 * sizeof(int32_t)); 500ffe3c632Sopenharmony_ci [dict enumerateKeysAndInt32sUsingBlock:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 533ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringInt32Dictionary *dict1 = 538ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary alloc] initWithInt32s:kValues1 539ffe3c632Sopenharmony_ci forKeys:kKeys1 540ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 541ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 542ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict1prime = 543ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary alloc] initWithInt32s:kValues1 544ffe3c632Sopenharmony_ci forKeys:kKeys1 545ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 546ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 547ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict2 = 548ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary alloc] initWithInt32s:kValues2 549ffe3c632Sopenharmony_ci forKeys:kKeys1 550ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 551ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 552ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict3 = 553ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary alloc] initWithInt32s:kValues1 554ffe3c632Sopenharmony_ci forKeys:kKeys2 555ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 556ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 557ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict4 = 558ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 587ffe3c632Sopenharmony_ci const int32_t kValues[] = { 200, 201, 202, 203 }; 588ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict = 589ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary alloc] initWithInt32s:kValues 590ffe3c632Sopenharmony_ci forKeys:kKeys 591ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 592ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 593ffe3c632Sopenharmony_ci 594ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *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:[GPBStringInt32Dictionary class]]); 601ffe3c632Sopenharmony_ci 602ffe3c632Sopenharmony_ci [dict2 release]; 603ffe3c632Sopenharmony_ci [dict release]; 604ffe3c632Sopenharmony_ci} 605ffe3c632Sopenharmony_ci 606ffe3c632Sopenharmony_ci- (void)testDictionaryFromDictionary { 607ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 608ffe3c632Sopenharmony_ci const int32_t kValues[] = { 200, 201, 202, 203 }; 609ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict = 610ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary alloc] initWithInt32s:kValues 611ffe3c632Sopenharmony_ci forKeys:kKeys 612ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 613ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 614ffe3c632Sopenharmony_ci 615ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict2 = 616ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary 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 GPBStringInt32Dictionary *dict = [[GPBStringInt32Dictionary alloc] init]; 628ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 629ffe3c632Sopenharmony_ci 630ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 631ffe3c632Sopenharmony_ci [dict setInt32:200 forKey:@"foo"]; 632ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 633ffe3c632Sopenharmony_ci 634ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"bar", @"baz", @"mumble" }; 635ffe3c632Sopenharmony_ci const int32_t kValues[] = { 201, 202, 203 }; 636ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict2 = 637ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary 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:@"foo"]); 646ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 647ffe3c632Sopenharmony_ci XCTAssertEqual(value, 200); 648ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"bar"]); 649ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"bar"]); 650ffe3c632Sopenharmony_ci XCTAssertEqual(value, 201); 651ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 652ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 653ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 654ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"mumble"]); 655ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"mumble"]); 656ffe3c632Sopenharmony_ci XCTAssertEqual(value, 203); 657ffe3c632Sopenharmony_ci [dict2 release]; 658ffe3c632Sopenharmony_ci [dict release]; 659ffe3c632Sopenharmony_ci} 660ffe3c632Sopenharmony_ci 661ffe3c632Sopenharmony_ci- (void)testRemove { 662ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 663ffe3c632Sopenharmony_ci const int32_t kValues[] = { 200, 201, 202, 203 }; 664ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict = 665ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary 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:@"bar"]; 672ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 673ffe3c632Sopenharmony_ci int32_t value; 674ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"foo"]); 675ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 676ffe3c632Sopenharmony_ci XCTAssertEqual(value, 200); 677ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"bar"]); 678ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 679ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 680ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 681ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"mumble"]); 682ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"mumble"]); 683ffe3c632Sopenharmony_ci XCTAssertEqual(value, 203); 684ffe3c632Sopenharmony_ci 685ffe3c632Sopenharmony_ci // Remove again does nothing. 686ffe3c632Sopenharmony_ci [dict removeInt32ForKey:@"bar"]; 687ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 688ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"foo"]); 689ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 690ffe3c632Sopenharmony_ci XCTAssertEqual(value, 200); 691ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"bar"]); 692ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 693ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 694ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 695ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"mumble"]); 696ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"mumble"]); 697ffe3c632Sopenharmony_ci XCTAssertEqual(value, 203); 698ffe3c632Sopenharmony_ci 699ffe3c632Sopenharmony_ci [dict removeInt32ForKey:@"mumble"]; 700ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 701ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"foo"]); 702ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 703ffe3c632Sopenharmony_ci XCTAssertEqual(value, 200); 704ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"bar"]); 705ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 706ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 707ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 708ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"mumble"]); 709ffe3c632Sopenharmony_ci 710ffe3c632Sopenharmony_ci [dict removeAll]; 711ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 712ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"foo"]); 713ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"bar"]); 714ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"baz"]); 715ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt32:NULL forKey:@"mumble"]); 716ffe3c632Sopenharmony_ci [dict release]; 717ffe3c632Sopenharmony_ci} 718ffe3c632Sopenharmony_ci 719ffe3c632Sopenharmony_ci- (void)testInplaceMutation { 720ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 721ffe3c632Sopenharmony_ci const int32_t kValues[] = { 200, 201, 202, 203 }; 722ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict = 723ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary 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:@"foo"]); 730ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 731ffe3c632Sopenharmony_ci XCTAssertEqual(value, 200); 732ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"bar"]); 733ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"bar"]); 734ffe3c632Sopenharmony_ci XCTAssertEqual(value, 201); 735ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 736ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 737ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 738ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"mumble"]); 739ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"mumble"]); 740ffe3c632Sopenharmony_ci XCTAssertEqual(value, 203); 741ffe3c632Sopenharmony_ci 742ffe3c632Sopenharmony_ci [dict setInt32:203 forKey:@"foo"]; 743ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 744ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"foo"]); 745ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 746ffe3c632Sopenharmony_ci XCTAssertEqual(value, 203); 747ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"bar"]); 748ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"bar"]); 749ffe3c632Sopenharmony_ci XCTAssertEqual(value, 201); 750ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 751ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 752ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 753ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"mumble"]); 754ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"mumble"]); 755ffe3c632Sopenharmony_ci XCTAssertEqual(value, 203); 756ffe3c632Sopenharmony_ci 757ffe3c632Sopenharmony_ci [dict setInt32:201 forKey:@"mumble"]; 758ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 759ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"foo"]); 760ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 761ffe3c632Sopenharmony_ci XCTAssertEqual(value, 203); 762ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"bar"]); 763ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"bar"]); 764ffe3c632Sopenharmony_ci XCTAssertEqual(value, 201); 765ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 766ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 767ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 768ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"mumble"]); 769ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"mumble"]); 770ffe3c632Sopenharmony_ci XCTAssertEqual(value, 201); 771ffe3c632Sopenharmony_ci 772ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 773ffe3c632Sopenharmony_ci const int32_t kValues2[] = { 202, 200 }; 774ffe3c632Sopenharmony_ci GPBStringInt32Dictionary *dict2 = 775ffe3c632Sopenharmony_ci [[GPBStringInt32Dictionary 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:@"foo"]); 782ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"foo"]); 783ffe3c632Sopenharmony_ci XCTAssertEqual(value, 203); 784ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"bar"]); 785ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"bar"]); 786ffe3c632Sopenharmony_ci XCTAssertEqual(value, 202); 787ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"baz"]); 788ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"baz"]); 789ffe3c632Sopenharmony_ci XCTAssertEqual(value, 200); 790ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:NULL forKey:@"mumble"]); 791ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt32:&value forKey:@"mumble"]); 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 - String -> UInt64 801ffe3c632Sopenharmony_ci 802ffe3c632Sopenharmony_ci@interface GPBStringUInt64DictionaryTests : XCTestCase 803ffe3c632Sopenharmony_ci@end 804ffe3c632Sopenharmony_ci 805ffe3c632Sopenharmony_ci@implementation GPBStringUInt64DictionaryTests 806ffe3c632Sopenharmony_ci 807ffe3c632Sopenharmony_ci- (void)testEmpty { 808ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict = [[GPBStringUInt64Dictionary alloc] init]; 809ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 810ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 811ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"foo"]); 812ffe3c632Sopenharmony_ci [dict enumerateKeysAndUInt64sUsingBlock:^(NSString *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 GPBStringUInt64Dictionary *dict = [[GPBStringUInt64Dictionary alloc] init]; 821ffe3c632Sopenharmony_ci [dict setUInt64:300U forKey:@"foo"]; 822ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 823ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 824ffe3c632Sopenharmony_ci uint64_t value; 825ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"foo"]); 826ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 827ffe3c632Sopenharmony_ci XCTAssertEqual(value, 300U); 828ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"bar"]); 829ffe3c632Sopenharmony_ci [dict enumerateKeysAndUInt64sUsingBlock:^(NSString *aKey, uint64_t aValue, BOOL *stop) { 830ffe3c632Sopenharmony_ci XCTAssertEqualObjects(aKey, @"foo"); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 839ffe3c632Sopenharmony_ci const uint64_t kValues[] = { 300U, 301U, 302U }; 840ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict = 841ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary 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:@"foo"]); 848ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 849ffe3c632Sopenharmony_ci XCTAssertEqual(value, 300U); 850ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"bar"]); 851ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"bar"]); 852ffe3c632Sopenharmony_ci XCTAssertEqual(value, 301U); 853ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 854ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 855ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 856ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"mumble"]); 857ffe3c632Sopenharmony_ci 858ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 859ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 860ffe3c632Sopenharmony_ci uint64_t *seenValues = malloc(3 * sizeof(uint64_t)); 861ffe3c632Sopenharmony_ci [dict enumerateKeysAndUInt64sUsingBlock:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 894ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringUInt64Dictionary *dict1 = 899ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary alloc] initWithUInt64s:kValues1 900ffe3c632Sopenharmony_ci forKeys:kKeys1 901ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 902ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 903ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict1prime = 904ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary alloc] initWithUInt64s:kValues1 905ffe3c632Sopenharmony_ci forKeys:kKeys1 906ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 907ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 908ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict2 = 909ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary alloc] initWithUInt64s:kValues2 910ffe3c632Sopenharmony_ci forKeys:kKeys1 911ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 912ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 913ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict3 = 914ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary alloc] initWithUInt64s:kValues1 915ffe3c632Sopenharmony_ci forKeys:kKeys2 916ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 917ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 918ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict4 = 919ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 948ffe3c632Sopenharmony_ci const uint64_t kValues[] = { 300U, 301U, 302U, 303U }; 949ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict = 950ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary alloc] initWithUInt64s:kValues 951ffe3c632Sopenharmony_ci forKeys:kKeys 952ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 953ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 954ffe3c632Sopenharmony_ci 955ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *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:[GPBStringUInt64Dictionary class]]); 962ffe3c632Sopenharmony_ci 963ffe3c632Sopenharmony_ci [dict2 release]; 964ffe3c632Sopenharmony_ci [dict release]; 965ffe3c632Sopenharmony_ci} 966ffe3c632Sopenharmony_ci 967ffe3c632Sopenharmony_ci- (void)testDictionaryFromDictionary { 968ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 969ffe3c632Sopenharmony_ci const uint64_t kValues[] = { 300U, 301U, 302U, 303U }; 970ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict = 971ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary alloc] initWithUInt64s:kValues 972ffe3c632Sopenharmony_ci forKeys:kKeys 973ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 974ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 975ffe3c632Sopenharmony_ci 976ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict2 = 977ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary 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 GPBStringUInt64Dictionary *dict = [[GPBStringUInt64Dictionary alloc] init]; 989ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 990ffe3c632Sopenharmony_ci 991ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 992ffe3c632Sopenharmony_ci [dict setUInt64:300U forKey:@"foo"]; 993ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 994ffe3c632Sopenharmony_ci 995ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"bar", @"baz", @"mumble" }; 996ffe3c632Sopenharmony_ci const uint64_t kValues[] = { 301U, 302U, 303U }; 997ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict2 = 998ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary 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:@"foo"]); 1007ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 1008ffe3c632Sopenharmony_ci XCTAssertEqual(value, 300U); 1009ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"bar"]); 1010ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"bar"]); 1011ffe3c632Sopenharmony_ci XCTAssertEqual(value, 301U); 1012ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 1013ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 1014ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 1015ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"mumble"]); 1016ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"mumble"]); 1017ffe3c632Sopenharmony_ci XCTAssertEqual(value, 303U); 1018ffe3c632Sopenharmony_ci [dict2 release]; 1019ffe3c632Sopenharmony_ci [dict release]; 1020ffe3c632Sopenharmony_ci} 1021ffe3c632Sopenharmony_ci 1022ffe3c632Sopenharmony_ci- (void)testRemove { 1023ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1024ffe3c632Sopenharmony_ci const uint64_t kValues[] = { 300U, 301U, 302U, 303U }; 1025ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict = 1026ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary 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:@"bar"]; 1033ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 1034ffe3c632Sopenharmony_ci uint64_t value; 1035ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"foo"]); 1036ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 1037ffe3c632Sopenharmony_ci XCTAssertEqual(value, 300U); 1038ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"bar"]); 1039ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 1040ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 1041ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 1042ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"mumble"]); 1043ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"mumble"]); 1044ffe3c632Sopenharmony_ci XCTAssertEqual(value, 303U); 1045ffe3c632Sopenharmony_ci 1046ffe3c632Sopenharmony_ci // Remove again does nothing. 1047ffe3c632Sopenharmony_ci [dict removeUInt64ForKey:@"bar"]; 1048ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 1049ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"foo"]); 1050ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 1051ffe3c632Sopenharmony_ci XCTAssertEqual(value, 300U); 1052ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"bar"]); 1053ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 1054ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 1055ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 1056ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"mumble"]); 1057ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"mumble"]); 1058ffe3c632Sopenharmony_ci XCTAssertEqual(value, 303U); 1059ffe3c632Sopenharmony_ci 1060ffe3c632Sopenharmony_ci [dict removeUInt64ForKey:@"mumble"]; 1061ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 1062ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"foo"]); 1063ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 1064ffe3c632Sopenharmony_ci XCTAssertEqual(value, 300U); 1065ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"bar"]); 1066ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 1067ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 1068ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 1069ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"mumble"]); 1070ffe3c632Sopenharmony_ci 1071ffe3c632Sopenharmony_ci [dict removeAll]; 1072ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 1073ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"foo"]); 1074ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"bar"]); 1075ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"baz"]); 1076ffe3c632Sopenharmony_ci XCTAssertFalse([dict getUInt64:NULL forKey:@"mumble"]); 1077ffe3c632Sopenharmony_ci [dict release]; 1078ffe3c632Sopenharmony_ci} 1079ffe3c632Sopenharmony_ci 1080ffe3c632Sopenharmony_ci- (void)testInplaceMutation { 1081ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1082ffe3c632Sopenharmony_ci const uint64_t kValues[] = { 300U, 301U, 302U, 303U }; 1083ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict = 1084ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary 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:@"foo"]); 1091ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 1092ffe3c632Sopenharmony_ci XCTAssertEqual(value, 300U); 1093ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"bar"]); 1094ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"bar"]); 1095ffe3c632Sopenharmony_ci XCTAssertEqual(value, 301U); 1096ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 1097ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 1098ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 1099ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"mumble"]); 1100ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"mumble"]); 1101ffe3c632Sopenharmony_ci XCTAssertEqual(value, 303U); 1102ffe3c632Sopenharmony_ci 1103ffe3c632Sopenharmony_ci [dict setUInt64:303U forKey:@"foo"]; 1104ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 1105ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"foo"]); 1106ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 1107ffe3c632Sopenharmony_ci XCTAssertEqual(value, 303U); 1108ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"bar"]); 1109ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"bar"]); 1110ffe3c632Sopenharmony_ci XCTAssertEqual(value, 301U); 1111ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 1112ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 1113ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 1114ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"mumble"]); 1115ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"mumble"]); 1116ffe3c632Sopenharmony_ci XCTAssertEqual(value, 303U); 1117ffe3c632Sopenharmony_ci 1118ffe3c632Sopenharmony_ci [dict setUInt64:301U forKey:@"mumble"]; 1119ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 1120ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"foo"]); 1121ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 1122ffe3c632Sopenharmony_ci XCTAssertEqual(value, 303U); 1123ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"bar"]); 1124ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"bar"]); 1125ffe3c632Sopenharmony_ci XCTAssertEqual(value, 301U); 1126ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 1127ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 1128ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 1129ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"mumble"]); 1130ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"mumble"]); 1131ffe3c632Sopenharmony_ci XCTAssertEqual(value, 301U); 1132ffe3c632Sopenharmony_ci 1133ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 1134ffe3c632Sopenharmony_ci const uint64_t kValues2[] = { 302U, 300U }; 1135ffe3c632Sopenharmony_ci GPBStringUInt64Dictionary *dict2 = 1136ffe3c632Sopenharmony_ci [[GPBStringUInt64Dictionary 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:@"foo"]); 1143ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"foo"]); 1144ffe3c632Sopenharmony_ci XCTAssertEqual(value, 303U); 1145ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"bar"]); 1146ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"bar"]); 1147ffe3c632Sopenharmony_ci XCTAssertEqual(value, 302U); 1148ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"baz"]); 1149ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"baz"]); 1150ffe3c632Sopenharmony_ci XCTAssertEqual(value, 300U); 1151ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:NULL forKey:@"mumble"]); 1152ffe3c632Sopenharmony_ci XCTAssertTrue([dict getUInt64:&value forKey:@"mumble"]); 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 - String -> Int64 1162ffe3c632Sopenharmony_ci 1163ffe3c632Sopenharmony_ci@interface GPBStringInt64DictionaryTests : XCTestCase 1164ffe3c632Sopenharmony_ci@end 1165ffe3c632Sopenharmony_ci 1166ffe3c632Sopenharmony_ci@implementation GPBStringInt64DictionaryTests 1167ffe3c632Sopenharmony_ci 1168ffe3c632Sopenharmony_ci- (void)testEmpty { 1169ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict = [[GPBStringInt64Dictionary alloc] init]; 1170ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1171ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 1172ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"foo"]); 1173ffe3c632Sopenharmony_ci [dict enumerateKeysAndInt64sUsingBlock:^(NSString *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 GPBStringInt64Dictionary *dict = [[GPBStringInt64Dictionary alloc] init]; 1182ffe3c632Sopenharmony_ci [dict setInt64:400 forKey:@"foo"]; 1183ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1184ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 1185ffe3c632Sopenharmony_ci int64_t value; 1186ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"foo"]); 1187ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1188ffe3c632Sopenharmony_ci XCTAssertEqual(value, 400); 1189ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"bar"]); 1190ffe3c632Sopenharmony_ci [dict enumerateKeysAndInt64sUsingBlock:^(NSString *aKey, int64_t aValue, BOOL *stop) { 1191ffe3c632Sopenharmony_ci XCTAssertEqualObjects(aKey, @"foo"); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 1200ffe3c632Sopenharmony_ci const int64_t kValues[] = { 400, 401, 402 }; 1201ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict = 1202ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary 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:@"foo"]); 1209ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1210ffe3c632Sopenharmony_ci XCTAssertEqual(value, 400); 1211ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"bar"]); 1212ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"bar"]); 1213ffe3c632Sopenharmony_ci XCTAssertEqual(value, 401); 1214ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1215ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1216ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1217ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"mumble"]); 1218ffe3c632Sopenharmony_ci 1219ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 1220ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 1221ffe3c632Sopenharmony_ci int64_t *seenValues = malloc(3 * sizeof(int64_t)); 1222ffe3c632Sopenharmony_ci [dict enumerateKeysAndInt64sUsingBlock:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 1255ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringInt64Dictionary *dict1 = 1260ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary alloc] initWithInt64s:kValues1 1261ffe3c632Sopenharmony_ci forKeys:kKeys1 1262ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 1263ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 1264ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict1prime = 1265ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary alloc] initWithInt64s:kValues1 1266ffe3c632Sopenharmony_ci forKeys:kKeys1 1267ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 1268ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 1269ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict2 = 1270ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary alloc] initWithInt64s:kValues2 1271ffe3c632Sopenharmony_ci forKeys:kKeys1 1272ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 1273ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 1274ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict3 = 1275ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary alloc] initWithInt64s:kValues1 1276ffe3c632Sopenharmony_ci forKeys:kKeys2 1277ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 1278ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 1279ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict4 = 1280ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1309ffe3c632Sopenharmony_ci const int64_t kValues[] = { 400, 401, 402, 403 }; 1310ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict = 1311ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary alloc] initWithInt64s:kValues 1312ffe3c632Sopenharmony_ci forKeys:kKeys 1313ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 1314ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1315ffe3c632Sopenharmony_ci 1316ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *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:[GPBStringInt64Dictionary class]]); 1323ffe3c632Sopenharmony_ci 1324ffe3c632Sopenharmony_ci [dict2 release]; 1325ffe3c632Sopenharmony_ci [dict release]; 1326ffe3c632Sopenharmony_ci} 1327ffe3c632Sopenharmony_ci 1328ffe3c632Sopenharmony_ci- (void)testDictionaryFromDictionary { 1329ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1330ffe3c632Sopenharmony_ci const int64_t kValues[] = { 400, 401, 402, 403 }; 1331ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict = 1332ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary alloc] initWithInt64s:kValues 1333ffe3c632Sopenharmony_ci forKeys:kKeys 1334ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 1335ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1336ffe3c632Sopenharmony_ci 1337ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict2 = 1338ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary 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 GPBStringInt64Dictionary *dict = [[GPBStringInt64Dictionary alloc] init]; 1350ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1351ffe3c632Sopenharmony_ci 1352ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 1353ffe3c632Sopenharmony_ci [dict setInt64:400 forKey:@"foo"]; 1354ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 1355ffe3c632Sopenharmony_ci 1356ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"bar", @"baz", @"mumble" }; 1357ffe3c632Sopenharmony_ci const int64_t kValues[] = { 401, 402, 403 }; 1358ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict2 = 1359ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary 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:@"foo"]); 1368ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1369ffe3c632Sopenharmony_ci XCTAssertEqual(value, 400); 1370ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"bar"]); 1371ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"bar"]); 1372ffe3c632Sopenharmony_ci XCTAssertEqual(value, 401); 1373ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1374ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1375ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1376ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"mumble"]); 1377ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"mumble"]); 1378ffe3c632Sopenharmony_ci XCTAssertEqual(value, 403); 1379ffe3c632Sopenharmony_ci [dict2 release]; 1380ffe3c632Sopenharmony_ci [dict release]; 1381ffe3c632Sopenharmony_ci} 1382ffe3c632Sopenharmony_ci 1383ffe3c632Sopenharmony_ci- (void)testRemove { 1384ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1385ffe3c632Sopenharmony_ci const int64_t kValues[] = { 400, 401, 402, 403 }; 1386ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict = 1387ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary 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:@"bar"]; 1394ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 1395ffe3c632Sopenharmony_ci int64_t value; 1396ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"foo"]); 1397ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1398ffe3c632Sopenharmony_ci XCTAssertEqual(value, 400); 1399ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"bar"]); 1400ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1401ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1402ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1403ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"mumble"]); 1404ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"mumble"]); 1405ffe3c632Sopenharmony_ci XCTAssertEqual(value, 403); 1406ffe3c632Sopenharmony_ci 1407ffe3c632Sopenharmony_ci // Remove again does nothing. 1408ffe3c632Sopenharmony_ci [dict removeInt64ForKey:@"bar"]; 1409ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 1410ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"foo"]); 1411ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1412ffe3c632Sopenharmony_ci XCTAssertEqual(value, 400); 1413ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"bar"]); 1414ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1415ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1416ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1417ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"mumble"]); 1418ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"mumble"]); 1419ffe3c632Sopenharmony_ci XCTAssertEqual(value, 403); 1420ffe3c632Sopenharmony_ci 1421ffe3c632Sopenharmony_ci [dict removeInt64ForKey:@"mumble"]; 1422ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 1423ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"foo"]); 1424ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1425ffe3c632Sopenharmony_ci XCTAssertEqual(value, 400); 1426ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"bar"]); 1427ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1428ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1429ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1430ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"mumble"]); 1431ffe3c632Sopenharmony_ci 1432ffe3c632Sopenharmony_ci [dict removeAll]; 1433ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 1434ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"foo"]); 1435ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"bar"]); 1436ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"baz"]); 1437ffe3c632Sopenharmony_ci XCTAssertFalse([dict getInt64:NULL forKey:@"mumble"]); 1438ffe3c632Sopenharmony_ci [dict release]; 1439ffe3c632Sopenharmony_ci} 1440ffe3c632Sopenharmony_ci 1441ffe3c632Sopenharmony_ci- (void)testInplaceMutation { 1442ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1443ffe3c632Sopenharmony_ci const int64_t kValues[] = { 400, 401, 402, 403 }; 1444ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict = 1445ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary 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:@"foo"]); 1452ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1453ffe3c632Sopenharmony_ci XCTAssertEqual(value, 400); 1454ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"bar"]); 1455ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"bar"]); 1456ffe3c632Sopenharmony_ci XCTAssertEqual(value, 401); 1457ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1458ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1459ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1460ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"mumble"]); 1461ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"mumble"]); 1462ffe3c632Sopenharmony_ci XCTAssertEqual(value, 403); 1463ffe3c632Sopenharmony_ci 1464ffe3c632Sopenharmony_ci [dict setInt64:403 forKey:@"foo"]; 1465ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 1466ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"foo"]); 1467ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1468ffe3c632Sopenharmony_ci XCTAssertEqual(value, 403); 1469ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"bar"]); 1470ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"bar"]); 1471ffe3c632Sopenharmony_ci XCTAssertEqual(value, 401); 1472ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1473ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1474ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1475ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"mumble"]); 1476ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"mumble"]); 1477ffe3c632Sopenharmony_ci XCTAssertEqual(value, 403); 1478ffe3c632Sopenharmony_ci 1479ffe3c632Sopenharmony_ci [dict setInt64:401 forKey:@"mumble"]; 1480ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 1481ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"foo"]); 1482ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1483ffe3c632Sopenharmony_ci XCTAssertEqual(value, 403); 1484ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"bar"]); 1485ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"bar"]); 1486ffe3c632Sopenharmony_ci XCTAssertEqual(value, 401); 1487ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1488ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1489ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1490ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"mumble"]); 1491ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"mumble"]); 1492ffe3c632Sopenharmony_ci XCTAssertEqual(value, 401); 1493ffe3c632Sopenharmony_ci 1494ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 1495ffe3c632Sopenharmony_ci const int64_t kValues2[] = { 402, 400 }; 1496ffe3c632Sopenharmony_ci GPBStringInt64Dictionary *dict2 = 1497ffe3c632Sopenharmony_ci [[GPBStringInt64Dictionary 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:@"foo"]); 1504ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"foo"]); 1505ffe3c632Sopenharmony_ci XCTAssertEqual(value, 403); 1506ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"bar"]); 1507ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"bar"]); 1508ffe3c632Sopenharmony_ci XCTAssertEqual(value, 402); 1509ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"baz"]); 1510ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"baz"]); 1511ffe3c632Sopenharmony_ci XCTAssertEqual(value, 400); 1512ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:NULL forKey:@"mumble"]); 1513ffe3c632Sopenharmony_ci XCTAssertTrue([dict getInt64:&value forKey:@"mumble"]); 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 - String -> Bool 1523ffe3c632Sopenharmony_ci 1524ffe3c632Sopenharmony_ci@interface GPBStringBoolDictionaryTests : XCTestCase 1525ffe3c632Sopenharmony_ci@end 1526ffe3c632Sopenharmony_ci 1527ffe3c632Sopenharmony_ci@implementation GPBStringBoolDictionaryTests 1528ffe3c632Sopenharmony_ci 1529ffe3c632Sopenharmony_ci- (void)testEmpty { 1530ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict = [[GPBStringBoolDictionary alloc] init]; 1531ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1532ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 1533ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"foo"]); 1534ffe3c632Sopenharmony_ci [dict enumerateKeysAndBoolsUsingBlock:^(NSString *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 GPBStringBoolDictionary *dict = [[GPBStringBoolDictionary alloc] init]; 1543ffe3c632Sopenharmony_ci [dict setBool:YES forKey:@"foo"]; 1544ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1545ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 1546ffe3c632Sopenharmony_ci BOOL value; 1547ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"foo"]); 1548ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1549ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1550ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"bar"]); 1551ffe3c632Sopenharmony_ci [dict enumerateKeysAndBoolsUsingBlock:^(NSString *aKey, BOOL aValue, BOOL *stop) { 1552ffe3c632Sopenharmony_ci XCTAssertEqualObjects(aKey, @"foo"); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 1561ffe3c632Sopenharmony_ci const BOOL kValues[] = { YES, YES, NO }; 1562ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict = 1563ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary 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:@"foo"]); 1570ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1571ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1572ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"bar"]); 1573ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"bar"]); 1574ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1575ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1576ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1577ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1578ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"mumble"]); 1579ffe3c632Sopenharmony_ci 1580ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 1581ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 1582ffe3c632Sopenharmony_ci BOOL *seenValues = malloc(3 * sizeof(BOOL)); 1583ffe3c632Sopenharmony_ci [dict enumerateKeysAndBoolsUsingBlock:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 1616ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringBoolDictionary *dict1 = 1621ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary alloc] initWithBools:kValues1 1622ffe3c632Sopenharmony_ci forKeys:kKeys1 1623ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 1624ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 1625ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict1prime = 1626ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary alloc] initWithBools:kValues1 1627ffe3c632Sopenharmony_ci forKeys:kKeys1 1628ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 1629ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 1630ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict2 = 1631ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary alloc] initWithBools:kValues2 1632ffe3c632Sopenharmony_ci forKeys:kKeys1 1633ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 1634ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 1635ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict3 = 1636ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary alloc] initWithBools:kValues1 1637ffe3c632Sopenharmony_ci forKeys:kKeys2 1638ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 1639ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 1640ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict4 = 1641ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1670ffe3c632Sopenharmony_ci const BOOL kValues[] = { YES, YES, NO, NO }; 1671ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict = 1672ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary alloc] initWithBools:kValues 1673ffe3c632Sopenharmony_ci forKeys:kKeys 1674ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 1675ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1676ffe3c632Sopenharmony_ci 1677ffe3c632Sopenharmony_ci GPBStringBoolDictionary *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:[GPBStringBoolDictionary class]]); 1684ffe3c632Sopenharmony_ci 1685ffe3c632Sopenharmony_ci [dict2 release]; 1686ffe3c632Sopenharmony_ci [dict release]; 1687ffe3c632Sopenharmony_ci} 1688ffe3c632Sopenharmony_ci 1689ffe3c632Sopenharmony_ci- (void)testDictionaryFromDictionary { 1690ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1691ffe3c632Sopenharmony_ci const BOOL kValues[] = { YES, YES, NO, NO }; 1692ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict = 1693ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary alloc] initWithBools:kValues 1694ffe3c632Sopenharmony_ci forKeys:kKeys 1695ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 1696ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1697ffe3c632Sopenharmony_ci 1698ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict2 = 1699ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary 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 GPBStringBoolDictionary *dict = [[GPBStringBoolDictionary alloc] init]; 1711ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1712ffe3c632Sopenharmony_ci 1713ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 1714ffe3c632Sopenharmony_ci [dict setBool:YES forKey:@"foo"]; 1715ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 1716ffe3c632Sopenharmony_ci 1717ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"bar", @"baz", @"mumble" }; 1718ffe3c632Sopenharmony_ci const BOOL kValues[] = { YES, NO, NO }; 1719ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict2 = 1720ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary 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:@"foo"]); 1729ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1730ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1731ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"bar"]); 1732ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"bar"]); 1733ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1734ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1735ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1736ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1737ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"mumble"]); 1738ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"mumble"]); 1739ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1740ffe3c632Sopenharmony_ci [dict2 release]; 1741ffe3c632Sopenharmony_ci [dict release]; 1742ffe3c632Sopenharmony_ci} 1743ffe3c632Sopenharmony_ci 1744ffe3c632Sopenharmony_ci- (void)testRemove { 1745ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1746ffe3c632Sopenharmony_ci const BOOL kValues[] = { YES, YES, NO, NO }; 1747ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict = 1748ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary 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:@"bar"]; 1755ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 1756ffe3c632Sopenharmony_ci BOOL value; 1757ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"foo"]); 1758ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1759ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1760ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"bar"]); 1761ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1762ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1763ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1764ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"mumble"]); 1765ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"mumble"]); 1766ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1767ffe3c632Sopenharmony_ci 1768ffe3c632Sopenharmony_ci // Remove again does nothing. 1769ffe3c632Sopenharmony_ci [dict removeBoolForKey:@"bar"]; 1770ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 1771ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"foo"]); 1772ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1773ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1774ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"bar"]); 1775ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1776ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1777ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1778ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"mumble"]); 1779ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"mumble"]); 1780ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1781ffe3c632Sopenharmony_ci 1782ffe3c632Sopenharmony_ci [dict removeBoolForKey:@"mumble"]; 1783ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 1784ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"foo"]); 1785ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1786ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1787ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"bar"]); 1788ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1789ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1790ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1791ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"mumble"]); 1792ffe3c632Sopenharmony_ci 1793ffe3c632Sopenharmony_ci [dict removeAll]; 1794ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 1795ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"foo"]); 1796ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"bar"]); 1797ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"baz"]); 1798ffe3c632Sopenharmony_ci XCTAssertFalse([dict getBool:NULL forKey:@"mumble"]); 1799ffe3c632Sopenharmony_ci [dict release]; 1800ffe3c632Sopenharmony_ci} 1801ffe3c632Sopenharmony_ci 1802ffe3c632Sopenharmony_ci- (void)testInplaceMutation { 1803ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 1804ffe3c632Sopenharmony_ci const BOOL kValues[] = { YES, YES, NO, NO }; 1805ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict = 1806ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary 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:@"foo"]); 1813ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1814ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1815ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"bar"]); 1816ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"bar"]); 1817ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1818ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1819ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1820ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1821ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"mumble"]); 1822ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"mumble"]); 1823ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1824ffe3c632Sopenharmony_ci 1825ffe3c632Sopenharmony_ci [dict setBool:NO forKey:@"foo"]; 1826ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 1827ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"foo"]); 1828ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1829ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1830ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"bar"]); 1831ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"bar"]); 1832ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1833ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1834ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1835ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1836ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"mumble"]); 1837ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"mumble"]); 1838ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1839ffe3c632Sopenharmony_ci 1840ffe3c632Sopenharmony_ci [dict setBool:YES forKey:@"mumble"]; 1841ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 1842ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"foo"]); 1843ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1844ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1845ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"bar"]); 1846ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"bar"]); 1847ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1848ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1849ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1850ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1851ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"mumble"]); 1852ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"mumble"]); 1853ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1854ffe3c632Sopenharmony_ci 1855ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 1856ffe3c632Sopenharmony_ci const BOOL kValues2[] = { NO, YES }; 1857ffe3c632Sopenharmony_ci GPBStringBoolDictionary *dict2 = 1858ffe3c632Sopenharmony_ci [[GPBStringBoolDictionary 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:@"foo"]); 1865ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"foo"]); 1866ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1867ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"bar"]); 1868ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"bar"]); 1869ffe3c632Sopenharmony_ci XCTAssertEqual(value, NO); 1870ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"baz"]); 1871ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"baz"]); 1872ffe3c632Sopenharmony_ci XCTAssertEqual(value, YES); 1873ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:NULL forKey:@"mumble"]); 1874ffe3c632Sopenharmony_ci XCTAssertTrue([dict getBool:&value forKey:@"mumble"]); 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 - String -> Float 1884ffe3c632Sopenharmony_ci 1885ffe3c632Sopenharmony_ci@interface GPBStringFloatDictionaryTests : XCTestCase 1886ffe3c632Sopenharmony_ci@end 1887ffe3c632Sopenharmony_ci 1888ffe3c632Sopenharmony_ci@implementation GPBStringFloatDictionaryTests 1889ffe3c632Sopenharmony_ci 1890ffe3c632Sopenharmony_ci- (void)testEmpty { 1891ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict = [[GPBStringFloatDictionary alloc] init]; 1892ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1893ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 1894ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"foo"]); 1895ffe3c632Sopenharmony_ci [dict enumerateKeysAndFloatsUsingBlock:^(NSString *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 GPBStringFloatDictionary *dict = [[GPBStringFloatDictionary alloc] init]; 1904ffe3c632Sopenharmony_ci [dict setFloat:500.f forKey:@"foo"]; 1905ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 1906ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 1907ffe3c632Sopenharmony_ci float value; 1908ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"foo"]); 1909ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 1910ffe3c632Sopenharmony_ci XCTAssertEqual(value, 500.f); 1911ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"bar"]); 1912ffe3c632Sopenharmony_ci [dict enumerateKeysAndFloatsUsingBlock:^(NSString *aKey, float aValue, BOOL *stop) { 1913ffe3c632Sopenharmony_ci XCTAssertEqualObjects(aKey, @"foo"); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 1922ffe3c632Sopenharmony_ci const float kValues[] = { 500.f, 501.f, 502.f }; 1923ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict = 1924ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary 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:@"foo"]); 1931ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 1932ffe3c632Sopenharmony_ci XCTAssertEqual(value, 500.f); 1933ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"bar"]); 1934ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"bar"]); 1935ffe3c632Sopenharmony_ci XCTAssertEqual(value, 501.f); 1936ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 1937ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 1938ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 1939ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"mumble"]); 1940ffe3c632Sopenharmony_ci 1941ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 1942ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 1943ffe3c632Sopenharmony_ci float *seenValues = malloc(3 * sizeof(float)); 1944ffe3c632Sopenharmony_ci [dict enumerateKeysAndFloatsUsingBlock:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 1977ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringFloatDictionary *dict1 = 1982ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary alloc] initWithFloats:kValues1 1983ffe3c632Sopenharmony_ci forKeys:kKeys1 1984ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 1985ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 1986ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict1prime = 1987ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary alloc] initWithFloats:kValues1 1988ffe3c632Sopenharmony_ci forKeys:kKeys1 1989ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 1990ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 1991ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict2 = 1992ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary alloc] initWithFloats:kValues2 1993ffe3c632Sopenharmony_ci forKeys:kKeys1 1994ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 1995ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 1996ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict3 = 1997ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary alloc] initWithFloats:kValues1 1998ffe3c632Sopenharmony_ci forKeys:kKeys2 1999ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 2000ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 2001ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict4 = 2002ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2031ffe3c632Sopenharmony_ci const float kValues[] = { 500.f, 501.f, 502.f, 503.f }; 2032ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict = 2033ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary alloc] initWithFloats:kValues 2034ffe3c632Sopenharmony_ci forKeys:kKeys 2035ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 2036ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2037ffe3c632Sopenharmony_ci 2038ffe3c632Sopenharmony_ci GPBStringFloatDictionary *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:[GPBStringFloatDictionary class]]); 2045ffe3c632Sopenharmony_ci 2046ffe3c632Sopenharmony_ci [dict2 release]; 2047ffe3c632Sopenharmony_ci [dict release]; 2048ffe3c632Sopenharmony_ci} 2049ffe3c632Sopenharmony_ci 2050ffe3c632Sopenharmony_ci- (void)testDictionaryFromDictionary { 2051ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2052ffe3c632Sopenharmony_ci const float kValues[] = { 500.f, 501.f, 502.f, 503.f }; 2053ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict = 2054ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary alloc] initWithFloats:kValues 2055ffe3c632Sopenharmony_ci forKeys:kKeys 2056ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 2057ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2058ffe3c632Sopenharmony_ci 2059ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict2 = 2060ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary 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 GPBStringFloatDictionary *dict = [[GPBStringFloatDictionary alloc] init]; 2072ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2073ffe3c632Sopenharmony_ci 2074ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 2075ffe3c632Sopenharmony_ci [dict setFloat:500.f forKey:@"foo"]; 2076ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 2077ffe3c632Sopenharmony_ci 2078ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"bar", @"baz", @"mumble" }; 2079ffe3c632Sopenharmony_ci const float kValues[] = { 501.f, 502.f, 503.f }; 2080ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict2 = 2081ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary 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:@"foo"]); 2090ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 2091ffe3c632Sopenharmony_ci XCTAssertEqual(value, 500.f); 2092ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"bar"]); 2093ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"bar"]); 2094ffe3c632Sopenharmony_ci XCTAssertEqual(value, 501.f); 2095ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 2096ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 2097ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 2098ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"mumble"]); 2099ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"mumble"]); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2107ffe3c632Sopenharmony_ci const float kValues[] = { 500.f, 501.f, 502.f, 503.f }; 2108ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict = 2109ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary 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:@"bar"]; 2116ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 2117ffe3c632Sopenharmony_ci float value; 2118ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"foo"]); 2119ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 2120ffe3c632Sopenharmony_ci XCTAssertEqual(value, 500.f); 2121ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"bar"]); 2122ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 2123ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 2124ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 2125ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"mumble"]); 2126ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"mumble"]); 2127ffe3c632Sopenharmony_ci XCTAssertEqual(value, 503.f); 2128ffe3c632Sopenharmony_ci 2129ffe3c632Sopenharmony_ci // Remove again does nothing. 2130ffe3c632Sopenharmony_ci [dict removeFloatForKey:@"bar"]; 2131ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 2132ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"foo"]); 2133ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 2134ffe3c632Sopenharmony_ci XCTAssertEqual(value, 500.f); 2135ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"bar"]); 2136ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 2137ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 2138ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 2139ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"mumble"]); 2140ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"mumble"]); 2141ffe3c632Sopenharmony_ci XCTAssertEqual(value, 503.f); 2142ffe3c632Sopenharmony_ci 2143ffe3c632Sopenharmony_ci [dict removeFloatForKey:@"mumble"]; 2144ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 2145ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"foo"]); 2146ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 2147ffe3c632Sopenharmony_ci XCTAssertEqual(value, 500.f); 2148ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"bar"]); 2149ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 2150ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 2151ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 2152ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"mumble"]); 2153ffe3c632Sopenharmony_ci 2154ffe3c632Sopenharmony_ci [dict removeAll]; 2155ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 2156ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"foo"]); 2157ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"bar"]); 2158ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"baz"]); 2159ffe3c632Sopenharmony_ci XCTAssertFalse([dict getFloat:NULL forKey:@"mumble"]); 2160ffe3c632Sopenharmony_ci [dict release]; 2161ffe3c632Sopenharmony_ci} 2162ffe3c632Sopenharmony_ci 2163ffe3c632Sopenharmony_ci- (void)testInplaceMutation { 2164ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2165ffe3c632Sopenharmony_ci const float kValues[] = { 500.f, 501.f, 502.f, 503.f }; 2166ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict = 2167ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary 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:@"foo"]); 2174ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 2175ffe3c632Sopenharmony_ci XCTAssertEqual(value, 500.f); 2176ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"bar"]); 2177ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"bar"]); 2178ffe3c632Sopenharmony_ci XCTAssertEqual(value, 501.f); 2179ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 2180ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 2181ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 2182ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"mumble"]); 2183ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"mumble"]); 2184ffe3c632Sopenharmony_ci XCTAssertEqual(value, 503.f); 2185ffe3c632Sopenharmony_ci 2186ffe3c632Sopenharmony_ci [dict setFloat:503.f forKey:@"foo"]; 2187ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 2188ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"foo"]); 2189ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 2190ffe3c632Sopenharmony_ci XCTAssertEqual(value, 503.f); 2191ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"bar"]); 2192ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"bar"]); 2193ffe3c632Sopenharmony_ci XCTAssertEqual(value, 501.f); 2194ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 2195ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 2196ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 2197ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"mumble"]); 2198ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"mumble"]); 2199ffe3c632Sopenharmony_ci XCTAssertEqual(value, 503.f); 2200ffe3c632Sopenharmony_ci 2201ffe3c632Sopenharmony_ci [dict setFloat:501.f forKey:@"mumble"]; 2202ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 2203ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"foo"]); 2204ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 2205ffe3c632Sopenharmony_ci XCTAssertEqual(value, 503.f); 2206ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"bar"]); 2207ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"bar"]); 2208ffe3c632Sopenharmony_ci XCTAssertEqual(value, 501.f); 2209ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 2210ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 2211ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 2212ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"mumble"]); 2213ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"mumble"]); 2214ffe3c632Sopenharmony_ci XCTAssertEqual(value, 501.f); 2215ffe3c632Sopenharmony_ci 2216ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 2217ffe3c632Sopenharmony_ci const float kValues2[] = { 502.f, 500.f }; 2218ffe3c632Sopenharmony_ci GPBStringFloatDictionary *dict2 = 2219ffe3c632Sopenharmony_ci [[GPBStringFloatDictionary 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:@"foo"]); 2226ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"foo"]); 2227ffe3c632Sopenharmony_ci XCTAssertEqual(value, 503.f); 2228ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"bar"]); 2229ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"bar"]); 2230ffe3c632Sopenharmony_ci XCTAssertEqual(value, 502.f); 2231ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"baz"]); 2232ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"baz"]); 2233ffe3c632Sopenharmony_ci XCTAssertEqual(value, 500.f); 2234ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:NULL forKey:@"mumble"]); 2235ffe3c632Sopenharmony_ci XCTAssertTrue([dict getFloat:&value forKey:@"mumble"]); 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 - String -> Double 2245ffe3c632Sopenharmony_ci 2246ffe3c632Sopenharmony_ci@interface GPBStringDoubleDictionaryTests : XCTestCase 2247ffe3c632Sopenharmony_ci@end 2248ffe3c632Sopenharmony_ci 2249ffe3c632Sopenharmony_ci@implementation GPBStringDoubleDictionaryTests 2250ffe3c632Sopenharmony_ci 2251ffe3c632Sopenharmony_ci- (void)testEmpty { 2252ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict = [[GPBStringDoubleDictionary alloc] init]; 2253ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2254ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 2255ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"foo"]); 2256ffe3c632Sopenharmony_ci [dict enumerateKeysAndDoublesUsingBlock:^(NSString *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 GPBStringDoubleDictionary *dict = [[GPBStringDoubleDictionary alloc] init]; 2265ffe3c632Sopenharmony_ci [dict setDouble:600. forKey:@"foo"]; 2266ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2267ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 2268ffe3c632Sopenharmony_ci double value; 2269ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"foo"]); 2270ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2271ffe3c632Sopenharmony_ci XCTAssertEqual(value, 600.); 2272ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"bar"]); 2273ffe3c632Sopenharmony_ci [dict enumerateKeysAndDoublesUsingBlock:^(NSString *aKey, double aValue, BOOL *stop) { 2274ffe3c632Sopenharmony_ci XCTAssertEqualObjects(aKey, @"foo"); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 2283ffe3c632Sopenharmony_ci const double kValues[] = { 600., 601., 602. }; 2284ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict = 2285ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary 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:@"foo"]); 2292ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2293ffe3c632Sopenharmony_ci XCTAssertEqual(value, 600.); 2294ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"bar"]); 2295ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"bar"]); 2296ffe3c632Sopenharmony_ci XCTAssertEqual(value, 601.); 2297ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2298ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2299ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2300ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"mumble"]); 2301ffe3c632Sopenharmony_ci 2302ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 2303ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 2304ffe3c632Sopenharmony_ci double *seenValues = malloc(3 * sizeof(double)); 2305ffe3c632Sopenharmony_ci [dict enumerateKeysAndDoublesUsingBlock:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 2338ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringDoubleDictionary *dict1 = 2343ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary alloc] initWithDoubles:kValues1 2344ffe3c632Sopenharmony_ci forKeys:kKeys1 2345ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 2346ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 2347ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict1prime = 2348ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary alloc] initWithDoubles:kValues1 2349ffe3c632Sopenharmony_ci forKeys:kKeys1 2350ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 2351ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 2352ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict2 = 2353ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary alloc] initWithDoubles:kValues2 2354ffe3c632Sopenharmony_ci forKeys:kKeys1 2355ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 2356ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 2357ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict3 = 2358ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary alloc] initWithDoubles:kValues1 2359ffe3c632Sopenharmony_ci forKeys:kKeys2 2360ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 2361ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 2362ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict4 = 2363ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2392ffe3c632Sopenharmony_ci const double kValues[] = { 600., 601., 602., 603. }; 2393ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict = 2394ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary alloc] initWithDoubles:kValues 2395ffe3c632Sopenharmony_ci forKeys:kKeys 2396ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 2397ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2398ffe3c632Sopenharmony_ci 2399ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *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:[GPBStringDoubleDictionary class]]); 2406ffe3c632Sopenharmony_ci 2407ffe3c632Sopenharmony_ci [dict2 release]; 2408ffe3c632Sopenharmony_ci [dict release]; 2409ffe3c632Sopenharmony_ci} 2410ffe3c632Sopenharmony_ci 2411ffe3c632Sopenharmony_ci- (void)testDictionaryFromDictionary { 2412ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2413ffe3c632Sopenharmony_ci const double kValues[] = { 600., 601., 602., 603. }; 2414ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict = 2415ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary alloc] initWithDoubles:kValues 2416ffe3c632Sopenharmony_ci forKeys:kKeys 2417ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 2418ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2419ffe3c632Sopenharmony_ci 2420ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict2 = 2421ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary 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 GPBStringDoubleDictionary *dict = [[GPBStringDoubleDictionary alloc] init]; 2433ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2434ffe3c632Sopenharmony_ci 2435ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 2436ffe3c632Sopenharmony_ci [dict setDouble:600. forKey:@"foo"]; 2437ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 2438ffe3c632Sopenharmony_ci 2439ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"bar", @"baz", @"mumble" }; 2440ffe3c632Sopenharmony_ci const double kValues[] = { 601., 602., 603. }; 2441ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict2 = 2442ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary 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:@"foo"]); 2451ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2452ffe3c632Sopenharmony_ci XCTAssertEqual(value, 600.); 2453ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"bar"]); 2454ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"bar"]); 2455ffe3c632Sopenharmony_ci XCTAssertEqual(value, 601.); 2456ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2457ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2458ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2459ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"mumble"]); 2460ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"mumble"]); 2461ffe3c632Sopenharmony_ci XCTAssertEqual(value, 603.); 2462ffe3c632Sopenharmony_ci [dict2 release]; 2463ffe3c632Sopenharmony_ci [dict release]; 2464ffe3c632Sopenharmony_ci} 2465ffe3c632Sopenharmony_ci 2466ffe3c632Sopenharmony_ci- (void)testRemove { 2467ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2468ffe3c632Sopenharmony_ci const double kValues[] = { 600., 601., 602., 603. }; 2469ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict = 2470ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary 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:@"bar"]; 2477ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 2478ffe3c632Sopenharmony_ci double value; 2479ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"foo"]); 2480ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2481ffe3c632Sopenharmony_ci XCTAssertEqual(value, 600.); 2482ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"bar"]); 2483ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2484ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2485ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2486ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"mumble"]); 2487ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"mumble"]); 2488ffe3c632Sopenharmony_ci XCTAssertEqual(value, 603.); 2489ffe3c632Sopenharmony_ci 2490ffe3c632Sopenharmony_ci // Remove again does nothing. 2491ffe3c632Sopenharmony_ci [dict removeDoubleForKey:@"bar"]; 2492ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 2493ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"foo"]); 2494ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2495ffe3c632Sopenharmony_ci XCTAssertEqual(value, 600.); 2496ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"bar"]); 2497ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2498ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2499ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2500ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"mumble"]); 2501ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"mumble"]); 2502ffe3c632Sopenharmony_ci XCTAssertEqual(value, 603.); 2503ffe3c632Sopenharmony_ci 2504ffe3c632Sopenharmony_ci [dict removeDoubleForKey:@"mumble"]; 2505ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 2506ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"foo"]); 2507ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2508ffe3c632Sopenharmony_ci XCTAssertEqual(value, 600.); 2509ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"bar"]); 2510ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2511ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2512ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2513ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"mumble"]); 2514ffe3c632Sopenharmony_ci 2515ffe3c632Sopenharmony_ci [dict removeAll]; 2516ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 2517ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"foo"]); 2518ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"bar"]); 2519ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"baz"]); 2520ffe3c632Sopenharmony_ci XCTAssertFalse([dict getDouble:NULL forKey:@"mumble"]); 2521ffe3c632Sopenharmony_ci [dict release]; 2522ffe3c632Sopenharmony_ci} 2523ffe3c632Sopenharmony_ci 2524ffe3c632Sopenharmony_ci- (void)testInplaceMutation { 2525ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2526ffe3c632Sopenharmony_ci const double kValues[] = { 600., 601., 602., 603. }; 2527ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict = 2528ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary 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:@"foo"]); 2535ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2536ffe3c632Sopenharmony_ci XCTAssertEqual(value, 600.); 2537ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"bar"]); 2538ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"bar"]); 2539ffe3c632Sopenharmony_ci XCTAssertEqual(value, 601.); 2540ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2541ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2542ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2543ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"mumble"]); 2544ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"mumble"]); 2545ffe3c632Sopenharmony_ci XCTAssertEqual(value, 603.); 2546ffe3c632Sopenharmony_ci 2547ffe3c632Sopenharmony_ci [dict setDouble:603. forKey:@"foo"]; 2548ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 2549ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"foo"]); 2550ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2551ffe3c632Sopenharmony_ci XCTAssertEqual(value, 603.); 2552ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"bar"]); 2553ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"bar"]); 2554ffe3c632Sopenharmony_ci XCTAssertEqual(value, 601.); 2555ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2556ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2557ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2558ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"mumble"]); 2559ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"mumble"]); 2560ffe3c632Sopenharmony_ci XCTAssertEqual(value, 603.); 2561ffe3c632Sopenharmony_ci 2562ffe3c632Sopenharmony_ci [dict setDouble:601. forKey:@"mumble"]; 2563ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 2564ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"foo"]); 2565ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2566ffe3c632Sopenharmony_ci XCTAssertEqual(value, 603.); 2567ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"bar"]); 2568ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"bar"]); 2569ffe3c632Sopenharmony_ci XCTAssertEqual(value, 601.); 2570ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2571ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2572ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2573ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"mumble"]); 2574ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"mumble"]); 2575ffe3c632Sopenharmony_ci XCTAssertEqual(value, 601.); 2576ffe3c632Sopenharmony_ci 2577ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 2578ffe3c632Sopenharmony_ci const double kValues2[] = { 602., 600. }; 2579ffe3c632Sopenharmony_ci GPBStringDoubleDictionary *dict2 = 2580ffe3c632Sopenharmony_ci [[GPBStringDoubleDictionary 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:@"foo"]); 2587ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"foo"]); 2588ffe3c632Sopenharmony_ci XCTAssertEqual(value, 603.); 2589ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"bar"]); 2590ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"bar"]); 2591ffe3c632Sopenharmony_ci XCTAssertEqual(value, 602.); 2592ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"baz"]); 2593ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"baz"]); 2594ffe3c632Sopenharmony_ci XCTAssertEqual(value, 600.); 2595ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:NULL forKey:@"mumble"]); 2596ffe3c632Sopenharmony_ci XCTAssertTrue([dict getDouble:&value forKey:@"mumble"]); 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 - String -> Enum 2606ffe3c632Sopenharmony_ci 2607ffe3c632Sopenharmony_ci@interface GPBStringEnumDictionaryTests : XCTestCase 2608ffe3c632Sopenharmony_ci@end 2609ffe3c632Sopenharmony_ci 2610ffe3c632Sopenharmony_ci@implementation GPBStringEnumDictionaryTests 2611ffe3c632Sopenharmony_ci 2612ffe3c632Sopenharmony_ci- (void)testEmpty { 2613ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = [[GPBStringEnumDictionary alloc] init]; 2614ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2615ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 2616ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"foo"]); 2617ffe3c632Sopenharmony_ci [dict enumerateKeysAndEnumsUsingBlock:^(NSString *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 GPBStringEnumDictionary *dict = [[GPBStringEnumDictionary alloc] init]; 2626ffe3c632Sopenharmony_ci [dict setEnum:700 forKey:@"foo"]; 2627ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2628ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 2629ffe3c632Sopenharmony_ci int32_t value; 2630ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 2631ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2632ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2633ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 2634ffe3c632Sopenharmony_ci [dict enumerateKeysAndEnumsUsingBlock:^(NSString *aKey, int32_t aValue, BOOL *stop) { 2635ffe3c632Sopenharmony_ci XCTAssertEqualObjects(aKey, @"foo"); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 2644ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 701, 702 }; 2645ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 2646ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"foo"]); 2653ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2654ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2655ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 2656ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 2657ffe3c632Sopenharmony_ci XCTAssertEqual(value, 701); 2658ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2659ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2660ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2661ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"mumble"]); 2662ffe3c632Sopenharmony_ci 2663ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 2664ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 2665ffe3c632Sopenharmony_ci int32_t *seenValues = malloc(3 * sizeof(int32_t)); 2666ffe3c632Sopenharmony_ci [dict enumerateKeysAndEnumsUsingBlock:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 2699ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringEnumDictionary *dict1 = 2704ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithEnums:kValues1 2705ffe3c632Sopenharmony_ci forKeys:kKeys1 2706ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 2707ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 2708ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict1prime = 2709ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithEnums:kValues1 2710ffe3c632Sopenharmony_ci forKeys:kKeys1 2711ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 2712ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 2713ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict2 = 2714ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithEnums:kValues2 2715ffe3c632Sopenharmony_ci forKeys:kKeys1 2716ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 2717ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 2718ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict3 = 2719ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithEnums:kValues1 2720ffe3c632Sopenharmony_ci forKeys:kKeys2 2721ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 2722ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 2723ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict4 = 2724ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2753ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 701, 702, 703 }; 2754ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 2755ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithEnums:kValues 2756ffe3c632Sopenharmony_ci forKeys:kKeys 2757ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 2758ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2759ffe3c632Sopenharmony_ci 2760ffe3c632Sopenharmony_ci GPBStringEnumDictionary *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:[GPBStringEnumDictionary class]]); 2767ffe3c632Sopenharmony_ci 2768ffe3c632Sopenharmony_ci [dict2 release]; 2769ffe3c632Sopenharmony_ci [dict release]; 2770ffe3c632Sopenharmony_ci} 2771ffe3c632Sopenharmony_ci 2772ffe3c632Sopenharmony_ci- (void)testDictionaryFromDictionary { 2773ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2774ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 701, 702, 703 }; 2775ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 2776ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithEnums:kValues 2777ffe3c632Sopenharmony_ci forKeys:kKeys 2778ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues)]; 2779ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2780ffe3c632Sopenharmony_ci 2781ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict2 = 2782ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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 GPBStringEnumDictionary *dict = [[GPBStringEnumDictionary alloc] init]; 2794ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 2795ffe3c632Sopenharmony_ci 2796ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 2797ffe3c632Sopenharmony_ci [dict setEnum:700 forKey:@"foo"]; 2798ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 2799ffe3c632Sopenharmony_ci 2800ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"bar", @"baz", @"mumble" }; 2801ffe3c632Sopenharmony_ci const int32_t kValues[] = { 701, 702, 703 }; 2802ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict2 = 2803ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"foo"]); 2812ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2813ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2814ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 2815ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 2816ffe3c632Sopenharmony_ci XCTAssertEqual(value, 701); 2817ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2818ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2819ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2820ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 2821ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 2822ffe3c632Sopenharmony_ci XCTAssertEqual(value, 703); 2823ffe3c632Sopenharmony_ci [dict2 release]; 2824ffe3c632Sopenharmony_ci [dict release]; 2825ffe3c632Sopenharmony_ci} 2826ffe3c632Sopenharmony_ci 2827ffe3c632Sopenharmony_ci- (void)testRemove { 2828ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2829ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 701, 702, 703 }; 2830ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 2831ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"bar"]; 2838ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 2839ffe3c632Sopenharmony_ci int32_t value; 2840ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 2841ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2842ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2843ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 2844ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2845ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2846ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2847ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 2848ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 2849ffe3c632Sopenharmony_ci XCTAssertEqual(value, 703); 2850ffe3c632Sopenharmony_ci 2851ffe3c632Sopenharmony_ci // Remove again does nothing. 2852ffe3c632Sopenharmony_ci [dict removeEnumForKey:@"bar"]; 2853ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 2854ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 2855ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2856ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2857ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 2858ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2859ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2860ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2861ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 2862ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 2863ffe3c632Sopenharmony_ci XCTAssertEqual(value, 703); 2864ffe3c632Sopenharmony_ci 2865ffe3c632Sopenharmony_ci [dict removeEnumForKey:@"mumble"]; 2866ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 2867ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 2868ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2869ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2870ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 2871ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2872ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2873ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2874ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"mumble"]); 2875ffe3c632Sopenharmony_ci 2876ffe3c632Sopenharmony_ci [dict removeAll]; 2877ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 2878ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"foo"]); 2879ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 2880ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"baz"]); 2881ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"mumble"]); 2882ffe3c632Sopenharmony_ci [dict release]; 2883ffe3c632Sopenharmony_ci} 2884ffe3c632Sopenharmony_ci 2885ffe3c632Sopenharmony_ci- (void)testInplaceMutation { 2886ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 2887ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 701, 702, 703 }; 2888ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 2889ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"foo"]); 2896ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2897ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2898ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 2899ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 2900ffe3c632Sopenharmony_ci XCTAssertEqual(value, 701); 2901ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2902ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2903ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2904ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 2905ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 2906ffe3c632Sopenharmony_ci XCTAssertEqual(value, 703); 2907ffe3c632Sopenharmony_ci 2908ffe3c632Sopenharmony_ci [dict setEnum:703 forKey:@"foo"]; 2909ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 2910ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 2911ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2912ffe3c632Sopenharmony_ci XCTAssertEqual(value, 703); 2913ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 2914ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 2915ffe3c632Sopenharmony_ci XCTAssertEqual(value, 701); 2916ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2917ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2918ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2919ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 2920ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 2921ffe3c632Sopenharmony_ci XCTAssertEqual(value, 703); 2922ffe3c632Sopenharmony_ci 2923ffe3c632Sopenharmony_ci [dict setEnum:701 forKey:@"mumble"]; 2924ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 2925ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 2926ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2927ffe3c632Sopenharmony_ci XCTAssertEqual(value, 703); 2928ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 2929ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 2930ffe3c632Sopenharmony_ci XCTAssertEqual(value, 701); 2931ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2932ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2933ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2934ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 2935ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 2936ffe3c632Sopenharmony_ci XCTAssertEqual(value, 701); 2937ffe3c632Sopenharmony_ci 2938ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 2939ffe3c632Sopenharmony_ci const int32_t kValues2[] = { 702, 700 }; 2940ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict2 = 2941ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"foo"]); 2948ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 2949ffe3c632Sopenharmony_ci XCTAssertEqual(value, 703); 2950ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 2951ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 2952ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2953ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 2954ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 2955ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2956ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 2957ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 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 - String -> Enum (Unknown Enums) 2967ffe3c632Sopenharmony_ci 2968ffe3c632Sopenharmony_ci@interface GPBStringEnumDictionaryUnknownEnumTests : XCTestCase 2969ffe3c632Sopenharmony_ci@end 2970ffe3c632Sopenharmony_ci 2971ffe3c632Sopenharmony_ci@implementation GPBStringEnumDictionaryUnknownEnumTests 2972ffe3c632Sopenharmony_ci 2973ffe3c632Sopenharmony_ci- (void)testRawBasics { 2974ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz" }; 2975ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 801, 702 }; 2976ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 2977ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"foo"]); 2986ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"foo"]); 2987ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 2988ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 2989ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 2990ffe3c632Sopenharmony_ci XCTAssertEqual(value, kGPBUnrecognizedEnumeratorValue); 2991ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"bar"]); 2992ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"bar"]); 2993ffe3c632Sopenharmony_ci XCTAssertEqual(value, 801); 2994ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"baz"]); 2995ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"baz"]); 2996ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 2997ffe3c632Sopenharmony_ci XCTAssertFalse([dict getRawValue:NULL forKey:@"mumble"]); 2998ffe3c632Sopenharmony_ci 2999ffe3c632Sopenharmony_ci __block NSUInteger idx = 0; 3000ffe3c632Sopenharmony_ci NSString **seenKeys = malloc(3 * sizeof(NSString*)); 3001ffe3c632Sopenharmony_ci int32_t *seenValues = malloc(3 * sizeof(int32_t)); 3002ffe3c632Sopenharmony_ci [dict enumerateKeysAndEnumsUsingBlock:^(NSString *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] isEqual: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:^(NSString *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] isEqual: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:^(NSString *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 NSString *kKeys1[] = { @"foo", @"bar", @"baz", @"mumble" }; 3057ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"foo", @"mumble" }; 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 GPBStringEnumDictionary *dict1 = 3062ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue 3063ffe3c632Sopenharmony_ci rawValues:kValues1 3064ffe3c632Sopenharmony_ci forKeys:kKeys1 3065ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 3066ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1); 3067ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict1prime = 3068ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue 3069ffe3c632Sopenharmony_ci rawValues:kValues1 3070ffe3c632Sopenharmony_ci forKeys:kKeys1 3071ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 3072ffe3c632Sopenharmony_ci XCTAssertNotNil(dict1prime); 3073ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict2 = 3074ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue 3075ffe3c632Sopenharmony_ci rawValues:kValues2 3076ffe3c632Sopenharmony_ci forKeys:kKeys1 3077ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues2)]; 3078ffe3c632Sopenharmony_ci XCTAssertNotNil(dict2); 3079ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict3 = 3080ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue 3081ffe3c632Sopenharmony_ci rawValues:kValues1 3082ffe3c632Sopenharmony_ci forKeys:kKeys2 3083ffe3c632Sopenharmony_ci count:GPBARRAYSIZE(kValues1)]; 3084ffe3c632Sopenharmony_ci XCTAssertNotNil(dict3); 3085ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict4 = 3086ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 3116ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 801, 702, 803 }; // Unknown 3117ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 3118ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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 GPBStringEnumDictionary *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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 3138ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 801, 702, 803 }; // Unknowns 3139ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 3140ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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 GPBStringEnumDictionary *dict2 = 3147ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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 GPBStringEnumDictionary *dict = 3160ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary alloc] initWithValidationFunction:TestingEnum_IsValidValue]; 3161ffe3c632Sopenharmony_ci XCTAssertNotNil(dict); 3162ffe3c632Sopenharmony_ci 3163ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 3164ffe3c632Sopenharmony_ci XCTAssertThrowsSpecificNamed([dict setEnum:801 forKey:@"bar"], // Unknown 3165ffe3c632Sopenharmony_ci NSException, NSInvalidArgumentException); 3166ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 3167ffe3c632Sopenharmony_ci [dict setRawValue:801 forKey:@"bar"]; // Unknown 3168ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 1U); 3169ffe3c632Sopenharmony_ci 3170ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"baz", @"mumble" }; 3171ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 702, 803 }; // Unknown 3172ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict2 = 3173ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"foo"]); 3182ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 3183ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 3184ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 3185ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 3186ffe3c632Sopenharmony_ci XCTAssertEqual(value, kGPBUnrecognizedEnumeratorValue); 3187ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"bar"]); 3188ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"bar"]); 3189ffe3c632Sopenharmony_ci XCTAssertEqual(value, 801); 3190ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 3191ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 3192ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3193ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 3194ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 3195ffe3c632Sopenharmony_ci XCTAssertEqual(value, kGPBUnrecognizedEnumeratorValue); 3196ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"mumble"]); 3197ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"mumble"]); 3198ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3199ffe3c632Sopenharmony_ci [dict2 release]; 3200ffe3c632Sopenharmony_ci [dict release]; 3201ffe3c632Sopenharmony_ci} 3202ffe3c632Sopenharmony_ci 3203ffe3c632Sopenharmony_ci- (void)testUnknownRemove { 3204ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 3205ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 801, 702, 803 }; // Unknowns 3206ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 3207ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"bar"]; 3215ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 3216ffe3c632Sopenharmony_ci int32_t value; 3217ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 3218ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 3219ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 3220ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 3221ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 3222ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 3223ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3224ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"mumble"]); 3225ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"mumble"]); 3226ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3227ffe3c632Sopenharmony_ci 3228ffe3c632Sopenharmony_ci // Remove again does nothing. 3229ffe3c632Sopenharmony_ci [dict removeEnumForKey:@"bar"]; 3230ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 3U); 3231ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 3232ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 3233ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 3234ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 3235ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 3236ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 3237ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3238ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"mumble"]); 3239ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"mumble"]); 3240ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3241ffe3c632Sopenharmony_ci 3242ffe3c632Sopenharmony_ci [dict removeEnumForKey:@"mumble"]; 3243ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 2U); 3244ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 3245ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 3246ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 3247ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 3248ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 3249ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 3250ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3251ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"mumble"]); 3252ffe3c632Sopenharmony_ci 3253ffe3c632Sopenharmony_ci [dict removeAll]; 3254ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 0U); 3255ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"foo"]); 3256ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"bar"]); 3257ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"baz"]); 3258ffe3c632Sopenharmony_ci XCTAssertFalse([dict getEnum:NULL forKey:@"mumble"]); 3259ffe3c632Sopenharmony_ci [dict release]; 3260ffe3c632Sopenharmony_ci} 3261ffe3c632Sopenharmony_ci 3262ffe3c632Sopenharmony_ci- (void)testInplaceMutationUnknowns { 3263ffe3c632Sopenharmony_ci const NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 3264ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 801, 702, 803 }; // Unknowns 3265ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 3266ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"foo"]); 3274ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 3275ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 3276ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"bar"]); 3277ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"bar"]); 3278ffe3c632Sopenharmony_ci XCTAssertEqual(value, 801); 3279ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 3280ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 3281ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3282ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"mumble"]); 3283ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"mumble"]); 3284ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3285ffe3c632Sopenharmony_ci 3286ffe3c632Sopenharmony_ci XCTAssertThrowsSpecificNamed([dict setEnum:803 forKey:@"foo"], // Unknown 3287ffe3c632Sopenharmony_ci NSException, NSInvalidArgumentException); 3288ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 3289ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"foo"]); 3290ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"foo"]); 3291ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 3292ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"bar"]); 3293ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"bar"]); 3294ffe3c632Sopenharmony_ci XCTAssertEqual(value, 801); 3295ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 3296ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 3297ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3298ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"mumble"]); 3299ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"mumble"]); 3300ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3301ffe3c632Sopenharmony_ci 3302ffe3c632Sopenharmony_ci [dict setRawValue:803 forKey:@"foo"]; // Unknown 3303ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 3304ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"foo"]); 3305ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"foo"]); 3306ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3307ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"bar"]); 3308ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"bar"]); 3309ffe3c632Sopenharmony_ci XCTAssertEqual(value, 801); 3310ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 3311ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 3312ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3313ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"mumble"]); 3314ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"mumble"]); 3315ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3316ffe3c632Sopenharmony_ci 3317ffe3c632Sopenharmony_ci [dict setRawValue:700 forKey:@"mumble"]; 3318ffe3c632Sopenharmony_ci XCTAssertEqual(dict.count, 4U); 3319ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"foo"]); 3320ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"foo"]); 3321ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3322ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"bar"]); 3323ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"bar"]); 3324ffe3c632Sopenharmony_ci XCTAssertEqual(value, 801); 3325ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"baz"]); 3326ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"baz"]); 3327ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3328ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 3329ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 3330ffe3c632Sopenharmony_ci XCTAssertEqual(value, 700); 3331ffe3c632Sopenharmony_ci 3332ffe3c632Sopenharmony_ci const NSString *kKeys2[] = { @"bar", @"baz" }; 3333ffe3c632Sopenharmony_ci const int32_t kValues2[] = { 702, 801 }; // Unknown 3334ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict2 = 3335ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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:@"foo"]); 3343ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"foo"]); 3344ffe3c632Sopenharmony_ci XCTAssertEqual(value, 803); 3345ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"bar"]); 3346ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"bar"]); 3347ffe3c632Sopenharmony_ci XCTAssertEqual(value, 702); 3348ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:NULL forKey:@"baz"]); 3349ffe3c632Sopenharmony_ci XCTAssertTrue([dict getRawValue:&value forKey:@"baz"]); 3350ffe3c632Sopenharmony_ci XCTAssertEqual(value, 801); 3351ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:NULL forKey:@"mumble"]); 3352ffe3c632Sopenharmony_ci XCTAssertTrue([dict getEnum:&value forKey:@"mumble"]); 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 NSString *kKeys[] = { @"foo", @"bar", @"baz", @"mumble" }; 3361ffe3c632Sopenharmony_ci const int32_t kValues[] = { 700, 801, 702, 803 }; 3362ffe3c632Sopenharmony_ci GPBStringEnumDictionary *dict = 3363ffe3c632Sopenharmony_ci [[GPBStringEnumDictionary 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 GPBStringEnumDictionary *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:[GPBStringEnumDictionary class]]); 3377ffe3c632Sopenharmony_ci 3378ffe3c632Sopenharmony_ci [dict2 release]; 3379ffe3c632Sopenharmony_ci [dict release]; 3380ffe3c632Sopenharmony_ci} 3381ffe3c632Sopenharmony_ci 3382ffe3c632Sopenharmony_ci@end 3383ffe3c632Sopenharmony_ci 3384ffe3c632Sopenharmony_ci// clang-format on 3385ffe3c632Sopenharmony_ci//%PDDM-EXPAND-END TESTS_FOR_POD_VALUES(String, NSString, *, Objects, @"foo", @"bar", @"baz", @"mumble") 3386ffe3c632Sopenharmony_ci 3387