]> git.saurik.com Git - wxWidgets.git/blob - tests/geometry/region.cpp
avoiding potential mismatch between true and expected state of wxGraphicsContext...
[wxWidgets.git] / tests / geometry / region.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/geometry/region.cpp
3 // Purpose: wxRegion unit test
4 // Author: Vadim Zeitlin
5 // Created: 2011-10-12
6 // RCS-ID: $Id$
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/region.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/iosfwrap.h"
25
26 // ----------------------------------------------------------------------------
27 // helper functions
28 // ----------------------------------------------------------------------------
29
30 namespace
31 {
32
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)
38 {
39 unsigned count = 0;
40 for ( wxRegionIterator iter(rgn); iter.HaveRects(); ++iter )
41 count++;
42 return count;
43 }
44
45 } // anonymous namespace
46
47 // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRegions
48 std::ostream& operator<<(std::ostream& os, const wxRegion& rgn)
49 {
50 wxRect r = rgn.GetBox();
51 os << "# rects = " << GetRectsCount(rgn)
52 << "; bounding box {"
53 << r.x << ", " << r.y << ", " << r.width << ", " << r.height
54 << "}";
55 return os;
56 }
57
58 // ----------------------------------------------------------------------------
59 // test class
60 // ----------------------------------------------------------------------------
61
62 class RegionTestCase : public CppUnit::TestCase
63 {
64 public:
65 RegionTestCase() { }
66
67 private:
68 CPPUNIT_TEST_SUITE( RegionTestCase );
69 CPPUNIT_TEST( Validity );
70 CPPUNIT_TEST_SUITE_END();
71
72 void Validity();
73
74 wxDECLARE_NO_COPY_CLASS(RegionTestCase);
75 };
76
77 // register in the unnamed registry so that these tests are run by default
78 CPPUNIT_TEST_SUITE_REGISTRATION( RegionTestCase );
79
80 // also include in its own registry so that these tests can be run alone
81 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegionTestCase, "RegionTestCase" );
82
83 void RegionTestCase::Validity()
84 {
85 wxRegion r;
86
87 CPPUNIT_ASSERT_MESSAGE
88 (
89 "Default constructed region must be invalid",
90 !r.IsOk()
91 );
92
93 CPPUNIT_ASSERT_MESSAGE
94 (
95 "Invalid region should be empty",
96 r.IsEmpty()
97 );
98
99 // Offsetting an invalid region doesn't make sense.
100 WX_ASSERT_FAILS_WITH_ASSERT( r.Offset(1, 1) );
101
102 CPPUNIT_ASSERT_MESSAGE
103 (
104 "Combining with a valid region should create a valid region",
105 r.Union(0, 0, 10, 10)
106 );
107
108 CPPUNIT_ASSERT_EQUAL_MESSAGE
109 (
110 "Union() with invalid region should give the same region",
111 wxRegion(0, 0, 10, 10),
112 r
113 );
114 }