UnitTestFramework.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Core/Atomics.h>
  6. #include <Jolt/Math/DVec3.h>
  7. #include <Jolt/Math/Float2.h>
  8. // Disable common warnings
  9. JPH_SUPPRESS_WARNINGS
  10. JPH_CLANG_SUPPRESS_WARNING("-Wheader-hygiene")
  11. #ifdef JPH_DOUBLE_PRECISION
  12. JPH_CLANG_SUPPRESS_WARNING("-Wdouble-promotion")
  13. #endif // JPH_DOUBLE_PRECISION
  14. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  15. #include "doctest.h"
  16. JPH_SUPPRESS_WARNINGS_STD_END
  17. using namespace JPH;
  18. using namespace JPH::literals;
  19. using namespace std;
  20. inline void CHECK_APPROX_EQUAL(float inLHS, float inRHS, float inTolerance = 1.0e-6f)
  21. {
  22. CHECK(abs(inRHS - inLHS) <= inTolerance);
  23. }
  24. inline void CHECK_APPROX_EQUAL(double inLHS, double inRHS, double inTolerance = 1.0e-6)
  25. {
  26. CHECK(abs(inRHS - inLHS) <= inTolerance);
  27. }
  28. inline void CHECK_APPROX_EQUAL(Vec3Arg inLHS, Vec3Arg inRHS, float inTolerance = 1.0e-6f)
  29. {
  30. CHECK(inLHS.IsClose(inRHS, inTolerance * inTolerance));
  31. }
  32. inline void CHECK_APPROX_EQUAL(Vec4Arg inLHS, Vec4Arg inRHS, float inTolerance = 1.0e-6f)
  33. {
  34. CHECK(inLHS.IsClose(inRHS, inTolerance * inTolerance));
  35. }
  36. inline void CHECK_APPROX_EQUAL(Mat44Arg inLHS, Mat44Arg inRHS, float inTolerance = 1.0e-6f)
  37. {
  38. CHECK(inLHS.IsClose(inRHS, inTolerance * inTolerance));
  39. }
  40. inline void CHECK_APPROX_EQUAL(DMat44Arg inLHS, DMat44Arg inRHS, float inTolerance = 1.0e-6f)
  41. {
  42. CHECK(inLHS.IsClose(inRHS, inTolerance * inTolerance));
  43. }
  44. inline void CHECK_APPROX_EQUAL(QuatArg inLHS, QuatArg inRHS, float inTolerance = 1.0e-6f)
  45. {
  46. bool close = inLHS.IsClose(inRHS, inTolerance * inTolerance) || inLHS.IsClose(-inRHS, inTolerance * inTolerance);
  47. CHECK(close);
  48. }
  49. inline void CHECK_APPROX_EQUAL(DVec3Arg inLHS, DVec3Arg inRHS, double inTolerance = 1.0e-6)
  50. {
  51. CHECK(inLHS.IsClose(inRHS, inTolerance * inTolerance));
  52. }
  53. inline void CHECK_APPROX_EQUAL(const Float2 &inLHS, const Float2 &inRHS, float inTolerance = 1.0e-6f)
  54. {
  55. Float2 diff(inLHS.x - inRHS.x, inLHS.y - inRHS.y);
  56. CHECK(Square(diff.x) + Square(diff.y) < inTolerance * inTolerance);
  57. }
  58. // Define the exact random number generator we want to use across platforms for consistency (default_random_engine's implementation is platform specific)
  59. using UnitTestRandom = mt19937;
  60. #ifdef JPH_ENABLE_ASSERTS
  61. // Stack based object that tests for an assert
  62. class ExpectAssert
  63. {
  64. public:
  65. /// Expect inCount asserts
  66. explicit ExpectAssert(int inCount)
  67. {
  68. CHECK(sCount == 0);
  69. sCount = inCount;
  70. mPrevAssertFailed = AssertFailed;
  71. AssertFailed = [](const char*, const char*, const char*, uint)
  72. {
  73. --sCount;
  74. return false;
  75. };
  76. }
  77. /// Verifies that the expected number of asserts were triggered
  78. ~ExpectAssert()
  79. {
  80. AssertFailed = mPrevAssertFailed;
  81. CHECK(sCount == 0);
  82. }
  83. private:
  84. // Keeps track of number of asserts that are expected
  85. inline static atomic<int> sCount { 0 };
  86. // Previous assert function
  87. AssertFailedFunction mPrevAssertFailed;
  88. };
  89. #endif // JPH_ENABLE_ASSERTS