Test.h 3.7 KB

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