|
22 | 22 |
|
23 | 23 | if (.runThisTest) { |
24 | 24 |
|
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 | | - |
94 | 25 | .setUp <- function(){ |
95 | 26 | suppressMessages( require( datasets ) ) |
96 | 27 | 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" ) ) |
102 | 29 | } |
103 | 30 |
|
104 | 31 | test.DataFrame.FromSEXP <- function() { |
105 | 32 | 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") |
108 | 34 | } |
109 | 35 |
|
110 | 36 | test.DataFrame.index.byName <- function() { |
111 | 37 | 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'") |
115 | 40 | } |
116 | 41 |
|
117 | 42 | test.DataFrame.index.byPosition <- function() { |
118 | 43 | 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") |
122 | 46 | } |
123 | 47 |
|
124 | 48 | test.DataFrame.string.element <- function() { |
125 | 49 | 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") |
128 | 51 | } |
129 | 52 |
|
130 | 53 | test.DataFrame.CreateOne <- function() { |
131 | 54 | 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") |
134 | 56 | } |
135 | 57 |
|
136 | 58 | test.DataFrame.CreateTwo <- function() { |
137 | 59 | 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") |
140 | 61 | } |
141 | 62 |
|
142 | 63 | test.DataFrame.SlotProxy <- function(){ |
143 | 64 | setClass("track", representation(x="data.frame", y = "function")) |
144 | 65 | 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" ) |
148 | 68 | } |
149 | 69 |
|
150 | 70 | test.DataFrame.AttributeProxy <- function(){ |
151 | 71 | 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" ) |
155 | 74 | } |
156 | 75 |
|
157 | 76 | test.DataFrame.CreateTwo.stringsAsFactors <- function() { |
158 | 77 | 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") |
161 | 79 | } |
162 | 80 |
|
| 81 | +test.DataFrame.nrows <- function(){ |
| 82 | + checkEquals( DataFrame_nrows( iris ), nrow(iris) ) |
| 83 | +} |
| 84 | + |
| 85 | + |
163 | 86 | } |
0 commit comments