Merge branch 'size-div-double'

Add operator/(wxSize, double) and cleanup some wxSize and related tests.

See https://github.com/wxWidgets/wxWidgets/pull/2593
This commit is contained in:
Vadim Zeitlin 2021-11-25 15:16:53 +01:00
commit 7ba71ecaa2
4 changed files with 54 additions and 112 deletions

View File

@ -435,6 +435,11 @@ inline wxSize operator*(unsigned long i, const wxSize& s)
return wxSize(int(s.x * i), int(s.y * i));
}
inline wxSize operator/(const wxSize& s, double i)
{
return wxSize(wxRound(s.x / i), wxRound(s.y / i));
}
inline wxSize operator*(const wxSize& s, double i)
{
return wxSize(wxRound(s.x * i), wxRound(s.y * i));

View File

@ -1064,12 +1064,19 @@ public:
/**
@name Miscellaneous operators
Sizes can be added to or subtracted from each other or divided or
multiplied by a number.
Note that these operators are documented as class members
(to make them easier to find) but, as their prototype shows,
they are implemented as global operators; note that this is
transparent to the user but it helps to understand why the
following functions are documented to take the wxSize they
operate on as an explicit argument.
Also note that using @c double factor may result in rounding errors,
as wxSize always stores @c int coordinates and the result is always
rounded.
*/
//@{
wxSize& operator=(const wxSize& sz);
@ -1083,10 +1090,15 @@ public:
wxSize& operator -=(const wxSize& sz);
wxSize operator /(const wxSize& sz, int factor);
wxSize operator /(const wxSize& sz, double factor);
wxSize operator *(const wxSize& sz, int factor);
wxSize operator *(const wxSize& sz, double factor);
wxSize operator *(int factor, const wxSize& sz);
wxSize operator *(double factor, const wxSize& sz);
wxSize& operator /=(int factor);
wxSize& operator /=(double factor);
wxSize& operator *=(int factor);
wxSize& operator *=(double factor);
//@}
};

View File

@ -19,49 +19,7 @@
#include "wx/math.h"
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class PointTestCase : public CppUnit::TestCase
{
public:
PointTestCase() { }
private:
CPPUNIT_TEST_SUITE( PointTestCase );
CPPUNIT_TEST( Operators );
CPPUNIT_TEST_SUITE_END();
void Operators();
wxDECLARE_NO_COPY_CLASS(PointTestCase);
};
class RealPointTestCase : public CppUnit::TestCase
{
public:
RealPointTestCase() { }
private:
CPPUNIT_TEST_SUITE( RealPointTestCase );
CPPUNIT_TEST( Operators );
CPPUNIT_TEST_SUITE_END();
void Operators();
wxDECLARE_NO_COPY_CLASS(RealPointTestCase);
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( PointTestCase );
CPPUNIT_TEST_SUITE_REGISTRATION( RealPointTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PointTestCase, "PointTestCase" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RealPointTestCase, "RealPointTestCase" );
void PointTestCase::Operators()
TEST_CASE("wxPoint::Operators", "[point]")
{
wxPoint p1(1,2);
wxPoint p2(6,3);
@ -69,48 +27,43 @@ void PointTestCase::Operators()
wxPoint p4(5,1);
wxPoint p5 = p2 + p1;
wxPoint p6 = p2 - p1;
CPPUNIT_ASSERT( p3.x == p5.x );
CPPUNIT_ASSERT( p3.y == p5.y );
CPPUNIT_ASSERT( p4.x == p6.x );
CPPUNIT_ASSERT( p4.y == p6.y );
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
CPPUNIT_ASSERT( p3 != p4 );
CHECK( p3.x == p5.x );
CHECK( p3.y == p5.y );
CHECK( p4.x == p6.x );
CHECK( p4.y == p6.y );
CHECK( p3 == p5 );
CHECK( p4 == p6 );
CHECK( p3 != p4 );
p5 = p2; p5 += p1;
p6 = p2; p6 -= p1;
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
CHECK( p3 == p5 );
CHECK( p4 == p6 );
wxSize s(p1.x,p1.y);
p5 = p2; p5 = p2 + s;
p6 = p2; p6 = p2 - s;
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
CHECK( p3 == p5 );
CHECK( p4 == p6 );
p5 = p2; p5 = s + p2;
p6 = p2; p6 = s - p2;
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == -p6 );
CHECK( p3 == p5 );
CHECK( p4 == -p6 );
p5 = p2; p5 += s;
p6 = p2; p6 -= s;
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
CHECK( p3 == p5 );
CHECK( p4 == p6 );
}
void RealPointTestCase::Operators()
TEST_CASE("wxRealPoint::Operators", "[point]")
{
const double EPSILON = 0.00001;
wxRealPoint p1(1.2,3.4);
wxRealPoint p2(8.7,5.4);
wxRealPoint p3(9.9,8.8);
wxRealPoint p4(7.5,2.0);
wxRealPoint p5 = p2 + p1;
wxRealPoint p6 = p2 - p1;
/*
CPPUNIT_ASSERT( p3 == p5 );
CPPUNIT_ASSERT( p4 == p6 );
CPPUNIT_ASSERT( p3 != p4 );
*/
CPPUNIT_ASSERT( fabs( p3.x - p5.x ) < EPSILON );
CPPUNIT_ASSERT( fabs( p3.y - p5.y ) < EPSILON );
CPPUNIT_ASSERT( fabs( p4.x - p6.x ) < EPSILON );
CPPUNIT_ASSERT( fabs( p4.y - p6.y ) < EPSILON );
CHECK( p3.x == Approx(p5.x) );
CHECK( p3.y == Approx(p5.y) );
CHECK( p4.x == Approx(p6.x) );
CHECK( p4.y == Approx(p6.y) );
CHECK( p3.x != Approx(p4.x) );
}

