Skip to content

Commit

Permalink
Merge pull request #27 from purescript/lcg
Browse files Browse the repository at this point in the history
Use Number type to avoid overflows in lcgNext
  • Loading branch information
paf31 committed Jun 18, 2015
2 parents b4a3a03 + 61ce30f commit 2dec9a7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/Test/QuickCheck.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ instance showResult :: Show Result
(<?>) :: Boolean -> String -> Result
```

_left-associative / precedence -1_

This operator attaches an error message to a failed test.

For example:
Expand All @@ -114,6 +116,8 @@ test x = myProperty x <?> ("myProperty did not hold for " <> show x)
(===) :: forall a b. (Eq a, Show a) => a -> a -> Result
```

_left-associative / precedence -1_

Self-documenting equality assertion

#### `(/==)`
Expand All @@ -122,6 +126,8 @@ Self-documenting equality assertion
(/==) :: forall a b. (Eq a, Show a) => a -> a -> Result
```

_left-associative / precedence -1_

Self-documenting inequality assertion


2 changes: 2 additions & 0 deletions docs/Test/QuickCheck/Data/ApproxNumber.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ instance numApproxNumber :: Num ApproxNumber
(=~=) :: Number -> Number -> Boolean
```

_left-associative / precedence -1_


2 changes: 2 additions & 0 deletions docs/Test/QuickCheck/LCG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ Step the linear congruential generator
randomSeed :: forall e. Eff (random :: RANDOM | e) Seed
```

Create a random seed


4 changes: 2 additions & 2 deletions src/Test/QuickCheck.purs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ quickCheck' n prop = do
seed <- randomSeed
let results = quickCheckPure seed n prop
let successes = countSuccesses results
log $ show (toNumber successes) ++ "/" ++ show (toNumber n) ++ " test(s) passed."
log $ show successes ++ "/" ++ show n ++ " test(s) passed."
throwOnFirstFailure one results

where

throwOnFirstFailure :: Int -> List Result -> QC Unit
throwOnFirstFailure _ Nil = return unit
throwOnFirstFailure n (Cons (Failed msg) _) = throwException $ error $ "Test " ++ show (toNumber n) ++ " failed: \n" ++ msg
throwOnFirstFailure n (Cons (Failed msg) _) = throwException $ error $ "Test " ++ show n ++ " failed: \n" ++ msg
throwOnFirstFailure n (Cons _ rest) = throwOnFirstFailure (n + one) rest

countSuccesses :: List Result -> Int
Expand Down
5 changes: 4 additions & 1 deletion src/Test/QuickCheck/LCG.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module Test.QuickCheck.LCG

import Prelude

import Math ((%))
import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Random (RANDOM(), randomInt)
import Data.Int (fromNumber, toNumber)
import Data.Int.Bits (shl)
import qualified Data.Maybe.Unsafe as U

type Seed = Int

Expand All @@ -30,7 +32,8 @@ lcgN = one `shl` 30

-- | Step the linear congruential generator
lcgNext :: Int -> Int
lcgNext n = (lcgM * n + lcgC) `mod` lcgN
lcgNext n = U.fromJust $ fromNumber $ (toNumber lcgM * toNumber n + toNumber lcgC) % toNumber lcgN

-- | Create a random seed
randomSeed :: forall e. Eff (random :: RANDOM | e) Seed
randomSeed = randomInt 0 lcgM

0 comments on commit 2dec9a7

Please sign in to comment.