-- $LastChangedDate: 2010-05-25 21:26:05 -0400 (Tue, 25 May 2010) $
-- LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG
-- Misc tests of lib.lua

dofile( "pre.lua" )
LoadScript( "lib.lua" )

--------------------------------------------------------------------------------
-- Basic tests.

print( "Testing int()" )
assert( lib.int(0.0) == 0 )  -- divide error shouldn't happen
assert( lib.int(0) == 0 )
assert( lib.int(0.0) == 0.0 )
assert( lib.int(1.5) == 1 )
assert( lib.int(-1.5) == -1 )
assert( lib.int(0.1) == 0 )
assert( lib.int(-0.1) == 0 )
assert( lib.int(0.5) == 0 )
assert( lib.int(-0.5) == 0 )
assert( lib.int(0.9) == 0 )
assert( lib.int(-0.9) == 0 )
assert( lib.int(10.1) == 10 )
assert( lib.int(-10.1) == -10 )
assert( lib.int(10.9) == 10 )
assert( lib.int(-10.9) == -10 )
assert( lib.int(1000000000.5) == 1000000000 )
assert( lib.int(-1000000000.5) == -1000000000 )
assert( lib.int(1000000000000.5) == 1000000000000 )
assert( lib.int(-1000000000000.5) == -1000000000000 )
assert( lib.int(1234567890123.567) == 1234567890123 )
assert( lib.int(-1234567890123.567) == -1234567890123 )
print( "ok" )

x = nil
x = lib.Default( x, "a" )
assert( x == "a" )

x = true
x = lib.Default( x, true )
assert( x == true )

x = false
x = lib.Default( x, false )
assert( x == false )

x = nil
x = lib.Default( x, true )
assert( x == true )

x = nil
x = lib.Default( x, false )
assert( x == false )

x = "abc"
x = lib.Default( x, "a" )
assert( x == "abc" )

--------------------------------------------------------------------------------
-- Test lib.NewInstance(), lib.Destroy().

print( "Testing lib.Destroy()." )

Class = { }

destroyCalled = false

function Class:New()
    self = lib.NewInstance( Class )
    self.name = "Class"
    return self
end

function Class:Destroy()
    -- Test that lib.Destroy() passed self to this method.
    print( "Class:Destroy() called." )
    assert( self )
    assert( self.name == "Class" )
    destroyCalled = true
end

-- Test lib.NewInstance().
print( "Testing lib.NewInstance()" )
container = { }
container.object = Class:New()
assert( container.object.name == "Class" )
print( "ok" )

-- Test lib.Destroy().
print( "Testing lib.Destroy()" )
lib.Destroy( container, "object" )
assert( not container.object )  -- lib.Destroy should nilify member
assert( destroyCalled )  -- Destroy() should be called
print( "ok" )

--------------------------------------------------------------------------------
-- Test lib.CopyTable().

print( "Testing lib.CopyTable()" )

subTable = { }

t = {
    a = 123,
    b = 456,
    ["100"] = "abc",
    t = subTable,
}

t2 = lib.CopyTable( t )
assert( t2.a == 123 )
assert( t2.b == 456 )
assert( t2["100"] == "abc" )
assert( t2.t == subTable )

print( "ok" )

--------------------------------------------------------------------------------
-- Test lib.CompareTable().

print( "Testing lib.CompareTable()" )

t0 = { }
t1 = { }
assert( lib.CompareTables( t0, t1 ) )

t0 = { "a" }
t1 = { "a" }
assert( lib.CompareTables( t0, t1 ) )

t0 = { "a" }
t1 = { "z" }
assert( not lib.CompareTables( t0, t1 ) )

-- You might think they should be equal because
-- of the fact that order doesn't matter in map.
-- But these sudo-arrays are really these maps
-- do have different key:value pairs:
-- t0 = { [1]="a", [2]="b" }
-- t0 = { [1]="b", [2]="a" }
t0 = { "a", "b" }
t1 = { "b", "a" }
assert( not lib.CompareTables( t0, t1 ) )

