|
1 |
| -# Copyright (C) 2010 - 2014 Dirk Eddelbuettel and Romain Francois |
| 1 | +# Copyright (C) 2010 - 2014 Dirk Eddelbuettel, Romain Francois and Kevin Ushey |
2 | 2 | #
|
3 | 3 | # This file is part of Rcpp.
|
4 | 4 | #
|
|
14 | 14 | #
|
15 | 15 | # You should have received a copy of the GNU General Public License
|
16 | 16 | # along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
|
17 |
| - |
18 |
| -test <- function(output=if(file.exists("/tmp")) "/tmp" else getwd()) { |
| 17 | +test <- function( |
| 18 | + output=if(file.exists("/tmp")) "/tmp" else getwd(), |
| 19 | + gctorture=FALSE, |
| 20 | + gctorture.exclude="runit.Module.client.package.R") { |
19 | 21 | if (require(RUnit)) {
|
| 22 | + |
| 23 | + if (gctorture) { |
| 24 | + |
| 25 | + message("Running tests with gctorture(TRUE)") |
| 26 | + if (length(gctorture.exclude)) { |
| 27 | + message("The following tests will be excluded:\n", |
| 28 | + paste(">>", gctorture.exclude, collapse="\n") |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + unitTestsDir <- system.file("unitTests", package="Rcpp") |
| 33 | + files <- list.files( |
| 34 | + unitTestsDir, |
| 35 | + recursive=TRUE |
| 36 | + ) |
| 37 | + files <- setdiff(files, gctorture.exclude) |
| 38 | + dirs <- list.dirs(unitTestsDir, full.names=FALSE) |
| 39 | + testDir <- file.path( tempdir(), "RcppTests" ) |
| 40 | + for (dir in file.path(testDir, dirs)) dir.create(dir) |
| 41 | + |
| 42 | + copySuccess <- sapply(files, function(file) { |
| 43 | + file.copy( |
| 44 | + file.path(unitTestsDir, file), |
| 45 | + file.path(testDir, file) |
| 46 | + ) |
| 47 | + }) |
| 48 | + |
| 49 | + if (!all(copySuccess)) { |
| 50 | + stop("Could not copy test files to temporary directory") |
| 51 | + } |
| 52 | + |
| 53 | + ## Modify all the test files that were copied |
| 54 | + testFiles <- list.files(testDir, pattern="^runit", full.names=TRUE) |
| 55 | + for (file in testFiles) gctortureRUnitTest(file) |
| 56 | + |
| 57 | + ## Ensure we can read and parse each file |
| 58 | + for (file in testFiles) { |
| 59 | + tryCatch( parse(text=readLines(file)), |
| 60 | + error=function(e) { |
| 61 | + "could not parse test file" |
| 62 | + } |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + } else { |
| 67 | + testDir <- system.file("unitTests", package = "Rcpp") |
| 68 | + } |
| 69 | + |
20 | 70 | testSuite <- defineTestSuite(name="Rcpp Unit Tests",
|
21 |
| - dirs=system.file("unitTests", package = "Rcpp"), |
| 71 | + dirs=testDir, |
22 | 72 | testFuncRegexp = "^[Tt]est.+")
|
23 | 73 |
|
24 | 74 | ## if someoone calls Rcpp::test(), he/she wants all tests
|
@@ -48,3 +98,42 @@ unitTestSetup <- function(file, packages=NULL,
|
48 | 98 | }
|
49 | 99 | }
|
50 | 100 |
|
| 101 | +gctortureRUnitTest <- function(file) { |
| 102 | + |
| 103 | + test <- readLines(file) |
| 104 | + |
| 105 | + ## TODO: handle '{', '}' within quotes |
| 106 | + findMatchingBrace <- function(test, start, balance=1) { |
| 107 | + line <- test[start] |
| 108 | + if (start > length(test)) { |
| 109 | + stop("error") |
| 110 | + } |
| 111 | + if (balance > 0) { |
| 112 | + balance <- balance + |
| 113 | + sum(gregexpr("{", line, fixed=TRUE)[[1]] > 0) - |
| 114 | + sum(gregexpr("}", line, fixed=TRUE)[[1]] > 0) |
| 115 | + return( findMatchingBrace(test, start+1, balance) ) |
| 116 | + } |
| 117 | + return(start - 1) |
| 118 | + } |
| 119 | + |
| 120 | + ## Find the lines defining unit tests |
| 121 | + testStarts <- grep("^[[:space:]]*[Tt]est\\.+", test) |
| 122 | + |
| 123 | + ## Get the line with the closing brace |
| 124 | + testEnds <- sapply(testStarts, function(ind) { |
| 125 | + findMatchingBrace(test, ind + 1) |
| 126 | + }) |
| 127 | + stopifnot( length(testStarts) == length(testEnds) ) |
| 128 | + |
| 129 | + ## Modify the function definition by wrapping it in gctorture |
| 130 | + for (i in seq_along(testStarts)) { |
| 131 | + start <- testStarts[i] |
| 132 | + end <- testEnds[i] |
| 133 | + test[start] <- paste( test[start], "gctorture(TRUE);" ) |
| 134 | + test[end] <- paste("gctorture(FALSE); }") |
| 135 | + } |
| 136 | + |
| 137 | + cat(test, file=file, sep="\n") |
| 138 | + |
| 139 | +} |
0 commit comments