TestRect.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. }