[Rcpp-devel] Trouble with package unit test and sourceCpp()

Dirk Eddelbuettel edd at debian.org
Wed Sep 3 12:23:23 CEST 2014


On 2 September 2014 at 20:42, Christian Gunning wrote:
| I'm working on a package that includes a unit test that calls
| sourceCpp() to test a particular use-case. 
[...]
| My question is whether there's a sane workaround, or if I should give
| up on these tests. Alternately, is there a cleaner way for a user to
| supply hand-written C++ functions?  Using sourceCpp() allows the user
| to locally manage custom functions without building a package, and
| ensures that user edits aren't overwritten by a package upgrade.

We do that in Rcpp itself, as well as in RcppArmadillo, RcppEigen, ...
by using RUnit.  You should be able to use any of the other fine unit test
packages too.

Part of the logic is farmed out to a little helper function.  From the last
one I worked on / added a few weeks ago for RcppArmadillo, ie
rcpparmadillo/inst/unitTests/runit.sparse.R 



.runThisTest <- suppressMessages(require(Matrix))

if (.runThisTest) {

    .setUp <- RcppArmadillo:::unit_test_setup("sparse.cpp") 

    #
    # tests follow below
    #
}


and unit_test_setup() mostly deals with trying to get the path right:



unit_test_setup <- function(file = NULL, packages = NULL) {
    function(){
        if (!is.null(packages)) {
            for (p in packages) {
                suppressMessages(require(p, character.only = TRUE))
            }
        }
        if (!is.null(file)) {
            if (exists("pathRcppArmadilloTests")) {
                sourceCpp(file.path(get("pathRcppArmadilloTests"), "cpp", file))
            } else if (file.exists(file.path("cpp", file))) {
                sourceCpp(file.path( "cpp", file))
            } else {
                sourceCpp(system.file("unitTests", "cpp", file, package="RcppArmadillo"))
            }
        }
    }
}


The cpp files themselves are then in cpp/ which is inside
inst/unitTests/cpp. 

Dirk


-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list