View File

@ -17,65 +17,37 @@
#include "wx/gdicmn.h"
#endif // WX_PRECOMP
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
#include "asserthelper.h"
class SizeTestCase : public CppUnit::TestCase
{
public:
SizeTestCase() { }
private:
CPPUNIT_TEST_SUITE( SizeTestCase );
CPPUNIT_TEST( Operators );
CPPUNIT_TEST_SUITE_END();
void Operators();
wxDECLARE_NO_COPY_CLASS(SizeTestCase);
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( SizeTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SizeTestCase, "SizeTestCase" );
void SizeTestCase::Operators()
TEST_CASE("wxSize::Operators", "[size]")
{
wxSize s1(1,2);
wxSize s2(3,4);
wxSize s3;
s3 = s1 + s2;
CPPUNIT_ASSERT( s3.GetWidth()==4 );
CPPUNIT_ASSERT( s3.GetHeight()==6 );
CHECK( s3 == wxSize(4, 6) );
s3 = s2 - s1;
CPPUNIT_ASSERT( s3.GetWidth()==2 );
CPPUNIT_ASSERT( s3.GetHeight()==2 );
CHECK( s3 == wxSize(2, 2) );
s3 = s1 * 2;
CPPUNIT_ASSERT( s3.GetWidth()==2 );
CPPUNIT_ASSERT( s3.GetHeight()==4 );
CHECK( s3 == wxSize(2, 4) );
s3 = 2 * s1;
CPPUNIT_ASSERT( s3.GetWidth()==2 );
CPPUNIT_ASSERT( s3.GetHeight()==4 );
CHECK( s3 == wxSize(2, 4) );
s3 = s3 / 2;
CPPUNIT_ASSERT( s3.GetWidth()==1 );
CPPUNIT_ASSERT( s3.GetHeight()==2 );
CHECK( s3 == wxSize(1, 2) );
s3 = s2;
CPPUNIT_ASSERT( s3 != s1 );
CHECK( s3 != s1 );
s3 = s1;
CPPUNIT_ASSERT( s3 == s1 );
CHECK( s3 == s1 );
s3 += s2;
CPPUNIT_ASSERT( s3.GetWidth()==4 );
CPPUNIT_ASSERT( s3.GetHeight()==6 );
CHECK( s3 == wxSize(4, 6) );
s3 -= s2;
CPPUNIT_ASSERT( s3 == s1 );
CHECK( s3 == s1 );
s3 *= 2;
CPPUNIT_ASSERT( s3.GetWidth()==2 );
CPPUNIT_ASSERT( s3.GetHeight()==4 );
CHECK( s3 == wxSize(2, 4) );
s3 /= 2;
CPPUNIT_ASSERT( s3 == s1 );
CHECK( s3 == s1 );
CHECK( wxSize(6, 9) / 1.5 == wxSize(4, 6) );
}