Test.cppm 1.7 KB

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