rect_test.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/rect.hpp"
  18. TEST(RectTest, contains_point)
  19. {
  20. ASSERT_TRUE(Rect(100, 100, 200, 200).contains(150, 150));
  21. ASSERT_FALSE(Rect(100, 100, 200, 200).contains(250, 150));
  22. }
  23. TEST(RectTest, contains_rect)
  24. {
  25. ASSERT_TRUE(Rect(100, 100, 200, 200).contains(Rect(150, 150, 190, 190)));
  26. ASSERT_TRUE(Rect(100, 100, 200, 200).contains(Rect(100, 100, 200, 200)));
  27. ASSERT_FALSE(Rect(100, 100, 200, 200).contains(Rect(150, 150, 250, 250)));
  28. }
  29. TEST(RectTest, moved)
  30. {
  31. ASSERT_EQ(Rect(0, 0, 100, 300).moved(16, 32), Rect(16, 32, 116, 332));
  32. ASSERT_EQ(Rect(0, 0, 100, 300).moved(-16, -32), Rect(-16, -32, 84, 268));
  33. }
  34. TEST(RectTest, normalized)
  35. {
  36. ASSERT_EQ(Rect(0, 0, 100, 300).normalized(), Rect(0, 0, 100, 300));
  37. ASSERT_EQ(Rect(100, 300, 0, 0).normalized(), Rect(0, 0, 100, 300));
  38. }
  39. TEST(RectTest, valid)
  40. {
  41. ASSERT_TRUE(Rect(0, 0, 100, 300).valid());
  42. ASSERT_FALSE(Rect(100, 300, 0, 0).valid());
  43. ASSERT_TRUE(Rect(0, 0, 0, 0).valid());
  44. }
  45. TEST(RectTest, empty)
  46. {
  47. ASSERT_FALSE(Rect(0, 0, 100, 300).empty());
  48. ASSERT_TRUE(Rect(100, 300, 0, 0).empty());
  49. ASSERT_TRUE(Rect(0, 0, 0, 0).empty());
  50. }
  51. TEST(RectTest, size)
  52. {
  53. ASSERT_EQ(Rect(50, 50, 100, 300).get_width(), 50);
  54. ASSERT_EQ(Rect(50, 50, 100, 300).get_height(), 250);
  55. }
  56. TEST(RectTest, from_center)
  57. {
  58. ASSERT_EQ(Rect::from_center(16, 16, 32, 32), Rect(0, 0, 32, 32));
  59. }
  60. TEST(RectTest, SDL)
  61. {
  62. // SDL
  63. const SDL_Rect sdl_rect_result = Rect(50, 50, 100, 100).to_sdl();
  64. const SDL_Rect sdl_rect_expected{50, 50, 50, 50};
  65. ASSERT_TRUE(SDL_RectEquals(&sdl_rect_result, &sdl_rect_expected));
  66. ASSERT_EQ(Rect(SDL_Rect{50, 50, 50, 50}), Rect(50, 50, 100, 100));
  67. }
  68. /* EOF */