mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
readColumns_test.cpp
Go to the documentation of this file.
1/** \file readColumns_test.cpp
2 */
3#include "../../catch2/catch.hpp"
4
5#define MX_NO_ERROR_REPORTS
6
7#include "../../../include/ioutils/readColumns.hpp"
8
9using namespace Catch::Matchers;
10
11namespace unitTest
12{
13namespace ioutilsTest
14{
15namespace readColumnsTest
16{
17
18/// Reading space delimited numeric data
19/**
20 * \ingroup readColumns_unit_tests
21 */
22TEST_CASE( "Reading space delimited numeric data", "[ioutils::readColumns]" )
23{
24 SECTION( "a single column of floating point numbers" )
25 {
26 std::string fname = "/tmp/readcol_test_single_col_float.dat";
27 std::ofstream fout;
28 fout.open( fname );
29 fout << "#a commment\n";
30 fout << "1.23\n";
31 fout << "2.15\n";
32 fout << "4.96 #with comment\n";
33 fout << "5.23\n";
34 fout << "1e-9"; // no newline on last line
35 fout.close();
36
37 SECTION( "reading as int" )
38 {
39 std::vector<int> data0;
40 mx::error_t errc = mx::ioutils::readColumns( fname, data0 );
41 REQUIRE( errc == mx::error_t::noerror );
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 ); // this is a consequence of stoi
48 }
49
50 SECTION( "reading as float" )
51 {
52 std::vector<float> data0;
53 mx::error_t errc = mx::ioutils::readColumns( fname, data0 );
54 REQUIRE( errc == mx::error_t::noerror );
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 ) );
61 }
62 }
63
64 SECTION( "a two columns of floating point numbers" )
65 {
66 std::string fname = "/tmp/readcol_test_two_cols_float.dat";
67 std::ofstream fout;
68 fout.open( fname );
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";
73 fout << "\n"; // empty line
74 fout << " \n"; // blank line
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";
78 fout.close();
79
80 SECTION( "reading both as int" )
81 {
82 std::vector<int> data0, data1;
83 mx::error_t errc = mx::ioutils::readColumns( fname, data0, data1 );
84 REQUIRE( errc == mx::error_t::noerror );
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 ); // this is a consequence of stoi
91
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 ); // this is a consequence of stoi
97 REQUIRE( data1[4] == -5 ); // this is a consequence of stoi
98 }
99
100 SECTION( "reading both as float" )
101 {
102 std::vector<float> data0, data1;
103 mx::error_t errc = mx::ioutils::readColumns( fname, data0, data1 );
104 REQUIRE( errc == mx::error_t::noerror );
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 ) );
111
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 ) );
118 }
119 }
120}
121
122/// Reading space delimited numeric data with errors
123/**
124 * \ingroup readColumns_unit_tests
125 */
126TEST_CASE( "Reading space delimited numeric data with errors", "[ioutils::readColumns]" )
127{
128 SECTION( "two columns of floating point numbers" )
129 {
130 std::string fname = "/tmp/readcol_test_two_col_float_intoverflow.dat";
131 std::ofstream fout;
132 fout.open( fname );
133 fout << "1.23 6.7\n";
134 fout << "2.15 8.8\n";
135 fout << "4.96 9.9\n";
136 fout << "5.23 10.11\n";
137 fout << "1e-9 55555555555555555555555555555555555\n";
138 fout.close();
139
140 SECTION( "reading as int" )
141 {
142 std::vector<int> data0, data1;
143 mx::error_t errc =
144 mx::ioutils::readColumns<mx::ioutils::readColSpaceDelim, mx::verbose::v>( fname, data0, data1 );
145 REQUIRE( errc == mx::error_t::erange );
146 }
147
148 /*SECTION( "reading as int64_t" )
149 {
150 std::vector<int64_t> data0, data1;
151 mx::error_t errc = mx::ioutils::readColumns(fname, data0, data1);
152 REQUIRE( errc == mx::error_t::std_out_of_range );
153 }*/
154 }
155
156#if 0 // for doxygen
157 std::string fname;
158 std::vector<int> data0, data1;
159 mx::ioutils::readColumns( fname, data0, data1 );
160#endif
161}
162
163/// Reading combined type data
164/**
165 * \ingroup readColumns_unit_tests
166 */
167TEST_CASE( "Reading combined type data", "[ioutils::readColumns]" )
168{
169 SECTION( "a single column of floating point numbers" )
170 {
171 std::string fname = "/tmp/readcol_test_combos.dat";
172 std::ofstream fout;
173 fout.open( fname );
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";
180 fout.close();
181
182 std::vector<std::string> str0;
183 std::vector<float> data1, data2;
184 mx::error_t errc =
185 mx::ioutils::readColumns<mx::ioutils::readColSpaceDelim, mx::verbose::vv>( fname, str0, data1, data2 );
186
187 REQUIRE( errc == mx::error_t::noerror );
188
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" );
195
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 ) );
202
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 );
209 }
210}
211
212} // namespace readColumnsTest
213} // namespace ioutilsTest
214} // namespace unitTest
error_t readColumns(const std::string &fname, arrTs &...arrays)
Read in columns from a text file.
error_t
The mxlib error codes.
Definition error_t.hpp:26
@ noerror
No error has occurred.
@ erange
Numerical result out of range (ERANGE)
TEST_CASE("Reading space delimited numeric data", "[ioutils::readColumns]")
Reading space delimited numeric data.