2004-12-12 14:23:19 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/geometry/rect.cpp
|
|
|
|
// Purpose: wxRect unit test
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2004-12-11
|
|
|
|
// RCS-ID: $Id$
|
2004-12-14 15:29:20 -05:00
|
|
|
// Copyright: (c) 2004 wxWindows
|
2004-12-12 14:23:19 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
2004-12-14 15:29:20 -05:00
|
|
|
#include "wx/gdicmn.h"
|
2004-12-12 14:23:19 -05:00
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
2006-02-09 19:01:19 -05:00
|
|
|
#include "wx/iosfwrap.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// helper functions
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxRects
|
|
|
|
wxSTD ostream& operator<<(wxSTD ostream& os, const wxRect& r)
|
|
|
|
{
|
|
|
|
os << "{"
|
|
|
|
<< r.x << ", " << r.y << ", " << r.width << ", " << r.height
|
|
|
|
<< "}";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2004-12-12 14:23:19 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class RectTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RectTestCase() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( RectTestCase );
|
2006-02-09 19:01:19 -05:00
|
|
|
CPPUNIT_TEST( CentreIn );
|
2005-02-04 06:04:43 -05:00
|
|
|
CPPUNIT_TEST( InflateDeflate );
|
2004-12-14 15:29:20 -05:00
|
|
|
CPPUNIT_TEST( Operators );
|
2004-12-12 14:23:19 -05:00
|
|
|
CPPUNIT_TEST( Union );
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2006-02-09 19:01:19 -05:00
|
|
|
void CentreIn();
|
2005-02-04 06:04:43 -05:00
|
|
|
void InflateDeflate();
|
2004-12-14 15:29:20 -05:00
|
|
|
void Operators();
|
2004-12-12 14:23:19 -05:00
|
|
|
void Union();
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(RectTestCase)
|
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( RectTestCase );
|
|
|
|
|
|
|
|
// also include in it's own registry so that these tests can be run alone
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RectTestCase, "RectTestCase" );
|
|
|
|
|
2006-02-09 19:01:19 -05:00
|
|
|
void RectTestCase::CentreIn()
|
|
|
|
{
|
|
|
|
typedef wxRect R;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( R(45, 45, 10, 10),
|
|
|
|
R(0, 0, 10, 10).CentreIn(R(0, 0, 100, 100)));
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( R(-5, -5, 20, 20),
|
|
|
|
R(0, 0, 20, 20).CentreIn(R(0, 0, 10, 10)));
|
|
|
|
}
|
|
|
|
|
2005-02-04 06:04:43 -05:00
|
|
|
void RectTestCase::InflateDeflate()
|
|
|
|
{
|
|
|
|
// This is the rectangle from the example in the documentation of wxRect::Inflate().
|
|
|
|
const wxRect r1(10, 10, 20, 40);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(r1.Inflate( 10, 10)==wxRect( 0, 0, 40, 60));
|
|
|
|
CPPUNIT_ASSERT(r1.Inflate( 20, 30)==wxRect(-10, -20, 60, 100));
|
|
|
|
CPPUNIT_ASSERT(r1.Inflate(-10, -10)==wxRect( 20, 20, 0, 20));
|
|
|
|
CPPUNIT_ASSERT(r1.Inflate(-15, -15)==wxRect( 20, 25, 0, 10));
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(r1.Inflate( 10, 10)==r1.Deflate(-10, -10));
|
|
|
|
CPPUNIT_ASSERT(r1.Inflate( 20, 30)==r1.Deflate(-20, -30));
|
|
|
|
CPPUNIT_ASSERT(r1.Inflate(-10, -10)==r1.Deflate( 10, 10));
|
|
|
|
CPPUNIT_ASSERT(r1.Inflate(-15, -15)==r1.Deflate( 15, 15));
|
|
|
|
}
|
|
|
|
|
2004-12-14 15:29:20 -05:00
|
|
|
void RectTestCase::Operators()
|
|
|
|
{
|
|
|
|
// test + operator which works like Union but does not ignore empty rectangles
|
|
|
|
static const struct RectData
|
|
|
|
{
|
|
|
|
int x1, y1, w1, h1;
|
|
|
|
int x2, y2, w2, h2;
|
|
|
|
int x, y, w, h;
|
|
|
|
|
|
|
|
wxRect GetFirst() const { return wxRect(x1, y1, w1, h1); }
|
|
|
|
wxRect GetSecond() const { return wxRect(x2, y2, w2, h2); }
|
|
|
|
wxRect GetResult() const { return wxRect(x, y, w, h); }
|
|
|
|
} s_rects[] =
|
|
|
|
{
|
|
|
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|
|
|
{ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 2, 2 },
|
|
|
|
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
|
|
|
|
{ 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 },
|
|
|
|
{ 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 },
|
|
|
|
{ 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 },
|
|
|
|
{ 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 }
|
|
|
|
};
|
|
|
|
|
|
|
|
for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ )
|
|
|
|
{
|
|
|
|
const RectData& data = s_rects[n];
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(
|
|
|
|
( data.GetFirst() + data.GetSecond() ) == data.GetResult()
|
|
|
|
);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(
|
|
|
|
( data.GetSecond() + data.GetFirst() ) == data.GetResult()
|
|
|
|
);
|
|
|
|
}
|
2007-05-23 21:47:29 -04:00
|
|
|
|
|
|
|
// test operator*() which returns the intersection of two rectangles
|
|
|
|
wxRect r1 = wxRect(0, 2, 3, 4);
|
|
|
|
wxRect r2 = wxRect(1, 2, 7, 8);
|
|
|
|
r1 *= r2;
|
|
|
|
CPPUNIT_ASSERT(wxRect(1, 2, 2, 4) == r1);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( (r1 * wxRect()).IsEmpty() );
|
2004-12-14 15:29:20 -05:00
|
|
|
}
|
|
|
|
|
2004-12-12 14:23:19 -05:00
|
|
|
void RectTestCase::Union()
|
|
|
|
{
|
|
|
|
static const struct RectData
|
|
|
|
{
|
|
|
|
int x1, y1, w1, h1;
|
|
|
|
int x2, y2, w2, h2;
|
|
|
|
int x, y, w, h;
|
|
|
|
|
|
|
|
wxRect GetFirst() const { return wxRect(x1, y1, w1, h1); }
|
|
|
|
wxRect GetSecond() const { return wxRect(x2, y2, w2, h2); }
|
|
|
|
wxRect GetResult() const { return wxRect(x, y, w, h); }
|
|
|
|
} s_rects[] =
|
|
|
|
{
|
|
|
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|
|
|
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
|
|
|
|
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
|
|
|
|
{ 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 4, 4 },
|
|
|
|
{ 1, 1, 2, 2, 4, 4, 1, 1, 1, 1, 4, 4 },
|
|
|
|
{ 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 6, 6 },
|
2004-12-14 15:29:20 -05:00
|
|
|
{ 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4 }
|
2004-12-12 14:23:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
for ( size_t n = 0; n < WXSIZEOF(s_rects); n++ )
|
|
|
|
{
|
|
|
|
const RectData& data = s_rects[n];
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(
|
|
|
|
data.GetFirst().Union(data.GetSecond()) == data.GetResult()
|
|
|
|
);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(
|
|
|
|
data.GetSecond().Union(data.GetFirst()) == data.GetResult()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|