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.

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