您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

122 行
3.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cppunit/extensions/HelperMacros.h>
  14. #include <cppunit/TestCaller.h>
  15. #include <cppunit/TestCase.h>
  16. #include <cppunit/TestSuite.h>
  17. #include "core.h"
  18. namespace lol
  19. {
  20. class MatrixTest : public CppUnit::TestCase
  21. {
  22. CPPUNIT_TEST_SUITE(MatrixTest);
  23. CPPUNIT_TEST(test_mat_det);
  24. CPPUNIT_TEST(test_mat_mul);
  25. CPPUNIT_TEST(test_mat_inv);
  26. CPPUNIT_TEST_SUITE_END();
  27. public:
  28. MatrixTest() : CppUnit::TestCase("Matrix Test") {}
  29. void setUp()
  30. {
  31. identity = mat4(1.0f);
  32. triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
  33. vec4(7.0f, 2.0f, 0.0f, 0.0f),
  34. vec4(1.0f, 5.0f, 3.0f, 0.0f),
  35. vec4(8.0f, 9.0f, 2.0f, 4.0f));
  36. invertible = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f),
  37. vec4(-2.0f, -1.0f, -2.0f, 2.0f),
  38. vec4( 4.0f, 2.0f, 5.0f, -4.0f),
  39. vec4( 5.0f, -3.0f, -7.0f, -6.0f));
  40. }
  41. void tearDown() {}
  42. void test_mat_det()
  43. {
  44. float d1 = triangular.det();
  45. CPPUNIT_ASSERT(d1 == 24.0f);
  46. float d2 = invertible.det();
  47. CPPUNIT_ASSERT(d2 == -1.0f);
  48. }
  49. void test_mat_mul()
  50. {
  51. mat4 m0 = identity;
  52. mat4 m1 = identity;
  53. mat4 m2 = m0 * m1;
  54. CPPUNIT_ASSERT(m2[0][0] == 1.0f);
  55. CPPUNIT_ASSERT(m2[1][0] == 0.0f);
  56. CPPUNIT_ASSERT(m2[2][0] == 0.0f);
  57. CPPUNIT_ASSERT(m2[3][0] == 0.0f);
  58. CPPUNIT_ASSERT(m2[0][1] == 0.0f);
  59. CPPUNIT_ASSERT(m2[1][1] == 1.0f);
  60. CPPUNIT_ASSERT(m2[2][1] == 0.0f);
  61. CPPUNIT_ASSERT(m2[3][1] == 0.0f);
  62. CPPUNIT_ASSERT(m2[0][2] == 0.0f);
  63. CPPUNIT_ASSERT(m2[1][2] == 0.0f);
  64. CPPUNIT_ASSERT(m2[2][2] == 1.0f);
  65. CPPUNIT_ASSERT(m2[3][2] == 0.0f);
  66. CPPUNIT_ASSERT(m2[0][3] == 0.0f);
  67. CPPUNIT_ASSERT(m2[1][3] == 0.0f);
  68. CPPUNIT_ASSERT(m2[2][3] == 0.0f);
  69. CPPUNIT_ASSERT(m2[3][3] == 1.0f);
  70. }
  71. void test_mat_inv()
  72. {
  73. mat4 m0 = invertible;
  74. mat4 m1 = m0.invert();
  75. mat4 m2 = m0 * m1;
  76. CPPUNIT_ASSERT(m2[0][0] == 1.0f);
  77. CPPUNIT_ASSERT(m2[1][0] == 0.0f);
  78. CPPUNIT_ASSERT(m2[2][0] == 0.0f);
  79. CPPUNIT_ASSERT(m2[3][0] == 0.0f);
  80. CPPUNIT_ASSERT(m2[0][1] == 0.0f);
  81. CPPUNIT_ASSERT(m2[1][1] == 1.0f);
  82. CPPUNIT_ASSERT(m2[2][1] == 0.0f);
  83. CPPUNIT_ASSERT(m2[3][1] == 0.0f);
  84. CPPUNIT_ASSERT(m2[0][2] == 0.0f);
  85. CPPUNIT_ASSERT(m2[1][2] == 0.0f);
  86. CPPUNIT_ASSERT(m2[2][2] == 1.0f);
  87. CPPUNIT_ASSERT(m2[3][2] == 0.0f);
  88. CPPUNIT_ASSERT(m2[0][3] == 0.0f);
  89. CPPUNIT_ASSERT(m2[1][3] == 0.0f);
  90. CPPUNIT_ASSERT(m2[2][3] == 0.0f);
  91. CPPUNIT_ASSERT(m2[3][3] == 1.0f);
  92. }
  93. private:
  94. mat4 triangular, identity, invertible;
  95. };
  96. CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest);
  97. } /* namespace lol */