From a25b5bbfc92c4824db1d6e2c8369ab41531c2363 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 26 Apr 2011 22:57:16 +0000 Subject: [PATCH] Make it easier to compare wxImages in the unit tests. Instead of forcing the tests to manually use memcmp(), specialize CppUnit::assertion_traits<> for wxImage. This allows to simply use CPPUNIT_ASSERT_EQUAL() and related macros with wxImage objects. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67616 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- tests/image/image.cpp | 41 ++++++++++++++++------------------------- tests/testimage.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 tests/testimage.h diff --git a/tests/image/image.cpp b/tests/image/image.cpp index 98487b0f85..6b98ad4f3c 100644 --- a/tests/image/image.cpp +++ b/tests/image/image.cpp @@ -24,7 +24,6 @@ #endif // WX_PRECOMP #include "wx/anidecod.h" // wxImageArray -#include "wx/image.h" #include "wx/palette.h" #include "wx/url.h" #include "wx/log.h" @@ -32,6 +31,8 @@ #include "wx/zstream.h" #include "wx/wfstream.h" +#include "testimage.h" + struct testData { const char* file; wxBitmapType type; @@ -832,12 +833,10 @@ void ImageTestCase::SizeImage() CPPUNIT_ASSERT_EQUAL( actual.GetSize().x, expected.GetSize().x ); CPPUNIT_ASSERT_EQUAL( actual.GetSize().y, expected.GetSize().y ); - const unsigned data_len = 3 * expected.GetHeight() * expected.GetWidth(); - - WX_ASSERT_MESSAGE + WX_ASSERT_EQUAL_MESSAGE ( ("Resize test #%u: (%d, %d), (%d, %d)", i, st.w, st.h, st.dx, st.dy), - memcmp(actual.GetData(), expected.GetData(), data_len) == 0 + expected, actual ); } } @@ -850,8 +849,6 @@ void ImageTestCase::CompareLoadedImage() wxImage expected24("horse.png"); CPPUNIT_ASSERT( expected24.IsOk() ); - const size_t dataLen = expected8.GetWidth() * expected8.GetHeight() * 3; - for (size_t i=0; iGetSize() ); unsigned bitsPerPixel = testPalette ? 8 : (testAlpha ? 32 : 24); - WX_ASSERT_MESSAGE + WX_ASSERT_EQUAL_MESSAGE ( ("Compare test '%s (%d-bit)' for saving failed", handler.GetExtension(), bitsPerPixel), - - memcmp(actual.GetData(), expected->GetData(), - expected->GetWidth() * expected->GetHeight() * 3) == 0 + *expected, + actual ); #if wxUSE_PALETTE @@ -968,12 +960,11 @@ void CompareImage(const wxImageHandler& handler, const wxImage& image, return; } - WX_ASSERT_MESSAGE + WX_ASSERT_EQUAL_MESSAGE ( ("Compare alpha test '%s' for saving failed", handler.GetExtension()), - - memcmp(actual.GetAlpha(), expected->GetAlpha(), - expected->GetWidth() * expected->GetHeight()) == 0 + *expected, + actual ); } @@ -1133,11 +1124,11 @@ void ImageTestCase::SaveAnimatedGIF() CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true, i) ); memIn.SeekI(pos); - WX_ASSERT_MESSAGE + WX_ASSERT_EQUAL_MESSAGE ( ("Compare test for GIF frame number %d failed", i), - memcmp(image.GetData(), images[i].GetData(), - images[i].GetWidth() * images[i].GetHeight() * 3) == 0 + images[i], + image ); } #endif // #if wxUSE_PALETTE diff --git a/tests/testimage.h b/tests/testimage.h new file mode 100644 index 0000000000..4c84b5cf51 --- /dev/null +++ b/tests/testimage.h @@ -0,0 +1,43 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/testimage.h +// Purpose: Unit test helpers for dealing with wxImage. +// Author: Vadim Zeitlin +// RCS-ID: $Id$ +// Copyright: (c) 2011 Vadim Zeitlin +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_TESTS_TESTIMAGE_H_ +#define _WX_TESTS_TESTIMAGE_H_ + +#include "wx/image.h" + +CPPUNIT_NS_BEGIN + +template <> +struct assertion_traits +{ + static bool equal(const wxImage& i1, const wxImage& i2) + { + if ( i1.GetWidth() != i2.GetWidth() ) + return false; + + if ( i1.GetHeight() != i2.GetHeight() ) + return false; + + return memcmp(i1.GetData(), i2.GetData(), + i1.GetWidth()*i1.GetHeight()*3) == 0; + } + + static std::string toString(const wxImage& image) + { + return wxString::Format("image of size %d*%d", + image.GetWidth(), + image.GetHeight()) + .ToStdString(); + } +}; + +CPPUNIT_NS_END + +#endif // _WX_TESTS_TESTIMAGE_H_ -- 2.45.2