From 7c6f29099500c3b975ebac9729011488d0b574fa Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 11 Jul 2021 18:44:29 +0100 Subject: [PATCH] Document wxMulDivInt32() and add a test for it This function exists since always and is probably already used outside wx, so make it officially public and add at least a trivial unit test for it. --- interface/wx/math.h | 10 ++++++++++ tests/misc/misctests.cpp | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/interface/wx/math.h b/interface/wx/math.h index 5e0b6e3d2c..6043de64c9 100644 --- a/interface/wx/math.h +++ b/interface/wx/math.h @@ -115,5 +115,15 @@ bool wxIsSameDouble(double x, double y); */ bool wxIsNullDouble(double x); +/** + Computes the product of a number with a fraction with rounding. + + This function returns @c n*numerator/denominator rounding the result. It is + similar to the standard Win32 @c MulDiv() function and, in fact, is + implemented by calling it under MSW, where @c wx/msw/wrapwin.h must be + included in addition to @c wx/math.h for it to be used. + */ +int wxMulDivInt32(int n, int numerator, int denominator); + //@} diff --git a/tests/misc/misctests.cpp b/tests/misc/misctests.cpp index 328c502f31..7ec98633b8 100644 --- a/tests/misc/misctests.cpp +++ b/tests/misc/misctests.cpp @@ -22,6 +22,11 @@ #include "wx/tarstrm.h" #include "wx/zipstrm.h" +#ifdef __WINDOWS__ + // Needed for wxMulDivInt32(). + #include "wx/msw/wrapwin.h" +#endif + // ---------------------------------------------------------------------------- // test class // ---------------------------------------------------------------------------- @@ -199,3 +204,12 @@ TEST_CASE("wxRound", "[math]") #endif #endif // WXWIN_COMPATIBILITY_3_0 } + +TEST_CASE("wxMulDivInt32", "[math]") +{ + // Check that it rounds correctly. + CHECK( wxMulDivInt32(15, 3, 2) == 23 ); + + // Check that it doesn't overflow. + CHECK( wxMulDivInt32((INT_MAX - 1)/2, 200, 100) == INT_MAX - 1 ); +}