Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Description: High level functions for parallel programming with 'Rcpp'.
Depends: R (>= 3.6.0)
Suggests:
Rcpp,
RUnit,
knitr,
rmarkdown
Roxygen: list(markdown = TRUE)
Expand Down
71 changes: 71 additions & 0 deletions R/test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Minimal test helpers used by the package's own test scripts in tests/.
#
# The test files (tests/test-*.R) are plain R scripts run by 'R CMD check'.
# Each one begins with a call to test_init(), which performs the shared setup
# those scripts rely on: choosing a default parallel backend, seeding the RNG
# for reproducibility, and working around environment quirks that would
# otherwise break R sub-processes spawned during testing.

# Copy every object in the RcppParallel namespace -- including internal,
# non-exported functions and constants -- into 'envir', so test scripts can
# reference them directly without the 'RcppParallel:::' prefix. Modelled on
# renv:::summon(). test_init() calls this for the test script's environment.
summon <- function(envir = parent.frame()) {
namespace <- asNamespace("RcppParallel")
contents <- as.list.environment(namespace, all.names = TRUE)
list2env(contents, envir = envir)
invisible(envir)
}

# Assert that 'cond' is true. Like a single-condition stopifnot(), but it also
# understands the all.equal() idiom: all.equal() returns TRUE when its
# arguments match and a character description of the differences when they do
# not, so a character result is reported as a failure. A logical 'cond' passes
# only if every element is TRUE (NAs count as failures), which makes it safe to
# pass a logical vector directly, e.g. assert(x > 0).
assert <- function(cond) {

label <- paste(deparse(substitute(cond)), collapse = " ")

# a character result (e.g. from all.equal()) describes a mismatch
if (is.character(cond))
stop(sprintf("assertion failed: %s\n%s", label, paste(cond, collapse = "\n")),
call. = FALSE)

if (!is.logical(cond) || anyNA(cond) || !all(cond))
stop(sprintf("assertion failed: %s", label), call. = FALSE)

invisible(TRUE)

}

test_init <- function() {

# R CMD check sets R_TESTS=startup.Rs, which breaks R sub-processes run
# with a different working directory (e.g. the Rscript invocations in a
# test package's Makevars). Clearing it avoids those failures.
# (based on practice in Rcpp)
Sys.setenv(R_TESTS = "")

# make tests that use random data deterministic
set.seed(42L)

# choose a default backend unless one was requested explicitly; tinythread
# is always available, so tests can run even where TBB is not enabled
backend <- Sys.getenv("RCPP_PARALLEL_BACKEND", unset = NA)
if (is.na(backend))
Sys.setenv(RCPP_PARALLEL_BACKEND = "tinythread")

writeLines(paste("Using backend:", Sys.getenv("RCPP_PARALLEL_BACKEND")))

# ensure the packages used by the C++ tests are available
requireNamespace("Rcpp", quietly = TRUE)
requireNamespace("RcppParallel", quietly = TRUE)

# make all RcppParallel functions (including internal ones) directly
# referenceable in the calling test script, without the ':::' prefix
summon(parent.frame())

invisible(TRUE)

}
18 changes: 0 additions & 18 deletions inst/tests/runit.distance.R

This file was deleted.

17 changes: 0 additions & 17 deletions inst/tests/runit.innerproduct.R

This file was deleted.

15 changes: 0 additions & 15 deletions inst/tests/runit.sum.R

This file was deleted.

15 changes: 0 additions & 15 deletions inst/tests/runit.transform.R

This file was deleted.

6 changes: 0 additions & 6 deletions inst/tests/runit.truefalse_macros.R

This file was deleted.

41 changes: 0 additions & 41 deletions tests/doRUnit.R

This file was deleted.

12 changes: 12 additions & 0 deletions tests/test-distance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
RcppParallel:::test_init()

Rcpp::sourceCpp(system.file("tests/cpp/distance.cpp", package = "RcppParallel"))

