1
exercism fetch haskell perfect-numbers

test/Tests.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
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable     (for_)
import Test.Hspec        (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import PerfectNumbers

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "classify" $ for_ cases test
  where

    test Case{..} = it description assertion
      where
        assertion = classify number `shouldBe` expected


data Case = Case { description :: String
                 , number      :: Int
                 , expected    :: Maybe Classification
                 }

cases :: [Case]
cases = [ Case { description = "Smallest perfect number is classified correctly"
               , number      = 6
               , expected    = Just Perfect
               }
        , Case { description = "Medium perfect number is classified correctly"
               , number      = 28
               , expected    = Just Perfect
               }
        , Case { description = "Large perfect number is classified correctly"
               , number      = 33550336
               , expected    = Just Perfect
               }
        , Case { description = "Smallest abundant number is classified correctly"
               , number      = 12
               , expected    = Just Abundant
               }
        , Case { description = "Medium abundant number is classified correctly"
               , number      = 30
               , expected    = Just Abundant
               }
        , Case { description = "Large abundant number is classified correctly"
               , number      = 33550335
               , expected    = Just Abundant
               }
        , Case { description = "Smallest prime deficient number is classified correctly"
               , number      = 2
               , expected    = Just Deficient
               }
        , Case { description = "Smallest non-prime deficient number is classified correctly"
               , number      = 4
               , expected    = Just Deficient
               }
        , Case { description = "Medium deficient number is classified correctly"
               , number      = 32
               , expected    = Just Deficient
               }
        , Case { description = "Large deficient number is classified correctly"
               , number      = 33550337
               , expected    = Just Deficient
               }
        , Case { description = "Edge case (no factors other than itself) is classified correctly"
               , number      = 1
               , expected    = Just Deficient
               }
        , Case { description = "Zero is rejected (not a natural number)"
               , number      = 0
               , expected    = Nothing
               }
        , Case { description = "Negative integer is rejected (not a natural number)"
               , number      = -1
               , expected    = Nothing
               }
        ]