]> git.saurik.com Git - wxWidgets.git/commitdiff
Make it easier to compare wxImages in the unit tests.
authorVadim Zeitlin <vadim@wxwidgets.org>
Tue, 26 Apr 2011 22:57:16 +0000 (22:57 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Tue, 26 Apr 2011 22:57:16 +0000 (22:57 +0000)
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
tests/testimage.h [new file with mode: 0644]

index 98487b0f856c8271a1afdf7cb8ecc634cf4cad9e..6b98ad4f3ce9a73ccecb7846ad62830b0d839942 100644 (file)
@@ -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; i<WXSIZEOF(g_testfiles); i++)
     {
         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),
-
-            memcmp(actual.GetData(),
-                (g_testfiles[i].bitDepth == 8)
-                    ? expected8.GetData()
-                    : expected24.GetData(),
-                dataLen) == 0
+            g_testfiles[i].bitDepth == 8 ? expected8 : expected24,
+            actual
         );
     }
 
@@ -947,13 +940,12 @@ void CompareImage(const wxImageHandler& handler, const wxImage& image,
     CPPUNIT_ASSERT( actual.GetSize() == expected->GetSize() );
 
     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 (file)
index 0000000..4c84b5c
--- /dev/null
@@ -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_