Skip to content

Commit 39bb71c

Browse files
committed
tests: port failing SDL_Rect tests from pysdl2 to testautomation
1 parent 177fb9c commit 39bb71c

File tree

1 file changed

+273
-1
lines changed

1 file changed

+273
-1
lines changed

test/testautomation_rect.c

Lines changed: 273 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@
1212

1313
/* Helper functions */
1414

15+
/* !
16+
* \brief Private helper to check SDL_FRect equality
17+
*/
18+
static SDL_bool IsFRectEqual(const SDL_FRect *r1, const SDL_FRect *r2) {
19+
static const float MAX_DELTA = 1e-5f;
20+
SDL_FRect delta;
21+
delta.x = r1->x - r2->x;
22+
delta.y = r1->y - r2->y;
23+
delta.w = r1->w - r2->w;
24+
delta.h = r1->h - r2->h;
25+
26+
return -MAX_DELTA <= delta.x && delta.x <= MAX_DELTA
27+
&& -MAX_DELTA <= delta.y && delta.y <= MAX_DELTA
28+
&& -MAX_DELTA <= delta.w && delta.w <= MAX_DELTA
29+
&& -MAX_DELTA <= delta.w && delta.h <= MAX_DELTA;
30+
}
31+
32+
/* !
33+
* \brief Private helper to check SDL_FPoint equality
34+
*/
35+
static SDL_bool IsFPointEqual(const SDL_FPoint *p1, const SDL_FPoint *p2) {
36+
static const float MAX_DELTA = 1e-5f;
37+
SDL_FPoint delta;
38+
delta.x = p1->x - p2->x;
39+
delta.y = p1->y - p2->y;
40+
41+
return -MAX_DELTA <= delta.x && delta.x <= MAX_DELTA
42+
&& -MAX_DELTA <= delta.y && delta.y <= MAX_DELTA;
43+
}
44+
45+
/* Helper functions */
46+
1547
/* !
1648
* \brief Private helper to check SDL_IntersectRectAndLine results
1749
*/
@@ -1698,6 +1730,226 @@ int rect_testFRectEqualsParam(void *arg)
16981730
return TEST_COMPLETED;
16991731
}
17001732

