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.
 
 
 

206 lines
5.4 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_vec_eq);
  24. CPPUNIT_TEST(test_vec_lt);
  25. CPPUNIT_TEST(test_vec_unary);
  26. CPPUNIT_TEST(test_mat_det);
  27. CPPUNIT_TEST(test_mat_mul);
  28. CPPUNIT_TEST(test_mat_inv);
  29. CPPUNIT_TEST_SUITE_END();
  30. public:
  31. MatrixTest() : CppUnit::TestCase("Matrix Test") {}
  32. void setUp()
  33. {
  34. identity = mat4(1.0f);
  35. triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
  36. vec4(7.0f, 2.0f, 0.0f, 0.0f),
  37. vec4(1.0f, 5.0f, 3.0f, 0.0f),
  38. vec4(8.0f, 9.0f, 2.0f, 4.0f));
  39. invertible = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f),
  40. vec4(-2.0f, -1.0f, -2.0f, 2.0f),
  41. vec4( 4.0f, 2.0f, 5.0f, -4.0f),
  42. vec4( 5.0f, -3.0f, -7.0f, -6.0f));
  43. }
  44. void tearDown() {}
  45. void test_vec_eq()
  46. {
  47. vec2 a2(1.0f, 2.0f);
  48. vec2 b2(0.0f, 2.0f);
  49. vec2 c2(1.0f, 0.0f);
  50. CPPUNIT_ASSERT(a2 == a2);
  51. CPPUNIT_ASSERT(!(a2 != a2));
  52. CPPUNIT_ASSERT(a2 != b2);
  53. CPPUNIT_ASSERT(!(a2 == b2));
  54. CPPUNIT_ASSERT(a2 != c2);
  55. CPPUNIT_ASSERT(!(a2 == c2));
  56. vec3 a3(1.0f, 2.0f, 3.0f);
  57. vec3 b3(0.0f, 2.0f, 3.0f);
  58. vec3 c3(1.0f, 0.0f, 3.0f);
  59. vec3 d3(1.0f, 2.0f, 0.0f);
  60. CPPUNIT_ASSERT(a3 == a3);
  61. CPPUNIT_ASSERT(!(a3 != a3));
  62. CPPUNIT_ASSERT(a3 != b3);
  63. CPPUNIT_ASSERT(!(a3 == b3));
  64. CPPUNIT_ASSERT(a3 != c3);
  65. CPPUNIT_ASSERT(!(a3 == c3));
  66. CPPUNIT_ASSERT(a3 != d3);
  67. CPPUNIT_ASSERT(!(a3 == d3));
  68. vec4 a4(1.0f, 2.0f, 3.0f, 4.0f);
  69. vec4 b4(0.0f, 2.0f, 3.0f, 4.0f);
  70. vec4 c4(1.0f, 0.0f, 3.0f, 4.0f);
  71. vec4 d4(1.0f, 2.0f, 0.0f, 4.0f);
  72. vec4 e4(1.0f, 2.0f, 3.0f, 0.0f);
  73. CPPUNIT_ASSERT(a4 == a4);
  74. CPPUNIT_ASSERT(!(a4 != a4));
  75. CPPUNIT_ASSERT(a4 != b4);
  76. CPPUNIT_ASSERT(!(a4 == b4));
  77. CPPUNIT_ASSERT(a4 != c4);
  78. CPPUNIT_ASSERT(!(a4 == c4));
  79. CPPUNIT_ASSERT(a4 != d4);
  80. CPPUNIT_ASSERT(!(a4 == d4));
  81. CPPUNIT_ASSERT(a4 != e4);
  82. CPPUNIT_ASSERT(!(a4 == e4));
  83. }
  84. void test_vec_lt()
  85. {
  86. vec2 a2(1.0f, 3.0f);
  87. vec2 b2(0.0f, 0.0f);
  88. vec2 c2(1.0f, 1.0f);
  89. vec2 d2(2.0f, 2.0f);
  90. vec2 e2(3.0f, 3.0f);
  91. vec2 f2(4.0f, 4.0f);
  92. CPPUNIT_ASSERT(a2 <= a2);
  93. CPPUNIT_ASSERT(!(a2 < a2));
  94. CPPUNIT_ASSERT(!(a2 <= b2));
  95. CPPUNIT_ASSERT(!(a2 < b2));
  96. CPPUNIT_ASSERT(!(a2 <= c2));
  97. CPPUNIT_ASSERT(!(a2 < c2));
  98. CPPUNIT_ASSERT(!(a2 <= d2));
  99. CPPUNIT_ASSERT(!(a2 < d2));
  100. CPPUNIT_ASSERT(a2 <= e2);
  101. CPPUNIT_ASSERT(!(a2 < e2));
  102. CPPUNIT_ASSERT(a2 <= f2);
  103. CPPUNIT_ASSERT(a2 < f2);
  104. }
  105. void test_vec_unary()
  106. {
  107. vec2 a(1.0f, 3.0f);
  108. vec2 b(-1.0f, -3.0f);
  109. CPPUNIT_ASSERT(a == -b);
  110. CPPUNIT_ASSERT(-a == b);
  111. }
  112. void test_mat_det()
  113. {
  114. float d1 = triangular.det();
  115. CPPUNIT_ASSERT(d1 == 24.0f);
  116. float d2 = invertible.det();
  117. CPPUNIT_ASSERT(d2 == -1.0f);
  118. }
  119. void test_mat_mul()
  120. {
  121. mat4 m0 = identity;
  122. mat4 m1 = identity;
  123. mat4 m2 = m0 * m1;
  124. CPPUNIT_ASSERT(m2[0][0] == 1.0f);
  125. CPPUNIT_ASSERT(m2[1][0] == 0.0f);
  126. CPPUNIT_ASSERT(m2[2][0] == 0.0f);
  127. CPPUNIT_ASSERT(m2[3][0] == 0.0f);
  128. CPPUNIT_ASSERT(m2[0][1] == 0.0f);
  129. CPPUNIT_ASSERT(m2[1][1] == 1.0f);
  130. CPPUNIT_ASSERT(m2[2][1] == 0.0f);
  131. CPPUNIT_ASSERT(m2[3][1] == 0.0f);
  132. CPPUNIT_ASSERT(m2[0][2] == 0.0f);
  133. CPPUNIT_ASSERT(m2[1][2] == 0.0f);
  134. CPPUNIT_ASSERT(m2[2][2] == 1.0f);
  135. CPPUNIT_ASSERT(m2[3][2] == 0.0f);
  136. CPPUNIT_ASSERT(m2[0][3] == 0.0f);
  137. CPPUNIT_ASSERT(m2[1][3] == 0.0f);
  138. CPPUNIT_ASSERT(m2[2][3] == 0.0f);
  139. CPPUNIT_ASSERT(m2[3][3] == 1.0f);
  140. }
  141. void test_mat_inv()
  142. {
  143. mat4 m0 = invertible;
  144. mat4 m1 = m0.invert();
  145. mat4 m2 = m0 * m1;
  146. CPPUNIT_ASSERT(m2[0][0] == 1.0f);
  147. CPPUNIT_ASSERT(m2[1][0] == 0.0f);
  148. CPPUNIT_ASSERT(m2[2][0] == 0.0f);
  149. CPPUNIT_ASSERT(m2[3][0] == 0.0f);
  150. CPPUNIT_ASSERT(m2[0][1] == 0.0f);
  151. CPPUNIT_ASSERT(m2[1][1] == 1.0f);
  152. CPPUNIT_ASSERT(m2[2][1] == 0.0f);
  153. CPPUNIT_ASSERT(m2[3][1] == 0.0f);
  154. CPPUNIT_ASSERT(m2[0][2] == 0.0f);
  155. CPPUNIT_ASSERT(m2[1][2] == 0.0f);
  156. CPPUNIT_ASSERT(m2[2][2] == 1.0f);
  157. CPPUNIT_ASSERT(m2[3][2] == 0.0f);
  158. CPPUNIT_ASSERT(m2[0][3] == 0.0f);
  159. CPPUNIT_ASSERT(m2[1][3] == 0.0f);
  160. CPPUNIT_ASSERT(m2[2][3] == 0.0f);
  161. CPPUNIT_ASSERT(m2[3][3] == 1.0f);
  162. }
  163. private:
  164. mat4 triangular, identity, invertible;
  165. };
  166. CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest);
  167. } /* namespace lol */