a b/tests/testthat/test-dirichlet.R
1
### Testing rdirichlet_alt
2
3
n <- 3
4
phi <- 2
5
mu <- c(0.01, 0.1, 0.29, 0.6)
6
7
# Incomplete testing of assertions on input
8
9
test_that("Error if n is not integerish", {
10
  expect_error(rdirichlet_alt(0.5, mu, phi))
11
})
12
13
test_that("Error if mu is not a vector", {
14
  expect_error(rdirichlet_alt(n, as.matrix(mu, nrow = 4), phi))
15
})
16
17
test_that("Error if mu is of length < 2", {
18
  expect_error(rdirichlet_alt(n, c(1), phi))
19
})
20
21
test_that("Error if mu is negative", {
22
  expect_error(rdirichlet_alt(n, c(-0.02, 0.9), phi))
23
})
24
25
test_that("Error if mu is zero", {
26
  expect_error(rdirichlet_alt(n, c(0.02, 0), phi))
27
})
28
29
test_that("Error if phi is zero", {
30
  expect_error(rdirichlet_alt(n, mu, 0))
31
})
32
33
test_that("Error if n is not a numeric scalar greater than 0", {
34
  expect_error(rdirichlet_alt(n, mu, 0))
35
})
36
37
38
# Testing of successful function execution
39
40
dirichlet_output <- rdirichlet_alt(n, mu, phi)
41
42
test_that("Output of rdirichlet_alt is a matrix", {
43
  expect_true(is.matrix(dirichlet_output))
44
})
45
46
test_that("Output of rdirichlet_alt is floating point", {
47
  expect_type(dirichlet_output, "double")
48
})
49
50
test_that("Matrix dimensions from rdirichlet_alt are correct", {
51
  expect_equal(dim(dirichlet_output), c(n, length(mu)))
52
})
53
54
test_that("No missing values in output from rdirichlet_alt", {
55
  expect_equal(sum(is.na(dirichlet_output)), 0)
56
})
57
58
test_that("Values from rdirichlet_alt are greater than 0", {
59
  expect_gt(range(dirichlet_output)[1], 0)
60
})
61
62
test_that("Values from rdirichlet_alt are less than 1", {
63
  expect_lt(range(dirichlet_output)[2], 1)
64
})
65
66
test_that("Matrix rows from rdirichlet_alt sum to 1", {
67
  expect_equal(rowSums(dirichlet_output), rep(1, n))
68
})
69