@@ -3,71 +3,68 @@ module Test.Main where
33import Prelude
44
55import Control.Comonad.Cofree (head, (:<))
6+import Data.List (List(Nil), (:))
67import Data.Maybe (Maybe(..), fromJust)
8+import Data.Tree (Tree, mkTree, scanTree)
9+import Data.Tree.Zipper (down, findDownWhere, findFromRoot, findUp, flattenLocDepthFirst, fromTree, insertAfter, modifyValue, next, toTree, value, firstChild, lastChild)
710import Effect (Effect)
8−import Effect.Aff (launchAff_)
911import Partial.Unsafe (unsafePartial)
1012import Test.Spec (describe, it)
1113import Test.Spec.Assertions (shouldEqual)
1214import Test.Spec.Reporter (consoleReporter)
13−import Test.Spec.Runner (runSpec)
14−import Yoga.Tree (Tree, leaf, scanTree, showTree)
15−import Yoga.Tree.Zipper (down, findDownWhere, findFromRoot, findUp, flattenLocDepthFirst, fromTree, modifyValue, next, toTree, value)
16−
17−newtype SpecTree a = SpecTree (Tree a)
18−
19−instance Show a => Show (SpecTree a) where
20− show (SpecTree t) = showTree t
21−
22−derive instance (Eq a) => Eq (SpecTree a)
15+import Test.Spec.Runner (run)
2316
2417sampleTree :: Tree Int
2518sampleTree =
2619 1 :<
27− [ leaf 2
28− , leaf 3
29− , 4 :<
30− [ leaf 5
31− , 6 :< [ leaf 7 ]
32− , leaf 8
33− ]
34−
35− ]
20+ (2 :< Nil)
21+ : (3 :< Nil)
22+ : (4 :<
23+ (5 :< Nil)
24+ : (6 :<
25+ (7 :< Nil) : Nil)
26+ : (8 :< Nil)
27+ : Nil
28+ )
29+ : Nil
3630
3731main :: Effect Unit
38−main = launchAff_ $ runSpec [ consoleReporter ] do
32+main = run [consoleReporter] do
3933 describe "Tree" do
4034
4135 it "mkTree" do
42− let t = 10 :< []
36+ let t = mkTree 10 Nil
4337 shouldEqual (head t) 10
4438
4539 it "Functor" do
46− let
47− result = 2 :<
48− [ leaf 3
49− , leaf 4
50− , 5 :<
51− [ leaf 6
52− , 7 :< [ leaf 8 ]
53− , leaf 9
54− ]
55− ]
56− SpecTree (((+) 1) <$> sampleTree) `shouldEqual` SpecTree result
40+ let result =
41+ 2 :<
42+ (3 :< Nil)
43+ : (4 :< Nil)
44+ : (5 :<
45+ (6 :< Nil)
46+ : (7 :<
47+ (8 :< Nil) : Nil)
48+ : (9 :< Nil)
49+ : Nil
50+ )
51+ : Nil
52+ shouldEqual (eq (((+)1) <$> sampleTree) result) true
5753
5854 it "scanTree" do
59− let
60− result =
61− 1 :<
62− [ leaf 3
63− , leaf 4
64− , 5 :<
65− [ leaf 10
66− , 11 :< [ leaf 18 ]
67− , leaf 13
68− ]
69− ]
70− SpecTree (scanTree (\a b -> a + b) 0 sampleTree) `shouldEqual` SpecTree result
55+ let result =
56+ 1 :<
57+ (3 :< Nil)
58+ : (4 :< Nil)
59+ : (5 :<
60+ (10 :< Nil)
61+ : (11 :<
62+ (18 :< Nil) : Nil)
63+ : (13 :< Nil)
64+ : Nil
65+ )
66+ : Nil
67+ shouldEqual (eq (scanTree (\a b -> a + b) 0 sampleTree) result) true
7168
7269 describe "Zipper" do
7370
@@ -77,145 +74,221 @@ main = launchAff_ $ runSpec [ consoleReporter ] do
7774
7875 let root' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down root)
7976 let root'' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down >=> next >=> next >=> down $ root)
80−
81− let
82− result =
83− 1 :<
84− [ leaf 4
85− , leaf 3
86− , 4 :<
87− [ leaf 5
88− , 6 :< [ leaf 7 ]
89− , leaf 8
90− ]
91−
92− ]
93−
94− let
95− result' =
96− 1 :<
97− [ 2 :< []
98− , 3 :< []
99− , 4 :<
100− [ 10 :< []
101− , 6 :< [ 7 :< [] ]
102− , 8 :< []
103− ]
104− ]
105−
106− SpecTree root' `shouldEqual` SpecTree result
107− SpecTree root'' `shouldEqual` SpecTree result'
108−
109− -- it "Insert" do
110−
111− -- let root1 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ down root)
112− -- let root2 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down)
113− -- let root3 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (firstChild root))
114− -- let root4 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (lastChild root))
115−
116− -- let result1 = 1 :< []
117− -- let result2 = 1 :< []
118− -- let result3 = 1 :< []
119− -- let result4 = 1 :< []
120− -- shouldEqual (eq root1 result1) true
121− -- shouldEqual (eq root2 result2) true
122− -- shouldEqual (eq root3 result3) true
123− -- shouldEqual (eq root4 result4) true
124−
125− it "Should findDownWhere with single node" do
126− let tree = 1 :< []
127− let loc = fromTree tree
128− shouldEqual (Just 1) ((findDownWhere (_ == 1) loc) <#> value)
129−
130− it "Should findDownWhere with 2 nodes and 2 levels" do
131− let tree = 1 :< [ leaf 2 ]
132− let loc = fromTree tree
133− shouldEqual (Just 2) ((findDownWhere (_ == 2) loc) <#> value)
134−
135− it "Should findDownWhere with 3 nodes and 2 levels" do
136− let tree = 1 :< [ leaf 2, leaf 3 ]
137− let loc = fromTree tree
138− shouldEqual (Just 3) ((findDownWhere (_ == 3) loc) <#> value)
139−
140− it "Should findDownWhere with 4 nodes and 2 levels" do
141− let tree = 1 :< (leaf <$> [ 2, 3, 4 ])
142− let loc = fromTree tree
143− shouldEqual (Just 4) ((findDownWhere (_ == 4) loc) <#> value)
144−
145− it "Should findDownWhere with 5 nodes and 3 levels" do
146− let
147− tree = 1 :<
148− [ leaf 2
149− , leaf 3
150− , 4 :< [ leaf 5 ]
151− ]
152− -- log $ showTree tree
153− let loc = fromTree tree
154− shouldEqual (Just 5) ((findDownWhere (_ == 5) loc) <#> value)
155−
156− it "Should findDownWhere with 6 nodes and 3 levels" do
157− let
158− tree = 1 :<
159− [ leaf 2
160− , leaf 3
161− , 4 :< [ leaf 5, leaf 6 ]
162− ]
163− -- log $ showTree tree
164− let loc = fromTree tree
165− shouldEqual (Just 6) ((findDownWhere (_ == 6) loc) <#> value)
166−
167− it "Should findDownWhere with 7 nodes and 4 levels" do
168− let
169− tree = 1 :<
170− [ leaf 2
171− , leaf 3
172− , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
173− ]
174− -- log $ showTree tree
175− let loc = fromTree tree
176− shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
177−
178− it "Should findDownWhere with 8 nodes and 4 levels" do
179− let
180− tree = 1 :<
181− [ leaf 2
182− , leaf 3
183− , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
184− , leaf 8
185− ]
186− -- log $ showTree tree
187− let loc = fromTree tree
188− shouldEqual (Just 8) ((findDownWhere (_ == 8) loc) <#> value)
189−
190− it "Should findDownWhere with 8 nodes and 4 levels with a step back" do
191− let
192− tree = 1 :<
193− [ leaf 2
194− , leaf 3
195− , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
196− , leaf 8
197− ]
198− -- log $ showTree tree
199− let loc = fromTree tree
200− shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
201−
202− it "Should find 7 from the sampleTree" do
203− shouldEqual (Just 7) (findDownWhere (_ == 7) (fromTree sampleTree) <#> value)
204−
205− it "Should find 8 from the sampleTree (the bottom) and then find 1 (the top) with findUp" do
206− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
207− shouldEqual (Just 1) (findUp 1 eight <#> value)
208−
209− it "Should find 8 from the sampleTree (the bottom) but then not find 7 because it would require a downward traversal" do
210− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
211− shouldEqual Nothing (findUp 7 eight <#> value)
212−
213− it "Should find 8 from the sampleTree (the bottom) and then find 7 using findFromRoot" do
214− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
215− shouldEqual (Just 7) (findFromRoot 7 eight <#> value)
216−
217− it "Should flatten the Tree into a list of locations following a depth first pattern" do
218− let flat = map value $ flattenLocDepthFirst $ fromTree sampleTree
219− --log $ showTree sampleTree
220− --log $ show flat
221− shouldEqual flat [1,2,3,4,5,6,7,8]
77+ let result =
78+ 1 :<
79+ (4 :< Nil)
80+ : (3 :< Nil)
81+ : (4 :<
82+ (5 :< Nil)
83+ : (6 :<
84+ (7 :< Nil) : Nil)
85+ : (8 :< Nil)
86+ : Nil
87+ )
88+ : Nil
89+
90+ let result' =
91+ 1 :<
92+ (2 :< Nil)
93+ : (3 :< Nil)
94+ : (4 :<
95+ (10 :< Nil)
96+ : (6 :<
97+ (7 :< Nil) : Nil)
98+ : (8 :< Nil)
99+ : Nil
100+ )
101+ : Nil
102+
103+ shouldEqual (eq root' result) true
104+ shouldEqual (eq root'' result') true
105+
106+ it "Insert" do
107+
108+ let root1 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ down root)
109+ let root2 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down)
110+ let root3 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (firstChild root))
111+ let root4 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (lastChild root))
112+
113+ let result1 =
114+ 1 :<
115+ (2 :< Nil)
116+ : (100 :< Nil)
117+ : (3 :< Nil)
118+ : (4 :<
119+ (5 :< Nil)
120+ : (6 :<
121+ (7 :< Nil) : Nil)
122+ : (8 :< Nil)
123+ : Nil
124+ )
125+ : Nil
126+
127+ let result2 =
128+ 1 :<
129+ (2 :< Nil)
130+ : (3 :< Nil)
131+ : (4 :<
132+ (5 :< Nil)
133+ : (6 :<
134+ (7 :< Nil)
135+ : (100 :< Nil)
136+ : Nil)
137+ : (8 :< Nil)
138+ : Nil
139+ )
140+ : Nil
141+ let result3 =
142+ 1 :<
143+ (2 :< Nil)
144+ : (100 :< Nil)
145+ : (3 :< Nil)
146+ : (4 :<
147+ (5 :< Nil)
148+ : (6 :<
149+ (7 :< Nil)
150+ : Nil
151+ )
152+ : (8 :< Nil)
153+ : Nil
154+ )
155+ : Nil
156+ let result4 =
157+ 1 :<
158+ (2 :< Nil)
159+ : (3 :< Nil)
160+ : (4 :<
161+ (5 :< Nil)
162+ : (6 :<
163+ (7 :< Nil) : Nil)
164+ : (8 :< Nil)
165+ : Nil
166+ )
167+ : (100 :< Nil)
168+ : Nil
169+ shouldEqual (eq root1 result1) true
170+ shouldEqual (eq root2 result2) true
171+ shouldEqual (eq root3 result3) true
172+ shouldEqual (eq root4 result4) true
173+
174+ it "Should findDownWhere with single node" do
175+ let tree = 1 :< Nil
176+ let loc = fromTree tree
177+ shouldEqual (Just 1) ((findDownWhere (_ == 1) loc) <#> value)
178+
179+ it "Should findDownWhere with 2 nodes and 2 levels" do
180+ let tree = 1 :<
181+ (2 :< Nil)
182+ : Nil
183+ -- log $ showTree tree
184+ let loc = fromTree tree
185+ shouldEqual (Just 2) ((findDownWhere (_ == 2) loc) <#> value)
186+
187+ it "Should findDownWhere with 3 nodes and 2 levels" do
188+ let tree = 1 :<
189+ (2 :< Nil)
190+ : (3 :< Nil)
191+ : Nil
192+ -- log $ showTree tree
193+ let loc = fromTree tree
194+ shouldEqual (Just 3) ((findDownWhere (_ == 3) loc) <#> value)
195+
196+ it "Should findDownWhere with 4 nodes and 2 levels" do
197+ let tree = 1 :<
198+ (2 :< Nil)
199+ : (3 :< Nil)
200+ : (4 :< Nil)
201+ : Nil
202+ -- log $ showTree tree
203+ let loc = fromTree tree
204+ shouldEqual (Just 4) ((findDownWhere (_ == 4) loc) <#> value)
205+
206+ it "Should findDownWhere with 5 nodes and 3 levels" do
207+ let tree = 1 :<
208+ (2 :< Nil)
209+ : (3 :< Nil)
210+ : (4 :<
211+ (5 :< Nil)
212+ : Nil)
213+ : Nil
214+ -- log $ showTree tree
215+ let loc = fromTree tree
216+ shouldEqual (Just 5) ((findDownWhere (_ == 5) loc) <#> value)
217+
218+ it "Should findDownWhere with 6 nodes and 3 levels" do
219+ let tree = 1 :<
220+ (2 :< Nil)
221+ : (3 :< Nil)
222+ : (4 :<
223+ (5 :< Nil)
224+ : (6 :< Nil)
225+ : Nil)
226+ : Nil
227+ -- log $ showTree tree
228+ let loc = fromTree tree
229+ shouldEqual (Just 6) ((findDownWhere (_ == 6) loc) <#> value)
230+
231+ it "Should findDownWhere with 7 nodes and 4 levels" do
232+ let tree = 1 :<
233+ (2 :< Nil)
234+ : (3 :< Nil)
235+ : (4 :<
236+ (5 :< Nil)
237+ : (6 :<
238+ (7 :< Nil) : Nil)
239+ : Nil)
240+ : Nil
241+ -- log $ showTree tree
242+ let loc = fromTree tree
243+ shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
244+
245+ it "Should findDownWhere with 8 nodes and 4 levels" do
246+ let tree = 1 :<
247+ (2 :< Nil)
248+ : (3 :< Nil)
249+ : (4 :<
250+ (5 :< Nil)
251+ : (6 :<
252+ (7 :< Nil) : Nil)
253+ : (8 :< Nil)
254+ : Nil)
255+ : Nil
256+ -- log $ showTree tree
257+ let loc = fromTree tree
258+ shouldEqual (Just 8) ((findDownWhere (_ == 8) loc) <#> value)
259+
260+ it "Should findDownWhere with 8 nodes and 4 levels with a step back" do
261+ let tree = 1 :<
262+ (2 :< Nil)
263+ : (3 :< Nil)
264+ : (4 :<
265+ (5 :< Nil)
266+ : (6 :<
267+ (7 :< Nil) : Nil)
268+ : (8 :< Nil)
269+ : Nil)
270+ : Nil
271+ -- log $ showTree tree
272+ let loc = fromTree tree
273+ shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
274+
275+ it "Should find 7 from the sampleTree" do
276+ shouldEqual (Just 7) (findDownWhere (_ == 7) (fromTree sampleTree) <#> value)
277+
278+ it "Should find 8 from the sampleTree (the bottom) and then find 1 (the top) with findUp" do
279+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
280+ shouldEqual (Just 1) (findUp 1 eight <#> value)
281+
282+ it "Should find 8 from the sampleTree (the bottom) but then not find 7 because it would require a downward traversal" do
283+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
284+ shouldEqual Nothing (findUp 7 eight <#> value)
285+
286+ it "Should find 8 from the sampleTree (the bottom) and then find 7 using findFromRoot" do
287+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
288+ shouldEqual (Just 7) (findFromRoot 7 eight <#> value)
289+
290+ it "Should flatten the Tree into a list of locations following a depth first pattern" do
291+ let flat = map value $ flattenLocDepthFirst $ fromTree sampleTree
292+ --log $ showTree sampleTree
293+ --log $ show flat
294+ shouldEqual flat (1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : Nil)