| 1 |
#include <pbcopper/utility/TimeReporter.h> |
| 2 |
|
| 3 |
#include <functional> |
| 4 |
#include <sstream> |
| 5 |
|
| 6 |
#include <gtest/gtest.h> |
| 7 |
|
| 8 |
#include <pbcopper/logging/Logging.h> |
| 9 |
|
| 10 |
using namespace PacBio; |
| 11 |
|
| 12 |
namespace TimeReporterTests { |
| 13 |
|
| 14 |
using TestFunction = std::function<void(void)>; |
| 15 |
|
| 16 |
std::string TestLog(const PacBio::Logging::LogLevel loggerLevel, const TestFunction& func) |
| 17 |
{ |
| 18 |
std::ostringstream out; |
| 19 |
{ |
| 20 |
// setup local logger |
| 21 |
Logging::Logger logger{out, loggerLevel}; |
| 22 |
Logging::Logger::Current(&logger); |
| 23 |
|
| 24 |
// run test block |
| 25 |
func(); |
| 26 |
|
| 27 |
// reset default logging |
| 28 |
Logging::Logger::Current(&Logging::Logger::Default()); |
| 29 |
} |
| 30 |
|
| 31 |
// return log output |
| 32 |
return out.str(); |
| 33 |
} |
| 34 |
|
| 35 |
} // namespace TimeReporterTests |
| 36 |
|
| 37 |
// clang-format off |
| 38 |
|
| 39 |
TEST(Utility_TimeReporter, can_log_default_message) |
| 40 |
{ |
| 41 |
const Logging::LogLevel runtimeLogLevel = Logging::LogLevel::INFO; |
| 42 |
const std::string output = TimeReporterTests::TestLog(runtimeLogLevel, []() |
| 43 |
{ |
| 44 |
Utility::TimeReporter tr{Logging::LogLevel::INFO}; |
| 45 |
}); |
| 46 |
|
| 47 |
EXPECT_TRUE(output.find("Finished in ") != std::string::npos); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(Utility_TimeReporter, does_not_log_on_incompatible_log_level) |
| 51 |
{ |
| 52 |
const Logging::LogLevel runtimeLogLevel = Logging::LogLevel::WARN; |
| 53 |
const std::string output = TimeReporterTests::TestLog(runtimeLogLevel, []() |
| 54 |
{ |
| 55 |
Utility::TimeReporter tr{Logging::LogLevel::INFO}; |
| 56 |
}); |
| 57 |
|
| 58 |
EXPECT_TRUE(output.empty()); |
| 59 |
} |
| 60 |
|
| 61 |
TEST(Utility_TimeReporter, can_log_using_custom_callback_printer) |
| 62 |
{ |
| 63 |
const Logging::LogLevel runtimeLogLevel = Logging::LogLevel::DEBUG; |
| 64 |
const std::string output = TimeReporterTests::TestLog(runtimeLogLevel, []() |
| 65 |
{ |
| 66 |
int count = 0; |
| 67 |
double score = 0.0; |
| 68 |
|
| 69 |
auto ScoreReport = [&](const double nsec) { |
| 70 |
std::ostringstream msg; |
| 71 |
msg << "Found " << count << " alignments with average score " |
| 72 |
<< score << " in " << Utility::Stopwatch::PrettyPrintNanoseconds(nsec); |
| 73 |
return msg.str(); |
| 74 |
}; |
| 75 |
|
| 76 |
Utility::TimeReporter tr{Logging::LogLevel::INFO, ScoreReport}; |
| 77 |
|
| 78 |
// task updates some values before the reporter prints in its dtor |
| 79 |
count = 42; |
| 80 |
score = 0.77; |
| 81 |
}); |
| 82 |
|
| 83 |
EXPECT_TRUE(output.find("Found 42 alignments with average score 0.77 in ") != |
| 84 |
std::string::npos); |
| 85 |
} |
| 86 |
|
| 87 |
// clang-format on |