StopWatch_test.cc
Go to the documentation of this file.
1 /**
2  * @file StopWatch_test.cpp
3  * @brief Test for the StopWatch class
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date July 3, 2016
6  * @see StopWatch.h
7  *
8  * This test instantiates a stopwatch and makes some queries.
9  *
10  * It is not an actual test in that there is no automatic verification of the
11  * results (which would be somehow problematic given that times are not
12  * completely reproducible).
13  *
14  */
15 
16 // our libraries
17 #include "TestUtils/StopWatch.h"
18 
19 // C/C++ standard libraries
20 #include <chrono>
21 #include <ratio>
22 #include <thread> // std::this_thread
23 #include <iostream>
24 
25 
27  std::cout << " <waiting for " << dur.count() << " ms>" << std::endl;
28  std::this_thread::sleep_for(dur);
29 } // waitgst()
30 
31 
32 int main() {
33 
34  std::chrono::milliseconds WaitFor { 250 };
35 
36  std::cout << "Creating stop watch..." << std::endl;
37  testing::StopWatch<std::chrono::duration<double>, std::chrono::system_clock> timer;
38  std::cout
39  << " - elapsed time so far: "
40  << timer.elapsed()
41  << " s; partial time: "
42  << timer.partial()
43  << " s"
44  << std::endl;
45 
46  waitgst(WaitFor);
47  std::cout
48  << " - elapsed time so far: "
49  << timer.elapsed()
50  << " s ("
51  << timer.elapsed<std::micro>()
52  << " us); partial time: "
53  << timer.partial<std::micro>()
54  << " us"
55  << std::endl;
56 
57  std::cout
58  << "Stopping watch"
59  << std::endl;
60  timer.stop();
61  waitgst(WaitFor);
62  std::cout
63  << " - elapsed time so far: "
64  << timer.elapsed()
65  << " s ("
67  << " us); partial time: "
69  << " us"
70  << std::endl;
71 
72  std::cout
73  << "Resuming watch"
74  << std::endl;
75  timer.resume();
76  waitgst(WaitFor);
77  std::cout
78  << " - elapsed time so far: "
79  << timer.elapsed()
80  << " s ("
81  << timer.elapsed<std::chrono::seconds>()
82  << " s); partial time: "
83  << timer.partial<std::chrono::seconds>()
84  << " s"
85  << std::endl;
86 
87  std::cout
88  << "Restarting watch"
89  << std::endl;
90  timer.restart();
91  waitgst(WaitFor);
92  std::cout
93  << " - elapsed time so far: "
94  << timer.elapsed()
95  << " s ("
97  << " ms); partial time: "
99  << " ms"
100  << std::endl;
101 
102 
103  return 0;
104 } // main()
void stop()
Pauses the watch.
Definition: StopWatch.h:236
ElapsedTime_t elapsed() const
Returns the total time spent running since the last restart.
Definition: StopWatch.h:254
microsecond microseconds
Alias for common language habits.
Definition: spacetime.h:122
ElapsedTime_t partial() const
Returns the time spent running since the last resume.
Definition: StopWatch.h:264
void restart()
Restarts the watch; previous time is forgotten.
Definition: StopWatch.h:218
int main()
Provides time interval measurements.
Definition: StopWatch.h:79
second seconds
Alias for common language habits.
Definition: spacetime.h:88
millisecond milliseconds
Alias for common language habits.
Definition: spacetime.h:105
void resume()
Resumes the run of the watch; previous time is preserved.
Definition: StopWatch.h:227
void waitgst(std::chrono::milliseconds dur)
QTextStream & endl(QTextStream &s)