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
11/** \test Scenario: Reading space delimited numeric data
12 *
13 * \anchor tests_ioutils_readColumns_space_numeric
14 */
15SCENARIO( "Reading space delimited numeric data", "[ioutils::readColumns]" )
16{
17 GIVEN( "a single column of floating point numbers" )
18 {
19 std::string fname = "/tmp/readcol_test_single_col_float.dat";
20 std::ofstream fout;
21 fout.open(fname);
22 fout << "#a commment\n";
23 fout << "1.23\n";
24 fout << "2.15\n";
25 fout << "4.96 #with comment\n";
26 fout << "5.23\n";
27 fout << "1e-9"; //no newline on last line
28 fout.close();
29
30 WHEN( "reading as int" )
31 {
32 std::vector<int> data0;
33 mx::error_t errc = mx::ioutils::readColumns(fname, data0);
34 REQUIRE( errc == mx::error_t::noerror );
35 REQUIRE(data0.size() == 5);
36 REQUIRE(data0[0] == 1);
37 REQUIRE(data0[1] == 2);
38 REQUIRE(data0[2] == 4);
39 REQUIRE(data0[3] == 5);
40 REQUIRE(data0[4] == 1); //this is a consequence of stoi
41 }
42
43 WHEN( "reading as float" )
44 {
45 std::vector<float> data0;
46 mx::error_t errc = mx::ioutils::readColumns(fname, data0);
47 REQUIRE( errc == mx::error_t::noerror );
48 REQUIRE(data0.size() == 5);
49 REQUIRE_THAT(data0[0], WithinRel(1.23, 1e-5));
50 REQUIRE_THAT(data0[1], WithinRel(2.15, 1e-5));
51 REQUIRE_THAT(data0[2], WithinRel(4.96, 1e-5));
52 REQUIRE_THAT(data0[3], WithinRel(5.23, 1e-5));
53 REQUIRE_THAT(data0[4], WithinRel(1e-9, 1e-5));
54
55 }
56 }
57
58 GIVEN( "a two columns of floating point numbers" )
59 {
60 std::string fname = "/tmp/readcol_test_two_cols_float.dat";
61 std::ofstream fout;
62 fout.open(fname);
63 fout << "#a commment\n";
64 fout << "1.23 6.78\n";
65 fout << "2.15 8.88\n";
66 fout << "4.96 -2.33#with comment\n";
67 fout << "\n"; //empty line
68 fout << " \n"; //blank line
69 fout << "5.23 9.9e5\n";
70 fout << "1e-9 -5.6e2\n";
71 fout << " #comment at end not on first char and no newline";
72 fout.close();
73
74 WHEN( "reading both as int" )
75 {
76 std::vector<int> data0, data1;
77 mx::error_t errc = mx::ioutils::readColumns(fname, data0, data1);
78 REQUIRE( errc == mx::error_t::noerror );
79 REQUIRE(data0.size() == 5);
80 REQUIRE(data0[0] == 1);
81 REQUIRE(data0[1] == 2);
82 REQUIRE(data0[2] == 4);
83 REQUIRE(data0[3] == 5);
84 REQUIRE(data0[4] == 1); //this is a consequence of stoi
85
86 REQUIRE(data1.size() == 5);
87 REQUIRE(data1[0] == 6);
88 REQUIRE(data1[1] == 8);
89 REQUIRE(data1[2] == -2);
90 REQUIRE(data1[3] == 9);//this is a consequence of stoi
91 REQUIRE(data1[4] == -5); //this is a consequence of stoi
92 }
93
94 WHEN( "reading both as float" )
95 {
96 std::vector<float> data0, data1;
97 mx::error_t errc = mx::ioutils::readColumns(fname, data0, data1);
98 REQUIRE( errc == mx::error_t::noerror );
99 REQUIRE(data0.size() == 5);
100 REQUIRE_THAT(data0[0], WithinRel(1.23, 1e-5));
101 REQUIRE_THAT(data0[1], WithinRel(2.15, 1e-5));
102 REQUIRE_THAT(data0[2], WithinRel(4.96, 1e-5));
103 REQUIRE_THAT(data0[3], WithinRel(5.23, 1e-5));
104 REQUIRE_THAT(data0[4], WithinRel(1e-9, 1e-5));
105
106 REQUIRE(data1.size() == 5);
107 REQUIRE_THAT(data1[0], WithinRel(6.78, 1e-5));
108 REQUIRE_THAT(data1[1], WithinRel(8.88, 1e-5));
109 REQUIRE_THAT(data1[2], WithinRel(-2.33, 1e-5));
110 REQUIRE_THAT(data1[3], WithinRel(9.9e5, 1e-5));
111 REQUIRE_THAT(data1[4], WithinRel(-5.6e2, 1e-5));
112 }
113
114 }
115}
116
117/** \test Scenario: Reading space delimited numeric data with errors
118 *
119 * \anchor tests_ioutils_readColumns_twocol_space_numeric_errors
120 */
121SCENARIO( "Reading space delimited numeric data with errors", "[ioutils::readColumns]" )
122{
123 GIVEN( "two columns of floating point numbers" )
124 {
125 std::string fname = "/tmp/readcol_test_two_col_float_intoverflow.dat";
126 std::ofstream fout;
127 fout.open(fname);
128 fout << "1.23 6.7\n";
129 fout << "2.15 8.8\n";
130 fout << "4.96 9.9\n";
131 fout << "5.23 10.11\n";
132 fout << "1e-9 55555555555555555555555555555555555\n";
133 fout.close();
134
135 WHEN( "reading as int" )
136 {
137 std::vector<int> data0, data1;
138 mx::error_t errc = mx::ioutils::readColumns<mx::ioutils::readColSpaceDelim, mx::verbose::v>(fname, data0, data1);
139 REQUIRE( errc == mx::error_t::std_out_of_range );
140 }
141
142 /*WHEN( "reading as int64_t" )
143 {
144 std::vector<int64_t> data0, data1;
145 mx::error_t errc = mx::ioutils::readColumns(fname, data0, data1);
146 REQUIRE( errc == mx::error_t::std_out_of_range );
147 }*/
148 }
149}
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:20
@ noerror
No error has occurred.
@ std_out_of_range
An out of range exception was thrown.
SCENARIO("Reading space delimited numeric data", "[ioutils::readColumns]")