fpumath_test.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/fpumath.h>
  7. #if !BX_COMPILER_MSVC || BX_COMPILER_MSVC >= 1800
  8. #include <cmath>
  9. TEST_CASE("isFinite, isInfinite, isNan", "")
  10. {
  11. for (uint64_t ii = 0; ii < UINT32_MAX; ii += rand()%(1<<13)+1)
  12. {
  13. union { uint32_t ui; float f; } u = { uint32_t(ii) };
  14. REQUIRE(std::isnan(u.f) == bx::isNan(u.f) );
  15. REQUIRE(std::isfinite(u.f) == bx::isFinite(u.f) );
  16. REQUIRE(std::isinf(u.f) == bx::isInfinite(u.f) );
  17. }
  18. }
  19. #endif // !BX_COMPILER_MSVC || BX_COMPILER_MSVC >= 1800
  20. void mtxCheck(const float* _a, const float* _b)
  21. {
  22. if (!bx::fequal(_a, _b, 16, 0.01f) )
  23. {
  24. DBG("\n"
  25. "A:\n"
  26. "%10.4f %10.4f %10.4f %10.4f\n"
  27. "%10.4f %10.4f %10.4f %10.4f\n"
  28. "%10.4f %10.4f %10.4f %10.4f\n"
  29. "%10.4f %10.4f %10.4f %10.4f\n"
  30. "B:\n"
  31. "%10.4f %10.4f %10.4f %10.4f\n"
  32. "%10.4f %10.4f %10.4f %10.4f\n"
  33. "%10.4f %10.4f %10.4f %10.4f\n"
  34. "%10.4f %10.4f %10.4f %10.4f\n"
  35. , _a[ 0], _a[ 1], _a[ 2], _a[ 3]
  36. , _a[ 4], _a[ 5], _a[ 6], _a[ 7]
  37. , _a[ 8], _a[ 9], _a[10], _a[11]
  38. , _a[12], _a[13], _a[14], _a[15]
  39. , _b[ 0], _b[ 1], _b[ 2], _b[ 3]
  40. , _b[ 4], _b[ 5], _b[ 6], _b[ 7]
  41. , _b[ 8], _b[ 9], _b[10], _b[11]
  42. , _b[12], _b[13], _b[14], _b[15]
  43. );
  44. CHECK(false);
  45. }
  46. }
  47. TEST_CASE("quaternion", "")
  48. {
  49. float mtxQ[16];
  50. float mtx[16];
  51. float quat[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
  52. bx::mtxQuat(mtxQ, quat);
  53. bx::mtxIdentity(mtx);
  54. mtxCheck(mtxQ, mtx);
  55. float ax = bx::pi/27.0f;
  56. float ay = bx::pi/13.0f;
  57. float az = bx::pi/7.0f;
  58. bx::quatRotateX(quat, ax);
  59. bx::mtxQuat(mtxQ, quat);
  60. bx::mtxRotateX(mtx, ax);
  61. mtxCheck(mtxQ, mtx);
  62. float euler[3];
  63. bx::quatToEuler(euler, quat);
  64. CHECK(bx::fequal(euler[0], ax, 0.001f) );
  65. bx::quatRotateY(quat, ay);
  66. bx::mtxQuat(mtxQ, quat);
  67. bx::mtxRotateY(mtx, ay);
  68. mtxCheck(mtxQ, mtx);
  69. bx::quatToEuler(euler, quat);
  70. CHECK(bx::fequal(euler[1], ay, 0.001f) );
  71. bx::quatRotateZ(quat, az);
  72. bx::mtxQuat(mtxQ, quat);
  73. bx::mtxRotateZ(mtx, az);
  74. mtxCheck(mtxQ, mtx);
  75. bx::quatToEuler(euler, quat);
  76. CHECK(bx::fequal(euler[2], az, 0.001f) );
  77. }