Vector2Test.cpp 3.8 KB

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