From 99f515f821081590253b91551c0eb232de079546 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sun, 5 Nov 2023 19:48:20 +0100 Subject: [PATCH] improve logging of tests --- tests/main.cpp | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/tests/main.cpp b/tests/main.cpp index e680fee..c4c9dfe 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,18 +1,22 @@ -#include #include "test.h" -#include -#include +#include #include +#include +#include +#include +#include #define RED "\033[1;91m" #define GREEN "\033[1;92m" #define YELLOW "\033[1;93m" #define AQUA "\033[1;36m" +#define GRAY "\033[1;38;5;244m" #define RESET "\033[;1m" + int main(int argc, char** argv) { - auto start = std::chrono::high_resolution_clock::now(); + const std::chrono::time_point start = std::chrono::high_resolution_clock::now(); testdef* startit = &__start_testlist, *endit = &__stop_testlist; int failcount = 0; @@ -20,35 +24,48 @@ int main(int argc, char** argv) { int testcount = endit-startit; int testnumber = 0; + // get the maximum length of a test name + int testNameMaxLen = 0; + for(testdef* it = startit; it != endit; ++it) { + testNameMaxLen = std::max(testNameMaxLen, std::string(it->name).size()); + } + // go through back -> front (tests are inserted in reverse order) for(testdef* it = startit + testcount-1; it >= startit; --it) { - printf("\033[1mRunning test: %d/%d " AQUA "%s " RESET, ++testnumber, testcount, it->name); + const std::string testName(it->name); + const std::string namePadding((int) (testNameMaxLen - testName.size()) + 1, ' '); + std::cout << RESET "Running test:" << std::setfill(' ') << std::setw(std::log10(testcount)+2) << ++testnumber << '/' << testcount << " " AQUA << testName << RESET << namePadding; // run test int result = TESTFAILED; + const std::chrono::time_point testStart = std::chrono::high_resolution_clock::now(); try { result = (it->testf)(); } catch(std::exception& e) { std::cout << "catched exception: \"" << e.what() << "\" " << std::flush; + result = TESTFAILED; } catch(...) {} + const std::chrono::time_point testEnd = std::chrono::high_resolution_clock::now(); + const std::chrono::duration testDuration(testEnd-testStart); + std::cout << std::fixed << std::setprecision(1); if(result == TESTGOOD) { - printf(GREEN "succeeded" RESET "!\n"); + std::cout << GREEN "succeeded" RESET "! " GRAY << testDuration.count() << "ms" RESET << std::endl; } else if(result == TESTSKIPPED) { - printf(YELLOW "skipped" RESET "!\n"); + std::cout << YELLOW " skipped" RESET "! " GRAY << testDuration.count() << "ms" RESET << std::endl; skipcount++; } else { - printf(RED "failed" RESET "\n"); + std::cout << RED " failed" RESET "! " GRAY << testDuration.count() << "ms" RESET << std::endl; failcount++; } } const char* color = (failcount > 0 ? RED : GREEN); // red or green - printf("%s%d" RESET "/%d failed (" YELLOW "%d " RESET "skipped)\n", color, failcount, testcount, skipcount); + std::cout << color << failcount << RESET "/" << testcount << " failed (" YELLOW << skipcount << RESET " skipped)" << std::endl; - auto end = std::chrono::high_resolution_clock::now(); - std::chrono::duration t = end - start; - printf("Testing took: %fms\n", (t.count() * 1000)); + const std::chrono::time_point end = std::chrono::high_resolution_clock::now(); + const std::chrono::duration t = end - start; + std::cout << "Testing took: " << t.count() << "ms" << std::endl; return failcount > 0; }