Permalink
Browse files

Replace Test.Framework, HUnit, and QuickCheck with Hedgehog

  • Loading branch information...
moodmosaic committed Jul 23, 2018
1 parent 7c6e716 commit 528be10c2529c7da2e235dea26df298a99b2a884
Showing with 79 additions and 58 deletions.
  1. +1 −5 Problem23.cabal
  2. +3 −2 stack.yaml
  3. +75 −51 test/Spec.hs
View
@@ -34,11 +34,7 @@ test-suite Problem23-test
main-is: Spec.hs
build-depends: base
, Problem23
, test-framework
, test-framework-quickcheck2
, test-framework-hunit
, QuickCheck
, HUnit
, hedgehog >= 0.6.1
, random
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
View
@@ -15,7 +15,7 @@
# resolver:
# name: custom-snapshot
# location: "./custom-snapshot.yaml"
resolver: lts-9.9
resolver: lts-11.22
# User packages to be built.
# Various formats can be used as shown in the example below.
@@ -39,7 +39,8 @@ packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver
# (e.g., acme-missiles-0.3)
extra-deps: []
extra-deps:
- hedgehog-0.6.1
# Override default flag values for local packages and extra-deps
flags: {}
View
@@ -1,57 +1,81 @@
module Main where
{-# LANGUAGE OverloadedStrings #-}
module Main (
main
) where
import Data.List (genericLength)
import System.Random
import Test.Framework (defaultMain, testGroup)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.Framework.Providers.HUnit
import Test.HUnit.Base
import Test.QuickCheck
import Lib
import Control.Monad (unless)
import Data.List (genericLength)
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Lib
import System.Exit (exitFailure)
import System.Random
lenProp :: Integral i => Int -> [a] -> NonNegative i -> Bool
lenProp seed xs (NonNegative i) =
i == genericLength (rndGenSelect (mkStdGen seed) xs i)
lenProp g = property $ do
seed <- forAll $ Gen.int Range.linearBounded
xs <- forAll $ Gen.list (Range.linear 0 100) g
i <- forAll $ Gen.int $ Range.linear 0 100
i === genericLength (rndGenSelect (mkStdGen seed) xs i)
negLenProp :: Integral i => Int -> [a] -> Positive i -> Bool
negLenProp seed xs (Positive i) =
0 == genericLength (rndGenSelect (mkStdGen seed) xs (-i))
negLenProp g = property $ do
seed <- forAll $ Gen.int Range.linearBounded
xs <- forAll $ Gen.list (Range.linear 0 100) g
i <- forAll $ Gen.int $ Range.linear 1 100
0 === genericLength (rndGenSelect (mkStdGen seed) xs (-i))
main :: IO ()
main = defaultMain tests
tests = [
testGroup "Properties" [
testProperty "rndSelect returns result of correct length" (lenProp :: Int -> [Int] -> NonNegative Int -> Bool),
testProperty "rndSelect returns result of correct length" (lenProp :: Int -> String -> NonNegative Integer -> Bool),
testProperty "rndGenSelect returns empty result when count is negative" (negLenProp :: Int -> [Int] -> Positive Int -> Bool)
],
testGroup "Regression tests" $ hUnitTestToTests $ TestList [
"rndGenSelect of chars returns correct result" ~: do
(seed, xs, count, expected) <-
[
( 42, "foo", 3, "ofo"),
( 1337, "bar", 10, "rabbaarrra"),
(-197221, ['a'..'z'], 5, "ntfnc")
]
let rnd = mkStdGen seed
let actual = rndGenSelect rnd xs count
return $ expected ~=? actual
,
"rndGenSelect of integers returns correct result" ~: do
(seed, xs, count, expected) <-
[
( 19, [1..3], 3, [3,1,3]),
(1770, [0,1,1,2,7], 10, [1,2,2,1,1,1,1,1,7,7]),
( -19, [0..99], 5, [67,48,8,47,42])
]
let rnd = mkStdGen seed
let actual = rndGenSelect rnd xs count
return $ expected ~=? actual
main = do
results <- sequence [
properties
, regressionTests
]
]
unless (and results)
exitFailure
properties :: IO Bool
properties =
checkParallel $ Group "Properties" [
("rndGenSelect returns result of correct length"
, lenProp (Gen.int $ Range.linear (-100) 100))
, ("rndGenSelect returns result of correct length"
, lenProp Gen.alpha)
, ("rndGenSelect returns empty result when count is negative"
, negLenProp (Gen.int $ Range.linear (-100) 100))
]
regressionTests :: IO Bool
regressionTests =
checkParallel $ Group "Regression tests" $
do
(seed, xs, count, expected) <-
[
( 42, "foo", 3, "ofo"),
( 1337, "bar", 10, "rabbaarrra"),
(-197221, ['a'..'z'], 5, "ntfnc")
]
let rnd = mkStdGen seed
let actual = rndGenSelect rnd xs count
return
("rndGenSelect of chars returns correct result"
, withTests 1 . property $ expected === actual)
++
do
(seed, xs, count, expected) <-
[
( 19, [1..3], 3, [3,1,3]),
(1770, [0,1,1,2,7], 10, [1,2,2,1,1,1,1,1,7,7]),
( -19, [0..99], 5, [67,48,8,47,42])
]
let rnd = mkStdGen seed
let actual = rndGenSelect rnd xs count
return
("rndGenSelect of integers returns correct result"
, withTests 1 . property $ expected === actual)

0 comments on commit 528be10

Please sign in to comment.