]> git.saurik.com Git - wxWidgets.git/blob - tests/geometry/region.cpp
Add a test for eol-native file existence in the release script.
[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( Intersect );
71 CPPUNIT_TEST_SUITE_END();
72
73 void Validity();
74 void Intersect();
75
76 wxDECLARE_NO_COPY_CLASS(RegionTestCase);
77 };
78
79 // register in the unnamed registry so that these tests are run by default
80 CPPUNIT_TEST_SUITE_REGISTRATION( RegionTestCase );
81
82 // also include in its own registry so that these tests can be run alone
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegionTestCase, "RegionTestCase" );
84
85 void RegionTestCase::Validity()
86 {
87 wxRegion r;
88
89 CPPUNIT_ASSERT_MESSAGE
90 (
91 "Default constructed region must be invalid",
92 !r.IsOk()
93 );
94
95 CPPUNIT_ASSERT_MESSAGE
96 (
97 "Invalid region should be empty",
98 r.IsEmpty()
99 );
100
101 // Offsetting an invalid region doesn't make sense.
102 WX_ASSERT_FAILS_WITH_ASSERT( r.Offset(1, 1) );
103
104 CPPUNIT_ASSERT_MESSAGE
105 (
106 "Combining with a valid region should create a valid region",
107 r.Union(0, 0, 10, 10)
108 );
109
110 CPPUNIT_ASSERT_EQUAL_MESSAGE
111 (
112 "Union() with invalid region should give the same region",
113 wxRegion(0, 0, 10, 10),
114 r
115 );
116 }
117
118 void RegionTestCase::Intersect()
119 {
120 const wxPoint points1[] = {
121 wxPoint(310, 392),
122 wxPoint(270, 392),
123 wxPoint(270, 421),
124 wxPoint(310, 421)
125 };
126
127 wxRegion region1(WXSIZEOF(points1), points1);
128
129 const wxPoint points2[] = {
130 wxPoint(54, 104),
131 wxPoint(85, 82),
132 wxPoint(68, 58),
133 wxPoint(37, 80)
134 };
135
136 wxRegion region2(4,points2);
137
138 CPPUNIT_ASSERT( region1.Intersect(region2) );
139 CPPUNIT_ASSERT( region1.IsEmpty() );
140 }