Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

157 rader
4.5 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 "core.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(VectorUnaryMinus)
  82. {
  83. vec2 a(1.0f, 3.0f);
  84. vec2 b(-1.0f, -3.0f);
  85. LOLUNIT_ASSERT_EQUAL(a, -b);
  86. LOLUNIT_ASSERT_EQUAL(-a, b);
  87. }
  88. LOLUNIT_TEST(CastVector)
  89. {
  90. vec2 a1(1.0f, 3.0f);
  91. vec3 b(a1, 0.0f);
  92. vec2 a2(b.xy);
  93. LOLUNIT_ASSERT_EQUAL(b.x, a1.x);
  94. LOLUNIT_ASSERT_EQUAL(b.y, a1.y);
  95. LOLUNIT_ASSERT_EQUAL(b.z, 0.0f);
  96. LOLUNIT_ASSERT_EQUAL(a2, a1);
  97. vec4 c(a1, 0.0f, 0.0f);
  98. vec2 a3(c.xy);
  99. LOLUNIT_ASSERT_EQUAL(c.x, a1.x);
  100. LOLUNIT_ASSERT_EQUAL(c.y, a1.y);
  101. LOLUNIT_ASSERT_EQUAL(c.z, 0.0f);
  102. LOLUNIT_ASSERT_EQUAL(c.w, 0.0f);
  103. LOLUNIT_ASSERT_EQUAL(a3, a1);
  104. }
  105. LOLUNIT_TEST(Orthogonal)
  106. {
  107. vec3 a(1.f, 0.f, 0.f);
  108. vec3 b(0.f, 1.f, 0.f);
  109. vec3 c(0.f, 0.f, 1.f);
  110. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(a), a), 0.f, 1e-6f);
  111. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(b), b), 0.f, 1e-6f);
  112. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthogonal(c), c), 0.f, 1e-6f);
  113. /* The length of the orthogonal vector should be at least
  114. * sqrt(2)/2 times the length of the original vector. */
  115. LOLUNIT_ASSERT_GREATER(length(orthogonal(a)), 0.7f);
  116. LOLUNIT_ASSERT_GREATER(length(orthogonal(b)), 0.7f);
  117. LOLUNIT_ASSERT_GREATER(length(orthogonal(c)), 0.7f);
  118. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(a), a), 0.f, 1e-6f);
  119. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(b), b), 0.f, 1e-6f);
  120. LOLUNIT_ASSERT_DOUBLES_EQUAL(dot(orthonormal(c), c), 0.f, 1e-6f);
  121. /* The length of the orthonormal vector should be 1. */
  122. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(a)), 1.f, 1e-6f);
  123. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(b)), 1.f, 1e-6f);
  124. LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(c)), 1.f, 1e-6f);
  125. }
  126. };
  127. } /* namespace lol */