Skip to content

Commit 8e653c9

Browse files
committed
Add gctorture arg to Rcpp:::test, so we can run tests wrapped in gctorture(TRUE)
1 parent 30bd875 commit 8e653c9

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* inst/include/Rcpp/proxy/FieldProxy.h: bandaid for protection issue
44
in operator= of FieldProxy
5+
* R/unit.tests.R: modify Rcpp:::test so we can run tests with
6+
gctorture(TRUE)
57

68
2014-03-03 Kevin Ushey <[email protected]>
79

R/unit.tests.R

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2010 - 2014 Dirk Eddelbuettel and Romain Francois
1+
# Copyright (C) 2010 - 2014 Dirk Eddelbuettel, Romain Francois and Kevin Ushey
22
#
33
# This file is part of Rcpp.
44
#
@@ -14,11 +14,61 @@
1414
#
1515
# You should have received a copy of the GNU General Public License
1616
# 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") {
1921
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+
2070
testSuite <- defineTestSuite(name="Rcpp Unit Tests",
21-
dirs=system.file("unitTests", package = "Rcpp"),
71+
dirs=testDir,
2272
testFuncRegexp = "^[Tt]est.+")
2373

2474
## if someoone calls Rcpp::test(), he/she wants all tests
@@ -48,3 +98,42 @@ unitTestSetup <- function(file, packages=NULL,
4898
}
4999
}
50100

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

Comments
 (0)