@@ -3,263 +3,219 @@ 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)
107import Effect (Effect)
8+import Effect.Aff (launchAff_)
119import Partial.Unsafe (unsafePartial)
1210import Test.Spec (describe, it)
1311import Test.Spec.Assertions (shouldEqual)
1412import 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)
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)
1623
1724sampleTree :: Tree Int
18−sampleTree =
19− 1 :<
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
25+sampleTree =
26+ 1 :<
27+ [ leaf 2
28+ , leaf 3
29+ , 4 :<
30+ [ leaf 5
31+ , 6 :< [ leaf 7 ]
32+ , leaf 8
33+ ]
34+
35+ ]
3036
3137main :: Effect Unit
32−main = run [consoleReporter] do
33− describe "Tree" do
38+main = launchAff_ $ runSpec [ consoleReporter ] do
39+ describe "Tree" do
3440
3541 it "mkTree" do
36− let t = mkTree 10 Nil
42+ let t = 10 :< []
3743 shouldEqual (head t) 10
3844
3945 it "Functor" do
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
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
5357
5458 it "scanTree" do
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
68−
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
71+
6972 describe "Zipper" do
7073
7174 let root = fromTree sampleTree
7275
7376 it "Modify" do
7477
75− 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)
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 root' = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ down root)
109− let root'' = unsafePartial $ toTree $ insertAfter (mkTree 100 Nil) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down)
110−
111− let result =
112− 1 :<
113− (2 :< Nil)
114− : (100 :< Nil)
115− : (3 :< Nil)
116− : (4 :<
117− (5 :< Nil)
118− : (6 :<
119− (7 :< Nil) : Nil)
120− : (8 :< Nil)
121− : Nil
122− )
123− : Nil
124−
125− let result' =
126− 1 :<
127− (2 :< Nil)
128− : (3 :< Nil)
129− : (4 :<
130− (5 :< Nil)
131− : (6 :<
132− (7 :< Nil)
133− : (100 :< Nil)
134− : Nil)
135− : (8 :< Nil)
136− : Nil
137− )
138− : Nil
139− shouldEqual (eq root' result) true
140− shouldEqual (eq root'' result') true
141−
142− it "Should findDownWhere with single node" do
143− let tree = 1 :< Nil
144− let loc = fromTree tree
145− shouldEqual (Just 1) ((findDownWhere (_ == 1) loc) <#> value)
146−
147− it "Should findDownWhere with 2 nodes and 2 levels" do
148− let tree = 1 :<
149− (2 :< Nil)
150− : Nil
151− -- log $ showTree tree
152− let loc = fromTree tree
153− shouldEqual (Just 2) ((findDownWhere (_ == 2) loc) <#> value)
154−
155− it "Should findDownWhere with 3 nodes and 2 levels" do
156− let tree = 1 :<
157− (2 :< Nil)
158− : (3 :< Nil)
159− : Nil
160− -- log $ showTree tree
161− let loc = fromTree tree
162− shouldEqual (Just 3) ((findDownWhere (_ == 3) loc) <#> value)
163−
164− it "Should findDownWhere with 4 nodes and 2 levels" do
165− let tree = 1 :<
166− (2 :< Nil)
167− : (3 :< Nil)
168− : (4 :< Nil)
169− : Nil
170− -- log $ showTree tree
171− let loc = fromTree tree
172− shouldEqual (Just 4) ((findDownWhere (_ == 4) loc) <#> value)
173−
174− it "Should findDownWhere with 5 nodes and 3 levels" do
175− let tree = 1 :<
176− (2 :< Nil)
177− : (3 :< Nil)
178− : (4 :<
179− (5 :< Nil)
180− : Nil)
181− : Nil
182− -- log $ showTree tree
183− let loc = fromTree tree
184− shouldEqual (Just 5) ((findDownWhere (_ == 5) loc) <#> value)
185−
186− it "Should findDownWhere with 6 nodes and 3 levels" do
187− let tree = 1 :<
188− (2 :< Nil)
189− : (3 :< Nil)
190− : (4 :<
191− (5 :< Nil)
192− : (6 :< Nil)
193− : Nil)
194− : Nil
195− -- log $ showTree tree
196− let loc = fromTree tree
197− shouldEqual (Just 6) ((findDownWhere (_ == 6) loc) <#> value)
198−
199− it "Should findDownWhere with 7 nodes and 4 levels" do
200− let tree = 1 :<
201− (2 :< Nil)
202− : (3 :< Nil)
203− : (4 :<
204− (5 :< Nil)
205− : (6 :<
206− (7 :< Nil) : Nil)
207− : Nil)
208− : Nil
209− -- log $ showTree tree
210− let loc = fromTree tree
211− shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
212−
213− it "Should findDownWhere with 8 nodes and 4 levels" do
214− let tree = 1 :<
215− (2 :< Nil)
216− : (3 :< Nil)
217− : (4 :<
218− (5 :< Nil)
219− : (6 :<
220− (7 :< Nil) : Nil)
221− : (8 :< Nil)
222− : Nil)
223− : Nil
224− -- log $ showTree tree
225− let loc = fromTree tree
226− shouldEqual (Just 8) ((findDownWhere (_ == 8) loc) <#> value)
227−
228− it "Should findDownWhere with 8 nodes and 4 levels with a step back" do
229− let tree = 1 :<
230− (2 :< Nil)
231− : (3 :< Nil)
232− : (4 :<
233− (5 :< Nil)
234− : (6 :<
235− (7 :< Nil) : Nil)
236− : (8 :< Nil)
237− : Nil)
238− : Nil
239− -- log $ showTree tree
240− let loc = fromTree tree
241− shouldEqual (Just 7) ((findDownWhere (_ == 7) loc) <#> value)
242−
243− it "Should find 7 from the sampleTree" do
244− shouldEqual (Just 7) (findDownWhere (_ == 7) (fromTree sampleTree) <#> value)
245−
246− it "Should find 8 from the sampleTree (the bottom) and then find 1 (the top) with findUp" do
247− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
248− shouldEqual (Just 1) (findUp 1 eight <#> value)
249−
250− it "Should find 8 from the sampleTree (the bottom) but then not find 7 because it would require a downward traversal" do
251− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
252− shouldEqual Nothing (findUp 7 eight <#> value)
253−
254− it "Should find 8 from the sampleTree (the bottom) and then find 7 using findFromRoot" do
255− let eight = unsafePartial $ fromJust $ findDownWhere (_ == 8) $ fromTree sampleTree
256− shouldEqual (Just 7) (findFromRoot 7 eight <#> value)
257−
258− it "Should flatten the Tree into a list of locations following a depth first pattern" do
259− let flat = map value $ flattenLocDepthFirst $ fromTree sampleTree
260− --log $ showTree sampleTree
261− --log $ show flat
262− shouldEqual flat (1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : Nil)
263−
264−
265−
78+ let root' = unsafePartial $ toTree $ modifyValue (\a -> 2 * a) (fromJust $ down root)
79+ 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]