Skip to content

Commit b670719

Browse files
committed
Added unit tests.
1 parent adafeb1 commit b670719

8 files changed

Lines changed: 271 additions & 4 deletions

File tree

DESCRIPTION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ Version: 0.1.1
55
Author: Daniel C. Dillon
66
Maintainer: Daniel C. Dillon <dcdillon@gmail.com>
77
URL: https://github.com/dcdillon/RcppHoney
8+
BugReports: https://github.com/dcdillon/RcppHoney/issues
89
Description: Creates an easy way to use expression templates with R
910
semantics on any iterator based structure.
1011
License: GPL (>= 2)
1112
LazyData: TRUE
1213
Imports:
1314
Rcpp (>= 0.12.5)
15+
Suggests:
16+
RUnit
1417
LinkingTo:
1518
Rcpp

NAMESPACE

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
exportPattern("^[[:alpha:]]+")
2-
importFrom(Rcpp, evalCpp)
31
useDynLib(RcppHoney)
2+
3+
import(Rcpp)
4+
5+
export(example_manually_hooked)

R/unit.tests.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (C) 2010 - 2015 Dirk Eddelbuettel, Romain Francois and Douglas Bates
2+
#
3+
# This file is part of RcppHoney.
4+
#
5+
# RcppHoney 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+
# RcppHoney 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 RcppHoney. If not, see <http://www.gnu.org/licenses/>.
17+
18+
unit_test_setup <- function(file = NULL, packages = NULL) {
19+
function(){
20+
if (!is.null(packages)) {
21+
for (p in packages) {
22+
suppressMessages(requireNamespace(p))
23+
}
24+
}
25+
if (!is.null(file)) {
26+
if (exists("pathRcppHoneyTests")) {
27+
sourceCpp(file.path(get("pathRcppHoneyTests"), "cpp", file))
28+
} else if (file.exists(file.path("cpp", file))) {
29+
sourceCpp(file.path( "cpp", file))
30+
} else {
31+
sourceCpp(system.file("unitTests", "cpp", file, package="RcppHoney"))
32+
}
33+
}
34+
}
35+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// [[Rcpp::depends(RcppHoney)]]
2+
13
#include <RcppHoney.hpp>
24

35
// [[Rcpp::export]]
@@ -46,7 +48,7 @@ Rcpp::NumericVector test_hooked_plus_operand(Rcpp::NumericVector v1, std::vector
4648
}
4749

4850
// [[Rcpp::export]]
49-
Rcpp::NumericVector test_operand_plus_operand(double s, std::vector< int > v1, std::vector< int > v2) {
51+
Rcpp::NumericVector test_operand_plus_operand(std::vector< int > v1, std::vector< int > v2) {
5052
return Rcpp::wrap((v1 + v2) + (v1 + v2));
5153
}
5254

inst/unitTests/runTests.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require(Rcpp)
2+
pkg <- "RcppHoney"
3+
4+
if (require("RUnit", quietly = TRUE)) {
5+
6+
library(package=pkg, character.only = TRUE)
7+
if (!(exists("path") && file.exists(path)))
8+
path <- system.file("unitTests", package = pkg)
9+
10+
## --- Testing ---
11+
12+
## Define tests
13+
testSuite <- defineTestSuite(name=paste(pkg, "unit testing"), dirs = path)
14+
15+
if (interactive()) {
16+
cat("Now have RUnit Test Suite 'testSuite' for package '", pkg, "' :\n", sep='')
17+
str(testSuite)
18+
cat('', "Consider doing",
19+
"\t tests <- runTestSuite(testSuite)", "\nand later",
20+
"\t printTextProtocol(tests)", '', sep="\n")
21+
} else { ## run from shell / Rscript / R CMD Batch / ...
22+
## Run
23+
tests <- runTestSuite(testSuite)
24+
25+
if (file.access(path, 02) != 0) {
26+
## cannot write to path -> use writable one
27+
tdir <- tempfile(paste(pkg, "unitTests", sep="_"))
28+
dir.create(tdir)
29+
pathReport <- file.path(tdir, "report")
30+
cat("RUnit reports are written into ", tdir, "/report.(txt|html)", sep = "")
31+
} else {
32+
pathReport <- file.path(path, "report")
33+
}
34+
35+
## Print results
36+
## printTextProtocol(tests)
37+
printTextProtocol(tests, fileName=paste(pathReport, ".txt", sep=""))
38+
## Print HTML version to a file
39+
## printHTMLProtocol has problems on Mac OS X
40+
if (Sys.info()["sysname"] != "Darwin")
41+
printHTMLProtocol(tests, fileName=paste(pathReport, ".html", sep=""))
42+
43+
## stop() if there are any failures i.e. FALSE to unit test.
44+
## This will cause R CMD check to return error and stop
45+
if (getErrors(tests)$nFail > 0) {
46+
stop("one of the unit tests failed")
47+
}
48+
}
49+
} else {
50+
cat("R package 'RUnit' cannot be loaded -- no unit tests run\n", "for package", pkg,"\n")
51+
}

inst/unitTests/runit.operators.R

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/r -t
2+
#
3+
# Copyright (C) 2016 Daniel C. Dillon
4+
#
5+
# This file is part of RcppHoney.
6+
#
7+
# RcppHoney is free software: you can redistribute it and/or modify it
8+
# under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 2 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# RcppHoney is distributed in the hope that it will be useful, but
13+
# WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with RcppHoney. If not, see <http://www.gnu.org/licenses/>.
19+
20+
.setUp <- RcppHoney:::unit_test_setup("operators.cpp", "RcppHoney")
21+
22+
test.hooked.and.scalar <- function() {
23+
v <- 1:100
24+
s <- 15
25+
checkEquals(v + s, test_hooked_plus_scalar(v, s))
26+
checkEquals(s + v, test_scalar_plus_hooked(s, v))
27+
}
28+
29+
test.hooked.and.hooked <- function() {
30+
v <- 1:100
31+
checkEquals(v + v, test_hooked_plus_hooked(v, v))
32+
checkEquals(v + v, test_hooked_plus_other_hooked(v, v))
33+
checkEquals(v + v, test_other_hooked_plus_hooked(v, v))
34+
}
35+
36+
test.operand.and.scalar <- function() {
37+
v <- 1:100
38+
s <- 15
39+
checkEquals((v + v) + s, test_operand_plus_scalar(v, v, s))
40+
checkEquals(s + (v + v), test_scalar_plus_operand(s, v, v))
41+
}
42+
43+
test.operand.and.hooked <- function() {
44+
v <- 1:100
45+
checkEquals((v + v) + v, test_operand_plus_hooked(v, v, v))
46+
checkEquals(v + (v + v), test_hooked_plus_operand(v, v, v))
47+
}
48+
49+
test.operand.and.operand <- function() {
50+
v <- 1:100
51+
checkEquals((v + v) + (v + v), test_operand_plus_operand(v, v))
52+
}
53+
54+
test.unary.operator <- function() {
55+
v <- 1:100
56+
checkEquals(-v, test_unary_operator_hooked(v))
57+
checkEquals(-(v + v), test_unary_operator_operand(v))
58+
}
59+
60+
test.unary.function <- function() {
61+
v <- 1:100
62+
checkEquals(log(v), test_unary_function_hooked(v))
63+
checkEquals(log(v + v), test_unary_function_operand(v))
64+
}
65+
66+
67+
test.na.hooked.and.scalar <- function() {
68+
v <- 1:100
69+
v <- c(v, NA)
70+
s <- 15
71+
checkEquals(v + s, test_hooked_plus_scalar(v, s))
72+
checkEquals(s + v, test_scalar_plus_hooked(s, v))
73+
}
74+
75+
test.na.hooked.and.hooked <- function() {
76+
v <- 1:100
77+
v <- c(v, NA)
78+
checkEquals(v + v, test_hooked_plus_hooked(v, v))
79+
checkEquals(v + v, test_hooked_plus_other_hooked(v, v))
80+
checkEquals(v + v, test_other_hooked_plus_hooked(v, v))
81+
}
82+
83+
test.na.operand.and.scalar <- function() {
84+
v <- 1:100
85+
v <- c(v, NA)
86+
s <- 15
87+
checkEquals((v + v) + s, test_operand_plus_scalar(v, v, s))
88+
checkEquals(s + (v + v), test_scalar_plus_operand(s, v, v))
89+
}
90+
91+
test.na.operand.and.hooked <- function() {
92+
v <- 1:100
93+
v <- c(v, NA)
94+
checkEquals((v + v) + v, test_operand_plus_hooked(v, v, v))
95+
checkEquals(v + (v + v), test_hooked_plus_operand(v, v, v))
96+
}
97+
98+
test.na.operand.and.operand <- function() {
99+
v <- 1:100
100+
v <- c(v, NA)
101+
checkEquals((v + v) + (v + v), test_operand_plus_operand(v, v))
102+
}
103+
104+
test.na.unary.operator <- function() {
105+
v <- 1:100
106+
v <- c(v, NA)
107+
checkEquals(-v, test_unary_operator_hooked(v))
108+
checkEquals(-(v + v), test_unary_operator_operand(v))
109+
}
110+
111+
test.na.unary.function <- function() {
112+
v <- 1:100
113+
v <- c(v, NA)
114+
checkEquals(log(v), test_unary_function_hooked(v))
115+
checkEquals(log(v + v), test_unary_function_operand(v))
116+
}

src/examples.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Rcpp::NumericVector example_manually_hooked() {
4343
l.push_back(5);
4444

4545
// std::vector is already hooked in to RcppHoney in default_hooks.hpp so we'll
46-
// create one of those two
46+
// create one of those too
4747
std::vector< int > v;
4848
v.push_back(1);
4949
v.push_back(2);

tests/doRUnit.R

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (C) 2010 - 2013 Dirk Eddelbuettel, Romain Francois and Douglas Bates
2+
# Copyright (C) 2014 Dirk Eddelbuettel
3+
# Earlier copyrights Gregor Gorjanc, Martin Maechler and Murray Stokely as detailed below
4+
#
5+
# This file is part of RcppHoney.
6+
#
7+
# RcppHoney is free software: you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License as
9+
# published by the Free Software Foundation, either version 2 of the
10+
# License, or (at your option) any later version.
11+
#
12+
# RcppHoney is distributed in the hope that it will be useful, but
13+
# WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
# General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with RcppHoney. If not, see <http://www.gnu.org/licenses/>.
19+
20+
## doRUnit.R --- Run RUnit tests
21+
##
22+
## with credits to package fUtilities in RMetrics
23+
## which credits Gregor Gojanc's example in CRAN package 'gdata'
24+
## as per the R Wiki http://wiki.r-project.org/rwiki/doku.php?id=developers:runit
25+
## and changed further by Martin Maechler
26+
## and more changes by Murray Stokely in HistogramTools
27+
## and then used adapted in RProtoBuf
28+
## and now used in Rcpp and here
29+
##
30+
## Dirk Eddelbuettel, Feb - June 2014
31+
32+
stopifnot(require(RUnit, quietly=TRUE))
33+
stopifnot(require(RcppHoney, quietly=TRUE))
34+
35+
## Define tests
36+
testSuite <- defineTestSuite(name="RcppHoney Unit Tests",
37+
dirs=system.file("unitTests", package = "RcppHoney"),
38+
testFuncRegexp = "^[Tt]est.+")
39+
40+
## without this, we get (or used to get) unit test failures
41+
Sys.setenv("R_TESTS"="")
42+
43+
## Run tests
44+
tests <- runTestSuite(testSuite)
45+
46+
## Print results
47+
printTextProtocol(tests)
48+
49+
## Return success or failure to R CMD CHECK
50+
if (getErrors(tests)$nFail > 0) {
51+
stop("TEST FAILED!")
52+
}
53+
if (getErrors(tests)$nErr > 0) {
54+
stop("TEST HAD ERRORS!")
55+
}
56+
if (getErrors(tests)$nTestFunc < 1) {
57+
stop("NO TEST FUNCTIONS RUN!")
58+
}

0 commit comments

Comments
 (0)