rectf_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SuperTux
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <gtest/gtest.h>
  17. #include "math/rectf.hpp"
  18. TEST(RectfTest, contains_point)
  19. {
  20. ASSERT_TRUE(Rectf(100.0f, 100.0f, 200.0f, 200.0f).contains(Vector(150.0f, 150.0f)));
  21. ASSERT_FALSE(Rectf(100.0f, 100.0f, 200.0f, 200.0f).contains(Vector(250.0f, 150.0f)));
  22. }
  23. TEST(RectfTest, overlaps_rect)
  24. {
  25. ASSERT_TRUE(Rectf(100.0f, 100.0f, 200.0f, 200.0f).overlaps(Rectf(150.0f, 150.0f, 190.0f, 190.0f)));
  26. ASSERT_TRUE(Rectf(100.0f, 100.0f, 200.0f, 200.0f).overlaps(Rectf(100.0f, 100.0f, 200.0f, 200.0f)));
  27. ASSERT_FALSE(Rectf(100.0f, 100.0f, 200.0f, 200.0f).overlaps(Rectf(250.0f, 250.0f, 300.0f, 300.0f)));
  28. }
  29. TEST(RectfTest, moved)
  30. {
  31. ASSERT_EQ(Rectf(0.0f, 0.0f, 100.0f, 300.0f).moved(Vector(16.0f, 32.0f)), Rectf(16.0f, 32.0f, 116.0f, 332.0f));
  32. ASSERT_EQ(Rectf(0.0f, 0.0f, 100.0f, 300.0f).moved(Vector(-16.0f, -32.0f)), Rectf(-16.0f, -32.0f, 84.0f, 268.0f));
  33. }
  34. TEST(RectfTest, set_leftrighttopbottom)
  35. {
  36. Rectf rect(0.0f, 50.0f, 100.0f, 150.0f);
  37. rect.set_left(10.0f);
  38. ASSERT_EQ(Rectf(10.0f, 50.0f, 100.0f, 150.0f), rect);
  39. rect.set_right(200.0f);
  40. ASSERT_EQ(Rectf(10.0f, 50.0f, 200.0f, 150.0f), rect);
  41. rect.set_top(100.0f);
  42. ASSERT_EQ(Rectf(10.0f, 100.0f, 200.0f, 150.0f), rect);
  43. rect.set_bottom(200.0f);
  44. ASSERT_EQ(Rectf(10.0f, 100.0f, 200.0f, 200.0f), rect);
  45. }
  46. TEST(RectfTest, size)
  47. {
  48. ASSERT_EQ(Rectf(50.0f, 50.0f, 100.0f, 300.0f).get_width(), 50.0f);
  49. ASSERT_EQ(Rectf(50.0f, 50.0f, 100.0f, 300.0f).get_height(), 250.0f);
  50. }
  51. TEST(RectfTest, from_center)
  52. {
  53. ASSERT_EQ(Rectf::from_center({16.0f, 16.0f}, {32.0f, 32.0f}), Rectf(0.0f, 0.0f, 32.0f, 32.0f));
  54. }
  55. TEST(RectfTest, set_p1)
  56. {
  57. Rectf rect(Vector(16.0f, 16.0f), Vector(32.0f, 32.0f));
  58. rect.set_p1({1.0f, 5.0f});
  59. ASSERT_EQ(Rectf(Vector(1.0f, 5.0f), Vector(32.0f, 32.0f)), rect);
  60. }
  61. TEST(RectfTest, set_p2)
  62. {
  63. Rectf rect(Vector(16.0f, 16.0f), Vector(32.0f, 32.0f));
  64. rect.set_p2({48.0f, 100.0f});
  65. ASSERT_EQ(Rectf(Vector(16.0f, 16.0f), Vector(48.0f, 100.0f)), rect);
  66. }
  67. /* EOF */