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.
 
 
 

315 lines
8.6 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/main.h>
  14. #include "lol/unit.h"
  15. namespace lol
  16. {
  17. LOLUNIT_FIXTURE(VectorTest)
  18. {
  19. void SetUp() {}
  20. void TearDown() {}
  21. LOLUNIT_TEST(VectorEquality)
  22. {
  23. vec2 a2(1.0f, 2.0f);
  24. vec2 b2(0.0f, 2.0f);
  25. vec2 c2(1.0f, 0.0f);
  26. LOLUNIT_ASSERT_EQUAL(a2, a2);
  27. LOLUNIT_ASSERT_NOT_DIFFERENT(a2, a2);
  28. LOLUNIT_ASSERT_DIFFERENT(a2, b2);
  29. LOLUNIT_ASSERT_NOT_EQUAL(a2, b2);
  30. LOLUNIT_ASSERT_DIFFERENT(a2, c2);
  31. LOLUNIT_ASSERT_NOT_EQUAL(a2, c2);
  32. vec3 a3(1.0f, 2.0f, 3.0f);
  33. vec3 b3(0.0f, 2.0f, 3.0f);
  34. vec3 c3(1.0f, 0.0f, 3.0f);
  35. vec3 d3(1.0f, 2.0f, 0.0f);
  36. LOLUNIT_ASSERT_EQUAL(a3, a3);
  37. LOLUNIT_ASSERT_NOT_DIFFERENT(a3, a3);
  38. LOLUNIT_ASSERT_DIFFERENT(a3, b3);
  39. LOLUNIT_ASSERT_NOT_EQUAL(a3, b3);
  40. LOLUNIT_ASSERT_DIFFERENT(a3, c3);
  41. LOLUNIT_ASSERT_NOT_EQUAL(a3, c3);
  42. LOLUNIT_ASSERT_DIFFERENT(a3, d3);
  43. LOLUNIT_ASSERT_NOT_EQUAL(a3, d3);
  44. vec4 a4(1.0f, 2.0f, 3.0f, 4.0f);
  45. vec4 b4(0.0f, 2.0f, 3.0f, 4.0f);
  46. vec4 c4(1.0f, 0.0f, 3.0f, 4.0f);
  47. vec4 d4(1.0f, 2.0f, 0.0f, 4.0f);
  48. vec4 e4(1.0f, 2.0f, 3.0f, 0.0f);
  49. LOLUNIT_ASSERT_EQUAL(a4, a4);
  50. LOLUNIT_ASSERT_NOT_DIFFERENT(a4, a4);
  51. LOLUNIT_ASSERT_DIFFERENT(a4, b4);
  52. LOLUNIT_ASSERT_NOT_EQUAL(a4, b4);
  53. LOLUNIT_ASSERT_DIFFERENT(a4, c4);
  54. LOLUNIT_ASSERT_NOT_EQUAL(a4, c4);
  55. LOLUNIT_ASSERT_DIFFERENT(a4, d4);
  56. LOLUNIT_ASSERT_NOT_EQUAL(a4, d4);
  57. LOLUNIT_ASSERT_DIFFERENT(a4, e4);
  58. LOLUNIT_ASSERT_NOT_EQUAL(a4, e4);
  59. }
  60. LOLUNIT_TEST(VectorInequality)
  61. {
  62. vec2 a2(1.0f, 3.0f);
  63. vec2 b2(0.0f, 0.0f);
  64. vec2 c2(1.0f, 1.0f);
  65. vec2 d2(2.0f, 2.0f);
  66. vec2 e2(3.0f, 3.0f);
  67. vec2 f2(4.0f, 4.0f);
  68. LOLUNIT_ASSERT_LEQUAL(a2, a2);
  69. LOLUNIT_ASSERT_NOT_LESS(a2, a2);
  70. LOLUNIT_ASSERT_NOT_LEQUAL(a2, b2);
  71. LOLUNIT_ASSERT_NOT_LESS(a2, b2);
  72. LOLUNIT_ASSERT_NOT_LEQUAL(a2, c2);
  73. LOLUNIT_ASSERT_NOT_LESS(a2, c2);
  74. LOLUNIT_ASSERT_NOT_LEQUAL(a2, d2);
  75. LOLUNIT_ASSERT_NOT_LESS(a2, d2);
  76. LOLUNIT_ASSERT_LEQUAL(a2, e2);
  77. LOLUNIT_ASSERT_NOT_LESS(a2, e2);
  78. LOLUNIT_ASSERT_LEQUAL(a2, f2);
  79. LOLUNIT_ASSERT_LESS(a2, f2);
  80. }
  81. LOLUNIT_TEST(VectorInit)
  82. {
  83. vec2 a { 1.f, 2.f };
  84. LOLUNIT_ASSERT_EQUAL(1.f, a.x);
  85. LOLUNIT_ASSERT_EQUAL(2.f, a.y);
  86. vec3 b { 1.f, 2.f, 3.f };
  87. LOLUNIT_ASSERT_EQUAL(1.f, b.x);
  88. LOLUNIT_ASSERT_EQUAL(2.f, b.y);
  89. LOLUNIT_ASSERT_EQUAL(3.f, b.z);
  90. vec4 c { 1.f, 2.f, 3.f, 4.f };
  91. LOLUNIT_ASSERT_EQUAL(1.f, c.x);
  92. LOLUNIT_ASSERT_EQUAL(2.f, c.y);
  93. LOLUNIT_ASSERT_EQUAL(3.f, c.z);
  94. LOLUNIT_ASSERT_EQUAL(4.f, c.w);
  95. vec_t<float, 10> d { 1.f, 2.f, 3.f, 4.f, 5.f };
  96. LOLUNIT_ASSERT_EQUAL(1.f, d[0]);
  97. LOLUNIT_ASSERT_EQUAL(2.f, d[1]);
  98. LOLUNIT_ASSERT_EQUAL(3.f, d[2]);
  99. LOLUNIT_ASSERT_EQUAL(4.f, d[3]);
  100. LOLUNIT_ASSERT_EQUAL(5.f, d[4]);
  101. LOLUNIT_ASSERT_EQUAL(0.f, d[5]);
  102. LOLUNIT_ASSERT_EQUAL(0.f, d[6]);
  103. LOLUNIT_ASSERT_EQUAL(0.f, d[7]);
  104. LOLUNIT_ASSERT_EQUAL(0.f, d[8]);
  105. LOLUNIT_ASSERT_EQUAL(0.f, d[9]);
  106. }
  107. LOLUNIT_TEST(VectorSwizzle)
  108. {
  109. vec3 a(1.0f, 2.0f, 3.0f);
  110. vec3 b(4.0f, 5.0f, 6.0f);
  111. vec3 c;
  112. c = a;
  113. c.x = b.y;
  114. LOLUNIT_ASSERT_EQUAL(c.x, 5.0f);
  115. LOLUNIT_ASSERT_EQUAL(c.y, 2.0f);
  116. LOLUNIT_ASSERT_EQUAL(c.z, 3.0f);
  117. c = a.zyx;
  118. LOLUNIT_ASSERT_EQUAL(c.x, 3.0f);
  119. LOLUNIT_ASSERT_EQUAL(c.y, 2.0f);
  120. LOLUNIT_ASSERT_EQUAL(c.z, 1.0f);
  121. #if 0 /* Visual Studio doesn't support these yet. */
  122. c = a;
  123. c.xy = b.yz;
  124. LOLUNIT_ASSERT_EQUAL(c.x, 5.0f);
  125. LOLUNIT_ASSERT_EQUAL(c.y, 6.0f);
  126. LOLUNIT_ASSERT_EQUAL(c.z, 3.0f);
  127. c = a;
  128. c.xy = b.zz;
  129. LOLUNIT_ASSERT_EQUAL(c.x, 6.0f);
  130. LOLUNIT_ASSERT_EQUAL(c.y, 6.0f);
  131. LOLUNIT_ASSERT_EQUAL(c.z, 3.0f);
  132. c = a;
  133. c.xz = b.xy;
  134. LOLUNIT_ASSERT_EQUAL(c.x, 4.0f);
  135. LOLUNIT_ASSERT_EQUAL(c.y, 2.0f);
  136. LOLUNIT_ASSERT_EQUAL(c.z, 5.0f);
  137. c = a;
  138. c.xz = b.xz;
  139. LOLUNIT_ASSERT_EQUAL(c.x, 4.0f);
  140. LOLUNIT_ASSERT_EQUAL(c.y, 2.0f);
  141. LOLUNIT_ASSERT_EQUAL(c.z, 6.0f);
  142. c = a;
  143. c.xz = c.zy = b.yx;
  144. LOLUNIT_ASSERT_EQUAL(c.x, 5.0f);
  145. LOLUNIT_ASSERT_EQUAL(c.y, 4.0f);
  146. LOLUNIT_ASSERT_EQUAL(c.z, 4.0f);
  147. #endif
  148. }
  149. LOLUNIT_TEST(VectorSwizzleMul)
  150. {
  151. ivec3 a(1, 2, 3);
  152. ivec3 b = a * 2;
  153. LOLUNIT_ASSERT_EQUAL(b.x, 2);
  154. LOLUNIT_ASSERT_EQUAL(b.y, 4);
  155. LOLUNIT_ASSERT_EQUAL(b.z, 6);
  156. ivec3 c = (ivec3)a.zyx * 2;
  157. LOLUNIT_ASSERT_EQUAL(c.x, 6);
  158. LOLUNIT_ASSERT_EQUAL(c.y, 4);
  159. LOLUNIT_ASSERT_EQUAL(c.z, 2);
  160. ivec3 d = 2 * (ivec3)a.zyx;
  161. LOLUNIT_ASSERT_EQUAL(d.x, 6);
  162. LOLUNIT_ASSERT_EQUAL(d.y, 4);
  163. LOLUNIT_ASSERT_EQUAL(d.z, 2);
  164. ivec3 e = a.zyx * 2;
  165. LOLUNIT_ASSERT_EQUAL(e.x, 6);
  166. LOLUNIT_ASSERT_EQUAL(e.y, 4);
  167. LOLUNIT_ASSERT_EQUAL(e.z, 2);
  168. ivec3 f = 2 * a.zyx;
  169. LOLUNIT_ASSERT_EQUAL(f.x, 6);
  170. LOLUNIT_ASSERT_EQUAL(f.y, 4);
  171. LOLUNIT_ASSERT_EQUAL(f.z, 2);
  172. }
  173. LOLUNIT_TEST(VectorUnaryMinus)
  174. {
  175. vec2 a(1.0f, 3.0f);
  176. vec2 b(-1.0f, -3.0f);
  177. LOLUNIT_ASSERT_EQUAL(a, -b);
  178. LOLUNIT_ASSERT_EQUAL(-a, b);
  179. }
  180. LOLUNIT_TEST(CastVector)
  181. {
  182. vec2 a1(1.0f, 3.0f);
  183. vec3 b(a1, 0.0f);
  184. vec2 a2(b.xy);
  185. LOLUNIT_ASSERT_EQUAL(b.x, a1.x);
  186. LOLUNIT_ASSERT_EQUAL(b.y, a1.y);
  187. LOLUNIT_ASSERT_EQUAL(b.z, 0.0f);
  188. LOLUNIT_ASSERT_EQUAL(a2, a1);
  189. vec4 c(a1, 0.0f, 0.0f);
  190. vec2 a3(c.xy);
  191. LOLUNIT_ASSERT_EQUAL(c.x, a1.x);
  192. LOLUNIT_ASSERT_EQUAL(c.y, a1.y);
  193. LOLUNIT_ASSERT_EQUAL(c.z, 0.0f);
  194. LOLUNIT_ASSERT_EQUAL(c.w, 0.0f);
  195. LOLUNIT_ASSERT_EQUAL(a3, a1);
  196. }
  197. LOLUNIT_TEST(Orthogonal)
  198. {
  199. vec3 a(1.f, 0.f, 0.f);
  200. vec3 b(0.f, 1.f, 0.f);
  201. vec3 c(0.f, 0.f, 1.f);
  202. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(a), a), 0.f, 1e-6f);
  203. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(b), b), 0.f, 1e-6f);
  204. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(c), c), 0.f, 1e-6f);
  205. /* The length of the orthogonal vector should be at least
  206. * sqrt(2)/2 times the length of the original vector. */
  207. LOLUNIT_ASSERT_GREATER(length(orthogonal(a)), 0.7f);
  208. LOLUNIT_ASSERT_GREATER(length(orthogonal(b)), 0.7f);
  209. LOLUNIT_ASSERT_GREATER(length(orthogonal(c)), 0.7f);
  210. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(a), a), 0.f, 1e-6f);
  211. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(b), b), 0.f, 1e-6f);
  212. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(c), c), 0.f, 1e-6f);
  213. /* The length of the orthonormal vector should be 1. */
  214. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(a)), 1.f, 1e-6f);
  215. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(b)), 1.f, 1e-6f);
  216. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(c)), 1.f, 1e-6f);
  217. }
  218. LOLUNIT_TEST(LargeVectors)
  219. {
  220. vec_t<int, 50> v0(0);
  221. vec_t<int, 50> v1(1);
  222. vec_t<int, 50> v2(2);
  223. vec_t<int, 50> v3(3);
  224. auto va = v0 + v3;
  225. auto vb = v1 + v2;
  226. LOLUNIT_ASSERT(va == vb);
  227. }
  228. LOLUNIT_TEST(VectorIterator)
  229. {
  230. vec4 v4(1.125f, 1.25f, 1.375f, 1.25f);
  231. for (auto x : v4)
  232. {
  233. LOLUNIT_ASSERT_GREATER(x, 1.0f);
  234. LOLUNIT_ASSERT_LESS(x, 1.5f);
  235. }
  236. for (auto &x : v4)
  237. {
  238. LOLUNIT_ASSERT_GREATER(x, 1.0f);
  239. LOLUNIT_ASSERT_LESS(x, 1.5f);
  240. }
  241. for (auto x : v4.zywx)
  242. {
  243. LOLUNIT_ASSERT_GREATER(x, 1.0f);
  244. LOLUNIT_ASSERT_LESS(x, 1.5f);
  245. }
  246. vec4 const &v4c = v4;
  247. for (auto x : v4c)
  248. {
  249. LOLUNIT_ASSERT_GREATER(x, 1.0f);
  250. LOLUNIT_ASSERT_LESS(x, 1.5f);
  251. }
  252. for (auto &x : v4c)
  253. {
  254. LOLUNIT_ASSERT_GREATER(x, 1.0f);
  255. LOLUNIT_ASSERT_LESS(x, 1.5f);
  256. }
  257. }
  258. };
  259. } /* namespace lol */