From dbab0a344d13400a0791357b6b2335d46ce52525 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Fri, 20 Aug 2021 11:09:45 +0200 Subject: [PATCH] Add tests for OFStream --- src/language/utils/OFStream.hpp | 4 +-- tests/CMakeLists.txt | 1 + tests/test_OFStream.cpp | 49 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/test_OFStream.cpp diff --git a/src/language/utils/OFStream.hpp b/src/language/utils/OFStream.hpp index 589f0fe27..8421906fc 100644 --- a/src/language/utils/OFStream.hpp +++ b/src/language/utils/OFStream.hpp @@ -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; }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 56386d8d7..714bb23bf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 diff --git a/tests/test_OFStream.cpp b/tests/test_OFStream.cpp new file mode 100644 index 000000000..6170ad55f --- /dev/null +++ b/tests/test_OFStream.cpp @@ -0,0 +1,49 @@ +#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)); + } +} -- GitLab