Skip to content

Commit

Permalink
Merge pull request #14 from purescript/simplify
Browse files Browse the repository at this point in the history
Simplify: revert to pre-'machines' state.
  • Loading branch information
paf31 committed Nov 8, 2014
2 parents da3a279 + 62394d4 commit d464c5c
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 1,004 deletions.
41 changes: 41 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module.exports = function(grunt) {

"use strict";

grunt.initConfig({

libFiles: [
"src/**/*.purs",
"bower_components/purescript-*/src/**/*.purs",
],

clean: ["output"],

pscMake: ["<%=libFiles%>"],
dotPsci: ["<%=libFiles%>"],
docgen: {
readme: {
src: "src/**/*.purs",
dest: "README.md"
}
},

psc: {
options: {
main: "Main",
modules: ["Main"]
},
example: {
src: ["<%=libFiles%>", "examples/Prelude.purs"],
dest: "tmp/Prelude.js"
}
}
});

grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-purescript");

grunt.registerTask("make", ["pscMake", "dotPsci", "docgen"]);
grunt.registerTask("example", ["psc:example"]);
grunt.registerTask("default", ["make"]);
};
104 changes: 65 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,12 @@
newtype AlphaNumString where
AlphaNumString :: String -> AlphaNumString

newtype Negative where
Negative :: Number -> Negative

newtype NonZero where
NonZero :: Number -> NonZero

newtype Positive where
Positive :: Number -> Positive

type QC a = forall eff. Eff (err :: Exception, random :: Random, trace :: Trace | eff) a

data Result where
Success :: Result
Failed :: String -> Result

newtype Signum where
Signum :: Number -> Signum


### Type Classes

Expand All @@ -46,24 +34,16 @@

instance arbBoolean :: Arbitrary Boolean

instance arbChar :: Arbitrary Char
instance arbChar :: Arbitrary S.Char

instance arbEither :: (Arbitrary a, Arbitrary b) => Arbitrary (Either a b)

instance arbFunction :: (CoArbitrary a, Arbitrary b) => Arbitrary (a -> b)

instance arbMaybe :: (Arbitrary a) => Arbitrary (Maybe a)

instance arbNegative :: Arbitrary Negative

instance arbNonZero :: Arbitrary NonZero

instance arbNumber :: Arbitrary Number

instance arbPositive :: Arbitrary Positive

instance arbSignum :: Arbitrary Signum

instance arbString :: Arbitrary String

instance arbTuple :: (Arbitrary a, Arbitrary b) => Arbitrary (Tuple a b)
Expand All @@ -74,32 +54,20 @@

instance coarbBoolean :: CoArbitrary Boolean

instance coarbChar :: CoArbitrary Char
instance coarbChar :: CoArbitrary S.Char

instance coarbEither :: (CoArbitrary a, CoArbitrary b) => CoArbitrary (Either a b)

instance coarbFunction :: (Arbitrary a, CoArbitrary b) => CoArbitrary (a -> b)

instance coarbMaybe :: (CoArbitrary a) => CoArbitrary (Maybe a)

instance coarbNegative :: CoArbitrary Negative

instance coarbNonZero :: CoArbitrary NonZero

instance coarbNumber :: CoArbitrary Number

instance coarbPositive :: CoArbitrary Positive

instance coarbSignum :: CoArbitrary Signum

instance coarbString :: CoArbitrary String

instance coarbTuple :: (CoArbitrary a, CoArbitrary b) => CoArbitrary (Tuple a b)

instance monoidResult :: Monoid Result

instance semigroupResult :: Semigroup Result

instance showResult :: Show Result

instance testableBoolean :: Testable Boolean
Expand All @@ -117,15 +85,73 @@

quickCheck' :: forall prop. (Testable prop) => Number -> prop -> QC Unit

quickCheckPure :: forall prop. (Testable prop) => Number -> Seed -> prop -> [Result]
quickCheckPure :: forall prop. (Testable prop) => Number -> Number -> prop -> [Result]


## Module Test.QuickCheck.Gen

### Types

data Gen a

type GenOut a = { value :: a, state :: GenState }

type GenState = { size :: Size, newSeed :: LCG }

type LCG = Number

type Size = Number


### Type Class Instances

instance applicativeGen :: Applicative Gen

instance applyGen :: Apply Gen

