Skip to content

Commit cb84be9

Browse files
committed
Add unit tests for RCPP_RETURN_VECTOR/MATRIX macro
1 parent 3050bd0 commit cb84be9

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

ChangeLog

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

33
* inst/include/Rcpp/macros/dispatch.h: Add variadic conditional macro
44
when C++11 compiler used
5+
* ints/include/unitTests/cpp/dispatch.cpp: Add unit tests for
6+
RCPP_RETURN_VECTOR and RCPP_RETURN_MATRIX macro
7+
* ints/include/unitTests/runit.dispatch.R: Idem
58

69
2016-08-05 James J Balamuta <balamut2@illinois.edu>
710

inst/unitTests/cpp/dispatch.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// dispatch.cpp: Rcpp R/C++ interface class library -- dispatch macro unit tests
4+
//
5+
// Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#include <Rcpp.h>
23+
using namespace Rcpp ;
24+
25+
template <typename T>
26+
T first_el_impl(const T& x) {
27+
T res(1);
28+
res[0] = x[0];
29+
return res;
30+
}
31+
32+
// [[Rcpp::export]]
33+
SEXP first_el(SEXP x) {
34+
RCPP_RETURN_VECTOR(first_el_impl, x);
35+
}
36+
37+
template <typename T>
38+
T first_cell_impl(const T& x) {
39+
T res(1, 1);
40+
res(0, 0) = x(0, 0);
41+
return res;
42+
}
43+
44+
// [[Rcpp::export]]
45+
SEXP first_cell(SEXP x) {
46+
RCPP_RETURN_MATRIX(first_cell_impl, x)
47+
}

inst/unitTests/runit.dispatch.R

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env r
2+
# -*- mode: R; tab-width: 4; -*-
3+
#
4+
# Copyright (C) 2009 - 2014 Dirk Eddelbuettel and Romain Francois
5+
#
6+
# This file is part of Rcpp.
7+
#
8+
# Rcpp is free software: you can redistribute it and/or modify it
9+
# under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation, either version 2 of the License, or
11+
# (at your option) any later version.
12+
#
13+
# Rcpp is distributed in the hope that it will be useful, but
14+
# WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU General Public License
19+
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20+
21+
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
22+
23+
if (.runThisTest) {
24+
.setUp <- Rcpp:::unitTestSetup("dispatch.cpp")
25+
26+
test.RawVector <- function() {
27+
x <- as.raw(0:9)
28+
checkEquals(first_el(x), x[1], msg = "RCPP_RETURN_VECTOR (raw)")
29+
}
30+
31+
test.ExpressionVector <- function() {
32+
x <- as.expression(0:9)
33+
checkEquals(first_el(x), x[1], msg = "RCPP_RETURN_VECTOR (expression)")
34+
}
35+
36+
test.ComplexVector <- function() {
37+
x <- as.complex(0:9)
38+
checkEquals(first_el(x), x[1], msg = "RCPP_RETURN_VECTOR (complex)")
39+
}
40+
41+
test.IntegerVector <- function() {
42+
x <- as.integer(0:9)
43+
checkEquals(first_el(x), x[1], msg = "RCPP_RETURN_VECTOR (integer)")
44+
}
45+
46+
test.NumericVector <- function() {
47+
x <- as.numeric(0:9)
48+
checkEquals(first_el(x), x[1], msg = "RCPP_RETURN_VECTOR (numeric)")
49+
}
50+
51+
test.GenericVector <- function() {
52+
x <- as.list(0:9)
53+
checkEquals(first_el(x), x[1], msg = "RCPP_RETURN_VECTOR (list)")
54+
}
55+
56+
test.CharacterVector <- function() {
57+
x <- as.character(0:9)
58+
checkEquals(first_el(x), x[1], msg = "RCPP_RETURN_VECTOR (character)")
59+
}
60+
61+
test.RawMatrix <- function() {
62+
x <- matrix(as.raw(0:9), ncol = 2L)
63+
checkEquals(first_cell(x), x[1, 1, drop = FALSE], msg = "RCPP_RETURN_MATRIX (raw)")
64+
}
65+
66+
test.ExpressionMatrix <- function() {
67+
x <- matrix(as.expression(0:9), ncol = 2L)
68+
checkEquals(first_cell(x), x[1, 1, drop = FALSE], msg = "RCPP_RETURN_MATRIX (expression)")
69+
}
70+
71+
test.ComplexMatrix <- function() {
72+
x <- matrix(as.complex(0:9), ncol = 2L)
73+
checkEquals(first_cell(x), x[1, 1, drop = FALSE], msg = "RCPP_RETURN_MATRIX (complex)")
74+
}
75+
76+
test.IntegerMatrix <- function() {
77+
x <- matrix(as.integer(0:9), ncol = 2L)
78+
checkEquals(first_cell(x), x[1, 1, drop = FALSE], msg = "RCPP_RETURN_MATRIX (integer)")
79+
}
80+
81+
test.NumericMatrix <- function() {
82+
x <- matrix(as.numeric(0:9), ncol = 2L)
83+
checkEquals(first_cell(x), x[1, 1, drop = FALSE], msg = "RCPP_RETURN_MATRIX (numeric)")
84+
}
85+
86+
test.GenericMatrix <- function() {
87+
x <- matrix(lapply(0:9, function(.) as.list(0:9)), ncol = 2L)
88+
checkEquals(first_cell(x), x[1, 1, drop = FALSE], msg = "RCPP_RETURN_MATRIX (list)")
89+
}
90+
91+
test.CharacterMatrix <- function() {
92+
x <- matrix(as.character(0:9), ncol = 2L)
93+
checkEquals(first_cell(x), x[1, 1, drop = FALSE], msg = "RCPP_RETURN_MATRIX (character)")
94+
}
95+
96+
}

0 commit comments

Comments
 (0)