PageRenderTime 114ms CodeModel.GetById 14ms app.highlight 97ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/AppKit/GTMIBArrayTest.m

http://macfuse.googlecode.com/
Objective C | 348 lines | 287 code | 43 blank | 18 comment | 12 complexity | bc3a6acbaeab3e225cdfe60bfce640aa MD5 | raw file
  1//
  2//  GTMIBArrayTest.m
  3//
  4//  Copyright 2009 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19
 20#import "GTMIBArray.h"
 21#import "GTMSenTestCase.h"
 22#import "GTMIBArrayTest.h"
 23
 24@interface GTMIBArrayTest : GTMTestCase
 25@end
 26
 27@interface IBArrayTestHelper : GTMIBArray
 28- (id)initWithObj1:(id)obj1 obj2:(id)obj2 obj3:(id)obj3
 29              obj4:(id)obj4 obj5:(id)obj5;
 30@end
 31
 32@implementation IBArrayTestHelper
 33- (id)initWithObj1:(id)obj1 obj2:(id)obj2 obj3:(id)obj3
 34              obj4:(id)obj4 obj5:(id)obj5 {
 35  if ((self = [super init])) {
 36    object1_ = [obj1 retain];
 37    object2_ = [obj2 retain];
 38    object3_ = [obj3 retain];
 39    object4_ = [obj4 retain];
 40    object5_ = [obj5 retain];
 41  }
 42  return self;
 43}
 44
 45- (void)dealloc {
 46  [object1_ release];
 47  [object2_ release];
 48  [object3_ release];
 49  [object4_ release];
 50  [object5_ release];
 51  [super dealloc];
 52}
 53
 54@end
 55
 56@implementation GTMIBArrayTest
 57
 58- (void)testEmpty {
 59  GTMIBArray *worker = [[[GTMIBArray alloc] init] autorelease];
 60
 61  STAssertNotNil(worker, nil);
 62  STAssertEquals([worker count], (NSUInteger)0, nil);
 63
 64  worker = [[[IBArrayTestHelper alloc] initWithObj1:nil
 65                                               obj2:nil
 66                                               obj3:nil
 67                                               obj4:nil
 68                                               obj5:nil] autorelease];
 69  STAssertNotNil(worker, nil);
 70  STAssertEquals([worker count], (NSUInteger)0, nil);
 71}
 72
 73- (void)testSparse {
 74  struct {
 75    id obj1;
 76    id obj2;
 77    id obj3;
 78    id obj4;
 79    id obj5;
 80    id combined;
 81  } data[] = {
 82    { @"a",  nil,  nil,  nil,  nil, @"a" },
 83    {  nil, @"a",  nil,  nil,  nil, @"a" },
 84    {  nil,  nil, @"a",  nil,  nil, @"a" },
 85    {  nil,  nil,  nil, @"a",  nil, @"a" },
 86    {  nil,  nil,  nil,  nil, @"a", @"a" },
 87
 88    { @"a", @"b",  nil,  nil,  nil, @"ab" },
 89    { @"a", @"b", @"c",  nil,  nil, @"abc" },
 90    { @"a", @"b", @"c", @"d",  nil, @"abcd" },
 91    {  nil, @"b", @"c",  nil,  nil, @"bc" },
 92    {  nil,  nil, @"c", @"d",  nil, @"cd" },
 93    {  nil,  nil,  nil, @"d", @"e", @"de" },
 94    { @"a",  nil, @"c",  nil, @"e", @"ace" },
 95
 96    { @"a", @"b", @"c", @"d", @"e", @"abcde" },
 97  };
 98
 99  for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
100    GTMIBArray *worker =
101      [[[IBArrayTestHelper alloc] initWithObj1:data[i].obj1
102                                          obj2:data[i].obj2
103                                          obj3:data[i].obj3
104                                          obj4:data[i].obj4
105                                          obj5:data[i].obj5] autorelease];
106    STAssertNotNil(worker, @"index %zu", i);
107    NSUInteger count = 0;
108    if (data[i].obj1) ++count;
109    if (data[i].obj2) ++count;
110    if (data[i].obj3) ++count;
111    if (data[i].obj4) ++count;
112    if (data[i].obj5) ++count;
113    STAssertEquals([worker count], count, @"index %zu", i);
114    STAssertEqualObjects([worker componentsJoinedByString:@""],
115                         data[i].combined,
116                         @"index %zu", i);
117  }
118}
119
120- (void)testRecursive {
121  GTMIBArray *ibArray1 =
122    [[[IBArrayTestHelper alloc] initWithObj1:@"a"
123                                        obj2:@"b"
124                                        obj3:@"c"
125                                        obj4:@"d"
126                                        obj5:@"e"] autorelease];
127  GTMIBArray *ibArray2 =
128    [[[IBArrayTestHelper alloc] initWithObj1:@"f"
129                                        obj2:@"g"
130                                        obj3:@"h"
131                                        obj4:@"i"
132                                        obj5:@"j"] autorelease];
133  GTMIBArray *ibArray3 =
134    [[[IBArrayTestHelper alloc] initWithObj1:@"k"
135                                        obj2:@"l"
136                                        obj3:@"m"
137                                        obj4:@"n"
138                                        obj5:@"o"] autorelease];
139  GTMIBArray *ibArray4 =
140    [[[IBArrayTestHelper alloc] initWithObj1:ibArray1
141                                        obj2:@"1"
142                                        obj3:ibArray2
143                                        obj4:@"2"
144                                        obj5:ibArray3] autorelease];
145  GTMIBArray *ibArray5 =
146    [[[IBArrayTestHelper alloc] initWithObj1:ibArray1
147                                        obj2:@"3"
148                                        obj3:nil
149                                        obj4:@"4"
150                                        obj5:ibArray3] autorelease];
151  GTMIBArray *ibArray6 =
152    [[[IBArrayTestHelper alloc] initWithObj1:nil
153                                        obj2:@"5"
154                                        obj3:ibArray1
155                                        obj4:@"6"
156                                        obj5:nil] autorelease];
157  GTMIBArray *ibArray7 =
158    [[[IBArrayTestHelper alloc] initWithObj1:nil
159                                        obj2:@"7"
160                                        obj3:ibArray1
161                                        obj4:@"8"
162                                        obj5:ibArray6] autorelease];
163  GTMIBArray *ibArray8 =
164    [[[IBArrayTestHelper alloc] initWithObj1:ibArray3
165                                        obj2:@"9"
166                                        obj3:ibArray7
167                                        obj4:nil
168                                        obj5:ibArray6] autorelease];
169
170  struct {
171    GTMIBArray *ibArray;
172    NSUInteger count;
173    NSString *result;
174  } data[] = {
175    { ibArray1,  5, @"abcde" },
176    { ibArray2,  5, @"fghij" },
177    { ibArray3,  5, @"klmno" },
178    { ibArray4, 17, @"abcde1fghij2klmno" },
179    { ibArray5, 12, @"abcde34klmno" },
180    { ibArray6,  7, @"5abcde6" },
181    { ibArray7, 14, @"7abcde85abcde6" },
182    { ibArray8, 27, @"klmno97abcde85abcde65abcde6" },
183  };
184
185  for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
186    NSArray *worker = data[i].ibArray;
187    STAssertNotNil(worker, @"index %zu", i);
188    STAssertEquals([worker count], data[i].count, @"index %zu", i);
189    STAssertEqualObjects([worker componentsJoinedByString:@""],
190                         data[i].result,
191                         @"index %zu", i);
192  }
193}
194
195- (void)testEnumeration {
196  GTMIBArray *worker =
197    [[[IBArrayTestHelper alloc] initWithObj1:@"a"
198                                        obj2:@"b"
199                                        obj3:@"c"
200                                        obj4:@"d"
201                                        obj5:@"e"] autorelease];
202
203  NSEnumerator *enumerator = [worker objectEnumerator];
204  STAssertNotNil(enumerator, nil);
205  STAssertEqualObjects([enumerator nextObject], @"a", nil);
206  STAssertEqualObjects([enumerator nextObject], @"b", nil);
207  STAssertEqualObjects([enumerator nextObject], @"c", nil);
208  STAssertEqualObjects([enumerator nextObject], @"d", nil);
209  STAssertEqualObjects([enumerator nextObject], @"e", nil);
210  STAssertNil([enumerator nextObject], nil);
211
212  enumerator = [worker reverseObjectEnumerator];
213  STAssertNotNil(enumerator, nil);
214  STAssertEqualObjects([enumerator nextObject], @"e", nil);
215  STAssertEqualObjects([enumerator nextObject], @"d", nil);
216  STAssertEqualObjects([enumerator nextObject], @"c", nil);
217  STAssertEqualObjects([enumerator nextObject], @"b", nil);
218  STAssertEqualObjects([enumerator nextObject], @"a", nil);
219  STAssertNil([enumerator nextObject], nil);
220}
221
222#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
223
224- (void)testFastEnumeration {
225  GTMIBArray *worker =
226    [[[IBArrayTestHelper alloc] initWithObj1:@"a"
227                                        obj2:@"b"
228                                        obj3:@"c"
229                                        obj4:@"d"
230                                        obj5:@"e"] autorelease];
231
232  NSUInteger idx = 0;
233  for (id obj in worker) {
234    switch (++idx) {
235      case 1:
236        STAssertEqualObjects(obj, @"a", nil);
237        break;
238      case 2:
239        STAssertEqualObjects(obj, @"b", nil);
240        break;
241      case 3:
242        STAssertEqualObjects(obj, @"c", nil);
243        break;
244      case 4:
245        STAssertEqualObjects(obj, @"d", nil);
246        break;
247      case 5:
248        STAssertEqualObjects(obj, @"e", nil);
249        break;
250      default:
251        STFail(@"looping too many times: %zu", idx);
252        break;
253    }
254  }
255}
256
257#endif  // MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
258
259- (void)testCopy {
260  GTMIBArray *worker =
261    [[[IBArrayTestHelper alloc] initWithObj1:@"a"
262                                        obj2:@"b"
263                                        obj3:@"c"
264                                        obj4:@"d"
265                                        obj5:@"e"] autorelease];
266
267  // Should get back a different object, but with the same contents.
268
269  NSArray *aCopy = [[worker copy] autorelease];
270  STAssertNotEquals(aCopy, worker, nil);
271  STAssertEqualObjects(aCopy, worker, nil);
272
273  NSArray *aMutableCopy = [[worker mutableCopy] autorelease];
274  STAssertNotEquals(aMutableCopy, worker, nil);
275  STAssertNotEquals(aMutableCopy, aCopy, nil);
276  STAssertEqualObjects(aMutableCopy, worker, nil);
277  STAssertEqualObjects(aMutableCopy, aCopy, nil);
278}
279
280- (void)testFromNib {
281  GTMIBArrayTestWindowController *controller =
282    [[[GTMIBArrayTestWindowController alloc]
283      initWithWindowNibName:@"GTMIBArrayTest"] autorelease];
284  NSWindow *window = [controller window];
285  STAssertNotNil(window, nil);
286
287  NSArray *labels = [controller labelsArray];
288  NSArray *fields = [controller fieldsArray];
289  NSArray *everything = [controller everythingArray];
290  STAssertNotNil(labels, nil);
291  STAssertNotNil(fields, nil);
292  STAssertNotNil(everything, nil);
293
294  STAssertEquals([labels count], (NSUInteger)3, nil);
295  STAssertEquals([fields count], (NSUInteger)3, nil);
296  STAssertEquals([everything count], (NSUInteger)8, nil);
297
298  NSSet *labelsSet = [NSSet setWithArray:labels];
299  NSSet *fieldsSet = [NSSet setWithArray:fields];
300  NSSet *everythingSet = [NSSet setWithArray:everything];
301  STAssertTrue([labelsSet isSubsetOfSet:everythingSet], nil);
302  STAssertTrue([fieldsSet isSubsetOfSet:everythingSet], nil);
303}
304
305- (void)testIsEqual {
306  GTMIBArray *ibArray1 =
307      [[[IBArrayTestHelper alloc] initWithObj1:@"a"
308                                          obj2:@"b"
309                                          obj3:@"c"
310                                          obj4:@"d"
311                                          obj5:@"e"] autorelease];
312  GTMIBArray *ibArray2 =
313      [[[IBArrayTestHelper alloc] initWithObj1:@"f"
314                                          obj2:@"g"
315                                          obj3:@"h"
316                                          obj4:@"i"
317                                          obj5:@"j"] autorelease];
318
319  STAssertEquals([ibArray1 hash], [ibArray2 hash], nil);
320  STAssertNotEqualObjects(ibArray1, ibArray2, nil);
321
322  NSArray *ibArray1Prime = [[ibArray1 copy] autorelease];
323  NSArray *ibArray2Prime = [[ibArray2 copy] autorelease];
324
325  STAssertTrue(ibArray1 != ibArray1Prime, nil);
326  STAssertTrue(ibArray2 != ibArray2Prime, nil);
327  STAssertNotEqualObjects(ibArray1Prime, ibArray2Prime, nil);
328  STAssertEqualObjects(ibArray1, ibArray1Prime, nil);
329  STAssertEqualObjects(ibArray2, ibArray2Prime, nil);
330}
331  
332@end
333
334@implementation GTMIBArrayTestWindowController
335
336- (NSArray *)labelsArray {
337  return labels_;
338}
339
340- (NSArray *)fieldsArray {
341  return fields_;
342}
343
344- (NSArray *)everythingArray {
345  return everything_;
346}
347
348@end