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.

MatMath.cpp 2.5KB

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