]> git.saurik.com Git - wxWidgets.git/blob - tests/geometry/rect.cpp
Import numpy.oldnumeric
[wxWidgets.git] / tests / geometry / rect.cpp
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 wxSTD ostream& operator<<(wxSTD 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
134 void RectTestCase::Union()
135 {
136 static const struct RectData
137 {
138 int x1, y1, w1, h1;
139 int x2, y2, w2, h2;
140 int x, y, w, h;
141
142 wxRect GetFirst() const { return wxRect(x1, y1, w1, h1); }
143 wxRect GetSecond() const { return wxRect(x2, y2, w2, h2); }
144 wxRect GetResult() const { return wxRect(x, y, w, h); }
145 } s_rects[] =
146 {
147 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
148 { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
149 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
150 { 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 },
151 { 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 },
152 { 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 },
153 { 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 }
154 };
155
156 for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ )
157 {
158 const RectData& data = s_rects[n];
159
160 CPPUNIT_ASSERT(
161 data.GetFirst().Union(data.GetSecond()) == data.GetResult()
162 );
163
164 CPPUNIT_ASSERT(
165 data.GetSecond().Union(data.GetFirst()) == data.GetResult()
166 );
167 }
168 }