1
exercism fetch r perfect-numbers

test_perfect-numbers.R

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
source("./perfect-numbers.R")
library(testthat)

test_that("Smallest perfect number is classified correctly", {
  n <- 6
  expect_equal(number_type(n), "perfect")
})

test_that("Medium perfect number is classified correctly", {
  n <- 28
  expect_equal(number_type(n), "perfect")
})

test_that("Large perfect number is classified correctly", {
  n <- 33550336
  expect_equal(number_type(n), "perfect")
})



test_that("Smallest abundant number is classified correctly", {
  n <- 12
  expect_equal(number_type(n), "abundant")
})

test_that("Medium abundant number is classified correctly", {
  n <- 30
  expect_equal(number_type(n), "abundant")
})

test_that("Large abundant number is classified correctly", {
  n <- 33550335
  expect_equal(number_type(n), "abundant")
})



test_that("Smallest prime deficient number is classified correctly", {
  n <- 2
  expect_equal(number_type(n), "deficient")
})

test_that("Smallest non-prime deficient number is classified correctly", {
  n <- 4
  expect_equal(number_type(n), "deficient")
})

test_that("Medium deficient number is classified correctly", {
  n <- 32
  expect_equal(number_type(n), "deficient")
})

test_that("Large deficient number is classified correctly", {
  n <- 33550337
  expect_equal(number_type(n), "deficient")
})

test_that("Edge case (no factors other than itself) is classified correctly", {
  n <- 1
  expect_equal(number_type(n), "deficient")
})



test_that("Zero is rejected (not a natural number)", {
  n <- 0
  expect_error(number_type(n))
})

test_that("Negative integer is rejected (not a natural number)", {
  n <- -1
  expect_error(number_type(n))
})


message("All tests passed for exercise: perfect-numbers")