FSparseSnapshotTests.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "FSparseSnapshotTests.h"
  17. #import "FSparseSnapshotTree.h"
  18. #import "FSnapshotUtilities.h"
  19. #import "FEmptyNode.h"
  20. @implementation FSparseSnapshotTests
  21. - (void) testBasicRememberAndFind {
  22. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  23. FPath* path = [[FPath alloc] initWith:@"a/b"];
  24. id<FNode> node = [FSnapshotUtilities nodeFrom:@"sdfsd"];
  25. [st rememberData:node onPath:path];
  26. id<FNode> found = [st findPath:path];
  27. XCTAssertFalse([found isEmpty], @"Should find node");
  28. found = [st findPath:path.parent];
  29. XCTAssertTrue(found == nil, @"Should not find a node");
  30. }
  31. - (void) testFindInsideAnExistingSnapshot {
  32. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  33. FPath* path = [[FPath alloc] initWith:@"t/tt"];
  34. id<FNode> node = [FSnapshotUtilities nodeFrom:@{@"a": @"sdfsd", @"x": @5, @"999i": @YES}];
  35. id<FNode> update = [FSnapshotUtilities nodeFrom:@{@"goats": @88}];
  36. node = [node updateImmediateChild:@"apples" withNewChild:update];
  37. [st rememberData:node onPath:path];
  38. id<FNode> found = [st findPath:path];
  39. XCTAssertFalse([found isEmpty], @"Should find the node we set");
  40. found = [st findPath:[path childFromString:@"a"]];
  41. XCTAssertTrue([[found val] isEqualToString:@"sdfsd"], @"Find works inside data snapshot");
  42. found = [st findPath:[path childFromString:@"999i"]];
  43. XCTAssertTrue([[found val] isEqualToNumber:@YES], @"Find works inside data snapshot");
  44. found = [st findPath:[path childFromString:@"apples"]];
  45. XCTAssertFalse([found isEmpty], @"Should find the node we set");
  46. found = [st findPath:[path childFromString:@"apples/goats"]];
  47. XCTAssertTrue([[found val] isEqualToNumber:@88], @"Find works inside data snapshot");
  48. }
  49. - (void) testWriteASnapshotInsideASnapshot {
  50. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  51. [st rememberData:[FSnapshotUtilities nodeFrom:@{@"a": @{@"b": @"v"}}] onPath:[[FPath alloc] initWith:@"t"]];
  52. [st rememberData:[FSnapshotUtilities nodeFrom:@19] onPath:[[FPath alloc] initWith:@"t/a/rr"]];
  53. id<FNode> found = [st findPath:[[FPath alloc] initWith:@"t/a/b"]];
  54. XCTAssertTrue([[found val] isEqualToString:@"v"], @"Find inside snap");
  55. found = [st findPath:[[FPath alloc] initWith:@"t/a/rr"]];
  56. XCTAssertTrue([[found val] isEqualToNumber:@19], @"Find inside snap");
  57. }
  58. - (void) testWriteANullValueAndConfirmItIsRemembered {
  59. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  60. [st rememberData:[FSnapshotUtilities nodeFrom:[NSNull null]] onPath:[[FPath alloc] initWith:@"awq/fff"]];
  61. id<FNode> found = [st findPath:[[FPath alloc] initWith:@"awq/fff"]];
  62. XCTAssertTrue([found isEmpty], @"Empty node");
  63. found = [st findPath:[[FPath alloc] initWith:@"awq/sdf"]];
  64. XCTAssertTrue(found == nil, @"No node here");
  65. found = [st findPath:[[FPath alloc] initWith:@"awq/fff/jjj"]];
  66. XCTAssertTrue([found isEmpty], @"Empty node");
  67. found = [st findPath:[[FPath alloc] initWith:@"awq/sdf/sdj/q"]];
  68. XCTAssertTrue(found == nil, @"No node here");
  69. }
  70. - (void) testOverwriteWithNullAndConfirmItIsRemembered {
  71. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  72. [st rememberData:[FSnapshotUtilities nodeFrom:@{@"a": @{@"b": @"v"}}] onPath:[[FPath alloc] initWith:@"t"]];
  73. id<FNode> found = [st findPath:[[FPath alloc] initWith:@"t"]];
  74. XCTAssertFalse([found isEmpty], @"non-empty node");
  75. [st rememberData:[FSnapshotUtilities nodeFrom:[NSNull null]] onPath:[[FPath alloc] initWith:@"t"]];
  76. found = [st findPath:[[FPath alloc] initWith:@"t"]];
  77. XCTAssertTrue([found isEmpty], @"Empty node");
  78. }
  79. - (void) testSimpleRememberAndForget {
  80. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  81. [st rememberData:[FSnapshotUtilities nodeFrom:@{@"a": @{@"b": @"v"}}] onPath:[[FPath alloc] initWith:@"t"]];
  82. id<FNode> found = [st findPath:[[FPath alloc] initWith:@"t"]];
  83. XCTAssertFalse([found isEmpty], @"non-empty node");
  84. [st forgetPath:[[FPath alloc] initWith:@"t"]];
  85. found = [st findPath:[[FPath alloc] initWith:@"t"]];
  86. XCTAssertTrue(found == nil, @"node is gone");
  87. }
  88. - (void) testForgetTheRoot {
  89. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  90. [st rememberData:[FSnapshotUtilities nodeFrom:@{@"a": @{@"b": @"v"}}] onPath:[[FPath alloc] initWith:@"t"]];
  91. id<FNode> found = [st findPath:[[FPath alloc] initWith:@"t"]];
  92. XCTAssertFalse([found isEmpty], @"non-empty node");
  93. found = [st findPath:[[FPath alloc] initWith:@""]];
  94. XCTAssertTrue(found == nil, @"node is gone");
  95. }
  96. - (void) testForgetSnapshotInsideSnapshot {
  97. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  98. [st rememberData:[FSnapshotUtilities nodeFrom:@{@"a": @{@"b": @"v", @"c": @9, @"art": @NO}}] onPath:[[FPath alloc] initWith:@"t"]];
  99. id<FNode> found = [st findPath:[[FPath alloc] initWith:@"t/a/c"]];
  100. XCTAssertFalse([found isEmpty], @"non-empty node");
  101. found = [st findPath:[[FPath alloc] initWith:@"t"]];
  102. XCTAssertFalse([found isEmpty], @"non-empty node");
  103. [st forgetPath:PATH(@"t/a/c")];
  104. XCTAssertTrue([st findPath:PATH(@"t")] == nil, @"no more node here");
  105. XCTAssertTrue([st findPath:PATH(@"t/a")] == nil, @"no more node here");
  106. XCTAssertTrue([[[st findPath:PATH(@"t/a/b")] val] isEqualToString:@"v"], @"child still exists");
  107. XCTAssertTrue([st findPath:PATH(@"t/a/c")] == nil, @"no more node here");
  108. XCTAssertTrue([[[st findPath:PATH(@"t/a/art")] val] isEqualToNumber:@NO], @"child still exists");
  109. }
  110. - (void) testPathShallowerThanSnapshots {
  111. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  112. [st rememberData:NODE(@NO) onPath:PATH(@"t/x1")];
  113. [st rememberData:NODE(@YES) onPath:PATH(@"t/x2")];
  114. [st forgetPath:PATH(@"t")];
  115. XCTAssertTrue([st findPath:PATH(@"t")] == nil, @"No more node here");
  116. }
  117. - (void) testIterateChildren {
  118. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  119. id<FNode> node = [FSnapshotUtilities nodeFrom:@{@"b": @"v", @"c": @9, @"art": @NO}];
  120. [st rememberData:node onPath:PATH(@"t")];
  121. [st rememberData:[FEmptyNode emptyNode] onPath:PATH(@"q")];
  122. __block int num = 0;
  123. __block BOOL gotT = NO;
  124. __block BOOL gotQ = NO;
  125. [st forEachChild:^(NSString* key, FSparseSnapshotTree* tree) {
  126. num++;
  127. if ([key isEqualToString:@"t"]) {
  128. gotT = YES;
  129. } else if ([key isEqualToString:@"q"]) {
  130. gotQ = YES;
  131. } else {
  132. XCTFail(@"Unknown child");
  133. }
  134. }];
  135. XCTAssertTrue(gotT, @"Saw t");
  136. XCTAssertTrue(gotQ, @"Saw q");
  137. XCTAssertTrue(num == 2, @"Saw two children");
  138. }
  139. - (void) testIterateTrees {
  140. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  141. __block int count = 0;
  142. [st forEachTreeAtPath:PATH(@"") do:^(FPath *path, id<FNode> data) {
  143. count++;
  144. }];
  145. XCTAssertTrue(count == 0, @"No trees to iterate through");
  146. [st rememberData:NODE(@1) onPath:PATH(@"t")];
  147. [st rememberData:NODE(@2) onPath:PATH(@"a/b")];
  148. [st rememberData:NODE(@3) onPath:PATH(@"a/x/g")];
  149. [st rememberData:NODE([NSNull null]) onPath:PATH(@"a/x/null")];
  150. __block int num = 0;
  151. __block BOOL got1 = NO;
  152. __block BOOL got2 = NO;
  153. __block BOOL got3 = NO;
  154. __block BOOL gotNull = NO;
  155. [st forEachTreeAtPath:PATH(@"q") do:^(FPath *path, id<FNode> data) {
  156. num++;
  157. NSString* pathString = [path description];
  158. if ([pathString isEqualToString:@"/q/t"]) {
  159. got1 = YES;
  160. XCTAssertTrue([[data val] isEqualToNumber:@1], @"got 1");
  161. } else if ([pathString isEqualToString:@"/q/a/b"]) {
  162. got2 = YES;
  163. XCTAssertTrue([[data val] isEqualToNumber:@2], @"got 2");
  164. } else if ([pathString isEqualToString:@"/q/a/x/g"]) {
  165. got3 = YES;
  166. XCTAssertTrue([[data val] isEqualToNumber:@3], @"got 3");
  167. } else if ([pathString isEqualToString:@"/q/a/x/null"]) {
  168. gotNull = YES;
  169. XCTAssertTrue([data val] == [NSNull null], @"got null");
  170. } else {
  171. XCTFail(@"unknown tree");
  172. }
  173. }];
  174. XCTAssertTrue(got1 && got2 && got3 && gotNull, @"saw all the children");
  175. XCTAssertTrue(num == 4, @"Saw the right number of children");
  176. }
  177. - (void) testSetLeafAndForgetDeeperPath {
  178. FSparseSnapshotTree* st = [[FSparseSnapshotTree alloc] init];
  179. [st rememberData:NODE(@"bar") onPath:PATH(@"foo")];
  180. BOOL safeToRemove = [st forgetPath:PATH(@"foo/baz")];
  181. XCTAssertFalse(safeToRemove, @"Should not have deleted anything, nothing to remove");
  182. }
  183. @end