From 10a727bf1dd26bda54a4f7b2b81dcff990aa46eb Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 12 Apr 2015 22:56:24 +0200 Subject: [PATCH] Compare scaled images approximately in the unit tests. Comparing them exactly results in tests failures when using compiler version different from the one that was used to generate the test fails, see http://thread.gmane.org/gmane.comp.lib.wxwidgets.devel/151149/focus=151154 and other messages in the same thread. --- tests/image/image.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/image/image.cpp b/tests/image/image.cpp index 94d4cefefe..3f775b8cd2 100644 --- a/tests/image/image.cpp +++ b/tests/image/image.cpp @@ -1340,6 +1340,39 @@ void ImageTestCase::BMPFlippingAndRLECompression() } +static bool +CompareApprox(const wxImage& i1, const wxImage& i2) +{ + if ( i1.GetWidth() != i2.GetWidth() ) + return false; + + if ( i1.GetHeight() != i2.GetHeight() ) + return false; + + const unsigned char* p1 = i1.GetData(); + const unsigned char* p2 = i2.GetData(); + const int numBytes = i1.GetWidth()*i1.GetHeight()*3; + for ( int n = 0; n < numBytes; n++, p1++, p2++ ) + { + switch ( *p1 - *p2 ) + { + case -1: + case 0: + case +1: + // Accept up to one pixel difference, this happens because of + // different rounding behaviours in different compiler versions + // even under the same architecture, see the example in + // http://thread.gmane.org/gmane.comp.lib.wxwidgets.devel/151149/focus=151154 + break; + + default: + return false; + } + } + + return true; +} + // The 0 below can be replaced with 1 to generate, instead of comparing with, // the test files. #define ASSERT_IMAGE_EQUAL_TO_FILE(image, file) \ @@ -1351,7 +1384,11 @@ void ImageTestCase::BMPFlippingAndRLECompression() { \ wxImage imageFromFile(file); \ CPPUNIT_ASSERT_MESSAGE( "Failed to load " file, imageFromFile.IsOk() ); \ - CPPUNIT_ASSERT_EQUAL( imageFromFile, image ); \ + CPPUNIT_ASSERT_MESSAGE \ + ( \ + "Wrong scaled " + CppUnit::assertion_traits::toString(image), \ + CompareApprox(imageFromFile, image) \ + ); \ } void ImageTestCase::ScaleCompare()