72 lines
2.7 KiB
C++
72 lines
2.7 KiB
C++
#include "test.h"
|
|
|
|
#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) {
|
|
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;
|
|
int skipcount = 0;
|
|
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) {
|
|
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) {
|
|
std::cout << GREEN "succeeded" RESET "! " GRAY << testDuration.count() << "ms" RESET << std::endl;
|
|
} else if(result == TESTSKIPPED) {
|
|
std::cout << YELLOW " skipped" RESET "! " GRAY << testDuration.count() << "ms" RESET << std::endl;
|
|
skipcount++;
|
|
} else {
|
|
std::cout << RED " failed" RESET "! " GRAY << testDuration.count() << "ms" RESET << std::endl;
|
|
failcount++;
|
|
}
|
|
}
|
|
|
|
const char* color = (failcount > 0 ? RED : GREEN); // red or green
|
|
std::cout << color << failcount << RESET "/" << testcount << " failed (" YELLOW << skipcount << RESET " skipped)" << std::endl;
|
|
|
|
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;
|
|
}
|