Skip to content

Commit c1f42b6

Browse files
committed
add unit tests
1 parent 25a4b13 commit c1f42b6

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

inst/unitTests/cpp/table.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// as.cpp: Rcpp R/C++ interface class library -- as<> 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+
// [[Rcpp::export]]
26+
IntegerVector RcppTable(SEXP x) {
27+
switch (TYPEOF(x)) {
28+
case INTSXP: return table(as<IntegerVector>(x));
29+
case REALSXP: return table(as<NumericVector>(x));
30+
case STRSXP: return table(as<CharacterVector>(x));
31+
case LGLSXP: return table(as<LogicalVector>(x));
32+
default: {
33+
stop("untested SEXP type");
34+
return R_NilValue;
35+
}
36+
}
37+
}

inst/unitTests/runit.table.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/r -t
2+
#
3+
# Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois
4+
#
5+
# This file is part of Rcpp.
6+
#
7+
# Rcpp 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+
# Rcpp 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 Rcpp. If not, see <http://www.gnu.org/licenses/>.
19+
20+
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
21+
22+
if (.runThisTest) {
23+
24+
.setUp <- Rcpp:::unit_test_setup("table.cpp")
25+
26+
table <- function(x) base::table(x, useNA="ifany")
27+
28+
test.table.numeric <- function() {
29+
x <- c(1, 2, NA, NaN, -Inf, Inf)
30+
checkEquals( RcppTable(x), c(table(x)))
31+
}
32+
33+
test.table.integer <- function() {
34+
x <- c(-1L, 1L, NA_integer_, NA_integer_, 100L, 1L)
35+
checkEquals( RcppTable(x), c(table(x)))
36+
}
37+
38+
test.table.logical <- function() {
39+
x <- c(TRUE, TRUE, FALSE, NA)
40+
checkEquals( RcppTable(x), c(table(x)))
41+
}
42+
43+
test.table.character <- function() {
44+
x <- c("a", "a", "b", "a", NA, NA)
45+
checkEquals( RcppTable(x), c(table(x)))
46+
}
47+
48+
}

0 commit comments

Comments
 (0)