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.
 
 
 

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