1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/geometry/region.cpp
3 // Purpose: wxRegion unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
21 #include "wx/region.h"
24 #include "wx/iosfwrap.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
33 // This function could be easily added to wxRegionIterator itself, where it
34 // could be implemented far more efficiently as all major platforms store the
35 // number of rectangles anyhow, but as we only use it for debugging purposes,
36 // just keep it here for now.
37 unsigned GetRectsCount(const wxRegion
& rgn
)
40 for ( wxRegionIterator
iter(rgn
); iter
.HaveRects(); ++iter
)
45 } // anonymous namespace
47 // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRegions
48 std::ostream
& operator<<(std::ostream
& os
, const wxRegion
& rgn
)
50 wxRect r
= rgn
.GetBox();
51 os
<< "# rects = " << GetRectsCount(rgn
)
53 << r
.x
<< ", " << r
.y
<< ", " << r
.width
<< ", " << r
.height
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class RegionTestCase
: public CppUnit::TestCase
68 CPPUNIT_TEST_SUITE( RegionTestCase
);
69 CPPUNIT_TEST( Validity
);
70 CPPUNIT_TEST( Intersect
);
71 CPPUNIT_TEST_SUITE_END();
76 wxDECLARE_NO_COPY_CLASS(RegionTestCase
);
79 // register in the unnamed registry so that these tests are run by default
80 CPPUNIT_TEST_SUITE_REGISTRATION( RegionTestCase
);
82 // also include in its own registry so that these tests can be run alone
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegionTestCase
, "RegionTestCase" );
85 void RegionTestCase::Validity()
89 CPPUNIT_ASSERT_MESSAGE
91 "Default constructed region must be invalid",
95 CPPUNIT_ASSERT_MESSAGE
97 "Invalid region should be empty",
101 // Offsetting an invalid region doesn't make sense.
102 WX_ASSERT_FAILS_WITH_ASSERT( r
.Offset(1, 1) );
104 CPPUNIT_ASSERT_MESSAGE
106 "Combining with a valid region should create a valid region",
107 r
.Union(0, 0, 10, 10)
110 CPPUNIT_ASSERT_EQUAL_MESSAGE
112 "Union() with invalid region should give the same region",
113 wxRegion(0, 0, 10, 10),
118 void RegionTestCase::Intersect()
120 const wxPoint points1
[] = {
127 wxRegion
region1(WXSIZEOF(points1
), points1
);
129 const wxPoint points2
[] = {
136 wxRegion
region2(4,points2
);
138 CPPUNIT_ASSERT( region1
.Intersect(region2
) );
139 CPPUNIT_ASSERT( region1
.IsEmpty() );