Skip to content

Commit a93259a

Browse files
committed
Redo 14.4 and play some more with QuickCheck
1 parent 1261f09 commit a93259a

File tree

6 files changed

+191
-0
lines changed

6 files changed

+191
-0
lines changed

ch14/qc/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.stack-work/

ch14/qc/Examples.hs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module Examples where
2+
3+
import Test.Hspec
4+
import Test.QuickCheck
5+
6+
trivialInt :: Gen Int
7+
trivialInt = return 1
8+
9+
oneThroughThree :: Gen Int
10+
oneThroughThree = elements [1, 2, 3]
11+
12+
genBool :: Gen Bool
13+
genBool = choose (False, True)
14+
15+
genBool' :: Gen Bool
16+
genBool' = elements [False, True]
17+
18+
genOrdering :: Gen Ordering
19+
genOrdering = elements [LT, EQ, GT]
20+
21+
genChar :: Gen Char
22+
genChar = elements ['a'..'z']
23+
24+
genTuple :: (Arbitrary a, Arbitrary b) => Gen (a, b)
25+
genTuple = do
26+
a <- arbitrary
27+
b <- arbitrary
28+
return (a, b)
29+
30+
genThreeple :: (Arbitrary a, Arbitrary b, Arbitrary c) => Gen (a, b, c)
31+
genThreeple = do
32+
a <- arbitrary
33+
b <- arbitrary
34+
c <- arbitrary
35+
return (a, b, c)
36+
37+
genEither :: (Arbitrary a, Arbitrary b) => Gen (Either a b)
38+
genEither = do
39+
a <- arbitrary
40+
b <- arbitrary
41+
elements [Left a, Right b]
42+
43+
-- equal probability
44+
genMaybe :: Arbitrary a => Gen (Maybe a)
45+
genMaybe = do
46+
a <- arbitrary
47+
elements [Nothing, Just a]
48+
49+
-- What QuickCheck actually does
50+
-- so you get more Just values
51+
genMaybe' :: Arbitrary a => Gen (Maybe a)
52+
genMaybe' = do
53+
a <- arbitrary
54+
frequency [ (1, return Nothing)
55+
, (3, return (Just a))
56+
]
57+
58+
-- Using QuickCheck with Hspec
59+
-- main :: IO ()
60+
-- main = hspec $ do
61+
-- describe "inc" $ do
62+
-- it "x + 1 is always greater than x" $ do
63+
-- property $ \x -> x + 1 > (x :: Int)
64+
65+
-- Using QuickCheck without Hspec
66+
prop_additionGreater :: Int -> Bool
67+
prop_additionGreater x = x + 1 > x
68+
69+
runQc :: IO ()
70+
runQc = quickCheck prop_additionGreater

ch14/qc/LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright Dwayne Crooks (c) 2016
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Dwayne Crooks nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

ch14/qc/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

ch14/qc/qc.cabal

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: qc
2+
version: 0.1.0.0
3+
synopsis: Simple project template from stack
4+
description: Please see README.md
5+
homepage: https://github.com/dwayne/qc#readme
6+
license: BSD3
7+
license-file: LICENSE
8+
author: Dwayne Crooks
9+
maintainer: me@dwaynecrooks.com
10+
copyright: 2016 Dwayne Crooks
11+
category: Text
12+
build-type: Simple
13+
cabal-version: >=1.10
14+
15+
library
16+
hs-source-dirs: .
17+
exposed-modules: Examples
18+
ghc-options: -Wall -fwarn-tabs
19+
default-language: Haskell2010
20+
build-depends: base >= 4.7 && < 5
21+
, hspec
22+
, QuickCheck

ch14/qc/stack.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This file was automatically generated by 'stack init'
2+
#
3+
# Some commonly used options have been documented as comments in this file.
4+
# For advanced use and comprehensive documentation of the format, please see:
5+
# http://docs.haskellstack.org/en/stable/yaml_configuration/
6+
7+
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8+
# A snapshot resolver dictates the compiler version and the set of packages
9+
# to be used for project dependencies. For example:
10+
#
11+
# resolver: lts-3.5
12+
# resolver: nightly-2015-09-21
13+
# resolver: ghc-7.10.2
14+
# resolver: ghcjs-0.1.0_ghc-7.10.2
15+
# resolver:
16+
# name: custom-snapshot
17+
# location: "./custom-snapshot.yaml"
18+
resolver: lts-5.17
19+
20+
# User packages to be built.
21+
# Various formats can be used as shown in the example below.
22+
#
23+
# packages:
24+
# - some-directory
25+
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
26+
# - location:
27+
# git: https://github.com/commercialhaskell/stack.git
28+
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
29+
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
30+
# extra-dep: true
31+
# subdirs:
32+
# - auto-update
33+
# - wai
34+
#
35+
# A package marked 'extra-dep: true' will only be built if demanded by a
36+
# non-dependency (i.e. a user package), and its test suites and benchmarks
37+
# will not be run. This is useful for tweaking upstream packages.
38+
packages:
39+
- '.'
40+
# Dependency packages to be pulled from upstream that are not in the resolver
41+
# (e.g., acme-missiles-0.3)
42+
extra-deps: []
43+
44+
# Override default flag values for local packages and extra-deps
45+
flags: {}
46+
47+
# Extra package databases containing global packages
48+
extra-package-dbs: []
49+
50+
# Control whether we use the GHC we find on the path
51+
# system-ghc: true
52+
#
53+
# Require a specific version of stack, using version ranges
54+
# require-stack-version: -any # Default
55+
# require-stack-version: ">=1.1"
56+
#
57+
# Override the architecture used by stack, especially useful on Windows
58+
# arch: i386
59+
# arch: x86_64
60+
#
61+
# Extra directories used by stack for building
62+
# extra-include-dirs: [/path/to/dir]
63+
# extra-lib-dirs: [/path/to/dir]
64+
#
65+
# Allow a newer minor version of GHC than the snapshot specifies
66+
# compiler-check: newer-minor

0 commit comments

Comments
 (0)