]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tests/geometry/rect.cpp | |
3 | // Purpose: wxRect unit test | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2004-12-11 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 wxWindows | |
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/gdicmn.h" | |
22 | #endif // WX_PRECOMP | |
23 | ||
24 | #include "wx/iosfwrap.h" | |
25 | ||
26 | // ---------------------------------------------------------------------------- | |
27 | // helper functions | |
28 | // ---------------------------------------------------------------------------- | |
29 | ||
30 | // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRects | |
31 | std::ostream& operator<<(std::ostream& os, const wxRect& r) | |
32 | { | |
33 | os << "{" | |
34 | << r.x << ", " << r.y << ", " << r.width << ", " << r.height | |
35 | << "}"; | |
36 | return os; | |
37 | } | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // test class | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | class RectTestCase : public CppUnit::TestCase | |
44 | { | |
45 | public: | |
46 | RectTestCase() { } | |
47 | ||
48 | private: | |
49 | CPPUNIT_TEST_SUITE( RectTestCase ); | |
50 | CPPUNIT_TEST( CentreIn ); | |
51 | CPPUNIT_TEST( InflateDeflate ); | |
52 | CPPUNIT_TEST( Operators ); | |
53 | CPPUNIT_TEST( Union ); | |
54 | CPPUNIT_TEST_SUITE_END(); | |
55 | ||
56 | void CentreIn(); | |
57 | void InflateDeflate(); | |
58 | void Operators(); | |
59 | void Union(); | |
60 | ||
61 | DECLARE_NO_COPY_CLASS(RectTestCase) | |
62 | }; | |
63 | ||
64 | // register in the unnamed registry so that these tests are run by default | |
65 | CPPUNIT_TEST_SUITE_REGISTRATION( RectTestCase ); | |
66 | ||
67 | // also include in it's own registry so that these tests can be run alone | |
68 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RectTestCase, "RectTestCase" ); | |
69 | ||
70 | void RectTestCase::CentreIn() | |
71 | { | |
72 | typedef wxRect R; | |
73 | ||
74 | CPPUNIT_ASSERT_EQUAL( R(45, 45, 10, 10), | |
75 | R(0, 0, 10, 10).CentreIn(R(0, 0, 100, 100))); | |
76 | ||
77 | CPPUNIT_ASSERT_EQUAL( R(-5, -5, 20, 20), | |
78 | R(0, 0, 20, 20).CentreIn(R(0, 0, 10, 10))); | |
79 | } | |
80 | ||
81 | void RectTestCase::InflateDeflate() | |
82 | { | |
83 | // This is the rectangle from the example in the documentation of wxRect::Inflate(). | |
84 | const wxRect r1(10, 10, 20, 40); | |
85 | ||
86 | CPPUNIT_ASSERT(r1.Inflate( 10, 10)==wxRect( 0, 0, 40, 60)); | |
87 | CPPUNIT_ASSERT(r1.Inflate( 20, 30)==wxRect(-10, -20, 60, 100)); | |
88 | CPPUNIT_ASSERT(r1.Inflate(-10, -10)==wxRect( 20, 20, 0, 20)); | |
89 | CPPUNIT_ASSERT(r1.Inflate(-15, -15)==wxRect( 20, 25, 0, 10)); | |
90 | ||
91 | CPPUNIT_ASSERT(r1.Inflate( 10, 10)==r1.Deflate(-10, -10)); | |
92 | CPPUNIT_ASSERT(r1.Inflate( 20, 30)==r1.Deflate(-20, -30)); | |
93 | CPPUNIT_ASSERT(r1.Inflate(-10, -10)==r1.Deflate( 10, 10)); | |
94 | CPPUNIT_ASSERT(r1.Inflate(-15, -15)==r1.Deflate( 15, 15)); | |
95 | } | |
96 | ||
97 | void RectTestCase::Operators() | |
98 | { | |
99 | // test + operator which works like Union but does not ignore empty rectangles | |
100 | static const struct RectData | |
101 | { | |
102 | int x1, y1, w1, h1; | |
103 | int x2, y2, w2, h2; | |
104 | int x, y, w, h; | |
105 | ||
106 | wxRect GetFirst() const { return wxRect(x1, y1, w1, h1); } | |
107 | wxRect GetSecond() const { return wxRect(x2, y2, w2, h2); } | |
108 | wxRect GetResult() const { return wxRect(x, y, w, h); } | |
109 | } s_rects[] = | |
110 | { | |
111 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | |
112 | { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 2, 2 }, | |
113 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, | |
114 | { 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 }, | |
115 | { 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 }, | |
116 | { 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 }, | |
117 | { 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 } | |
118 | }; | |
119 | ||
120 | for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ ) | |
121 | { | |
122 | const RectData& data = s_rects[n]; | |
123 | ||
124 | CPPUNIT_ASSERT( | |
125 | ( data.GetFirst() + data.GetSecond() ) == data.GetResult() | |
126 | ); | |
127 | ||
128 | CPPUNIT_ASSERT( | |
129 | ( data.GetSecond() + data.GetFirst() ) == data.GetResult() | |
130 | ); | |
131 | } | |
132 | ||
133 | // test operator*() which returns the intersection of two rectangles | |
134 | wxRect r1 = wxRect(0, 2, 3, 4); | |
135 | wxRect r2 = wxRect(1, 2, 7, 8); | |
136 | r1 *= r2; | |
137 | CPPUNIT_ASSERT(wxRect(1, 2, 2, 4) == r1); | |
138 | ||
139 | CPPUNIT_ASSERT( (r1 * wxRect()).IsEmpty() ); | |
140 | } | |
141 | ||
142 | void RectTestCase::Union() | |
143 | { | |
144 | static const struct RectData | |
145 | { | |
146 | int x1, y1, w1, h1; | |
147 | int x2, y2, w2, h2; | |
148 | int x, y, w, h; | |
149 | ||
150 | wxRect GetFirst() const { return wxRect(x1, y1, w1, h1); } | |
151 | wxRect GetSecond() const { return wxRect(x2, y2, w2, h2); } | |
152 | wxRect GetResult() const { return wxRect(x, y, w, h); } | |
153 | } s_rects[] = | |
154 | { | |
155 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | |
156 | { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 }, | |
157 | { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, | |
158 | { 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 }, | |
159 | { 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 }, | |
160 | { 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 }, | |
161 | { 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 } | |
162 | }; | |
163 | ||
164 | for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ ) | |
165 | { | |
166 | const RectData& data = s_rects[n]; | |
167 | ||
168 | CPPUNIT_ASSERT( | |
169 | data.GetFirst().Union(data.GetSecond()) == data.GetResult() | |
170 | ); | |
171 | ||
172 | CPPUNIT_ASSERT( | |
173 | data.GetSecond().Union(data.GetFirst()) == data.GetResult() | |
174 | ); | |
175 | } | |
176 | } |