Test.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef CORE_TEST_HPP
  2. #define CORE_TEST_HPP
  3. #include "core/data/HashMap.hpp"
  4. #include "core/math/Vector.hpp"
  5. #include "core/utils/Logger.hpp"
  6. namespace Core::Test {
  7. namespace Internal {
  8. struct Result final {
  9. int tests = 0;
  10. int successTests = 0;
  11. };
  12. using FileName = String32<256>;
  13. extern HashMap<FileName, Result> results;
  14. void warn(const char* file, int line, Error e);
  15. template<typename T>
  16. bool check(const char* file, int line, const T& wanted, const T& actual,
  17. bool c) {
  18. file = Logger::getFileName(file);
  19. Error e = Error::NONE;
  20. FileName fileName;
  21. if(checkError(e, fileName.append(file))) {
  22. warn(file, line, e);
  23. return false;
  24. }
  25. Result* result = results.search(fileName);
  26. if(result == nullptr &&
  27. checkError(e, results.tryEmplace(result, fileName))) {
  28. warn(file, line, e);
  29. return false;
  30. }
  31. result->tests++;
  32. if(c) {
  33. result->successTests++;
  34. return true;
  35. }
  36. CORE_LOG_ERROR("#:# - expected '#' got '#'", fileName, line, wanted,
  37. actual);
  38. return false;
  39. }
  40. template<typename T>
  41. bool checkEqual(const char* file, int line, const T& wanted,
  42. const T& actual) {
  43. return check(file, line, wanted, actual, wanted == actual);
  44. }
  45. template<typename A, typename B>
  46. bool checkString(const char* file, int line, const A& wanted,
  47. const B& actual) {
  48. Error e = Error::NONE;
  49. String32<2048> a;
  50. if(checkError(e, a.append(wanted))) {
  51. warn(file, line, e);
  52. return false;
  53. }
  54. String32<2048> b;
  55. if(checkError(e, b.append(actual))) {
  56. warn(file, line, e);
  57. return false;
  58. }
  59. return checkEqual(file, line, a, b);
  60. }
  61. bool checkFloat(const char* file, int line, float wanted, float actual,
  62. float error);
  63. template<int N, typename T>
  64. bool checkVector(const char* file, int line,
  65. const Core::Vector<N, T>& wanted,
  66. const Core::Vector<N, T>& actual, float error) {
  67. bool result = true;
  68. for(int i = 0; i < N; i++) {
  69. result &= checkFloat(file, line, static_cast<float>(wanted[i]),
  70. static_cast<float>(actual[i]), error);
  71. }
  72. return result;
  73. }
  74. }
  75. void finalize();
  76. }
  77. #define CORE_TEST_EQUAL(wanted, actual) \
  78. Core::Test::Internal::checkEqual<Core::RemoveReference<decltype(actual)>>( \
  79. __FILE__, __LINE__, wanted, actual)
  80. #define CORE_TEST_STRING(wanted, actual) \
  81. Core::Test::Internal::checkString(__FILE__, __LINE__, wanted, actual)
  82. #define CORE_TEST_FALSE(actual) CORE_TEST_EQUAL(false, actual)
  83. #define CORE_TEST_TRUE(actual) CORE_TEST_EQUAL(true, actual)
  84. #define CORE_TEST_ERROR(actual) CORE_TEST_EQUAL(Core::Error::NONE, actual)
  85. #define CORE_TEST_NULL(actual) CORE_TEST_EQUAL(true, actual == nullptr)
  86. // double check to make the null check clear for the compiler
  87. #define CORE_TEST_NOT_NULL(actual) \
  88. (CORE_TEST_EQUAL(true, (actual) != nullptr) && (actual) != nullptr)
  89. #define CORE_TEST_FLOAT(wanted, actual, error) \
  90. Core::Test::Internal::checkFloat(__FILE__, __LINE__, wanted, actual, error)
  91. #define CORE_TEST_VECTOR(wanted, actual) \
  92. Core::Test::Internal::checkVector(__FILE__, __LINE__, wanted, actual, \
  93. 0.0001f)
  94. #endif