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