Skip to content

Commit 00851f5

Browse files
committed
unit tests for Nullable
1 parent ac7defc commit 00851f5

File tree

3 files changed

+85
-33
lines changed

3 files changed

+85
-33
lines changed

inst/include/Rcpp/Nullable.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@
2222
#ifndef Rcpp_Nullable_h
2323
#define Rcpp_Nullable_h
2424

25+
// This class could possibly be written in a templated manner too, and we looked
26+
// into this. However, as an exception is thrown as soon as an actual proxy
27+
// object is accessed _when it was initialized with NULL_ we found no
28+
// satisfactory solution.
29+
//
30+
// We looked into the safe_bool_idiom [1] but found that more trouble than is
31+
// warranted here. We first and foremost want an operator SEXP() which got in
32+
// the way of redefining operator bool.
33+
// [1] http://www.artima.com/cppsource/safebool.html)
34+
2535
namespace Rcpp {
36+
2637
class Nullable {
2738
public:
2839

inst/unitTests/cpp/misc.cpp

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
22
//
33
// misc.cpp: Rcpp R/C++ interface class library -- misc unit tests
44
//
5-
// Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2013 - 2015 Dirk Eddelbuettel and Romain Francois
66
//
77
// This file is part of Rcpp.
88
//
@@ -20,7 +20,7 @@
2020
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2121

2222
#include <Rcpp.h>
23-
using namespace Rcpp ;
23+
using namespace Rcpp;
2424
using namespace std;
2525
#include <iostream>
2626
#include <fstream>
@@ -34,47 +34,47 @@ class simple {
3434
};
3535

3636
// [[Rcpp::export]]
37-
SEXP symbol_(){
37+
SEXP symbol_() {
3838
return LogicalVector::create(
3939
Symbol( Rf_install("foobar") ) == Rf_install("foobar"),
4040
Symbol( Rf_mkChar("foobar") ) == Rf_install("foobar"),
4141
Symbol( Rf_mkString("foobar") ) == Rf_install("foobar"),
4242
Symbol( "foobar" ) == Rf_install("foobar")
43-
) ;
43+
);
4444
}
4545

4646
// [[Rcpp::export]]
47-
Symbol symbol_ctor(SEXP x){ return Symbol(x); }
47+
Symbol symbol_ctor(SEXP x) { return Symbol(x); }
4848

4949
// [[Rcpp::export]]
50-
List Argument_(){
50+
List Argument_() {
5151
Argument x("x"), y("y");
5252
return List::create( x = 2, y = 3 );
5353
}
5454

5555
// [[Rcpp::export]]
56-
int Dimension_const( SEXP ia ){
56+
int Dimension_const( SEXP ia ) {
5757
simple ss(ia);
58-
return ss.nrow();
58+
return ss.nrow();
5959
}
6060

6161
// [[Rcpp::export]]
62-
SEXP evaluator_error(){
63-
return Rcpp_eval( Rf_lang2( Rf_install("stop"), Rf_mkString( "boom" ) ) ) ;
62+
SEXP evaluator_error() {
63+
return Rcpp_eval( Rf_lang2( Rf_install("stop"), Rf_mkString( "boom" ) ) );
6464
}
6565

6666
// [[Rcpp::export]]
67-
SEXP evaluator_ok(SEXP x){
68-
return Rcpp_eval( Rf_lang2( Rf_install("sample"), x ) ) ;
67+
SEXP evaluator_ok(SEXP x) {
68+
return Rcpp_eval( Rf_lang2( Rf_install("sample"), x ) );
6969
}
7070

7171
// [[Rcpp::export]]
72-
void exceptions_(){
73-
throw std::range_error("boom") ;
72+
void exceptions_() {
73+
throw std::range_error("boom");
7474
}
7575

7676
// [[Rcpp::export]]
77-
LogicalVector has_iterator_( ){
77+
LogicalVector has_iterator_( ) {
7878
return LogicalVector::create(
7979
(bool)Rcpp::traits::has_iterator< std::vector<int> >::value,
8080
(bool)Rcpp::traits::has_iterator< std::list<int> >::value,
@@ -87,7 +87,7 @@ LogicalVector has_iterator_( ){
8787
}
8888

8989
// [[Rcpp::export]]
90-
void test_rcout(std::string tfile, std::string teststring){
90+
void test_rcout(std::string tfile, std::string teststring) {
9191
// define and open testfile
9292
std::ofstream testfile(tfile.c_str());
9393

@@ -108,8 +108,8 @@ void test_rcout(std::string tfile, std::string teststring){
108108
}
109109

110110
// [[Rcpp::export]]
111-
LogicalVector na_proxy(){
112-
CharacterVector s("foo") ;
111+
LogicalVector na_proxy() {
112+
CharacterVector s("foo");
113113
return LogicalVector::create(
114114
NA_REAL == NA,
115115
NA_INTEGER == NA,
@@ -130,29 +130,49 @@ LogicalVector na_proxy(){
130130
NA == 12 ,
131131
NA == "foo",
132132
NA == s[0]
133-
) ;
133+
);
134134
}
135135

136136
// [[Rcpp::export]]
137-
StretchyList stretchy_list(){
138-
StretchyList out ;
139-
out.push_back( 1 ) ;
140-
out.push_front( "foo" ) ;
141-
out.push_back( 3.2 ) ;
137+
StretchyList stretchy_list() {
138+
StretchyList out;
139+
out.push_back( 1 );
140+
out.push_front( "foo" );
141+
out.push_back( 3.2 );
142142
return out;
143143
}
144144

145145
// [[Rcpp::export]]
146-
StretchyList named_stretchy_list(){
147-
StretchyList out ;
148-
out.push_back( _["b"] = 1 ) ;
149-
out.push_front( _["a"] = "foo" ) ;
150-
out.push_back( _["c"] = 3.2 ) ;
146+
StretchyList named_stretchy_list() {
147+
StretchyList out;
148+
out.push_back( _["b"] = 1 );
149+
out.push_front( _["a"] = "foo" );
150+
out.push_back( _["c"] = 3.2 );
151151
return out;
152152
}
153153

154154
// [[Rcpp::export]]
155-
void test_stop_variadic(){
156-
stop( "%s %d", "foo", 3 ) ;
155+
void test_stop_variadic() {
156+
stop( "%s %d", "foo", 3 );
157+
}
158+
159+
// [[Rcpp::export]]
160+
bool testNullableForNull(Nullable N) {
161+
return N.isNull();
162+
}
163+
164+
// [[Rcpp::export]]
165+
bool testNullableForNotNull(Nullable N) {
166+
return N.isNotNull();
167+
}
168+
169+
// [[Rcpp::export]]
170+
SEXP testNullableOperator(Nullable N) {
171+
return N;
172+
}
173+
174+
// [[Rcpp::export]]
175+
SEXP testNullableGet(Nullable N) {
176+
return N.get();
157177
}
158178

inst/unitTests/runit.misc.R

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/r -t
22
#
3-
# Copyright (C) 2010 - 2014 Dirk Eddelbuettel and Romain Francois
3+
# Copyright (C) 2010 - 2015 Dirk Eddelbuettel and Romain Francois
44
#
55
# This file is part of Rcpp.
66
#
@@ -147,4 +147,25 @@ if (.runThisTest) {
147147
checkEquals( m, "foo 3" )
148148
}
149149

150+
test.NullableForNull <- function() {
151+
S <- seq(1, 10)
152+
checkTrue( testNullableForNull(NULL) )
153+
checkTrue( ! testNullableForNull(S) )
154+
}
155+
156+
test.NullableForNotNull <- function() {
157+
S <- seq(1, 10)
158+
checkTrue( ! testNullableForNotNull(NULL) )
159+
checkTrue( testNullableForNotNull(S) )
160+
}
161+
162+
test.NullableAccessOperator <- function() {
163+
S <- seq(1, 10)
164+
checkEquals( testNullableOperator(S), S )
165+
}
166+
167+
test.NullableAccessGet <- function() {
168+
S <- seq(1, 10)
169+
checkEquals( testNullableGet(S), S )
170+
}
150171
}

0 commit comments

Comments
 (0)