TestRect.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Rect.h>
  7. #include <LibTest/TestCase.h>
  8. TEST_CASE(int_rect_right_and_bottom)
  9. {
  10. Gfx::IntRect rect = { 2, 3, 4, 5 };
  11. EXPECT_EQ(rect.right(), 6);
  12. EXPECT_EQ(rect.bottom(), 8);
  13. }
  14. TEST_CASE(float_rect_right_and_bottom)
  15. {
  16. Gfx::FloatRect rect = { 1.f, 2.f, 3.5f, 4.5f };
  17. EXPECT_APPROXIMATE(rect.right(), 4.5f);
  18. EXPECT_APPROXIMATE(rect.bottom(), 6.5f);
  19. }
  20. TEST_CASE(rect_contains_vertically)
  21. {
  22. Gfx::FloatRect rect = { 0.f, 0.f, 100.f, 100.f };
  23. EXPECT(rect.contains_vertically(99.f));
  24. EXPECT(!rect.contains_vertically(100.f));
  25. }
  26. TEST_CASE(rect_shatter)
  27. {
  28. Gfx::IntRect glass_plate = { 0, 0, 100, 100 };
  29. Gfx::IntRect hammer = { 30, 40, 40, 10 };
  30. auto shards = glass_plate.shatter(hammer);
  31. EXPECT(!shards.is_empty());
  32. int total_shard_area = 0;
  33. for (auto shard : shards) {
  34. EXPECT(glass_plate.contains(shard));
  35. EXPECT(!hammer.intersects(shard));
  36. total_shard_area += shard.size().area();
  37. }
  38. EXPECT_EQ(glass_plate.size().area() - hammer.size().area(), total_shard_area);
  39. }
  40. TEST_CASE(rect_closest_to)
  41. {
  42. Gfx::IntRect const screen_rect = { 0, 0, 960, 540 };
  43. Gfx::Point<int> p = { 460, 592 }; // point is below the rect
  44. Gfx::Point<int> closest = screen_rect.closest_to(p);
  45. EXPECT_EQ(screen_rect.side(closest), Gfx::IntRect::Side::Bottom);
  46. p = { 960, 0 }; // point exactly on top right corner
  47. closest = screen_rect.closest_to(p);
  48. EXPECT_EQ(screen_rect.side(closest), Gfx::IntRect::Side::Top);
  49. }
  50. TEST_CASE(rect_unite_horizontally)
  51. {
  52. Gfx::IntRect rect { 10, 10, 100, 100 };
  53. Gfx::IntRect huge_rect { 0, 0, 1000, 1000 };
  54. rect.unite_horizontally(huge_rect);
  55. EXPECT_EQ(rect.left(), 0);
  56. EXPECT_EQ(rect.right(), 1000);
  57. EXPECT_EQ(rect.top(), 10);
  58. EXPECT_EQ(rect.bottom(), 110);
  59. }
  60. TEST_CASE(rect_unite_vertically)
  61. {
  62. Gfx::IntRect rect { 10, 10, 100, 100 };
  63. Gfx::IntRect huge_rect { 0, 0, 1000, 1000 };
  64. rect.unite_vertically(huge_rect);
  65. EXPECT_EQ(rect.top(), 0);
  66. EXPECT_EQ(rect.bottom(), 1000);
  67. EXPECT_EQ(rect.left(), 10);
  68. EXPECT_EQ(rect.right(), 110);
  69. }