#!/usr/bin/env python
#ckwg +5
# Copyright 2011 by Kitware, Inc. All Rights Reserved. Please refer to
# KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
# Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.


def log(msg):
    import sys
    sys.stderr.write("%s\n" % msg)


def ensure_exception(action, func, *args):
    got_exception = False

    try:
        func(*args)
    except:
        got_exception = True

    if not got_exception:
        log("Error: Did not get exception when %s" % action)


def test_import():
    try:
        import vistk.image.vil
    except:
        log("Error: Failed to import the vil module")


def test_create():
    from vistk.image import vil

    try:
        config.empty_config()
    except:
        log("Error: Failed to create an empty configuration")

    config.ConfigKey()
    config.ConfigKeys()
    config.ConfigValue()


def test_vil_to_numpy():
    from vistk.image import vil
    from vistk.test import test_image
    import numpy as np

    width = 800
    height = 600
    planes = 3

    shape = (width, height, planes)

    types = [ (test_image.make_image_bool, np.bool)
            , (test_image.make_image_uint8_t, np.uint8)
            , (test_image.make_image_float, np.float32)
            , (test_image.make_image_double, np.double)
            ]

    for f, t in types:
        i = f(width, height, planes)

        if not i.dtype == t:
            log("Error: Wrong type returned: got: '%s' expected: '%s'" % (i.dtype, t))

        if not i.ndim == 3:
            log("Error: Did not get a 3-dimensional array: got: '%d' expected: '%d'" % (i.dim, 3))

        if not i.shape == shape:
            log("Error: Did not get expected array sizes: got '%s' expected: '%s'" % (str(i.shape), str(shape)))


def test_numpy_to_vil():
    from vistk.image import vil
    import numpy

    log("Error: Not implemented")


def test_datum():
    from vistk.image import vil
    import numpy

    log("Error: Not implemented")


def main(testname):
    if testname == 'import':
        test_import()
    elif testname == 'vil_to_numpy':
        test_vil_to_numpy()
    elif testname == 'numpy_to_vil':
        test_numpy_to_vil()
    elif testname == 'datum':
        test_datum()
    else:
        log("Error: No such test '%s'" % testname)


if __name__ == '__main__':
    import os
    import sys

    if not len(sys.argv) == 4:
        log("Error: Expected three arguments")
        sys.exit(1)

    testname = sys.argv[1]

    os.chdir(sys.argv[2])

    sys.path.append(sys.argv[3])

    try:
        main(testname)
    except BaseException as e:
        log("Error: Unexpected exception: %s" % str(e))

@@ -1857,54 +1857,615 @@
     
-import numpy%0A%0A    log(%22Error: Not implemented%22
+from vistk.test import test_image%0A    import numpy as np%0A%0A    width = 800%0A    height = 600%0A    planes = 3%0A%0A    shape = (width, height, planes)%0A%0A    size = width * height * planes%0A%0A    types = %5B (test_image.take_image_bool, np.bool)%0A            , (test_image.take_image_uint8_t, np.uint8)%0A            , (test_image.take_image_float, np.float32)%0A            , (test_image.take_image_double, np.double)%0A            %5D%0A%0A    for f, t in types:%0A        i = np.zeros(shape, dtype=t)%0A%0A        sz = f(i)%0A%0A        if not sz == size:%0A            log(%22Error: Wrong size calculated: got: '%25d' expected: '%25d'%22 %25 (sz, size)
 )%0A%0A%0A

Implement NumPy -> vil test