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
This commit is contained in:
Vadim Zeitlin
2011-04-26 22:57:16 +00:00
parent 051b7b264f
commit a25b5bbfc9
2 changed files with 59 additions and 25 deletions

View File

@@ -24,7 +24,6 @@
#endif // WX_PRECOMP #endif // WX_PRECOMP
#include "wx/anidecod.h" // wxImageArray #include "wx/anidecod.h" // wxImageArray
#include "wx/image.h"
#include "wx/palette.h" #include "wx/palette.h"
#include "wx/url.h" #include "wx/url.h"
#include "wx/log.h" #include "wx/log.h"
@@ -32,6 +31,8 @@
#include "wx/zstream.h" #include "wx/zstream.h"
#include "wx/wfstream.h" #include "wx/wfstream.h"
#include "testimage.h"
struct testData { struct testData {
const char* file; const char* file;
wxBitmapType type; wxBitmapType type;
@@ -832,12 +833,10 @@ void ImageTestCase::SizeImage()
CPPUNIT_ASSERT_EQUAL( actual.GetSize().x, expected.GetSize().x ); CPPUNIT_ASSERT_EQUAL( actual.GetSize().x, expected.GetSize().x );
CPPUNIT_ASSERT_EQUAL( actual.GetSize().y, expected.GetSize().y ); CPPUNIT_ASSERT_EQUAL( actual.GetSize().y, expected.GetSize().y );
const unsigned data_len = 3 * expected.GetHeight() * expected.GetWidth(); WX_ASSERT_EQUAL_MESSAGE
WX_ASSERT_MESSAGE
( (
("Resize test #%u: (%d, %d), (%d, %d)", i, st.w, st.h, st.dx, st.dy), ("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"); wxImage expected24("horse.png");
CPPUNIT_ASSERT( expected24.IsOk() ); CPPUNIT_ASSERT( expected24.IsOk() );
const size_t dataLen = expected8.GetWidth() * expected8.GetHeight() * 3;
for (size_t i=0; i<WXSIZEOF(g_testfiles); i++) for (size_t i=0; i<WXSIZEOF(g_testfiles); i++)
{ {
if ( !(g_testfiles[i].bitDepth == 8 || g_testfiles[i].bitDepth == 24) if ( !(g_testfiles[i].bitDepth == 8 || g_testfiles[i].bitDepth == 24)
@@ -868,15 +865,11 @@ void ImageTestCase::CompareLoadedImage()
} }
WX_ASSERT_MESSAGE WX_ASSERT_EQUAL_MESSAGE
( (
("Compare test '%s' for loading failed", g_testfiles[i].file), ("Compare test '%s' for loading failed", g_testfiles[i].file),
g_testfiles[i].bitDepth == 8 ? expected8 : expected24,
memcmp(actual.GetData(), actual
(g_testfiles[i].bitDepth == 8)
? expected8.GetData()
: expected24.GetData(),
dataLen) == 0
); );
} }
@@ -947,13 +940,12 @@ void CompareImage(const wxImageHandler& handler, const wxImage& image,
CPPUNIT_ASSERT( actual.GetSize() == expected->GetSize() ); CPPUNIT_ASSERT( actual.GetSize() == expected->GetSize() );
unsigned bitsPerPixel = testPalette ? 8 : (testAlpha ? 32 : 24); unsigned bitsPerPixel = testPalette ? 8 : (testAlpha ? 32 : 24);
WX_ASSERT_MESSAGE WX_ASSERT_EQUAL_MESSAGE
( (
("Compare test '%s (%d-bit)' for saving failed", ("Compare test '%s (%d-bit)' for saving failed",
handler.GetExtension(), bitsPerPixel), handler.GetExtension(), bitsPerPixel),
*expected,
memcmp(actual.GetData(), expected->GetData(), actual
expected->GetWidth() * expected->GetHeight() * 3) == 0
); );
#if wxUSE_PALETTE #if wxUSE_PALETTE
@@ -968,12 +960,11 @@ void CompareImage(const wxImageHandler& handler, const wxImage& image,
return; return;
} }
WX_ASSERT_MESSAGE WX_ASSERT_EQUAL_MESSAGE
( (
("Compare alpha test '%s' for saving failed", handler.GetExtension()), ("Compare alpha test '%s' for saving failed", handler.GetExtension()),
*expected,
memcmp(actual.GetAlpha(), expected->GetAlpha(), actual
expected->GetWidth() * expected->GetHeight()) == 0
); );
} }
@@ -1133,11 +1124,11 @@ void ImageTestCase::SaveAnimatedGIF()
CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true, i) ); CPPUNIT_ASSERT( handler.LoadFile(&image, memIn, true, i) );
memIn.SeekI(pos); memIn.SeekI(pos);
WX_ASSERT_MESSAGE WX_ASSERT_EQUAL_MESSAGE
( (
("Compare test for GIF frame number %d failed", i), ("Compare test for GIF frame number %d failed", i),
memcmp(image.GetData(), images[i].GetData(), images[i],
images[i].GetWidth() * images[i].GetHeight() * 3) == 0 image
); );
} }
#endif // #if wxUSE_PALETTE #endif // #if wxUSE_PALETTE

43
tests/testimage.h Normal file
View File

@@ -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 <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_TESTS_TESTIMAGE_H_
#define _WX_TESTS_TESTIMAGE_H_
#include "wx/image.h"
CPPUNIT_NS_BEGIN
template <>
struct assertion_traits<wxImage>
{
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_