Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MatMath.cpp 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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("2Pi not correct!", HEL_2_PI, (M_PI * 2.0));
  23. ASSERT_EQm("Pi/4 not correct!", HEL_PI_OVER_4, (M_PI / 4.0));
  24. ASSERT_EQm("Pi/180 not correct!", HEL_PI_OVER_180, (M_PI / 180.0));
  25. ASSERT_EQm("180/Pi not correct!", HEL_180_OVER_PI, (180.0 / 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 * M_PI / 180.0);
  38. if (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.0 / M_PI);
  48. if (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. }