[Rcpp-commits] r2137 - in pkg/Rcpp/inst: examples/SugarPerformance unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Sep 19 23:51:26 CEST 2010
Author: edd
Date: 2010-09-19 23:51:26 +0200 (Sun, 19 Sep 2010)
New Revision: 2137
Modified:
pkg/Rcpp/inst/examples/SugarPerformance/Timer.h
pkg/Rcpp/inst/examples/SugarPerformance/Timertest.cpp
pkg/Rcpp/inst/unitTests/runit.Module.R
pkg/Rcpp/inst/unitTests/runit.Module.client.package.R
Log:
minor whitespace / indentation changes
Modified: pkg/Rcpp/inst/examples/SugarPerformance/Timer.h
===================================================================
--- pkg/Rcpp/inst/examples/SugarPerformance/Timer.h 2010-09-19 15:22:35 UTC (rev 2136)
+++ pkg/Rcpp/inst/examples/SugarPerformance/Timer.h 2010-09-19 21:51:26 UTC (rev 2137)
@@ -30,25 +30,25 @@
class Timer {
public:
- Timer() : sys_time("Sys.time") { Reset(); }
- void Start() { start_t = getFractionalSeconds() ; }
- void Stop() {
- end_t = getFractionalSeconds();
- elapsed = end_t - start_t; // Calculate elapsed time in seconds
- cumul += elapsed;
- }
- void Reset() { end_t = start_t = elapsed = cumul = 0.0; }
- double ElapsedTime() { return elapsed; }
- double CumulativeTime() { return cumul; }
+ Timer() : sys_time("Sys.time") { Reset(); }
+ void Start() { start_t = getFractionalSeconds() ; }
+ void Stop() {
+ end_t = getFractionalSeconds();
+ elapsed = end_t - start_t; // Calculate elapsed time in seconds
+ cumul += elapsed;
+ }
+ void Reset() { end_t = start_t = elapsed = cumul = 0.0; }
+ double ElapsedTime() { return elapsed; }
+ double CumulativeTime() { return cumul; }
private:
- Function sys_time ;
- double start_t, end_t, elapsed, cumul;
+ Function sys_time ;
+ double start_t, end_t, elapsed, cumul;
- double getFractionalSeconds(void) {
+ double getFractionalSeconds(void) {
return as<double>( sys_time() ) ;
- }
+ }
};
#endif
Modified: pkg/Rcpp/inst/examples/SugarPerformance/Timertest.cpp
===================================================================
--- pkg/Rcpp/inst/examples/SugarPerformance/Timertest.cpp 2010-09-19 15:22:35 UTC (rev 2136)
+++ pkg/Rcpp/inst/examples/SugarPerformance/Timertest.cpp 2010-09-19 21:51:26 UTC (rev 2137)
@@ -1,4 +1,4 @@
-// -*- mode: c++; compile-command: "g++ -Wall -O3 -o Timertest Timertest.cpp" -*-
+// -*- mode: c++; compile-command: "g++ -Wall -O3 -o Timertest Timertest.cpp"; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
// from http://www.cs.uiowa.edu/~sriram/30/fall03/
@@ -7,25 +7,25 @@
#include "Timer.h"
int main() {
- Timer test;
+ Timer test;
- std::cout << "Sleeping 2 seconds" << std::endl;
- test.Start();
- sleep(2);
- test.Stop();
- std::cout << "Sleep lasted for " << test.ElapsedTime() << " seconds." << std::endl;
- std::cout << "Sleeping 1 second" << std::endl;
- test.Start();
- sleep(1);
- test.Stop();
- std::cout << "Sleep lasted for " << test.ElapsedTime() << " seconds." << std::endl;
- std::cout << "Cumulative time is " << test.CumulativeTime() << " seconds." << std::endl;
- std::cout << "Reseting" << std::endl;
- test.Reset();
- std::cout << "Sleeping 2 seconds" << std::endl;
- test.Start();
- sleep(2);
- test.Stop();
- std::cout << "Sleep lasted for " << test.ElapsedTime() << " seconds." << std::endl;
- std::cout << "Cumulative time is " << test.CumulativeTime() << " seconds." << std::endl;
+ std::cout << "Sleeping 2 seconds" << std::endl;
+ test.Start();
+ sleep(2);
+ test.Stop();
+ std::cout << "Sleep lasted for " << test.ElapsedTime() << " seconds." << std::endl;
+ std::cout << "Sleeping 1 second" << std::endl;
+ test.Start();
+ sleep(1);
+ test.Stop();
+ std::cout << "Sleep lasted for " << test.ElapsedTime() << " seconds." << std::endl;
+ std::cout << "Cumulative time is " << test.CumulativeTime() << " seconds." << std::endl;
+ std::cout << "Reseting" << std::endl;
+ test.Reset();
+ std::cout << "Sleeping 2 seconds" << std::endl;
+ test.Start();
+ sleep(2);
+ test.Stop();
+ std::cout << "Sleep lasted for " << test.ElapsedTime() << " seconds." << std::endl;
+ std::cout << "Cumulative time is " << test.CumulativeTime() << " seconds." << std::endl;
}
Modified: pkg/Rcpp/inst/unitTests/runit.Module.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Module.R 2010-09-19 15:22:35 UTC (rev 2136)
+++ pkg/Rcpp/inst/unitTests/runit.Module.R 2010-09-19 21:51:26 UTC (rev 2137)
@@ -1,4 +1,5 @@
#!/usr/bin/r -t
+# hey emacs, please make this use -*- tab-width: 4 -*-
#
# Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
#
@@ -26,66 +27,66 @@
test.Module <- function(){
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 ){
+ 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" )
.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 )
@@ -93,16 +94,16 @@
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
w <- new( World )
checkEquals( w$greet(), "hello" )
- w$set( "hello world" )
+ w$set( "hello world" )
checkEquals( w$greet(), "hello world" )
w$clear( )
checkEquals( w$greet(), "" )
-
-
+
+
}
# hide this one for now (suncc not happy about overloads)
@@ -129,43 +130,43 @@
RCPP_MODULE(yada){
using namespace Rcpp ;
-
+
class_<vec>( "vec")
.method( "size", &vec::size)
- .method( "max_size", &vec::max_size)
- .method( "resize", &vec::resize)
- .method( "capacity", &vec::capacity)
- .method( "empty", &vec::empty)
- .method( "reserve", &vec::reserve)
+ .method( "max_size", &vec::max_size)
+ .method( "resize", &vec::resize)
+ .method( "capacity", &vec::capacity)
+ .method( "empty", &vec::empty)
+ .method( "reserve", &vec::reserve)
.method( "push_back", &vec::push_back )
.method( "pop_back", &vec::pop_back )
.method( "clear", &vec::clear )
-
+
.const_method( "back", &vec::back )
.const_method( "front", &vec::front )
.const_method( "at", &vec::at )
-
+
.method( "assign", &vec_assign )
.method( "insert", &vec_insert )
- .method( "as.vector", &vec_asR )
-
-
+ .method( "as.vector", &vec_asR )
+
+
;
-}
+}
'
fx <- cxxfunction( signature(), "", include = inc, plugin = "Rcpp" )
-
+
yada <- Rcpp:::Module( "yada", getDynLib( fx ) )
v <- new( yada$vec )
v$assign( 1:10 )
-
+
checkEquals( v$back(), 10 )
v$push_back( 10 )
checkEquals( as.integer(v$size()), 11L )
checkEquals( v$at( 0 ), 1 )
checkEquals( v$as.vector(), c(1:10, 10 ) )
-
+
}
}
@@ -175,81 +176,81 @@
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" )
-
+
// 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 )
-
+ checkEquals( w$x, 0.0 )
+ checkEquals( w$y, 0L )
+
w$x <- 2.0
- checkEquals( w$x, 2.0 )
-
+ checkEquals( w$x, 2.0 )
+
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" )
-
+
// 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 )
-
+ checkEquals( w$x, 0.0 )
+ checkEquals( w$y, 0L )
+
w$x <- 2.0
- checkEquals( w$x, 2.0 )
-
+ checkEquals( w$x, 2.0 )
+
checkException( { w$y <- 3 } )
}
-
+
}
Modified: pkg/Rcpp/inst/unitTests/runit.Module.client.package.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Module.client.package.R 2010-09-19 15:22:35 UTC (rev 2136)
+++ pkg/Rcpp/inst/unitTests/runit.Module.client.package.R 2010-09-19 21:51:26 UTC (rev 2137)
@@ -1,4 +1,5 @@
#!/usr/bin/r -t
+# hey emacs, please make this use -*- tab-width: 4 -*-
#
# Copyright (C) 2010 John Chambers, Dirk Eddelbuettel and Romain Francois
#
@@ -24,42 +25,42 @@
if( Rcpp:::capabilities()[["Rcpp modules"]] ) {
test.Module.package <- function( ){
-
- td <- tempfile()
- cwd <- getwd()
- dir.create( td )
- file.copy( system.file( "unitTests", "testRcppModule", package = "Rcpp" ) , td, recursive = TRUE)
- setwd( td )
- on.exit( { setwd( cwd) ; unlink( td, recursive = TRUE ) } )
- R <- shQuote( file.path( R.home( component = "bin" ), "R" ))
- cmd <- paste( R , "CMD build testRcppModule" )
- system( cmd )
- dir.create( "templib" )
- install.packages( "testRcppModule_0.1.tar.gz", "templib", repos = NULL, type = "source" )
- require( "testRcppModule", "templib", character.only = TRUE )
-
+
+ td <- tempfile()
+ cwd <- getwd()
+ dir.create( td )
+ file.copy( system.file( "unitTests", "testRcppModule", package = "Rcpp" ) , td, recursive = TRUE)
+ setwd( td )
+ on.exit( { setwd( cwd) ; unlink( td, recursive = TRUE ) } )
+ R <- shQuote( file.path( R.home( component = "bin" ), "R" ))
+ cmd <- paste( R , "CMD build testRcppModule" )
+ system( cmd )
+ dir.create( "templib" )
+ install.packages( "testRcppModule_0.1.tar.gz", "templib", repos = NULL, type = "source" )
+ require( "testRcppModule", "templib", character.only = TRUE )
+
vClass <- stdVector$vec
vec <- new(vClass)
-
+
data <- 1:10
vec$assign(data)
vec[[3]] <- vec[[3]] + 1
-
+
data[[4]] <- data[[4]] +1
-
+
checkEquals( vec$as.vector(), data )
-
+
## a few function calls
-
+
checkEquals( yada$bar(2), 4)
-
+
e <- tryCatch(yada$hello(), error = function(x)x)
-
+
checkTrue(is(e, "error"))
checkEquals( e$message, "boom")
-
+
checkEquals( yada$foo(2,3), 6)
-
+
## properties (at one stage this seqfaulted, so beware)
nc = NumEx$Num
nn <- new(nc)
More information about the Rcpp-commits
mailing list