@@ -3,68 +3,71 @@ 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)
8+import Effect.Aff (launchAff_)
11import Partial.Unsafe (unsafePartial)
9import Partial.Unsafe (unsafePartial)
12import Test.Spec (describe, it)
10import Test.Spec (describe, it)
13import Test.Spec.Assertions (shouldEqual)
11import Test.Spec.Assertions (shouldEqual)
14import Test.Spec.Reporter (consoleReporter)
12import Test.Spec.Reporter (consoleReporter)
15−import Test.Spec.Runner (run)
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)
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)
17sampleTree :: Tree Int
24sampleTree :: Tree Int
18sampleTree =
25sampleTree =
20− (2 :< Nil)
27+ [ leaf 2
21− : (3 :< Nil)
28+ , leaf 3
23− (5 :< Nil)
30+ [ leaf 5
24− : (6 :<
31+ , 6 :< [ leaf 7 ]
25− (7 :< Nil) : Nil)
32+ , leaf 8
31main :: Effect Unit
37main :: Effect Unit
32−main = run [consoleReporter] do
38+main = launchAff_ $ runSpec [ consoleReporter ] do
33 describe "Tree" do
39 describe "Tree" do
35 it "mkTree" do
41 it "mkTree" do
36− let t = mkTree 10 Nil
42+ let t = 10 :< []
37 shouldEqual (head t) 10
43 shouldEqual (head t) 10
39 it "Functor" do
45 it "Functor" do
40− let result =
47+ result = 2 :<
42− (3 :< Nil)
49+ , leaf 4
45− (6 :< Nil)
51+ [ leaf 6
46− : (7 :<
52+ , 7 :< [ leaf 8 ]
47− (8 :< Nil) : Nil)
53+ , leaf 9
50− )
56+ SpecTree (((+) 1) <$> sampleTree) `shouldEqual` SpecTree result
52− shouldEqual (eq (((+)1) <$> sampleTree) result) true
54 it "scanTree" do
58 it "scanTree" do
55− let result =
60+ result =
57− (3 :< Nil)
62+ [ leaf 3
58− : (4 :< Nil)
63+ , leaf 4
60− (10 :< Nil)
65+ [ leaf 10
61− : (11 :<
66+ , 11 :< [ leaf 18 ]
62− (18 :< Nil) : Nil)
67+ , leaf 13
67− shouldEqual (eq (scanTree (\a b -> a + b) 0 sampleTree) result) true
70+ SpecTree (scanTree (\a b -> a + b) 0 sampleTree) `shouldEqual` SpecTree result
69 describe "Zipper" do
72 describe "Zipper" do
@@ -74,221 +77,145 @@ main = run [consoleReporter] do
75 let root' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down root)
78 let root' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down root)
76 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)
77− let result =
82+ result =
79− (4 :< Nil)
84+ [ leaf 4
80− : (3 :< Nil)
85+ , leaf 3
82− (5 :< Nil)
87+ [ leaf 5
83− : (6 :<
88+ , 6 :< [ leaf 7 ]
84− (7 :< Nil) : Nil)
89+ , leaf 8
90− let result' =
95+ result' =
92− (2 :< Nil)
97+ [ 2 :< []
93− : (3 :< Nil)
98+ , 3 :< []
95− (10 :< Nil)
100+ [ 10 :< []
96− : (6 :<
101+ , 6 :< [ 7 :< [] ]
98− : (8 :< Nil)
102+ , 8 :< []
102−
106+ SpecTree root' `shouldEqual` SpecTree result
103− shouldEqual (eq root' result) true
107+ SpecTree root'' `shouldEqual` SpecTree result'
104− shouldEqual (eq root'' result') true
108+
106− it "Insert" do
109+ -- it "Insert" do
108− let root1 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ down root)
111+ -- let root1 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ down root)
109− 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)
110− let root3 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (firstChild root))
113+ -- let root3 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (firstChild root))
111− let root4 = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (lastChild root))
114+ -- let root4 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (lastChild root))
113− let result1 =
116+ -- let result1 = 1 :< []
127− let result2 =
117+ -- let result2 = 1 :< []
141− let result3 =
118+ -- let result3 = 1 :< []
156− let result4 =
119+ -- let result4 = 1 :< []
169− shouldEqual (eq root1 result1) true
120+ -- shouldEqual (eq root1 result1) true
170− shouldEqual (eq root2 result2) true
121+ -- shouldEqual (eq root2 result2) true
171− shouldEqual (eq root3 result3) true
122+ -- shouldEqual (eq root3 result3) true
172− shouldEqual (eq root4 result4) true
123+ -- shouldEqual (eq root4 result4) true
174− it "Should findDownWhere with single node" do
125+ it "Should findDownWhere with single node" do
175− let tree = 1 :< Nil
126+ let tree = 1 :< []
176− let loc = fromTree tree
127+ let loc = fromTree tree
177− shouldEqual (Just 1) ((findDownWhere (_ == 1) loc) <#> value)
128+ shouldEqual (Just 1) ((findDownWhere (_ == 1) loc) <#> value)
179− it "Should findDownWhere with 2 nodes and 2 levels" do
130+ it "Should findDownWhere with 2 nodes and 2 levels" do
180− let tree = 1 :<
131+ let tree = 1 :< [ leaf 2 ]
183− -- log $ showTree tree
184− let loc = fromTree tree
132+ let loc = fromTree tree
185− shouldEqual (Just 2) ((findDownWhere (_ == 2) loc) <#> value)
133+ shouldEqual (Just 2) ((findDownWhere (_ == 2) loc) <#> value)
187− it "Should findDownWhere with 3 nodes and 2 levels" do
135+ it "Should findDownWhere with 3 nodes and 2 levels" do
188− let tree = 1 :<
136+ let tree = 1 :< [ leaf 2, leaf 3 ]
192− -- log $ showTree tree
193− let loc = fromTree tree
137+ let loc = fromTree tree
194− shouldEqual (Just 3) ((findDownWhere (_ == 3) loc) <#> value)
138+ shouldEqual (Just 3) ((findDownWhere (_ == 3) loc) <#> value)
196− it "Should findDownWhere with 4 nodes and 2 levels" do
140+ it "Should findDownWhere with 4 nodes and 2 levels" do
197− let tree = 1 :<
141+ let tree = 1 :< (leaf <$> [ 2, 3, 4 ])
202− -- log $ showTree tree
203− let loc = fromTree tree
142+ let loc = fromTree tree
204− shouldEqual (Just 4) ((findDownWhere (_ == 4) loc) <#> value)
143+ shouldEqual (Just 4) ((findDownWhere (_ == 4) loc) <#> value)
206− it "Should findDownWhere with 5 nodes and 3 levels" do
145+ it "Should findDownWhere with 5 nodes and 3 levels" do
207− let tree = 1 :<
147+ tree = 1 :<
208− (2 :< Nil)
148+ [ leaf 2
209− : (3 :< Nil)
149+ , leaf 3
210− : (4 :<
150+ , 4 :< [ leaf 5 ]
214− -- log $ showTree tree
152+ -- log $ showTree tree
215− let loc = fromTree tree
153+ let loc = fromTree tree
216− shouldEqual (Just 5) ((findDownWhere (_ == 5) loc) <#> value)
154+ shouldEqual (Just 5) ((findDownWhere (_ == 5) loc) <#> value)
218− it "Should findDownWhere with 6 nodes and 3 levels" do
156+ it "Should findDownWhere with 6 nodes and 3 levels" do
219− let tree = 1 :<
158+ tree = 1 :<
220− (2 :< Nil)
159+ [ leaf 2
221− : (3 :< Nil)
160+ , leaf 3
222− : (4 :<
161+ , 4 :< [ leaf 5, leaf 6 ]
227− -- log $ showTree tree
163+ -- log $ showTree tree
228− let loc = fromTree tree
164+ let loc = fromTree tree
229− shouldEqual (Just 6) ((findDownWhere (_ == 6) loc) <#> value)
165+ shouldEqual (Just 6) ((findDownWhere (_ == 6) loc) <#> value)
231− it "Should findDownWhere with 7 nodes and 4 levels" do
167+ it "Should findDownWhere with 7 nodes and 4 levels" do
232− let tree = 1 :<
169+ tree = 1 :<
233− (2 :< Nil)
170+ [ leaf 2
234− : (3 :< Nil)
171+ , leaf 3
235− : (4 :<
172+ , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
241− -- log $ showTree tree
174+ -- log $ showTree tree
242− let loc = fromTree tree
175+ let loc = fromTree tree
243− shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
176+ shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
245− it "Should findDownWhere with 8 nodes and 4 levels" do
178+ it "Should findDownWhere with 8 nodes and 4 levels" do
246− let tree = 1 :<
180+ tree = 1 :<
247− (2 :< Nil)
181+ [ leaf 2
248− : (3 :< Nil)
182+ , leaf 3
249− : (4 :<
183+ , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
250− (5 :< Nil)
184+ , leaf 8
256− -- log $ showTree tree
186+ -- log $ showTree tree
257− let loc = fromTree tree
187+ let loc = fromTree tree
258− shouldEqual (Just 8) ((findDownWhere (_ == 8) loc) <#> value)
188+ shouldEqual (Just 8) ((findDownWhere (_ == 8) loc) <#> value)
260− 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
261− let tree = 1 :<
192+ tree = 1 :<
262− (2 :< Nil)
193+ [ leaf 2
263− : (3 :< Nil)
194+ , leaf 3
264− : (4 :<
195+ , 4 :< [ leaf 5, 6 :< [ leaf 7 ] ]
265− (5 :< Nil)
196+ , leaf 8
271− -- log $ showTree tree
198+ -- log $ showTree tree
272− let loc = fromTree tree
199+ let loc = fromTree tree
273− shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
200+ shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
275− it "Should find 7 from the sampleTree" do
202+ it "Should find 7 from the sampleTree" do
276− shouldEqual (Just 7) (findDownWhere (_ == 7) (fromTree sampleTree) <#> value)
203+ shouldEqual (Just 7) (findDownWhere (_ == 7) (fromTree sampleTree) <#> value)
278− 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
279− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
206+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
280− shouldEqual (Just 1) (findUp 1 eight <#> value)
207+ shouldEqual (Just 1) (findUp 1 eight <#> value)
282− 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
283− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
210+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
284− shouldEqual Nothing (findUp 7 eight <#> value)
211+ shouldEqual Nothing (findUp 7 eight <#> value)
286− 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
287− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
214+ let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
288− shouldEqual (Just 7) (findFromRoot 7 eight <#> value)
215+ shouldEqual (Just 7) (findFromRoot 7 eight <#> value)
290− 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
291− let flat = map value $ flattenLocDepthFirst $ fromTree sampleTree
218+ let flat = map value $ flattenLocDepthFirst $ fromTree sampleTree
292− --log $ showTree sampleTree
219+ --log $ showTree sampleTree
293− --log $ show flat
220+ --log $ show flat
294− shouldEqual flat (1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : Nil)
221+ shouldEqual flat [1,2,3,4,5,6,7,8]