mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
fitsHeaderCard_test.cpp
Go to the documentation of this file.
1/** \file fitsHeaderCard_test.cpp
2 */
3#include "../../../catch2/catch.hpp"
4
5#define MX_NO_ERROR_REPORTS
6
7#include "../../../../include/ioutils/fits/fitsHeaderCard.hpp"
8using namespace mx::fits;
9
10namespace mx
11{
12namespace unitTest
13{
14namespace fitsTest
15{
16namespace fitsHeaderCardTest
17{
18
19/// Verify conversion of types
20/**
21 * \ingroup fitsHeaderCard_unit_tests
22 */
23TEST_CASE( "fitsHeaderCard setting types", "[ioutils::fits::fitsHeaderCard]" )
24{
25 SECTION( "a fitsHeaderCard constructed with char type" )
26 {
27 SECTION( "setting a char from a string" )
28 {
29 fitsHeaderCard fhc( "KEYWORD", "39", fitsType<char>(), "this comment" );
30
31 REQUIRE( fhc.keyword() == "KEYWORD" );
32 std::string s = fhc.valueStr();
33 REQUIRE( s == "39" );
34 REQUIRE( fhc.type() == fitsType<char>() );
35 REQUIRE( fhc.valueGood() == false );
36 REQUIRE( fhc.valueStrGood() == true );
37 REQUIRE( fhc.comment() == "this comment" );
38
39 char c = fhc.value<char>();
40 REQUIRE( c == 39 );
41 REQUIRE( fhc.valueGood() == true );
42
43 int i = fhc.value<int>();
44 REQUIRE( i == 39 );
45 REQUIRE( fhc.type() == fitsType<char>() );
46
47 REQUIRE( fhc.valueGood() == true );
48 REQUIRE( fhc.valueStrGood() == true );
49
50 fhc.type( fitsType<int>() );
51 REQUIRE( fhc.type() == fitsType<int>() );
52 REQUIRE( fhc.Int() == 39 );
53 REQUIRE( fhc.valueGood() == true );
54 REQUIRE( fhc.valueStrGood() == false );
55
56 s = fhc.valueStr();
57 REQUIRE( s == "39" );
58 REQUIRE( fhc.valueStrGood() == true );
59 }
60 SECTION( "setting a char from a char" )
61 {
62 fitsHeaderCard fhc( "KEYWORD", static_cast<char>( 39 ), "this comment" );
63
64 REQUIRE( fhc.keyword() == "KEYWORD" );
65 REQUIRE( fhc.type() == fitsType<char>() );
66 REQUIRE( fhc.valueGood() == true );
67 REQUIRE( fhc.valueStrGood() == false );
68 REQUIRE( fhc.comment() == "this comment" );
69
70 char c = fhc.value<char>();
71 REQUIRE( c == 39 );
72 REQUIRE( fhc.valueGood() == true );
73
74 // Test that valueStr stayed false and then read it
75 REQUIRE( fhc.valueStrGood() == false );
76 std::string s = fhc.valueStr();
77 REQUIRE( s == "39" );
78 REQUIRE( fhc.valueStrGood() == true );
79
80 int i = fhc.value<int>();
81 REQUIRE( i == 39 );
82 REQUIRE( fhc.type() == fitsType<char>() );
83
84 REQUIRE( fhc.valueGood() == true );
85 REQUIRE( fhc.valueStrGood() == true );
86
87 mx::error_t errc = fhc.type( fitsType<int>() );
88 REQUIRE( !errc );
89
90 REQUIRE( fhc.type() == fitsType<int>() );
91 errc = mx::error_t::error;
92 REQUIRE( fhc.Int( &errc ) == 39 );
93 REQUIRE( errc == mx::error_t::noerror );
94
95 REQUIRE( fhc.valueGood() == true );
96 REQUIRE( fhc.valueStrGood() == false );
97
98 s = fhc.valueStr();
99 REQUIRE( s == "39" );
100 REQUIRE( fhc.valueStrGood() == true );
101 }
102 }
103}
104
105/// Removing white space around string values
106/**
107 * \ingroup fitsHeaderCard_unit_tests
108 */
109TEST_CASE( "Removing white space around string values", "[ioutils::fits::fitsHeaderCard]" )
110{
111 // clang-format off
112 #ifdef MXLIBTEST_DOXYGEN_REF
113 fitsHeaderCard fhc;
114 mx::error_t errc;
115 fhc.value( mx::meta::tagT<std::string>(), errc );
116 #endif
117 // clang-format on
118
119 SECTION( "typical case" )
120 {
121 fitsHeaderCard fhc( "KEYTEST", "'simple '", "comment" );
122 REQUIRE( fhc.String() == "simple " );
123 }
124
125 SECTION( "no ' and no space" )
126 {
127 fitsHeaderCard fhc( "KEYTEST", "simple", "comment" );
128 REQUIRE( fhc.String() == "simple" );
129 }
130
131 SECTION( "no spaces" )
132 {
133 fitsHeaderCard fhc( "KEYTEST", "'simple'", "comment" );
134 REQUIRE( fhc.String() == "simple" );
135 }
136
137 SECTION( "no '" )
138 {
139 fitsHeaderCard fhc( "KEYTEST", "simple ", "comment" );
140 REQUIRE( fhc.String() == "simple" );
141 }
142
143 SECTION( "space at beginning" )
144 {
145 fitsHeaderCard fhc( "KEYTEST", "' simple '", "comment" );
146 REQUIRE( fhc.String() == " simple " );
147 }
148
149 SECTION( "spaces at beginning, no spaces at end" )
150 {
151 fitsHeaderCard fhc( "KEYTEST", "' simple'", "comment" );
152 REQUIRE( fhc.String() == " simple" );
153 }
154
155 SECTION( "spaces at beginning, no '" )
156 {
157 fitsHeaderCard fhc( "KEYTEST", " simple ", "comment" );
158 REQUIRE( fhc.String() == "simple" );
159 }
160
161 SECTION( "empty" )
162 {
163 fitsHeaderCard fhc( "KEYTEST", "", "comment" );
164 REQUIRE( fhc.String() == "" );
165 }
166
167 SECTION( "one '" )
168 {
169 fitsHeaderCard fhc( "KEYTEST", "'", "comment" );
170 REQUIRE( fhc.String() == "" );
171 }
172
173 SECTION( "two ''" )
174 {
175 fitsHeaderCard fhc( "KEYTEST", "''", "comment" );
176 REQUIRE( fhc.String() == "" );
177 }
178
179 SECTION( "two '', spaces" )
180 {
181 fitsHeaderCard fhc( "KEYTEST", "' '", "comment" );
182 REQUIRE( fhc.String() == " " );
183 }
184
185 SECTION( "spaces only" )
186 {
187 fitsHeaderCard fhc( "KEYTEST", " ", "comment" );
188 REQUIRE( fhc.String() == "" );
189 }
190
191 SECTION( "' part of value" )
192 {
193 fitsHeaderCard fhc( "KEYTEST", "z'", "comment" );
194 REQUIRE( fhc.String() == "z'" );
195 }
196
197 SECTION( "' part of value at end with spaces" )
198 {
199 fitsHeaderCard fhc( "KEYTEST", "'z' '", "comment" );
200 REQUIRE( fhc.String() == "z' " );
201 }
202
203 SECTION( "' part of value at beginning with spaces" )
204 {
205 fitsHeaderCard fhc( "KEYTEST", "''z '", "comment" );
206 REQUIRE( fhc.String() == "'z " );
207 }
208
209 SECTION( "spaces before and after '' with ' in value surrounded by spaces" )
210 {
211 fitsHeaderCard fhc( "KEYTEST", " ' 'z ' ", "comment" );
212 REQUIRE( fhc.String() == " 'z " );
213 }
214}
215
216/// CONTINUE-ing a card
217/**
218 * \ingroup fitsHeaderCard_unit_tests
219 */
220TEST_CASE( "CONTINUE-ing a card", "[ioutils::fits::fitsHeaderCard]" )
221{
222 SECTION("normal CONTINUE, trailing space")
223 {
224 fitsHeaderCard fhc( "KEYTEST", "'one,two,three,&'", "" );
225 fitsHeaderCard fhcc( "CONTINUE", "", "'four,five,six' / the comment" );
226
227 REQUIRE(fhc.appendContinue(fhcc) == error_t::noerror);
228
229 REQUIRE(fhc.keyword() == "KEYTEST");
230 REQUIRE(fhc.String() == "one,two,three,four,five,six");
231 REQUIRE(fhc.comment() == "the comment");
232
233 }
234
235 SECTION("normal CONTINUE, trailing space")
236 {
237 fitsHeaderCard fhc( "KEYTEST", "'one two three &'", "" );
238 fitsHeaderCard fhcc( "CONTINUE", "", "'four five six' / the comment" );
239
240 REQUIRE(fhc.appendContinue(fhcc) == error_t::noerror);
241
242 REQUIRE(fhc.keyword() == "KEYTEST");
243 REQUIRE(fhc.String() == "one two three four five six");
244 REQUIRE(fhc.comment() == "the comment");
245
246 }
247
248 SECTION("normal CONTINUE, leading space")
249 {
250 fitsHeaderCard fhc( "KEYTEST", "'one two three&'", "" );
251 fitsHeaderCard fhcc( "CONTINUE", "", "' four five six' / the comment" );
252
253 REQUIRE(fhc.appendContinue(fhcc) == error_t::noerror);
254
255 REQUIRE(fhc.keyword() == "KEYTEST");
256 REQUIRE(fhc.String() == "one two three four five six");
257 REQUIRE(fhc.comment() == "the comment");
258
259 }
260}
261
262} // namespace fitsHeaderCardTest
263} // namespace fitsTest
264} // namespace unitTest
265} // namespace mx
Class to manage the three components of a FITS header card.
int type() const
Get the type.
std::string value(meta::tagT< std::string >, mx::error_t &errc)
Get value tag dispatcher for std::string.
bool valueStrGood()
Get the current value string good flag.
const std::string & keyword() const
Get the keyword.
bool valueGood()
Get the current value good flag.
std::string valueStr()
Get the current value string.
const std::string & comment()
Get the comment.
std::string String(error_t *errc=nullptr)
Get the value as a string.
int Int(error_t *errc=nullptr)
Get the value as a int.
error_t
The mxlib error codes.
Definition error_t.hpp:26
@ noerror
No error has occurred.
@ error
A general error has occurred.
TEST_CASE("fitsHeaderCard setting types", "[ioutils::fits::fitsHeaderCard]")
Verify conversion of types.
The mxlib c++ namespace.
Definition mxlib.hpp:37
Empty type for tag dispatching.
Definition tagT.hpp:44