module Test.Aeson.Internal.RandomSamples where
import Control.Exception
import Data.Aeson
import Data.ByteString.Lazy (ByteString)
import GHC.Generics
import Test.QuickCheck
import Test.QuickCheck.Gen
import Test.QuickCheck.Random
data RandomSamples a = RandomSamples {
seed :: Int
, samples :: [a]
} deriving (Eq, Ord, Show, Generic)
instance FromJSON a => FromJSON (RandomSamples a)
instance ToJSON a => ToJSON (RandomSamples a)
setSeed :: Int -> Gen a -> Gen a
setSeed rSeed (MkGen g) = MkGen $ \ _randomSeed size -> g (mkQCGen rSeed) size
readSeed :: ByteString -> IO Int
readSeed s = case eitherDecode s :: Either String (RandomSamples Value) of
Right rSamples -> return $ seed rSamples
Left err -> throwIO $ ErrorCall err
readSampleSize :: ByteString -> IO Int
readSampleSize s = case eitherDecode s :: Either String (RandomSamples Value) of
Right rSamples -> return . length . samples $ rSamples
Left err -> throwIO $ ErrorCall err