Test.cppm 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. module;
  2. export module Core.Test;
  3. import Core.Logger;
  4. import Core.TerminalConstants;
  5. import Core.ToString;
  6. import Core.Std;
  7. #define SOURCE const std::source_location& l = std::source_location::current()
  8. export namespace Core {
  9. void finalizeTests(void);
  10. bool addTestResult(const char* file, bool comparison);
  11. template<typename A, typename B>
  12. bool test(const A& wanted, const B& actual, SOURCE) {
  13. const char* file = getShortFileName(l.file_name());
  14. if(addTestResult(file, static_cast<const B&>(wanted) == actual)) {
  15. return true;
  16. }
  17. char buffer[512];
  18. formatBuffer(
  19. buffer, sizeof(buffer), "#@#:# - expected '#' got '#'#",
  20. Terminal::FG_RED, file, l.line(), wanted, actual, Terminal::RESET);
  21. puts(buffer);
  22. return false;
  23. }
  24. template<typename T>
  25. bool testTrue(const T& actual, SOURCE) {
  26. return test<T>(true, actual, l);
  27. }
  28. template<typename T>
  29. bool testFalse(const T& actual, SOURCE) {
  30. return test<T>(false, actual, l);
  31. }
  32. bool testString(const char* wanted, const char* actual, SOURCE);
  33. template<typename A, typename B>
  34. bool testString(const A& wanted, const B& actual, SOURCE) {
  35. char wantedString[512];
  36. size_t lw = toString(wanted, wantedString, sizeof(wantedString));
  37. char actualString[512];
  38. size_t la = toString(actual, actualString, sizeof(actualString));
  39. test(lw, la, l);
  40. return testString(
  41. static_cast<const char*>(wantedString),
  42. static_cast<const char*>(actualString), l);
  43. }
  44. bool testFloat(float wanted, float actual, float error, SOURCE);
  45. bool testNull(const void* p, SOURCE);
  46. bool testNotNull(const void* p, SOURCE);
  47. }