Test.cppm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. print(
  17. "{}{}:{} - expected '{}' got '{}'{}\n", Terminal::FG_RED, file,
  18. l.line(), wanted, actual, Terminal::RESET);
  19. return false;
  20. }
  21. template<typename T>
  22. bool testTrue(const T& actual, SOURCE) {
  23. return test<T>(true, actual, l);
  24. }
  25. template<typename T>
  26. bool testFalse(const T& actual, SOURCE) {
  27. return test<T>(false, actual, l);
  28. }
  29. bool testString(const char* wanted, const char* actual, SOURCE);
  30. template<typename A, typename B>
  31. bool testString(const A& wanted, const B& actual, SOURCE) {
  32. char wantedString[512];
  33. size_t lw = toString(wanted, wantedString, sizeof(wantedString));
  34. char actualString[512];
  35. size_t la = toString(actual, actualString, sizeof(actualString));
  36. test(lw, la, l);
  37. return testString(
  38. static_cast<const char*>(wantedString),
  39. static_cast<const char*>(actualString), l);
  40. }
  41. bool testFloat(float wanted, float actual, float error, SOURCE);
  42. bool testNull(const void* p, SOURCE);
  43. bool testNotNull(const void* p, SOURCE);
  44. }