|
| 1 | +# Copyright (C) 2013 - 2014 Dirk Eddelbuettel and Romain Francois |
| 2 | +# |
| 3 | +# This file is part of Rcpp. |
| 4 | +# |
| 5 | +# Rcpp is free software: you can redistribute it and/or modify it |
| 6 | +# under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 2 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# Rcpp is distributed in the hope that it will be useful, but |
| 11 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes" |
| 19 | + |
| 20 | +if (.runThisTest) { |
| 21 | + |
| 22 | + test.exposeClass <- function(){ |
| 23 | + |
| 24 | + tempdir <- tempdir() |
| 25 | + ## foo has already been loaded in test.Rcpp.package.skeleton.R, |
| 26 | + ## so named differently here to avoid namespace conflicts |
| 27 | + pkg_name <- "fooModule" |
| 28 | + path <- tempdir |
| 29 | + pkg_path <- file.path(path, pkg_name) |
| 30 | + R_path <- file.path(pkg_path, "R") |
| 31 | + src_path <- file.path(pkg_path, "src") |
| 32 | + foo_header <- " |
| 33 | +#ifndef foo_h |
| 34 | +#define foo_h 1 |
| 35 | +// class to set/get double |
| 36 | +class foo { |
| 37 | + private: |
| 38 | + double x; |
| 39 | + public: |
| 40 | + double get_x() {return x;} |
| 41 | + void set_x(double _x) {x = _x; return;} |
| 42 | + foo(double _x) {set_x(_x);} |
| 43 | +}; |
| 44 | +#endif |
| 45 | +" |
| 46 | + |
| 47 | + ## create package |
| 48 | + Rcpp.package.skeleton(pkg_name, path=path, environment = environment(), |
| 49 | + example_code = FALSE, module = TRUE) |
| 50 | + on.exit(unlink(pkg_path, recursive=TRUE)) |
| 51 | + file.remove(list.files(c(src_path, R_path), full.names = TRUE)) |
| 52 | + cat(foo_header, file = file.path(src_path, "foo.h")) |
| 53 | + |
| 54 | + ## check that result of exposeClass compiles and runs properly |
| 55 | + exposeClass(class = "fooR", |
| 56 | + constructors = list("double"), |
| 57 | + fields = character(), |
| 58 | + methods = c("get_x", "set_x"), |
| 59 | + header = '#include "foo.h"', |
| 60 | + CppClass = "foo", |
| 61 | + file = file.path(src_path, "fooModule.cpp"), |
| 62 | + Rfile = file.path(R_path, "fooClass.R")) |
| 63 | + compileAttributes(pkg_path) |
| 64 | + invisible(sapply( list.files( file.path(pkg_path, "man"), full.names=TRUE), unlink )) |
| 65 | + |
| 66 | + ## check build |
| 67 | + owd <- getwd() |
| 68 | + setwd(path) |
| 69 | + on.exit( setwd(owd), add=TRUE ) |
| 70 | + R <- shQuote( file.path( R.home( component = "bin" ), "R" )) |
| 71 | + system( paste(R, "CMD build", pkg_path) ) |
| 72 | + gz_name <- paste0(pkg_name, "_1.0.tar.gz") |
| 73 | + checkTrue( file.exists(gz_name), "can successfully R CMD build the pkg") |
| 74 | + |
| 75 | + ## check install + require |
| 76 | + dir.create("templib") |
| 77 | + install.packages(gz_name, file.path(path, "templib"), repos=NULL, type="source") |
| 78 | + on.exit( unlink( file.path(path, gz_name) ), add=TRUE) |
| 79 | + status <- require(pkg_name, file.path(path, "templib"), character.only=TRUE) |
| 80 | + on.exit( unlink( file.path(path, "templib"), recursive=TRUE), add=TRUE ) |
| 81 | + checkTrue(status, "can successfully require the pkg") |
| 82 | + |
| 83 | + ## check object creation |
| 84 | + bar <- fooR(0) |
| 85 | + env <- environment() |
| 86 | + checkTrue( exists("bar", envir = env, inherits = FALSE), |
| 87 | + "module object successfully instantiated" ) |
| 88 | + gs <- replicate(n = 10, { |
| 89 | + x <- rnorm(1) |
| 90 | + bar$set_x(x) |
| 91 | + bar$get_x() - x |
| 92 | + }) |
| 93 | + checkTrue( all(gs == 0), "object methods function as expected" ) |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments