Test.hpp 3.7 KB

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