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_SUITE_END();
74 wxDECLARE_NO_COPY_CLASS(RegionTestCase
);
77 // register in the unnamed registry so that these tests are run by default
78 CPPUNIT_TEST_SUITE_REGISTRATION( RegionTestCase
);
80 // also include in its own registry so that these tests can be run alone
81 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegionTestCase
, "RegionTestCase" );
83 void RegionTestCase::Validity()
87 CPPUNIT_ASSERT_MESSAGE
89 "Default constructed region must be invalid",
93 CPPUNIT_ASSERT_MESSAGE
95 "Invalid region should be empty",
99 // Offsetting an invalid region doesn't make sense.
100 WX_ASSERT_FAILS_WITH_ASSERT( r
.Offset(1, 1) );
102 CPPUNIT_ASSERT_MESSAGE
104 "Combining with a valid region should create a valid region",
105 r
.Union(0, 0, 10, 10)
108 CPPUNIT_ASSERT_EQUAL_MESSAGE
110 "Union() with invalid region should give the same region",
111 wxRegion(0, 0, 10, 10),