Skip to content

Commit 89a3b35

Browse files
using sourceCpp in runit.Vector tests
1 parent 0b4343c commit 89a3b35

7 files changed

Lines changed: 259 additions & 1030 deletions

File tree

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2012-11-29 Romain Francois <romain@r-enthusiasts.com>
2+
3+
* unitTests/runit.DataFrame.R: using sourceCpp
4+
* include/Rcpp/vector/Matrix.h: fix yet another const correctness issue
5+
16
2012-11-27 Dirk Eddelbuettel <edd@debian.org>
27

38
* inst/include/Rcpp/iostream/Rostream.h: Check before deleting buf

inst/unitTests/cpp/DataFrame.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// DataFrame.cpp: Rcpp R/C++ interface class library -- DataFrame unit tests
4+
//
5+
// Copyright (C) 2012 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+
DataFrame FromSEXP( SEXP x){
27+
DataFrame df(x) ;
28+
return df;
29+
}
30+
31+
// [[Rcpp::export]]
32+
SEXP index_byName( DataFrame df, std::string s ){
33+
return df[s];
34+
}
35+
36+
// [[Rcpp::export]]
37+
SEXP index_byPosition( DataFrame df, int i ){
38+
return df[i];
39+
}
40+
// [[Rcpp::export]]
41+
std::string string_element( DataFrame df ){
42+
CharacterVector b = df[1];
43+
std::string s;
44+
s = b[1];
45+
return s;
46+
}
47+
48+
// [[Rcpp::export]]
49+
DataFrame createOne(){
50+
IntegerVector v = IntegerVector::create(1,2,3);
51+
return DataFrame::create(Named("a")=v);
52+
}
53+
54+
// [[Rcpp::export]]
55+
DataFrame createTwo(){
56+
IntegerVector v = IntegerVector::create(1,2,3);
57+
std::vector<std::string> s(3);
58+
s[0] = "a";
59+
s[1] = "b";
60+
s[2] = "c";
61+
return DataFrame::create(Named("a")=v, Named("b")=s);
62+
}
63+
64+
// [[Rcpp::export]]
65+
DataFrame SlotProxy( S4 o, std::string yy ){
66+
return DataFrame( o.slot( yy ) ) ;
67+
}
68+
69+
// [[Rcpp::export]]
70+
DataFrame AttributeProxy( List o, std::string y ){
71+
return DataFrame( o.attr( y )) ;
72+
}
73+
74+
// [[Rcpp::export]]
75+
DataFrame createTwoStringsAsFactors(){
76+
IntegerVector v = IntegerVector::create(1,2,3);
77+
std::vector<std::string> s(3);
78+
s[0] = "a";
79+
s[1] = "b";
80+
s[2] = "c";
81+
return DataFrame::create(
82+
_["a"] = v,
83+
_["b"] = s,
84+
_["stringsAsFactors"] = false );
85+
}
86+
87+
// [[Rcpp::export]]
88+
int DataFrame_nrows( DataFrame df){
89+
return df.nrows() ;
90+
}
91+

inst/unitTests/cpp/reg_tests_0_10_1.cpp

Lines changed: 0 additions & 8 deletions
This file was deleted.

inst/unitTests/runit.DataFrame.R

Lines changed: 19 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -22,142 +22,65 @@
2222

