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