1 {-# LANGUAGE OverloadedLists #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 import Test.Tasty.HUnit
8 import Control.Arrow (second)
9 import qualified Data.Map.Strict as Map
19 [(3,15), (2,7), (1,3), (0::Int,2)]
20 [(3,16), (2,6), (1,2), (0,3)]
21 , testGroup "OfMerits"
22 [ let m = mkMerit ['A'..'F'] in
24 [ (The, m [136,307,251,148,84,74])
26 [ (The, [('C',251),('B',307),('D',148),('E',84),('A',136),('F',74)])
28 , let m = mkMerit [ToReject .. TooGood] in
30 [ (This, m [12,10,21,5,5,5,2])
31 , (That, m [12,16,22,3,3,3,1])
33 [ (This, [(Acceptable,21),(Insufficient,10),(Good,5),(ToReject,12),(Perfect,5),(VeryGood,5),(TooGood,2)])
34 , (That, [(Acceptable,22),(Insufficient,16),(ToReject,12),(VeryGood,3),(Perfect,3),(Good,3),(TooGood,1)])
37 , testGroup "OfOpinions"
38 [ testValueOfOpinions [No,Yes]
47 [ (The, [(No,4),(Yes,2)])
49 , testValueOfOpinions [No,Yes]
58 [ (The, [(No,3),(Yes,3)])
60 , testValueOfOpinions [No,Yes]
69 [ (This, [(No,4),(Yes,2)])
70 , (That, [(Yes,5),(No,1)])
72 , testValueOfOpinions [No,Yes]
81 [ (This, [(No,6),(Yes,0)])
82 , (That, [(No,3),(Yes,3)])
84 , testValueOfOpinions [No,Yes]
93 [ (This, [(Yes,6),(No,0)])
94 , (That, [(No,3),(Yes,3)])
96 , testValueOfOpinions [No,Yes]
105 [ (This, [(Yes,4),(No,2)])
106 , (That, [(No,3),(Yes,3)])
108 , testValueOfOpinions [ToReject,Insufficient,Acceptable,Good,VeryGood,Perfect]
110 [ [Perfect,Perfect,Acceptable,VeryGood,Good,VeryGood]
111 , [Perfect,VeryGood,Perfect,Good,Acceptable,Acceptable]
112 , [VeryGood,VeryGood,Good,Acceptable,VeryGood,Insufficient]
113 , [Perfect,VeryGood,VeryGood,Good,Good,Acceptable]
114 , [Perfect,Good,VeryGood,Good,Good,Acceptable]
115 , [Perfect,VeryGood,Perfect,Good,Good,Good]
117 [ (1, [(Perfect,5),(VeryGood,1),(ToReject,0),(Insufficient,0),(Acceptable,0),(Good,0)])
118 , (2, [(VeryGood,4),(Good,1),(Perfect,1),(ToReject,0),(Insufficient,0),(Acceptable,0)])
119 , (3, [(VeryGood,2),(Good,1),(Perfect,2),(Acceptable,1),(ToReject,0),(Insufficient,0)])
120 , (4, [(Good,4),(Acceptable,1),(VeryGood,1),(ToReject,0),(Insufficient,0),(Perfect,0)])
121 , (5, [(Good,4),(Acceptable,1),(VeryGood,1),(ToReject,0),(Insufficient,0),(Perfect,0)])
122 , (6, [(Acceptable,3),(Good,1),(Insufficient,1),(VeryGood,1),(ToReject,0),(Perfect,0)])
129 elide :: String -> String
130 elide s | length s > 42 = take 42 s ++ ['…']
133 mkOpinion :: Ord prop =>
134 Choices prop -> judge -> [grade] ->
135 (judge, Opinion prop grade)
136 mkOpinion props j gs = (j, Map.fromList $ toList props `zip` gs)
138 mkMerit :: (Ord grade, Show grade) => [grade] -> [Count] -> Merit grade
139 mkMerit scal = fromList . (scal`zip`)
141 mkMerits :: (Ord prop, Ord grade) =>
142 [grade] -> Choices prop -> [[grade]] ->
144 mkMerits scal props opins =
145 merits (Scale (fromList scal) (head scal)) props $ fromList $
146 zipWith (mkOpinion props) [1::Int ..] opins
149 testCompareValue :: (Ord grade, Show grade) =>
150 [(grade, Count)] -> [(grade, Count)] -> TestTree
151 testCompareValue x y =
152 testGroup (elide $ show (x,y))
153 [ testCase "x == x" $ Value x`compare`Value x @?= EQ
154 , testCase "y == y" $ Value y`compare`Value y @?= EQ
155 , testCase "x < y" $ Value x`compare`Value y @?= LT
156 , testCase "y > x" $ Value y`compare`Value x @?= GT
159 testRanking :: (Ord prop, Ord grade, Show grade, Show prop) =>
160 [grade] -> Choices prop -> [[grade]] ->
161 Ranking prop -> TestTree
162 testRanking scal props opins expect =
163 testCase (elide $ show (toList props,opins)) $
164 majorityRanking (mkMerits scal props opins) @?= expect
166 testValueOfOpinions :: (Show grade, Show prop, Ord grade, Ord prop) =>
167 [grade] -> Choices prop -> [[grade]] ->
168 [(prop, [(grade,Count)])] -> TestTree
169 testValueOfOpinions scal props opins expect =
170 testCase (elide $ show (toList props,opins)) $
171 majorityValueByChoice (mkMerits scal props opins)
172 @?= ((Value`second`)<$>expect)
174 testValueOfMerits :: (Show grade, Show prop, Ord grade, Ord prop) =>
176 [(prop, [(grade,Count)])] -> TestTree
177 testValueOfMerits ms expect =
178 testCase (elide $ show ms) $
179 majorityValueByChoice ms
180 @?= ((Value`second`)<$>expect)