tests/testthat/test-compute_stat_mode.R

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode fails for list arg", {
  x <- list(matrix(1:9, 3, 3), c("a", "b", "c"))
  expect_jute_error(compute_stat_mode(x))
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode fails with message for list arg", {
  x <- list(matrix(1:9, 3, 3), c("a", "b", "c"))
  expect_snapshot(compute_stat_mode(x), error = TRUE)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode fails for matrix arg", {
  x <- matrix(1:9, 3, 3)
  expect_jute_error(compute_stat_mode(x))
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode fails with message for matrix arg", {
  x <- matrix(1:9, 3, 3)
  expect_snapshot(compute_stat_mode(x), error = TRUE)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode fails for type complex", {
  x <- complex(length.out = 3, 5)
  expect_jute_error(compute_stat_mode(x))
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode fails with message for complex vector", {
  x <- complex(length.out = 3, 5)
  expect_snapshot(compute_stat_mode(x), error = TRUE)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for integers", {
  expect_equal(compute_stat_mode(c(1, 1, 3, 5, 1, 3, 1, 2)), 1)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for integers with one duplicate", {
  expect_equal(compute_stat_mode(c(1, 3, 5, 3, 0)), 3)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for integers all unique", {
  expect_equal(compute_stat_mode(c(1, 3, 5, 2)), NA)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for integers all identical", {
  expect_equal(compute_stat_mode(c(4, 4, 4, 4, 4)), NA)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for integers all with same freq", {
  expect_equal(compute_stat_mode(c(4, 4, 2, 2, 8, 8)), NA)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for integers with 2 modes", {
  expect_equal(
    compute_stat_mode(c(63, 62, 66, 67, 63, 70, 67, 68, 61)),
    c(63, 67)
  )
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for a sample from a continuous RV", {
  set.seed(10)
  expect_equal(compute_stat_mode(rnorm(100)), -0.1795327, tolerance = 1e-4)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for logicals", {
  expect_equal(compute_stat_mode(c(TRUE, FALSE, FALSE, TRUE, FALSE)), FALSE)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for 2 logicals both unique", {
  expect_equal(compute_stat_mode(c(FALSE, TRUE)), NA)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for characters", {
  expect_equal(compute_stat_mode(c("a", "b", "c", "c")), "c")
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for characters all unique", {
  expect_equal(compute_stat_mode(c("a", "b", "c", "d")), NA)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for character strings", {
  expect_equal(
    compute_stat_mode(c("dog", "buffalo", "cat", "buffalo")),
    "buffalo"
  )
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for character strings with no mode", {
  expect_equal(compute_stat_mode(c("dog", "buffalo", "cat")), NA)
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for factors", {
  expect_equal(compute_stat_mode(as.factor(c("a", "b", "c", "c"))), "c")
})

# ---------------------------------------------------------------------------- #
test_that("compute_stat_mode works for factors all unique", {
  expect_equal(compute_stat_mode(as.factor(c("a", "b", "c", "d"))), NA)
})

# ---------------------------------------------------------------------------- #
toniprice/jute documentation built on Jan. 11, 2023, 8:23 a.m.