test_vector2.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <AP_gtest.h>
  2. #include <AP_Math/AP_Math.h>
  3. TEST(Vector2Test, IsEqual)
  4. {
  5. Vector2l v_int1(1, 1);
  6. Vector2l v_int2(1, 0);
  7. Vector2f v_float1(1.0f, 1.0f);
  8. Vector2f v_float2(1.0f, 0.0f);
  9. EXPECT_FALSE(v_int1 == v_int2);
  10. EXPECT_TRUE(v_int1 == v_int1);
  11. EXPECT_FALSE(v_float1 == v_float2);
  12. EXPECT_TRUE(v_float1 == v_float1);
  13. }
  14. TEST(Vector2Test, angle)
  15. {
  16. EXPECT_FLOAT_EQ(M_PI/2, Vector2f(0, 1).angle());
  17. EXPECT_FLOAT_EQ(M_PI/4, Vector2f(1, 1).angle());
  18. EXPECT_FLOAT_EQ(0.0f, Vector2f(1, 0).angle());
  19. EXPECT_FLOAT_EQ(M_PI*5/4, Vector2f(-1, -1).angle());
  20. EXPECT_FLOAT_EQ(M_PI*5/4, Vector2f(-5, -5).angle());
  21. }
  22. TEST(Vector2Test, length)
  23. {
  24. EXPECT_FLOAT_EQ(25, Vector2f(3, 4).length_squared());
  25. }
  26. TEST(Vector2Test, normalized)
  27. {
  28. EXPECT_EQ(Vector2f(sqrtf(2)/2, sqrtf(2)/2), Vector2f(5, 5).normalized());
  29. EXPECT_EQ(Vector2f(3, 3).normalized(), Vector2f(5, 5).normalized());
  30. EXPECT_EQ(Vector2f(-3, 3).normalized(), Vector2f(-5, 5).normalized());
  31. EXPECT_NE(Vector2f(-3, 3).normalized(), Vector2f(5, 5).normalized());
  32. }
  33. TEST(Vector2Test, reflect)
  34. {
  35. Vector2f reflected1 = Vector2f(3, 8);
  36. reflected1.reflect(Vector2f(0, 1));
  37. EXPECT_EQ(reflected1, Vector2f(-3, 8));
  38. // colinear vectors
  39. Vector2f reflected2 = Vector2f(3, 3);
  40. reflected2.reflect(Vector2f(1, 1));
  41. EXPECT_EQ(reflected2, Vector2f(3, 3));
  42. // orthogonal vectors
  43. Vector2f reflected3 = Vector2f(3, 3);
  44. reflected3.reflect(Vector2f(1, -1));
  45. EXPECT_EQ(reflected3, Vector2f(-3, -3));
  46. }
  47. TEST(Vector2Test, closest_point)
  48. {
  49. // closest_point is (p, v,w)
  50. // the silly case:
  51. EXPECT_EQ((Vector2f{0, 0}),
  52. (Vector2f::closest_point(Vector2f{0, 0}, Vector2f{0, 0}, Vector2f{0, 0})));
  53. // on line:
  54. EXPECT_EQ((Vector2f{0, 0}),
  55. (Vector2f::closest_point(Vector2f{0, 0}, Vector2f{0, 0}, Vector2f{1, 1})));
  56. EXPECT_EQ((Vector2f{5, 5}),
  57. (Vector2f::closest_point(Vector2f{5, 5}, Vector2f{0, 0}, Vector2f{5, 5})));
  58. // on line but not segment:
  59. EXPECT_EQ((Vector2f{5, 5}),
  60. (Vector2f::closest_point(Vector2f{6, 6}, Vector2f{0, 0}, Vector2f{5, 5})));
  61. EXPECT_EQ((Vector2f{0.5, 0.5}),
  62. (Vector2f::closest_point(Vector2f{1,0}, Vector2f{0, 0}, Vector2f{5, 5})));
  63. EXPECT_EQ((Vector2f{0, 1}),
  64. (Vector2f::closest_point(Vector2f{0,0}, Vector2f{-1, 1}, Vector2f{1, 1})));
  65. }
  66. AP_GTEST_MAIN()