From 01b4fab13ef82dfdc3e17fd5f8cc2903e87feeef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Florian=20G=C3=A4rber?= <florian.gaerber@uni-hamburg.de>
Date: Fri, 21 Jul 2023 12:33:55 +0200
Subject: [PATCH] test: Add test cases for `mean.index`

These should help prevent a regression against d9ed74ba0f7e6c1ac5aa2ea1d065e2b6ff536e3d
---
 tests/testthat/test-meanAdjAgree_index.mean.R | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 tests/testthat/test-meanAdjAgree_index.mean.R

diff --git a/tests/testthat/test-meanAdjAgree_index.mean.R b/tests/testthat/test-meanAdjAgree_index.mean.R
new file mode 100644
index 0000000..21e9f93
--- /dev/null
+++ b/tests/testthat/test-meanAdjAgree_index.mean.R
@@ -0,0 +1,27 @@
+test_that("mean.index always returns a numeric vector of length(index.variables)", {
+  list.res <- rlist::list.flatten(
+    list(
+      list(`1` = c(0.0, 0.5, 0.5, 0.5)),
+      list(`1` = c(0.0, 0.6, 0.7, 0.8)),
+      list(`5` = c(0.4, 0.0, 0.4, 0.4)),
+      list(`100` = c(0.9, 0.3, 0.0, 0.9)),
+      list(`10000` = c(0.7, 0.8, 0.9, 0.0)),
+      list(`5` = c(0.3, 0.0, 0.5, 0.6), `100` = c(0.7, 0.6, 0.0, 0.9))
+    )
+  )
+  index.variables = c(1, 5, 100, 10000)
+
+  # 1 would not go out of bounds, thus should always pass
+  testthat::expect_length(mean.index(1, list.res, index.variables), length(index.variables))
+  testthat::expect_length(mean.index(2, list.res, index.variables), length(index.variables))
+  testthat::expect_length(mean.index(3, list.res, index.variables), length(index.variables))
+  testthat::expect_length(mean.index(4, list.res, index.variables), length(index.variables))
+  # 5 is out of bounds of index.variables, returning only NA of correct length
+  testthat::expect_length(mean.index(5, list.res, index.variables), length(index.variables))
+})
+
+test_that("mean.index returns NA vector when i is out of bounds of index.variables", {
+  list.res <- list()
+  index.variables = 1:4
+  testthat::expect_equal(mean.index(5, list.res, index.variables), rep(NA, length(index.variables)))
+})
-- 
GitLab