Vector2Test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. public:
  21. Vector2Test() = default;
  22. ~Vector2Test() override = default;
  23. };
  24. TEST_F(Vector2Test, operator_addition)
  25. {
  26. Vector2 c = Vector2(3.0f, 4.0f) + Vector2(5.0f, 3.0f);
  27. ASSERT_FLOAT_EQ(8.0f, c.getX());
  28. ASSERT_FLOAT_EQ(7.0f, c.getY());
  29. }
  30. TEST_F(Vector2Test, operator_substraction)
  31. {
  32. Vector2 c = Vector2(3.0f, 4.0f) - Vector2(5.0f, 3.0f);
  33. ASSERT_FLOAT_EQ(-2.0f, c.getX());
  34. ASSERT_FLOAT_EQ(1.0f, c.getY());
  35. }
  36. TEST_F(Vector2Test, operator_scalar)
  37. {
  38. vector_scalar scalar = Vector2(3.0f, 4.0f) * Vector2(5.0f, 3.0f);
  39. ASSERT_FLOAT_EQ(27.0f, scalar);
  40. }
  41. TEST_F(Vector2Test, operator_multiplicator)
  42. {
  43. Vector2 c = Vector2(3.0f, 4.0f) * 1.5f;
  44. ASSERT_FLOAT_EQ(4.5f, c.getX());
  45. ASSERT_FLOAT_EQ(6.0f, c.getY());
  46. }
  47. TEST_F(Vector2Test, operator_divisor)
  48. {
  49. auto c = Vector2(150.0f, -25.0f);
  50. Vector2 n = c / 152.4f;
  51. // round by two decimals
  52. double powValue = pow(10.0f, (double) 2);
  53. double roundedX = round(n.getX() * powValue) / powValue;
  54. double roundedY = round(n.getY() * powValue) / powValue;
  55. // evaluate result
  56. ASSERT_FLOAT_EQ(0.98f, roundedX);
  57. ASSERT_FLOAT_EQ(-0.16f, roundedY);
  58. }
  59. TEST_F(Vector2Test, operator_equals)
  60. {
  61. ASSERT_TRUE(Vector2(3.0f, 4.0f) == Vector2(3.0f, 4.0f));
  62. }
  63. TEST_F(Vector2Test, operator_equals_not_equals)
  64. {
  65. ASSERT_FALSE(Vector2(3.0f, 8.0f) == Vector2(3.0f, 4.0f));
  66. }
  67. TEST_F(Vector2Test, operator_not_equals)
  68. {
  69. ASSERT_TRUE(Vector2(3.0f, 5.0f) != Vector2(3.0f, 4.0f));
  70. }
  71. TEST_F(Vector2Test, operator_not_equals_equals)
  72. {
  73. ASSERT_FALSE(Vector2(3.0f, 4.0f) != Vector2(3.0f, 4.0f));
  74. }
  75. TEST_F(Vector2Test, add)
  76. {
  77. Vector2 c = Vector2::add(Vector2(3.0f, 4.0f), Vector2(5.0f, 3.0f));
  78. ASSERT_FLOAT_EQ(8.0f, c.getX());
  79. ASSERT_FLOAT_EQ(7.0f, c.getY());
  80. }
  81. TEST_F(Vector2Test, dot)
  82. {
  83. vector_scalar scalar = Vector2::dot(Vector2(3.0f, 4.0f), Vector2(5.0f, 3.0f));
  84. ASSERT_FLOAT_EQ(27.0f, scalar);
  85. }
  86. TEST_F(Vector2Test, getComponents)
  87. {
  88. Vector2 a{6.0f, 8.0f};
  89. vector2_components components = a.getComponents();
  90. ASSERT_FLOAT_EQ(6.0f, components[0]);
  91. ASSERT_FLOAT_EQ(8.0f, components[1]);
  92. }
  93. TEST_F(Vector2Test, getLength)
  94. {
  95. Vector2 a{6.0f, 8.0f};
  96. ASSERT_FLOAT_EQ(10.0f, a.getLength());
  97. }
  98. TEST_F(Vector2Test, getX)
  99. {
  100. Vector2 a{3.0f, 4.0f};
  101. ASSERT_FLOAT_EQ(3.0f, a.getX());
  102. }
  103. TEST_F(Vector2Test, getY)
  104. {
  105. Vector2 a{3.0f, 4.0f};
  106. ASSERT_FLOAT_EQ(4.0f, a.getY());
  107. }
  108. TEST_F(Vector2Test, normalize)
  109. {
  110. Vector2 a{150.0f, -25.0f};
  111. Vector2 n = a.normalize();
  112. // round by two decimals
  113. double roundedX = round(n.getX() * 1000.0f) / 1000.0f;
  114. double roundedY = round(n.getY() * 1000.0f) / 1000.0f;
  115. // evaluate results
  116. ASSERT_FLOAT_EQ(0.986f, roundedX);
  117. ASSERT_FLOAT_EQ(-0.164f, roundedY);
  118. ASSERT_FLOAT_EQ(1.0f, n.getLength());
  119. }
  120. TEST_F(Vector2Test, setX)
  121. {
  122. Vector2 a{3.0f, 4.0f};
  123. a.setX(14.0f);
  124. ASSERT_FLOAT_EQ(14.0f, a.getX());
  125. }
  126. TEST_F(Vector2Test, setY)
  127. {
  128. Vector2 a{3.0f, 4.0f};
  129. a.setY(8.0f);
  130. ASSERT_FLOAT_EQ(8.0f, a.getY());
  131. }
  132. TEST_F(Vector2Test, subtract)
  133. {
  134. Vector2 c = Vector2::subtract(Vector2(3.0f, 4.0f), Vector2(5.0f, 3.0f));
  135. ASSERT_FLOAT_EQ(-2.0f, c.getX());
  136. ASSERT_FLOAT_EQ(1.0f, c.getY());
  137. }
  138. }