Commit d5ae202a authored by Hans-Peter Deifel's avatar Hans-Peter Deifel 🐢

wta: Add tests for WTA generator

parent 0e62f678
...@@ -306,3 +306,21 @@ executable random-wta ...@@ -306,3 +306,21 @@ executable random-wta
, megaparsec >= 7 && <8 , megaparsec >= 7 && <8
, scientific >= 0.3 && <0.4 , scientific >= 0.3 && <0.4
, containers >= 0.6 && <0.7 , containers >= 0.6 && <0.7
test-suite random-wta-tests
type: exitcode-stdio-1.0
hs-source-dirs: src/random-wta
main-is: Tests.hs
other-modules: Types
, IndexedTransition
, Generator
, Probability
default-language: Haskell2010
build-depends: base >= 4.11
, vector >= 0.12 && <0.13
, random >= 1.1 && <1.2
, mtl >= 2.2 && <2.3
, scientific >= 0.3 && <0.4
, containers >= 0.6 && <0.7
, hspec
, QuickCheck
...@@ -7,6 +7,7 @@ module Generator ...@@ -7,6 +7,7 @@ module Generator
, runGenerator , runGenerator
, GeneratorConfig(..) , GeneratorConfig(..)
, EdgeConfig(..) , EdgeConfig(..)
, uniqueTransitions
) )
where where
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
{-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
module IndexedTransition (IndexedTransition(..), maxIndex, fromIndex) where module IndexedTransition (IndexedTransition(..), maxIndex, fromIndex, index) where
import Data.Vector ( Vector ) import Data.Vector ( Vector )
import qualified Data.Vector as V import qualified Data.Vector as V
...@@ -49,8 +49,8 @@ fromIndex spec (Index i) = ...@@ -49,8 +49,8 @@ fromIndex spec (Index i) =
} }
in (State (fromIntegral state), trans) in (State (fromIntegral state), trans)
index :: WTASpec m1 -> Int -> Transition m2 -> IndexedTransition index :: WTASpec m1 -> State -> Transition m2 -> IndexedTransition
index spec state trans = index spec (State state) trans =
let (t, symbolSums) = transitionsPerState spec let (t, symbolSums) = transitionsPerState spec
arity :: Int = summandArity spec (summand trans) arity :: Int = summandArity spec (summand trans)
......
module Main (main) where
import Test.Hspec
import Test.QuickCheck
import qualified Data.Vector as V
import Data.List (nub)
import Types
import qualified IndexedTransition
import Generator
main :: IO ()
main = hspec $ do
describe "IndexedTransition" $ do
it "is isomorphic to a transition (10 states, signature 4xX)" $ do
let wta = WTASpec MaxInt 10 (V.fromList [0, 4])
property $ \t ->
uncurry (IndexedTransition.index wta)
(IndexedTransition.fromIndex wta t)
== t
it "is isomorphic to a transition (50 states, signature 4xX^2)" $ do
let wta = WTASpec MaxInt 50 (V.fromList [0, 0, 4])
property $ \t ->
uncurry (IndexedTransition.index wta)
(IndexedTransition.fromIndex wta t)
== t
describe "Generator" $ describe "uniqueTransitions" $ do
it "generates unique transitions" $ do
res <- uniqueTransitions 5 (IndexedTransition.Index 10)
nub res `shouldBe` res
res <- uniqueTransitions 100 (IndexedTransition.Index 120)
nub res `shouldBe` res
it "generates the correct number of transitions" $ do
(length <$> uniqueTransitions 5 (IndexedTransition.Index 10))
`shouldReturn` 5
(length <$> uniqueTransitions 20 (IndexedTransition.Index 21))
`shouldReturn` 20
(length <$> uniqueTransitions 100 (IndexedTransition.Index 101))
`shouldReturn` 100
it "doesn't generate a transition greater than max" $ do
res <- maximum <$> uniqueTransitions 100 (IndexedTransition.Index 150)
res `shouldSatisfy` (<150)
instance Arbitrary IndexedTransition.IndexedTransition where
arbitrary = IndexedTransition.Index <$> arbitrary
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment