Skip to content

Commit

Permalink
Fill in some of the random code for enum.
Browse files Browse the repository at this point in the history
We still need to figure out a reasonable way to sample an enum.
  • Loading branch information
yav committed Jan 23, 2024
1 parent 5236aff commit 4c1b857
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Cryptol/Testing/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import Control.Monad (liftM2)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Bits
import Data.List (unfoldr, genericTake, genericIndex, genericReplicate)
import Data.Map(Map)
import qualified Data.Map as Map
import qualified Data.Sequence as Seq

import System.Random.TF.Gen
Expand Down Expand Up @@ -170,7 +172,9 @@ randomValue sym ty =
TVStruct fs ->
do gs <- traverse (randomValue sym) fs
return (randomRecord gs)
-- XXX: Do random testing on `enum` types
TVEnum cons ->
do gs <- traverse (mapM (randomValue sym)) cons
pure (randomCon gs)

TVArray{} -> Nothing
TVFun{} -> Nothing
Expand Down Expand Up @@ -266,6 +270,9 @@ randomTuple gens sz = go [] gens

{-# INLINE randomRecord #-}

randomCon :: (Backend sym, RandomGen g) => Map Ident [Gen g sym] -> Gen g sym
randomCon cons sz g0 = undefined

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 9.4.7, 3.10.1.0, true)

Defined but not used: ‘cons’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 9.4.7, 3.10.1.0, true)

Defined but not used: ‘sz’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 9.4.7, 3.10.1.0, true)

Defined but not used: ‘g0’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 9.6.2, 3.10.1.0, true)

Defined but not used: ‘cons’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 9.6.2, 3.10.1.0, true)

Defined but not used: ‘sz’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 9.6.2, 3.10.1.0, true)

Defined but not used: ‘g0’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 9.2.8, false)

Defined but not used: ‘cons’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 9.2.8, 3.10.1.0, true)

Defined but not used: ‘cons’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (macos-12, 9.2.8, 3.10.1.0, true)

Defined but not used: ‘cons’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (windows-2019, 9.2.8, 3.10.1.0, true)

Defined but not used: ‘cons’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (macos-12, 9.6.2, 3.10.1.0, true)

Defined but not used: ‘cons’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (macos-12, 9.6.2, 3.10.1.0, true)

Defined but not used: ‘sz’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (macos-12, 9.6.2, 3.10.1.0, true)

Defined but not used: ‘g0’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (macos-12, 9.4.7, 3.10.1.0, true)

Defined but not used: ‘cons’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (macos-12, 9.4.7, 3.10.1.0, true)

Defined but not used: ‘sz’

Check warning on line 274 in src/Cryptol/Testing/Random.hs

View workflow job for this annotation

GitHub Actions / build (macos-12, 9.4.7, 3.10.1.0, true)

Defined but not used: ‘g0’

-- | Generate a random record value.
randomRecord :: (Backend sym, RandomGen g) => RecordMap Ident (Gen g sym) -> Gen g sym
randomRecord gens sz g0 =
Expand Down Expand Up @@ -408,7 +415,8 @@ typeSize ty = case ty of
TVNewtype _ _ nv ->
case nv of
TVStruct tbody -> typeSize (TVRec tbody)
-- XXX: enum
TVEnum cons -> sum <$> mapM conSize (Map.elems cons)
where conSize = foldr (\t sz -> liftM2 (*) (typeSize t) sz) (Just 1)

{- | Returns all the values in a type. Returns an empty list of values,
for types where 'typeSize' returned 'Nothing'. -}
Expand Down Expand Up @@ -443,7 +451,11 @@ typeValues ty =
TVNewtype _ _ nv ->
case nv of
TVStruct tbody -> typeValues (TVRec tbody)
-- XXX: enum
TVEnum cons ->
[ VEnum i (map pure vs)
| (i,ts) <- Map.toList cons
, vs <- mapM typeValues ts
]

--------------------------------------------------------------------------------
-- Driver function
Expand Down

0 comments on commit 4c1b857

Please sign in to comment.