improve logging of tests

This commit is contained in:
mrbesen 2023-11-05 19:48:20 +01:00
parent b96ecbda8c
commit 99f515f821
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
1 changed files with 29 additions and 12 deletions

View File

@ -1,18 +1,22 @@
#include <stdio.h>
#include "test.h"
#include <map>
#include <string>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#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<std::chrono::high_resolution_clock> 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<int>(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<std::chrono::high_resolution_clock> 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<std::chrono::high_resolution_clock> testEnd = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double, std::milli> 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<double> t = end - start;
printf("Testing took: %fms\n", (t.count() * 1000));
const std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double, std::milli> t = end - start;
std::cout << "Testing took: " << t.count() << "ms" << std::endl;
return failcount > 0;
}