1
exercism fetch r prime-factors

test_prime-factors.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
source("./prime-factors.R")
suppressPackageStartupMessages({
  library(testthat)
})

test_that("no factors", {
  number <- 1
  expect_equal(prime_factors(number),
               c())
})

test_that("prime number", {
  number <- 2
  expect_equal(prime_factors(number),
               c(2))
})

test_that("square of a prime", {
  number <- 9
  expect_equal(prime_factors(number),
               c(3, 3))
})

test_that("cube of a prime", {
  number <- 8
  expect_equal(prime_factors(number),
               c(2, 2, 2))
})

test_that("product of primes and non-primes", {
  number <- 12
  expect_equal(prime_factors(number),
               c(2, 2, 3))
})

test_that("product of primes", {
  number <- 901255
  expect_equal(prime_factors(number),
               c(5, 17, 23, 461))
})
test_that("factors include a large prime", {
  number <- 93819012551
  expect_equal(prime_factors(number),
               c(11, 9539, 894119))
})

message("All tests passed for exercise: prime-factors")