1
exercism fetch sml perfect-numbers

test.sml

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
(* version 1.0.1 *)

use "perfect-numbers.sml";
use "testlib.sml";

infixr |>
fun x |> f = f x

val testsuite =
  describe "perfect-numbers" [
    describe "Perfect numbers" [
      test "Smallest perfect number is classified correctly"
        (fn _ => classify (6) |> Expect.equalTo Perfect),

      test "Medium perfect number is classified correctly"
        (fn _ => classify (28) |> Expect.equalTo Perfect),

      test "Large perfect number is classified correctly"
        (fn _ => classify (33550336) |> Expect.equalTo Perfect)
    ],

    describe "Abundant numbers" [
      test "Smallest abundant number is classified correctly"
        (fn _ => classify (12) |> Expect.equalTo Abundant),

      test "Medium abundant number is classified correctly"
        (fn _ => classify (30) |> Expect.equalTo Abundant),

      test "Large abundant number is classified correctly"
        (fn _ => classify (33550335) |> Expect.equalTo Abundant)
    ],

    describe "Deficient numbers" [
      test "Smallest prime deficient number is classified correctly"
        (fn _ => classify (2) |> Expect.equalTo Deficient),

      test "Smallest non-prime deficient number is classified correctly"
        (fn _ => classify (4) |> Expect.equalTo Deficient),

      test "Medium deficient number is classified correctly"
        (fn _ => classify (32) |> Expect.equalTo Deficient),

      test "Large deficient number is classified correctly"
        (fn _ => classify (33550337) |> Expect.equalTo Deficient),

      test "Edge case (no factors other than itself) is classified correctly"
        (fn _ => classify (1) |> Expect.equalTo Deficient)
    ],

    describe "Invalid inputs" [
      test "Zero is rejected (not a natural number)"
        (fn _ => (fn _ => classify (~1)) |> Expect.error NotAPositiveInteger),

      test "Negative integer is rejected (not a natural number)"
        (fn _ => (fn _ => classify (~1)) |> Expect.error NotAPositiveInteger)
    ]
  ]

val _ = Test.run testsuite