instance bindGen :: Bind Gen

instance functorGen :: Functor Gen

instance monadGen :: Monad Gen


### Values

arrayOf :: forall a. Gen a -> Gen [a]

arrayOf1 :: forall a. Gen a -> Gen (Tuple a [a])

choose :: Number -> Number -> Gen Number

chooseInt :: Number -> Number -> Gen Number

elements :: forall a. a -> [a] -> Gen a

evalGen :: forall a. Gen a -> GenState -> a

frequency :: forall a. Tuple Number (Gen a) -> [Tuple Number (Gen a)] -> Gen a

oneOf :: forall a. Gen a -> [Gen a] -> Gen a

perturbGen :: forall a. Number -> Gen a -> Gen a

repeatable :: forall a b. (a -> Gen b) -> Gen (a -> b)

resize :: forall a. Size -> Gen a -> Gen a

runGen :: forall a. Gen a -> GenState -> GenOut a

smallCheck :: forall prop. (Testable prop) => prop -> QC Unit
showSample :: forall r a. (Show a) => Gen a -> Eff (trace :: Trace | r) Unit

smallCheckPure :: forall prop. (Testable prop) => Number -> prop -> [Result]
showSample' :: forall r a. (Show a) => Size -> Gen a -> Eff (trace :: Trace | r) Unit

statCheck :: forall prop. (Testable prop) => Number -> prop -> QC Unit
sized :: forall a. (Size -> Gen a) -> Gen a

statCheckPure :: forall prop. (Testable prop) => Seed -> Number -> prop -> Result
stateful :: forall a. (GenState -> Gen a) -> Gen a

uniform :: Gen Number

variant :: forall a. LCG -> Gen a -> Gen a

