-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTest.hs
107 lines (93 loc) · 3.22 KB
/
Test.hs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{-# LANGUAGE TemplateHaskell #-}
module Test where
import Test.QuickCheck
import Test.Framework.TH
import Test.Framework
import Test.HUnit
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import qualified Data.List as List
import qualified Data.Set as Set
import Main hiding (main)
instance Arbitrary Person where
arbitrary = elements [minBound .. maxBound]
instance Arbitrary PayedFor where
arbitrary = do
payer <- arbitrary
amount <- arbitrary
receivers <- elements $ tail $ List.subsequences [minBound .. maxBound]
return (Payed payer amount receivers)
case_oneReceiver = ((Owes Klaus 200 Hans) `elem` (processAll [(Payed Hans 200 [Klaus])])) @=? True
case_twoReceivers = ((Owes Klaus 100 Hans) `elem` (processAll [(Payed Hans 200 [Klaus, Hans])])) @=? True
case_threeReceivers = ((Owes Klaus 100 Hans) `elem` (processAll [(Payed Hans 300 [Klaus, Hans, Elke])])) @=? True
case_fourReceivers = ((Owes Klaus 50 Hans) `elem` (processAll [(Payed Hans 200 [Klaus, Hans, Elke, Erna])])) @=? True
case_everythingEven = [] @=? (processAll
[ (Payed Hans 100 [Klaus])
, (Payed Klaus 100 [Erna])
, (Payed Erna 100 [Elke])
, (Payed Elke 100 [Hans])
])
case_minimalExample = Set.fromList expected @=? Set.fromList result
where
result = processAll
[ Hans `Payed` 10 `for` [Elke]
, Hans `Payed` 10 `for` [Erna]
, Peter `Payed` 11 `for` [Klaus]
]
expected =
[ Owes Klaus 11 Peter
, Owes Erna 10 Hans
, Owes Elke 10 Hans
]
case_minimalExample2 = Set.fromList expected @=? Set.fromList result
where
result = processAll
[ Hans `Payed` 10 `for` [Klaus]
, Peter `Payed` 10 `for` [Klaus]
, Maria `Payed` 22 `for` [Erna, Elke]
]
expected =
[ Owes Klaus 10 Hans
, Owes Klaus 10 Peter
, Owes Erna 11 Maria
, Owes Elke 11 Maria
]
case_negativeAmount = Set.fromList expected @=? Set.fromList result
where result = processAll [ (Payed Hans (-30) [Klaus, Erna, Elke]) ]
expected =
[ (Owes Hans 10 Klaus)
, (Owes Hans 10 Erna)
, (Owes Hans 10 Elke)
]
case_negativeAndPositiveAmount = Set.fromList expected @=? Set.fromList result
where result = processAll
[ Payed Hans (-30) [Klaus, Erna, Elke]
, Payed Erna 10 [Hans]
]
expected =
[ Owes Hans 10 Klaus
, Owes Hans 10 Elke
, Owes Hans 20 Erna
]
case_negativeAndPositiveAmount2 = Set.fromList expected @=? Set.fromList result
where result = processAll
[ Payed Hans (-30) [Klaus, Erna, Elke]
, Payed Hans 10 [Erna]
]
expected =
[ Owes Hans 10 Klaus
, Owes Hans 10 Elke
]
-- the case
-- [ Hans owes Erna
-- , ...
-- , Hans owes Erna
-- ]
-- does not occur
prop_eachPairOfPersonsAppearsOnlyOnce expenses = length result == (Set.size $ Set.fromList $ map personsOnly result)
where result = processAll expenses
personsOnly (Owes p1 _ p2) = (p1, p2)
prop_allResultAmountsArePositive expenses = all (> 0) $ map amountOnly result
where result = processAll expenses
amountOnly (Owes _ amount _) = amount
main = $(defaultMainGenerator)