libMesh
Public Member Functions | Private Member Functions | List of all members
XdrTest Class Reference
Inheritance diagram for XdrTest:
[legend]

Public Member Functions

 LIBMESH_CPPUNIT_TEST_SUITE (XdrTest)
 
 CPPUNIT_TEST (testDataVec)
 
 CPPUNIT_TEST (testDataStream)
 
 CPPUNIT_TEST_SUITE_END ()
 
void setUp ()
 
void tearDown ()
 
void testDataVec ()
 
void testDataStream ()
 

Private Member Functions

template<typename ReadLambda , typename WriteLambda >
void test_read_write (ReadLambda &act_read, WriteLambda &act_write)
 

Detailed Description

Definition at line 17 of file xdr_test.C.

Member Function Documentation

◆ CPPUNIT_TEST() [1/2]

XdrTest::CPPUNIT_TEST ( testDataVec  )

◆ CPPUNIT_TEST() [2/2]

XdrTest::CPPUNIT_TEST ( testDataStream  )

◆ CPPUNIT_TEST_SUITE_END()

XdrTest::CPPUNIT_TEST_SUITE_END ( )

◆ LIBMESH_CPPUNIT_TEST_SUITE()

XdrTest::LIBMESH_CPPUNIT_TEST_SUITE ( XdrTest  )

◆ setUp()

void XdrTest::setUp ( )
inline

Definition at line 76 of file xdr_test.C.

77  {}

◆ tearDown()

void XdrTest::tearDown ( )
inline

Definition at line 79 of file xdr_test.C.

80  {}

◆ test_read_write()

template<typename ReadLambda , typename WriteLambda >
void XdrTest::test_read_write ( ReadLambda &  act_read,
WriteLambda &  act_write 
)
inlineprivate

Definition at line 28 of file xdr_test.C.

References libMesh::global_processor_id(), TIMPI::Communicator::rank(), libMesh::READ, TestCommWorld, and libMesh::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  }
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
processor_id_type rank() const
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:67
processor_id_type global_processor_id()
Definition: libmesh_base.h:85

◆ testDataStream()

void XdrTest::testDataStream ( )
inline

Definition at line 103 of file xdr_test.C.

References libMesh::index_range(), libMesh::Real, and libMesh::TOLERANCE.

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  }
void test_read_write(ReadLambda &act_read, WriteLambda &act_write)
Definition: xdr_test.C:28
static constexpr Real TOLERANCE
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:67
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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:117

◆ testDataVec()

void XdrTest::testDataVec ( )
inline

Definition at line 82 of file xdr_test.C.

References libMesh::index_range(), libMesh::pi, and libMesh::TOLERANCE.

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  }
void test_read_write(ReadLambda &act_read, WriteLambda &act_write)
Definition: xdr_test.C:28
static constexpr Real TOLERANCE
This class implements a C++ interface to the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:67
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:117
const Real pi
.
Definition: libmesh.h:299

The documentation for this class was generated from the following file: