Skip to content

Commit 6ec2910

Browse files
fix findCycle (not the nicest fix) and add coverage requirements for the
tests
1 parent 8fc963a commit 6ec2910

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/Constrained/Graph.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ topsort gr@(Graph e _) = go [] e
171171
-- | Simple DFS cycle finding
172172
-- TODO: tests for this, currently it can produce a stem with a cycle after it
173173
findCycle :: Ord node => Graph node -> node -> [node]
174-
findCycle (Graph e _) node = concat . take 1 $ go mempty node
174+
findCycle g@(Graph e _) node = concat . take 1 $ filter loopy $ go mempty node
175175
where
176+
loopy [] = False
177+
loopy c@(x:_) = dependsOn (last c) x g
176178
go seen n
177179
| n `Set.member` seen = [[]]
178180
| otherwise = do

test/Constrained/GraphSpec.hs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ prop_delete_topsort g n =
3737

3838
prop_op_topsort :: Graph Node -> Property
3939
prop_op_topsort g =
40-
isRight (topsort g) === isRight (topsort $ opGraph g)
40+
cover 60 (isRight (topsort g)) "has topsort" $
41+
isRight (topsort g) == isRight (topsort $ opGraph g)
4142

4243
prop_trC_topsort :: Graph Node -> Property
4344
prop_trC_topsort g =
44-
isRight (topsort g) === isRight (topsort $ transitiveClosure g)
45+
cover 60 (isRight (topsort g)) "has topsort" $
46+
isRight (topsort g) == isRight (topsort $ transitiveClosure g)
4547

4648
prop_trC_opgraph_commute :: Graph Node -> Property
4749
prop_trC_opgraph_commute g =
@@ -58,13 +60,13 @@ prop_topsort_all_nodes :: Graph Node -> Property
5860
prop_topsort_all_nodes g =
5961
case topsort g of
6062
Left{} -> discard
61-
Right o -> Set.fromList o === nodes g
63+
Right o -> cover 60 True "has topsort" $ Set.fromList o === nodes g
6264

6365
prop_topsort_sound :: Graph Node -> Property
6466
prop_topsort_sound g =
6567
case topsort g of
6668
Left{} -> discard
67-
Right o -> property $ go o
69+
Right o -> cover 60 True "has topsort" $ property $ go o
6870
where
6971
go [] = True
7072
go (n : ns) = all (\n' -> not $ dependsOn n n' g) ns && go ns
@@ -82,22 +84,22 @@ prop_find_cycle_loops =
8284
forAllShrink (mkGraph @Node <$> arbitrary) shrink $ \ g ->
8385
conjoin
8486
[ case findCycle g n of
85-
[] -> discard
86-
c@(x:_) -> counterexample (show c) $ dependsOn (last c) x g
87+
[] -> property True
88+
c@(x:_) -> cover 40 True "found cycle" $ counterexample (show c) $ dependsOn (last c) x g
8789
| n <- Set.toList $ nodes g
8890
]
8991

9092
tests :: Bool -> Spec
9193
tests _nightly =
9294
describe "Graph tests" $ do
93-
prop "prop_no_dependencies_topsort" $ withMaxSuccess 10000 prop_no_dependencies_topsort
94-
prop "prop_subtract_topsort" $ withMaxSuccess 10000 prop_subtract_topsort
95-
prop "prop_delete_topsort" $ withMaxSuccess 10000 prop_delete_topsort
96-
prop "prop_op_topsort" $ withMaxSuccess 10000 prop_op_topsort
97-
prop "prop_trC_topsort" $ withMaxSuccess 10000 prop_trC_topsort
98-
prop "prop_trC_opgraph_commute" $ withMaxSuccess 10000 prop_trC_opgraph_commute
99-
prop "prop_depends_grows" $ withMaxSuccess 10000 prop_depends_grows
100-
prop "prop_topsort_all_nodes" $ withMaxSuccess 10000 prop_topsort_all_nodes
101-
prop "prop_topsort_sound" $ withMaxSuccess 10000 prop_topsort_sound
102-
prop "prop_find_cycle_sound" $ withMaxSuccess 10000 prop_find_cycle_sound
103-
prop "prop_find_cycle_loops" $ withMaxSuccess 10000 prop_find_cycle_loops
95+
prop "prop_no_dependencies_topsort" $ checkCoverage $ withMaxSuccess 10000 prop_no_dependencies_topsort
96+
prop "prop_subtract_topsort" $ checkCoverage $ withMaxSuccess 10000 prop_subtract_topsort
97+
prop "prop_delete_topsort" $ checkCoverage $ withMaxSuccess 10000 prop_delete_topsort
98+
prop "prop_op_topsort" $ checkCoverage $ withMaxSuccess 10000 prop_op_topsort
99+
prop "prop_trC_topsort" $ checkCoverage $ withMaxSuccess 10000 prop_trC_topsort
100+
prop "prop_trC_opgraph_commute" $ checkCoverage $ withMaxSuccess 10000 prop_trC_opgraph_commute
101+
prop "prop_depends_grows" $ checkCoverage $ withMaxSuccess 10000 prop_depends_grows
102+
prop "prop_topsort_all_nodes" $ checkCoverage $ withMaxSuccess 10000 prop_topsort_all_nodes
103+
prop "prop_topsort_sound" $ checkCoverage $ withMaxSuccess 10000 prop_topsort_sound
104+
prop "prop_find_cycle_sound" $ checkCoverage $ withMaxSuccess 10000 prop_find_cycle_sound
105+
prop "prop_find_cycle_loops" $ checkCoverage $ withMaxSuccess 10000 prop_find_cycle_loops

0 commit comments

Comments
 (0)