]> Git — Sourcephile - majurity.git/blob - test/HUnit.hs
Add default section share at judgment level.
[majurity.git] / test / HUnit.hs
1 {-# LANGUAGE OverloadedLists #-}
2 {-# LANGUAGE TypeFamilies #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module HUnit where
5
6 import Test.Tasty
7 import Test.Tasty.HUnit
8
9 import Control.Arrow (second)
10 import Data.Hashable (Hashable)
11 import Data.Ratio ((%))
12 import Data.Tree (Tree(..))
13 import GHC.Exts (IsList(..))
14 import Prelude
15 import qualified Data.HashMap.Strict as HM
16
17 import Hjugement
18 import Types
19
20 hunits :: TestTree
21 hunits =
22 testGroup "HUnit"
23 [ testGroup "MajorityValue" $
24 [ testCompareValue
25 [(3,15), (2,7), (1,3), (0::Int,2)]
26 [(3,16), (2,6), (1,2), (0,3)]
27 , testGroup "Merit"
28 [ let m = mkMerit ['A'..'F'] in
29 testMajorityValueOfMerits
30 [ (The, m [136,307,251,148,84,74])
31 ]
32 [ (The, [('C',251),('B',307),('D',148),('E',84),('A',136),('F',74)])
33 ]
34 , let m = mkMerit [ToReject .. TooGood] in
35 testMajorityValueOfMerits
36 [ (This, m [12,10,21,5,5,5,2])
37 , (That, m [12,16,22,3,3,3,1])
38 ]
39 [ (This, [(Acceptable,21),(Insufficient,10),(Good,5),(ToReject,12),(Perfect,5),(VeryGood,5),(TooGood,2)])
40 , (That, [(Acceptable,22),(Insufficient,16),(ToReject,12),(VeryGood,3),(Perfect,3),(Good,3),(TooGood,1)])
41 ]
42 ]
43 , testGroup "MajorityRanking"
44 [ testMajorityValueOfOpinions
45 [ (The, [No,No,No,No,Yes,Yes]) ]
46 [ (The, [(No,4),(Yes,2)]) ]
47 , testMajorityValueOfOpinions
48 [ (The, [No,No,No,Yes,Yes,Yes]) ]
49 [ (The, [(No,3),(Yes,3)]) ]
50 , testMajorityValueOfOpinions
51 [ (This, [No,No,No,No,Yes,Yes])
52 , (That, [No,Yes,Yes,Yes,Yes,Yes])
53 ]
54 [ (This, [(No,4),(Yes,2)])
55 , (That, [(Yes,5),(No,1)])
56 ]
57 , testMajorityValueOfOpinions
58 [ (This, [No,No,No,No,No,No])
59 , (That, [No,No,No,Yes,Yes,Yes])
60 ]
61 [ (This, [(No,6)])
62 , (That, [(No,3),(Yes,3)])
63 ]
64 , testMajorityValueOfOpinions
65 [ (This, [Yes,Yes,Yes,Yes,Yes,Yes])
66 , (That, [No,No,No,Yes,Yes,Yes])
67 ]
68 [ (This, [(Yes,6)])
69 , (That, [(No,3),(Yes,3)])
70 ]
71 , testMajorityValueOfOpinions
72 [ (This, [No,No,Yes,Yes,Yes,Yes])
73 , (That, [No,No,No,Yes,Yes,Yes])
74 ]
75 [ (This, [(Yes,4),(No,2)])
76 , (That, [(No,3),(Yes,3)])
77 ]
78 , testMajorityValueOfOpinions
79 [ (1::Int, [Perfect,Perfect,VeryGood,Perfect,Perfect,Perfect])
80 , (2, [Perfect,VeryGood,VeryGood,VeryGood,Good,VeryGood])
81 , (3, [Acceptable,Perfect,Good,VeryGood,VeryGood,Perfect])
82 , (4, [VeryGood,Good,Acceptable,Good,Good,Good])
83 , (5, [Good,Acceptable,VeryGood,Good,Good,Good])
84 , (6, [VeryGood,Acceptable,Insufficient,Acceptable,Acceptable,Good])
85 ]
86 [ (1, [(Perfect,5),(VeryGood,1)])
87 , (2, [(VeryGood,4),(Good,1),(Perfect,1)])
88 , (3, [(VeryGood,2),(Good,1),(Perfect,2),(Acceptable,1)])
89 , (4, [(Good,4),(Acceptable,1),(VeryGood,1)])
90 , (5, [(Good,4),(Acceptable,1),(VeryGood,1)])
91 , (6, [(Acceptable,3),(Good,1),(Insufficient,1),(VeryGood,1)])
92 ]
93 ]
94 , testGroup "Section"
95 [ testSection "0 judge"
96 ([]::Choices C2)
97 ([]::Judges Int G6)
98 (node0 [])
99 (Right $ node0 [])
100 , testSection "1 judge, default grade"
101 [This]
102 [(1::Int,ToReject)]
103 (node0 [])
104 (Right $ node0 [(This, [(1,[(ToReject,1%1)])])])
105 , testSection "1 judge, default grade, 2 choices"
106 [This, That]
107 [(1::Int,ToReject)]
108 (node0 [])
109 (Right $ node0 [ (This, [(1,[(ToReject,1%1)])])
110 , (That, [(1,[(ToReject,1%1)])])
111 ])
112 , testSection "1 judge, default grade"
113 [This]
114 [(1::Int,ToReject)]
115 (node0 [(This,[(1,Section Nothing Nothing)])])
116 (Right $ node0 [(This,[(1,[(ToReject,1%1)])])])
117 , testSection "2 judges, default grade"
118 [This]
119 [(1::Int,ToReject), (2::Int,ToReject)]
120 (node0
121 [ (This, [(1,Section Nothing Nothing)])
122 ])
123 (Right $ node0
124 [ (This, [ (1,[(ToReject,1%1)])
125 , (2,[(ToReject,1%1)])
126 ])
127 ])
128 , testSection "ErrorSection_unknown_choices"
129 []
130 [(1::Int,ToReject)]
131 (node0 [(This,[])])
132 (Left $ ErrorSection_unknown_choices [This])
133 , testSection "ErrorSection_unknown_choices"
134 []
135 [(1::Int,ToReject)]
136 (node0 [(This,[(2,Section Nothing Nothing)])])
137 (Left $ ErrorSection_unknown_choices [This])
138 , testSection "ErrorSection_unknown_choices"
139 [This]
140 [(1::Int,ToReject)]
141 (node0 [ (This,[(1,Section Nothing Nothing)])
142 , (That,[(2,Section Nothing Nothing)])
143 ])
144 (Left $ ErrorSection_unknown_choices [That])
145 , testSection "ErrorSection_unknown_judges"
146 [This]
147 [(1::Int,ToReject)]
148 (node0 [(This,[(2,Section Nothing Nothing)])])
149 (Left $ ErrorSection_unknown_judges [(This,[2])])
150 , testSection "1 judge, 1 grade"
151 [This]
152 [(1::Int,ToReject)]
153 (node0 [(This,[(1,Section Nothing (Just Acceptable))])])
154 (Right $ node0 [(This,[(1,[(Acceptable,1%1)])])])
155 , testSection "1 judge, 1 grade, 2 sections"
156 [This]
157 [(1::Int,ToReject)]
158 (Node
159 [ (This, [(1,Section Nothing (Just Acceptable))]) ]
160 [ node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
161 , node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
162 ])
163 (Right $ Node
164 [ (This, [(1,[(Acceptable,1%1)])]) ]
165 [ node0 [(This, [(1,[(Acceptable,1%1)])])]
166 , node0 [(This, [(1,[(Acceptable,1%1)])])]
167 ])
168 , testSection "sectionNodeShare with judge"
169 [This]
170 [(1::Int,ToReject), (2,Insufficient)]
171 (Node
172 [(This, [(1,Section Nothing (Just Acceptable))])]
173 [ node0 $ SectionNode (Just $ 1%3) [(This, [ (1,Section (Just $ 1%2) Nothing)
174 , (2,Section Nothing Nothing)
175 ])]
176 , node0 [(This, [ (1,Section (Just $ 1%2) Nothing)
177 , (2,Section Nothing (Just Good))
178 ])]
179 ])
180 (Right $ Node
181 [ (This, [ (1,[(Acceptable,1%2 + 1%2)])
182 , (2,[(Insufficient,1%3), (Good,2%3)])
183 ]) ]
184 [ node0 [(This, [ (1,[(Acceptable,1%1)])
185 , (2,[(Insufficient,1%1)])
186 ])]
187 , node0 [(This, [ (1,[(Acceptable,1%1)])
188 , (2,[(Good,1%1)])
189 ])]
190 ])
191 , testSection "sectionNodeShare without judge"
192 [This]
193 [(1::Int,ToReject), (2,Insufficient)]
194 (Node
195 [(This, [(1,Section Nothing (Just Acceptable))])]
196 [ node0 $ SectionNode (Just $ 1%3) [(This, [ (1,Section (Just $ 1%2) Nothing) ])]
197 , node0 [(This, [ (1,Section (Just $ 1%2) Nothing)
198 , (2,Section Nothing (Just Good))
199 ])]
200 ])
201 (Right $ Node
202 [ (This, [ (1,[(Acceptable,1%2 + 1%2)])
203 , (2,[(Insufficient,1%3), (Good,2%3)])
204 ]) ]
205 [ node0 [(This, [ (1,[(Acceptable,1%1)])
206 , (2,[(Insufficient,1%1)])
207 ])]
208 , node0 [(This, [ (1,[(Acceptable,1%1)])
209 , (2,[(Good,1%1)])
210 ])]
211 ])
212 , testSection "1 judge, 2 grades, 2 sections"
213 [This]
214 [(1::Int,ToReject)]
215 (Node
216 [ (This, [(1,Section Nothing (Just Acceptable))]) ]
217 [ node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
218 , node0 [(This, [(1,Section (Just $ 1%2) (Just Good))])]
219 ])
220 (Right $ Node
221 [(This, [(1,[(Acceptable,1%2), (Good,1%2)])])]
222 [ node0 [(This, [(1,[(Acceptable,1%1)])])]
223 , node0 [(This, [(1,[(Good,1%1)])])]
224 ])
225 , testSection "1 judge, 2 grades, 2 sections (1 default)"
226 [This]
227 [(1::Int,ToReject)]
228 (Node
229 [ (This, [(1,Section Nothing (Just Acceptable))]) ]
230 [ node0 [(This, [(1,Section Nothing Nothing)])]
231 , node0 [(This, [(1,Section (Just $ 1%2) (Just Good))])]
232 ])
233 (Right $ Node
234 [(This, [(1,[(Acceptable,1%2), (Good,1%2)])])]
235 [ node0 [(This, [(1,[(Acceptable,1%1)])])]
236 , node0 [(This, [(1,[(Good,1%1)])])]
237 ])
238 , testSection "1 judge, 3 grades, 3 sections (2 default)"
239 [This]
240 [(1::Int,ToReject)]
241 (Node
242 [ (This, [(1,Section Nothing (Just Acceptable))]) ]
243 [ node0 [(This, [(1,Section Nothing Nothing)])]
244 , node0 [(This, [(1,Section (Just $ 1%2) (Just Good))])]
245 , node0 [(This, [(1,Section Nothing (Just VeryGood))])]
246 ])
247 (Right $ Node
248 [(This, [(1,[(Acceptable,1%4), (Good,1%2), (VeryGood,1%4)])])]
249 [ node0 [(This, [(1,[(Acceptable,1%1)])])]
250 , node0 [(This, [(1,[(Good,1%1)])])]
251 , node0 [(This, [(1,[(VeryGood,1%1)])])]
252 ])
253 , testSection "ErrorSection_invalid_shares sum not 1"
254 [This]
255 [(1::Int,ToReject)]
256 (Node
257 [ (This, [(1,Section Nothing (Just Acceptable))]) ]
258 [ node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
259 , node0 [(This, [(1,Section (Just $ 1%3) (Just Good))])]
260 ])
261 (Left $ ErrorSection_invalid_shares [(This, [(1,[1%2,1%3])])])
262 , testSection "ErrorSection_invalid_shares negative share"
263 [This]
264 [(1::Int,ToReject)]
265 (Node
266 [ (This, [(1,Section Nothing (Just Acceptable))]) ]
267 [ node0 [(This, [(1,Section (Just $ 1%2) Nothing)])]
268 , node0 [(This, [(1,Section (Just $ -1%2) (Just Good))])]
269 ])
270 (Left $ ErrorSection_invalid_shares [(This, [(1,[1%2,-1%2])])])
271 , testSection "2 judges, 3 grade, 3 sections (1 default)"
272 [This]
273 [(1::Int,ToReject), (2::Int,ToReject)]
274 (Node
275 [ (This, [(1,Section Nothing (Just Acceptable))])
276 ]
277 [ node0
278 [ (This, [(1,Section Nothing Nothing)])
279 ]
280 , node0
281 [ (This, [(1,Section (Just $ 1%2) (Just Good))])
282 ]
283 ])
284 (Right $ Node
285 [ (This, [ (1,[(Acceptable,1%2), (Good,1%2)])
286 , (2,[(ToReject,1%1)])
287 ])
288 ]
289 [ node0
290 [ (This, [ (1,[(Acceptable,1%1)])
291 , (2,[(ToReject,1%1)])
292 ])
293 ]
294 , node0
295 [ (This, [ (1,[(Good,1%1)])
296 , (2,[(ToReject,1%1)])
297 ])
298 ]
299 ])
300 , testSection "2 judges, 4 grades, 5 sections (2 defaults)"
301 [This]
302 [(1::Int,ToReject), (2::Int,ToReject)]
303 (Node
304 [ (This, [(1,Section Nothing (Just Acceptable))])
305 ]
306 [ node0
307 [ (This, [(1,Section Nothing Nothing)])
308 ]
309 , node0
310 [ (This, [(1,Section (Just $ 1%2) (Just Good))])
311 ]
312 , Node
313 [ (This, [(1,Section Nothing (Just Good))])
314 ]
315 [ node0
316 [ (This, [ (1,Section Nothing (Just VeryGood))
317 , (2,Section Nothing (Just Insufficient))
318 ])
319 ]
320 ]
321 ])
322 (Right $ Node
323 [ (This, [ (1,[(Acceptable,1%4), (Good,1%2), (VeryGood,1%4)])
324 , (2,[(ToReject,2%3), (Insufficient,1%3)])
325 ])
326 ]
327 [ node0
328 [ (This, [ (1,[(Acceptable,1%1)])
329 , (2,[(ToReject,1%1)])
330 ])
331 ]
332 , node0
333 [ (This, [ (1,[(Good,1%1)])
334 , (2,[(ToReject,1%1)])
335 ])
336 ]
337 , Node
338 [ (This, [ (1,[(VeryGood,1%1)])
339 , (2,[(Insufficient,1%1)])
340 ])
341 ]
342 [ node0
343 [ (This, [ (1,[(VeryGood,1%1)])
344 , (2,[(Insufficient,1%1)])
345 ])
346 ]
347 ]
348 ])
349 , testSection "1 judge, default grade, 2 choices"
350 [This, That]
351 [(1::Int,ToReject)]
352 (node0 [])
353 (Right $ node0 [ (This,[(1,[(ToReject,1%1)])])
354 , (That,[(1,[(ToReject,1%1)])])
355 ])
356 , testSection "2 judges, 2 choices"
357 [This, That]
358 [(1::Int,ToReject), (2::Int,ToReject)]
359 (Node
360 [ ]
361 [ node0
362 [ (This, [(1,Section Nothing (Just Good))])
363 , (That, [(2,Section Nothing (Just Insufficient))])
364 ]
365 , node0
366 [ (This, [(1,Section Nothing (Just Acceptable))])
367 , (That, [(2,Section Nothing (Just VeryGood))])
368 ]
369 ])
370 (Right $ Node
371 [ (This, [ (1,[(Good,1%2), (Acceptable,1%2)])
372 , (2,[(ToReject,1%1)])
373 ])
374 , (That, [ (1,[(ToReject,1%1)])
375 , (2,[(Insufficient,1%2), (VeryGood,1%2)])
376 ])
377 ]
378 [ node0 [ (This, [ (1,[(Good,1%1)])
379 , (2,[(ToReject,1%1)])
380 ])
381 , (That, [ (1,[(ToReject,1%1)])
382 , (2,[(Insufficient,1%1)])
383 ])
384 ]
385 , node0 [ (This, [ (1,[(Acceptable,1%1)])
386 , (2,[(ToReject,1%1)])
387 ])
388 , (That, [ (1,[(ToReject,1%1)])
389 , (2,[(VeryGood,1%1)])
390 ])
391 ]
392 ])
393 , testSection "1 judge, 1 choice"
394 [This]
395 [(1::Int,ToReject)]
396 (Node []
397 [ node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
398 ]
399 , node0 [ (This, [(1,Section Nothing Nothing)])
400 ]
401 ])
402 (Right $ Node
403 [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
404 ]
405 [ node0 [ (This, [(1,[(Acceptable, 1%1)])])
406 ]
407 , node0 [ (This, [(1,[(ToReject, 1%1)])])
408 ]
409 ])
410 , testSection "1 judge, 1 choice (missing judge)"
411 [This]
412 [(1::Int,ToReject)]
413 (Node []
414 [ node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
415 ]
416 , node0 [ (This, [])
417 ]
418 ])
419 (Right $ Node
420 [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
421 ]
422 [ node0 [ (This, [(1,[(Acceptable, 1%1)])])
423 ]
424 , node0 [ (This, [(1,[(ToReject, 1%1)])])
425 ]
426 ])
427 , testSection "1 judge, 1 choice (missing judge)"
428 [This]
429 [(1::Int,ToReject)]
430 (Node []
431 [ node0 [ (This, [])
432 ]
433 , node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
434 ]
435 ])
436 (Right $ Node
437 [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
438 ]
439 [ node0 [ (This, [(1,[(ToReject, 1%1)])])
440 ]
441 , node0 [ (This, [(1,[(Acceptable, 1%1)])])
442 ]
443 ])
444 , testSection "1 judge, 1 choice (missing choice)"
445 [This]
446 [(1::Int,ToReject)]
447 (Node []
448 [ node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
449 ]
450 , node0 [
451 ]
452 ])
453 (Right $ Node
454 [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
455 ]
456 [ node0 [ (This, [(1,[(Acceptable, 1%1)])])
457 ]
458 , node0 [ (This, [(1,[(ToReject, 1%1)])])
459 ]
460 ])
461 , testSection "1 judge, 1 choice (missing choice)"
462 [This]
463 [(1::Int,ToReject)]
464 (Node []
465 [ node0 [
466 ]
467 , node0 [ (This, [(1,Section (Just $ 1%8) (Just Acceptable))])
468 ]
469 ])
470 (Right $ Node
471 [ (This, [(1,[(Acceptable,1%8), (ToReject,7%8)])])
472 ]
473 [ node0 [ (This, [(1,[(ToReject, 1%1)])])
474 ]
475 , node0 [ (This, [(1,[(Acceptable, 1%1)])])
476 ]
477 ])
478 , testSection "2 judges, 2 choices"
479 [This, That]
480 [(1::Int,ToReject), (2::Int,ToReject)]
481 (node0
482 [ (This, [(1,Section Nothing (Just Acceptable))])
483 , (That, [(2,Section (Just $ 1%8) (Just VeryGood))])
484 ])
485 (Right $ node0
486 [ (This, [ (1,[(Acceptable,1%1)])
487 , (2,[(ToReject,1%1)])
488 ])
489 , (That, [ (1,[(ToReject,1%1)])
490 , (2,[(VeryGood,1%1)])
491 ])
492 ])
493 , testSection "2 judges, 2 choices"
494 [This, That]
495 [(1::Int,ToReject), (2::Int,ToReject)]
496 (Node
497 [ ]
498 [ node0
499 [ (This, [(1,Section Nothing (Just Good))])
500 , (That, [(2,Section Nothing (Just Insufficient))])
501 ]
502 , node0
503 [ (This, [(1,Section Nothing (Just Acceptable))])
504 , (That, [(2,Section (Just $ 1%8) (Just VeryGood))])
505 ]
506 ])
507 (Right $ Node
508 [ (This, [ (1,[(Good,1%2), (Acceptable,1%2)])
509 , (2,[(ToReject,1%1)])
510 ])
511 , (That, [ (1,[(ToReject,1%1)])
512 , (2,[(Insufficient,7%8), (VeryGood,1%8)])
513 ])
514 ]
515 [ node0 [ (This, [ (1,[(Good,1%1)])
516 , (2,[(ToReject,1%1)])
517 ])
518 , (That, [ (1,[(ToReject,1%1)])
519 , (2,[(Insufficient,1%1)])
520 ])
521 ]
522 , node0 [ (This, [ (1,[(Acceptable,1%1)])
523 , (2,[(ToReject,1%1)])
524 ])
525 , (That, [ (1,[(ToReject,1%1)])
526 , (2,[(VeryGood,1%1)])
527 ])
528 ]
529 ])
530 , testSection "2 judges, 2 choices"
531 [This, That]
532 [(1::Int,ToReject), (2::Int,ToReject)]
533 (Node [ (This, [(1,Section Nothing (Just Acceptable))])
534 ]
535 [ node0 [ (This, [(1,Section Nothing Nothing)])
536 ]
537 , node0 [ (This, [ (1,Section (Just $ 1%2) (Just Good)) ])
538 , (That, [ (1,Section (Just $ 1%3) Nothing)
539 , (2,Section (Just $ 1%5) (Just Insufficient))
540 ])
541 ]
542 , Node [ (This, [(1,Section Nothing (Just Good))])
543 , (That, [(2,Section Nothing (Just VeryGood))])
544 ]
545 [ node0 [ (This, [ (1,Section Nothing (Just VeryGood))
546 , (2,Section Nothing (Just Insufficient))
547 ])
548 , (That, [ (1,Section Nothing (Just Acceptable)) ])
549 ]
550 , node0 [ (This, [ (1,Section Nothing (Just Acceptable))
551 ])
552 , (That, [ (1,Section Nothing (Just VeryGood))
553 , (2,Section Nothing (Just Good))
554 ])
555 ]
556 ]
557 ])
558 (Right $
559 Node [ (This, [ (1,[(Acceptable,1%4 + 1%8), (Good,1%2), (VeryGood,1%8)])
560 , (2,[(ToReject,1%3 + 1%3 + 1%6), (Insufficient,1%6)])
561 ])
562 , (That, [ (1,[(ToReject,1%3 + 1%3), (Acceptable,1%6), (VeryGood,1%6)])
563 , (2,[(ToReject,4%10), (Insufficient,1%5), (VeryGood,4%20), (Good,4%20)])
564 ])
565 ]
566 [ node0 [ (This, [ (1,[(Acceptable,1%1)]) -- 1%4
567 , (2,[(ToReject,1%1)]) -- 1%3
568 ])
569 , (That, [ (1,[(ToReject,1%1)]) -- 1%3
570 , (2,[(ToReject,1%1)]) -- 4%10
571 ])
572 ]
573 , node0 [ (This, [ (1,[(Good,1%1)]) -- 1%2
574 , (2,[(ToReject,1%1)]) -- 1%3
575 ])
576 , (That, [ (1,[(ToReject,1%1)]) -- 1%3
577 , (2,[(Insufficient,1%1)]) -- 1%5
578 ])
579 ]
580 , Node [ (This, [ (1,[(VeryGood,1%2), (Acceptable,1%2)]) -- 1%4
581 , (2,[(Insufficient,1%2), (ToReject,1%2)]) -- 1%3
582 ])
583 , (That, [ (1,[(Acceptable,1%2), (VeryGood,1%2)]) -- 1%3
584 , (2,[(VeryGood,1%2), (Good,1%2)]) -- 4%10
585 ])
586 ]
587 [ node0 [ (This, [ (1,[(VeryGood,1%1)])
588 , (2,[(Insufficient,1%1)])
589 ])
590 , (That, [ (1,[(Acceptable,1%1)])
591 , (2,[(VeryGood,1%1)])
592 ])
593 ]
594 , node0 [ (This, [ (1,[(Acceptable,1%1)])
595 , (2,[(ToReject,1%1)])
596 ])
597 , (That, [ (1,[(VeryGood,1%1)])
598 , (2,[(Good,1%1)])
599 ])
600 ]
601 ]
602 ]
603 )
604 ]
605 ]
606 ]
607
608 elide :: String -> String
609 elide s | length s > 42 = take 42 s ++ ['…']
610 | otherwise = s
611
612 mkMerit :: (Ord grade, Show grade) => [grade] -> [Share] -> Merit grade
613 mkMerit gs = fromList . (gs`zip`)
614
615 mkMeritByChoice ::
616 (Eq choice, Hashable choice, Ord grade) =>
617 [(choice,[grade])] ->
618 MeritByChoice choice grade
619 mkMeritByChoice os =
620 meritByChoice $ fromList $
621 second (fromList . zip [1::Int ..] . (singleGrade <$>)) <$> os
622
623 testCompareValue :: (Ord grade, Show grade) =>
624 [(grade, Share)] -> [(grade, Share)] -> TestTree
625 testCompareValue x y =
626 testGroup (elide $ show (x,y))
627 [ testCase "x == x" $ MajorityValue x`compare`MajorityValue x @?= EQ
628 , testCase "y == y" $ MajorityValue y`compare`MajorityValue y @?= EQ
629 , testCase "x < y" $ MajorityValue x`compare`MajorityValue y @?= LT
630 , testCase "y > x" $ MajorityValue y`compare`MajorityValue x @?= GT
631 ]
632
633 testMajorityRanking ::
634 (Eq choice, Hashable choice, Ord grade, Show grade, Show choice) =>
635 [(choice, [grade])] ->
636 MajorityRanking choice grade -> TestTree
637 testMajorityRanking os expect =
638 testCase (elide $ show os) $
639 majorityRanking (mkMeritByChoice os) @?= expect
640
641 testMajorityValueOfOpinions ::
642 (Show grade, Show choice, Ord grade, Eq choice, Hashable choice) =>
643 [(choice, [grade])] ->
644 [(choice, [(grade,Share)])] -> TestTree
645 testMajorityValueOfOpinions os expect =
646 testCase (elide $ show os) $
647 majorityValueByChoice (mkMeritByChoice os)
648 @?= (MajorityValue<$>HM.fromList expect)
649
650 testMajorityValueOfMerits ::
651 (Show grade, Show choice, Ord grade, Eq choice, Hashable choice) =>
652 MeritByChoice choice grade ->
653 [(choice, [(grade,Share)])] -> TestTree
654 testMajorityValueOfMerits ms expect =
655 testCase (elide $ show ms) $
656 majorityValueByChoice ms
657 @?= (MajorityValue<$>HM.fromList expect)
658
659 testSection ::
660 Eq choice =>
661 Hashable choice =>
662 Eq judge =>
663 Hashable judge =>
664 Ord grade =>
665 Show choice =>
666 Show judge =>
667 Show grade =>
668 String ->
669 Choices choice ->
670 Judges judge grade ->
671 Tree (SectionNode choice judge grade) ->
672 Either (ErrorSection choice judge grade)
673 (Tree (OpinionsByChoice choice judge grade)) ->
674 TestTree
675 testSection msg cs js ss expect =
676 testCase (elide msg) $
677 opinionsBySection cs js ss @?= expect
678
679 node0 :: a -> Tree a
680 node0 = (`Node`[])
681
682 instance (Eq choice, Hashable choice) => IsList (SectionNode choice judge grade) where
683 type Item (SectionNode choice judge grade) = (choice, SectionByJudge judge grade)
684 fromList = SectionNode Nothing . fromList
685 toList = GHC.Exts.toList . sectionByJudgeByChoice