From a932079f21b204999d013fed8787627e640d55c3 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 5 Sep 2021 21:06:10 +0200 Subject: [PATCH] Test: Improve suite print formatting --- test/macro.h | 2 +- test/testsuite.cpp | 52 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/test/macro.h b/test/macro.h index f013ed9..ab23a69 100644 --- a/test/macro.h +++ b/test/macro.h @@ -11,7 +11,7 @@ #define EXPECT_EQ(a, b) \ if (a != b) { \ - std::cout << "FAIL: " << __FILE__ << ":" << __LINE__ \ + std::cout << "\033[31;1mFAIL\033[0m: " << __FILE__ << ":" << __LINE__ \ << ": EXPECT_EQ(" << #a << ", " << #b ") failed with" \ << " lhs='" << a << "' and rhs='" << b << "'" << std::endl; \ Test::TestSuite::the().currentTestCaseFailed(); \ diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 052839b..bcf878d 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -1,6 +1,7 @@ #include // size_t -#include // uint32_t -#include // fclose, fopen, printf, stdout +#include // fclose, fopen, printf, stdout +#include +#include #include "testsuite.h" #include "util/timer.h" @@ -20,30 +21,55 @@ TestSuite::~TestSuite() void TestSuite::run() { - printf("TestSuite: %d cases have been added!\n", (int)m_cases.size()); + const char* escapePass = "\033[42;30;1m"; + const char* escapeFail = "\033[41;1m"; + const char* escapeGreen = "\033[32m"; + const char* escapeGrey = "\033[37m"; + const char* escapeRed = "\033[31m"; + const char* escapeReset = "\033[0m"; - size_t caseFailedCount = 0; + printf("\n"); + printf("---- Running %zu Test Cases ----\n", m_cases.size()); Util::Timer totalTimer; - for (auto& testCase : m_cases) { - printf("Start %s\n", testCase.name()); + size_t caseFailedCount = 0; + for (std::vector::size_type i = 0; i < m_cases.size(); ++i) { + + printf(" START %s (%zu/%zu)\n", m_cases.at(i).name(), i + 1, m_cases.size()); m_currentTestCasePassed = true; Util::Timer caseTimer; - testCase.function()(); - printf(" %s, %luns\n", testCase.name(), caseTimer.elapsedNanoseconds()); + m_cases.at(i).function()(); + double elapsed = caseTimer.elapsedNanoseconds(); - if (!m_currentTestCasePassed) { + std::string state; + if (m_currentTestCasePassed) { + state.append(escapePass); + state.append(" PASS "); + state.append(escapeReset); + } + else { caseFailedCount++; + state.append(escapeFail); + state.append(" FAIL "); + state.append(escapeReset); } + + printf("%s %s %s(", state.c_str(), m_cases.at(i).name(), escapeGrey); + Util::Timer::fancyPrint(elapsed); + printf(")%s\n", escapeReset); } - uint32_t percentagePassed = (1 - caseFailedCount / (float)m_cases.size()) * 100; - printf("Passed %d%% of tests\n", percentagePassed); + printf("\n"); + printf("Tests: %s%zu failed%s, %s%zu passed%s, %zu total\n", + escapeRed, caseFailedCount, escapeReset, + escapeGreen, m_cases.size() - caseFailedCount, escapeReset, + m_cases.size()); - float elapsed = totalTimer.elapsedNanoseconds() / 1000000.0; - printf("Elapsed: %.3f milliseconds\n", elapsed); + printf("Time: "); + Util::Timer::fancyPrint(totalTimer.elapsedNanoseconds()); + printf("\n"); } } // namespace Test