1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00
CLI11/tests/TimerTest.cpp
Henry Fredrick Schreiner 416e6a0443 Adding division to timer
2017-05-15 11:10:55 -04:00

66 lines
1.7 KiB
C++

#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "CLI/Timer.hpp"
#include <string>
#include <chrono>
#include <thread>
#include <sstream>
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::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"));
}