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

Add tests for OFStream

parent bec8f051
No related branches found
No related tags found
1 merge request!111Add ofstream support within the language
......@@ -5,7 +5,7 @@
#include <fstream>
class OFStream : public OStream
class OFStream final : public OStream
{
private:
std::ofstream m_fstream;
......@@ -13,7 +13,7 @@ class OFStream : public OStream
public:
OFStream(const std::string& filename);
OFStream() = default;
OFStream() = delete;
~OFStream() = default;
};
......
......@@ -115,6 +115,7 @@ add_executable (mpi_unit_tests
test_ItemValue.cpp
test_ItemValueUtils.cpp
test_Messenger.cpp
test_OFStream.cpp
test_Partitioner.cpp
test_RandomEngine.cpp
test_SubItemValuePerItem.cpp
......
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp>
#include <language/utils/OFStream.hpp>
#include <utils/Messenger.hpp>
#include <filesystem>
// clazy:excludeall=non-pod-global-static
TEST_CASE("OFStream", "[language]")
{
SECTION("ofstream")
{
const std::string basename = "ofstream_";
const std::string filename = basename + std::to_string(parallel::rank());
// Ensures that the file is closed after this line
std::make_shared<OFStream>(filename) << "foo" << 3 << " bar\n";
if (parallel::rank() == 0) {
REQUIRE(std::filesystem::is_regular_file(filename));
std::ifstream is(filename);
char file_content[10];
for (size_t i = 0; i < 10; ++i) {
char c = is.get();
file_content[i] = c;
if (c == '\n') {
file_content[i + 1] = '\0';
REQUIRE(i == 8);
c = is.get();
REQUIRE(is.eof());
break;
}
}
std::string content = file_content;
REQUIRE(content == "foo3 bar\n");
std::filesystem::remove(filename);
}
REQUIRE(not std::filesystem::exists(filename));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment