Nektar++
Loading...
Searching...
No Matches
TestRootTableWriter.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestRootTableWriter.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Unit test for the table writer of the SolverCore filters:
32// every row ended with EndRow() is in the file while the writer is still
33// open, so a run that terminates abnormally keeps its completed rows.
34//
35///////////////////////////////////////////////////////////////////////////////
36
37#define BOOST_TEST_MODULE TestRootTableWriter
38
41
42#include <SolverCore/Filters/FilterUtils.h>
43
46
47#include <fstream>
48#include <iomanip>
49#include <sstream>
50
51using namespace Nektar;
52using namespace Nektar::SolverCore;
53
55
57
58namespace
59{
60
61const std::string tablePath = "root_table_writer.tbl";
62
63// Reads the table through a stream of its own, as another process reading
64// the file would, without closing or flushing the writer.
65std::string ReadTable()
66{
67 std::ifstream file(tablePath);
68 BOOST_REQUIRE(file.good());
69 std::ostringstream contents;
70 contents << file.rdbuf();
71 return contents.str();
72}
73
74} // namespace
75
76BOOST_AUTO_TEST_CASE(RootTableWriter_RowsReachFileBeforeClose)
77{
78 fs::remove(tablePath);
79
80 auto comm =
81 LibUtilities::GetCommFactory().CreateInstance("Serial", 0, nullptr);
82 RootTableWriter writer(comm);
83 BOOST_REQUIRE(writer.IsWriter());
84 writer.Open(tablePath);
85
86 // The expected contents, written with the formatting Open() sets up.
87 std::ostringstream expected;
88 expected << std::scientific << std::setprecision(16);
89
90 writer.Metadata("Table of test rows");
91 expected << "# Table of test rows\n";
92 BOOST_CHECK_EQUAL(ReadTable(), expected.str());
93
94 writer.Columns({"Time", "Value"});
95 expected << "Time Value\n";
96 BOOST_CHECK_EQUAL(ReadTable(), expected.str());
97
98 // Each row is far shorter than the stream buffer, so it is only in the
99 // file if EndRow() flushed it.
100 for (int row = 0; row < 3; ++row)
101 {
102 const double time = 0.25 * row;
103 const double value = 1.0 / (row + 3.0);
104 writer.Stream() << time << " " << value;
105 writer.EndRow();
106 expected << time << " " << value << "\n";
107 BOOST_CHECK_EQUAL(ReadTable(), expected.str());
108 }
109
110 // Rows ended with a newline reach the file together at Flush().
111 for (int row = 3; row < 5; ++row)
112 {
113 const double time = 0.25 * row;
114 const double value = 1.0 / (row + 3.0);
115 writer.Stream() << time << " " << value << '\n';
116 expected << time << " " << value << "\n";
117 }
118 writer.Flush();
119 BOOST_CHECK_EQUAL(ReadTable(), expected.str());
120
121 writer.Close();
122 BOOST_CHECK_EQUAL(ReadTable(), expected.str());
123
124 fs::remove(tablePath);
125}
#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args)
tBasePtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
CommFactory & GetCommFactory()
BOOST_AUTO_TEST_CASE(TestCanGetRawPtr)