1 {-# LANGUAGE OverloadedLists #-}
2 module HUnit.Merit where
4 import Control.Arrow (second)
5 import Data.Eq (Eq(..))
6 import Data.Function (($), (.))
7 import Data.Functor ((<$>))
8 import Data.Hashable (Hashable)
10 import Data.List (zip)
11 import Data.Ord (Ord(..))
12 import Data.Ratio ((%))
14 import GHC.Exts (IsList(..))
15 import Text.Show (Show(..))
16 import qualified Data.HashMap.Strict as HM
17 import qualified Data.Set as Set
20 import Test.Tasty.HUnit
21 import Majority.Judgment
26 hunit = testGroup "Merit"
27 [ let m = mkMerit ['A'..'F'] in
28 testMajorityValueOfMerits
29 [ (The, m [136,307,251,148,84,74])
32 [ Middle ( 57 % 1) 'C' 'C'
33 , Middle (137 % 1) 'B' 'C'
34 , Middle (148 % 1) 'B' 'D'
35 , Middle ( 22 % 1) 'B' 'E'
36 , Middle ( 62 % 1) 'A' 'E'
37 , Middle ( 74 % 1) 'A' 'F'
40 , let m = mkMerit (enum::Set SchoolGrade) in
41 testMajorityValueOfMerits
42 [ (This, m [12,10,21,5,5,5])
43 , (That, m [12,16,22,3,3,3])
45 [ (This, [ Middle (7 % 1) Acceptable Acceptable
46 , Middle (7 % 1) Insufficient Acceptable
47 , Middle (3 % 1) Insufficient Good
48 , Middle (2 % 1) ToReject Good
49 , Middle (5 % 1) ToReject VeryGood
50 , Middle (5 % 1) ToReject Perfect
52 , (That, [ Middle ( 3 % 2) Acceptable Acceptable
53 , Middle (16 % 1) Insufficient Acceptable
54 , Middle ( 3 % 1) ToReject Acceptable
55 , Middle ( 3 % 1) ToReject Good
56 , Middle ( 3 % 1) ToReject VeryGood
57 , Middle ( 3 % 1) ToReject Perfect
61 mkMerit :: (Ord grade, Show grade) => Set grade -> [Share] -> Merit grade
62 mkMerit gs = fromList . (Set.toList gs`zip`)
65 (Eq choice, Hashable choice, Ord grade) =>
67 MeritByChoice choice grade
69 meritByChoice $ fromList $
70 second (fromList . zip [1::Int ..] . (singleGrade <$>)) <$> os
72 testMajorityValueOfMerits ::
73 (Show grade, Show choice, Ord grade, Eq choice, Hashable choice) =>
74 MeritByChoice choice grade ->
75 [(choice, [Middle grade])] -> TestTree
76 testMajorityValueOfMerits ms expect =
77 testCase (elide $ show $ unMeritByChoice ms) $
78 majorityValueByChoice ms
79 @?= (MajorityValue<$>HM.fromList expect)