-- $LastChangedDate: 2010-05-25 21:26:05 -0400 (Tue, 25 May 2010) $
-- LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG
-- Test lib.Iterator()/IteratorAfter().

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

t = {}
t.a = 1
t.b = 1
t.c = 1

print( "Testing lib.Iterator() basic case." )
u = {}
for k,v in lib.Iterator(t,"a") do
    u[k] = v
end
assert( u.a == t.a and u.b == t.b and u.c == t.c )
print( "ok" )

-- Order is undefined.
-- An indirect way to test is by summing.
-- 1,1,1
-- 1,1
-- 1
-- sum = 6
print( "Testing lib.Iterator() multiple cases." )
sum = 0
for k,v in lib.Iterator(t,"a") do
    sum = sum + 1
end
for k,v in lib.Iterator(t,"b") do
    sum = sum + 1
end
for k,v in lib.Iterator(t,"c") do
    sum = sum + 1
end
assert( sum == 6 )
print( "ok" )

-------------------------------------------------------------------------------
-- Test lib.IteratorAfter()

print( "Testing lib.IteratorAfter() with empty table." )
t = {}
for k in lib.IteratorAfter( t ) do
    assert( false )
end
print( "ok" )

print( "Testing lib.IteratorAfter() with one-element table." )
t = {}
t.a = 1
for k in lib.IteratorAfter( t, "a" ) do
    assert( false )
end
print( "ok" )

-- Although order is undefined, pretend it is, then:
-- a: 1,1
-- b: 1
-- c: none
-- sum = 3
print( "Testing lib.IteratorAfter() with two-element table." )
t = {}
t.a = 1
t.b = 1
t.c = 1
sum = 0
for k in lib.IteratorAfter( t, "a" ) do
    sum = sum + 1
end
for k in lib.IteratorAfter( t, "b" ) do
    sum = sum + 1
end
for k in lib.IteratorAfter( t, "c" ) do
    sum = sum + 1
end
assert( sum == 3 )
print( "ok" )

-- Success.
os.exit( 0 )
