22TEST_CASE(
"Reading space delimited numeric data",
"[ioutils::readColumns]" )
24 SECTION(
"a single column of floating point numbers" )
26 std::string fname =
"/tmp/readcol_test_single_col_float.dat";
29 fout <<
"#a commment\n";
32 fout <<
"4.96 #with comment\n";
37 SECTION(
"reading as int" )
39 std::vector<int> data0;
42 REQUIRE( data0.size() == 5 );
43 REQUIRE( data0[0] == 1 );
44 REQUIRE( data0[1] == 2 );
45 REQUIRE( data0[2] == 4 );
46 REQUIRE( data0[3] == 5 );
47 REQUIRE( data0[4] == 1 );
50 SECTION(
"reading as float" )
52 std::vector<float> data0;
55 REQUIRE( data0.size() == 5 );
56 REQUIRE_THAT( data0[0], WithinRel( 1.23, 1e-5 ) );
57 REQUIRE_THAT( data0[1], WithinRel( 2.15, 1e-5 ) );
58 REQUIRE_THAT( data0[2], WithinRel( 4.96, 1e-5 ) );
59 REQUIRE_THAT( data0[3], WithinRel( 5.23, 1e-5 ) );
60 REQUIRE_THAT( data0[4], WithinRel( 1e-9, 1e-5 ) );
64 SECTION(
"a two columns of floating point numbers" )
66 std::string fname =
"/tmp/readcol_test_two_cols_float.dat";
69 fout <<
"#a commment\n";
70 fout <<
"1.23 6.78\n";
71 fout <<
"2.15 8.88\n";
72 fout <<
"4.96 -2.33#with comment\n";
75 fout <<
"5.23 9.9e5\n";
76 fout <<
"1e-9 -5.6e2\n";
77 fout <<
" #comment at end not on first char and no newline";
80 SECTION(
"reading both as int" )
82 std::vector<int> data0, data1;
85 REQUIRE( data0.size() == 5 );
86 REQUIRE( data0[0] == 1 );
87 REQUIRE( data0[1] == 2 );
88 REQUIRE( data0[2] == 4 );
89 REQUIRE( data0[3] == 5 );
90 REQUIRE( data0[4] == 1 );
92 REQUIRE( data1.size() == 5 );
93 REQUIRE( data1[0] == 6 );
94 REQUIRE( data1[1] == 8 );
95 REQUIRE( data1[2] == -2 );
96 REQUIRE( data1[3] == 9 );
97 REQUIRE( data1[4] == -5 );
100 SECTION(
"reading both as float" )
102 std::vector<float> data0, data1;
105 REQUIRE( data0.size() == 5 );
106 REQUIRE_THAT( data0[0], WithinRel( 1.23, 1e-5 ) );
107 REQUIRE_THAT( data0[1], WithinRel( 2.15, 1e-5 ) );
108 REQUIRE_THAT( data0[2], WithinRel( 4.96, 1e-5 ) );
109 REQUIRE_THAT( data0[3], WithinRel( 5.23, 1e-5 ) );
110 REQUIRE_THAT( data0[4], WithinRel( 1e-9, 1e-5 ) );
112 REQUIRE( data1.size() == 5 );
113 REQUIRE_THAT( data1[0], WithinRel( 6.78, 1e-5 ) );
114 REQUIRE_THAT( data1[1], WithinRel( 8.88, 1e-5 ) );
115 REQUIRE_THAT( data1[2], WithinRel( -2.33, 1e-5 ) );
116 REQUIRE_THAT( data1[3], WithinRel( 9.9e5, 1e-5 ) );
117 REQUIRE_THAT( data1[4], WithinRel( -5.6e2, 1e-5 ) );
167TEST_CASE(
"Reading combined type data",
"[ioutils::readColumns]" )
169 SECTION(
"a single column of floating point numbers" )
171 std::string fname =
"/tmp/readcol_test_combos.dat";
174 fout <<
"test1 1.23 2.56\n";
175 fout <<
"test2 2.15 8.93\n";
176 fout <<
"test3 0 0\n";
177 fout <<
"test4 6.7 1\n";
178 fout <<
"test5 0 22.2\n";
179 fout <<
"test6 1e-4 0\n";
182 std::vector<std::string> str0;
183 std::vector<float> data1, data2;
185 mx::ioutils::readColumns<mx::ioutils::readColSpaceDelim, mx::verbose::vv>( fname, str0, data1, data2 );
189 REQUIRE( str0[0] ==
"test1" );
190 REQUIRE( str0[1] ==
"test2" );
191 REQUIRE( str0[2] ==
"test3" );
192 REQUIRE( str0[3] ==
"test4" );
193 REQUIRE( str0[4] ==
"test5" );
194 REQUIRE( str0[5] ==
"test6" );
196 REQUIRE_THAT( data1[0], WithinRel( 1.23, 1e-7 ) );
197 REQUIRE_THAT( data1[1], WithinRel( 2.15, 1e-7 ) );
198 REQUIRE( data1[2] == 0 );
199 REQUIRE_THAT( data1[3], WithinRel( 6.7, 1e-7 ) );
200 REQUIRE( data1[4] == 0 );
201 REQUIRE_THAT( data1[5], WithinRel( 1e-4, 1e-7 ) );
203 REQUIRE_THAT( data2[0], WithinRel( 2.56, 1e-7 ) );
204 REQUIRE_THAT( data2[1], WithinRel( 8.93, 1e-7 ) );
205 REQUIRE( data2[2] == 0 );
206 REQUIRE_THAT( data2[3], WithinRel( 1, 1e-7 ) );
207 REQUIRE_THAT( data2[4], WithinRel( 22.2, 1e-7 ) );
208 REQUIRE( data2[5] == 0 );