mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
ompLoopWatcher_test.cpp
Go to the documentation of this file.
1/** \file ompLoopWatcher_test.cpp
2 * \brief Tests OpenMP loop progress tracking and output formatting.
3 */
4
5#include "../../catch2/catch.hpp"
6
7#include <sstream>
8#include <string>
9
11
12/** \defgroup ompLoopWatcher_unit_tests OpenMP Loop Watcher Unit Tests
13 * \ingroup ipc_unit_tests
14 */
15
16namespace unitTest::ipc_ompLoopWatcher_test
17{
18
19/** \brief Verifies the default ompLoopWatcher batches status output and exposes each public counter wrapper.
20 *
21 * \ingroup ompLoopWatcher_unit_tests
22 */
23TEST_CASE( "Default OpenMP loop watcher tracks and reports progress", "[ipc::ompLoopWatcher][default]" )
24{
25 std::ostringstream output;
26 mx::ipc::ompLoopWatcher<> watcher( 20, output );
27
28 watcher.increment();
29 watcher.advance( 2 );
30 for( int iteration = 0; iteration < 9; ++iteration )
31 {
32 watcher.outputStatus();
33 }
34 REQUIRE( output.str().empty() );
35
36 watcher.outputStatus();
37 REQUIRE( output.str().find( "3 / 20 (15%)" ) != std::string::npos );
38 REQUIRE( output.str().find( "s/loop" ) != std::string::npos );
39 REQUIRE( output.str().find( "s left" ) != std::string::npos );
40 REQUIRE( output.str().back() == '\r' );
41
42 output.str( "" );
43 output.clear();
44 for( int iteration = 0; iteration < 10; ++iteration )
45 {
47 }
48 REQUIRE( output.str().find( "13 / 20 (65%)" ) != std::string::npos );
49
50 output.str( "" );
51 output.clear();
52 for( int iteration = 0; iteration < 10; ++iteration )
53 {
54 watcher.advanceAndOutputStatus( 1 );
55 }
56 REQUIRE( output.str().find( "23 / 20 (115%)" ) != std::string::npos );
57
58 output.str( "" );
59 output.clear();
60 watcher.outputFinalStatus();
61 REQUIRE( output.str().find( "s elapsed" ) != std::string::npos );
62 REQUIRE( output.str().find( "s/loop" ) != std::string::npos );
63 REQUIRE( output.str().back() == '\r' );
64
65 output.str( "" );
66 output.clear();
67 watcher.clearOutput();
68 REQUIRE( output.str().find_first_not_of( ' ' ) == output.str().size() - 1 );
69 REQUIRE( output.str().back() == '\r' );
70}
71
72/** \brief Verifies ompLoopWatcher newline and unformatted output policies.
73 *
74 * \ingroup ompLoopWatcher_unit_tests
75 */
76TEST_CASE( "OpenMP loop watcher honors formatting policies", "[ipc::ompLoopWatcher][format]" )
77{
78 std::ostringstream prettyOutput;
80 for( int iteration = 0; iteration < 10; ++iteration )
81 {
82 prettyWatcher.incrementAndOutputStatus();
83 }
84 REQUIRE( prettyOutput.str().find( "10 / 4 (250%)" ) != std::string::npos );
85 REQUIRE( prettyOutput.str().back() == '\n' );
86
87 prettyOutput.str( "" );
88 prettyOutput.clear();
89 prettyWatcher.outputFinalStatus();
90 REQUIRE( prettyOutput.str().find( "s elapsed" ) != std::string::npos );
91 REQUIRE( prettyOutput.str().back() == '\n' );
92
93 std::ostringstream rawOutput;
95 for( int iteration = 0; iteration < 10; ++iteration )
96 {
97 rawWatcher.incrementAndOutputStatus();
98 }
99 REQUIRE( rawOutput.str().find( "108125" ) == 0 );
100 REQUIRE( rawOutput.str().back() == '\n' );
101
102 rawOutput.str( "" );
103 rawOutput.clear();
104 rawWatcher.outputFinalStatus();
105 REQUIRE_FALSE( rawOutput.str().empty() );
106 REQUIRE( rawOutput.str().front() == ' ' );
107 REQUIRE( rawOutput.str().back() == '\n' );
108
109 std::ostringstream terseOutput;
111 for( int iteration = 0; iteration < 10; ++iteration )
112 {
113 terseWatcher.incrementAndOutputStatus();
114 }
115 REQUIRE( terseOutput.str() == "10" );
116
117 terseOutput.str( "" );
118 terseOutput.clear();
119 terseWatcher.outputFinalStatus();
120 REQUIRE( terseOutput.str().empty() );
121 terseWatcher.clearOutput();
122 REQUIRE( terseOutput.str().empty() );
123}
124
125} // namespace unitTest::ipc_ompLoopWatcher_test
A class to track the number of iterations in an OMP parallelized loop.
void increment()
Increment the counter.
void advanceAndOutputStatus(size_t diff_count)
Advance and output status.
void outputStatus()
Output current status.
void clearOutput()
Clear the output line of any text.
void outputFinalStatus()
Output a final report about elapsed time.
void incrementAndOutputStatus()
Increment and output status.
void advance(size_t diff_count)
Advance the counter by a number of steps.
TEST_CASE("Default OpenMP loop watcher tracks and reports progress", "[ipc::ompLoopWatcher][default]")
Verifies the default ompLoopWatcher batches status output and exposes each public counter wrapper.
Track iterations in an OMP parallelized looop.