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