Skip to content

Commit c9dc0e2

Browse files
committed
merging PR #398
2 parents f99eb4b + e7da0e7 commit c9dc0e2

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2015-11-11 Qiang Kou <[email protected]>
2+
3+
* include/Rcpp/complex.h: operator<< for Rcomplex
4+
15
2015-11-10 Dirk Eddelbuettel <[email protected]>
26

37
* inst/include/Rcpp/vector/Matrix.h: Added transpose for character

inst/include/Rcpp/complex.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ inline bool operator==( const Rcomplex& a, const Rcomplex& b){
6565
return a.r == b.r && a.i == b.i ;
6666
}
6767

68+
inline std::ostream & operator<<(std::ostream &os, const Rcomplex& cplx ){
69+
return os << cplx.r << "+" << cplx.i << "i" ;
70+
}
71+
6872
#endif

inst/unitTests/cpp/misc.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ void test_rcout(std::string tfile, std::string teststring) {
107107
testfile.close();
108108
}
109109

110+
// [[Rcpp::export]]
111+
void test_rcout_rcomplex(std::string tfile, SEXP rc) {
112+
Rcomplex rx = Rcpp::as<Rcomplex>(rc);
113+
// define and open testfile
114+
std::ofstream testfile(tfile.c_str());
115+
116+
// save output buffer of the Rcout stream
117+
std::streambuf* Rcout_buffer = Rcout.rdbuf();
118+
119+
// redirect ouput into testfile
120+
Rcout.rdbuf( testfile.rdbuf() );
121+
122+
// write a test string to the file
123+
Rcout << rx << std::endl;
124+
125+
// restore old output buffer
126+
Rcout.rdbuf(Rcout_buffer);
127+
128+
// close testfile
129+
testfile.close();
130+
}
131+
110132
// [[Rcpp::export]]
111133
LogicalVector na_proxy() {
112134
CharacterVector s("foo");

inst/unitTests/runit.misc.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ if (.runThisTest) {
118118
checkEquals( readLines(rcppfile), readLines(rfile), msg="Rcout output")
119119
}
120120

121+
test.rcout.complex <- function(){
122+
123+
rcppfile <- tempfile()
124+
rfile <- tempfile()
125+
126+
z <- complex(real=sample(1:10, 1), imaginary=sample(1:10, 1))
127+
128+
## write to test_rcpp.txt from Rcpp
129+
test_rcout_rcomplex(rcppfile, z )
130+
131+
## write to test_r.txt from R
132+
cat( z, file=rfile, sep='\n' )
133+
134+
## compare whether the two files have the same data
135+
checkEquals( readLines(rcppfile), readLines(rfile), msg="Rcout Rcomplex")
136+
}
137+
121138
test.na_proxy <- function(){
122139
checkEquals(
123140
na_proxy(),

0 commit comments

Comments
 (0)