Source

pinyomi / pixel / testComparePng.py

Full commit
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# testComparePng.py
# Lars Yencken <lars.yencken@gmail.com>
# vim: ts=4 sw=4 sts=4 et tw=78:
# $Id: testComparePng.py 233 2007-02-01 04:47:04Z lars $
#
#----------------------------------------------------------------------------# 

import os
import unittest
from comparePng import *

#----------------------------------------------------------------------------#

def suite():
    testSuite = unittest.TestSuite((
            unittest.makeSuite(ComparePngTestCase)
        ))
    return testSuite

#----------------------------------------------------------------------------#

class ComparePngTestCase(unittest.TestCase):
    """ This class tests the ComparePng class. 
    """
    #------------------------------------------------------------------------#

    def setUp(self):
        thisDir = os.path.abspath(os.path.dirname(__file__))
        self.blank = os.path.join(thisDir, 'testData', 'blank.png')
        self.glyph = os.path.join(thisDir, 'testData', 'glyph.png')
        self.translated = os.path.join(thisDir, 'testData',
                'glyph-translated.png')
        self.defigured = os.path.join(thisDir, 'testData',
            'glyph-defigured.png')

        return

    #------------------------------------------------------------------------#

    def testSimpleIdentical(self):
        """ Tests basic loading and comparison operations with a white image.
        """
        blankPng = PngImage(self.blank)
        glyphPng = PngImage(self.glyph)

        self.assertAlmostEqual(blankPng.norm(blankPng, 1.0), 0.0)
        self.assertAlmostEqual(glyphPng.norm(glyphPng, 1.0), 0.0)
        return
    
    #------------------------------------------------------------------------#

    def testSimpleDifference(self):
        """ Tests where actual difference arises between two images.
        """
        blankPng = PngImage(self.blank)
        glyphPng = PngImage(self.glyph)

        diffVal = blankPng.norm(glyphPng, 1.0)
        assert 0 < diffVal <= 1, "Out of range difference %f" % diffVal

        return

    #------------------------------------------------------------------------#

#----------------------------------------------------------------------------#

if __name__ == "__main__":
    unittest.TextTestRunner(verbosity=2).run(suite())

#----------------------------------------------------------------------------#

# vim: ts=4 sw=4 sts=4 et tw=78: