File: Main.hs

package info (click to toggle)
haskell-either 5.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 116 kB
  • sloc: haskell: 252; makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,531 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Applicative
import Data.Either.Validation
import Data.Monoid (Sum(..))

import Test.QuickCheck (Property, Gen, (===), (.&&.), Arbitrary (..), forAllShrink, oneof)
import Test.Framework (defaultMain)
import Test.Framework.Providers.QuickCheck2 (testProperty)


main :: IO ()
main = defaultMain
    [ testProperty "identity" $ identity (<|>) empty genValSumInt shrinkValidation
    , testProperty "associativity" $ associativity (<|>) genValSumInt shrinkValidation
    ]

genValSumInt :: Gen (Validation (Sum Int) (Sum Int))
genValSumInt = genValidation

genValidation :: (Arbitrary a, Arbitrary b) => Gen (Validation a b)
genValidation = oneof
    [ fmap Failure arbitrary
    , fmap Success arbitrary
    ]

shrinkValidation :: (Arbitrary a, Arbitrary b) => Validation a b -> [Validation a b]
shrinkValidation (Success x) = Success `fmap` shrink x
shrinkValidation (Failure x) = Failure `fmap` shrink x

-- -- empty is a neutral element
-- empty <|> u  =  u
-- u <|> empty  =  u
-- -- (<|>) is associative
-- u <|> (v <|> w)  =  (u <|> v) <|> w

identity :: (Eq a, Show a) => (a -> a -> a) -> a -> Gen a -> (a -> [a]) -> Property
identity f i gen shr = forAllShrink gen shr $ \x ->
    f x i === x .&&. f i x === x

associativity :: (Eq a, Show a) => (a -> a -> a) -> Gen a -> (a -> [a]) -> Property
associativity f gen shr =
    forAllShrink gen shr $ \x ->
    forAllShrink gen shr $ \y ->
    forAllShrink gen shr $ \z ->
        f x (f y z) === f (f x y) z