Skip to content

Commit b607308

Browse files
committed
Add tests for sfShape
1 parent e37de74 commit b607308

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ add_executable(test-csfml-graphics
5151
Graphics/PrimitiveType.test.cpp
5252
Graphics/Rect.test.cpp
5353
Graphics/RenderStates.test.cpp
54+
Graphics/Shape.test.cpp
5455
Graphics/StencilMode.test.cpp
5556
Graphics/Transform.test.cpp
5657
Graphics/VertexArray.test.cpp

test/Graphics/Shape.test.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <CSFML/Graphics/Shape.h>
2+
3+
#include <catch2/catch_test_macros.hpp>
4+
5+
TEST_CASE("[Graphics] sfShape")
6+
{
7+
const auto getPointCount = [](void* userData) -> std::size_t
8+
{ return static_cast<std::vector<sfVector2f>*>(userData)->size(); };
9+
const auto getPoint = [](std::size_t index, void* userData) -> sfVector2f
10+
{ return static_cast<std::vector<sfVector2f>*>(userData)->operator[](index); };
11+
std::vector<sfVector2f> points = {{0, 0}, {30, 0}, {0, 30}};
12+
13+
SECTION("sfShape_create")
14+
{
15+
const sfShape* shape = sfShape_create(getPointCount, getPoint, &points);
16+
const sfVector2f position = sfShape_getPosition(shape);
17+
CHECK(position.x == 0);
18+
CHECK(position.y == 0);
19+
CHECK(sfShape_getRotation(shape) == 0);
20+
const sfVector2f scale = sfShape_getScale(shape);
21+
CHECK(scale.x == 1);
22+
CHECK(scale.y == 1);
23+
const sfVector2f origin = sfShape_getOrigin(shape);
24+
CHECK(origin.x == 0);
25+
CHECK(origin.y == 0);
26+
const sfTransform transform = sfShape_getTransform(shape);
27+
const std::vector matrix(std::data(transform.matrix), std::data(transform.matrix) + std::size(transform.matrix));
28+
CHECK(matrix == std::vector<float>({1, 0, 0, 0, 1, 0, 0, 0, 1}));
29+
const sfTransform inverseTransform = sfShape_getInverseTransform(shape);
30+
const std::vector inverseMatrix(std::data(inverseTransform.matrix),
31+
std::data(inverseTransform.matrix) + std::size(inverseTransform.matrix));
32+
CHECK(inverseMatrix == std::vector<float>({1, 0, 0, 0, 1, 0, 0, 0, 1}));
33+
CHECK(sfShape_getTexture(shape) == nullptr);
34+
const sfIntRect textureRect = sfShape_getTextureRect(shape);
35+
CHECK(textureRect.position.x == 0);
36+
CHECK(textureRect.position.y == 0);
37+
CHECK(textureRect.size.x == 0);
38+
CHECK(textureRect.size.y == 0);
39+
const sfColor fillColor = sfShape_getFillColor(shape);
40+
CHECK(fillColor.r == sfWhite.r);
41+
CHECK(fillColor.g == sfWhite.g);
42+
CHECK(fillColor.b == sfWhite.b);
43+
CHECK(fillColor.a == sfWhite.a);
44+
const sfColor outlineColor = sfShape_getOutlineColor(shape);
45+
CHECK(outlineColor.r == sfWhite.r);
46+
CHECK(outlineColor.g == sfWhite.g);
47+
CHECK(outlineColor.b == sfWhite.b);
48+
CHECK(outlineColor.a == sfWhite.a);
49+
CHECK(sfShape_getOutlineThickness(shape) == 0);
50+
CHECK(sfShape_getPointCount(shape) == 3);
51+
const sfVector2f point = sfShape_getPoint(shape, 0);
52+
CHECK(point.x == 0);
53+
CHECK(point.y == 0);
54+
const sfVector2f geometricCenter = sfShape_getGeometricCenter(shape);
55+
CHECK(geometricCenter.x == 10);
56+
CHECK(geometricCenter.y == 10);
57+
const sfFloatRect localBounds = sfShape_getLocalBounds(shape);
58+
CHECK(localBounds.position.x == 0);
59+
CHECK(localBounds.position.y == 0);
60+
CHECK(localBounds.size.x == 0);
61+
CHECK(localBounds.size.y == 0);
62+
const sfFloatRect globalBounds = sfShape_getGlobalBounds(shape);
63+
CHECK(globalBounds.position.x == 0);
64+
CHECK(globalBounds.position.y == 0);
65+
CHECK(globalBounds.size.x == 0);
66+
CHECK(globalBounds.size.y == 0);
67+
sfShape_destroy(shape);
68+
}
69+
70+
SECTION("Set/get position")
71+
{
72+
sfShape* shape = sfShape_create(getPointCount, getPoint, &points);
73+
sfShape_setPosition(shape, {10, 20});
74+
const sfVector2f position = sfShape_getPosition(shape);
75+
CHECK(position.x == 10);
76+
CHECK(position.y == 20);
77+
sfShape_destroy(shape);
78+
}
79+
80+
SECTION("Set/get rotation")
81+
{
82+
sfShape* shape = sfShape_create(getPointCount, getPoint, &points);
83+
sfShape_setRotation(shape, 45);
84+
CHECK(sfShape_getRotation(shape) == 45);
85+
sfShape_destroy(shape);
86+
}
87+
88+
SECTION("Set/get scale")
89+
{
90+
sfShape* shape = sfShape_create(getPointCount, getPoint, &points);
91+
sfShape_setScale(shape, {0.5f, 0.75f});
92+
const sfVector2f scale = sfShape_getScale(shape);
93+
CHECK(scale.x == 0.5f);
94+
CHECK(scale.y == 0.75f);
95+
sfShape_destroy(shape);
96+
}
97+
98+
SECTION("Set/get origin")
99+
{
100+
sfShape* shape = sfShape_create(getPointCount, getPoint, &points);
101+
sfShape_setOrigin(shape, {80, 90});
102+
const sfVector2f origin = sfShape_getOrigin(shape);
103+
CHECK(origin.x == 80);
104+
CHECK(origin.y == 90);
105+
sfShape_destroy(shape);
106+
}
107+
}

0 commit comments

Comments
 (0)