@@ -3,10 +3,7 @@ module Test.Main where
3import Prelude
3import Prelude
5import Control.Comonad.Cofree (head, (:<))
5import Control.Comonad.Cofree (head, (:<))
6−import Data.List (List(Nil), (:))
7import Data.Maybe (Maybe(..), fromJust)
6import 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)
10import Effect (Effect)
7import Effect (Effect)
11import Effect.Aff (launchAff_)
8import Effect.Aff (launchAff_)
12import Partial.Unsafe (unsafePartial)
9import Partial.Unsafe (unsafePartial)
@@ -14,58 +11,63 @@ import Test.Spec (describe, it)
14import Test.Spec.Assertions (shouldEqual)
11import Test.Spec.Assertions (shouldEqual)
15import Test.Spec.Reporter (consoleReporter)
12import Test.Spec.Reporter (consoleReporter)
16import Test.Spec.Runner (runSpec)
13import 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)
17+newtype SpecTree a = SpecTree (Tree a)
19+instance Show a => Show (SpecTree a) where
20+ show (SpecTree t) = showTree t
22+derive instance (Eq a) => Eq (SpecTree a)
18sampleTree :: Tree Int
24sampleTree :: Tree Int
19sampleTree =
25sampleTree =
21− (2 :< Nil)
27+ [ leaf 2
22− : (3 :< Nil)
28+ , leaf 3
24− (5 :< Nil)
30+ [ leaf 5
25− : (6 :<
31+ , 6 :< [ leaf 7 ]
26− (7 :< Nil) : Nil)
32+ , leaf 8
32main :: Effect Unit
37main :: Effect Unit
33−main = launchAff_ $ runSpec [consoleReporter] do
38+main = launchAff_ $ runSpec [ consoleReporter ] do
34 describe "Tree" do
39 describe "Tree" do
36 it "mkTree" do
41 it "mkTree" do
37− let t = mkTree 10 Nil
42+ let t = 10 :< []
38 shouldEqual (head t) 10
43 shouldEqual (head t) 10
40 it "Functor" do
45 it "Functor" do
41− let result =
47+ result = 2 :<
43− (3 :< Nil)
49+ , leaf 4
46− (6 :< Nil)
51+ [ leaf 6
47− : (7 :<
52+ , 7 :< [ leaf 8 ]
48− (8 :< Nil) : Nil)
53+ , leaf 9
51− )
56+ SpecTree (((+) 1) <$> sampleTree) `shouldEqual` SpecTree result
53− shouldEqual (eq (((+)1) <$> sampleTree) result) true
55 it "scanTree" do
58 it "scanTree" do
56− let result =
60+ result =
58− (3 :< Nil)
62+ [ leaf 3
59− : (4 :< Nil)
63+ , leaf 4
61− (10 :< Nil)
65+ [ leaf 10
62− : (11 :<
66+ , 11 :< [ leaf 18 ]
63− (18 :< Nil) : Nil)
67+ , leaf 13
68− shouldEqual (eq (scanTree (\a b -> a + b) 0 sampleTree) result) true
70+ SpecTree (scanTree (\a b -> a + b) 0 sampleTree) `shouldEqual` SpecTree result
70 describe "Zipper" do
72 describe "Zipper" do
@@ -75,221 +77,145 @@ main = launchAff_ $ runSpec [consoleReporter] do
76 let root' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down root)
78 let root' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down root)
77 let root'' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down >=> next >=> next >=> down $ root)
79 let root'' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down >=> next >=> next >=> down $ root)
78− let result =
82+ result =
80− (4 :< Nil)
84+ [ leaf 4
81− : (3 :< Nil)
85+ , leaf 3
83− (5 :< Nil)
87+ [ leaf 5
84− : (6 :<
88+ , 6 :< [ leaf 7 ]
85− (7 :< Nil) : Nil)
89+ , leaf 8
91− let result' =
95+ result' =
93− (2 :< Nil)
97+ [ 2 :< []
94− : (3 :< Nil)
98+ , 3 :< []
96− (10 :< Nil)
100+ [ 10 :< []
97− : (6 :<
101+ , 6 :< [ 7 :< [] ]
99− : (8 :< Nil)
102+ , 8 :< []
103−
106+ SpecTree root' `shouldEqual` SpecTree result
104− shouldEqual (eq root' result) true
107+ SpecTree root'' `shouldEqual` SpecTree result'
105− shouldEqual (eq root'' result') true
108+
107− it "Insert" do
109+ -- it "Insert" do
109− let root1 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ down root)
111+ -- let root1 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ down root)
110− let root2 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down)
112+ -- let root2 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down)
111− let root3 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (firstChild root))
113+ -- let root3 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (firstChild root))
112− let root4 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (lastChild root))
114+ -- let root4 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (lastChild root))
114− let result1 =
116+ -- let result1 = 1 :< []
128− let result2 =
117+ -- let result2 = 1 :< []
142− let result3 =
118+ -- let result3 = 1 :< []
157− let result4 =
119+ -- let result4 = 1 :< []
170− shouldEqual (eq root1 result1) true
120+ -- shouldEqual (eq root1 result1) true
171− shouldEqual (eq root2 result2) true
121+ -- shouldEqual (eq root2 result2) true
172− shouldEqual (eq root3 result3) true
122+ -- shouldEqual (eq root3 result3) true
173− shouldEqual (eq root4 result4) true
123+ -- shouldEqual (eq root4 result4) true
175− it "Should findDownWhere with single node" do
125+ it "Should findDownWhere with single node" do
176− let tree = 1 :< Nil
126+ let tree = 1 :< []
177− let loc = fromTree tree
127+ let loc = fromTree tree
178− shouldEqual (Just 1) ((findDownWhere (_ == 1) loc) <#> value)
128+ shouldEqual (Just 1) ((findDownWhere (_ == 1) loc) <#> value)
180− it "Should findDownWhere with 2 nodes and 2 levels" do
130+ it "Should findDownWhere with 2 nodes and 2 levels" do
181− let tree = 1 :<
131+ let tree = 1 :< [ leaf 2 ]
184− -- log $ showTree tree
185− let loc = fromTree tree
132+ let loc = fromTree tree
186− shouldEqual (Just 2) ((findDownWhere (_ == 2) loc) <#> value)
133+ shouldEqual (Just 2) ((findDownWhere (_ == 2) loc) <#> value)
188− it "Should findDownWhere with 3 nodes and 2 levels" do
135+ it "Should findDownWhere with 3 nodes and 2 levels" do
189− let tree = 1 :<
136+ let tree = 1 :< [ leaf 2, leaf 3 ]
193− -- log $ showTree tree
194− let loc = fromTree tree
137+ let loc = fromTree tree
195− shouldEqual (Just 3) ((findDownWhere (_ == 3) loc) <#> value)
138+ shouldEqual (Just 3) ((findDownWhere (_ == 3) loc) <#> value)
197− it "Should findDownWhere with 4 nodes and 2 levels" do
140+ it "Should findDownWhere with 4 nodes and 2 levels" do
198− let tree = 1 :<
141+ let tree = 1 :< (leaf <$> [ 2, 3, 4 ])
203− -- log $ showTree tree
204− let loc = fromTree tree
142+ let loc = fromTree tree
205− shouldEqual (Just 4) ((findDownWhere (_ == 4) loc) <#> value)
143+ shouldEqual (Just 4) ((findDownWhere (_ == 4) loc) <#> value)
207− it "Should findDownWhere with 5 nodes and 3 levels" do
145+ it "Should findDownWhere with 5 nodes and 3 levels" do
208− let tree = 1 :<
147+ tree = 1 :<
209− (2 :< Nil)
148+ [ leaf 2
210− : (3 :< Nil)
149+ , leaf 3
211− : (4 :<
150+ , 4 :< [ leaf 5 ]
215− -- log $ showTree tree
152+ -- log $ showTree tree
216− let loc = fromTree tree
153+ let loc = fromTree tree
217− shouldEqual (Just 5) ((findDownWhere (_ == 5) loc) <#> value)
154+ shouldEqual (Just 5) ((findDownWhere (_ == 5) loc) <#> value)
219− it "Should findDownWhere with 6 nodes and 3 levels" do
156+ it "Should findDownWhere with 6 nodes and 3 levels" do
220− let tree = 1 :<
158+ tree = 1 :<
221− (2 :< Nil)
159+ [ leaf 2
222− : (3 :< Nil)
160+ , leaf 3
223− : (4 :<
161+ , 4 :< [ leaf 5, leaf 6 ]
228− -- log $ showTree tree
163+ -- log $ showTree tree
229− let loc = fromTree tree
164+ let loc = fromTree tree
230− shouldEqual (Just 6) ((findDownWhere (_ == 6) loc) <#> value)
165+ shouldEqual (Just 6) ((findDownWhere (_ == 6) loc) <#> value)
232− it "Should findDownWhere with 7 nodes and 4 levels" do
167+ it "Should findDownWhere with 7 nodes and 4 levels" do
233− let tree = 1 :<
169+ tree = 1 :<
234− (2 :< Nil)
170+ [ leaf 2
235− : (3 :< Nil)
171+ , leaf 3
236− : (4 :<
172+ , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
242− -- log $ showTree tree
174+ -- log $ showTree tree
243− let loc = fromTree tree
175+ let loc = fromTree tree
244− shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
176+ shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
246− it "Should findDownWhere with 8 nodes and 4 levels" do
178+ it "Should findDownWhere with 8 nodes and 4 levels" do
247− let tree = 1 :<
180+ tree = 1 :<
248− (2 :< Nil)
181+ [ leaf 2
249− : (3 :< Nil)
182+ , leaf 3
250− : (4 :<
183+ , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
251− (5 :< Nil)
184+ , leaf 8
257− -- log $ showTree tree
186+ -- log $ showTree tree
258− let loc = fromTree tree
187+ let loc = fromTree tree
259− shouldEqual (Just 8) ((findDownWhere (_ == 8) loc) <#> value)
188+ shouldEqual (Just 8) ((findDownWhere (_ == 8) loc) <#> value)
261− it "Should findDownWhere with 8 nodes and 4 levels with a step back" do
190+ it "Should findDownWhere with 8 nodes and 4 levels with a step back" do
262− let tree = 1 :<
192+ tree = 1 :<
263− (2 :< Nil)
193+ [ leaf 2
264− : (3 :< Nil)
194+ , leaf 3
265− : (4 :<
195+ , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
266− (5 :< Nil)
196+ , leaf 8
272− -- log $ showTree tree
198+ -- log $ showTree tree
273− let loc = fromTree tree
199+ let loc = fromTree tree
274− shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
200+ shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
276− it "Should find 7 from the sampleTree" do
202+ it "Should find 7 from the sampleTree" do
277− shouldEqual (Just 7) (findDownWhere (_ == 7) (fromTree sampleTree) <#> value)
203+ shouldEqual (Just 7) (findDownWhere (_ == 7) (fromTree sampleTree) <#> value)
279− it "Should find 8 from the sampleTree (the bottom) and then find 1 (the top) with findUp" do
205+ 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
206+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
281− shouldEqual (Just 1) (findUp 1 eight <#> value)
207+ shouldEqual (Just 1) (findUp 1 eight <#> value)
283− it "Should find 8 from the sampleTree (the bottom) but then not find 7 because it would require a downward traversal" do
209+ 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
210+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
285− shouldEqual Nothing (findUp 7 eight <#> value)
211+ shouldEqual Nothing (findUp 7 eight <#> value)
287− it "Should find 8 from the sampleTree (the bottom) and then find 7 using findFromRoot" do
213+ 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
214+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
289− shouldEqual (Just 7) (findFromRoot 7 eight <#> value)
215+ shouldEqual (Just 7) (findFromRoot 7 eight <#> value)
291− it "Should flatten the Tree into a list of locations following a depth first pattern" do
217+ it "Should flatten the Tree into a list of locations following a depth first pattern" do
292− let flat = map value $ flattenLocDepthFirst $ fromTree sampleTree
218+ let flat = map value $ flattenLocDepthFirst $ fromTree sampleTree
293− --log $ showTree sampleTree
219+ --log $ showTree sampleTree
294− --log $ show flat
220+ --log $ show flat
295− shouldEqual flat (1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : Nil)
221+ shouldEqual flat [1,2,3,4,5,6,7,8]