a = "a"
t0 = { a }
t1 = { a }
assert( lib.CompareTables( t0, t1 ) )

a = "a"
t0 = { a }  -- strings should compare equal by value
t1 = { "a" }
assert( lib.CompareTables( t0, t1 ) )

t0 = { "a", "b", "c" }
t1 = { "a", "b", "c" }
assert( lib.CompareTables( t0, t1 ) )

a = "a"
t0 = { mem = a }
t1 = { mem = a }
assert( lib.CompareTables( t0, t1 ) )

a = "a"
t0 = { mem = a }
t1 = { mem = "a" }
assert( lib.CompareTables( t0, t1 ) )

obj0 = { }
obj1 = { }
t0 = { a = obj0 }
t1 = { a = obj0 }
assert( lib.CompareTables( t0, t1 ) )

obj0 = { }
obj1 = { }
t0 = { a = obj0 }
t1 = { b = obj0 }
assert( not lib.CompareTables( t0, t1 ) )

obj0 = { }
obj1 = { }
t0 = { a = obj0, b = obj1 }  -- order of key:value pairs doesn't matter
t1 = { b = obj1, a = obj0 }  -- as long as they're all equal
assert( lib.CompareTables( t0, t1 ) )

print( "ok" )

--------------------------------------------------------------------------------
-- Test lib.SplitFields()

print( "Testing lib.SplitFields()" )

t0 = lib.SplitFields( "", "," )
t1 = { "" }
assert( lib.CompareTables(t0,t1) )

t0 = lib.SplitFields( "a,b,c", "," )
t1 = { "a", "b", "c" }
assert( lib.CompareTables(t0,t1) )

t0 = lib.SplitFields( "a,b,", "," )
t1 = { "a", "b" }
assert( lib.CompareTables(t0,t1) )

print( "ok" )

--------------------------------------------------------------------------------
-- Test lib.RemoveSurroundingSpaces()

print( "Testing lib.RemoveSurroundingSpaces()" )

assert( lib.RemoveSurroundingSpaces( "" ), "" )
assert( lib.RemoveSurroundingSpaces( " " ), "" )
assert( lib.RemoveSurroundingSpaces( "a" ), "a" )
assert( lib.RemoveSurroundingSpaces( " a" ), "a" )
assert( lib.RemoveSurroundingSpaces( "a " ), "a" )
assert( lib.RemoveSurroundingSpaces( " a " ), "a" )
assert( lib.RemoveSurroundingSpaces( "  abc  " ), "abc" )
assert( lib.RemoveSurroundingSpaces( "  abc" ), "abc" )
assert( lib.RemoveSurroundingSpaces( "abc  " ), "abc" )
assert( lib.RemoveSurroundingSpaces( "[a b c]" ), "[a b c]" )
assert( lib.RemoveSurroundingSpaces( "[a b c]  " ), "[a b c]" )
assert( lib.RemoveSurroundingSpaces( "  [a b c]" ), "[a b c]" )
assert( lib.RemoveSurroundingSpaces( "  [a b c]  " ), "[a b c]" )

print( "ok" )

--------------------------------------------------------------------------------
-- Test lib.ParseCommas()

print( "Testing lib.ParseCommas()" )

-- lib.ParseCommas() is intended for parsing function args
-- so not concerned with every corner-case.

t0 = { "x" }
t1 = lib.ParseCommas( "x" )
assert( lib.CompareTables( t0, t1 ) )

t0 = { "x" }
t1 = lib.ParseCommas( " x  " )
assert( lib.CompareTables( t0, t1 ) )

t0 = { "x", "y", "z" }
t1 = lib.ParseCommas( "x, y, z" )
assert( lib.CompareTables( t0, t1 ) )

t0 = { "x", "y", "z" }
t1 = lib.ParseCommas( "  x, y, z  " )
assert( lib.CompareTables( t0, t1 ) )

print( "ok" )

--------------------------------------------------------------------------------
-- Success.

os.exit( 0 )
