1010
1111// C++ includes
1212#include <vector>
13+ #include <sstream>
1314
1415using namespace libMesh ;
1516
@@ -23,20 +24,9 @@ public:
2324 CPPUNIT_TEST_SUITE_END ();
2425
2526private :
26-
27- public :
28- void setUp ()
29- {}
30-
31- void tearDown ()
32- {}
33-
34- void testDataVec ()
27+ template < typename ReadLambda , typename WriteLambda >
28+ void test_read_write (ReadLambda & act_read , WriteLambda & act_write )
3529 {
36- LOG_UNIT_TEST ;
37-
38- std ::vector < Real > vec = {libMesh ::pi , 2 * libMesh ::pi , 3 * libMesh ::pi };
39-
4030 // There was once a weird bug mutating our TestCommWorld in
4131 // a previous unit test, which turned *this* test into a race
4232 // condition. Let's make sure that never happens again.
@@ -49,23 +39,65 @@ public:
4939 // Write to file
5040 {
5141 Xdr xdr ("output.dat" , WRITE );
52- xdr . data ( vec , "# This is a comment" );
42+ act_write ( xdr );
5343 }
5444
5545 // Read from file
5646 {
5747 Xdr xdr ("output.dat" , READ );
58- std ::vector < Real > vec_in ;
59- xdr .data (vec_in );
48+ act_read (xdr );
49+ }
50+ }
6051
61- // Check that correct number of values were read in
62- CPPUNIT_ASSERT_EQUAL (vec_in .size (), vec .size ());
52+ // Test reading/writing to a stream directly on all processors
53+ {
54+ std ::stringstream stream ;
6355
64- // Check that values were written/read with sufficient accuracy
65- for (auto i : index_range (vec_in ))
66- LIBMESH_ASSERT_FP_EQUAL (vec [i ], vec_in [i ], TOLERANCE );
67- }
56+ // Write to stream
57+ {
58+ std ::ostream ostream (stream .rdbuf ());
59+ Xdr xdr (ostream );
60+ act_write (xdr );
6861 }
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+
75+ public :
76+ void setUp ()
77+ {}
78+
79+ void tearDown ()
80+ {}
81+
82+ void testDataVec ()
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 );
69101 }
70102
71103 void testDataStream ()
@@ -76,29 +108,21 @@ public:
76108 for (auto i : index_range (vec ))
77109 vec [i ] = static_cast < Real > (i + 1 ) / vec .size ();
78110
79- // Test reading/writing on 1 processor
80- if (TestCommWorld -> rank () == 0 )
81- {
82- // Write to file. If "line_break" does not exactly divide the
83- // vector size, there will be one line with fewer values than
84- // the others.
85- {
86- Xdr xdr ("output.dat" , WRITE );
87- xdr .data_stream (vec .data (), vec .size (), /*line_break=*/ 16 );
88- }
89-
90- // Read from file. To use data_stream(), the storage needs to
91- // be pre-sized and the line_break parameter is ignored.
92- {
93- Xdr xdr ("output.dat" , READ );
94- std ::vector < Real > vec_in (100 );
95- xdr .data_stream (vec_in .data (), vec_in .size ());
96-
97- // Check that values were written/read correctly
98- for (auto i : index_range (vec_in ))
99- LIBMESH_ASSERT_FP_EQUAL (vec [i ], vec_in [i ], TOLERANCE );
100- }
101- }
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 );
102126 }
103127};
104128
0 commit comments