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