Vector2Test.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, 4) + Vector2(5, 3);
  29. ASSERT_FLOAT_EQ(8, c.getX());
  30. ASSERT_FLOAT_EQ(7, c.getY());
  31. }
  32. TEST_F(Vector2Test, operator_substraction)
  33. {
  34. Vector2 c = Vector2(3, 4) - Vector2(5, 3);
  35. ASSERT_FLOAT_EQ(-2, c.getX());
  36. ASSERT_FLOAT_EQ(1, c.getY());
  37. }
  38. TEST_F(Vector2Test, operator_scalar)
  39. {
  40. vector_scalar scalar = Vector2(3, 4) * Vector2(5, 3);
  41. ASSERT_FLOAT_EQ(27, scalar);
  42. }
  43. TEST_F(Vector2Test, operator_multiplicator)
  44. {
  45. Vector2 c = Vector2(3, 4) * 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, 4) == Vector2(3, 4));
  64. }
  65. TEST_F(Vector2Test, operator_equals_not_equals)
  66. {
  67. ASSERT_FALSE(Vector2(3, 8) == Vector2(3, 4));
  68. }
  69. TEST_F(Vector2Test, operator_not_equals)
  70. {
  71. ASSERT_TRUE(Vector2(3, 5) != Vector2(3, 4));
  72. }
  73. TEST_F(Vector2Test, operator_not_equals_equals)
  74. {
  75. ASSERT_FALSE(Vector2(3, 4) != Vector2(3, 4));
  76. }
  77. TEST_F(Vector2Test, getLength)
  78. {
  79. Vector2 a{6, 8};
  80. ASSERT_FLOAT_EQ(10, a.getLength());
  81. }
  82. TEST_F(Vector2Test, getX)
  83. {
  84. Vector2 a{3, 4};
  85. ASSERT_FLOAT_EQ(3, a.getX());
  86. }
  87. TEST_F(Vector2Test, getY)
  88. {
  89. Vector2 a{3, 4};
  90. ASSERT_FLOAT_EQ(4, a.getY());
  91. }
  92. TEST_F(Vector2Test, setX)
  93. {
  94. Vector2 a{3, 4};
  95. a.setX(14);
  96. ASSERT_FLOAT_EQ(14, a.getX());
  97. }
  98. TEST_F(Vector2Test, setY)
  99. {
  100. Vector2 a{3, 4};
  101. a.setY(8);
  102. ASSERT_FLOAT_EQ(8, a.getY());
  103. }
  104. }