Consistently handle DST start time in wxDateTime::Set().
[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 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/region.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/iosfwrap.h"
24
25 // ----------------------------------------------------------------------------
26 // helper functions
27 // ----------------------------------------------------------------------------
28
29 namespace
30 {
31
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)
37 {
38 unsigned count = 0;
39 for ( wxRegionIterator iter(rgn); iter.HaveRects(); ++iter )
40 count++;
41 return count;
42 }
43
44 } // anonymous namespace
45
46 // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRegions
47 std::ostream& operator<<(std::ostream& os, const wxRegion& rgn)
48 {
49 wxRect r = rgn.GetBox();
50 os << "# rects = " << GetRectsCount(rgn)
51 << "; bounding box {"
52 << r.x << ", " << r.y << ", " << r.width << ", " << r.height
53 << "}";
54 return os;
55 }
56
57 // ----------------------------------------------------------------------------
58 // test class
59 // ----------------------------------------------------------------------------
60
61 class RegionTestCase : public CppUnit::TestCase
62 {
63 public:
64 RegionTestCase() { }
65
66 private:
67 CPPUNIT_TEST_SUITE( RegionTestCase );
68 CPPUNIT_TEST( Validity );
69 CPPUNIT_TEST( Intersect );
70 CPPUNIT_TEST_SUITE_END();
71
72 void Validity();
73 void Intersect();
74
75 wxDECLARE_NO_COPY_CLASS(RegionTestCase);
76 };
77
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( RegionTestCase );
80
81 // also include in its own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RegionTestCase, "RegionTestCase" );
83
84 void RegionTestCase::Validity()
85 {
86 wxRegion r;
87
88 CPPUNIT_ASSERT_MESSAGE
89 (
90 "Default constructed region must be invalid",
91 !r.IsOk()
92 );
93
94 CPPUNIT_ASSERT_MESSAGE
95 (
96 "Invalid region should be empty",
97 r.IsEmpty()
98 );
99
100 // Offsetting an invalid region doesn't make sense.
101 WX_ASSERT_FAILS_WITH_ASSERT( r.Offset(1, 1) );
102
103 CPPUNIT_ASSERT_MESSAGE
104 (
105 "Combining with a valid region should create a valid region",
106 r.Union(0, 0, 10, 10)
107 );
108
109 CPPUNIT_ASSERT_EQUAL_MESSAGE
110 (
111 "Union() with invalid region should give the same region",
112 wxRegion(0, 0, 10, 10),
113 r
114 );
115 }
116
117 void RegionTestCase::Intersect()
118 {
119 const wxPoint points1[] = {
120 wxPoint(310, 392),
121 wxPoint(270, 392),
122 wxPoint(270, 421),
123 wxPoint(310, 421)
124 };
125
126 wxRegion region1(WXSIZEOF(points1), points1);
127
128 const wxPoint points2[] = {
129 wxPoint(54, 104),
130 wxPoint(85, 82),
131 wxPoint(68, 58),
132 wxPoint(37, 80)
133 };
134
135 wxRegion region2(4,points2);
136
137 CPPUNIT_ASSERT( region1.Intersect(region2) );
138 CPPUNIT_ASSERT( region1.IsEmpty() );
139 }