fork/slippc
git clone https://git.julia.blue/fork/slippc
2589656
0.8.1
1#ifndef TESTS_H_ 2#define TESTS_H_ 3 4#include <string> 5#include <filesystem> 6 7#include "util.h" 8#include "parser.h" 9#include "analyzer.h" 10#include "compressor.h" 11 12#ifdef _WIN32 13#include <Windows.h> //sleep() 14#else 15#include <unistd.h> //sleep() 16#endif 17 18#include <algorithm> 19 20namespace slip { 21 22typedef struct _test_suite { 23const char * name ; 24unsigned p = 0 ;//pass 25unsigned w = 0 ;//warn 26unsigned f = 0 ;//fail 27unsigned e = 0 ;//error 28unsigned t = 0 ;//total 29_test_suite * next ; 30}test_suite ; 31 32static unsigned __tests_total = 0 ; 33static unsigned __tests_passed = 0 ; 34static unsigned __tests_warned = 0 ; 35static unsigned __tests_failed = 0 ; 36static unsigned __tests_errors = 0 ; 37static test_suite * suites = NULL ,* tail = NULL ; 38static test_suite * s ; 39 40static bool __test_passed__ = 1 ; 41 42#define NEART (x ,y ,t ) \ 43 ((((y)-(t)) <= (x)) && (((y)+(t)) >= (x))) 44#define NEAR (x ,y ) NEART((x),(y),0.001f) 45 46#define TSUITE (tag ) \ 47 std::cout << "----------\nTesting suite " << CYN << (tag) << BLN << std::endl; \ 48 s = (test_suite*)calloc(1,sizeof(test_suite)); \ 49 s->name = (tag); \ 50 if(!tail) {suites = s;} \ 51 else {tail->next = s;} \ 52 tail = s; 53 54#define TPASS () \ 55 std::cout << "\r " << GRN << " pass\n" << BLN; \ 56 ++__tests_total; \ 57 ++__tests_passed; \ 58 ++(tail->t); \ 59 ++(tail->p); 60#define TWARN () \ 61 std::cout << "\r " << YLW << " warn\n" << BLN; \ 62 ++__tests_total; \ 63 ++__tests_warned; \ 64 ++(tail->t); \ 65 ++(tail->w); 66#define TFAIL () \ 67 std::cout << "\r " << RED << " fail\n" << BLN; \ 68 ++__tests_total; \ 69 ++__tests_failed; \ 70 ++(tail->t); \ 71 ++(tail->f); 72#define TERR (e ) \ 73 std::cout << "\r " << CRT << "error\n" << BLN; \ 74 ++__tests_total; \ 75 ++__tests_errors; \ 76 ++(tail->t); \ 77 ++(tail->e); 78 79#define BAILONFAIL (r ) if (!__test_passed__) { return (r); } 80#define NEXTONFAIL () if (!__test_passed__) { continue; } 81 82#define ASSERTNOERR (name ,expr ,onfail ) \ 83 std::cout << " " << (name) << BLN; \ 84 if ((expr)) { TPASS(); __test_passed__ = 1; } else { TFAIL(); __test_passed__ = 0; std::cout << " " << RED << onfail << BLN << " (Asserted: " << RED << #expr << BLN << ")" << std::endl;} 85 86#define ASSERT (name ,expr ,onfail ) \ 87 std::cout << " " << (name) << BLN; \ 88 try { \ 89 if ((expr)) { TPASS(); __test_passed__ = 1; } else { TFAIL(); __test_passed__ = 0; std::cout << " " << RED << onfail << BLN << " (Asserted: " << RED << #expr << BLN << ")" << std::endl;} \ 90 } catch (const std::exception& e) { TERR(e); __test_passed__ = 0; } 91 92#define SUGGEST (name ,expr ,onfail ) \ 93 std::cout << " " << (name) << BLN; \ 94 try { \ 95 if ((expr)) { TPASS(); __test_passed__ = 1; } else { TWARN(); __test_passed__ = 0; std::cout << " " << YLW << onfail << BLN << " (Suggested: " << YLW << #expr << BLN << ")" << std::endl;} \ 96 } catch (const std::exception& e) { TERR(e); __test_passed__ = 0; } 97 98#define TESTRESULTS () \ 99 std::cout << "----------TEST SUMMARY----------" << std::endl; \ 100 std::cout << GRN << " " << __tests_passed << "/" << __tests_total << " tests passed" << " (" << ((100.0*__tests_passed)/(__tests_passed+__tests_failed+__tests_warned+__tests_errors))<< "%)" << BLN; \ 101 if (__tests_warned) { \ 102 std::cout << " with " << YLW << __tests_warned << " warning" << (__tests_warned==1 ? "" : "s") << BLN; \ 103 if (__tests_failed) { \ 104 std::cout << " and " << RED << __tests_failed << " failure" << (__tests_failed==1 ? "" : "s") << BLN; \ 105 } \ 106 if (__tests_errors) { \ 107 std::cout << " and " << CRT << __tests_errors << " error" << (__tests_errors==1 ? "" : "s") << BLN; \ 108 } \ 109 } else if (__tests_failed) { \ 110 std::cout << " with " << RED << __tests_failed << " failure" << (__tests_failed==1 ? "" : "s") << BLN; \ 111 if (__tests_errors) { \ 112 std::cout << " and " << CRT << __tests_errors << " error" << (__tests_errors==1 ? "" : "s") << BLN; \ 113 } \ 114 } else if (__tests_errors) { \ 115 std::cout << " with " << CRT << __tests_errors << " error" << (__tests_errors==1 ? "" : "s") << BLN; \ 116 } \ 117 std::cout << std::endl; \ 118 for(test_suite* s = suites; s; ) { \ 119 printf(" %s%40s%s: %3u test%s passed (%3.0f%%)",s->e ? CRT : s->f ? RED : s->w ? YLW : GRN,s->name,BLN,s->p,s->p==1 ? " " : "s",(100.0f*s->p/s->t)); \ 120 if (s->w) { \ 121 printf(" with %s%u warning%s%s",YLW,s->w,s->w==1 ? "" : "s",BLN);\ 122 if (s->f) { \ 123 printf(" and %s%u failure%s%s",RED,s->f,s->f==1 ? "" : "s",BLN);\ 124 }\ 125 if (s->e) { \ 126 printf(" and %s%u error%s%s",CRT,s->e,s->e==1 ? "" : "s",BLN);\ 127 }\ 128 } else if (s->f) { \ 129 printf(" with %s%u failure%s%s",RED,s->f,s->f==1 ? "" : "s",BLN);\ 130 if (s->e) { \ 131 printf(" and %s%u error%s%s",CRT,s->e,s->e==1 ? "" : "s",BLN);\ 132 }\ 133 } else if (s->e) { \ 134 printf(" with %s%u error%s%s",CRT,s->e,s->e==1 ? "" : "s",BLN);\ 135 }\ 136 printf("\n"); \ 137 struct _test_suite* next = s->next; \ 138 free(s); \ 139 s = next; \ 140 } \ 141 std::cout << "--------END TEST SUMMARY--------" << std::endl; \ 142 143} 144 145#endif /* TESTS_H_ */ 146