Skip to content

Commit 3bcb0a6

Browse files
committed
port runit tests to a minimal in-package test harness
Replace the RUnit-based test suite with plain 'tests/test-*.R' scripts and a small helper set shipped in the package (R/test.R): - test_init() does the shared setup (clears R_TESTS, seeds the RNG, picks a default backend) and summons the RcppParallel namespace into the calling script so internal functions are referenceable without the ':::' prefix - summon() copies the namespace into an environment, modelled on renv:::summon() - assert() is a single-condition check that also understands the all.equal() idiom, surfacing the reported difference on failure Port the five inst/tests/runit.*.R files to tests/test-*.R using these helpers and base-R assertions, and rename the existing plain-R tests to the same 'test-*.R' convention. Remove tests/doRUnit.R, the old RUnit tests, and the RUnit dependency from DESCRIPTION.
1 parent 77684de commit 3bcb0a6

15 files changed

Lines changed: 124 additions & 121 deletions

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Description: High level functions for parallel programming with 'Rcpp'.
2626
Depends: R (>= 3.6.0)
2727
Suggests:
2828
Rcpp,
29-
RUnit,
3029
knitr,
3130
rmarkdown
3231
Roxygen: list(markdown = TRUE)

R/test.R

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Minimal test helpers used by the package's own test scripts in tests/.
2+
#
3+
# The test files (tests/test-*.R) are plain R scripts run by 'R CMD check'.
4+
# Each one begins with a call to test_init(), which performs the shared setup
5+
# those scripts rely on: choosing a default parallel backend, seeding the RNG
6+
# for reproducibility, and working around environment quirks that would
7+
# otherwise break R sub-processes spawned during testing.
8+
9+
# Copy every object in the RcppParallel namespace -- including internal,
10+
# non-exported functions and constants -- into 'envir', so test scripts can
11+
# reference them directly without the 'RcppParallel:::' prefix. Modelled on
12+
# renv:::summon(). test_init() calls this for the test script's environment.
13+
summon <- function(envir = parent.frame()) {
14+
namespace <- asNamespace("RcppParallel")
15+
contents <- as.list.environment(namespace, all.names = TRUE)
16+
list2env(contents, envir = envir)
17+
invisible(envir)
18+
}
19+
20+
# Assert that 'cond' is true. Like a single-condition stopifnot(), but it also
21+
# understands the all.equal() idiom: all.equal() returns TRUE when its
22+
# arguments match and a character description of the differences when they do
23+
# not, so a character result is reported as a failure. A logical 'cond' passes
24+
# only if every element is TRUE (NAs count as failures), which makes it safe to
25+
# pass a logical vector directly, e.g. assert(x > 0).
26+
assert <- function(cond) {
27+
28+
label <- paste(deparse(substitute(cond)), collapse = " ")
29+
30+
# a character result (e.g. from all.equal()) describes a mismatch
31+
if (is.character(cond))
32+
stop(sprintf("assertion failed: %s\n%s", label, paste(cond, collapse = "\n")),
33+
call. = FALSE)
34+
35+
if (!is.logical(cond) || anyNA(cond) || !all(cond))
36+
stop(sprintf("assertion failed: %s", label), call. = FALSE)
37+
38+
invisible(TRUE)
39+
40+
}
41+
42+
test_init <- function() {
43+
44+
# R CMD check sets R_TESTS=startup.Rs, which breaks R sub-processes run
45+
# with a different working directory (e.g. the Rscript invocations in a
46+
# test package's Makevars). Clearing it avoids those failures.
47+
# (based on practice in Rcpp)
48+
Sys.setenv(R_TESTS = "")
49+
50+
# make tests that use random data deterministic
51+
set.seed(42L)
52+
53+
# choose a default backend unless one was requested explicitly; tinythread
54+
# is always available, so tests can run even where TBB is not enabled
55+
backend <- Sys.getenv("RCPP_PARALLEL_BACKEND", unset = NA)
56+
if (is.na(backend))
57+
Sys.setenv(RCPP_PARALLEL_BACKEND = "tinythread")
58+
59+
writeLines(paste("Using backend:", Sys.getenv("RCPP_PARALLEL_BACKEND")))
60+
61+
# ensure the packages used by the C++ tests are available
62+
requireNamespace("Rcpp", quietly = TRUE)
63+
requireNamespace("RcppParallel", quietly = TRUE)
64+
65+
# make all RcppParallel functions (including internal ones) directly
66+
# referenceable in the calling test script, without the ':::' prefix
67+
summon(parent.frame())
68+
69+
invisible(TRUE)
70+
71+
}

inst/tests/runit.distance.R

Lines changed: 0 additions & 18 deletions
This file was deleted.

inst/tests/runit.innerproduct.R

Lines changed: 0 additions & 17 deletions
This file was deleted.

inst/tests/runit.sum.R

Lines changed: 0 additions & 15 deletions
This file was deleted.

inst/tests/runit.transform.R

Lines changed: 0 additions & 15 deletions
This file was deleted.

inst/tests/runit.truefalse_macros.R

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/doRUnit.R

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/test-distance.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
RcppParallel:::test_init()
2+
3+
Rcpp::sourceCpp(system.file("tests/cpp/distance.cpp", package = "RcppParallel"))
4+
5+
n <- 1000
6+
m <- matrix(runif(n * 10), ncol = 10)
7+
m <- m / rowSums(m)
8+
9+
assert(all.equal(
10+
rcpp_js_distance(m),
11+
rcpp_parallel_js_distance(m)
12+
))

tests/test-innerproduct.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
RcppParallel:::test_init()
2+
3+
Rcpp::sourceCpp(system.file("tests/cpp/innerproduct.cpp", package = "RcppParallel"))
4+
5+
x <- runif(1000000)
6+
y <- runif(1000000)
7+
8+
assert(all.equal(
9+
innerProduct(x, y),
10+
parallelInnerProduct(x, y)
11+
))

0 commit comments

Comments
 (0)