FTreeSortedDictionaryTests.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FTreeSortedDictionary.h"
  18. #import "FLLRBNode.h"
  19. #import "FLLRBEmptyNode.h"
  20. #import "FLLRBValueNode.h"
  21. @interface FLLRBValueNode (Tests)
  22. - (id<FLLRBNode>) rotateLeft;
  23. - (id<FLLRBNode>) rotateRight;
  24. @end
  25. @interface FTreeSortedDictionaryTests : XCTestCase
  26. @end
  27. @implementation FTreeSortedDictionaryTests
  28. - (NSComparator) defaultComparator {
  29. return ^(id obj1, id obj2) {
  30. if([obj1 respondsToSelector:@selector(compare:)] && [obj2 respondsToSelector:@selector(compare:)]) {
  31. return [obj1 compare:obj2];
  32. }
  33. else {
  34. if(obj1 < obj2) {
  35. return (NSComparisonResult)NSOrderedAscending;
  36. }
  37. else if (obj1 > obj2) {
  38. return (NSComparisonResult)NSOrderedDescending;
  39. }
  40. else {
  41. return (NSComparisonResult)NSOrderedSame;
  42. }
  43. }
  44. };
  45. }
  46. - (void)testCreateNode
  47. {
  48. FTreeSortedDictionary* map = [[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  49. insertKey:@"key" withValue:@"value"];
  50. XCTAssertTrue([map.root.left isEmpty], @"Left child is properly empty");
  51. XCTAssertTrue([map.root.right isEmpty], @"Right child is properly empty");
  52. }
  53. - (void)testGetNilReturnsNil {
  54. FImmutableSortedDictionary *map1 = [[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  55. insertKey:@"key" withValue:@"value"];
  56. XCTAssertNil([map1 get:nil]);
  57. FImmutableSortedDictionary *map2 = [[[FTreeSortedDictionary alloc] initWithComparator:^NSComparisonResult(id obj1, id obj2) {
  58. return [obj1 compare:obj2];
  59. }]
  60. insertKey:@"key" withValue:@"value"];
  61. XCTAssertNil([map2 get:nil]);
  62. }
  63. - (void)testSearchForSpecificKey {
  64. FTreeSortedDictionary* map = [[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  65. insertKey:@1 withValue:@1]
  66. insertKey:@2 withValue:@2];
  67. XCTAssertEqualObjects([map get:@1], @1, @"Found first object");
  68. XCTAssertEqualObjects([map get:@2], @2, @"Found second object");
  69. XCTAssertNil([map get:@3], @"Properly not found object");
  70. }
  71. - (void)testInsertNewKeyValuePair {
  72. FTreeSortedDictionary* map = [[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  73. insertKey:@1 withValue:@1]
  74. insertKey:@2 withValue:@2];
  75. XCTAssertEqualObjects(map.root.key, @2, @"Check the root key");
  76. XCTAssertEqualObjects(map.root.left.key, @1, @"Check the root.left key");
  77. }
  78. - (void)testRemoveKeyValuePair {
  79. FTreeSortedDictionary* map = [[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  80. insertKey:@1 withValue:@1]
  81. insertKey:@2 withValue:@2];
  82. FImmutableSortedDictionary* newMap = [map removeKey:@1];
  83. XCTAssertEqualObjects([newMap get:@2], @2, @"Found second object");
  84. XCTAssertNil([newMap get:@1], @"Properly not found object");
  85. // Make sure the original one is not mutated
  86. XCTAssertEqualObjects([map get:@1], @1, @"Found first object");
  87. XCTAssertEqualObjects([map get:@2], @2, @"Found second object");
  88. }
  89. - (void)testMoreRemovals {
  90. FTreeSortedDictionary* map = [[[[[[[[[[[[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  91. insertKey:@1 withValue:@1]
  92. insertKey:@50 withValue:@50]
  93. insertKey:@3 withValue:@3]
  94. insertKey:@4 withValue:@4]
  95. insertKey:@7 withValue:@7]
  96. insertKey:@9 withValue:@9]
  97. insertKey:@20 withValue:@20]
  98. insertKey:@18 withValue:@18]
  99. insertKey:@2 withValue:@2]
  100. insertKey:@71 withValue:@71]
  101. insertKey:@42 withValue:@42]
  102. insertKey:@88 withValue:@88];
  103. XCTAssertNotNil([map get:@7], @"Found object");
  104. XCTAssertNotNil([map get:@3], @"Found object");
  105. XCTAssertNotNil([map get:@1], @"Found object");
  106. FImmutableSortedDictionary* m1 = [map removeKey:@7];
  107. FImmutableSortedDictionary* m2 = [map removeKey:@3];
  108. FImmutableSortedDictionary* m3 = [map removeKey:@1];
  109. XCTAssertNil([m1 get:@7], @"Removed object");
  110. XCTAssertNotNil([m1 get:@3], @"Found object");
  111. XCTAssertNotNil([m1 get:@1], @"Found object");
  112. XCTAssertNil([m2 get:@3], @"Removed object");
  113. XCTAssertNotNil([m2 get:@7], @"Found object");
  114. XCTAssertNotNil([m2 get:@1], @"Found object");
  115. XCTAssertNil([m3 get:@1], @"Removed object");
  116. XCTAssertNotNil([m3 get:@7], @"Found object");
  117. XCTAssertNotNil([m3 get:@3], @"Found object");
  118. }
  119. - (void) testRemovalBug {
  120. FTreeSortedDictionary* map = [[[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  121. insertKey:@1 withValue:@1]
  122. insertKey:@2 withValue:@2]
  123. insertKey:@3 withValue:@3];
  124. XCTAssertEqualObjects([map get:@1], @1, @"Found object");
  125. XCTAssertEqualObjects([map get:@2], @2, @"Found object");
  126. XCTAssertEqualObjects([map get:@3], @3, @"Found object");
  127. FImmutableSortedDictionary* m1 = [map removeKey:@2];
  128. XCTAssertEqualObjects([m1 get:@1], @1, @"Found object");
  129. XCTAssertEqualObjects([m1 get:@3], @3, @"Found object");
  130. XCTAssertNil([m1 get:@2], @"Removed object");
  131. }
  132. - (void) testIncreasing {
  133. int total = 100;
  134. FTreeSortedDictionary* map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  135. for(int i = 0; i < total; i++) {
  136. NSNumber* item = [NSNumber numberWithInt:i];
  137. map = [map insertKey:item withValue:item];
  138. }
  139. XCTAssertTrue([map count] == 100, @"Check if all 100 objects are in the map");
  140. XCTAssertTrue([map.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  141. XCTAssertTrue([(FLLRBValueNode *)map.root checkMaxDepth], @"Checking valid depth and tree structure");
  142. for(int i = 0; i < total; i++) {
  143. NSNumber* item = [NSNumber numberWithInt:i];
  144. map = [map removeKey:item];
  145. }
  146. XCTAssertTrue([map count] == 0, @"Check if all 100 objects were removed");
  147. // We can't check the depth here because the map no longer contains values, so we check that it doesn't responsd to this check
  148. XCTAssertTrue([map.root isMemberOfClass:[FLLRBEmptyNode class]], @"Root is an empty node");
  149. XCTAssertFalse([map respondsToSelector:@selector(checkMaxDepth)], @"The empty node doesn't respond to this selector.");
  150. }
  151. - (void) testStructureShouldBeValidAfterInsertionA {
  152. FTreeSortedDictionary* map = [[[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  153. insertKey:@1 withValue:@1]
  154. insertKey:@2 withValue:@2]
  155. insertKey:@3 withValue:@3];
  156. XCTAssertEqualObjects(map.root.key, @2, @"Check root key");
  157. XCTAssertEqualObjects(map.root.left.key, @1, @"Check the left key is correct");
  158. XCTAssertEqualObjects(map.root.right.key, @3, @"Check the right key is correct");
  159. }
  160. - (void) testStructureShouldBeValidAfterInsertionB {
  161. FTreeSortedDictionary* map = [[[[[[[[[[[[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  162. insertKey:@1 withValue:@1]
  163. insertKey:@50 withValue:@50]
  164. insertKey:@3 withValue:@3]
  165. insertKey:@4 withValue:@4]
  166. insertKey:@7 withValue:@7]
  167. insertKey:@9 withValue:@9]
  168. insertKey:@20 withValue:@20]
  169. insertKey:@18 withValue:@18]
  170. insertKey:@2 withValue:@2]
  171. insertKey:@71 withValue:@71]
  172. insertKey:@42 withValue:@42]
  173. insertKey:@88 withValue:@88];
  174. XCTAssertTrue([map count] == 12, @"Check if all 12 objects are in the map");
  175. XCTAssertTrue([map.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  176. XCTAssertTrue([(FLLRBValueNode *)map.root checkMaxDepth], @"Checking valid depth and tree structure");
  177. }
  178. - (void) testRotateLeftLeavesTreeInAValidState {
  179. FLLRBValueNode* node = [[FLLRBValueNode alloc] initWithKey:@4 withValue:@4 withColor:BLACK withLeft:
  180. [[FLLRBValueNode alloc] initWithKey:@2 withValue:@2 withColor:BLACK withLeft:nil withRight:nil] withRight:[[FLLRBValueNode alloc]initWithKey:@7 withValue:@7 withColor:RED withLeft:[[FLLRBValueNode alloc ]initWithKey:@5 withValue:@5 withColor:BLACK withLeft:nil withRight:nil] withRight:[[FLLRBValueNode alloc] initWithKey:@8 withValue:@8 withColor:BLACK withLeft:nil withRight:nil]]];
  181. FLLRBValueNode* node2 = [node performSelector:@selector(rotateLeft)];
  182. XCTAssertTrue([node2 count] == 5, @"Make sure the count is correct");
  183. XCTAssertTrue([node2 checkMaxDepth], @"Check proper structure");
  184. }
  185. - (void) testRotateRightLeavesTreeInAValidState {
  186. FLLRBValueNode* node = [[FLLRBValueNode alloc] initWithKey:@7 withValue:@7 withColor:BLACK withLeft:[[FLLRBValueNode alloc] initWithKey:@4 withValue:@4 withColor:RED withLeft:[[FLLRBValueNode alloc] initWithKey:@2 withValue:@2 withColor:BLACK withLeft:nil withRight:nil] withRight:[[FLLRBValueNode alloc] initWithKey:@5 withValue:@5 withColor:BLACK withLeft:nil withRight:nil]] withRight:[[FLLRBValueNode alloc] initWithKey:@8 withValue:@8 withColor:BLACK withLeft:nil withRight:nil]];
  187. FLLRBValueNode* node2 = [node performSelector:@selector(rotateRight)];
  188. XCTAssertTrue([node2 count] == 5, @"Make sure the count is correct");
  189. XCTAssertEqualObjects(node2.key, @4, @"Check roots key");
  190. XCTAssertEqualObjects(node2.left.key, @2, @"Check first left child key");
  191. XCTAssertEqualObjects(node2.right.key, @7, @"Check first right child key");
  192. XCTAssertEqualObjects(node2.right.left.key, @5, @"Check second right left key");
  193. XCTAssertEqualObjects(node2.right.right.key, @8, @"Check second right left key");
  194. }
  195. - (void) testStructureShouldBeValidAfterInsertionC {
  196. FTreeSortedDictionary* map = [[[[[[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  197. insertKey:@1 withValue:@1]
  198. insertKey:@50 withValue:@50]
  199. insertKey:@3 withValue:@3]
  200. insertKey:@4 withValue:@4]
  201. insertKey:@7 withValue:@7]
  202. insertKey:@9 withValue:@9];
  203. XCTAssertTrue([map count] == 6, @"Check if all 6 objects are in the map");
  204. XCTAssertTrue([map.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  205. XCTAssertTrue([(FLLRBValueNode *)map.root checkMaxDepth], @"Checking valid depth and tree structure");
  206. FTreeSortedDictionary* m2 = [[[map insertKey:@20 withValue:@20]
  207. insertKey:@18 withValue:@18]
  208. insertKey:@2 withValue:@2];
  209. XCTAssertTrue([m2 count] == 9, @"Check if all 9 objects are in the map");
  210. XCTAssertTrue([m2.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  211. XCTAssertTrue([(FLLRBValueNode *)m2.root checkMaxDepth], @"Checking valid depth and tree structure");
  212. FTreeSortedDictionary* m3 = [[[[m2 insertKey:@71 withValue:@71]
  213. insertKey:@42 withValue:@42]
  214. insertKey:@88 withValue:@88]
  215. insertKey:@20 withValue:@20]; // Add a dupe to see if the size is correct
  216. XCTAssertTrue([m3 count] == 12, @"Check if all 12 (minus dupe @20) objects are in the map");
  217. XCTAssertTrue([m3.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  218. XCTAssertTrue([(FLLRBValueNode *)m3.root checkMaxDepth], @"Checking valid depth and tree structure");
  219. }
  220. - (void) testOverride {
  221. FTreeSortedDictionary* map = [[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  222. insertKey:@10 withValue:@10]
  223. insertKey:@10 withValue:@8];
  224. XCTAssertEqualObjects([map get:@10], @8, @"Found first object");
  225. }
  226. - (void) testEmpty {
  227. FTreeSortedDictionary* map = [[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  228. insertKey:@10 withValue:@10]
  229. removeKey:@10];
  230. XCTAssertTrue([map isEmpty], @"Properly empty");
  231. }
  232. - (void) testEmptyGet {
  233. FTreeSortedDictionary* map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  234. XCTAssertNil([map get:@"something"], @"Properly nil");
  235. }
  236. - (void) testEmptyCount {
  237. FTreeSortedDictionary* map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  238. XCTAssertTrue([map count] == 0, @"Properly zero count");
  239. }
  240. - (void) testEmptyRemoval {
  241. FTreeSortedDictionary* map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  242. XCTAssertTrue([[map removeKey:@"sometjhing"] count] == 0, @"Properly zero count");
  243. }
  244. - (void) testReverseTraversal {
  245. FTreeSortedDictionary* map = [[[[[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  246. insertKey:@1 withValue:@1]
  247. insertKey:@5 withValue:@5]
  248. insertKey:@3 withValue:@3]
  249. insertKey:@2 withValue:@2]
  250. insertKey:@4 withValue:@4];
  251. __block int next = 5;
  252. [map enumerateKeysAndObjectsReverse:YES usingBlock:^(id key, id value, BOOL *stop) {
  253. XCTAssertEqualObjects(key, [NSNumber numberWithInt:next], @"Properly equal");
  254. next = next - 1;
  255. }];
  256. }
  257. - (void) testInsertionAndRemovalOfAHundredItems {
  258. int N = 100;
  259. NSMutableArray* toInsert = [[NSMutableArray alloc] initWithCapacity:N];
  260. NSMutableArray* toRemove = [[NSMutableArray alloc] initWithCapacity:N];
  261. for(int i = 0; i < N; i++) {
  262. [toInsert addObject:[NSNumber numberWithInt:i]];
  263. [toRemove addObject:[NSNumber numberWithInt:i]];
  264. }
  265. [self shuffleArray:toInsert];
  266. [self shuffleArray:toRemove];
  267. FTreeSortedDictionary* map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  268. // add them to the dictionary
  269. for(int i = 0; i < N; i++) {
  270. map = [map insertKey:[toInsert objectAtIndex:i] withValue:[toInsert objectAtIndex:i]];
  271. XCTAssertTrue([map.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  272. XCTAssertTrue([(FLLRBValueNode *)map.root checkMaxDepth], @"Checking valid depth and tree structure");
  273. }
  274. XCTAssertTrue([map count] == N, @"Check if all N objects are in the map");
  275. // check the order is correct
  276. __block int next = 0;
  277. [map enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
  278. XCTAssertEqualObjects(key, [NSNumber numberWithInt:next], @"Correct key");
  279. XCTAssertEqualObjects(value, [NSNumber numberWithInt:next], @"Correct value");
  280. next = next + 1;
  281. }];
  282. XCTAssertEqual(next, N, @"Check we traversed all of the items");
  283. // remove them
  284. for(int i = 0; i < N; i++) {
  285. if([map.root isMemberOfClass:[FLLRBValueNode class]]) {
  286. XCTAssertTrue([map.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  287. XCTAssertTrue([(FLLRBValueNode *)map.root checkMaxDepth], @"Checking valid depth and tree structure");
  288. }
  289. map = [map removeKey:[toRemove objectAtIndex:i]];
  290. }
  291. XCTAssertEqual([map count], 0, @"Check we removed all of the items");
  292. }
  293. - (void) shuffleArray:(NSMutableArray *)array {
  294. NSUInteger count = [array count];
  295. for(NSUInteger i = 0; i < count; i++) {
  296. NSInteger nElements = count - i;
  297. NSInteger n = (arc4random() % nElements) + i;
  298. [array exchangeObjectAtIndex:i withObjectAtIndex:n];
  299. }
  300. }
  301. - (void) testBalanceProblem {
  302. NSArray* toInsert = [[NSArray alloc] initWithObjects:@1,@7,@8,@5,@2,@6,@4,@0,@3, nil];
  303. FTreeSortedDictionary* map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  304. // add them to the dictionary
  305. for(int i = 0; i < [toInsert count]; i++) {
  306. map = [map insertKey:[toInsert objectAtIndex:i] withValue:[toInsert objectAtIndex:i]];
  307. XCTAssertTrue([map.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  308. XCTAssertTrue([(FLLRBValueNode *)map.root checkMaxDepth], @"Checking valid depth and tree structure");
  309. }
  310. XCTAssertTrue([map count] == [toInsert count], @"Check if all N objects are in the map");
  311. // check the order is correct
  312. __block int next = 0;
  313. [map enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
  314. XCTAssertEqualObjects(key, [NSNumber numberWithInt:next], @"Correct key");
  315. XCTAssertEqualObjects(value, [NSNumber numberWithInt:next], @"Correct value");
  316. next = next + 1;
  317. }];
  318. XCTAssertEqual(next, [[NSNumber numberWithUnsignedInteger:[toInsert count]] intValue], @"Check we traversed all of the items");
  319. // removing one triggers the balance problem
  320. map = [map removeKey:@5];
  321. if([map.root isMemberOfClass:[FLLRBValueNode class]]) {
  322. XCTAssertTrue([map.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  323. XCTAssertTrue([(FLLRBValueNode *)map.root checkMaxDepth], @"Checking valid depth and tree structure");
  324. }
  325. }
  326. - (void) testPredecessorKey {
  327. FTreeSortedDictionary* map = [[[[[[[[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]]
  328. insertKey:@1 withValue:@1]
  329. insertKey:@50 withValue:@50]
  330. insertKey:@3 withValue:@3]
  331. insertKey:@4 withValue:@4]
  332. insertKey:@7 withValue:@7]
  333. insertKey:@9 withValue:@9];
  334. XCTAssertNil([map getPredecessorKey:@1], @"First object doesn't have a predecessor");
  335. XCTAssertEqualObjects([map getPredecessorKey:@3], @1, @"@1");
  336. XCTAssertEqualObjects([map getPredecessorKey:@4], @3, @"@3");
  337. XCTAssertEqualObjects([map getPredecessorKey:@7], @4, @"@4");
  338. XCTAssertEqualObjects([map getPredecessorKey:@9], @7, @"@7");
  339. XCTAssertEqualObjects([map getPredecessorKey:@50], @9, @"@9");
  340. XCTAssertThrows([map getPredecessorKey:@777], @"Expect exception about nonexistant key");
  341. }
  342. - (void) testEnumerator {
  343. int N = 100;
  344. NSMutableArray* toInsert = [[NSMutableArray alloc] initWithCapacity:N];
  345. NSMutableArray* toRemove = [[NSMutableArray alloc] initWithCapacity:N];
  346. for(int i = 0; i < N; i++) {
  347. [toInsert addObject:[NSNumber numberWithInt:i]];
  348. [toRemove addObject:[NSNumber numberWithInt:i]];
  349. }
  350. [self shuffleArray:toInsert];
  351. [self shuffleArray:toRemove];
  352. FTreeSortedDictionary* map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  353. // add them to the dictionary
  354. for(int i = 0; i < N; i++) {
  355. map = [map insertKey:[toInsert objectAtIndex:i] withValue:[toInsert objectAtIndex:i]];
  356. XCTAssertTrue([map.root isMemberOfClass:[FLLRBValueNode class]], @"Root is a value node");
  357. XCTAssertTrue([(FLLRBValueNode *)map.root checkMaxDepth], @"Checking valid depth and tree structure");
  358. }
  359. XCTAssertTrue([map count] == N, @"Check if all N objects are in the map");
  360. NSEnumerator* enumerator = [map keyEnumerator];
  361. id next = [enumerator nextObject];
  362. int correctValue = 0;
  363. while(next != nil) {
  364. XCTAssertEqualObjects(next, [NSNumber numberWithInt:correctValue], @"Correct key");
  365. next = [enumerator nextObject];
  366. correctValue = correctValue + 1;
  367. }
  368. }
  369. - (void) testReverseEnumerator {
  370. int N = 20;
  371. NSMutableArray* toInsert = [[NSMutableArray alloc] initWithCapacity:N];
  372. for(int i = 0; i < N; i++) {
  373. [toInsert addObject:[NSNumber numberWithInt:i]];
  374. }
  375. [self shuffleArray:toInsert];
  376. FImmutableSortedDictionary *map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  377. // add them to the dictionary
  378. for(int i = 0; i < N; i++) {
  379. map = [map insertKey:[toInsert objectAtIndex:i] withValue:[toInsert objectAtIndex:i]];
  380. }
  381. XCTAssertTrue([map count] == N, @"Check if all N objects are in the map");
  382. XCTAssertTrue([map isKindOfClass:[FTreeSortedDictionary class]], @"Make sure we still have a array backed dictionary");
  383. NSEnumerator* enumerator = [map reverseKeyEnumerator];
  384. id next = [enumerator nextObject];
  385. int correctValue = N - 1;
  386. while(next != nil) {
  387. XCTAssertEqualObjects(next, [NSNumber numberWithInt:correctValue], @"Correct key");
  388. next = [enumerator nextObject];
  389. correctValue--;
  390. }
  391. }
  392. - (void) testEnumeratorFrom {
  393. int N = 20;
  394. NSMutableArray* toInsert = [[NSMutableArray alloc] initWithCapacity:N];
  395. for(int i = 0; i < N; i++) {
  396. [toInsert addObject:[NSNumber numberWithInt:i*2]];
  397. }
  398. [self shuffleArray:toInsert];
  399. FImmutableSortedDictionary *map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  400. // add them to the dictionary
  401. for(int i = 0; i < N; i++) {
  402. map = [map insertKey:[toInsert objectAtIndex:i] withValue:[toInsert objectAtIndex:i]];
  403. }
  404. XCTAssertTrue([map count] == N, @"Check if all N objects are in the map");
  405. XCTAssertTrue([map isKindOfClass:[FTreeSortedDictionary class]], @"Make sure we still have a array backed dictionary");
  406. // Test from inbetween keys
  407. {
  408. NSEnumerator* enumerator = [map keyEnumeratorFrom:@11];
  409. id next = [enumerator nextObject];
  410. int correctValue = 12;
  411. while(next != nil) {
  412. XCTAssertEqualObjects(next, [NSNumber numberWithInt:correctValue], @"Correct key");
  413. next = [enumerator nextObject];
  414. correctValue = correctValue + 2;
  415. }
  416. }
  417. // Test from key in map
  418. {
  419. NSEnumerator* enumerator = [map keyEnumeratorFrom:@10];
  420. id next = [enumerator nextObject];
  421. int correctValue = 10;
  422. while(next != nil) {
  423. XCTAssertEqualObjects(next, [NSNumber numberWithInt:correctValue], @"Correct key");
  424. next = [enumerator nextObject];
  425. correctValue = correctValue + 2;
  426. }
  427. }
  428. }
  429. - (void) testReverseEnumeratorFrom {
  430. int N = 20;
  431. NSMutableArray* toInsert = [[NSMutableArray alloc] initWithCapacity:N];
  432. for(int i = 0; i < N; i++) {
  433. [toInsert addObject:[NSNumber numberWithInt:i*2]];
  434. }
  435. [self shuffleArray:toInsert];
  436. FImmutableSortedDictionary *map = [[FTreeSortedDictionary alloc] initWithComparator:[self defaultComparator]];
  437. // add them to the dictionary
  438. for(int i = 0; i < N; i++) {
  439. map = [map insertKey:[toInsert objectAtIndex:i] withValue:[toInsert objectAtIndex:i]];
  440. }
  441. XCTAssertTrue([map count] == N, @"Check if all N objects are in the map");
  442. XCTAssertTrue([map isKindOfClass:[FTreeSortedDictionary class]], @"Make sure we still have a array backed dictionary");
  443. // Test from inbetween keys
  444. {
  445. NSEnumerator* enumerator = [map reverseKeyEnumeratorFrom:@11];
  446. id next = [enumerator nextObject];
  447. int correctValue = 10;
  448. while(next != nil) {
  449. XCTAssertEqualObjects(next, [NSNumber numberWithInt:correctValue], @"Correct key");
  450. next = [enumerator nextObject];
  451. correctValue = correctValue - 2;
  452. }
  453. }
  454. // Test from key in map
  455. {
  456. NSEnumerator* enumerator = [map reverseKeyEnumeratorFrom:@10];
  457. id next = [enumerator nextObject];
  458. int correctValue = 10;
  459. while(next != nil) {
  460. XCTAssertEqualObjects(next, [NSNumber numberWithInt:correctValue], @"Correct key");
  461. next = [enumerator nextObject];
  462. correctValue = correctValue - 2;
  463. }
  464. }
  465. }
  466. @end