n <- 1000
m <- matrix(runif(n * 10), ncol = 10)
m <- m / rowSums(m)

assert(all.equal(
rcpp_js_distance(m),
rcpp_parallel_js_distance(m)
))
11 changes: 11 additions & 0 deletions tests/test-innerproduct.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RcppParallel:::test_init()

Rcpp::sourceCpp(system.file("tests/cpp/innerproduct.cpp", package = "RcppParallel"))

x <- runif(1000000)
y <- runif(1000000)

assert(all.equal(
innerProduct(x, y),
parallelInnerProduct(x, y)
))
2 changes: 2 additions & 0 deletions tests/testInstallLibs.R → tests/test-install-libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# helper definitions that precede its '# Main' section (evaluating the whole
# file would trigger an actual install), and exercise them directly.

RcppParallel:::test_init()

# locate src/install.libs.R by walking up from the working directory; this is
# reachable when tests are run from the package sources. If it cannot be found
# (e.g. only the installed package is available), skip rather than fail.
Expand Down
11 changes: 3 additions & 8 deletions tests/scalable-allocator.R → tests/test-scalable-allocator.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# https://github.com/RcppCore/RcppParallel/pull/262

library(RcppParallel)
RcppParallel:::test_init()

# building the test package requires a toolchain, so keep this test
# off of CRAN's machines
Expand All @@ -20,7 +20,7 @@ if (!ci) {
}

# the scalable allocator is only available with the TBB backend
if (!RcppParallel:::TBB_ENABLED) {
if (!TBB_ENABLED) {
writeLines("TBB is not enabled; skipping scalable allocator test.")
quit(save = "no")
}
Expand Down Expand Up @@ -89,11 +89,6 @@ libDir <- file.path(tempdir(), "library")
dir.create(libDir, recursive = TRUE, showWarnings = FALSE)
Sys.setenv(R_LIBS = paste(.libPaths(), collapse = .Platform$path.sep))

# R CMD check sets R_TESTS=startup.Rs, which breaks R sub-processes run
# with a different working directory -- like the Rscript invocations in
# the test package's Makevars (see also tests/doRUnit.R)
Sys.setenv(R_TESTS = "")

rExe <- file.path(R.home("bin"), if (.Platform$OS.type == "windows") "R.exe" else "R")
args <- c("CMD", "INSTALL", "--no-multiarch", paste0("--library=", shQuote(libDir)), shQuote(pkgRoot))

Expand All @@ -108,4 +103,4 @@ if (is.numeric(status) && status != 0L)
invisible(loadNamespace("scalabletest", lib.loc = libDir))
ok <- .Call("scalable_roundtrip", 1000L, PACKAGE = "scalabletest")
writeLines(paste("scalable allocator round trip:", if (isTRUE(ok)) "OK" else "FAILED"))
stopifnot(isTRUE(ok))
assert(isTRUE(ok))
10 changes: 10 additions & 0 deletions tests/test-sum.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RcppParallel:::test_init()

Rcpp::sourceCpp(system.file("tests/cpp/sum.cpp", package = "RcppParallel"))

v <- as.numeric(1:10000000)

assert(all.equal(
vectorSum(v),
parallelVectorSum(v)
))
10 changes: 10 additions & 0 deletions tests/test-transform.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RcppParallel:::test_init()

Rcpp::sourceCpp(system.file("tests/cpp/transform.cpp", package = "RcppParallel"))

m <- matrix(as.numeric(1:1000000), nrow = 1000, ncol = 1000)

assert(all.equal(
matrixSqrt(m),
parallelMatrixSqrt(m)
))
5 changes: 5 additions & 0 deletions tests/test-truefalse-macros.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RcppParallel:::test_init()

# this file has no runtime assertions: the TRUE/FALSE macro checks are
# static_assert()s in the C++ source, so a successful compile is the test
Rcpp::sourceCpp(system.file("tests/cpp/truefalse_macros.cpp", package = "RcppParallel"))
Loading