Skip to content

Commit f047f52

Browse files
committed
Add unit tests
1 parent 5c23627 commit f047f52

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

inst/unitTests/cpp/ListOf.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <Rcpp.h>
2+
using namespace Rcpp;
3+
4+
typedef ListOf<NumericVector> NVList;
5+
6+
// [[Rcpp::export]]
7+
NVList test_identity(NVList x) {
8+
return x;
9+
}
10+
11+
template <typename T>
12+
double sum_(const T& x) {
13+
return sum(x);
14+
}
15+
16+
// [[Rcpp::export]]
17+
List test_lapply_sum(NVList x) {
18+
return lapply(x, sum_<NumericVector>);
19+
}
20+
21+
// [[Rcpp::export]]
22+
NumericVector test_sapply_sum(NVList x) {
23+
return sapply(x, sum_<NumericVector>);
24+
}
25+
26+
// [[Rcpp::export]]
27+
NVList test_assign(ListOf<NumericVector> x, NumericVector y, CharacterVector z) {
28+
x[1] = y;
29+
x[2] = 1;
30+
return x;
31+
}
32+
33+
// [[Rcpp::export]]
34+
NVList test_assign_names(NVList x) {
35+
x["a"] = x["b"];
36+
return x;
37+
}
38+
39+
// [[Rcpp::export]]
40+
NumericVector test_add(NVList x) {
41+
return x[0] + x[1] + x[2];
42+
}
43+
44+
// [[Rcpp::export]]
45+
NVList test_add2(NVList x) {
46+
x[0] += x[1];
47+
return x;
48+
}
49+
50+
// [[Rcpp::export]]
51+
NumericVector test_add_subtract(NVList x) {
52+
return x[0] + x[1] - x[2];
53+
}
54+
55+
// [[Rcpp::export]]
56+
NumericVector test_mult(NVList x) {
57+
return x[0] * x[1] * x[2];
58+
}
59+
60+
typedef ListOf<CharacterVector> CVList;
61+
// [[Rcpp::export]]
62+
CVList test_char(CVList x) {
63+
x[0] = "apple";
64+
return x;
65+
}
66+
67+
typedef ListOf<NumericMatrix> NMList;
68+
69+
// [[Rcpp::export]]
70+
NVList test_matrix_sum(NVList x) {
71+
return lapply(x, sum_<NumericVector>);
72+
}
73+
74+
// [[Rcpp::export]]
75+
NVList test_as_wrap(NVList x) {
76+
List y = as<List>(x);
77+
NVList z = as<NVList>(y);
78+
NumericVector k = x[0];
79+
x[0] = z[1];
80+
return z;
81+
}
82+
83+
// [[Rcpp::export]]
84+
NumericVector test_add_NV(NVList x, NumericVector y) {
85+
return y + x[0];
86+
}
87+
88+
// [[Rcpp::export]]
89+
NVList test_binary_ops(NVList x) {
90+
return List::create(
91+
x[0] > x[1],
92+
x[0] < x[1],
93+
x[0] >= x[1],
94+
x[0] <= x[1]
95+
);
96+
}

inst/unitTests/runit.ListOf.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (C) 2014 Dirk Eddelbuettel, Romain Francois and Kevin Ushey
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+
.setUp <- Rcpp:::unitTestSetup("ListOf.cpp")
23+
24+
x <- list( c(1, 5), c(2, 6), c(3, 7) )
25+
26+
test.identity <- function() {
27+
checkIdentical(
28+
test_identity(setNames(x, c('a', 'b', 'c'))),
29+
setNames(x, c('a', 'b', 'c'))
30+
)
31+
}
32+
33+
test.lapply.sum <- function() {
34+
checkIdentical( test_lapply_sum(x), lapply(x, sum) )
35+
}
36+
37+
test.sapply.sum <- function() {
38+
checkIdentical( test_sapply_sum(x), sapply(x, sum) )
39+
}
40+
41+
test.assign <- function() {
42+
test_assign(x, 100, "apple")
43+
checkIdentical( x[[2]], 100 )
44+
}
45+
46+
test.assign.names <- function() {
47+
x <- setNames(list(1, 2, 3), c('a', 'b', 'c'))
48+
test_assign_names(x)
49+
checkIdentical( x[["a"]], x[["b"]] )
50+
}
51+
52+
test.arith <- function() {
53+
checkIdentical(test_add(list(1, 2, 3)), 6)
54+
checkIdentical(test_add2(list(1, 2, 3)), list(3, 2, 3))
55+
checkIdentical(test_add_subtract(list(1, 2, 3)), 0)
56+
checkIdentical(test_mult( list(1, 2, 3) ), 6)
57+
checkIdentical(test_char( list("banana") ), list("apple"))
58+
}
59+
60+
test.assign.names <- function() {
61+
checkException(test_assign_names(list(alpha=1, beta=2, gamma=3)))
62+
}
63+
64+
}

0 commit comments

Comments
 (0)