libMesh
Loading...
Searching...
No Matches
xdr_test.C
Go to the documentation of this file.
1// Unit test includes
2#include "libmesh_cppunit.h"
3#include "test_comm.h"
4
5// libMesh includes
6#include <libmesh/libmesh.h>
7#include <libmesh/xdr_cxx.h>
8#include <libmesh/int_range.h>
10
11// C++ includes
12#include <vector>
13#include <sstream>
14
15using namespace libMesh;
16
17class XdrTest : public CppUnit::TestCase {
18public:
20
23
25
26private:
27 template <typename ReadLambda, typename WriteLambda>
28 void test_read_write(ReadLambda & act_read, WriteLambda & act_write)
29 {
30 // There was once a weird bug mutating our TestCommWorld in
31 // a previous unit test, which turned *this* test into a race
32 // condition. Let's make sure that never happens again.
33 CPPUNIT_ASSERT_EQUAL(TestCommWorld->rank(),
35
36 // Test reading/writing on 1 processor
37 if (TestCommWorld->rank() == 0)
38 {
39 // Write to file
40 {
41 Xdr xdr("output.dat", WRITE);
42 act_write(xdr);
43 }
44
45 // Read from file
46 {
47 Xdr xdr("output.dat", READ);
48 act_read(xdr);
49 }
50 }
51
52 // Test reading/writing to a stream directly on all processors
53 {
54 std::stringstream stream;
55
56 // Write to stream
57 {
58 std::ostream ostream(stream.rdbuf());
59 Xdr xdr(ostream);
60 act_write(xdr);
61 }
62
63 // Rewind stream
64 stream.seekp(0);
65
66 // Read from stream
67 {
68 std::istream istream(stream.rdbuf());
69 Xdr xdr(istream);
70 act_read(xdr);
71 }
72 }
73 }
74
75public:
76 void setUp()
77 {}
78
79 void tearDown()
80 {}
81
83 {
84 LOG_UNIT_TEST;
85
86 std::vector<Real> vec = {libMesh::pi, 2*libMesh::pi, 3*libMesh::pi};
87
88 auto act_write = [&vec](Xdr & xdr) { xdr.data(vec, "# This is a comment"); };
89 auto act_read = [&vec](Xdr & xdr) {
90 std::vector<Real> vec_in;
91 xdr.data(vec_in);
92
93 // Check that correct number of values were read in
94 CPPUNIT_ASSERT_EQUAL(vec_in.size(), vec.size());
95
96 // Check that values were written/read with sufficient accuracy
97 for (auto i : index_range(vec_in))
98 LIBMESH_ASSERT_FP_EQUAL(vec[i], vec_in[i], TOLERANCE); };
99
100 test_read_write(act_read, act_write);
101 }
102
104 {
105 LOG_UNIT_TEST;
106
107 std::vector<Real> vec(100);
108 for (auto i : index_range(vec))
109 vec[i] = static_cast<Real>(i+1) / vec.size();
110
111 // Write. If "line_break" does not exactly divide the vector size,
112 // there will be one line with fewer values than the others.
113 auto act_write = [&vec](Xdr & xdr) {
114 xdr.data_stream(vec.data(), vec.size(), /*line_break=*/16); };
115 // Read. To use data_stream(), the storage needs to be pre-sized
116 // and the line_break parameter is ignored.
117 auto act_read = [&vec](Xdr & xdr) {
118 std::vector<Real> vec_in(100);
119 xdr.data_stream(vec_in.data(), vec_in.size());
120
121 // Check that values were written/read correctly
122 for (auto i : index_range(vec_in))
123 LIBMESH_ASSERT_FP_EQUAL(vec[i], vec_in[i], TOLERANCE); };
124
125 test_read_write(act_read, act_write);
126 }
127};
128
void setUp()
Definition xdr_test.C:76
CPPUNIT_TEST(testDataVec)
void tearDown()
Definition xdr_test.C:79
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(testDataStream)
LIBMESH_CPPUNIT_TEST_SUITE(XdrTest)
void testDataVec()
Definition xdr_test.C:82
void test_read_write(ReadLambda &act_read, WriteLambda &act_write)
Definition xdr_test.C:28
void testDataStream()
Definition xdr_test.C:103
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition xdr_cxx.h:68
Communicator * TestCommWorld
The libMesh namespace provides an interface to certain functionality in the library.
processor_id_type global_processor_id()
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
const Real pi
.
Definition libmesh.h:292
static constexpr Real TOLERANCE
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
CPPUNIT_TEST_SUITE_REGISTRATION(XdrTest)