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

207 行
5.9 KiB

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