|
| 1 | +{-# LANGUAGE DerivingStrategies #-} |
| 2 | +{-# LANGUAGE TemplateHaskell #-} |
| 3 | +{-# LANGUAGE TypeApplications #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE NumericUnderscores #-} |
| 6 | +{-# LANGUAGE ImportQualifiedPost #-} |
| 7 | +{-# LANGUAGE DerivingVia #-} |
| 8 | +module Constrained.GraphSpec where |
| 9 | + |
| 10 | +import Data.Either |
| 11 | +import Constrained.Graph |
| 12 | +import Data.Set (Set) |
| 13 | +import Data.Set qualified as Set |
| 14 | +import Test.Hspec |
| 15 | +import Test.Hspec.QuickCheck |
| 16 | +import Test.QuickCheck |
| 17 | + |
| 18 | +newtype Node = Node Int |
| 19 | + deriving (Ord, Eq) |
| 20 | + deriving Show via Int |
| 21 | + |
| 22 | +instance Arbitrary Node where |
| 23 | + arbitrary = Node <$> choose (0, 20) |
| 24 | + shrink (Node n) = Node <$> shrink n |
| 25 | + |
| 26 | +prop_arbitrary_reasonable_distribution :: Graph Node -> Property |
| 27 | +prop_arbitrary_reasonable_distribution g = |
| 28 | + cover 60 (isRight $ topsort g) "has topsort" True |
| 29 | + |
| 30 | +prop_no_dependencies_topsort :: Set Node -> Property |
| 31 | +prop_no_dependencies_topsort = property . isRight . topsort . noDependencies |
| 32 | + |
| 33 | +prop_subtract_topsort :: Graph Node -> Graph Node -> Property |
| 34 | +prop_subtract_topsort g g' = |
| 35 | + isRight (topsort g) ==> |
| 36 | + isRight (topsort $ subtractGraph g g') |
| 37 | + |
| 38 | +prop_subtract_union :: Graph Node -> Graph Node -> Property |
| 39 | +prop_subtract_union g g0' = |
| 40 | + let g' = subtractGraph g g0' |
| 41 | + in subtractGraph g g' <> g' === g |
| 42 | + |
| 43 | +prop_subtract_keeps_nodes :: Graph Node -> Graph Node -> Property |
| 44 | +prop_subtract_keeps_nodes g g' = nodes (subtractGraph g g') === nodes g |
| 45 | + |
| 46 | +prop_subtract_removes_edges :: Graph Node -> Graph Node -> Node -> Node -> Property |
| 47 | +prop_subtract_removes_edges g g' x y = |
| 48 | + property $ not (dependsOn x y (subtractGraph (dependency x (Set.singleton y) <> g) $ dependency x (Set.singleton y) <> g')) |
| 49 | + |
| 50 | +prop_union_commutes :: Graph Node -> Graph Node -> Property |
| 51 | +prop_union_commutes g g' = g <> g' === g' <> g |
| 52 | + |
| 53 | +prop_delete_topsort :: Graph Node -> Node -> Property |
| 54 | +prop_delete_topsort g n = |
| 55 | + isRight (topsort g) ==> |
| 56 | + isRight (topsort $ deleteNode n g) |
| 57 | + |
| 58 | +prop_op_topsort :: Graph Node -> Property |
| 59 | +prop_op_topsort g = |
| 60 | + isRight (topsort g) === isRight (topsort $ opGraph g) |
| 61 | + |
| 62 | +prop_trC_topsort :: Graph Node -> Property |
| 63 | +prop_trC_topsort g = |
| 64 | + isRight (topsort g) === isRight (topsort $ transitiveClosure g) |
| 65 | + |
| 66 | +prop_trC_opgraph_commute :: Graph Node -> Property |
| 67 | +prop_trC_opgraph_commute g = |
| 68 | + transitiveClosure (opGraph g) === opGraph (transitiveClosure g) |
| 69 | + |
| 70 | +prop_depends_grows :: Graph Node -> Graph Node -> Node -> Property |
| 71 | +prop_depends_grows g g' n = property $ dependencies n g `Set.isSubsetOf` dependencies n (g <> g') |
| 72 | + |
| 73 | +prop_transitive_dependencies :: Graph Node -> Node -> Property |
| 74 | +prop_transitive_dependencies g n = |
| 75 | + transitiveDependencies n g === dependencies n (transitiveClosure g) |
| 76 | + |
| 77 | +prop_topsort_all_nodes :: Graph Node -> Property |
| 78 | +prop_topsort_all_nodes g = |
| 79 | + case topsort g of |
| 80 | + Left{} -> discard |
| 81 | + Right o -> Set.fromList o === nodes g |
| 82 | + |
| 83 | +prop_topsort_sound :: Graph Node -> Property |
| 84 | +prop_topsort_sound g = |
| 85 | + case topsort g of |
| 86 | + Left{} -> discard |
| 87 | + Right o -> property $ go o |
| 88 | + where |
| 89 | + go [] = True |
| 90 | + go (n : ns) = all (\n' -> not $ dependsOn n n' g) ns && go ns |
| 91 | + |
| 92 | +prop_topsort_complete :: Graph Node -> Property |
| 93 | +prop_topsort_complete g = |
| 94 | + isLeft (topsort g) === any (\ n -> not . null $ findCycle g n) (nodes g) |
| 95 | + |
| 96 | +prop_find_cycle_sound :: Property |
| 97 | +prop_find_cycle_sound = |
| 98 | + forAllShrink (mkGraph @Node <$> arbitrary) shrink $ \ g -> |
| 99 | + and [ all (\(x, y) -> dependsOn x y g) (zip c (drop 1 c)) |
| 100 | + | n <- Set.toList $ nodes g |
| 101 | + , let c = findCycle g n |
| 102 | + ] |
| 103 | + |
| 104 | +prop_find_cycle_loops :: Property |
| 105 | +prop_find_cycle_loops = |
| 106 | + forAllShrink (mkGraph @Node <$> arbitrary) shrink $ \ g -> |
| 107 | + conjoin |
| 108 | + [ case findCycle g n of |
| 109 | + [] -> property True |
| 110 | + c@(x:_) -> cover 40 True "found cycle" $ counterexample (show c) $ dependsOn (last c) x g |
| 111 | + | n <- Set.toList $ nodes g |
| 112 | + ] |
| 113 | + |
| 114 | +return [] |
| 115 | + |
| 116 | +tests :: Bool -> Spec |
| 117 | +tests _nightly = |
| 118 | + describe "Graph tests" $ sequence_ [ prop n (checkCoverage $ withMaxSuccess 1000 p) | (n, p) <- $allProperties ] |
0 commit comments