Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

math.cpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*!
  2. * \file test/math.cpp
  3. * \brief Mat Math Unit Test
  4. *
  5. * \todo Also test the geometric methods (intersection, distance, midpoint)
  6. *
  7. * \author xythobuz
  8. */
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <MatMath.h>
  12. #include "greatest.h"
  13. vec_t testDegrees[] = { 0.0f, 30.0f, 45.0f, 60.0f, 90.0f, 360.0f, -100.0f };
  14. vec_t testRads[] = { 0.0f, 0.5236f, 0.7854f, 1.0472f, 1.5708f, 6.2832f, -1.7453f };
  15. vec_t testBounds[][2] = {
  16. { 0.0f, 1.0f },
  17. { -5.0f, 5.0f },
  18. { 1.0f, 1.0f },
  19. };
  20. TEST constants() {
  21. ASSERTm("Pi not correct!", equalEpsilon(HEL_PI, (vec_t)M_PI));
  22. ASSERTm("2Pi not correct!", equalEpsilon(HEL_2_PI, ((vec_t)M_PI * 2.0f)));
  23. ASSERTm("Pi/4 not correct!", equalEpsilon(HEL_PI_OVER_4, ((vec_t)M_PI / 4.0f)));
  24. ASSERTm("Pi/180 not correct!", equalEpsilon(HEL_PI_OVER_180, ((vec_t)M_PI / 180.0f)));
  25. ASSERTm("180/Pi not correct!", equalEpsilon(HEL_180_OVER_PI, (180.0f / (vec_t)M_PI)));
  26. PASS();
  27. }
  28. TEST types() {
  29. ASSERT_EQ(sizeof(vec2_t), (2 * sizeof(vec_t)));
  30. ASSERT_EQ(sizeof(vec3_t), (3 * sizeof(vec_t)));
  31. ASSERT_EQ(sizeof(vec4_t), (4 * sizeof(vec_t)));
  32. ASSERT_EQ(sizeof(matrix_t), (16 * sizeof(vec_t)));
  33. PASS();
  34. }
  35. TEST conversionToRad(vec_t deg) {
  36. vec_t conv = HEL_DEG_TO_RAD(deg);
  37. vec_t hand = (deg * (vec_t)M_PI / 180.0f);
  38. if (!equalEpsilon(conv, hand)) {
  39. printf("Degree to Radian conversion failed: %f != %f\n", conv, hand);
  40. FAIL();
  41. } else {
  42. PASS();
  43. }
  44. }
  45. TEST conversionToDeg(vec_t rad) {
  46. vec_t conv = HEL_RAD_TO_DEG(rad);
  47. vec_t hand = (rad * 180.0f / (vec_t)M_PI);
  48. if (!equalEpsilon(conv, hand)) {
  49. printf("Radian to Degree conversion failed: %f != %f\n", conv, hand);
  50. FAIL();
  51. } else {
  52. PASS();
  53. }
  54. }
  55. #define TESTNUMS 100
  56. TEST random(vec_t bound[2]) {
  57. for (int i = 0; i < TESTNUMS; i++) {
  58. vec_t number = helRandomNum(bound[0], bound[1]);
  59. ASSERT(number >= bound[0]);
  60. ASSERT(number <= bound[1]);
  61. }
  62. PASS();
  63. }
  64. SUITE(mathSuite) {
  65. RUN_TEST(constants);
  66. RUN_TEST(types);
  67. for (unsigned int i = 0; i < (sizeof(testDegrees) / sizeof(testDegrees[0])); i++) {
  68. RUN_TESTp(conversionToRad, testDegrees[i]);
  69. }
  70. for (unsigned int i = 0; i < (sizeof(testRads) / sizeof(testRads[0])); i++) {
  71. RUN_TESTp(conversionToDeg, testRads[i]);
  72. }
  73. for (unsigned int i = 0; i < sizeof(testBounds) / sizeof(testBounds[0]); i++) {
  74. RUN_TESTp(random, testBounds[i]);
  75. }
  76. }
  77. GREATEST_MAIN_DEFS();
  78. int main(int argc, char *argv[]) {
  79. GREATEST_MAIN_BEGIN();
  80. RUN_SUITE(mathSuite);
  81. GREATEST_MAIN_END();
  82. }