25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

117 satır
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. class MatrixTest : public CppUnit::TestCase
  19. {
  20. CPPUNIT_TEST_SUITE(MatrixTest);
  21. CPPUNIT_TEST(test_mat_det);
  22. CPPUNIT_TEST(test_mat_mul);
  23. CPPUNIT_TEST(test_mat_inv);
  24. CPPUNIT_TEST_SUITE_END();
  25. public:
  26. MatrixTest() : CppUnit::TestCase("Matrix Test") {}
  27. void setUp()
  28. {
  29. identity = mat4(1.0f);
  30. triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
  31. vec4(7.0f, 2.0f, 0.0f, 0.0f),
  32. vec4(1.0f, 5.0f, 3.0f, 0.0f),
  33. vec4(8.0f, 9.0f, 2.0f, 4.0f));
  34. invertible = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f),
  35. vec4(-2.0f, -1.0f, -2.0f, 2.0f),
  36. vec4( 4.0f, 2.0f, 5.0f, -4.0f),
  37. vec4( 5.0f, -3.0f, -7.0f, -6.0f));
  38. }
  39. void tearDown() {}
  40. void test_mat_det()
  41. {
  42. float d1 = triangular.det();
  43. CPPUNIT_ASSERT(d1 == 24.0f);
  44. float d2 = invertible.det();
  45. CPPUNIT_ASSERT(d2 == -1.0f);
  46. }
  47. void test_mat_mul()
  48. {
  49. mat4 m0 = identity;
  50. mat4 m1 = identity;
  51. mat4 m2 = m0 * m1;
  52. CPPUNIT_ASSERT(m2[0][0] == 1.0f);
  53. CPPUNIT_ASSERT(m2[1][0] == 0.0f);
  54. CPPUNIT_ASSERT(m2[2][0] == 0.0f);
  55. CPPUNIT_ASSERT(m2[3][0] == 0.0f);
  56. CPPUNIT_ASSERT(m2[0][1] == 0.0f);
  57. CPPUNIT_ASSERT(m2[1][1] == 1.0f);
  58. CPPUNIT_ASSERT(m2[2][1] == 0.0f);
  59. CPPUNIT_ASSERT(m2[3][1] == 0.0f);
  60. CPPUNIT_ASSERT(m2[0][2] == 0.0f);
  61. CPPUNIT_ASSERT(m2[1][2] == 0.0f);
  62. CPPUNIT_ASSERT(m2[2][2] == 1.0f);
  63. CPPUNIT_ASSERT(m2[3][2] == 0.0f);
  64. CPPUNIT_ASSERT(m2[0][3] == 0.0f);
  65. CPPUNIT_ASSERT(m2[1][3] == 0.0f);
  66. CPPUNIT_ASSERT(m2[2][3] == 0.0f);
  67. CPPUNIT_ASSERT(m2[3][3] == 1.0f);
  68. }
  69. void test_mat_inv()
  70. {
  71. mat4 m0 = invertible;
  72. mat4 m1 = m0.invert();
  73. mat4 m2 = m0 * m1;
  74. CPPUNIT_ASSERT(m2[0][0] == 1.0f);
  75. CPPUNIT_ASSERT(m2[1][0] == 0.0f);
  76. CPPUNIT_ASSERT(m2[2][0] == 0.0f);
  77. CPPUNIT_ASSERT(m2[3][0] == 0.0f);
  78. CPPUNIT_ASSERT(m2[0][1] == 0.0f);
  79. CPPUNIT_ASSERT(m2[1][1] == 1.0f);
  80. CPPUNIT_ASSERT(m2[2][1] == 0.0f);
  81. CPPUNIT_ASSERT(m2[3][1] == 0.0f);
  82. CPPUNIT_ASSERT(m2[0][2] == 0.0f);
  83. CPPUNIT_ASSERT(m2[1][2] == 0.0f);
  84. CPPUNIT_ASSERT(m2[2][2] == 1.0f);
  85. CPPUNIT_ASSERT(m2[3][2] == 0.0f);
  86. CPPUNIT_ASSERT(m2[0][3] == 0.0f);
  87. CPPUNIT_ASSERT(m2[1][3] == 0.0f);
  88. CPPUNIT_ASSERT(m2[2][3] == 0.0f);
  89. CPPUNIT_ASSERT(m2[3][3] == 1.0f);
  90. }
  91. private:
  92. mat4 triangular, identity, invertible;
  93. };
  94. CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest);