Vector2Test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-08-05
  6. * Changed: 2023-06-02
  7. *
  8. * */
  9. #include <cmath>
  10. #include <gtest/gtest.h>
  11. #include <ls_math/ls_math_vector.hpp>
  12. using ls::math::core::type::vector2_components;
  13. using ls::math::core::type::vector_scalar;
  14. using ls::math::vector::Vector2;
  15. using testing::Test;
  16. namespace
  17. {
  18. class Vector2Test : public Test
  19. {
  20. protected:
  21. Vector2Test() = default;
  22. ~Vector2Test() override = default;
  23. void SetUp() override
  24. {}
  25. void TearDown() override
  26. {}
  27. };
  28. TEST_F(Vector2Test, operator_addition)
  29. {
  30. Vector2 c = Vector2(3.0f, 4.0f) + Vector2(5.0f, 3.0f);
  31. ASSERT_FLOAT_EQ(8.0f, c.getX());
  32. ASSERT_FLOAT_EQ(7.0f, c.getY());
  33. }
  34. TEST_F(Vector2Test, operator_substraction)
  35. {
  36. Vector2 c = Vector2(3.0f, 4.0f) - Vector2(5.0f, 3.0f);
  37. ASSERT_FLOAT_EQ(-2.0f, c.getX());
  38. ASSERT_FLOAT_EQ(1.0f, c.getY());
  39. }
  40. TEST_F(Vector2Test, operator_scalar)
  41. {
  42. vector_scalar scalar = Vector2(3.0f, 4.0f) * Vector2(5.0f, 3.0f);
  43. ASSERT_FLOAT_EQ(27.0f, scalar);
  44. }
  45. TEST_F(Vector2Test, operator_multiplicator)
  46. {
  47. Vector2 c = Vector2(3.0f, 4.0f) * 1.5f;
  48. ASSERT_FLOAT_EQ(4.5f, c.getX());
  49. ASSERT_FLOAT_EQ(6.0f, c.getY());
  50. }
  51. TEST_F(Vector2Test, operator_divisor)
  52. {
  53. Vector2 c = Vector2(150.0f, -25.0f);
  54. Vector2 n = c / 152.4f;
  55. // round by two decimals
  56. double powValue = pow(10.0f, (double) 2);
  57. double roundedX = round(n.getX() * powValue) / powValue;
  58. double roundedY = round(n.getY() * powValue) / powValue;
  59. // evaluate result
  60. ASSERT_FLOAT_EQ(0.98f, roundedX);
  61. ASSERT_FLOAT_EQ(-0.16f, roundedY);
  62. }
  63. TEST_F(Vector2Test, operator_equals)
  64. {
  65. ASSERT_TRUE(Vector2(3.0f, 4.0f) == Vector2(3.0f, 4.0f));
  66. }
  67. TEST_F(Vector2Test, operator_equals_not_equals)
  68. {
  69. ASSERT_FALSE(Vector2(3.0f, 8.0f) == Vector2(3.0f, 4.0f));
  70. }
  71. TEST_F(Vector2Test, operator_not_equals)
  72. {
  73. ASSERT_TRUE(Vector2(3.0f, 5.0f) != Vector2(3.0f, 4.0f));
  74. }
  75. TEST_F(Vector2Test, operator_not_equals_equals)
  76. {
  77. ASSERT_FALSE(Vector2(3.0f, 4.0f) != Vector2(3.0f, 4.0f));
  78. }
  79. TEST_F(Vector2Test, add)
  80. {
  81. Vector2 c = Vector2::add(Vector2(3.0f, 4.0f), Vector2(5.0f, 3.0f));
  82. ASSERT_FLOAT_EQ(8.0f, c.getX());
  83. ASSERT_FLOAT_EQ(7.0f, c.getY());
  84. }
  85. TEST_F(Vector2Test, dot)
  86. {
  87. vector_scalar scalar = Vector2::dot(Vector2(3.0f, 4.0f), Vector2(5.0f, 3.0f));
  88. ASSERT_FLOAT_EQ(27.0f, scalar);
  89. }
  90. TEST_F(Vector2Test, getComponents)
  91. {
  92. Vector2 a{6.0f, 8.0f};
  93. vector2_components components = a.getComponents();
  94. ASSERT_FLOAT_EQ(6.0f, components[0]);
  95. ASSERT_FLOAT_EQ(8.0f, components[1]);
  96. }
  97. TEST_F(Vector2Test, getLength)
  98. {
  99. Vector2 a{6.0f, 8.0f};
  100. ASSERT_FLOAT_EQ(10.0f, a.getLength());
  101. }
  102. TEST_F(Vector2Test, getX)
  103. {
  104. Vector2 a{3.0f, 4.0f};
  105. ASSERT_FLOAT_EQ(3.0f, a.getX());
  106. }
  107. TEST_F(Vector2Test, getY)
  108. {
  109. Vector2 a{3.0f, 4.0f};
  110. ASSERT_FLOAT_EQ(4.0f, a.getY());
  111. }
  112. TEST_F(Vector2Test, normalize)
  113. {
  114. Vector2 a{150.0f, -25.0f};
  115. Vector2 n = a.normalize();
  116. // round by two decimals
  117. double roundedX = round(n.getX() * 1000.0f) / 1000.0f;
  118. double roundedY = round(n.getY() * 1000.0f) / 1000.0f;
  119. // evaluate results
  120. ASSERT_FLOAT_EQ(0.986f, roundedX);
  121. ASSERT_FLOAT_EQ(-0.164f, roundedY);
  122. ASSERT_FLOAT_EQ(1.0f, n.getLength());
  123. }
  124. TEST_F(Vector2Test, setX)
  125. {
  126. Vector2 a{3.0f, 4.0f};
  127. a.setX(14.0f);
  128. ASSERT_FLOAT_EQ(14.0f, a.getX());
  129. }
  130. TEST_F(Vector2Test, setY)
  131. {
  132. Vector2 a{3.0f, 4.0f};
  133. a.setY(8.0f);
  134. ASSERT_FLOAT_EQ(8.0f, a.getY());
  135. }
  136. TEST_F(Vector2Test, subtract)
  137. {
  138. Vector2 c = Vector2::subtract(Vector2(3.0f, 4.0f), Vector2(5.0f, 3.0f));
  139. ASSERT_FLOAT_EQ(-2.0f, c.getX());
  140. ASSERT_FLOAT_EQ(1.0f, c.getY());
  141. }
  142. }