#!/usr/bin/env python 

import OpenImageIO as oiio


# Test that every expected enum value of BASETYPE exists
def basetype_enum_test():
    try:
        oiio.UNKNOWN
        oiio.NONE
        oiio.UCHAR
        oiio.UINT8
        oiio.CHAR
        oiio.INT8
        oiio.USHORT
        oiio.UINT16
        oiio.SHORT
        oiio.INT16
        oiio.UINT
        oiio.UINT32
        oiio.INT
        oiio.INT32
        oiio.ULONGLONG
        oiio.UINT64
        oiio.LONGLONG
        oiio.INT64
        oiio.HALF
        oiio.FLOAT
        oiio.DOUBLE
        oiio.STRING
        oiio.PTR
        oiio.LASTBASE
        print "Passed BASETYPE"
    except:
        print "Failed BASETYPE"


# Test that every expected enum value of AGGREGATE exists
def aggregate_enum_test():
    try:
        oiio.NOSEMANTICS
        oiio.SCALAR
        oiio.VEC2
        oiio.VEC3
        oiio.VEC4
        oiio.MATRIX44
        oiio.TIMECODE
        oiio.KEYCODE
        print "Passed AGGREGATE"
    except:
        print "Failed AGGREGATE"


# Test that every expected enum value of VECSEMANTICS exists
def vecsemantics_enum_test():
    try:
        oiio.NOXFORM
        oiio.COLOR
        oiio.POINT
        oiio.VECTOR
        oiio.NORMAL
        print "Passed VECSEMANTICS"
    except:
        print "Failed VECSEMANTICS"

# print the details of a type t
def breakdown_test(t, name="", verbose=True):
    print "type '%s'" % name
    print "    c_str \"" + t.c_str() + "\""
    if verbose:
        print "    basetype", t.basetype
        print "    aggregate", t.aggregate
        print "    vecsemantics", t.vecsemantics
        print "    arraylen", t.arraylen
        print "    str(t) = \"" + str(t) + "\""
        print "    size =", t.size()
        print "    elementtype =", t.elementtype()
        print "    numelements =", t.numelements()
        print "    elementsize =", t.elementsize()
        print "    basesize =", t.basesize()


######################################################################
# main test starts here

try:
    # Test that all the enum values exist
    basetype_enum_test()
    aggregate_enum_test()
    vecsemantics_enum_test()
    print

    # Exercise the different constructors, make sure they create the
    # correct TypeDesc (also exercises the individual fields, c_str(),
    # conversion to string).
    breakdown_test (oiio.TypeDesc(), "(default)")
    breakdown_test (oiio.TypeDesc(oiio.UINT8), "UINT8")
    breakdown_test (oiio.TypeDesc(oiio.HALF, oiio.VEC3, oiio.COLOR),
                    "HALF, VEC3, COLOR")
    breakdown_test (oiio.TypeDesc(oiio.FLOAT, oiio.SCALAR, oiio.NOXFORM, 6),
                    "FLOAT, SCALAR, NOXFORM, array of 6")
    breakdown_test (oiio.TypeDesc(oiio.FLOAT, oiio.VEC3, oiio.POINT, 2),
                    "FLOAT, VEC3, POINT, array of 2")
    print

    # Test construction from a string descriptor
    breakdown_test (oiio.TypeDesc("float[2]"), "float[2]")
    breakdown_test (oiio.TypeDesc("normal"), "normal")
    breakdown_test (oiio.TypeDesc("uint16"), "uint16")
    print

    # Test equality, inequality, and equivalent
    t_uint8 = oiio.TypeDesc("uint8")
    t_uint16 = oiio.TypeDesc("uint16")
    t_uint8_b = oiio.TypeDesc("uint8")
    print "uint8 == uint8?", (t_uint8 == t_uint8)
    print "uint8 == uint8?", (t_uint8 == t_uint8_b)
    print "uint8 == uint16", (t_uint8 == t_uint16)
    print "uint8 != uint8?", (t_uint8 != t_uint8)
    print "uint8 != uint8?", (t_uint8 != t_uint8_b)
    print "uint8 != uint16", (t_uint8 != t_uint16)
    print "vector == color", (oiio.TypeDesc("vector") == oiio.TypeDesc("color"))
    print "vector.equivalent(color)", oiio.TypeDesc("vector").equivalent(oiio.TypeDesc("color"))
    print "equivalent(vector,color)", oiio.TypeDesc.equivalent(oiio.TypeDesc("vector"), oiio.TypeDesc("color"))
    print "vector.equivalent(float)", oiio.TypeDesc("vector").equivalent(oiio.TypeDesc("float"))
    print "equivalent(vector,float)", oiio.TypeDesc.equivalent(oiio.TypeDesc("vector"), oiio.TypeDesc("float"))
    print

    # Test the static data member types of pre-constructed types
    breakdown_test (oiio.TypeDesc.TypeFloat,    "TypeFloat",    verbose=False)
    breakdown_test (oiio.TypeDesc.TypeColor,    "TypeColor",    verbose=False)
    breakdown_test (oiio.TypeDesc.TypeString,   "TypeString",   verbose=False)
    breakdown_test (oiio.TypeDesc.TypeInt,      "TypeInt",      verbose=False)
    breakdown_test (oiio.TypeDesc.TypePoint,    "TypePoint",    verbose=False)
    breakdown_test (oiio.TypeDesc.TypeVector,   "TypeVector",   verbose=False)
    breakdown_test (oiio.TypeDesc.TypeNormal,   "TypeNormal",   verbose=False)
    breakdown_test (oiio.TypeDesc.TypeMatrix,   "TypeMatrix",   verbose=False)
    breakdown_test (oiio.TypeDesc.TypeTimeCode, "TypeTimeCode", verbose=False)
    breakdown_test (oiio.TypeDesc.TypeKeyCode,  "TypeKeyCode",  verbose=False)
    print

    print "Done."
except Exception as detail:
    print "Unknown exception:", detail