1733+
/* !
1734+
* \brief Test SDL_HasIntersectionF
1735+
*
1736+
* \sa
1737+
* http://wiki.libsdl.org/SDL2/SDL_HasIntersectionF
1738+
*/
1739+
int rect_testHasIntersectionF(void *arg)
1740+
{
1741+
const struct {
1742+
SDL_FRect r1;
1743+
SDL_FRect r2;
1744+
SDL_bool expected;
1745+
} cases[] = {
1746+
{ { 0, 0, 0, 0 }, {0, 0, 0, 0}, SDL_FALSE },
1747+
{ { 0, 0, -200, 200 }, {0, 0, -200, 200}, SDL_FALSE },
1748+
{ { 0, 0, 10, 10 }, {-5, 5, 10, 2}, SDL_TRUE },
1749+
{ { 0, 0, 10, 10 }, {-5, -5, 10, 2}, SDL_FALSE },
1750+
{ { 0, 0, 10, 10 }, {-5, -5, 2, 10}, SDL_FALSE },
1751+
{ { 0, 0, 10, 10 }, {-5, -5, 5, 5}, SDL_FALSE },
1752+
{ { 0, 0, 10, 10 }, {-5, -5, 5.1f, 5.1f}, SDL_TRUE },
1753+
{ { 0, 0, 10, 10 }, {-4.99f, -4.99f, 5, 5}, SDL_TRUE },
1754+
};
1755+
size_t i;
1756+
1757+
for (i = 0; i < SDL_arraysize(cases); i++) {
1758+
SDL_bool result;
1759+
SDLTest_AssertPass("About to call SDL_HasIntersectionF(&{ %g, %g, %g, %g }, &{ %g, %g, %g, %g })",
1760+
cases[i].r1.x, cases[i].r1.y, cases[i].r1.w, cases[i].r1.h,
1761+
cases[i].r2.x, cases[i].r2.y, cases[i].r2.w, cases[i].r2.h
1762+
);
1763+
result = SDL_HasIntersectionF(&cases[i].r1, &cases[i].r2);
1764+
SDLTest_AssertCheck(result == cases[i].expected, "Got %d, expected %d", result, cases[i].expected);
1765+
}
1766+
return TEST_COMPLETED;
1767+
}
1768+
1769+
/* !
1770+
* \brief Test SDL_IntersectFRect
1771+
*
1772+
* \sa
1773+
* http://wiki.libsdl.org/SDL2/SDL_IntersectFRect
1774+
*/
1775+
int rect_testIntersectFRect(void *arg)
1776+
{
1777+
const struct {
1778+
SDL_FRect r1;
1779+
SDL_FRect r2;
1780+
SDL_bool result;
1781+
SDL_FRect intersect;
1782+
} cases[] = {
1783+
{ { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, SDL_FALSE },
1784+
{ { 0, 0, -200, 200 }, { 0, 0, -200, 200 }, SDL_FALSE },
1785+
{ { 0, 0, 10, 10 }, { -5, 5, 9.9f, 2 }, SDL_TRUE, { 0, 5, 4.9f, 2 } },
1786+
{ { 0, 0, 10, 10 }, { -5, -5, 10, 2 }, SDL_FALSE},
1787+
{ { 0, 0, 10, 10 }, { -5, -5, 2, 10 }, SDL_FALSE},
1788+
{ { 0, 0, 10, 10 }, { -5, -5, 5, 5 }, SDL_FALSE},
1789+
{ { 0, 0, 10, 10 }, { -5, -5, 5.5f, 6 }, SDL_TRUE, { 0, 0, 0.5f, 1 } }
1790+
};
1791+
size_t i;
1792+
1793+
for (i = 0; i < SDL_arraysize(cases); i++) {
1794+
SDL_bool result;
1795+
SDL_FRect intersect;
1796+
SDLTest_AssertPass("About to call SDL_IntersectFRect(&{ %g, %g, %g, %g }, &{ %g, %g, %g, %g })",
1797+
cases[i].r1.x, cases[i].r1.y, cases[i].r1.w, cases[i].r1.h,
1798+
cases[i].r2.x, cases[i].r2.y, cases[i].r2.w, cases[i].r2.h
1799+
);
1800+
result = SDL_IntersectFRect(&cases[i].r1, &cases[i].r2, &intersect);
1801+
SDLTest_AssertCheck(result == cases[i].result, "Got %d, expected %d", result, cases[i].result);
1802+
if (cases[i].result) {
1803+
SDLTest_AssertCheck(IsFRectEqual(&intersect, &cases[i].intersect),
1804+
"Got { %g, %g, %g, %g }, expected { %g, %g, %g, %g }",
1805+
intersect.x, intersect.y, intersect.w, intersect.h,
1806+
cases[i].intersect.x, cases[i].intersect.y, cases[i].intersect.w, cases[i].intersect.h);
1807+
}
1808+
}
1809+
return TEST_COMPLETED;
1810+
}
1811+
1812+
/* !
1813+
* \brief Test SDL_UnionFRect
1814+
*
1815+
* \sa
1816+
* http://wiki.libsdl.org/SDL2/SDL_UnionFRect
1817+
*/
1818+
int rect_testUnionFRect(void *arg)
1819+
{
1820+
const struct {
1821+
SDL_FRect r1;
1822+
SDL_FRect r2;
1823+
SDL_FRect expected;
1824+
} cases[] = {
1825+
{ { 0, 0, 10, 10 }, { 19.9f, 20, 10, 10 }, { 0, 0, 29.9f, 30 } },
1826+
{ { 0, 0, 0, 0 }, { 20, 20.1f, 10.1f, 10 }, { 20, 20.1f, 10.1f, 10 } },
1827+
{ { -200, -4.5f, 450, 33 }, { 20, 20, 10, 10 }, { -200, -4.5f, 450, 34.5f } },
1828+
{ { 0, 0, 15, 16.5f }, { 20, 20, 0, 0 }, { 0, 0, 15, 16.5f } }
1829+
};
1830+
size_t i;
1831+
1832+
for (i = 0; i < SDL_arraysize(cases); i++) {
1833+
SDL_FRect result;
1834+
SDLTest_AssertPass("About to call SDL_UnionFRect(&{ %g, %g, %g, %g }, &{ %g, %g, %g, %g })",
1835+
cases[i].r1.x, cases[i].r1.y, cases[i].r1.w, cases[i].r1.h,
1836+
cases[i].r2.x, cases[i].r2.y, cases[i].r2.w, cases[i].r2.h
1837+
);
1838+
SDL_UnionFRect(&cases[i].r1, &cases[i].r2, &result);
1839+
SDLTest_AssertCheck(IsFRectEqual(&result, &cases[i].expected),
1840+
"Got { %g, %g, %g, %g }, expected { %g, %g, %g, %g }",
1841+
result.x, result.y, result.w, result.h,
1842+
cases[i].expected.x, cases[i].expected.y, cases[i].expected.w, cases[i].expected.h);
1843+
}
1844+
return TEST_COMPLETED;
1845+
}
1846+
1847+
/* !
1848+
* \brief Test SDL_EncloseFPointsUnionFRect
1849+
*
1850+
* \sa
1851+
* http://wiki.libsdl.org/SDL2/SDL_EncloseFPoints
1852+
*/
1853+
int rect_testEncloseFPointsUnionFRect(void *arg)
1854+
{
1855+
const struct {
1856+
SDL_bool with_clip;
1857+
SDL_FRect clip;
1858+
SDL_bool result;
1859+
SDL_FRect enclosing;
1860+
} cases[] = {
1861+
{ SDL_TRUE, { 0, 0, 10, 10 }, SDL_TRUE, { 0.5f, 0.1f, 6, 8 }},
1862+
{ SDL_TRUE, { 1.2f, 1, 10, 10 }, SDL_TRUE, { 1.5f, 1.1f, 5, 7 }},
1863+
{ SDL_TRUE, { -10, -10, 3, 3 }, SDL_FALSE },
1864+
{ SDL_FALSE, { 0 }, SDL_TRUE, { 0.5, 0.1f, 6, 8 }}
1865+
};
1866+
const SDL_FPoint points[] = {
1867+
{ 0.5f, 0.1f },
1868+
{ 5.5f, 7.1f },
1869+
{ 1.5f, 1.1f }
1870+
};
1871+
char points_str[256];
1872+
size_t i;
1873+
1874+
SDL_strlcpy(points_str, "{", sizeof(points_str));
1875+
for (i = 0; i < SDL_arraysize(points); i++) {
1876+
char point_str[32];
1877+
SDL_snprintf(point_str, sizeof(point_str), "{ %g, %g }, ", points[i].x, points[i].y);
1878+
SDL_strlcat(points_str, point_str, sizeof(points_str));
1879+
}
1880+
SDL_strlcat(points_str, "}", sizeof(points_str));
1881+
for (i = 0; i < SDL_arraysize(cases); i++) {
1882+
char clip_str[64];
1883+
SDL_bool result;
1884+
SDL_FRect enclosing;
1885+
const SDL_FRect* clip_ptr = NULL;
1886+
if (cases[i].with_clip) {
1887+
SDL_snprintf(clip_str, sizeof(clip_str), "&{ %g, %g, %g, %g }",
1888+
cases[i].clip.x, cases[i].clip.y, cases[i].clip.w, cases[i].clip.h);
1889+
clip_ptr = &cases[i].clip;
1890+
} else {
1891+
SDL_strlcpy(clip_str, "NULL", sizeof(clip_str));
1892+
}
1893+
SDLTest_AssertPass("About to call SDL_EncloseFPoints(&%s, %d, %s)", points_str, (int)SDL_arraysize(points), clip_str);
1894+
result = SDL_EncloseFPoints(points, SDL_arraysize(points), clip_ptr, &enclosing);
1895+
SDLTest_AssertCheck(result == cases[i].result, "Got %d, expected %d", result, cases[i].result);
1896+
if (cases[i].result) {
1897+
SDLTest_AssertCheck(IsFRectEqual(&enclosing, &cases[i].enclosing),
1898+
"Got { %g, %g, %g, %g }, expected { %g, %g, %g, %g }",
1899+
enclosing.x, enclosing.y, enclosing.w, enclosing.h,
1900+
cases[i].enclosing.x, cases[i].enclosing.y, cases[i].enclosing.w, cases[i].enclosing.h);
1901+
}
1902+
}
1903+
return TEST_COMPLETED;
1904+
}
1905+
1906+
/* !
1907+
* \brief Test SDL_IntersectFRectAndLine
1908+
*
1909+
* \sa
1910+
* http://wiki.libsdl.org/SDL2/SDL_IntersectFRectAndLine
1911+
*/
1912+
int rect_testIntersectFRectAndLine(void *arg)
1913+
{
1914+
const struct {
1915+
SDL_FRect rect;
1916+
SDL_FPoint p1;
1917+
SDL_FPoint p2;
1918+
SDL_bool result;
1919+
SDL_FPoint expected1;
1920+
SDL_FPoint expected2;
1921+
} cases[] = {
1922+
{ { 0, 0, 0, 0 }, { -4.8f, -4.8f }, { 5.2f, 5.2f}, SDL_FALSE },
1923+
{ { 0, 0, 2, 2 }, { -1, -1 }, { 3.5f, 3.5f}, SDL_TRUE, { 0, 0 }, { 1, 1 } },
1924+
{ { -4, -4, 14, 14 }, { 8, 22 }, { 8, 33}, SDL_FALSE }
1925+
1926+
};
1927+
size_t i;
1928+
1929+
for (i = 0; i < SDL_arraysize(cases); i++) {
1930+
SDL_bool result;
1931+
SDL_FPoint p1 = cases[i].p1;
1932+
SDL_FPoint p2 = cases[i].p2;
1933+
1934+
SDLTest_AssertPass("About to call SDL_IntersectFRectAndLine(&{%g, %g, %g, %g}, &%g, &%g, &%g, &%g)",
1935+
cases[i].rect.x, cases[i].rect.y, cases[i].rect.w, cases[i].rect.h,
1936+
p1.x, p1.y, p2.x, p2.y);
1937+
result = SDL_IntersectFRectAndLine(&cases[i].rect, &p1.x, &p1.y, &p2.x, &p2.y);
1938+
SDLTest_AssertCheck(result == cases[i].result, "Got %d, expected %d", result, cases[i].result);
1939+
if (cases[i].result) {
1940+
SDLTest_AssertCheck(IsFPointEqual(&p1, &cases[i].p1) && IsFPointEqual(&p2, &cases[i].p2),
1941+
"Got p1={ %g, %g }, expected p1={ %g, %g }",
1942+
p1.x, p1.y,
1943+
cases[i].expected1.x, cases[i].expected1.y);
1944+
SDLTest_AssertCheck(IsFPointEqual(&p1, &cases[i].p1),
1945+
"Got p2={ %g, %g }, expected p2={ %g, %g }",
1946+
p2.x, p2.y,
1947+
cases[i].expected2.x, cases[i].expected2.y);
1948+
}
1949+
}
1950+
return TEST_COMPLETED;
1951+
}
1952+
17011953
/* ================= Test References ================== */
17021954

17031955
/* Rect test cases */
@@ -1836,6 +2088,26 @@ static const SDLTest_TestCaseReference rectTest31 = {
18362088
(SDLTest_TestCaseFp)rect_testFRectEqualsParam, "rect_testFRectEqualsParam", "Negative tests against SDL_FRectEquals with invalid parameters", TEST_ENABLED
18372089
};
18382090

2091+
static const SDLTest_TestCaseReference rectTest32 = {
2092+
(SDLTest_TestCaseFp)rect_testHasIntersectionF, "rect_testHasIntersectionF", "Tests SDL_HasIntersectionF", TEST_ENABLED
2093+
};
2094+
2095+
static const SDLTest_TestCaseReference rectTest33 = {
2096+
(SDLTest_TestCaseFp)rect_testIntersectFRect, "rect_IntersectFRect", "Tests SDL_IntersectFRect", TEST_ENABLED
2097+
};
2098+
2099+
static const SDLTest_TestCaseReference rectTest34 = {
2100+
(SDLTest_TestCaseFp)rect_testUnionFRect, "rect_testUnionFRect", "Tests SDL_UnionFRect", TEST_ENABLED
2101+
};
2102+
2103+
static const SDLTest_TestCaseReference rectTest35 = {
2104+
(SDLTest_TestCaseFp)rect_testUnionFRect, "rect_testEncloseFPointsUnionFRect", "Tests SDL_EncloseFPoints", TEST_ENABLED
2105+
};
2106+
2107+
static const SDLTest_TestCaseReference rectTest36 = {
2108+
(SDLTest_TestCaseFp)rect_testUnionFRect, "rect_testIntersectFRectAndLine", "Tests SDL_IntersectFRectAndLine", TEST_ENABLED
2109+
};
2110+
18392111
/* !
18402112
* \brief Sequence of Rect test cases; functions that handle simple rectangles including overlaps and merges.
18412113
*
@@ -1845,7 +2117,7 @@ static const SDLTest_TestCaseReference rectTest31 = {
18452117
static const SDLTest_TestCaseReference *rectTests[] = {
18462118
&rectTest1, &rectTest2, &rectTest3, &rectTest4, &rectTest5, &rectTest6, &rectTest7, &rectTest8, &rectTest9, &rectTest10, &rectTest11, &rectTest12, &rectTest13, &rectTest14,
18472119
&rectTest15, &rectTest16, &rectTest17, &rectTest18, &rectTest19, &rectTest20, &rectTest21, &rectTest22, &rectTest23, &rectTest24, &rectTest25, &rectTest26, &rectTest27,
1848-
&rectTest28, &rectTest29, &rectTest30, &rectTest31, NULL
2120+
&rectTest28, &rectTest29, &rectTest30, &rectTest31, &rectTest32, &rectTest33, &rectTest34, &rectTest35, &rectTest36, NULL
18492121
};
18502122

18512123
/* Rect test suite (global) */

0 commit comments

Comments
 (0)