]>
git.saurik.com Git - wxWidgets.git/blob - tests/geometry/rect.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/geometry/rect.cpp
3 // Purpose: wxRect unit test
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2004 wxWindows
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
20 #include "wx/gdicmn.h"
23 #include "wx/iosfwrap.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 // this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRects
30 std::ostream
& operator<<(std::ostream
& os
, const wxRect
& r
)
33 << r
.x
<< ", " << r
.y
<< ", " << r
.width
<< ", " << r
.height
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class RectTestCase
: public CppUnit::TestCase
48 CPPUNIT_TEST_SUITE( RectTestCase
);
49 CPPUNIT_TEST( CentreIn
);
50 CPPUNIT_TEST( InflateDeflate
);
51 CPPUNIT_TEST( Operators
);
52 CPPUNIT_TEST( Union
);
53 CPPUNIT_TEST_SUITE_END();
56 void InflateDeflate();
60 DECLARE_NO_COPY_CLASS(RectTestCase
)
63 // register in the unnamed registry so that these tests are run by default
64 CPPUNIT_TEST_SUITE_REGISTRATION( RectTestCase
);
66 // also include in its own registry so that these tests can be run alone
67 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RectTestCase
, "RectTestCase" );
69 void RectTestCase::CentreIn()
73 CPPUNIT_ASSERT_EQUAL( R(45, 45, 10, 10),
74 R(0, 0, 10, 10).CentreIn(R(0, 0, 100, 100)));
76 CPPUNIT_ASSERT_EQUAL( R(-5, -5, 20, 20),
77 R(0, 0, 20, 20).CentreIn(R(0, 0, 10, 10)));
80 void RectTestCase::InflateDeflate()
82 // This is the rectangle from the example in the documentation of wxRect::Inflate().
83 const wxRect
r1(10, 10, 20, 40);
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));
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));
96 void RectTestCase::Operators()
98 // test + operator which works like Union but does not ignore empty rectangles
99 static const struct RectData
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
); }
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 }
119 for ( size_t n
= 0; n
< WXSIZEOF(s_rects
); n
++ )
121 const RectData
& data
= s_rects
[n
];
124 ( data
.GetFirst() + data
.GetSecond() ) == data
.GetResult()
128 ( data
.GetSecond() + data
.GetFirst() ) == data
.GetResult()
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);
136 CPPUNIT_ASSERT(wxRect(1, 2, 2, 4) == r1
);
138 CPPUNIT_ASSERT( (r1
* wxRect()).IsEmpty() );
141 void RectTestCase::Union()
143 static const struct RectData
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
); }
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 },
160 { 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 }
163 for ( size_t n
= 0; n
< WXSIZEOF(s_rects
); n
++ )
165 const RectData
& data
= s_rects
[n
];
168 data
.GetFirst().Union(data
.GetSecond()) == data
.GetResult()
172 data
.GetSecond().Union(data
.GetFirst()) == data
.GetResult()