From 5dd8d0a8444f6f42bf3115ff4e310208f29af9dd Mon Sep 17 00:00:00 2001 From: Gary Burgess Date: Sat, 4 Apr 2015 23:00:38 +0100 Subject: [PATCH 1/2] Add Arb/CoArb instances for Unit and Ordering --- README.md | 34 +++++++++++++++++++++++++++++++--- src/Test/QuickCheck.purs | 27 +++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 75c7bea..8fe8661 100755 --- a/README.md +++ b/README.md @@ -28,11 +28,11 @@ class Arbitrary t where arbitrary :: Gen t ``` -The `Arbitrary` class represents those types whose values can be +The `Arbitrary` class represents those types whose values can be _randomly-generated_. `arbitrary` uses the `Gen` monad to express a random generator for -the type `t`. Combinators in the `Test.QuickCheck.Gen` +the type `t`. Combinators in the `Test.QuickCheck.Gen` module can be used to construct random generators. #### `CoArbitrary` @@ -138,6 +138,34 @@ instance coarbString :: CoArbitrary String ``` +#### `arbUnit` + +``` purescript +instance arbUnit :: Arbitrary Unit +``` + + +#### `coarbUnit` + +``` purescript +instance coarbUnit :: CoArbitrary Unit +``` + + +#### `arbOrdering` + +``` purescript +instance arbOrdering :: Arbitrary Ordering +``` + + +#### `coarbOrdering` + +``` purescript +instance coarbOrdering :: CoArbitrary Ordering +``` + + #### `AlphaNumString` ``` purescript @@ -145,7 +173,7 @@ newtype AlphaNumString = AlphaNumString String ``` -A newtype for `String` whose `Arbitrary` instance generated random +A newtype for `String` whose `Arbitrary` instance generated random alphanumeric strings. #### `arbAlphaNumString` diff --git a/src/Test/QuickCheck.purs b/src/Test/QuickCheck.purs index ad83f31..0a03bf5 100644 --- a/src/Test/QuickCheck.purs +++ b/src/Test/QuickCheck.purs @@ -35,11 +35,11 @@ import qualified Data.String.Unsafe as SU import Test.QuickCheck.Gen --- | The `Arbitrary` class represents those types whose values can be +-- | The `Arbitrary` class represents those types whose values can be -- | _randomly-generated_. --- | +-- | -- | `arbitrary` uses the `Gen` monad to express a random generator for --- | the type `t`. Combinators in the `Test.QuickCheck.Gen` +-- | the type `t`. Combinators in the `Test.QuickCheck.Gen` -- | module can be used to construct random generators. class Arbitrary t where arbitrary :: Gen t @@ -100,7 +100,26 @@ instance arbString :: Arbitrary String where instance coarbString :: CoArbitrary String where coarbitrary s = coarbitrary $ (S.charCodeAt 0 <$> S.split "" s) --- | A newtype for `String` whose `Arbitrary` instance generated random +instance arbUnit :: Arbitrary Unit where + arbitrary = return unit + +instance coarbUnit :: CoArbitrary Unit where + coarbitrary _ = perturbGen 1 + +instance arbOrdering :: Arbitrary Ordering where + arbitrary = do + n <- (3 *) <$> uniform + return $ case n of + _ | n < 1 -> LT + | n < 2 -> EQ + | otherwise -> GT + +instance coarbOrdering :: CoArbitrary Ordering where + coarbitrary LT = perturbGen 1 + coarbitrary EQ = perturbGen 2 + coarbitrary GT = perturbGen 3 + +-- | A newtype for `String` whose `Arbitrary` instance generated random -- | alphanumeric strings. newtype AlphaNumString = AlphaNumString String From 4ca4c03d411dab392f253c764b4fe3476efbc4de Mon Sep 17 00:00:00 2001 From: Gary Burgess Date: Sun, 5 Apr 2015 00:36:48 +0100 Subject: [PATCH 2/2] Better Ordering instance --- src/Test/QuickCheck.purs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Test/QuickCheck.purs b/src/Test/QuickCheck.purs index 0a03bf5..ba0c5b2 100644 --- a/src/Test/QuickCheck.purs +++ b/src/Test/QuickCheck.purs @@ -108,11 +108,11 @@ instance coarbUnit :: CoArbitrary Unit where instance arbOrdering :: Arbitrary Ordering where arbitrary = do - n <- (3 *) <$> uniform + n <- chooseInt 1 3 return $ case n of - _ | n < 1 -> LT - | n < 2 -> EQ - | otherwise -> GT + 1 -> LT + 2 -> EQ + 3 -> GT instance coarbOrdering :: CoArbitrary Ordering where coarbitrary LT = perturbGen 1