]> git.saurik.com Git - wxWidgets.git/blob - tests/geometry/rect.cpp
disable the buttons while a find dialog is active
[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 // ----------------------------------------------------------------------------
25 // test class
26 // ----------------------------------------------------------------------------
27
28 class RectTestCase : public CppUnit::TestCase
29 {
30 public:
31 RectTestCase() { }
32
33 private:
34 CPPUNIT_TEST_SUITE( RectTestCase );
35 CPPUNIT_TEST( InflateDeflate );
36 CPPUNIT_TEST( Operators );
37 CPPUNIT_TEST( Union );
38 CPPUNIT_TEST_SUITE_END();
39
40 void InflateDeflate();
41 void Operators();
42 void Union();
43
44 DECLARE_NO_COPY_CLASS(RectTestCase)
45 };
46
47 // register in the unnamed registry so that these tests are run by default
48 CPPUNIT_TEST_SUITE_REGISTRATION( RectTestCase );
49
50 // also include in it's own registry so that these tests can be run alone
51 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RectTestCase, "RectTestCase" );
52
53 void RectTestCase::InflateDeflate()
54 {
55 // This is the rectangle from the example in the documentation of wxRect::Inflate().
56 const wxRect r1(10, 10, 20, 40);
57
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));
62
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));
67 }
68
69 void RectTestCase::Operators()
70 {
71 // test + operator which works like Union but does not ignore empty rectangles
72 static const struct RectData
73 {
74 int x1, y1, w1, h1;
75 int x2, y2, w2, h2;
76 int x, y, w, h;
77
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); }
81 } s_rects[] =
82 {
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 }
90 };
91
92 for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ )
93 {
94 const RectData& data = s_rects[n];
95
96 CPPUNIT_ASSERT(
97 ( data.GetFirst() + data.GetSecond() ) == data.GetResult()
98 );
99
100 CPPUNIT_ASSERT(
101 ( data.GetSecond() + data.GetFirst() ) == data.GetResult()
102 );
103 }
104 }
105
106 void RectTestCase::Union()
107 {
108 static const struct RectData
109 {
110 int x1, y1, w1, h1;
111 int x2, y2, w2, h2;
112 int x, y, w, h;
113
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); }
117 } s_rects[] =
118 {
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 }
126 };
127
128 for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ )
129 {
130 const RectData& data = s_rects[n];
131
132 CPPUNIT_ASSERT(
133 data.GetFirst().Union(data.GetSecond()) == data.GetResult()
134 );
135
136 CPPUNIT_ASSERT(
137 data.GetSecond().Union(data.GetFirst()) == data.GetResult()
138 );
139 }
140 }