1 {-# LANGUAGE OverloadedLists #-}
 
   2 {-# OPTIONS_GHC -fno-warn-orphans #-}
 
   3 module HUnit.Section where
 
   4 import Data.Either (Either(..))
 
   5 import Data.Eq (Eq(..))
 
   6 import Data.Function (($), (.))
 
   7 import Data.Hashable (Hashable)
 
   9 import Data.Maybe (Maybe(..))
 
  10 import Data.Ord (Ord(..))
 
  11 import Data.Ratio ((%))
 
  12 import Data.String (String)
 
  13 import Data.Tree (Tree(..))
 
  14 import GHC.Exts (IsList(..))
 
  15 import Prelude (Num(..))
 
  17 import Test.Tasty.HUnit
 
  18 import Text.Show (Show(..))
 
  20 import Majority.Judgment
 
  25 hunit = testGroup "Section"
 
  26  [ testSection "0 judge"
 
  28          ([]::Judges Int SchoolGrade)
 
  31  , testSection "1 judge, default grade"
 
  35          (Right $ node0 [(This, [(1,[(ToReject,1%1)])])])
 
  36  , testSection "1 judge, default grade, 2 choices"
 
  40          (Right $ node0 [ (This, [(1,[(ToReject,1%1)])])
 
  41                         , (That, [(1,[(ToReject,1%1)])])
 
  43  , testSection "1 judge, default grade"
 
  46          (node0 [(This,[(1,Section Nothing Nothing)])])
 
  47          (Right $ node0 [(This,[(1,[(ToReject,1%1)])])])
 
  48  , testSection "2 judges, default grade"
 
  50          [(1::Int,ToReject), (2::Int,ToReject)]
 
  52                  [ (This, [(1,Section Nothing Nothing)])
 
  55                  [ (This, [ (1,[(ToReject,1%1)])
 
  56                           , (2,[(ToReject,1%1)])
 
  59  , testSection "ErrorSection_unknown_choices"
 
  63          (Left $ ErrorSection_unknown_choices [This])
 
  64  , testSection "ErrorSection_unknown_choices"
 
  67          (node0 [(This,[(2,Section Nothing Nothing)])])
 
  68          (Left $ ErrorSection_unknown_choices [This])
 
  69  , testSection "ErrorSection_unknown_choices"
 
  72          (node0 [ (This,[(1,Section Nothing Nothing)])
 
  73                 , (That,[(2,Section Nothing Nothing)])
 
  75          (Left $ ErrorSection_unknown_choices [That])
 
  76  , testSection "ErrorSection_unknown_judges"
 
  79          (node0 [(This,[(2,Section Nothing Nothing)])])
 
  80          (Left $ ErrorSection_unknown_judges [(This,[2])])
 
  81  , testSection "1 judge, 1 grade"
 
  84          (node0 [(This,[(1,Section Nothing (Just Acceptable))])])
 
  85          (Right $ node0 [(This,[(1,[(Acceptable,1%1)])])])
 
  86  , testSection "1 judge, 1 grade, 2 sections"
 
  90                  [ (This, [(1,Section Nothing (Just Acceptable))]) ]
 
  91                  [ node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
 
  92                  , node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
 
  95                  [ (This, [(1,[(Acceptable,1%1)])]) ]
 
  96                  [ node0 [(This, [(1,[(Acceptable,1%1)])])]
 
  97                  , node0 [(This, [(1,[(Acceptable,1%1)])])]
 
  99  , testSection "sectionNodeShare with judge"
 
 101          [(1::Int,ToReject), (2,Insufficient)]
 
 103                  [(This, [(1,Section Nothing (Just Acceptable))])]
 
 104                  [ node0 $ SectionNode (Just $ 1%3) [(This, [ (1,Section (Just $ 1%2) Nothing)
 
 105                                                             , (2,Section Nothing Nothing)
 
 107                  , node0                            [(This, [ (1,Section (Just $ 1%2) Nothing)
 
 108                                                             , (2,Section Nothing (Just Good))
 
 112                  [ (This, [ (1,[(Acceptable,1%2 + 1%2)])
 
 113                           , (2,[(Insufficient,1%3), (Good,2%3)])
 
 115                  [ node0 [(This, [ (1,[(Acceptable,1%1)])
 
 116                                  , (2,[(Insufficient,1%1)])
 
 118                  , node0 [(This, [ (1,[(Acceptable,1%1)])
 
 122  , testSection "sectionNodeShare without judge"
 
 124          [(1::Int,ToReject), (2,Insufficient)]
 
 126                  [(This, [(1,Section Nothing (Just Acceptable))])]
 
 127                  [ node0 $ SectionNode (Just $ 1%3) [(This, [ (1,Section (Just $ 1%2) Nothing) ])]
 
 128                  , node0                            [(This, [ (1,Section (Just $ 1%2) Nothing)
 
 129                                                             , (2,Section Nothing (Just Good))
 
 133                  [ (This, [ (1,[(Acceptable,1%2 + 1%2)])
 
 134                           , (2,[(Insufficient,1%3), (Good,2%3)])
 
 136                  [ node0 [(This, [ (1,[(Acceptable,1%1)])
 
 137                                  , (2,[(Insufficient,1%1)])
 
 139                  , node0 [(This, [ (1,[(Acceptable,1%1)])
 
 143  , testSection "1 judge, 2 grades, 2 sections"
 
 147                  [ (This, [(1,Section Nothing (Just Acceptable))]) ]
 
 148                  [ node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
 
 149                  , node0 [(This, [(1,Section (Just $ 1%2) (Just Good))])]
 
 152                  [(This, [(1,[(Acceptable,1%2), (Good,1%2)])])]
 
 153                  [ node0 [(This, [(1,[(Acceptable,1%1)])])]
 
 154                  , node0 [(This, [(1,[(Good,1%1)])])]
 
 156  , testSection "1 judge, 2 grades, 2 sections (1 default)"
 
 160                  [ (This, [(1,Section Nothing (Just Acceptable))]) ]
 
 161                  [ node0 [(This, [(1,Section Nothing Nothing)])]
 
 162                  , node0 [(This, [(1,Section (Just $ 1%2) (Just Good))])]
 
 165                  [(This, [(1,[(Acceptable,1%2), (Good,1%2)])])]
 
 166                  [ node0 [(This, [(1,[(Acceptable,1%1)])])]
 
 167                  , node0 [(This, [(1,[(Good,1%1)])])]
 
 169  , testSection "1 judge, 3 grades, 3 sections (2 default)"
 
 173                  [ (This, [(1,Section Nothing (Just Acceptable))]) ]
 
 174                  [ node0 [(This, [(1,Section Nothing Nothing)])]
 
 175                  , node0 [(This, [(1,Section (Just $ 1%2) (Just Good))])]
 
 176                  , node0 [(This, [(1,Section Nothing (Just VeryGood))])]
 
 179                  [(This, [(1,[(Acceptable,1%4), (Good,1%2), (VeryGood,1%4)])])]
 
 180                  [ node0 [(This, [(1,[(Acceptable,1%1)])])]
 
 181                  , node0 [(This, [(1,[(Good,1%1)])])]
 
 182                  , node0 [(This, [(1,[(VeryGood,1%1)])])]
 
 184  , testSection "ErrorSection_invalid_shares sum not 1"
 
 188                  [ (This, [(1,Section Nothing (Just Acceptable))]) ]
 
 189                  [ node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
 
 190                  , node0 [(This, [(1,Section (Just $ 1%3) (Just Good))])]
 
 192          (Left $ ErrorSection_invalid_shares [(This, [(1,[1%2,1%3])])])
 
 193  , testSection "ErrorSection_invalid_shares negative share"
 
 197                  [ (This, [(1,Section Nothing (Just Acceptable))]) ]
 
 198                  [ node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
 
 199                  , node0 [(This, [(1,Section (Just $ -1%2) (Just Good))])]
 
 201          (Left $ ErrorSection_invalid_shares [(This, [(1,[1%2,-1%2])])])
 
 202  , testSection "2 judges, 3 grade, 3 sections (1 default)"
 
 204          [(1::Int,ToReject), (2::Int,ToReject)]
 
 206                  [ (This, [(1,Section Nothing (Just Acceptable))])
 
 209                          [ (This, [(1,Section Nothing Nothing)])
 
 212                          [ (This, [(1,Section (Just $ 1%2) (Just Good))])
 
 216                  [ (This, [ (1,[(Acceptable,1%2), (Good,1%2)])
 
 217                           , (2,[(ToReject,1%1)])
 
 221                          [ (This, [ (1,[(Acceptable,1%1)])
 
 222                                   , (2,[(ToReject,1%1)])
 
 226                          [ (This, [ (1,[(Good,1%1)])
 
 227                                   , (2,[(ToReject,1%1)])
 
 231  , testSection "2 judges, 4 grades, 5 sections (2 defaults)"
 
 233          [(1::Int,ToReject), (2::Int,ToReject)]
 
 235                  [ (This, [(1,Section Nothing (Just Acceptable))])
 
 238                          [ (This, [(1,Section Nothing Nothing)])
 
 241                          [ (This, [(1,Section (Just $ 1%2) (Just Good))])
 
 244                          [ (This, [(1,Section Nothing (Just Good))])
 
 247                                  [ (This, [ (1,Section Nothing (Just VeryGood))
 
 248                                           , (2,Section Nothing (Just Insufficient))
 
 254                  [ (This, [ (1,[(Acceptable,1%4), (Good,1%2), (VeryGood,1%4)])
 
 255                           , (2,[(ToReject,2%3), (Insufficient,1%3)])
 
 259                          [ (This, [ (1,[(Acceptable,1%1)])
 
 260                                   , (2,[(ToReject,1%1)])
 
 264                          [ (This, [ (1,[(Good,1%1)])
 
 265                                   , (2,[(ToReject,1%1)])
 
 269                          [ (This, [ (1,[(VeryGood,1%1)])
 
 270                                   , (2,[(Insufficient,1%1)])
 
 274                                  [ (This, [ (1,[(VeryGood,1%1)])
 
 275                                           , (2,[(Insufficient,1%1)])
 
 280  , testSection "1 judge, default grade, 2 choices"
 
 284          (Right $ node0 [ (This,[(1,[(ToReject,1%1)])])
 
 285                         , (That,[(1,[(ToReject,1%1)])])
 
 287  , testSection "2 judges, 2 choices"
 
 289          [(1::Int,ToReject), (2::Int,ToReject)]
 
 293                          [ (This, [(1,Section Nothing (Just Good))])
 
 294                          , (That, [(2,Section Nothing (Just Insufficient))])
 
 297                          [ (This, [(1,Section Nothing (Just Acceptable))])
 
 298                          , (That, [(2,Section Nothing (Just VeryGood))])
 
 302                  [ (This, [ (1,[(Good,1%2), (Acceptable,1%2)])
 
 303                           , (2,[(ToReject,1%1)])
 
 305                  , (That, [ (1,[(ToReject,1%1)])
 
 306                           , (2,[(Insufficient,1%2), (VeryGood,1%2)])
 
 309                  [ node0 [ (This, [ (1,[(Good,1%1)])
 
 310                                   , (2,[(ToReject,1%1)])
 
 312                          , (That, [ (1,[(ToReject,1%1)])
 
 313                                   , (2,[(Insufficient,1%1)])
 
 316                  , node0 [ (This, [ (1,[(Acceptable,1%1)])
 
 317                                   , (2,[(ToReject,1%1)])
 
 319                          , (That, [ (1,[(ToReject,1%1)])
 
 320                                   , (2,[(VeryGood,1%1)])
 
 324  , testSection "1 judge, 1 choice"
 
 328                  [ node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
 
 330                  , node0 [ (This, [(1,Section Nothing Nothing)])
 
 334                  [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
 
 336                  [ node0 [ (This, [(1,[(Acceptable, 1%1)])])
 
 338                  , node0 [ (This, [(1,[(ToReject, 1%1)])])
 
 341  , testSection "1 judge, 1 choice (missing judge)"
 
 345                  [ node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
 
 351                  [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
 
 353                  [ node0 [ (This, [(1,[(Acceptable, 1%1)])])
 
 355                  , node0 [ (This, [(1,[(ToReject, 1%1)])])
 
 358  , testSection "1 judge, 1 choice (missing judge)"
 
 364                  , node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
 
 368                  [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
 
 370                  [ node0 [ (This, [(1,[(ToReject, 1%1)])])
 
 372                  , node0 [ (This, [(1,[(Acceptable, 1%1)])])
 
 375  , testSection "1 judge, 1 choice (missing choice)"
 
 379                  [ node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
 
 385                  [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
 
 387                  [ node0 [ (This, [(1,[(Acceptable, 1%1)])])
 
 389                  , node0 [ (This, [(1,[(ToReject, 1%1)])])
 
 392  , testSection "1 judge, 1 choice (missing choice)"
 
 398                  , node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
 
 402                  [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
 
 404                  [ node0 [ (This, [(1,[(ToReject, 1%1)])])
 
 406                  , node0 [ (This, [(1,[(Acceptable, 1%1)])])
 
 409  , testSection "2 judges, 2 choices"
 
 411          [(1::Int,ToReject), (2::Int,ToReject)]
 
 413                  [ (This, [(1,Section Nothing (Just Acceptable))])
 
 414                  , (That, [(2,Section (Just $ 1%8) (Just VeryGood))])
 
 417                  [ (This, [ (1,[(Acceptable,1%1)])
 
 418                           , (2,[(ToReject,1%1)])
 
 420                  , (That, [ (1,[(ToReject,1%1)])
 
 421                           , (2,[(VeryGood,1%1)])
 
 424  , testSection "2 judges, 2 choices"
 
 426          [(1::Int,ToReject), (2::Int,ToReject)]
 
 430                          [ (This, [(1,Section Nothing (Just Good))])
 
 431                          , (That, [(2,Section Nothing (Just Insufficient))])
 
 434                          [ (This, [(1,Section Nothing (Just Acceptable))])
 
 435                          , (That, [(2,Section (Just $ 1%8) (Just VeryGood))])
 
 439                  [ (This, [ (1,[(Good,1%2), (Acceptable,1%2)])
 
 440                           , (2,[(ToReject,1%1)])
 
 442                  , (That, [ (1,[(ToReject,1%1)])
 
 443                           , (2,[(Insufficient,7%8), (VeryGood,1%8)])
 
 446                  [ node0 [ (This, [ (1,[(Good,1%1)])
 
 447                                   , (2,[(ToReject,1%1)])
 
 449                          , (That, [ (1,[(ToReject,1%1)])
 
 450                                   , (2,[(Insufficient,1%1)])
 
 453                  , node0 [ (This, [ (1,[(Acceptable,1%1)])
 
 454                                   , (2,[(ToReject,1%1)])
 
 456                          , (That, [ (1,[(ToReject,1%1)])
 
 457                                   , (2,[(VeryGood,1%1)])
 
 461  , testSection "2 judges, 2 choices"
 
 463          [(1::Int,ToReject), (2::Int,ToReject)]
 
 464          (Node [ (This, [(1,Section Nothing (Just Acceptable))])
 
 466                [ node0 [ (This, [(1,Section Nothing Nothing)])
 
 468                , node0 [ (This, [ (1,Section (Just $ 1%2) (Just Good)) ])
 
 469                        , (That, [ (1,Section (Just $ 1%3) Nothing)
 
 470                                 , (2,Section (Just $ 1%5) (Just Insufficient))
 
 473                , Node [ (This, [(1,Section Nothing (Just Good))])
 
 474                       , (That, [(2,Section Nothing (Just VeryGood))])
 
 476                       [ node0 [ (This, [ (1,Section Nothing (Just VeryGood))
 
 477                                        , (2,Section Nothing (Just Insufficient))
 
 479                               , (That, [ (1,Section Nothing (Just Acceptable)) ])
 
 481                       , node0 [ (This, [ (1,Section Nothing (Just Acceptable))
 
 483                               , (That, [ (1,Section Nothing (Just VeryGood))
 
 484                                        , (2,Section Nothing (Just Good))
 
 490                 Node [ (This, [ (1,[(Acceptable,1%4 + 1%8), (Good,1%2), (VeryGood,1%8)])
 
 491                               , (2,[(ToReject,1%3 + 1%3 + 1%6), (Insufficient,1%6)])
 
 493                      , (That, [ (1,[(ToReject,1%3 + 1%3), (Acceptable,1%6), (VeryGood,1%6)])
 
 494                               , (2,[(ToReject,4%10), (Insufficient,1%5), (VeryGood,4%20), (Good,4%20)])
 
 497                      [ node0 [ (This, [ (1,[(Acceptable,1%1)]) -- 1%4
 
 498                                       , (2,[(ToReject,1%1)])   -- 1%3
 
 500                              , (That, [ (1,[(ToReject,1%1)])   -- 1%3
 
 501                                       , (2,[(ToReject,1%1)])   -- 4%10
 
 504                      , node0 [ (This, [ (1,[(Good,1%1)])         -- 1%2
 
 505                                       , (2,[(ToReject,1%1)])     -- 1%3
 
 507                              , (That, [ (1,[(ToReject,1%1)])     -- 1%3
 
 508                                       , (2,[(Insufficient,1%1)]) -- 1%5
 
 511                      , Node [ (This, [ (1,[(VeryGood,1%2), (Acceptable,1%2)])   -- 1%4
 
 512                                      , (2,[(Insufficient,1%2), (ToReject,1%2)]) -- 1%3
 
 514                             , (That, [ (1,[(Acceptable,1%2), (VeryGood,1%2)])   -- 1%3
 
 515                                      , (2,[(VeryGood,1%2), (Good,1%2)])         -- 4%10
 
 518                             [ node0 [ (This, [ (1,[(VeryGood,1%1)])
 
 519                                              , (2,[(Insufficient,1%1)])
 
 521                                     , (That, [ (1,[(Acceptable,1%1)])
 
 522                                              , (2,[(VeryGood,1%1)])
 
 525                             , node0 [ (This, [ (1,[(Acceptable,1%1)])
 
 526                                              , (2,[(ToReject,1%1)])
 
 528                                     , (That, [ (1,[(VeryGood,1%1)])
 
 548  Judges judge grade ->
 
 549  Tree (SectionNode choice judge grade) ->
 
 550  Either (ErrorSection choice judge grade)
 
 551         (Tree (OpinionsByChoice choice judge grade)) ->
 
 553 testSection msg cs js ss expect =
 
 554         testCase (elide msg) $
 
 555                 opinionsBySection cs js ss @?= expect
 
 561  (Eq choice, Hashable choice) =>
 
 562  IsList (SectionNode choice judge grade) where
 
 563         type Item (SectionNode choice judge grade) = (choice, SectionByJudge judge grade)
 
 564         fromList = SectionNode Nothing . fromList
 
 565         toList = GHC.Exts.toList . sectionByJudgeByChoice