Skip to content

Commit 8816c7c

Browse files
committed
Merge pull request #164 from cauthmann/internal_function_tests
Add unit tests for InternalFunction
2 parents e14d276 + dd6c7cd commit 8816c7c

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// InternalFunction.cpp: Rcpp R/C++ interface class library -- InternalFunction unit tests
4+
//
5+
// Copyright (C) 2014 Christian Authmann
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+
23+
#include <Rcpp.h>
24+
25+
26+
int add(int a, int b) {
27+
return a + b;
28+
}
29+
30+
31+
// [[Rcpp::export]]
32+
Rcpp::InternalFunction getAdd() {
33+
return Rcpp::InternalFunction( &add );
34+
}
35+
36+
37+
std::string concatenate(std::string a, std::string b) {
38+
return a + b;
39+
}
40+
41+
// [[Rcpp::export]]
42+
Rcpp::InternalFunction getConcatenate() {
43+
return Rcpp::InternalFunction( &concatenate );
44+
}
45+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// InternalFunction.cpp: Rcpp R/C++ interface class library -- InternalFunction unit tests
4+
//
5+
// Copyright (C) 2014 Christian Authmann
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+
23+
#define RCPP_USE_STD_FUNCTION
24+
#include <Rcpp.h>
25+
26+
27+
int add(int a, int b) {
28+
return a + b;
29+
}
30+
31+
32+
// [[Rcpp::export]]
33+
Rcpp::InternalFunction getAdd4() {
34+
std::function<int(int)> func = std::bind(add, 4, std::placeholders::_1);
35+
return Rcpp::InternalFunction( func );
36+
}
37+
38+
39+
// [[Rcpp::export]]
40+
Rcpp::InternalFunction getConcatenate() {
41+
std::function<std::string(std::string,std::string)> func = [] (std::string a, std::string b) -> std::string {
42+
return a + b;
43+
};
44+
return Rcpp::InternalFunction( func );
45+
}
46+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/r -t
2+
# hey emacs, please make this use -*- tab-width: 4 -*-
3+
#
4+
# Copyright (C) 2014 Christian Authmann
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+
25+
.tearDown <- function(){
26+
gc()
27+
}
28+
29+
.setUp <- Rcpp:::unitTestSetup("InternalFunction.cpp")
30+
31+
test.internal_function_add <- function(){
32+
fun <- getAdd()
33+
checkEquals( fun(10,32), 42 )
34+
}
35+
36+
test.internal_function_concatenate <- function(){
37+
fun <- getConcatenate()
38+
checkEquals( fun("Hello"," World"), "Hello World" )
39+
}
40+
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/r -t
2+
# hey emacs, please make this use -*- tab-width: 4 -*-
3+
#
4+
# Copyright (C) 2014 Christian Authmann
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+
hasCXX11 <- grepl("-std=c\\+\\+1[1-9yz]", Sys.getenv("PKG_CXXFLAGS"))
22+
23+
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes" && hasCXX11
24+
25+
if( .runThisTest ) {
26+
27+
.tearDown <- function(){
28+
gc()
29+
}
30+
31+
.setUp <- Rcpp:::unitTestSetup("InternalFunctionCPP11.cpp")
32+
33+
test.internal_function_add <- function(){
34+
fun <- getAdd4()
35+
checkEquals( fun(38), 42 )
36+
}
37+
38+
test.internal_function_concatenate <- function(){
39+
fun <- getConcatenate()
40+
checkEquals( fun("Hello"," World"), "Hello World" )
41+
}
42+
43+
}

0 commit comments

Comments
 (0)