Test.cppm 1.7 KB

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