mirror of
https://github.com/msberends/AMR.git
synced 2026-05-31 13:01:42 +02:00
unit tests
This commit is contained in:
@@ -441,6 +441,7 @@ test_that("test-sir.R", {
|
|||||||
# Tests must pass even when only 1 core is available; parallel = TRUE then
|
# Tests must pass even when only 1 core is available; parallel = TRUE then
|
||||||
# silently falls back to sequential, but results must still be identical.
|
# silently falls back to sequential, but results must still be identical.
|
||||||
|
|
||||||
|
if (AMR:::pkg_is_available("future.apply")) {
|
||||||
set.seed(42)
|
set.seed(42)
|
||||||
n_par <- 200
|
n_par <- 200
|
||||||
df_par <- data.frame(
|
df_par <- data.frame(
|
||||||
@@ -457,6 +458,9 @@ test_that("test-sir.R", {
|
|||||||
sir_seq <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE))
|
sir_seq <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE))
|
||||||
log_seq <- sir_interpretation_history(clean = TRUE)
|
log_seq <- sir_interpretation_history(clean = TRUE)
|
||||||
|
|
||||||
|
future::plan(future::multicore)
|
||||||
|
n_max_workers <- future::nbrOfWorkers()
|
||||||
|
|
||||||
sir_par <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE))
|
sir_par <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE))
|
||||||
log_par <- sir_interpretation_history(clean = TRUE)
|
log_par <- sir_interpretation_history(clean = TRUE)
|
||||||
|
|
||||||
@@ -473,8 +477,10 @@ test_that("test-sir.R", {
|
|||||||
# run sequential once to populate the history, then run parallel and
|
# run sequential once to populate the history, then run parallel and
|
||||||
# verify the new parallel run adds exactly as many rows as sequential
|
# verify the new parallel run adds exactly as many rows as sequential
|
||||||
sir_interpretation_history(clean = TRUE)
|
sir_interpretation_history(clean = TRUE)
|
||||||
|
future::plan(future::sequential)
|
||||||
suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE)) # populate history
|
suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE)) # populate history
|
||||||
pre_n <- nrow(sir_interpretation_history())
|
pre_n <- nrow(sir_interpretation_history())
|
||||||
|
future::plan(future::multicore)
|
||||||
suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE))
|
suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE))
|
||||||
post_n <- nrow(sir_interpretation_history())
|
post_n <- nrow(sir_interpretation_history())
|
||||||
expect_equal(post_n - pre_n, nrow(log_seq)) # exactly one run's worth of new rows
|
expect_equal(post_n - pre_n, nrow(log_seq)) # exactly one run's worth of new rows
|
||||||
@@ -486,23 +492,30 @@ test_that("test-sir.R", {
|
|||||||
expect_identical(sir_par[["GEN"]], sir_par2[["GEN"]])
|
expect_identical(sir_par[["GEN"]], sir_par2[["GEN"]])
|
||||||
|
|
||||||
# 5. max_cores = 1 gives same results as default sequential
|
# 5. max_cores = 1 gives same results as default sequential
|
||||||
sir_mc1 <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE, max_cores = 1L))
|
future::plan(future::multicore, workers = 1)
|
||||||
|
sir_mc1 <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE))
|
||||||
expect_identical(sir_seq[["AMC"]], sir_mc1[["AMC"]])
|
expect_identical(sir_seq[["AMC"]], sir_mc1[["AMC"]])
|
||||||
expect_identical(sir_seq[["GEN"]], sir_mc1[["GEN"]])
|
expect_identical(sir_seq[["GEN"]], sir_mc1[["GEN"]])
|
||||||
|
|
||||||
# 6. max_cores = 2 and max_cores = 3 give same results as sequential
|
# 6. max_cores = 2 and max_cores = 3 give same results as sequential
|
||||||
sir_mc2 <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE, max_cores = 2L))
|
if (n_max_workers >= 3) {
|
||||||
sir_mc3 <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE, max_cores = 3L))
|
future::plan(future::multicore, workers = 2)
|
||||||
|
sir_mc2 <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE))
|
||||||
|
future::plan(future::multicore, workers = 3)
|
||||||
|
sir_mc3 <- suppressMessages(as.sir(df_par, col_mo = "mo", info = FALSE, parallel = TRUE))
|
||||||
expect_identical(sir_seq[["AMC"]], sir_mc2[["AMC"]])
|
expect_identical(sir_seq[["AMC"]], sir_mc2[["AMC"]])
|
||||||
expect_identical(sir_seq[["GEN"]], sir_mc3[["GEN"]])
|
expect_identical(sir_seq[["GEN"]], sir_mc3[["GEN"]])
|
||||||
|
}
|
||||||
|
|
||||||
# 7. single-column data frame falls back silently to sequential
|
# 7. single-column data frame falls back silently to sequential
|
||||||
df_single <- df_par[, c("mo", "AMC")]
|
df_single <- df_par[, c("mo", "AMC")]
|
||||||
|
future::plan(future::sequential)
|
||||||
sir_single_seq <- suppressMessages(as.sir(df_single, col_mo = "mo", info = FALSE))
|
sir_single_seq <- suppressMessages(as.sir(df_single, col_mo = "mo", info = FALSE))
|
||||||
|
future::plan(future::multicore)
|
||||||
sir_single_par <- suppressMessages(as.sir(df_single, col_mo = "mo", info = FALSE, parallel = TRUE))
|
sir_single_par <- suppressMessages(as.sir(df_single, col_mo = "mo", info = FALSE, parallel = TRUE))
|
||||||
expect_identical(sir_single_seq[["AMC"]], sir_single_par[["AMC"]])
|
expect_identical(sir_single_seq[["AMC"]], sir_single_par[["AMC"]])
|
||||||
|
|
||||||
# 9. row-batch mode (n_cols < n_cores): force row splitting via max_cores and
|
# 8. row-batch mode (n_cols < n_cores): force row splitting via max_cores and
|
||||||
# verify identical output to sequential for a dataset with 2 AB columns so
|
# verify identical output to sequential for a dataset with 2 AB columns so
|
||||||
# pieces_per_col = ceiling(max_cores / 2) >= 2 and row batching activates
|
# pieces_per_col = ceiling(max_cores / 2) >= 2 and row batching activates
|
||||||
df_wide <- data.frame(
|
df_wide <- data.frame(
|
||||||
@@ -511,7 +524,9 @@ test_that("test-sir.R", {
|
|||||||
GEN = as.mic(sample(c("1", "2", "4", "8"), n_par, TRUE)),
|
GEN = as.mic(sample(c("1", "2", "4", "8"), n_par, TRUE)),
|
||||||
stringsAsFactors = FALSE
|
stringsAsFactors = FALSE
|
||||||
)
|
)
|
||||||
|
future::plan(future::sequential)
|
||||||
sir_wide_seq <- suppressMessages(as.sir(df_wide, col_mo = "mo", info = FALSE))
|
sir_wide_seq <- suppressMessages(as.sir(df_wide, col_mo = "mo", info = FALSE))
|
||||||
|
future::plan(future::multicore)
|
||||||
sir_wide_par <- suppressMessages(as.sir(df_wide,
|
sir_wide_par <- suppressMessages(as.sir(df_wide,
|
||||||
col_mo = "mo", info = FALSE,
|
col_mo = "mo", info = FALSE,
|
||||||
parallel = TRUE, max_cores = 8L
|
parallel = TRUE, max_cores = 8L
|
||||||
@@ -530,6 +545,8 @@ test_that("test-sir.R", {
|
|||||||
n_mentions <- sum(grepl(ab_nm, msgs, fixed = TRUE))
|
n_mentions <- sum(grepl(ab_nm, msgs, fixed = TRUE))
|
||||||
expect_lte(n_mentions, 1L)
|
expect_lte(n_mentions, 1L)
|
||||||
}
|
}
|
||||||
|
future::plan(future::sequential)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
# issue #239 — custom reference_data support
|
# issue #239 — custom reference_data support
|
||||||
|
|||||||
Reference in New Issue
Block a user