API
 
Loading...
Searching...
No Matches
fileTimes_test.cpp
Go to the documentation of this file.
1/** \file fileTimes_test.hpp
2 * \brief Tests for file timestamps
3 * \ingroup file_files
4 */
5
6
7#include <iostream>
8
9#include "../../../tests/testXWC.hpp"
10
11#include "../fileTimes.hpp"
12
13#undef file_fileTimes_hpp
14#define XWCTEST_NAMESPACE XWCTEST_TIMESTAMP_THROW_BAD_ALLOC_ns
15#define XWCTEST_TIMESTAMP_THROW_BAD_ALLOC
16#include "../fileTimes.hpp"
17#undef XWCTEST_NAMESPACE
18#undef XWCTEST_TIMESTAMP_THROW_BAD_ALLOC
19
20#undef file_fileTimes_hpp
21#define XWCTEST_NAMESPACE XWCTEST_TIMESTAMP_THROW_FORMAT_ERROR_ns
22#define XWCTEST_TIMESTAMP_THROW_FORMAT_ERROR
23#include "../fileTimes.hpp"
24#undef XWCTEST_NAMESPACE
25#undef XWCTEST_TIMESTAMP_THROW_FORMAT_ERROR
26
27#undef file_fileTimes_hpp
28#define XWCTEST_NAMESPACE XWCTEST_TIMESTAMP_THROW_EXCEPTION_ns
29#define XWCTEST_TIMESTAMP_THROW_EXCEPTION
30#include "../fileTimes.hpp"
31#undef XWCTEST_NAMESPACE
32#undef XWCTEST_TIMESTAMP_THROW_EXCEPTION
33
34#undef file_fileTimes_hpp
35#define XWCTEST_NAMESPACE XWCTEST_TIMESTAMP_GMTIME_OTHER_ns
36#define XWCTEST_TIMESTAMP_GMTIME_OTHER
37#include "../fileTimes.hpp"
38#undef XWCTEST_NAMESPACE
39#undef XWCTEST_TIMESTAMP_GMTIME_OTHER
40
41#undef file_fileTimes_hpp
42#define XWCTEST_NAMESPACE XWCTEST_FILETIMERELPATH_THROW_BAD_ALLOC_ns
43#define XWCTEST_FILETIMERELPATH_THROW_BAD_ALLOC
44#include "../fileTimes.hpp"
45#undef XWCTEST_NAMESPACE
46#undef XWCTEST_FILETIMERELPATH_THROW_BAD_ALLOC
47
48#undef file_fileTimes_hpp
49#define XWCTEST_NAMESPACE XWCTEST_FILETIMERELPATH_THROW_FORMAT_ERROR_ns
50#define XWCTEST_FILETIMERELPATH_THROW_FORMAT_ERROR
51#include "../fileTimes.hpp"
52#undef XWCTEST_NAMESPACE
53#undef XWCTEST_FILETIMERELPATH_THROW_FORMAT_ERROR
54
55#undef file_fileTimes_hpp
56#define XWCTEST_NAMESPACE XWCTEST_FILETIMERELPATH_THROW_EXCEPTION_ns
57#define XWCTEST_FILETIMERELPATH_THROW_EXCEPTION
58#include "../fileTimes.hpp"
59#undef XWCTEST_NAMESPACE
60#undef XWCTEST_FILETIMERELPATH_THROW_EXCEPTION
61
62#undef file_fileTimes_hpp
63#define XWCTEST_NAMESPACE XWCTEST_FILETIMERELPATHSTRING_THROW_BAD_ALLOC_ns
64#define XWCTEST_FILETIMERELPATHSTRING_THROW_BAD_ALLOC
65#include "../fileTimes.hpp"
66#undef XWCTEST_NAMESPACE
67#undef XWCTEST_FILETIMERELPATHSTRING_THROW_BAD_ALLOC
68
69#undef file_fileTimes_hpp
70#define XWCTEST_NAMESPACE XWCTEST_FILETIMERELPATHSTRING_THROW_EXCEPTION_ns
71#define XWCTEST_FILETIMERELPATHSTRING_THROW_EXCEPTION
72#include "../fileTimes.hpp"
73#undef XWCTEST_NAMESPACE
74#undef XWCTEST_FILETIMERELPATHSTRING_THROW_EXCEPTION
75
76#undef file_fileTimes_hpp
77#define XWCTEST_NAMESPACE XWCTEST_PARSETIMESTAMP_THROW_BAD_ALLOC_ns
78#define XWCTEST_PARSETIMESTAMP_THROW_BAD_ALLOC
79#include "../fileTimes.hpp"
80#undef XWCTEST_NAMESPACE
81#undef XWCTEST_PARSETIMESTAMP_THROW_BAD_ALLOC
82
83#undef file_fileTimes_hpp
84#define XWCTEST_NAMESPACE XWCTEST_PARSETIMESTAMP_THROW_OUT_OF_RANGE_ns
85#define XWCTEST_PARSETIMESTAMP_THROW_OUT_OF_RANGE
86#include "../fileTimes.hpp"
87#undef XWCTEST_NAMESPACE
88#undef XWCTEST_PARSETIMESTAMP_THROW_OUT_OF_RANGE
89
90#undef file_fileTimes_hpp
91#define XWCTEST_NAMESPACE XWCTEST_PARSETIMESTAMP_THROW_EXCEPTION_ns
92#define XWCTEST_PARSETIMESTAMP_THROW_EXCEPTION
93#include "../fileTimes.hpp"
94#undef XWCTEST_NAMESPACE
95#undef XWCTEST_PARSETIMESTAMP_THROW_EXCEPTION
96
97#undef file_fileTimes_hpp
98#define XWCTEST_NAMESPACE XWCTEST_PARSEFILEPATH_THROW_BAD_ALLOC_ns
99#define XWCTEST_PARSEFILEPATH_THROW_BAD_ALLOC
100#include "../fileTimes.hpp"
101#undef XWCTEST_NAMESPACE
102#undef XWCTEST_PARSEFILEPATH_THROW_BAD_ALLOC
103
104#undef file_fileTimes_hpp
105#define XWCTEST_NAMESPACE XWCTEST_PARSEFILEPATH_THROW_OUT_OF_RANGE_ns
106#define XWCTEST_PARSEFILEPATH_THROW_OUT_OF_RANGE
107#include "../fileTimes.hpp"
108#undef XWCTEST_NAMESPACE
109#undef XWCTEST_PARSEFILEPATH_THROW_OUT_OF_RANGE
110
111#undef file_fileTimes_hpp
112#define XWCTEST_NAMESPACE XWCTEST_PARSEFILEPATH_THROW_EXCEPTION_ns
113#define XWCTEST_PARSEFILEPATH_THROW_EXCEPTION
114#include "../fileTimes.hpp"
115#undef XWCTEST_NAMESPACE
116#undef XWCTEST_PARSEFILEPATH_THROW_EXCEPTION
117
118namespace libXWCTest
119{
120
121/** \defgroup file_unit_test libXWC::file Unit Tests
122 * \ingroup unit_test
123*/
124
125/// Namespace for XWC::file tests
126/** \ingroup file_unit_test
127 *
128*/
129namespace fileTest
130{
131
132/** \defgroup fileTimes_unit_test fileTimes Unit Tests
133 * \ingroup file_unit_test
134*/
135
136/// Namespace for XWC::file::fileTimes tests
137/** \ingroup fileTimes_unit_test
138 *
139*/
140namespace fileTimesTest
141{
142
143
144/// Getting timestamp string and broken-down time for a given time
145/**
146 * \ingroup fileTimes_unit_test
147 */
148TEST_CASE( "Getting timestamp string and broken-down time for a given time", "[libMagAOX::file::fileTimes]" )
149{
150 SECTION( "A time with 0 sec and 0 nsec" )
151 {
152 time_t ts_sec = 1732170780;
153 unsigned long ts_nsec = 0;
154
155 std::string tstamp;
156 tm uttime;
157
158 mx::error_t errc = MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec );
159
160 REQUIRE( errc == mx::error_t::noerror );
161 REQUIRE( tstamp == "20241121063300000000000" );
162 REQUIRE( uttime.tm_year == 124 );
163 REQUIRE( uttime.tm_mon == 10 );
164 REQUIRE( uttime.tm_mday == 21 );
165 REQUIRE( uttime.tm_hour == 6 );
166 REQUIRE( uttime.tm_min == 33 );
167 REQUIRE( uttime.tm_sec == 0 );
168 }
169
170 SECTION( "A time with non-0 sec and 0 nsec" )
171 {
172 time_t ts_sec = 1732170781;
173 unsigned long ts_nsec = 0;
174
175 std::string tstamp;
176 tm uttime;
177
178 mx::error_t errc = MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec );
179
180 REQUIRE( errc == mx::error_t::noerror );
181 REQUIRE( tstamp == "20241121063301000000000" );
182 REQUIRE( uttime.tm_year == 124 );
183 REQUIRE( uttime.tm_mon == 10 );
184 REQUIRE( uttime.tm_mday == 21 );
185 REQUIRE( uttime.tm_hour == 6 );
186 REQUIRE( uttime.tm_min == 33 );
187 REQUIRE( uttime.tm_sec == 1 );
188 }
189
190 SECTION( "A time with non-0 sec and 9-digit nsec" )
191 {
192 time_t ts_sec = 1732170785;
193 unsigned long ts_nsec = 434878292;
194
195 std::string tstamp;
196 tm uttime;
197
198 mx::error_t errc = MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec );
199
200 REQUIRE( errc == mx::error_t::noerror );
201 REQUIRE( tstamp == "20241121063305434878292" );
202 REQUIRE( uttime.tm_year == 124 );
203 REQUIRE( uttime.tm_mon == 10 );
204 REQUIRE( uttime.tm_mday == 21 );
205 REQUIRE( uttime.tm_hour == 6 );
206 REQUIRE( uttime.tm_min == 33 );
207 REQUIRE( uttime.tm_sec == 5 );
208 }
209
210 SECTION( "A time with non-0 sec and 3-digit nsec" )
211 {
212 time_t ts_sec = 1732170785;
213 unsigned long ts_nsec = 292;
214
215 std::string tstamp;
216 tm uttime;
217
218 mx::error_t errc = MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec );
219
220 REQUIRE( errc == mx::error_t::noerror );
221 REQUIRE( tstamp == "20241121063305000000292" );
222 REQUIRE( uttime.tm_year == 124 );
223 REQUIRE( uttime.tm_mon == 10 );
224 REQUIRE( uttime.tm_mday == 21 );
225 REQUIRE( uttime.tm_hour == 6 );
226 REQUIRE( uttime.tm_min == 33 );
227 REQUIRE( uttime.tm_sec == 5 );
228 }
229}
230
231/// Getting timestamp and broken-down time with errors
232/**
233 * This is in a separate file due to need to define a buffer size too small to generate errors
234 *
235 * \ingroup fileTimes_unit_test
236 */
237TEST_CASE( "Getting timestamp and broken-down time with errors", "[libMagAOX::file::fileTimes]" )
238{
239 SECTION( "A year that's too big (gmtime_r error)" )
240 {
241 time_t ts_sec = 1.355388599402496e+17; // huge year
242 unsigned long ts_nsec = 0;
243
244 std::string tstamp;
245 tm uttime;
246
247 mx::error_t errc = MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec );
248
249 REQUIRE( errc == mx::error_t::eoverflow );
250 }
251
252 SECTION( "gmtime_r error with errno == 0" )
253 {
254 time_t ts_sec = 1.355388599402496e+17; // huge year
255 unsigned long ts_nsec = 0;
256
257 std::string tstamp;
258 tm uttime;
259
260 mx::error_t errc =
261 MagAOX::file::XWCTEST_TIMESTAMP_GMTIME_OTHER_ns::timestamp( tstamp, uttime, ts_sec, ts_nsec );
262
263 XWCTEST_DOXYGEN_REF( MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec ) );
264
265 REQUIRE( errc == mx::error_t::error );
266 }
267}
268
269/// Getting timestamp only with errors
270/**
271 * This is in a separate file due to need to define a buffer size too small to generate errors
272 *
273 * \anchor tests_libMagAOX_file_fileTimes_timestamp_only_errors
274 */
275TEST_CASE( "Getting timestamp only with errors", "[libMagAOX::file::fileTimes]" )
276{
277 SECTION( "A year that's too big (gmtime_r error)" )
278 {
279 time_t ts_sec = 1.355388599402496e+17; // huge year
280 unsigned long ts_nsec = 0;
281
282 std::string tstamp;
283
284 mx::error_t errc = MagAOX::file::timestamp( tstamp, ts_sec, ts_nsec );
285
286 REQUIRE( errc == mx::error_t::eoverflow );
287 }
288}
289
290
291/// Getting timestamp string and broken-down time causes bad_alloc
292/**
293 * \ingroup fileTimes_unit_test
294 */
295TEST_CASE( "Getting timestamp string and broken-down time causes bad_alloc", "[libMagAOX::file::fileTimes]" )
296{
297 SECTION( "A time with non-0 sec and 3-digit nsec" )
298 {
299
300 bool caught = false;
301
302 try
303 {
304 time_t ts_sec = 1732170785;
305 unsigned long ts_nsec = 292;
306
307 std::string tstamp;
308 tm uttime;
309
310 MagAOX::file::XWCTEST_TIMESTAMP_THROW_BAD_ALLOC_ns::timestamp( tstamp, uttime, ts_sec, ts_nsec );
311
312 XWCTEST_DOXYGEN_REF( MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec ) );
313 }
314 catch( const mx::exception<XWC_DEFAULT_VERBOSITY> &e )
315 {
316 caught = true;
317 }
318
319 REQUIRE( caught == true );
320 }
321}
322
323/// Getting timestamp only causes bad_alloc
324/**
325 * \ingroup fileTimes_unit_test
326 */
327TEST_CASE( "Getting timestamp only causes bad_alloc", "[libMagAOX::file::fileTimes]" )
328{
329 SECTION( "A time with non-0 sec and 3-digit nsec" )
330 {
331 time_t ts_sec = 1732170785;
332 unsigned long ts_nsec = 292;
333
334 std::string tstamp;
335
336 bool caught = false;
337
338 try
339 {
340 MagAOX::file::XWCTEST_TIMESTAMP_THROW_BAD_ALLOC_ns::timestamp( tstamp, ts_sec, ts_nsec );
341 XWCTEST_DOXYGEN_REF( MagAOX::file::timestamp( tstamp, ts_sec, ts_nsec ) );
342 }
343 catch( const mx::exception<XWC_DEFAULT_VERBOSITY> &e )
344 {
345 caught = true;
346 }
347
348 REQUIRE( caught == true );
349 }
350}
351
352/// Getting timestamp string and broken-down time causes filesystem_error
353/**
354 * \ingroup fileTimes_unit_test
355 */
356TEST_CASE( "Getting timestamp string and broken-down time causes filesystem_error", "[libMagAOX::file::fileTimes]" )
357{
358 SECTION( "A time with non-0 sec and 3-digit nsec" )
359 {
360 time_t ts_sec = 1732170785;
361 unsigned long ts_nsec = 292;
362
363 std::string tstamp;
364 tm uttime;
365
366 mx::error_t errc =
367 MagAOX::file::XWCTEST_TIMESTAMP_THROW_FORMAT_ERROR_ns::timestamp( tstamp, uttime, ts_sec, ts_nsec );
368 XWCTEST_DOXYGEN_REF( MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec ) );
369
370 REQUIRE( errc == mx::error_t::std_format_error );
371 }
372}
373
374/// Getting timestamp only causes filesystem_error
375/**
376 * \ingroup fileTimes_unit_test
377 */
378TEST_CASE( "Getting timestamp only causes filesystem_error", "[libMagAOX::file::fileTimes]" )
379{
380 SECTION( "A time with non-0 sec and 3-digit nsec" )
381 {
382 time_t ts_sec = 1732170785;
383 unsigned long ts_nsec = 292;
384
385 std::string tstamp;
386
387 mx::error_t errc = MagAOX::file::XWCTEST_TIMESTAMP_THROW_FORMAT_ERROR_ns::timestamp( tstamp, ts_sec, ts_nsec );
388 XWCTEST_DOXYGEN_REF( MagAOX::file::timestamp( tstamp, ts_sec, ts_nsec ) );
389
390 REQUIRE( errc == mx::error_t::std_format_error );
391 }
392}
393
394/// Getting timestamp string and broken-down time causes exception
395/**
396 * \ingroup fileTimes_unit_test
397 */
398TEST_CASE( "Getting timestamp string and broken-down time causes exception", "[libMagAOX::file::fileTimes]" )
399{
400 SECTION( "A time with non-0 sec and 3-digit nsec" )
401 {
402 time_t ts_sec = 1732170785;
403 unsigned long ts_nsec = 292;
404
405 std::string tstamp;
406 tm uttime;
407
408 mx::error_t errc =
409 MagAOX::file::XWCTEST_TIMESTAMP_THROW_EXCEPTION_ns::timestamp( tstamp, uttime, ts_sec, ts_nsec );
410 XWCTEST_DOXYGEN_REF( MagAOX::file::timestamp( tstamp, uttime, ts_sec, ts_nsec ) );
411
412 REQUIRE( errc == mx::error_t::std_exception );
413 }
414}
415
416/// Getting timestamp only causes exception
417/**
418 * \ingroup fileTimes_unit_test
419 */
420TEST_CASE( "Getting timestamp only causes exception", "[libMagAOX::file::fileTimes]" )
421{
422 SECTION( "A time with non-0 sec and 3-digit nsec" )
423 {
424 time_t ts_sec = 1732170785;
425 unsigned long ts_nsec = 292;
426
427 std::string tstamp;
428
429 mx::error_t errc = MagAOX::file::XWCTEST_TIMESTAMP_THROW_EXCEPTION_ns::timestamp( tstamp, ts_sec, ts_nsec );
430 XWCTEST_DOXYGEN_REF( MagAOX::file::timestamp( tstamp, ts_sec, ts_nsec ) );
431
432 REQUIRE( errc == mx::error_t::std_exception );
433 }
434}
435
436/// Getting filename and relative path for a given time
437/**
438 * \ingroup fileTimes_unit_test
439 */
440TEST_CASE( "Getting filename and relative path for a given time", "[libMagAOX::file::fileTimes]" )
441{
442 SECTION( "A time with 0 sec and 0 nsec" )
443 {
444 time_t ts_sec = 1732170780;
445 unsigned long ts_nsec = 0;
446
447 std::string fileName, relPath;
448
449 mx::error_t errc = MagAOX::file::fileTimeRelPath( fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
450
451 REQUIRE( errc == mx::error_t::noerror );
452 REQUIRE( fileName == "tdevice_20241121063300000000000.txt" );
453 REQUIRE( relPath == "tdevice/2024_11_21" );
454 }
455}
456
457/// Getting filename and relative path for a given time causes overflow
458/**
459 * \ingroup fileTimes_unit_test
460 */
461TEST_CASE( "Getting filename and relative path for a given time causes overflow", "[libMagAOX::file::fileTimes]" )
462{
463 SECTION( "A year that's too big (gmtime_r error)" )
464 {
465 time_t ts_sec = 1.355388599402496e+17; // huge year
466 unsigned long ts_nsec = 0;
467
468 std::string fileName, relPath;
469
470 mx::error_t errc = MagAOX::file::fileTimeRelPath( fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
471
472 REQUIRE( errc == mx::error_t::eoverflow );
473 }
474}
475
476/// Getting filename and relative path for a given time causes bad_alloc in timestamp
477/**
478 * \ingroup fileTimes_unit_test
479 */
480TEST_CASE( "Getting filename and relative path for a given time causes bad_alloc in timestamp",
481 "[libMagAOX::file::fileTimes]" )
482{
483 SECTION( "A time with 0 sec and 0 nsec" )
484 {
485
486 bool caught = false;
487 try
488 {
489 time_t ts_sec = 1732170780;
490 unsigned long ts_nsec = 0;
491 std::string fileName, relPath;
492
493 MagAOX::file::XWCTEST_TIMESTAMP_THROW_BAD_ALLOC_ns::fileTimeRelPath(
494 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
496 MagAOX::file::fileTimeRelPath( fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec ) );
497 }
498 catch( const mx::exception<XWC_DEFAULT_VERBOSITY> &e )
499 {
500 caught = true;
501 }
502
503 REQUIRE( caught == true );
504 }
505}
506
507/// Getting filename and relative path for a given time causes format_error in timestamp
508/**
509 * \ingroup fileTimes_unit_test
510 */
511TEST_CASE( "Getting filename and relative path for a given time causes format_error in timestamp",
512 "[libMagAOX::file::fileTimes]" )
513{
514 SECTION( "A time with 0 sec and 0 nsec" )
515 {
516 time_t ts_sec = 1732170780;
517 unsigned long ts_nsec = 0;
518
519 std::string fileName, relPath;
520
521 mx::error_t errc = MagAOX::file::XWCTEST_TIMESTAMP_THROW_FORMAT_ERROR_ns::fileTimeRelPath(
522 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
523
524 XWCTEST_DOXYGEN_REF( MagAOX::file::fileTimeRelPath( fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec ) );
525
526 REQUIRE( errc == mx::error_t::std_format_error );
527 }
528}
529
530/// Getting filename and relative path for a given time causes exception in timestamp
531/**
532 * \ingroup fileTimes_unit_test
533 */
534TEST_CASE( "Getting filename and relative path for a given time causes exception in timestamp",
535 "[libMagAOX::file::fileTimes]" )
536{
537 SECTION( "A time with 0 sec and 0 nsec" )
538 {
539 time_t ts_sec = 1732170780;
540 unsigned long ts_nsec = 0;
541
542 std::string fileName, relPath;
543
544 mx::error_t errc = MagAOX::file::XWCTEST_TIMESTAMP_THROW_EXCEPTION_ns::fileTimeRelPath(
545 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
546
547 XWCTEST_DOXYGEN_REF( MagAOX::file::fileTimeRelPath( fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec ) );
548
549 REQUIRE( errc == mx::error_t::std_exception );
550 }
551}
552
553/// Getting filename and relative path for a given time causes bad_alloc in top relpath
554/**
555 * \ingroup fileTimes_unit_test
556 */
557TEST_CASE( "Getting filename and relative path for a given time causes bad_alloc in top relpath",
558 "[libMagAOX::file::fileTimes]" )
559{
560 SECTION( "A time with 0 sec and 0 nsec" )
561 {
562 time_t ts_sec = 1732170780;
563 unsigned long ts_nsec = 0;
564
565 std::string fileName, relPath;
566
567 bool caught = false;
568 try
569 {
570 MagAOX::file::XWCTEST_FILETIMERELPATH_THROW_BAD_ALLOC_ns::fileTimeRelPath(
571 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
572
574 MagAOX::file::fileTimeRelPath( fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec ) );
575 }
576 catch( const mx::exception<XWC_DEFAULT_VERBOSITY> &e )
577 {
578 caught = true;
579 }
580
581 REQUIRE( caught == true );
582 }
583}
584
585/// Getting filename and relative path for a given time causes bad_alloc in relpath string
586/**
587 * \ingroup fileTimes_unit_test
588 */
589TEST_CASE( "Getting filename and relative path for a given time causes bad_alloc in relpath string",
590 "[libMagAOX::file::fileTimes]" )
591{
592 SECTION( "A time with 0 sec and 0 nsec" )
593 {
594 time_t ts_sec = 1732170780;
595 unsigned long ts_nsec = 0;
596
597 std::string fileName, relPath;
598
599 bool caught = false;
600 try
601 {
602 MagAOX::file::XWCTEST_FILETIMERELPATHSTRING_THROW_BAD_ALLOC_ns::fileTimeRelPath(
603 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
604
606 MagAOX::file::fileTimeRelPath( fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec ) );
607 }
608 catch( const mx::exception<XWC_DEFAULT_VERBOSITY> &e )
609 {
610 caught = true;
611 }
612
613 REQUIRE( caught == true );
614 }
615}
616
617/// Getting filename and relative path for a given time causes format_error in top relpath
618/**
619 * \ingroup fileTimes_unit_test
620 */
621TEST_CASE( "Getting filename and relative path for a given time causes format_error in top relpath",
622 "[libMagAOX::file::fileTimes]" )
623{
624 SECTION( "A time with 0 sec and 0 nsec" )
625 {
626 time_t ts_sec = 1732170780;
627 unsigned long ts_nsec = 0;
628
629 std::string fileName, relPath;
630
631 mx::error_t errc = MagAOX::file::XWCTEST_FILETIMERELPATH_THROW_FORMAT_ERROR_ns::fileTimeRelPath(
632 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
633
635 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec ));
636
637 REQUIRE( errc == mx::error_t::std_format_error );
638
639 // for doxygen (not a test):
640 MagAOX::file::fileTimeRelPath( fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
641 }
642}
643
644/// Getting filename and relative path for a given time causes exception in top relpath
645/**
646 * \ingroup fileTimes_unit_test
647 */
648TEST_CASE( "Getting filename and relative path for a given time causes exception in top relpath",
649 "[libMagAOX::file::fileTimes]" )
650{
651 SECTION( "A time with 0 sec and 0 nsec" )
652 {
653 time_t ts_sec = 1732170780;
654 unsigned long ts_nsec = 0;
655
656 std::string fileName, relPath;
657
658 mx::error_t errc = MagAOX::file::XWCTEST_FILETIMERELPATH_THROW_EXCEPTION_ns::fileTimeRelPath(
659 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
660
662 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec ));
663
664 REQUIRE( errc == mx::error_t::std_exception );
665 }
666}
667
668/// Getting filename and relative path for a given time causes exception in relpath string
669/**
670 * \ingroup fileTimes_unit_test
671 */
672TEST_CASE( "Getting filename and relative path for a given time causes exception in relpath string",
673 "[libMagAOX::file::fileTimes]" )
674{
675 SECTION( "A time with 0 sec and 0 nsec" )
676 {
677 time_t ts_sec = 1732170780;
678 unsigned long ts_nsec = 0;
679
680 std::string fileName, relPath;
681
682 mx::error_t errc = MagAOX::file::XWCTEST_FILETIMERELPATHSTRING_THROW_EXCEPTION_ns::fileTimeRelPath(
683 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec );
684
686 fileName, relPath, "tdevice", "txt", ts_sec, ts_nsec ));
687
688 REQUIRE( errc == mx::error_t::std_exception );
689 }
690}
691
692/// Parsing filenames, paths and timestamps, with no errors
693/**
694 * \ingroup fileTimes_unit_test
695 */
696TEST_CASE( "Parsing filenames, paths and timestamps, with no errors", "[libMagAOX::file::fileTimes]" )
697{
698 SECTION( "A valid MagAO-X filename" )
699 {
700 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
701
702 mx::error_t errc =
703 MagAOX::file::parseFilePath( devName, YYYY, MM, DD, hh, mm, ss, nn, "device_20241121063300000000000.txt" );
704
705 REQUIRE( errc == mx::error_t::noerror );
706 REQUIRE( devName == "device" );
707 REQUIRE( YYYY == "2024" );
708 REQUIRE( MM == "11" );
709 REQUIRE( DD == "21" );
710 REQUIRE( hh == "06" );
711 REQUIRE( mm == "33" );
712 REQUIRE( ss == "00" );
713 REQUIRE( nn == "000000000" );
714 }
715
716 SECTION( "A valid MagAO-X filepath" )
717 {
718 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
719
720 mx::error_t errc = MagAOX::file::parseFilePath(
721 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" );
722
723 REQUIRE( errc == mx::error_t::noerror );
724 REQUIRE( devName == "device" );
725 REQUIRE( YYYY == "2024" );
726 REQUIRE( MM == "11" );
727 REQUIRE( DD == "21" );
728 REQUIRE( hh == "06" );
729 REQUIRE( mm == "33" );
730 REQUIRE( ss == "00" );
731 REQUIRE( nn == "000000000" );
732 }
733
734 SECTION( "A valid MagAO-X filename without extension" )
735 {
736 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
737
738 mx::error_t errc =
739 MagAOX::file::parseFilePath( devName, YYYY, MM, DD, hh, mm, ss, nn, "device_20241121063300000000000" );
740
741 REQUIRE( errc == mx::error_t::noerror );
742 REQUIRE( devName == "device" );
743 REQUIRE( YYYY == "2024" );
744 REQUIRE( MM == "11" );
745 REQUIRE( DD == "21" );
746 REQUIRE( hh == "06" );
747 REQUIRE( mm == "33" );
748 REQUIRE( ss == "00" );
749 REQUIRE( nn == "000000000" );
750 }
751
752 SECTION( "A valid MagAO-X filepath without extension" )
753 {
754 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
755
756 mx::error_t errc = MagAOX::file::parseFilePath(
757 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000" );
758
759 REQUIRE( errc == mx::error_t::noerror );
760 REQUIRE( devName == "device" );
761 REQUIRE( YYYY == "2024" );
762 REQUIRE( MM == "11" );
763 REQUIRE( DD == "21" );
764 REQUIRE( hh == "06" );
765 REQUIRE( mm == "33" );
766 REQUIRE( ss == "00" );
767 REQUIRE( nn == "000000000" );
768 }
769
770 SECTION( "A valid MagAO-X filename without device, no _" )
771 {
772 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
773
774 mx::error_t errc =
775 MagAOX::file::parseFilePath( devName, YYYY, MM, DD, hh, mm, ss, nn, "20241121063300000000000.txt" );
776
777 REQUIRE( errc == mx::error_t::noerror );
778 REQUIRE( devName == "" );
779 REQUIRE( YYYY == "2024" );
780 REQUIRE( MM == "11" );
781 REQUIRE( DD == "21" );
782 REQUIRE( hh == "06" );
783 REQUIRE( mm == "33" );
784 REQUIRE( ss == "00" );
785 REQUIRE( nn == "000000000" );
786 }
787
788 SECTION( "A valid MagAO-X filepath without device, no _" )
789 {
790 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
791
792 mx::error_t errc = MagAOX::file::parseFilePath(
793 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/20241121063300000000000.txt" );
794
795 REQUIRE( errc == mx::error_t::noerror );
796 REQUIRE( devName == "" );
797 REQUIRE( YYYY == "2024" );
798 REQUIRE( MM == "11" );
799 REQUIRE( DD == "21" );
800 REQUIRE( hh == "06" );
801 REQUIRE( mm == "33" );
802 REQUIRE( ss == "00" );
803 REQUIRE( nn == "000000000" );
804 }
805
806 SECTION( "A valid MagAO-X filename without device or extension, no _" )
807 {
808 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
809
810 mx::error_t errc =
811 MagAOX::file::parseFilePath( devName, YYYY, MM, DD, hh, mm, ss, nn, "20241121063300000000000" );
812
813 REQUIRE( errc == mx::error_t::noerror );
814 REQUIRE( devName == "" );
815 REQUIRE( YYYY == "2024" );
816 REQUIRE( MM == "11" );
817 REQUIRE( DD == "21" );
818 REQUIRE( hh == "06" );
819 REQUIRE( mm == "33" );
820 REQUIRE( ss == "00" );
821 REQUIRE( nn == "000000000" );
822 }
823
824 SECTION( "A valid MagAO-X filepath without device or extension, no _" )
825 {
826 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
827
828 mx::error_t errc =
829 MagAOX::file::parseFilePath( devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/20241121063300000000000" );
830
831 REQUIRE( errc == mx::error_t::noerror );
832 REQUIRE( devName == "" );
833 REQUIRE( YYYY == "2024" );
834 REQUIRE( MM == "11" );
835 REQUIRE( DD == "21" );
836 REQUIRE( hh == "06" );
837 REQUIRE( mm == "33" );
838 REQUIRE( ss == "00" );
839 REQUIRE( nn == "000000000" );
840 }
841
842 SECTION( "A valid MagAO-X filename without device, with _" )
843 {
844 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
845
846 mx::error_t errc =
847 MagAOX::file::parseFilePath( devName, YYYY, MM, DD, hh, mm, ss, nn, "_20241121063300000000000.txt" );
848
849 REQUIRE( errc == mx::error_t::noerror );
850 REQUIRE( devName == "" );
851 REQUIRE( YYYY == "2024" );
852 REQUIRE( MM == "11" );
853 REQUIRE( DD == "21" );
854 REQUIRE( hh == "06" );
855 REQUIRE( mm == "33" );
856 REQUIRE( ss == "00" );
857 REQUIRE( nn == "000000000" );
858 }
859
860 SECTION( "A valid MagAO-X filepath without device, with _" )
861 {
862 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
863
864 mx::error_t errc = MagAOX::file::parseFilePath(
865 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/_20241121063300000000000.txt" );
866
867 REQUIRE( errc == mx::error_t::noerror );
868 REQUIRE( devName == "" );
869 REQUIRE( YYYY == "2024" );
870 REQUIRE( MM == "11" );
871 REQUIRE( DD == "21" );
872 REQUIRE( hh == "06" );
873 REQUIRE( mm == "33" );
874 REQUIRE( ss == "00" );
875 REQUIRE( nn == "000000000" );
876 }
877
878 SECTION( "A valid MagAO-X filename without device or extension, with _" )
879 {
880 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
881
882 mx::error_t errc =
883 MagAOX::file::parseFilePath( devName, YYYY, MM, DD, hh, mm, ss, nn, "_20241121063300000000000" );
884
885 REQUIRE( errc == mx::error_t::noerror );
886 REQUIRE( devName == "" );
887 REQUIRE( YYYY == "2024" );
888 REQUIRE( MM == "11" );
889 REQUIRE( DD == "21" );
890 REQUIRE( hh == "06" );
891 REQUIRE( mm == "33" );
892 REQUIRE( ss == "00" );
893 REQUIRE( nn == "000000000" );
894 }
895
896 SECTION( "A valid MagAO-X filepath without device or extension, with _" )
897 {
898 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
899
900 mx::error_t errc =
901 MagAOX::file::parseFilePath( devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/_20241121063300000000000" );
902
903 REQUIRE( errc == mx::error_t::noerror );
904 REQUIRE( devName == "" );
905 REQUIRE( YYYY == "2024" );
906 REQUIRE( MM == "11" );
907 REQUIRE( DD == "21" );
908 REQUIRE( hh == "06" );
909 REQUIRE( mm == "33" );
910 REQUIRE( ss == "00" );
911 REQUIRE( nn == "000000000" );
912 }
913}
914
915/// Parsing filenames and paths with errors
916/**
917 * \ingroup fileTimes_unit_test
918 */
919TEST_CASE( "Parsing filenames and paths with errors", "[libMagAOX::file::fileTimes]" )
920{
921 SECTION( "An invalid MagAO-X filepath with too short timestamp" )
922 {
923 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
924
925 mx::error_t errc = MagAOX::file::parseFilePath(
926 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_2024112106330000000000.txt" );
927
928 REQUIRE( errc == mx::error_t::invalidarg );
929 }
930
931 SECTION( "An invalid MagAO-X filepath with too long timestamp" )
932 {
933 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
934
935 mx::error_t errc = MagAOX::file::parseFilePath(
936 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_202411210633000000000001.txt" );
937 REQUIRE( errc == mx::error_t::invalidarg );
938 }
939
940 SECTION( "An valid MagAO-X filepath but bad_alloc is thrown in parseTimeStamp" )
941 {
942
943 bool caught = false;
944 try
945 {
946 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
947
948 MagAOX::file::XWCTEST_PARSETIMESTAMP_THROW_BAD_ALLOC_ns::parseFilePath(
949 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" );
950
952 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" ));
953 }
954 catch( const mx::exception<XWC_DEFAULT_VERBOSITY> & e )
955 {
956 caught = true;
957 }
958
959 REQUIRE( caught == true );
960 }
961
962 SECTION( "An valid MagAO-X filepath but out_of_range is thrown in parseTimeStamp" )
963 {
964 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
965
966 mx::error_t errc = MagAOX::file::XWCTEST_PARSETIMESTAMP_THROW_OUT_OF_RANGE_ns::parseFilePath(
967 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" );
968
970 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" ));
971
972 REQUIRE( errc == mx::error_t::std_out_of_range );
973 }
974
975 SECTION( "An valid MagAO-X filepath but exception is thrown in parseTimeStamp" )
976 {
977 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
978
979 mx::error_t errc = MagAOX::file::XWCTEST_PARSETIMESTAMP_THROW_EXCEPTION_ns::parseFilePath(
980 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" );
981
983 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" ));
984
985 REQUIRE( errc == mx::error_t::std_exception );
986 }
987
988 SECTION( "An valid MagAO-X filepath but bad_alloc is thrown in parseFilePath" )
989 {
990 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
991
992 bool caught = false;
993 try
994 {
995 MagAOX::file::XWCTEST_PARSEFILEPATH_THROW_BAD_ALLOC_ns::parseFilePath(
996 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" );
997
999 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" ));
1000 }
1001 catch( const mx::exception<XWC_DEFAULT_VERBOSITY> &e )
1002 {
1003 caught = true;
1004 }
1005 REQUIRE( caught == true );
1006 }
1007
1008 SECTION( "An valid MagAO-X filepath but out_of_range is thrown in parseFilePAth" )
1009 {
1010 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
1011
1012 mx::error_t errc = MagAOX::file::XWCTEST_PARSEFILEPATH_THROW_OUT_OF_RANGE_ns::parseFilePath(
1013 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" );
1014
1016 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" ));
1017
1018 REQUIRE( errc == mx::error_t::std_out_of_range );
1019 }
1020
1021 SECTION( "An valid MagAO-X filepath but exception is thrown in parseFilePAth" )
1022 {
1023 std::string devName, YYYY, MM, DD, hh, mm, ss, nn;
1024
1025 mx::error_t errc = MagAOX::file::XWCTEST_PARSEFILEPATH_THROW_EXCEPTION_ns::parseFilePath(
1026 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" );
1027
1029 devName, YYYY, MM, DD, hh, mm, ss, nn, "/path/to/device_20241121063300000000000.txt" ));
1030
1031 REQUIRE( errc == mx::error_t::std_exception );
1032 }
1033}
1034
1035/// Parsing timestamps with errors
1036/**
1037 * Tests only size errors. Exceptions tested with parseFilePath tests.
1038 *
1039 * \ingroup fileTimes_unit_test
1040 */
1041TEST_CASE( "Parsing timestamps with errors", "[libMagAOX::file::fileTimes]" )
1042{
1043 SECTION( "An invalid MagAO-X timestamp, too short" )
1044 {
1045 std::string YYYY, MM, DD, hh, mm, ss, nn;
1046
1047 mx::error_t errc = MagAOX::file::parseTimestamp( YYYY, MM, DD, hh, mm, ss, nn, "202411210633000000000" );
1048
1049 REQUIRE( errc == mx::error_t::invalidarg );
1050 }
1051
1052 SECTION( "An invalid MagAO-X timestamp, too long" )
1053 {
1054 std::string YYYY, MM, DD, hh, mm, ss, nn;
1055
1056 mx::error_t errc = MagAOX::file::parseTimestamp( YYYY, MM, DD, hh, mm, ss, nn, "202411210633000000000001" );
1057 REQUIRE( errc == mx::error_t::invalidarg );
1058 }
1059}
1060
1061} // namespace fileTimesTest
1062} // namespace fileTest
1063} // namespace libXWCTest
TEST_CASE("Getting timestamp string and broken-down time for a given time", "[libMagAOX::file::fileTimes]")
Getting timestamp string and broken-down time for a given time.
#define XWCTEST_DOXYGEN_REF(fxn)
This inserts an unused call to a function signature to make doxygen make the link.
Definition testXWC.hpp:18
mx::error_t parseTimestamp(std::string &YYYY, std::string &MM, std::string &DD, std::string &hh, std::string &mm, std::string &ss, std::string &nn, const std::string &tstamp)
Parse a standard XWCTk timestamp string.
mx::error_t timestamp(std::string &tstamp, const tm &uttime, long ts_nsec)
Get the filename timestamp from the breakdown for a time.
Definition fileTimes.hpp:44
mx::error_t parseFilePath(std::string &devName, std::string &YYYY, std::string &MM, std::string &DD, std::string &hh, std::string &mm, std::string &ss, std::string &nn, const std::string &fname)
Parse a standard XWCTk timestamp filepath.
mx::error_t fileTimeRelPath(std::string &tstamp, std::string &relPath, time_t ts_sec, long ts_nsec)
Get the timestamp and the relative path based on a time.
Namespace for all libXWC tests.
Definition MagAOXApp.hpp:49