vectorOf :: forall a. Number -> Gen a -> Gen [a]
6 changes: 1 addition & 5 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "purescript-quickcheck",
"version": "0.1.6",
"license": "MIT",
"authors": [
"John A. De Goes <john@degoes.net> (http://degoes.net)",
Expand All @@ -21,11 +20,8 @@
"purescript-random": "~0.1.1",
"purescript-enums": "~0.2.1",
"purescript-exceptions": "~0.2.1",
"purescript-transformers": "~0.2.1",
"purescript-free": "~0.1.3",
"purescript-machines": "~0.1.5",
"purescript-arrays": "~0.2.1",
"purescript-strings": "~0.3.0",
"purescript-strings": "~0.4.0",
"purescript-math": "~0.1.0",
"purescript-tuples": "~0.2.1",
"purescript-either": "~0.1.3",
Expand Down
136 changes: 17 additions & 119 deletions examples/Prelude.purs
Original file line number Diff line number Diff line change
@@ -1,132 +1,30 @@
module PreludeTests where
module Main where

import Control.Monad.Eff
import Data.Eq
import Debug.Trace
import Test.QuickCheck
import Test.QuickCheck.LCG
import Control.Monad.Trampoline
import Data.Monoid
import Data.Tuple
import Data.Maybe
import Data.Foldable
import qualified Data.Array as Array
import qualified Math as Math

between :: forall a. (Ord a) => a -> a -> a -> Boolean
between min max = \n -> n >= min && n <= max
testConst :: Number -> Number -> Number -> Boolean
testConst a b c = const a b == const a c

data Mega = Mega {
arrayOf :: [Number],
arrayOf1 :: [Number],
choose :: Number,
chooseInt :: Number,
collectAll :: [Number],
allInArray :: [Number],
allInRange :: [Number],
dropGen :: [Number],
takeGen :: [Number],
elements :: [String],
extend :: [String],
infinite :: [String],
perms :: [[String]] }

{- TODO: Remaining cases
, frequency
, oneOf
, perturbGen
, repeatable
, resize
, sample
, sample'
, showSample
, showSample'
, sized
, stateful
, suchThat
, suchThatMaybe
, unfoldGen
, uniform
, variant
, vectorOf
-}

data DetABC = DetABC String

runDetABC :: DetABC -> String
runDetABC (DetABC s) = s

instance arbDetABC :: Arbitrary DetABC where
arbitrary = DetABC <$> allInArray ["A", "B", "C"]

data OneToTen = OneToTen Number

runOneToTen :: OneToTen -> Number
runOneToTen (OneToTen n) = n

instance arbOneToTen :: Arbitrary OneToTen where
arbitrary = OneToTen <$> chooseInt 0 10

instance arbMega :: Arbitrary Mega where
arbitrary = do
arrayOf' <- arrayOf (choose 0 10)
arrayOf1' <- arrayOf1 (choose 0 10)
choose' <- choose 0 10
chooseInt' <- chooseInt 0 10
collectAll' <- collectAll mempty (allInArray [0, 1, 2])
allInArray' <- collectAll mempty (allInArray [0, 1, 2])
allInRange' <- collectAll mempty (allInRange 0 10)
dropGen' <- collectAll mempty $ dropGen 2 (allInArray [2, 1, -1])
takeGen' <- collectAll mempty $ takeGen 2 (allInArray [2, 1, -1])
elements' <- arrayOf $ elements "foo" ["bar", "baz"]
extend' <- collectAll mempty $ extend 3 (pure "5")
infinite' <- collectAll mempty $ takeGen 4 (infinite $ pure "foo")
perms' <- collectAll mempty $ perms ["John", "D"]
return $ Mega {
arrayOf: arrayOf',
arrayOf1: (case arrayOf1' of Tuple a as -> a : as),
choose: choose',
chooseInt: chooseInt',
collectAll: collectAll',
allInArray: allInArray',
allInRange: allInRange',
dropGen: dropGen',
takeGen: takeGen',
elements: elements',
extend: extend',
infinite: infinite',
perms: perms' }

verify_gen :: Mega -> Result
verify_gen (Mega m) = fold [
all (between 0 10) m.arrayOf <?> "arrayOf: " ++ show m.arrayOf,
Array.length m.arrayOf1 >= 1 <?> "arrayOf1: " ++ show m.arrayOf1,
between 0 10 m.choose <?> "choose: " ++ show m.choose,
between 0 10 m.chooseInt &&
Math.floor(m.chooseInt) == m.chooseInt <?> "chooseInt: " ++ show m.chooseInt,
m.collectAll == [0, 1, 2] <?> "collectAll: " ++ show m.collectAll,
m.allInArray == [0, 1, 2] <?> "allInArray: " ++ show m.allInArray,
m.allInRange == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] <?> "allInRange: " ++ show m.allInRange,
m.dropGen == [-1] <?> "dropGen: " ++ show m.dropGen,
m.takeGen == [2, 1] <?> "takeGen: " ++ show m.takeGen,
all (flip elem ["foo", "bar", "baz"]) m.elements <?> "elements: " ++ show m.elements,
m.extend == ["5", "5", "5"] <?> "extend: " ++ show m.extend,
m.infinite == ["foo", "foo", "foo", "foo"] <?> "infinite: " ++ show m.infinite,
m.perms == ["John", "D"] : ["D", "John"] : [] <?> "perms: " ++ show m.perms]
mkMessage :: (Number -> Number) -> String
mkMessage f = "Test failed for function ("
++ show (f 0) ++ ", "
++ show (f 1) ++ ", "
++ show (f 2) ++ ")"

main = do
trace "Gen combinators"
quickCheck $ verify_gen

trace "foldGen"
quickCheck $ (runTrampoline $ foldGen (\a b -> Just $ a + b) 1 mempty (allInArray [1, 2, 3])) == 7
Debug.Trace.trace "testConst:"
quickCheck testConst

trace "smallCheck"
smallCheck $ runDetABC >>> (flip elem ["A", "B", "C"])
Debug.Trace.trace "id is a left unit for <<<"
quickCheck $ \f a -> ((id <<< f) (a :: Number) == (f a) :: Number) <?> mkMessage f

trace "Fair distribution of booleans"
statCheck (1/2) $ (==) true
Debug.Trace.trace "Precedence of && and ||:"
quickCheck $ \a b c -> ((a :: Boolean && b) || c) == ((a || c) && (b || c))

trace "Fair distribution of ints"
statCheck (1/11) $ runOneToTen >>> ((==) 1)
Debug.Trace.trace "Test Eq instance for Ref:"
quickCheck $ \a -> (Ref a :: Ref Number) == Ref a
quickCheck $ \a -> not $ (Ref a :: Ref Number /= Ref a)

Loading

0 comments on commit d464c5c

Please sign in to comment.