[Rcpp-commits] r3219 - in pkg/Rcpp: . inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Oct 26 03:57:45 CEST 2011


Author: edd
Date: 2011-10-26 03:57:37 +0200 (Wed, 26 Oct 2011)
New Revision: 3219

Added:
   pkg/Rcpp/inst/unitTests/runit.rcout.R
Modified:
   pkg/Rcpp/ChangeLog
Log:
added simple unit test based on proposed test by Jelmer


Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2011-10-26 01:34:00 UTC (rev 3218)
+++ pkg/Rcpp/ChangeLog	2011-10-26 01:57:37 UTC (rev 3219)
@@ -7,6 +7,8 @@
 	* include/include/Rcpp/iostream/Rstreambuf.h: Idem
 	* include/include/Rcpp.h:  Include new headers
 
+	* inst/unitTests/runit.rcout.R: Added simple unit test
+
 2011-09-29  Dirk Eddelbuettel  <edd at debian.org>
 
         * DESCRIPTION: Release 0.9.7

Added: pkg/Rcpp/inst/unitTests/runit.rcout.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.rcout.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.rcout.R	2011-10-26 01:57:37 UTC (rev 3219)
@@ -0,0 +1,66 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2011	Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
+#
+# 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/>.
+
+test.rcout <- function() {
+
+    src <- '
+        std::string tfile = as<std::string>(tmpfile);
+
+	// define and open testfile
+        std::ofstream testfile(tfile.c_str());
+
+        // save output buffer of the Rcout stream
+        std::streambuf* Rcout_buffer = Rcout.rdbuf();
+
+        // redirect ouput into testfile
+        Rcout.rdbuf( testfile.rdbuf() );
+
+	// write a test string to the file
+        Rcout << as<std::string>( teststring ) << std::endl;
+
+        // restore old output buffer
+        Rcout.rdbuf(Rcout_buffer);
+
+	// close testfile
+	testfile.close();
+
+        return Rcpp::wrap<int>( 0 );
+'
+
+    fun <- cxxfunction(signature(tmpfile="character",
+                                 teststring = "character" ),
+                       includes = "#include <iostream>\n#include <fstream>",
+                       body = src, plugin="Rcpp")
+
+    ## define test string that is written to two files
+    teststr <- "First line.\nSecond line."
+
+    rcppfile <- tempfile()
+    rfile <- tempfile()
+
+    ## write to test_rcpp.txt from Rcpp
+    fun(rcppfile,  teststr )
+
+    ## write to test_r.txt from R
+    cat( teststr, file=rfile, sep='\n' )
+
+    ## compare whether the two files have the same data
+    checkEquals( readLines(rcppfile), readLines(rfile), msg="Rcout output")
+
+}



More information about the Rcpp-commits mailing list