diff --git a/tests/main.cpp b/tests/main.cpp index f55c453..4168da5 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,26 +1,43 @@ #include #include "test.h" -//tests +#include +#include +#include -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 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::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 t = end - start; + printf("Testing took: %fms\n", (t.count() * 1000)); + return failcount > 0; } diff --git a/tests/sampletest.cpp b/tests/sampletest.cpp new file mode 100644 index 0000000..926bc2a --- /dev/null +++ b/tests/sampletest.cpp @@ -0,0 +1,11 @@ +#include "test.h" + +TEST(ABC) { + CMPASSERT(1, true); + +} TESTEND + +TEST(CDE) { + CMPASSERT(0, true); + +} TESTEND \ No newline at end of file diff --git a/tests/test.h b/tests/test.h index fb492f1..1cad15b 100644 --- a/tests/test.h +++ b/tests/test.h @@ -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)(); diff --git a/tests/tests.cpp b/tests/tests.cpp new file mode 100644 index 0000000..64419ee --- /dev/null +++ b/tests/tests.cpp @@ -0,0 +1,16 @@ +#include "test.h" + +#include +#include + +#define REGISTERTEST(NAME) tests.insert({#NAME, TESTNAME(NAME)}) + +TESTFUNC(ABC); +TESTFUNC(CDE); + +std::map tests; + +void loadTests() { + REGISTERTEST(ABC); + REGISTERTEST(CDE); +} \ No newline at end of file