2323
if (.runThisTest) {
2424

25-
definitions <- function(){
26-
list("FromSEXP"=list(
27-
signature(x="ANY"),
28-
'DataFrame df(x) ;
29-
return df;')
30-
31-
,"index_byName"=list(
32-
signature(x='ANY', y='character'),
33-
'DataFrame df(x);
34-
std::string s = as<std::string>(y);
35-
return df[s];')
36-
37-
,"index_byPosition"=list(
38-
signature(x='ANY', y='integer'),
39-
'DataFrame df(x);
40-
int i = as<int>(y);
41-
return df[i]; ')
42-
43-
,"string_element"=list(
44-
signature(x='ANY'),
45-
'DataFrame df(x);
46-
CharacterVector b = df[1];
47-
std::string s;
48-
s = b[1];
49-
return wrap(s); ')
50-
51-
,"createOne"=list(
52-
signature(),
53-
'IntegerVector v = IntegerVector::create(1,2,3);
54-
return DataFrame::create(Named("a")=v); ')
55-
56-
,"createTwo"=list(
57-
signature(),
58-
'IntegerVector v = IntegerVector::create(1,2,3);
59-
std::vector<std::string> s(3);
60-
s[0] = "a";
61-
s[1] = "b";
62-
s[2] = "c";
63-
return DataFrame::create(Named("a")=v, Named("b")=s); ')
64-
65-
,"SlotProxy"=list(
66-
signature(x="ANY", y="character"),
67-
'
68-
S4 o(x) ;
69-
std::string yy = as<std::string>( y ) ;
70-
return DataFrame( o.slot( yy ) ) ;
71-
')
72-
73-
,"AttributeProxy"=list(
74-
signature(x="ANY", y="character"),
75-
'List o(x) ;
76-
return DataFrame( o.attr( as<std::string>(y) )) ; ')
77-
78-
,"createTwoStringsAsFactors"=list(
79-
signature(),
80-
'IntegerVector v = IntegerVector::create(1,2,3);
81-
std::vector<std::string> s(3);
82-
s[0] = "a";
83-
s[1] = "b";
84-
s[2] = "c";
85-
return DataFrame::create(
86-
_["a"] = v,
87-
_["b"] = s,
88-
_["stringsAsFactors"] = false ); ')
89-
90-
)
91-
92-
}
93-
9425
.setUp <- function(){
9526
suppressMessages( require( datasets ) )
9627
data( iris )
97-
tests <- ".Rcpp.DataFrame"
98-
if( ! exists(tests, globalenv() )) {
99-
fun <- Rcpp:::compile_unit_tests( definitions() )
100-
assign( tests, fun, globalenv() )
101-
}
28+
sourceCpp( system.file( "unitTests/cpp/DataFrame.cpp" , package = "Rcpp" ) )
10229
}
10330

10431
test.DataFrame.FromSEXP <- function() {
10532
DF <- data.frame(a=1:3, b=c("a","b","c"))
106-
fun <- .Rcpp.DataFrame$FromSEXP
107-
checkEquals( fun(DF), DF, msg = "DataFrame pass-through")
33+
checkEquals( FromSEXP(DF), DF, msg = "DataFrame pass-through")
10834
}
10935

11036
test.DataFrame.index.byName <- function() {
11137
DF <- data.frame(a=1:3, b=c("a","b","c"))
112-
fun <- .Rcpp.DataFrame$index_byName
113-
checkEquals( fun(DF, "a"), DF$a, msg = "DataFrame column by name 'a'")
114-
checkEquals( fun(DF, "b"), DF$b, msg = "DataFrame column by name 'b'")
38+
checkEquals( index_byName(DF, "a"), DF$a, msg = "DataFrame column by name 'a'")
39+
checkEquals( index_byName(DF, "b"), DF$b, msg = "DataFrame column by name 'b'")
11540
}
11641

11742
test.DataFrame.index.byPosition <- function() {
11843
DF <- data.frame(a=1:3, b=c("a","b","c"))
119-
fun <- .Rcpp.DataFrame$index_byPosition
120-
checkEquals( fun(DF, 0), DF$a, msg = "DataFrame column by position 0")
121-
checkEquals( fun(DF, 1), DF$b, msg = "DataFrame column by position 1")
44+
checkEquals( index_byPosition(DF, 0), DF$a, msg = "DataFrame column by position 0")
45+
checkEquals( index_byPosition(DF, 1), DF$b, msg = "DataFrame column by position 1")
12246
}
12347

12448
test.DataFrame.string.element <- function() {
12549
DF <- data.frame(a=1:3, b=c("a","b","c"), stringsAsFactors=FALSE)
126-
fun <- .Rcpp.DataFrame$string_element
127-
checkEquals( fun(DF), DF[2,"b"], msg = "DataFrame string element")
50+
checkEquals( string_element(DF), DF[2,"b"], msg = "DataFrame string element")
12851
}
12952

13053
test.DataFrame.CreateOne <- function() {
13154
DF <- data.frame(a=1:3)
132-
fun <- .Rcpp.DataFrame$createOne
133-
checkEquals( fun(), DF, msg = "DataFrame create1")
55+
checkEquals( createOne(), DF, msg = "DataFrame create1")
13456
}
13557

13658
test.DataFrame.CreateTwo <- function() {
13759
DF <- data.frame(a=1:3, b=c("a","b","c"))
138-
fun <- .Rcpp.DataFrame$createTwo
139-
checkEquals( fun(), DF, msg = "DataFrame create2")
60+
checkEquals( createTwo(), DF, msg = "DataFrame create2")
14061
}
14162

14263
test.DataFrame.SlotProxy <- function(){
14364
setClass("track", representation(x="data.frame", y = "function"))
14465
tr1 <- new( "track", x = iris, y = rnorm )
145-
fun <- .Rcpp.DataFrame$SlotProxy
146-
checkTrue( identical( fun(tr1, "x"), iris ), msg = "DataFrame( SlotProxy )" )
147-
checkException( fun(tr1, "y"), msg = "DataFrame( SlotProxy ) -> exception" )
66+
checkTrue( identical( SlotProxy(tr1, "x"), iris ), msg = "DataFrame( SlotProxy )" )
67+
checkException( SlotProxy(tr1, "y"), msg = "DataFrame( SlotProxy ) -> exception" )
14868
}
14969

15070
test.DataFrame.AttributeProxy <- function(){
15171
tr1 <- structure( NULL, x = iris, y = rnorm )
152-
fun <- .Rcpp.DataFrame$AttributeProxy
153-
checkTrue( identical( fun(tr1, "x"), iris) , msg = "DataFrame( AttributeProxy )" )
154-
checkException( fun(tr1, "y"), msg = "DataFrame( AttributeProxy ) -> exception" )
72+
checkTrue( identical( AttributeProxy(tr1, "x"), iris) , msg = "DataFrame( AttributeProxy )" )
73+
checkException( AttributeProxy(tr1, "y"), msg = "DataFrame( AttributeProxy ) -> exception" )
15574
}
15675

15776
test.DataFrame.CreateTwo.stringsAsFactors <- function() {
15877
DF <- data.frame(a=1:3, b=c("a","b","c"), stringsAsFactors = FALSE )
159-
fun <- .Rcpp.DataFrame$createTwoStringsAsFactors
160-
checkEquals( fun(), DF, msg = "DataFrame create2 stringsAsFactors = false")
78+
checkEquals( createTwoStringsAsFactors(), DF, msg = "DataFrame create2 stringsAsFactors = false")
16179
}
16280

81+
test.DataFrame.nrows <- function(){
82+
checkEquals( DataFrame_nrows( iris ), nrow(iris) )
83+
}
84+
85+
16386
}

0 commit comments

Comments
 (0)