netxsimdg
Loading...
Searching...
No Matches
Timer_test.cpp
Go to the documentation of this file.
1
8#include "include/Timer.hpp"
9
10#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
11#include <doctest/doctest.h>
12
13#include <chrono>
14#include <iostream>
15#include <sstream>
16#include <thread>
17
18TEST_SUITE_BEGIN("Timer");
19TEST_CASE("Test a timer")
20{
22 Nextsim::Timer::main.tick("Level 1");
23 std::this_thread::sleep_for(std::chrono::milliseconds(20));
24 Nextsim::Timer::main.tick("Level 2a");
25 std::this_thread::sleep_for(std::chrono::milliseconds(15));
26 Nextsim::Timer::main.tock("Level 2a");
27 Nextsim::Timer::main.tick("Level 2b");
28 std::this_thread::sleep_for(std::chrono::milliseconds(35));
29 Nextsim::Timer::main.tick("Level π");
30 std::this_thread::sleep_for(std::chrono::milliseconds(11));
31 Nextsim::Timer::main.tock("Level π");
32 Nextsim::Timer::main.tick("Level e+½");
33 std::this_thread::sleep_for(std::chrono::milliseconds(11));
34 Nextsim::Timer::main.tock("Level e+½");
35 Nextsim::Timer::main.tock("Level 2b");
36 Nextsim::Timer::main.tick("Level 2a");
37 std::this_thread::sleep_for(std::chrono::milliseconds(1));
38 Nextsim::Timer::main.tick("Level 3");
39 std::this_thread::sleep_for(std::chrono::milliseconds(11));
40 Nextsim::Timer::main.tock("Level 3");
41 Nextsim::Timer::main.tock("Level 2a");
42 Nextsim::Timer::main.tock("Level 1");
43 Nextsim::Timer::main.tick("Level 1bis");
44 std::this_thread::sleep_for(std::chrono::milliseconds(20));
45 Nextsim::Timer::main.tock("Level 1bis");
46
47 std::stringstream sout;
49 std::cout << sout.str() << std::endl;
50
51 // TODO: Parse the output to actually test something
52}
53
54void timeAndSleep()
55{
56 Nextsim::Timer::main.tick("time and sleep");
57 std::this_thread::sleep_for(std::chrono::milliseconds(45));
59}
60
61TEST_CASE("Test the scope dependent timer")
62{
64 Nextsim::Timer::main.tick("test scope timer");
65 {
66 Nextsim::Timer::main.tick("local scope timer");
67 std::this_thread::sleep_for(std::chrono::milliseconds(35));
69 }
70 timeAndSleep();
71
72 const int nint = 10;
73 for (int i = 0; i < nint; ++i) {
74 Nextsim::Timer::main.tick("loop timer");
75 std::this_thread::sleep_for(std::chrono::milliseconds(5));
77 }
78
79 for (int i = 0; i < nint; ++i) {
80 Nextsim::Timer::main.tick("loop timer 2");
81 timeAndSleep();
83 }
85
86 std::cout << Nextsim::Timer::main << std::endl;
87}
88TEST_SUITE_END();
89
void tick(const Key &timerName)
Starts a named timer.
Definition Timer.cpp:35
void reset()
Deletes all timers except the root, which is reset.
Definition Timer.cpp:110
void tock(const Key &timerName)
Stops a named timer.
Definition Timer.cpp:48
static Timer main
Static timer for general use.
Definition Timer.hpp:109