aboutsummaryrefslogtreecommitdiffstats
path: root/src/Test/VeriFuzz/Graph/Random.hs
blob: 2b9115770beedc317649126dd872b31fe3d8ec63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Test.VeriFuzz.Graph.Random where

import Data.Graph.Inductive (Graph, LNode, LEdge, mkGraph)
import Test.QuickCheck (Arbitrary, Gen, arbitrary, generate, infiniteListOf, suchThat, listOf, scale, resize)

import Test.VeriFuzz.Types

arbitraryEdge :: (Arbitrary e) => Int -> Gen (LEdge e)
arbitraryEdge n = do
  x <- with $ \a -> a < n && a > 0 && a /= n-1
  y <- with $ \a -> x < a && a < n && a > 0
  z <- arbitrary
  return (x, y, z)
    where
      with = suchThat . resize n $ arbitrary

randomDAG :: (Arbitrary l, Arbitrary e, Graph gr)
          => Int         -- ^ The number of nodes
          -> IO (gr l e) -- ^ The generated graph. It uses Arbitrary to
                         -- generate random instances of each node
randomDAG n = do
  list <- generate . infiniteListOf $ arbitrary
  l <- generate . infiniteListOf $ arbitraryEdge n
  return . mkGraph (nodes list) $ take (2*n) l
    where
      nodes l = (zip [0..n] $ take n l)