# This file comprises the tests that are internally validated (as
# opposed to tests which produce output files that are externally
# validated).  Primarily they are unittests.

# There is a read/write asymmetry: It is fairly easy to
# internally validate the results of reading a PNG file because we
# can know what pixels it should produce, but when writing a PNG
# file many choices are possible. The only thing we can do is read
# it back in again, which merely checks consistency, not that the
# PNG file we produce is valid.

# Run the tests from the command line:
#   python -c 'import test_png;test_png.runTest()'
# If you have nose installed you can use that:
#   nosetests .

import itertools
try:
    import struct
except ImportError:
    import ustruct as struct
import sys
# http://www.python.org/doc/2.4.4/lib/module-unittest.html
import unittest
try:
    import zlib
except ImportError:
    import uzlib as zlib

from array import array
try:
    from io import BytesIO
except ImportError:
    from uio import BytesIO

try:
    import numpy
except ImportError:
    numpy = False

import png
import pngsuite


def runTest():
    unittest.main(__name__)

#def topngbytes(name, rows, x, y, **k):
#    """
#    Convenience function for creating a PNG file "in memory" as
#    a string.  Creates a :class:`Writer` instance using the keyword
#    arguments, then passes `rows` to its :meth:`Writer.write` method.
#    The resulting PNG file is returned as a string.  `name` is used
#    to identify the file for debugging.
#    """
#
#    import os
#
#    if os.environ.get('PYPNG_TEST_FILENAME'):
#        print(name, file=sys.stderr)
#    f = BytesIO()
#    w = png.Writer(x, y, **k)
#    w.write(f, rows)
#    if os.environ.get('PYPNG_TEST_TMP'):
#        w = open(name, 'wb')
#        w.write(f.getvalue())
#        w.close()
#    return f.getvalue()

def _redirect_io(inp, out, f):
    """Calls the function `f` with ``sys.stdin`` changed to `inp`
    and ``sys.stdout`` changed to `out`.  They are restored when `f`
    returns.  This function returns whatever `f` returns.
    """

    import os
    import sys

    oldin, sys.stdin = sys.stdin, inp
    oldout, sys.stdout = sys.stdout, out
    try:
        x = f()
    finally:
        sys.stdin = oldin
        sys.stdout = oldout
    if os.environ.get('PYPNG_TEST_TMP') and hasattr(out,'getvalue'):
        name = mycallersname()
        if name:
            w = open(name+'.png', 'wb')
            w.write(out.getvalue())
            w.close()
    return x

def mycallersname():
    """Returns the name of the caller of the caller of this function
    (hence the name of the caller of the function in which
    "mycallersname()" textually appears).  Returns None if this cannot
    be determined.
    """

    # http://docs.python.org/library/inspect.html#the-interpreter-stack
    import inspect

    frame = inspect.currentframe()
    if not frame:
        return None
    frame_,filename_,lineno_,funname,linelist_,listi_ = (
      inspect.getouterframes(frame)[2])
    return funname