forked from CLIUtils/CLI11
/
TimerTest.cpp
64 lines (56 loc) · 1.73 KB
/
TimerTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "CLI/Timer.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <chrono>
#include <sstream>
#include <string>
#include <thread>
using ::testing::HasSubstr;
TEST(Timer, MSTimes) {
CLI::Timer timer{"My Timer"};
std::this_thread::sleep_for(std::chrono::milliseconds(123));
std::string output = timer.to_string();
std::string new_output = (timer / 1000000).to_string();
EXPECT_THAT(output, HasSubstr("My Timer"));
EXPECT_THAT(output, HasSubstr(" ms"));
EXPECT_THAT(new_output, HasSubstr(" ns"));
}
/* Takes too long
TEST(Timer, STimes) {
CLI::Timer timer;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::string output = timer.to_string();
EXPECT_THAT(output, HasSubstr(" s"));
}
*/
// Fails on Windows
// TEST(Timer, UStimes) {
// CLI::Timer timer;
// std::this_thread::sleep_for(std::chrono::microseconds(2));
// std::string output = timer.to_string();
// EXPECT_THAT(output, HasSubstr(" ms"));
//}
TEST(Timer, BigTimer) {
CLI::Timer timer{"My Timer", CLI::Timer::Big};
std::string output = timer.to_string();
EXPECT_THAT(output, HasSubstr("Time ="));
EXPECT_THAT(output, HasSubstr("-----------"));
}
TEST(Timer, AutoTimer) {
CLI::AutoTimer timer;
std::string output = timer.to_string();
EXPECT_THAT(output, HasSubstr("Timer"));
}
TEST(Timer, PrintTimer) {
std::stringstream out;
CLI::AutoTimer timer;
out << timer;
std::string output = out.str();
EXPECT_THAT(output, HasSubstr("Timer"));
}
TEST(Timer, TimeItTimer) {
CLI::Timer timer;
std::string output = timer.time_it([]() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); }, .1);
std::cout << output << std::endl;
EXPECT_THAT(output, HasSubstr("ms"));
}