[Rcpp-commits] r4389 - in pkg/Rcpp: . inst/unitTests inst/unitTests/cpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jul 3 11:29:28 CEST 2013
Author: romain
Date: 2013-07-03 11:29:27 +0200 (Wed, 03 Jul 2013)
New Revision: 4389
Added:
pkg/Rcpp/inst/unitTests/cpp/Module.cpp
pkg/Rcpp/inst/unitTests/cpp/modref.cpp
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/unitTests/runit.Module.R
pkg/Rcpp/inst/unitTests/runit.modref.R
Log:
more use of sourceCpp in testing
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2013-07-02 17:38:03 UTC (rev 4388)
+++ pkg/Rcpp/ChangeLog 2013-07-03 09:29:27 UTC (rev 4389)
@@ -1,3 +1,8 @@
+2013-07-03 Romain Francois <romain at r-enthusiasts.com>
+
+ * unitTests/runit.modref.R: using sourceCpp
+ * unitTests/runit.Module.R: using sourceCpp
+
2013-07-02 Romain Francois <romain at r-enthusiasts.com>
* include/Rcpp/vector/Vector.h: fill__dispatch was mispelled (as
Added: pkg/Rcpp/inst/unitTests/cpp/Module.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/cpp/Module.cpp (rev 0)
+++ pkg/Rcpp/inst/unitTests/cpp/Module.cpp 2013-07-03 09:29:27 UTC (rev 4389)
@@ -0,0 +1,146 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Module.cpp: Rcpp R/C++ interface class library -- module unit tests
+//
+// Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+#include <Rcpp.h>
+using namespace Rcpp ;
+
+std::string hello(){
+ return "hello" ;
+}
+
+int bar( int x){
+ return x*2 ;
+}
+
+double foo( int x, double y){
+ return x * y ;
+}
+
+void bla( ){
+ Rprintf( "hello\\n" ) ;
+}
+
+void bla1( int x){
+ Rprintf( "hello (x = %d)\\n", x ) ;
+}
+
+void bla2( int x, double y){
+ Rprintf( "hello (x = %d, y = %5.2f)\\n", x, y ) ;
+}
+
+class World {
+public:
+ World() : msg("hello"){}
+ void set(std::string msg_) { this->msg = msg_; }
+ std::string greet() { return msg; }
+
+private:
+ std::string msg;
+};
+
+void clearWorld( World* w ){
+ w->set( "" );
+}
+
+class Num{
+public:
+ Num() : x(0.0), y(0){} ;
+
+ double getX() const { return x ; }
+ void setX(double value){ x = value ; }
+
+ int getY() { return y ; }
+
+private:
+ double x ;
+ int y ;
+};
+
+class Number{
+public:
+ Number() : x(0.0), y(0){} ;
+
+ double x ;
+ int y ;
+};
+
+class Randomizer {
+public:
+
+ // Randomizer() : min(0), max(1){}
+ Randomizer( double min_, double max_) : min(min_), max(max_){}
+
+ NumericVector get( int n ){
+ RNGScope scope ;
+ return runif( n, min, max );
+ }
+
+private:
+ double min, max ;
+} ;
+
+
+RCPP_MODULE(yada){
+ function( "hello" , &hello ) ;
+ function( "bar" , &bar ) ;
+ function( "foo" , &foo ) ;
+ function( "bla" , &bla ) ;
+ function( "bla1" , &bla1 ) ;
+ function( "bla2" , &bla2 ) ;
+
+ class_<World>( "World" )
+
+ .constructor()
+
+ .method( "greet", &World::greet )
+ .method( "set", &World::set )
+ .method( "clear", &clearWorld )
+ ;
+
+ class_<Num>( "Num" )
+ .constructor()
+
+ // read and write property
+ .property( "x", &Num::getX, &Num::setX )
+
+ // read-only property
+ .property( "y", &Num::getY )
+ ;
+
+ class_<Number>( "Number" )
+
+ .constructor()
+
+ // read and write data member
+ .field( "x", &Number::x )
+
+ // read only data member
+ .field_readonly( "y", &Number::y )
+ ;
+
+ class_<Randomizer>( "Randomizer" )
+ // No default: .default_constructor()
+ .constructor<double,double>()
+
+ .method( "get" , &Randomizer::get )
+ ;
+}
+
Added: pkg/Rcpp/inst/unitTests/cpp/modref.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/cpp/modref.cpp (rev 0)
+++ pkg/Rcpp/inst/unitTests/cpp/modref.cpp 2013-07-03 09:29:27 UTC (rev 4389)
@@ -0,0 +1,55 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// modref.cpp: Rcpp R/C++ interface class library -- module unit tests
+//
+// Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+#include <Rcpp.h>
+using namespace Rcpp ;
+
+class World {
+public:
+ World() : foo(1), msg("hello") {}
+ void set(std::string msg_) { this->msg = msg_; }
+ std::string greet() { return msg; }
+
+ int foo ;
+ double bar ;
+
+private:
+ std::string msg;
+};
+
+void clearWorld( World* w ){
+ w->set( "" );
+}
+
+RCPP_MODULE(yada){
+ class_<World>( "World" )
+ .default_constructor()
+
+ .method( "greet", &World::greet )
+ .method( "set", &World::set )
+ .method( "clear", &clearWorld )
+
+ .field( "foo", &World::foo )
+ .field_readonly( "bar", &World::bar )
+ ;
+
+}
+
Modified: pkg/Rcpp/inst/unitTests/runit.Module.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Module.R 2013-07-02 17:38:03 UTC (rev 4388)
+++ pkg/Rcpp/inst/unitTests/runit.Module.R 2013-07-03 09:29:27 UTC (rev 4389)
@@ -1,7 +1,7 @@
#!/usr/bin/r -t
# hey emacs, please make this use -*- tab-width: 4 -*-
#
-# Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
+# Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
#
# This file is part of Rcpp.
#
@@ -18,136 +18,31 @@
# You should have received a copy of the GNU General Public License
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
-.tearDown <- function(){
- gc()
-}
-
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
# .runThisTest <- FALSE
if( .runThisTest && Rcpp:::capabilities()[["Rcpp modules"]] ) {
-test.Module <- function(){
+.tearDown <- function(){
+ gc()
+}
- inc <- '
-
- std::string hello(){
- return "hello" ;
- }
-
- int bar( int x){
- return x*2 ;
- }
-
- double foo( int x, double y){
- return x * y ;
- }
-
- void bla( ){
- Rprintf( "hello\\n" ) ;
- }
-
- void bla1( int x){
- Rprintf( "hello (x = %d)\\n", x ) ;
- }
-
- void bla2( int x, double y){
- Rprintf( "hello (x = %d, y = %5.2f)\\n", x, y ) ;
- }
-
- class World {
- public:
- World() : msg("hello"){}
- void set(std::string msg_) { this->msg = msg_; }
- std::string greet() { return msg; }
-
- private:
- std::string msg;
- };
-
- void clearWorld( World* w ){
- w->set( "" );
- }
-
- RCPP_MODULE(yada){
- using namespace Rcpp ;
-
- function( "hello" , &hello ) ;
- function( "bar" , &bar ) ;
- function( "foo" , &foo ) ;
- function( "bla" , &bla ) ;
- function( "bla1" , &bla1 ) ;
- function( "bla2" , &bla2 ) ;
-
- class_<World>( "World" )
-
- .constructor()
-
- .method( "greet", &World::greet )
- .method( "set", &World::set )
- .method( "clear", &clearWorld )
- ;
-
- }
-
- '
- fx <- cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" )
-
- mod <- Module( "yada", getDynLib(fx) )
- checkEquals( mod$bar( 2L ), 4L )
- checkEquals( mod$foo( 2L, 10.0 ), 20.0 )
- checkEquals( mod$hello(), "hello" )
- # checkEquals( capture.output( mod$bla() ), "hello" )
- # checkEquals( capture.output( mod$bla1(2L) ), "hello (x = 2)" )
- # checkEquals( capture.output( mod$bla2(2L, 5.0) ), "hello (x = 2, y = 5.00)" )
-
- World <- mod$World
+.setUp <- Rcpp:::unit_test_setup( "Module.cpp" )
+
+test.Module <- function(){
+ checkEquals( bar( 2L ), 4L )
+ checkEquals( foo( 2L, 10.0 ), 20.0 )
+ checkEquals( hello(), "hello" )
+
w <- new( World )
checkEquals( w$greet(), "hello" )
w$set( "hello world" )
checkEquals( w$greet(), "hello world" )
w$clear( )
checkEquals( w$greet(), "" )
-
-
}
test.Module.property <- function(){
-
- inc <- '
- class Num{
- public:
- Num() : x(0.0), y(0){} ;
-
- double getX() const { return x ; }
- void setX(double value){ x = value ; }
-
- int getY() { return y ; }
-
- private:
- double x ;
- int y ;
- };
-
- RCPP_MODULE(yada){
- using namespace Rcpp ;
-
- class_<Num>( "Num" )
-
- .constructor()
-
- // read and write property
- .property( "x", &Num::getX, &Num::setX )
-
- // read-only property
- .property( "y", &Num::getY )
- ;
- }
- '
- fx <- cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" )
-
- mod <- Module( "yada", getDynLib(fx) )
- Num <- mod$Num
w <- new( Num )
checkEquals( w$x, 0.0 )
checkEquals( w$y, 0L )
@@ -158,37 +53,7 @@
checkException( { w$y <- 3 } )
}
-
test.Module.member <- function(){
-
- inc <- '
- class Number{
- public:
- Number() : x(0.0), y(0){} ;
-
- double x ;
- int y ;
- };
-
- RCPP_MODULE(yada){
- using namespace Rcpp ;
-
- class_<Number>( "Number" )
-
- .constructor()
-
- // read and write data member
- .field( "x", &Number::x )
-
- // read only data member
- .field_readonly( "y", &Number::y )
- ;
- }
- '
- fx <- cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" )
-
- mod <- Module( "yada", getDynLib(fx) )
- Number <- mod$Number
w <- new( Number )
checkEquals( w$x, 0.0 )
checkEquals( w$y, 0L )
@@ -200,48 +65,11 @@
}
test.Module.Constructor <- function() {
-
- inc <- '
-
-class Randomizer {
-public:
-
- // Randomizer() : min(0), max(1){}
- Randomizer( double min_, double max_) : min(min_), max(max_){}
-
- NumericVector get( int n ){
- RNGScope scope ;
- return runif( n, min, max );
- }
-
-private:
- double min, max ;
-} ;
-
-RCPP_MODULE(mod){
-
- class_<Randomizer>( "Randomizer" )
-
- // No default: .default_constructor()
- .constructor<double,double>()
-
- .method( "get" , &Randomizer::get ) ;
-
-}
-'
- fx <- cxxfunction( , '', includes = inc, plugin = "Rcpp" )
-
- mod <- Module( "mod", getDynLib( fx ) )
-
- Randomizer <- mod$Randomizer
r <- new( Randomizer, 10.0, 20.0 )
set.seed(123)
x10 <- runif(10, 10.0, 20.0)
set.seed(123)
checkEquals(r$get(10), x10)
-
- ##r <- new( Randomizer )
- ##stopifnot(is(tryCatch(r$get(10), error = function(e)e), "error"))
}
}
Modified: pkg/Rcpp/inst/unitTests/runit.modref.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.modref.R 2013-07-02 17:38:03 UTC (rev 4388)
+++ pkg/Rcpp/inst/unitTests/runit.modref.R 2013-07-03 09:29:27 UTC (rev 4389)
@@ -1,6 +1,6 @@
#!/usr/bin/r -t
#
-# Copyright (C) 2010 - 2012 John Chambers, Dirk Eddelbuettel and Romain Francois
+# Copyright (C) 2010 - 2013 John Chambers, Dirk Eddelbuettel and Romain Francois
#
# This file is part of Rcpp.
#
@@ -21,65 +21,25 @@
if (.runThisTest) {
- test.modRef <- function() {
- inc <- '
+.setUp <- Rcpp:::unit_test_setup( "modref.cpp" )
+
+test.modRef <- function() {
+ ww = new(World)
+ wg = World$new()
+
+ checkEquals(ww$greet(), wg$greet())
+ wgg <- wg$greet()
+
+ ww$set("Other")
+
+ ## test independence of ww, wg
+ checkEquals(ww$greet(), "Other")
+ checkEquals(wg$greet(), wgg)
+
+ World$methods(twice = function() paste(greet(), greet()))
+
+ checkEquals(ww$twice(), paste(ww$greet(), ww$greet()))
- class World {
- public:
- World() : foo(1), msg("hello") {}
- void set(std::string msg_) { this->msg = msg_; }
- std::string greet() { return msg; }
+}
- int foo ;
- double bar ;
-
- private:
- std::string msg;
- };
-
- void clearWorld( World* w ){
- w->set( "" );
- }
-
- RCPP_MODULE(yada){
- using namespace Rcpp ;
-
- class_<World>( "World" )
-
- .default_constructor()
-
- .method( "greet", &World::greet )
- .method( "set", &World::set )
- .method( "clear", &clearWorld )
-
- .field( "foo", &World::foo )
- .field_readonly( "bar", &World::bar )
- ;
-
- }
-
- '
- fx <- inline::cxxfunction( signature(), "" , include = inc, plugin = "Rcpp" )
-
- mod <- Module( "yada", getDynLib(fx) )
-
- World <- mod$World
-
- ww = new(World)
- wg = World$new()
-
- checkEquals(ww$greet(), wg$greet())
- wgg <- wg$greet()
-
- ww$set("Other")
-
- ## test independence of ww, wg
- checkEquals(ww$greet(), "Other")
- checkEquals(wg$greet(), wgg)
-
- World$methods(twice = function() paste(greet(), greet()))
-
- checkEquals(ww$twice(), paste(ww$greet(), ww$greet()))
-
- }
}
More information about the Rcpp-commits
mailing list