[Rcpp-commits] r1601 - pkg/Rcpp/inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 18 15:22:41 CEST 2010


Author: edd
Date: 2010-06-18 15:22:40 +0200 (Fri, 18 Jun 2010)
New Revision: 1601

Modified:
   pkg/Rcpp/inst/unitTests/runit.DataFrame.R
Log:
added some tests for string access via data.frame


Modified: pkg/Rcpp/inst/unitTests/runit.DataFrame.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.DataFrame.R	2010-06-18 12:04:23 UTC (rev 1600)
+++ pkg/Rcpp/inst/unitTests/runit.DataFrame.R	2010-06-18 13:22:40 UTC (rev 1601)
@@ -33,27 +33,43 @@
 
 test.DataFrame.index.byName <- function() {
     DF <- data.frame(a=1:3, b=c("a","b","c"))
-    fun <- cppfunction( signature(x='ANY'), '
-	DataFrame df(x) ;
-	return df["a"];
+    fun <- cppfunction( signature(x='ANY', y='character'), '
+	DataFrame df(x);
+        std::string s = as<std::string>(y);
+	return df[s];
     ' )
-    checkEquals( fun(DF), DF$a, msg = "DataFrame column by name")
+    checkEquals( fun(DF, "a"), DF$a, msg = "DataFrame column by name 'a'")
+    checkEquals( fun(DF, "b"), DF$b, msg = "DataFrame column by name 'b'")
 }
 
 test.DataFrame.index.byPosition <- function() {
     DF <- data.frame(a=1:3, b=c("a","b","c"))
+    fun <- cppfunction( signature(x='ANY', y='integer'), '
+	DataFrame df(x);
+        int i = as<int>(y);
+	return df[i];
+    ' )
+    checkEquals( fun(DF, 0), DF$a, msg = "DataFrame column by position 0")
+    checkEquals( fun(DF, 1), DF$b, msg = "DataFrame column by position 1")
+}
+
+test.DataFrame.string.element <- function() {
+    DF <- data.frame(a=1:3, b=c("a","b","c"), stringsAsFactors=FALSE)
     fun <- cppfunction( signature(x='ANY'), '
-	DataFrame df(x) ;
-	return df[0];
+        DataFrame df(x);
+        CharacterVector b = df[1];
+        std::string s;
+        s = b[1];
+        return wrap(s);
     ' )
-    checkEquals( fun(DF), DF$a, msg = "DataFrame column by position")
+    checkEquals( fun(DF), DF[2,"b"], msg = "DataFrame string element")
 }
 
 test.DataFrame.CreateOne <- function() {
     DF <- data.frame(a=1:3)
     fun <- cppfunction( signature(), '
         IntegerVector v = IntegerVector::create(1,2,3);
-		return DataFrame::create(Named("a")=v);
+        return DataFrame::create(Named("a")=v);
     ' )
     checkEquals( fun(), DF, msg = "DataFrame create1")
 }



More information about the Rcpp-commits mailing list