]>
git.saurik.com Git - wxWidgets.git/blob - tests/geometry/rect.cpp
c90197e9f440da53a4e670586598a3ca0da719b1
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/geometry/rect.cpp
3 // Purpose: wxRect unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2004 wxWindows
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
21 #include "wx/gdicmn.h"
24 #include "wx/iosfwrap.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRects
31 std::ostream
& operator<<(std::ostream
& os
, const wxRect
& r
)
34 << r
.x
<< ", " << r
.y
<< ", " << r
.width
<< ", " << r
.height
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 class RectTestCase
: public CppUnit::TestCase
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();
57 void InflateDeflate();
61 DECLARE_NO_COPY_CLASS(RectTestCase
)
64 // register in the unnamed registry so that these tests are run by default
65 CPPUNIT_TEST_SUITE_REGISTRATION( RectTestCase
);
67 // also include in its own registry so that these tests can be run alone
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RectTestCase
, "RectTestCase" );
70 void RectTestCase::CentreIn()
74 CPPUNIT_ASSERT_EQUAL( R(45, 45, 10, 10),
75 R(0, 0, 10, 10).CentreIn(R(0, 0, 100, 100)));
77 CPPUNIT_ASSERT_EQUAL( R(-5, -5, 20, 20),
78 R(0, 0, 20, 20).CentreIn(R(0, 0, 10, 10)));
81 void RectTestCase::InflateDeflate()
83 // This is the rectangle from the example in the documentation of wxRect::Inflate().
84 const wxRect
r1(10, 10, 20, 40);
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));
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));
97 void RectTestCase::Operators()
99 // test + operator which works like Union but does not ignore empty rectangles
100 static const struct RectData
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
); }
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 }
120 for ( size_t n
= 0; n
< WXSIZEOF(s_rects
); n
++ )
122 const RectData
& data
= s_rects
[n
];
125 ( data
.GetFirst() + data
.GetSecond() ) == data
.GetResult()
129 ( data
.GetSecond() + data
.GetFirst() ) == data
.GetResult()
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);
137 CPPUNIT_ASSERT(wxRect(1, 2, 2, 4) == r1
);
139 CPPUNIT_ASSERT( (r1
* wxRect()).IsEmpty() );
142 void RectTestCase::Union()
144 static const struct RectData
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
); }
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 }
164 for ( size_t n
= 0; n
< WXSIZEOF(s_rects
); n
++ )
166 const RectData
& data
= s_rects
[n
];
169 data
.GetFirst().Union(data
.GetSecond()) == data
.GetResult()
173 data
.GetSecond().Union(data
.GetFirst()) == data
.GetResult()