From b607308b3f1e58222e3ba70322f94dfa1ad0ce13 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Mon, 23 Dec 2024 22:53:41 -0600 Subject: [PATCH] Add tests for `sfShape` --- test/CMakeLists.txt | 1 + test/Graphics/Shape.test.cpp | 107 +++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 test/Graphics/Shape.test.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8935697..928d1d5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -51,6 +51,7 @@ add_executable(test-csfml-graphics Graphics/PrimitiveType.test.cpp Graphics/Rect.test.cpp Graphics/RenderStates.test.cpp + Graphics/Shape.test.cpp Graphics/StencilMode.test.cpp Graphics/Transform.test.cpp Graphics/VertexArray.test.cpp diff --git a/test/Graphics/Shape.test.cpp b/test/Graphics/Shape.test.cpp new file mode 100644 index 0000000..e43ee42 --- /dev/null +++ b/test/Graphics/Shape.test.cpp @@ -0,0 +1,107 @@ +#include + +#include + +TEST_CASE("[Graphics] sfShape") +{ + const auto getPointCount = [](void* userData) -> std::size_t + { return static_cast*>(userData)->size(); }; + const auto getPoint = [](std::size_t index, void* userData) -> sfVector2f + { return static_cast*>(userData)->operator[](index); }; + std::vector points = {{0, 0}, {30, 0}, {0, 30}}; + + SECTION("sfShape_create") + { + const sfShape* shape = sfShape_create(getPointCount, getPoint, &points); + const sfVector2f position = sfShape_getPosition(shape); + CHECK(position.x == 0); + CHECK(position.y == 0); + CHECK(sfShape_getRotation(shape) == 0); + const sfVector2f scale = sfShape_getScale(shape); + CHECK(scale.x == 1); + CHECK(scale.y == 1); + const sfVector2f origin = sfShape_getOrigin(shape); + CHECK(origin.x == 0); + CHECK(origin.y == 0); + const sfTransform transform = sfShape_getTransform(shape); + const std::vector matrix(std::data(transform.matrix), std::data(transform.matrix) + std::size(transform.matrix)); + CHECK(matrix == std::vector({1, 0, 0, 0, 1, 0, 0, 0, 1})); + const sfTransform inverseTransform = sfShape_getInverseTransform(shape); + const std::vector inverseMatrix(std::data(inverseTransform.matrix), + std::data(inverseTransform.matrix) + std::size(inverseTransform.matrix)); + CHECK(inverseMatrix == std::vector({1, 0, 0, 0, 1, 0, 0, 0, 1})); + CHECK(sfShape_getTexture(shape) == nullptr); + const sfIntRect textureRect = sfShape_getTextureRect(shape); + CHECK(textureRect.position.x == 0); + CHECK(textureRect.position.y == 0); + CHECK(textureRect.size.x == 0); + CHECK(textureRect.size.y == 0); + const sfColor fillColor = sfShape_getFillColor(shape); + CHECK(fillColor.r == sfWhite.r); + CHECK(fillColor.g == sfWhite.g); + CHECK(fillColor.b == sfWhite.b); + CHECK(fillColor.a == sfWhite.a); + const sfColor outlineColor = sfShape_getOutlineColor(shape); + CHECK(outlineColor.r == sfWhite.r); + CHECK(outlineColor.g == sfWhite.g); + CHECK(outlineColor.b == sfWhite.b); + CHECK(outlineColor.a == sfWhite.a); + CHECK(sfShape_getOutlineThickness(shape) == 0); + CHECK(sfShape_getPointCount(shape) == 3); + const sfVector2f point = sfShape_getPoint(shape, 0); + CHECK(point.x == 0); + CHECK(point.y == 0); + const sfVector2f geometricCenter = sfShape_getGeometricCenter(shape); + CHECK(geometricCenter.x == 10); + CHECK(geometricCenter.y == 10); + const sfFloatRect localBounds = sfShape_getLocalBounds(shape); + CHECK(localBounds.position.x == 0); + CHECK(localBounds.position.y == 0); + CHECK(localBounds.size.x == 0); + CHECK(localBounds.size.y == 0); + const sfFloatRect globalBounds = sfShape_getGlobalBounds(shape); + CHECK(globalBounds.position.x == 0); + CHECK(globalBounds.position.y == 0); + CHECK(globalBounds.size.x == 0); + CHECK(globalBounds.size.y == 0); + sfShape_destroy(shape); + } + + SECTION("Set/get position") + { + sfShape* shape = sfShape_create(getPointCount, getPoint, &points); + sfShape_setPosition(shape, {10, 20}); + const sfVector2f position = sfShape_getPosition(shape); + CHECK(position.x == 10); + CHECK(position.y == 20); + sfShape_destroy(shape); + } + + SECTION("Set/get rotation") + { + sfShape* shape = sfShape_create(getPointCount, getPoint, &points); + sfShape_setRotation(shape, 45); + CHECK(sfShape_getRotation(shape) == 45); + sfShape_destroy(shape); + } + + SECTION("Set/get scale") + { + sfShape* shape = sfShape_create(getPointCount, getPoint, &points); + sfShape_setScale(shape, {0.5f, 0.75f}); + const sfVector2f scale = sfShape_getScale(shape); + CHECK(scale.x == 0.5f); + CHECK(scale.y == 0.75f); + sfShape_destroy(shape); + } + + SECTION("Set/get origin") + { + sfShape* shape = sfShape_create(getPointCount, getPoint, &points); + sfShape_setOrigin(shape, {80, 90}); + const sfVector2f origin = sfShape_getOrigin(shape); + CHECK(origin.x == 80); + CHECK(origin.y == 90); + sfShape_destroy(shape); + } +}