]>
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
7 // Copyright: (c) 2004 wxWindows
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
21 #include "wx/gdicmn.h"
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 class RectTestCase
: public CppUnit::TestCase
34 CPPUNIT_TEST_SUITE( RectTestCase
);
35 CPPUNIT_TEST( InflateDeflate
);
36 CPPUNIT_TEST( Operators
);
37 CPPUNIT_TEST( Union
);
38 CPPUNIT_TEST_SUITE_END();
40 void InflateDeflate();
44 DECLARE_NO_COPY_CLASS(RectTestCase
)
47 // register in the unnamed registry so that these tests are run by default
48 CPPUNIT_TEST_SUITE_REGISTRATION( RectTestCase
);
50 // also include in it's own registry so that these tests can be run alone
51 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RectTestCase
, "RectTestCase" );
53 void RectTestCase::InflateDeflate()
55 // This is the rectangle from the example in the documentation of wxRect::Inflate().
56 const wxRect
r1(10, 10, 20, 40);
58 CPPUNIT_ASSERT(r1
.Inflate( 10, 10)==wxRect( 0, 0, 40, 60));
59 CPPUNIT_ASSERT(r1
.Inflate( 20, 30)==wxRect(-10, -20, 60, 100));
60 CPPUNIT_ASSERT(r1
.Inflate(-10, -10)==wxRect( 20, 20, 0, 20));
61 CPPUNIT_ASSERT(r1
.Inflate(-15, -15)==wxRect( 20, 25, 0, 10));
63 CPPUNIT_ASSERT(r1
.Inflate( 10, 10)==r1
.Deflate(-10, -10));
64 CPPUNIT_ASSERT(r1
.Inflate( 20, 30)==r1
.Deflate(-20, -30));
65 CPPUNIT_ASSERT(r1
.Inflate(-10, -10)==r1
.Deflate( 10, 10));
66 CPPUNIT_ASSERT(r1
.Inflate(-15, -15)==r1
.Deflate( 15, 15));
69 void RectTestCase::Operators()
71 // test + operator which works like Union but does not ignore empty rectangles
72 static const struct RectData
78 wxRect
GetFirst() const { return wxRect(x1
, y1
, w1
, h1
); }
79 wxRect
GetSecond() const { return wxRect(x2
, y2
, w2
, h2
); }
80 wxRect
GetResult() const { return wxRect(x
, y
, w
, h
); }
83 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
84 { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 2, 2 },
85 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
86 { 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 },
87 { 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 },
88 { 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 },
89 { 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 }
92 for ( size_t n
= 0; n
< WXSIZEOF(s_rects
); n
++ )
94 const RectData
& data
= s_rects
[n
];
97 ( data
.GetFirst() + data
.GetSecond() ) == data
.GetResult()
101 ( data
.GetSecond() + data
.GetFirst() ) == data
.GetResult()
106 void RectTestCase::Union()
108 static const struct RectData
114 wxRect
GetFirst() const { return wxRect(x1
, y1
, w1
, h1
); }
115 wxRect
GetSecond() const { return wxRect(x2
, y2
, w2
, h2
); }
116 wxRect
GetResult() const { return wxRect(x
, y
, w
, h
); }
119 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
120 { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
121 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
122 { 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 },
123 { 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 },
124 { 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 },
125 { 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 }
128 for ( size_t n
= 0; n
< WXSIZEOF(s_rects
); n
++ )
130 const RectData
& data
= s_rects
[n
];
133 data
.GetFirst().Union(data
.GetSecond()) == data
.GetResult()
137 data
.GetSecond().Union(data
.GetFirst()) == data
.GetResult()