Add compound operators * and / to wxPoint and wxRealPoint

Also add unit tests for them as well as for the existing additive
compound operators.
This commit is contained in:
David Miguel Susano Pinto 2024-01-04 02:08:41 +00:00 committed by Vadim Zeitlin
parent 1300c56f0d
commit 68bef2fbf3
3 changed files with 52 additions and 0 deletions

View file

@ -472,6 +472,8 @@ public:
wxRealPoint& operator/=(int i) { x *= i; y *= i; return *this; }
wxRealPoint& operator*=(int i) { x /= i; y /= i; return *this; }
wxRealPoint& operator/=(double f) { x /= f; y /= f; return *this; }
wxRealPoint& operator*=(double f) { x *= f; y *= f; return *this; }
};
@ -620,6 +622,8 @@ public:
wxPoint& operator/=(int i) { x /= i, y /= i; return *this; }
wxPoint& operator*=(int i) { x *= i, y *= i; return *this; }
wxPoint& operator/=(double f) { x = wxRound(x/f); y = wxRound(y/f); return *this; }
wxPoint& operator*=(double f) { x = wxRound(x*f); y = wxRound(y*f); return *this; }
// check if both components are set/initialized
bool IsFullySpecified() const { return x != wxDefaultCoord && y != wxDefaultCoord; }

View file

@ -238,6 +238,8 @@ public:
wxRealPoint operator /(const wxRealPoint& pt, double divisor);
wxRealPoint operator *(const wxRealPoint& pt, double factor);
wxRealPoint operator *(double factor, const wxRealPoint& pt);
wxRealPoint& operator /=(double divisor);
wxRealPoint& operator *=(double factor);
///@}
/**
@ -748,6 +750,8 @@ public:
wxPoint operator /(const wxPoint& pt, double divisor);
wxPoint operator *(const wxPoint& pt, double factor);
wxPoint operator *(double factor, const wxPoint& pt);
wxPoint& operator /=(double divisor);
wxPoint& operator *=(double factor);
///@}

View file

@ -51,6 +51,31 @@ TEST_CASE("wxPoint::Operators", "[point]")
p6 = p2; p6 -= s;
CHECK( p3 == p5 );
CHECK( p4 == p6 );
// Test arithmetic compound assignment operators with scalars
wxPoint p7(3, 5);
p7 /= 2; // chosen to check results truncate
CHECK( p7.x == 1 );
CHECK( p7.y == 2 );
p7 *= 2;
CHECK( p7.x == 2 );
CHECK( p7.y == 4 );
p7 *= 3.2; // chosen so that x and y rounds down and up respectively
CHECK( p7.x == 6 );
CHECK( p7.y == 13 );
p7 /= 1.1; // chosen so that x and y rounds up and down respectively
CHECK( p7.x == 5 );
CHECK( p7.y == 12 );
// Test arithmetic compound assignment operators with wxSizes
wxSize s1(2, 3);
p7 += s1;
CHECK( p7.x == 7 );
CHECK( p7.y == 15 );
wxSize s2(3, 4);
p7 -= s2;
CHECK( p7.x == 4 );
CHECK( p7.y == 11 );
}
TEST_CASE("wxRealPoint::Operators", "[point]")
@ -66,4 +91,23 @@ TEST_CASE("wxRealPoint::Operators", "[point]")
CHECK( p4.x == Approx(p6.x) );
CHECK( p4.y == Approx(p6.y) );
CHECK( p3.x != Approx(p4.x) );
// Test arithmetic compound assignment operators with scalars
wxRealPoint p7(3.0, 5.0);
p7 /= 2.0;
CHECK( p7.x == Approx(1.5) );
CHECK( p7.y == Approx(2.5) );
p7 *= 3.0;
CHECK( p7.x == Approx(4.5) );
CHECK( p7.y == Approx(7.5) );
// Test arithmetic compound assignment operators with wxSizes
wxSize s1(2, 3);
p7 += s1;
CHECK( p7.x == Approx(6.5) );
CHECK( p7.y == Approx(10.5) );
wxSize s2(3, 4);
p7 -= s2;
CHECK( p7.x == Approx(3.5) );
CHECK( p7.y == Approx(6.5) );
}