on master help sign in
Add tests for the directed graph modules
d1d28b9Jasper Van der Jeugt
src/Hakyll/Core/DirectedGraph.hs+8−19
@@ -4,6 +4,7 @@
4module Hakyll.Core.DirectedGraph
4module Hakyll.Core.DirectedGraph
5 ( DirectedGraph
5 ( DirectedGraph
6 , fromList
6 , fromList
7+ , nodes
7 , neighbours
8 , neighbours
8 , reverse
9 , reverse
9 , filter
10 , filter
@@ -26,6 +27,13 @@ fromList :: Ord a
26 -> DirectedGraph a -- ^ Resulting directed graph
27 -> DirectedGraph a -- ^ Resulting directed graph
27fromList = DirectedGraph . M.fromList . map (\(t, d) -> (t, Node t d))
28fromList = DirectedGraph . M.fromList . map (\(t, d) -> (t, Node t d))
28
29
30+-- | Get all nodes in the graph
31+--
32+nodes :: Ord a
33+ => DirectedGraph a -- ^ Graph to get the nodes from
34+ -> Set a -- ^ All nodes in the graph
35+nodes = M.keysSet . unDirectedGraph
36+
29-- | Get a set of reachable neighbours from a directed graph
37-- | Get a set of reachable neighbours from a directed graph
30--
38--
31neighbours :: Ord a
39neighbours :: Ord a
@@ -66,22 +74,3 @@ reachableNodes x graph = reachable (neighbours x graph) (S.singleton x)
66 sanitize = S.filter (`S.notMember` visited)
74 sanitize = S.filter (`S.notMember` visited)
67 neighbours' = S.unions $ map (flip neighbours graph)
75 neighbours' = S.unions $ map (flip neighbours graph)
68 $ S.toList $ sanitize next
76 $ S.toList $ sanitize next
69−
70−{-
71−exampleGraph :: DirectedGraph Int
72−exampleGraph = fromList
73− [ makeNode 8 [2, 4, 6]
74− , makeNode 2 [4, 3]
75− , makeNode 4 [3]
76− , makeNode 6 [4]
77− , makeNode 3 []
78− ]
79− where
80− makeNode tag deps = (tag, S.fromList deps)
81−
82−cyclic :: DirectedGraph Int
83−cyclic = fromList
84− [ (1, S.fromList [2])
85− , (2, S.fromList [1, 3])
86− ]
87−-}
src/Hakyll/Core/DirectedGraph/DependencySolver.hs+1−2
@@ -15,7 +15,6 @@ import qualified Data.Map as M
15import qualified Data.Set as S
15import qualified Data.Set as S
16
16
17import Hakyll.Core.DirectedGraph
17import Hakyll.Core.DirectedGraph
18−import qualified Hakyll.Core.DirectedGraph as DG
19import Hakyll.Core.DirectedGraph.Internal
18import Hakyll.Core.DirectedGraph.Internal
20
19
21-- | Solve a dependency graph. This function returns an order to run the
20-- | Solve a dependency graph. This function returns an order to run the
@@ -55,7 +54,7 @@ order temp stack set graph@(DirectedGraph graph')
55 -- All dependencies for node are satisfied, we can return it and
54 -- All dependencies for node are satisfied, we can return it and
56 -- remove it from the graph
55 -- remove it from the graph
57 [] -> order (tag : temp) stackTail (S.delete tag set)
56 [] -> order (tag : temp) stackTail (S.delete tag set)
58− (DG.filter (== tag) graph)
57+ (DirectedGraph $ M.delete tag graph')
59
58
60 -- There is at least one dependency left. We need to solve that
59 -- There is at least one dependency left. We need to solve that
61 -- one first...
60 -- one first...
src/Hakyll/Core/DirectedGraph/ObsoleteFilter.hs+3−3
@@ -4,7 +4,7 @@
4-- obsolete nodes.
4-- obsolete nodes.
5--
5--
6module Hakyll.Core.DirectedGraph.ObsoleteFilter
6module Hakyll.Core.DirectedGraph.ObsoleteFilter
7− ( obsoleteFilter
7+ ( filterObsolete
8 ) where
8 ) where
9
9
10import qualified Data.Set as S
10import qualified Data.Set as S
@@ -15,11 +15,11 @@ import qualified Hakyll.Core.DirectedGraph as DG
15-- | Given a list of obsolete items, filter the dependency graph so it only
15-- | Given a list of obsolete items, filter the dependency graph so it only
16-- contains these items
16-- contains these items
17--
17--
18−obsoleteFilter :: Ord a
18+filterObsolete :: Ord a
19 => [a] -- ^ List of obsolete items
19 => [a] -- ^ List of obsolete items
20 -> DirectedGraph a -- ^ Dependency graph
20 -> DirectedGraph a -- ^ Dependency graph
21 -> DirectedGraph a -- ^ Resulting dependency graph
21 -> DirectedGraph a -- ^ Resulting dependency graph
22−obsoleteFilter obsolete graph =
22+filterObsolete obsolete graph =
23 let reversed = DG.reverse graph
23 let reversed = DG.reverse graph
24 allObsolete = S.unions $ map (flip reachableNodes reversed) obsolete
24 allObsolete = S.unions $ map (flip reachableNodes reversed) obsolete
25 in DG.filter (`S.member` allObsolete) graph
25 in DG.filter (`S.member` allObsolete) graph
tests/Hakyll/Core/DirectedGraph/Tests.hsnew file+48
@@ -0,0 +1,48 @@
1+module Hakyll.Core.DirectedGraph.Tests
2+ ( tests
3+ ) where
4+
5+import Data.Set (Set)
6+import qualified Data.Set as S
7+
8+import Test.Framework
9+import Test.Framework.Providers.HUnit
10+import Test.HUnit hiding (Test)
11+
12+import Hakyll.Core.DirectedGraph
13+import Hakyll.Core.DirectedGraph.DependencySolver
14+import Hakyll.Core.DirectedGraph.ObsoleteFilter
15+
16+tests :: [Test]
17+tests =
18+ [ testCase "solveDependencies01" solveDependencies01
19+ , testCase "filterObsolete01" filterObsolete01
20+ , testCase "filterObsolete02" filterObsolete02
21+ ]
22+
23+node :: Ord a => a -> [a] -> (a, Set a)
24+node t n = (t, S.fromList n)
25+
26+testGraph01 :: DirectedGraph Int
27+testGraph01 = fromList
28+ [ node 8 [2, 4, 6]
29+ , node 2 [4, 3]
30+ , node 4 [3]
31+ , node 6 [4]
32+ , node 3 []
33+ ]
34+
35+solveDependencies01 :: Assertion
36+solveDependencies01 = result == [3, 4, 2, 6, 8] || result == [3, 4, 2, 6, 8]
37+ @? "solveDependencies01"
38+ where
39+ result = solveDependencies testGraph01
40+
41+filterObsolete01 :: Assertion
42+filterObsolete01 = nodes (filterObsolete [6] testGraph01) == S.fromList [6, 8]
43+ @? "filterObsolete01"
44+
45+filterObsolete02 :: Assertion
46+filterObsolete02 =
47+ nodes (filterObsolete [4] testGraph01) == S.fromList [4, 2, 6, 8]
48+ @? "filterObsolete02"
tests/TestSuite.hsnew file+11
@@ -0,0 +1,11 @@
1+module TestSuite where
2+
3+import Test.Framework (defaultMain, testGroup)
4+
5+import qualified Hakyll.Core.DirectedGraph.Tests
6+
7+main :: IO ()
8+main = defaultMain
9+ [ testGroup "Hakyll.Core.DirectedGraph.Tests"
10+ Hakyll.Core.DirectedGraph.Tests.tests
11+ ]