API
 
Loading...
Searching...
No Matches
cred2Utils_test.cpp
Go to the documentation of this file.
1/** \file cred2Utils_test.cpp
2 * \brief Catch2 tests for the C-RED 2 utility helpers.
3 * \author Jared R. Males (jaredmales@gmail.com)
4 *
5 * \ingroup cred2Ctrl_files
6 */
7
8#include "../../../tests/testXWC.hpp"
9
10#include "../cred2Utils.hpp"
11
12using namespace MagAOX::app;
13
14namespace libXWCTest
15{
16
17/** \addtogroup cred2Ctrl_unit_test
18 * \brief Additional utility tests for the cred2Ctrl application.
19 *
20 * \ingroup application_unit_test
21 */
22
23/// Namespace for `cred2Ctrl` utility unit tests.
24/** \ingroup cred2Ctrl_unit_test
25 */
26namespace cred2CtrlTest
27{
28
29/// Verify `cred2CleanResponse()` strips prompts and trailing line endings.
30/**
31 * \ingroup cred2Ctrl_unit_test
32 */
33TEST_CASE( "cred2Ctrl utility helpers clean CLI responses", "[cred2Utils]" )
34{
35 // clang-format off
36 #ifdef CRED2CTRL_TEST_DOXYGEN_REF
38 #endif
39 // clang-format on
40
41 SECTION( "responses without prompts are trimmed" )
42 {
43 std::string clean = cred2CleanResponse( "400\r\n" );
44
45 REQUIRE( clean == "400" );
46 }
47
48 SECTION( "responses with prompts are trimmed" )
49 {
50 std::string clean = cred2CleanResponse( "400\r\nfli-cli>" );
51
52 REQUIRE( clean == "400" );
53 }
54}
55
56/// Verify `cred2ParseFloat()` accepts valid values and rejects non-numeric responses.
57/**
58 * \ingroup cred2Ctrl_unit_test
59 */
60TEST_CASE( "cred2Ctrl utility helpers parse float responses", "[cred2Utils]" )
61{
62 // clang-format off
63 #ifdef CRED2CTRL_TEST_DOXYGEN_REF
64 cred2ParseFloat( *(float *)nullptr, "" );
65 #endif
66 // clang-format on
67
68 SECTION( "valid float responses are parsed" )
69 {
70 float value = 0;
71 int rv = cred2ParseFloat( value, "-15.5\r\nfli-cli>" );
72
73 REQUIRE( rv == 0 );
74 REQUIRE( value == Approx( -15.5f ) );
75 }
76
77 SECTION( "non-numeric responses are rejected" )
78 {
79 float value = 0;
80 int rv = cred2ParseFloat( value, "Result: OK\r\nfli-cli>" );
81
82 REQUIRE( rv == -1 );
83 }
84}
85
86/// Verify `cred2ParseFloatVector()` enforces both parsing and element-count expectations.
87/**
88 * \ingroup cred2Ctrl_unit_test
89 */
90TEST_CASE( "cred2Ctrl utility helpers parse float-vector responses", "[cred2Utils]" )
91{
92 // clang-format off
93 #ifdef CRED2CTRL_TEST_DOXYGEN_REF
94 cred2ParseFloatVector( *(std::vector<float> *)nullptr, "", 0 );
95 #endif
96 // clang-format on
97
98 SECTION( "valid delimited float responses are parsed" )
99 {
100 std::vector<float> values;
101 int rv = cred2ParseFloatVector( values, "40.50:37.00:40.25:-14.92:2.29:27.50\r\n", 6 );
102
103 REQUIRE( rv == 0 );
104 REQUIRE( values.size() == 6 );
105 REQUIRE( values[0] == Approx( 40.50f ) );
106 REQUIRE( values[1] == Approx( 37.00f ) );
107 REQUIRE( values[2] == Approx( 40.25f ) );
108 REQUIRE( values[3] == Approx( -14.92f ) );
109 REQUIRE( values[4] == Approx( 2.29f ) );
110 REQUIRE( values[5] == Approx( 27.50f ) );
111 }
112
113 SECTION( "mismatched element counts are rejected" )
114 {
115 std::vector<float> values;
116 int rv = cred2ParseFloatVector( values, "40.50:37.00:40.25\r\n", 6 );
117
118 REQUIRE( rv == -1 );
119 REQUIRE( values.empty() );
120 }
121}
122
123/// Verify `cred2ParseRange()` extracts the encoded bounds from valid responses.
124/**
125 * \ingroup cred2Ctrl_unit_test
126 */
127TEST_CASE( "cred2Ctrl utility helpers parse range responses", "[cred2Utils]" )
128{
129 // clang-format off
130 #ifdef CRED2CTRL_TEST_DOXYGEN_REF
131 cred2ParseRange( *(int *)nullptr, *(int *)nullptr, "" );
132 #endif
133 // clang-format on
134
135 int firstValue = 0;
136 int secondValue = 0;
137 int rv = cred2ParseRange( firstValue, secondValue, "0-639\r\nfli-cli>" );
138
139 REQUIRE( rv == 0 );
140 REQUIRE( firstValue == 0 );
141 REQUIRE( secondValue == 639 );
142}
143
144/// Verify `cred2ParseBool()` translates textual on/off responses into booleans.
145/**
146 * \ingroup cred2Ctrl_unit_test
147 */
148TEST_CASE( "cred2Ctrl utility helpers parse boolean responses", "[cred2Utils]" )
149{
150 // clang-format off
151 #ifdef CRED2CTRL_TEST_DOXYGEN_REF
152 cred2ParseBool( *(bool *)nullptr, "" );
153 #endif
154 // clang-format on
155
156 bool value = false;
157 int rv = cred2ParseBool( value, "on\r\nfli-cli>" );
158
159 REQUIRE( rv == 0 );
160 REQUIRE( value == true );
161}
162
163/// Verify `cred2ParseCropState()` unpacks the enabled flag and ROI bounds.
164/**
165 * \ingroup cred2Ctrl_unit_test
166 */
167TEST_CASE( "cred2Ctrl utility helpers parse crop responses", "[cred2Utils]" )
168{
169 // clang-format off
170 #ifdef CRED2CTRL_TEST_DOXYGEN_REF
171 cred2ParseCropState( *(bool *)nullptr, *(int *)nullptr, *(int *)nullptr, *(int *)nullptr, *(int *)nullptr, "" );
172 #endif
173 // clang-format on
174
175 bool enabled = false;
176 int startColumn = 0;
177 int endColumn = 0;
178 int startRow = 0;
179 int endRow = 0;
180 int rv = cred2ParseCropState( enabled, startColumn, endColumn, startRow, endRow, "on:192-447:128-383\r\n" );
181
182 REQUIRE( rv == 0 );
183 REQUIRE( enabled == true );
184 REQUIRE( startColumn == 192 );
185 REQUIRE( endColumn == 447 );
186 REQUIRE( startRow == 128 );
187 REQUIRE( endRow == 383 );
188}
189
190/// Verify the C-RED 2 ROI conversion helpers translate between center/size and corner commands.
191/**
192 * \ingroup cred2Ctrl_unit_test
193 */
194TEST_CASE( "cred2Ctrl utility helpers format ROI commands", "[cred2Utils]" )
195{
196 // clang-format off
197 #ifdef CRED2CTRL_TEST_DOXYGEN_REF
198 cred2RoiFromCenter( *(cred2Roi *)nullptr, 0, 0, 0, 0, 0, 0 );
201 cred2RoiToCenter( *(float *)nullptr, *(float *)nullptr, *(int *)nullptr, *(int *)nullptr, cred2Roi(), 0, 0 );
202 #endif
203 // clang-format on
204
205 SECTION( "full-frame centers expand to the detector corners" )
206 {
207 cred2Roi roi;
208 int rv = cred2RoiFromCenter( roi, 319.5f, 255.5f, 640, 512, 640, 512 );
209
210 REQUIRE( rv == 0 );
211 REQUIRE( roi.startColumn == 0 );
212 REQUIRE( roi.endColumn == 639 );
213 REQUIRE( roi.startRow == 0 );
214 REQUIRE( roi.endRow == 511 );
215 REQUIRE( roi.fullFrame == true );
216 }
217
218 SECTION( "subframe centers convert to C-RED 2 row and column specifications" )
219 {
220 cred2Roi roi;
221 int rv = cred2RoiFromCenter( roi, 127.5f, 63.5f, 256, 128, 640, 512 );
222
223 REQUIRE( rv == 0 );
224 REQUIRE( cred2ColumnsSpec( roi ) == "0-255" );
225 REQUIRE( cred2RowsSpec( roi ) == "0-127" );
226 REQUIRE( roi.fullFrame == false );
227 }
228
229 SECTION( "corner-defined subframes convert back to a MagAO-X center and size" )
230 {
231 cred2Roi roi;
232 roi.startColumn = 192;
233 roi.endColumn = 447;
234 roi.startRow = 128;
235 roi.endRow = 383;
236 roi.fullFrame = false;
237
238 float centerX = 0;
239 float centerY = 0;
240 int width = 0;
241 int height = 0;
242 int rv = cred2RoiToCenter( centerX, centerY, width, height, roi, 640, 512 );
243
244 REQUIRE( rv == 0 );
245 REQUIRE( centerX == Approx( 319.5f ) );
246 REQUIRE( centerY == Approx( 255.5f ) );
247 REQUIRE( width == 256 );
248 REQUIRE( height == 256 );
249 }
250}
251
252} // namespace cred2CtrlTest
253
254} // namespace libXWCTest
TEST_CASE("cred2Ctrl lifecycle entrypoints handle startup failures and success", "[cred2Ctrl]")
Verify lifecycle entrypoints cover startup failure handling and successful startup cleanup.
bool fullFrame
True when the ROI spans the full detector.
int endRow
Last included row.
int startRow
First included row.
int endColumn
Last included column.
int startColumn
First included column.
C-RED 2 ROI expressed as 0-based inclusive column and row limits.
std::string cred2ColumnsSpec(const cred2Roi &roi)
Format the column command payload for set cropping columns.
std::string cred2CleanResponse(const std::string &response)
Strip an optional prompt and surrounding whitespace from a C-RED 2 CLI response.
int cred2ParseRange(int &firstValue, int &secondValue, const std::string &response)
Parse a raw range response such as 0-639.
int cred2RoiToCenter(float &centerX, float &centerY, int &width, int &height, const cred2Roi &roi, int fullWidth, int fullHeight)
Convert C-RED 2 ROI corners into a MagAO-X ROI center/size description.
int cred2ParseCropState(bool &enabled, int &startColumn, int &endColumn, int &startRow, int &endRow, const std::string &response)
Parse a raw cropping status response such as on or on:192-447:128-383.
int cred2RoiFromCenter(cred2Roi &roi, float centerX, float centerY, int width, int height, int fullWidth, int fullHeight)
Convert a MagAO-X ROI center/size description into C-RED 2 corners.
int cred2ParseFloatVector(std::vector< float > &values, const std::string &response, size_t expectedValues)
Parse a delimited list of raw numeric responses into a float vector.
std::string cred2RowsSpec(const cred2Roi &roi)
Format the row command payload for set cropping rows.
int cred2ParseFloat(float &value, const std::string &response)
Parse a raw numeric response into a float.
int cred2ParseBool(bool &value, const std::string &response)
Parse a raw on/off response into a boolean.
Namespace for all libXWC tests.