I have a C++ code that has a lot of functions which receives ostream as argument. I wanted to unit test those functions, for that I have to verify ostream object data after execution to the function. I can redirect output stream to a file but I wanted to check if I can create a temporary buffer and redirect the output stream to the buffer and read from that buffer.
You can use std::stringstream
as an in memory std::ostream
:
#include <iosfwd>
#include <sstream>
#include <cassert>
void my_func(std::ostream& out) {
out << "test";
}
int main() {
std::ostringstream buf;
my_func(buf);
assert(buf.str() == "test");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With