/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. #import "GTMIBArray.h"
  19. #import "GTMSenTestCase.h"
  20. #import "GTMIBArrayTest.h"
  21. @interface GTMIBArrayTest : GTMTestCase
  22. @end
  23. @interface IBArrayTestHelper : GTMIBArray
  24. - (id)initWithObj1:(id)obj1 obj2:(id)obj2 obj3:(id)obj3
  25. obj4:(id)obj4 obj5:(id)obj5;
  26. @end
  27. @implementation IBArrayTestHelper
  28. - (id)initWithObj1:(id)obj1 obj2:(id)obj2 obj3:(id)obj3
  29. obj4:(id)obj4 obj5:(id)obj5 {
  30. if ((self = [super init])) {
  31. object1_ = [obj1 retain];
  32. object2_ = [obj2 retain];
  33. object3_ = [obj3 retain];
  34. object4_ = [obj4 retain];
  35. object5_ = [obj5 retain];
  36. }
  37. return self;
  38. }
  39. - (void)dealloc {
  40. [object1_ release];
  41. [object2_ release];
  42. [object3_ release];
  43. [object4_ release];
  44. [object5_ release];
  45. [super dealloc];
  46. }
  47. @end
  48. @implementation GTMIBArrayTest
  49. - (void)testEmpty {
  50. GTMIBArray *worker = [[[GTMIBArray alloc] init] autorelease];
  51. STAssertNotNil(worker, nil);
  52. STAssertEquals([worker count], (NSUInteger)0, nil);
  53. worker = [[[IBArrayTestHelper alloc] initWithObj1:nil
  54. obj2:nil
  55. obj3:nil
  56. obj4:nil
  57. obj5:nil] autorelease];
  58. STAssertNotNil(worker, nil);
  59. STAssertEquals([worker count], (NSUInteger)0, nil);
  60. }
  61. - (void)testSparse {
  62. struct {
  63. id obj1;
  64. id obj2;
  65. id obj3;
  66. id obj4;
  67. id obj5;
  68. id combined;
  69. } data[] = {
  70. { @"a", nil, nil, nil, nil, @"a" },
  71. { nil, @"a", nil, nil, nil, @"a" },
  72. { nil, nil, @"a", nil, nil, @"a" },
  73. { nil, nil, nil, @"a", nil, @"a" },
  74. { nil, nil, nil, nil, @"a", @"a" },
  75. { @"a", @"b", nil, nil, nil, @"ab" },
  76. { @"a", @"b", @"c", nil, nil, @"abc" },
  77. { @"a", @"b", @"c", @"d", nil, @"abcd" },
  78. { nil, @"b", @"c", nil, nil, @"bc" },
  79. { nil, nil, @"c", @"d", nil, @"cd" },
  80. { nil, nil, nil, @"d", @"e", @"de" },
  81. { @"a", nil, @"c", nil, @"e", @"ace" },
  82. { @"a", @"b", @"c", @"d", @"e", @"abcde" },
  83. };
  84. for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
  85. GTMIBArray *worker =
  86. [[[IBArrayTestHelper alloc] initWithObj1:data[i].obj1
  87. obj2:data[i].obj2
  88. obj3:data[i].obj3
  89. obj4:data[i].obj4
  90. obj5:data[i].obj5] autorelease];
  91. STAssertNotNil(worker, @"index %zu", i);
  92. NSUInteger count = 0;
  93. if (data[i].obj1) ++count;
  94. if (data[i].obj2) ++count;
  95. if (data[i].obj3) ++count;
  96. if (data[i].obj4) ++count;
  97. if (data[i].obj5) ++count;
  98. STAssertEquals([worker count], count, @"index %zu", i);
  99. STAssertEqualObjects([worker componentsJoinedByString:@""],
  100. data[i].combined,
  101. @"index %zu", i);
  102. }
  103. }
  104. - (void)testRecursive {
  105. GTMIBArray *ibArray1 =
  106. [[[IBArrayTestHelper alloc] initWithObj1:@"a"
  107. obj2:@"b"
  108. obj3:@"c"
  109. obj4:@"d"
  110. obj5:@"e"] autorelease];
  111. GTMIBArray *ibArray2 =
  112. [[[IBArrayTestHelper alloc] initWithObj1:@"f"
  113. obj2:@"g"
  114. obj3:@"h"
  115. obj4:@"i"
  116. obj5:@"j"] autorelease];
  117. GTMIBArray *ibArray3 =
  118. [[[IBArrayTestHelper alloc] initWithObj1:@"k"
  119. obj2:@"l"
  120. obj3:@"m"
  121. obj4:@"n"
  122. obj5:@"o"] autorelease];
  123. GTMIBArray *ibArray4 =
  124. [[[IBArrayTestHelper alloc] initWithObj1:ibArray1
  125. obj2:@"1"
  126. obj3:ibArray2
  127. obj4:@"2"
  128. obj5:ibArray3] autorelease];
  129. GTMIBArray *ibArray5 =
  130. [[[IBArrayTestHelper alloc] initWithObj1:ibArray1
  131. obj2:@"3"
  132. obj3:nil
  133. obj4:@"4"
  134. obj5:ibArray3] autorelease];
  135. GTMIBArray *ibArray6 =
  136. [[[IBArrayTestHelper alloc] initWithObj1:nil
  137. obj2:@"5"
  138. obj3:ibArray1
  139. obj4:@"6"
  140. obj5:nil] autorelease];
  141. GTMIBArray *ibArray7 =
  142. [[[IBArrayTestHelper alloc] initWithObj1:nil
  143. obj2:@"7"
  144. obj3:ibArray1
  145. obj4:@"8"
  146. obj5:ibArray6] autorelease];
  147. GTMIBArray *ibArray8 =
  148. [[[IBArrayTestHelper alloc] initWithObj1:ibArray3
  149. obj2:@"9"
  150. obj3:ibArray7
  151. obj4:nil
  152. obj5:ibArray6] autorelease];
  153. struct {
  154. GTMIBArray *ibArray;
  155. NSUInteger count;
  156. NSString *result;
  157. } data[] = {
  158. { ibArray1, 5, @"abcde" },
  159. { ibArray2, 5, @"fghij" },
  160. { ibArray3, 5, @"klmno" },
  161. { ibArray4, 17, @"abcde1fghij2klmno" },
  162. { ibArray5, 12, @"abcde34klmno" },
  163. { ibArray6, 7, @"5abcde6" },
  164. { ibArray7, 14, @"7abcde85abcde6" },
  165. { ibArray8, 27, @"klmno97abcde85abcde65abcde6" },
  166. };
  167. for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
  168. NSArray *worker = data[i].ibArray;
  169. STAssertNotNil(worker, @"index %zu", i);
  170. STAssertEquals([worker count], data[i].count, @"index %zu", i);
  171. STAssertEqualObjects([worker componentsJoinedByString:@""],
  172. data[i].result,
  173. @"index %zu", i);
  174. }
  175. }
  176. - (void)testEnumeration {
  177. GTMIBArray *worker =
  178. [[[IBArrayTestHelper alloc] initWithObj1:@"a"
  179. obj2:@"b"
  180. obj3:@"c"
  181. obj4:@"d"
  182. obj5:@"e"] autorelease];
  183. NSEnumerator *enumerator = [worker objectEnumerator];
  184. STAssertNotNil(enumerator, nil);
  185. STAssertEqualObjects([enumerator nextObject], @"a", nil);
  186. STAssertEqualObjects([enumerator nextObject], @"b", nil);
  187. STAssertEqualObjects([enumerator nextObject], @"c", nil);
  188. STAssertEqualObjects([enumerator nextObject], @"d", nil);
  189. STAssertEqualObjects([enumerator nextObject], @"e", nil);
  190. STAssertNil([enumerator nextObject], nil);
  191. enumerator = [worker reverseObjectEnumerator];
  192. STAssertNotNil(enumerator, nil);
  193. STAssertEqualObjects([enumerator nextObject], @"e", nil);
  194. STAssertEqualObjects([enumerator nextObject], @"d", nil);
  195. STAssertEqualObjects([enumerator nextObject], @"c", nil);
  196. STAssertEqualObjects([enumerator nextObject], @"b", nil);
  197. STAssertEqualObjects([enumerator nextObject], @"a", nil);
  198. STAssertNil([enumerator nextObject], nil);
  199. }
  200. #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
  201. - (void)testFastEnumeration {
  202. GTMIBArray *worker =
  203. [[[IBArrayTestHelper alloc] initWithObj1:@"a"
  204. obj2:@"b"
  205. obj3:@"c"
  206. obj4:@"d"
  207. obj5:@"e"] autorelease];
  208. NSUInteger idx = 0;
  209. for (id obj in worker) {
  210. switch (++idx) {
  211. case 1:
  212. STAssertEqualObjects(obj, @"a", nil);
  213. break;
  214. case 2:
  215. STAssertEqualObjects(obj, @"b", nil);
  216. break;
  217. case 3:
  218. STAssertEqualObjects(obj, @"c", nil);
  219. break;
  220. case 4:
  221. STAssertEqualObjects(obj, @"d", nil);
  222. break;
  223. case 5:
  224. STAssertEqualObjects(obj, @"e", nil);
  225. break;
  226. default:
  227. STFail(@"looping too many times: %zu", idx);
  228. break;
  229. }
  230. }
  231. }
  232. #endif // MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
  233. - (void)testCopy {
  234. GTMIBArray *worker =
  235. [[[IBArrayTestHelper alloc] initWithObj1:@"a"
  236. obj2:@"b"
  237. obj3:@"c"
  238. obj4:@"d"
  239. obj5:@"e"] autorelease];
  240. // Should get back a different object, but with the same contents.
  241. NSArray *aCopy = [[worker copy] autorelease];
  242. STAssertNotEquals(aCopy, worker, nil);
  243. STAssertEqualObjects(aCopy, worker, nil);
  244. NSArray *aMutableCopy = [[worker mutableCopy] autorelease];
  245. STAssertNotEquals(aMutableCopy, worker, nil);
  246. STAssertNotEquals(aMutableCopy, aCopy, nil);
  247. STAssertEqualObjects(aMutableCopy, worker, nil);
  248. STAssertEqualObjects(aMutableCopy, aCopy, nil);
  249. }
  250. - (void)testFromNib {
  251. GTMIBArrayTestWindowController *controller =
  252. [[[GTMIBArrayTestWindowController alloc]
  253. initWithWindowNibName:@"GTMIBArrayTest"] autorelease];
  254. NSWindow *window = [controller window];
  255. STAssertNotNil(window, nil);
  256. NSArray *labels = [controller labelsArray];
  257. NSArray *fields = [controller fieldsArray];
  258. NSArray *everything = [controller everythingArray];
  259. STAssertNotNil(labels, nil);
  260. STAssertNotNil(fields, nil);
  261. STAssertNotNil(everything, nil);
  262. STAssertEquals([labels count], (NSUInteger)3, nil);
  263. STAssertEquals([fields count], (NSUInteger)3, nil);
  264. STAssertEquals([everything count], (NSUInteger)8, nil);
  265. NSSet *labelsSet = [NSSet setWithArray:labels];
  266. NSSet *fieldsSet = [NSSet setWithArray:fields];
  267. NSSet *everythingSet = [NSSet setWithArray:everything];
  268. STAssertTrue([labelsSet isSubsetOfSet:everythingSet], nil);
  269. STAssertTrue([fieldsSet isSubsetOfSet:everythingSet], nil);
  270. }
  271. - (void)testIsEqual {
  272. GTMIBArray *ibArray1 =
  273. [[[IBArrayTestHelper alloc] initWithObj1:@"a"
  274. obj2:@"b"
  275. obj3:@"c"
  276. obj4:@"d"
  277. obj5:@"e"] autorelease];
  278. GTMIBArray *ibArray2 =
  279. [[[IBArrayTestHelper alloc] initWithObj1:@"f"
  280. obj2:@"g"
  281. obj3:@"h"
  282. obj4:@"i"
  283. obj5:@"j"] autorelease];
  284. STAssertEquals([ibArray1 hash], [ibArray2 hash], nil);
  285. STAssertNotEqualObjects(ibArray1, ibArray2, nil);
  286. NSArray *ibArray1Prime = [[ibArray1 copy] autorelease];
  287. NSArray *ibArray2Prime = [[ibArray2 copy] autorelease];
  288. STAssertTrue(ibArray1 != ibArray1Prime, nil);
  289. STAssertTrue(ibArray2 != ibArray2Prime, nil);
  290. STAssertNotEqualObjects(ibArray1Prime, ibArray2Prime, nil);
  291. STAssertEqualObjects(ibArray1, ibArray1Prime, nil);
  292. STAssertEqualObjects(ibArray2, ibArray2Prime, nil);
  293. }
  294. @end
  295. @implementation GTMIBArrayTestWindowController
  296. - (NSArray *)labelsArray {
  297. return labels_;
  298. }
  299. - (NSArray *)fieldsArray {
  300. return fields_;
  301. }
  302. - (NSArray *)everythingArray {
  303. return everything_;
  304. }
  305. @end