SFML/test/Graphics/Rect.cpp

121 lines
4.2 KiB
C++
Raw Normal View History

2015-04-28 12:07:19 +08:00
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp>
#include <doctest.h>
2022-07-05 00:20:58 +08:00
#include <GraphicsUtil.hpp>
2021-12-24 21:31:27 +08:00
TEST_CASE("sf::Rect class template - [graphics]")
2015-04-28 12:07:19 +08:00
{
2021-12-24 21:31:27 +08:00
SUBCASE("Construction")
2015-04-28 12:07:19 +08:00
{
2021-12-24 21:31:27 +08:00
SUBCASE("Default constructor")
2015-04-28 12:07:19 +08:00
{
sf::IntRect rectangle;
CHECK(rectangle.left == 0);
CHECK(rectangle.top == 0);
CHECK(rectangle.width == 0);
CHECK(rectangle.height == 0);
}
2021-12-24 21:31:27 +08:00
SUBCASE("(left, top, width, height) constructor")
2015-04-28 12:07:19 +08:00
{
sf::IntRect rectangle({1, 2}, {3, 4});
2015-04-28 12:07:19 +08:00
CHECK(rectangle.left == 1);
CHECK(rectangle.top == 2);
CHECK(rectangle.width == 3);
CHECK(rectangle.height == 4);
}
2021-12-24 21:31:27 +08:00
SUBCASE("(Vector2, Vector2) constructor")
2015-04-28 12:07:19 +08:00
{
sf::Vector2i position(1, 2);
sf::Vector2i dimension(3, 4);
2022-07-05 00:20:58 +08:00
sf::IntRect rectangle(position, dimension);
2015-04-28 12:07:19 +08:00
CHECK(rectangle.left == 1);
CHECK(rectangle.top == 2);
CHECK(rectangle.width == 3);
CHECK(rectangle.height == 4);
}
2021-12-24 21:31:27 +08:00
SUBCASE("Conversion constructor")
2015-04-28 12:07:19 +08:00
{
sf::FloatRect sourceRectangle({1.0f, 2.0f}, {3.0f, 4.0f});
2022-07-05 00:20:58 +08:00
sf::IntRect rectangle(sourceRectangle);
2015-04-28 12:07:19 +08:00
CHECK(rectangle.left == static_cast<int>(sourceRectangle.left));
CHECK(rectangle.top == static_cast<int>(sourceRectangle.top));
CHECK(rectangle.width == static_cast<int>(sourceRectangle.width));
CHECK(rectangle.height == static_cast<int>(sourceRectangle.height));
}
}
2022-06-25 07:00:59 +08:00
SUBCASE("contains(Vector2)")
2015-04-28 12:07:19 +08:00
{
2022-06-25 07:00:59 +08:00
sf::IntRect rectangle({0, 0}, {10, 10});
CHECK(rectangle.contains(sf::Vector2i(0, 0)) == true);
CHECK(rectangle.contains(sf::Vector2i(9, 0)) == true);
CHECK(rectangle.contains(sf::Vector2i(0, 9)) == true);
CHECK(rectangle.contains(sf::Vector2i(9, 9)) == true);
CHECK(rectangle.contains(sf::Vector2i(9, 10)) == false);
CHECK(rectangle.contains(sf::Vector2i(10, 9)) == false);
CHECK(rectangle.contains(sf::Vector2i(10, 10)) == false);
CHECK(rectangle.contains(sf::Vector2i(15, 15)) == false);
2015-04-28 12:07:19 +08:00
}
2022-06-25 07:00:59 +08:00
SUBCASE("findIntersection()")
2015-04-28 12:07:19 +08:00
{
const sf::IntRect rectangle({0, 0}, {10, 10});
const sf::IntRect intersectingRectangle({5, 5}, {10, 10});
const auto intersectionResult = rectangle.findIntersection(intersectingRectangle);
REQUIRE(intersectionResult.has_value());
CHECK(intersectionResult->top == 5);
CHECK(intersectionResult->left == 5);
CHECK(intersectionResult->width == 5);
CHECK(intersectionResult->height == 5);
const sf::IntRect nonIntersectingRectangle({-5, -5}, {5, 5});
CHECK_FALSE(rectangle.findIntersection(nonIntersectingRectangle).has_value());
2015-04-28 12:07:19 +08:00
}
2022-06-25 07:00:59 +08:00
SUBCASE("getPosition()")
{
CHECK(sf::IntRect({}, {}).getPosition() == sf::Vector2i());
CHECK(sf::IntRect({1, 2}, {3, 4}).getPosition() == sf::Vector2i(1, 2));
}
SUBCASE("getSize()")
2015-04-28 12:07:19 +08:00
{
2022-06-25 07:00:59 +08:00
CHECK(sf::IntRect({}, {}).getSize() == sf::Vector2i());
CHECK(sf::IntRect({1, 2}, {3, 4}).getSize() == sf::Vector2i(3, 4));
}
2015-04-28 12:07:19 +08:00
2022-06-25 07:00:59 +08:00
SUBCASE("Operators")
{
SUBCASE("operator==")
{
CHECK(sf::IntRect() == sf::IntRect());
CHECK(sf::IntRect({1, 3}, {2, 5}) == sf::IntRect({1, 3}, {2, 5}));
CHECK_FALSE(sf::IntRect({1, 0}, {0, 0}) == sf::IntRect({0, 0}, {0, 0}));
CHECK_FALSE(sf::IntRect({0, 1}, {0, 0}) == sf::IntRect({0, 0}, {0, 0}));
CHECK_FALSE(sf::IntRect({0, 0}, {1, 0}) == sf::IntRect({0, 0}, {0, 0}));
CHECK_FALSE(sf::IntRect({0, 0}, {0, 1}) == sf::IntRect({0, 0}, {0, 0}));
}
SUBCASE("operator!=")
{
CHECK(sf::IntRect({1, 0}, {0, 0}) != sf::IntRect({0, 0}, {0, 0}));
CHECK(sf::IntRect({0, 1}, {0, 0}) != sf::IntRect({0, 0}, {0, 0}));
CHECK(sf::IntRect({0, 0}, {1, 0}) != sf::IntRect({0, 0}, {0, 0}));
CHECK(sf::IntRect({0, 0}, {0, 1}) != sf::IntRect({0, 0}, {0, 0}));
CHECK_FALSE(sf::IntRect() != sf::IntRect());
CHECK_FALSE(sf::IntRect({1, 3}, {2, 5}) != sf::IntRect({1, 3}, {2, 5}));
}
2015-04-28 12:07:19 +08:00
}
}