new testing

This commit is contained in:
MrBesen 2021-11-12 16:36:49 +01:00
parent 82804630b2
commit 28b38f2734
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
4 changed files with 61 additions and 12 deletions

View File

@ -1,26 +1,43 @@
#include <stdio.h>
#include "test.h"
//tests
#include <map>
#include <string>
#include <chrono>
test_t tests[] = {NULL};
#define RED "\033[1;91m"
#define GREEN "\033[1;92m"
#define AQUA "\033[1;36m"
#define RESET "\033[;1m"
extern std::map<std::string, test_t> tests;
void loadTests();
int main(int argc, char** argv) {
auto start = std::chrono::high_resolution_clock::now();
loadTests();
test_t* current = tests;
int failcount = 0;
int testcount = 0;
for(; *current; current++) {
testcount++;
printf("\033[1mRunning test number: %d ", testcount);
if((*current)()) {
printf("\033[1;92msucceeded\033[0;1m!\n");
int testcount = tests.size();
int testnumber = 0;
for(std::map<std::string, test_t>::iterator current = tests.begin(); current != tests.end(); ++current) {
printf("\033[1mRunning test: %d/%d " AQUA "%s " RESET, ++testnumber, testcount, current->first.c_str());
if((current->second)()) {
printf(GREEN "succeeded" RESET "!\n");
} else {
printf("\033[1;91mfailed\033[0;1m\n");
printf(RED "failed" RESET "\n");
failcount++;
}
}
printf("\033[1;93m%d\033[0;1m/%d failed\n", failcount, testcount);
const char* color = (failcount > 0 ? RED : GREEN); // red or green
printf("%s%d" RESET "/%d failed\n", color, failcount, testcount);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> t = end - start;
printf("Testing took: %fms\n", (t.count() * 1000));
return failcount > 0;
}

11
tests/sampletest.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "test.h"
TEST(ABC) {
CMPASSERT(1, true);
} TESTEND
TEST(CDE) {
CMPASSERT(0, true);
} TESTEND

View File

@ -5,8 +5,13 @@
#define TESTDATA "./tests/data/"
#define TESTNAME(NAME) test_##NAME
#define TESTFUNC(NAME) bool TESTNAME(NAME)()
#define TEST(NAME) TESTFUNC(NAME) {
#define TESTEND return TESTGOOD; }
#define ASSERT(BED, ERR) if(!(BED)) { std::cout << __FILE__ << ":" << __LINE__ << " " << ERR << std::endl; return TESTFAILED; }
#define CMPASSERTE(A, B, ERR) if( !((A) == (B))) { std::cout << __FILE__ << ":" << __LINE__ << " is: \"" << (A) << "\" should: \"" << (B) << "\""<< std::endl; return TESTFAILED; }
#define CMPASSERT(A, B) CMPASSERTE(A, B, "")
typedef int (*test_t)();
typedef bool (*test_t)();

16
tests/tests.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "test.h"
#include <map>
#include <string>
#define REGISTERTEST(NAME) tests.insert({#NAME, TESTNAME(NAME)})
TESTFUNC(ABC);
TESTFUNC(CDE);
std::map<std::string, test_t> tests;
void loadTests() {
REGISTERTEST(ABC);
REGISTERTEST(CDE);
}