rectf_test.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /* FIXME: Current implementation is actually overlaps(), not contains()
  24. TEST(RectfTest, contains_rect)
  25. {
  26. ASSERT_TRUE(Rectf(100.0f, 100.0f, 200.0f, 200.0f).contains(Rectf(150.0f, 150.0f, 190.0f, 190.0f)));
  27. ASSERT_TRUE(Rectf(100.0f, 100.0f, 200.0f, 200.0f).contains(Rectf(100.0f, 100.0f, 200.0f, 200.0f)));
  28. ASSERT_FALSE(Rectf(100.0f, 100.0f, 200.0f, 200.0f).contains(Rectf(150.0f, 150.0f, 250.0f, 250.0f)));
  29. }
  30. */
  31. TEST(RectfTest, moved)
  32. {
  33. 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));
  34. 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));
  35. }
  36. TEST(RectfTest, set_leftrighttopbottom)
  37. {
  38. Rectf rect(0.0f, 50.0f, 100.0f, 150.0f);
  39. rect.set_left(10.0f);
  40. ASSERT_EQ(Rectf(10.0f, 50.0f, 100.0f, 150.0f), rect);
  41. rect.set_right(200.0f);
  42. ASSERT_EQ(Rectf(10.0f, 50.0f, 200.0f, 150.0f), rect);
  43. rect.set_top(100.0f);
  44. ASSERT_EQ(Rectf(10.0f, 100.0f, 200.0f, 150.0f), rect);
  45. rect.set_bottom(200.0f);
  46. ASSERT_EQ(Rectf(10.0f, 100.0f, 200.0f, 200.0f), rect);
  47. }
  48. TEST(RectfTest, size)
  49. {
  50. ASSERT_EQ(Rectf(50.0f, 50.0f, 100.0f, 300.0f).get_width(), 50.0f);
  51. ASSERT_EQ(Rectf(50.0f, 50.0f, 100.0f, 300.0f).get_height(), 250.0f);
  52. }
  53. TEST(RectfTest, from_center)
  54. {
  55. ASSERT_EQ(Rectf::from_center({16.0f, 16.0f}, {32.0f, 32.0f}), Rectf(0.0f, 0.0f, 32.0f, 32.0f));
  56. }
  57. TEST(RectfTest, set_p1)
  58. {
  59. Rectf rect(Vector(16.0f, 16.0f), Vector(32.0f, 32.0f));
  60. rect.set_p1({1.0f, 5.0f});
  61. ASSERT_EQ(Rectf(Vector(1.0f, 5.0f), Vector(32.0f, 32.0f)), rect);
  62. }
  63. TEST(RectfTest, set_p2)
  64. {
  65. Rectf rect(Vector(16.0f, 16.0f), Vector(32.0f, 32.0f));
  66. rect.set_p2({48.0f, 100.0f});
  67. ASSERT_EQ(Rectf(Vector(16.0f, 16.0f), Vector(48.0f, 100.0f)), rect);
  68. }
  69. /* EOF */