1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/geometry/region.cpp
3 // Purpose: wxRegion unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
20 #include "wx/region.h"
23 #include "wx/iosfwrap.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
32 // This function could be easily added to wxRegionIterator itself, where it
33 // could be implemented far more efficiently as all major platforms store the
34 // number of rectangles anyhow, but as we only use it for debugging purposes,
35 // just keep it here for now.
36 unsigned GetRectsCount(const wxRegion
& rgn
)
39 for ( wxRegionIterator
iter(rgn
); iter
.HaveRects(); ++iter
)
44 } // anonymous namespace
46 // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRegions
47 std::ostream
& operator<<(std::ostream
& os
, const wxRegion
& rgn
)
49 wxRect r
= rgn
.GetBox();
50 os
<< "# rects = " << GetRectsCount(rgn
)
52 << r
.x
<< ", " << r
.y
<< ", " << r
.width
<< ", " << r
.height
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 class RegionTestCase
: public CppUnit::TestCase
67 CPPUNIT_TEST_SUITE( RegionTestCase
);
68 CPPUNIT_TEST( Validity
);
69 CPPUNIT_TEST( Intersect
);
70 CPPUNIT_TEST_SUITE_END();
75 wxDECLARE_NO_COPY_CLASS(RegionTestCase
);
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( RegionTestCase
);
81 // also include in its own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegionTestCase
, "RegionTestCase" );
84 void RegionTestCase::Validity()
88 CPPUNIT_ASSERT_MESSAGE
90 "Default constructed region must be invalid",
94 CPPUNIT_ASSERT_MESSAGE
96 "Invalid region should be empty",
100 // Offsetting an invalid region doesn't make sense.
101 WX_ASSERT_FAILS_WITH_ASSERT( r
.Offset(1, 1) );
103 CPPUNIT_ASSERT_MESSAGE
105 "Combining with a valid region should create a valid region",
106 r
.Union(0, 0, 10, 10)
109 CPPUNIT_ASSERT_EQUAL_MESSAGE
111 "Union() with invalid region should give the same region",
112 wxRegion(0, 0, 10, 10),
117 void RegionTestCase::Intersect()
119 const wxPoint points1
[] = {
126 wxRegion
region1(WXSIZEOF(points1
), points1
);
128 const wxPoint points2
[] = {
135 wxRegion
region2(4,points2
);
137 CPPUNIT_ASSERT( region1
.Intersect(region2
) );
138 CPPUNIT_ASSERT( region1
.IsEmpty() );