Skip to content
Snippets Groups Projects
Commit d30405f1 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add failure tests for OStream and OFStream

parent dbab0a34
Branches
No related tags found
1 merge request!111Add ofstream support within the language
......@@ -6,7 +6,12 @@ OFStream::OFStream(const std::string& filename)
{
if (parallel::rank() == 0) {
m_fstream.open(filename);
Assert(m_ostream == nullptr, "ostream was already defined");
if (m_fstream.is_open()) {
m_ostream = &m_fstream;
} else {
std::stringstream error_msg;
error_msg << "cannot create file " << rang::fgB::yellow << filename << rang::style::reset;
throw NormalError(error_msg.str());
}
}
}
......@@ -16,7 +16,7 @@ class OStream
friend std::shared_ptr<const OStream>
operator<<(const std::shared_ptr<const OStream>& os, const DataT& t)
{
Assert(os.use_count() > 0);
Assert(os.use_count() > 0, "non allocated stream");
if (os->m_ostream != nullptr) {
*os->m_ostream << t;
}
......
......@@ -46,4 +46,13 @@ TEST_CASE("OFStream", "[language]")
REQUIRE(not std::filesystem::exists(filename));
}
SECTION("invalid filename")
{
if (parallel::rank() == 0) {
const std::string filename = "badpath/invalidpath/ofstream";
REQUIRE_THROWS_WITH(std::make_shared<OFStream>(filename), "error: cannot create file " + filename);
}
}
}
......@@ -25,4 +25,12 @@ TEST_CASE("OStream", "[language]")
REQUIRE(sstr.str() == "foo3 bar");
}
#ifndef NDEBUG
SECTION("non allocated stream")
{
std::shared_ptr<OStream> bad_os;
REQUIRE_THROWS_WITH((bad_os << 2), "non allocated stream");
}
#endif // NDEBUG
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment