
makeclass("unittests")


function unittests.tables()
    -- check table.count
    assert(table.count({}) == 0)
    assert(table.count({a=1,b=2}) == 2)
    assert(table.count({[2]=5,[5]=3}) == 2)

    -- check table.clear
    local t = { a = 1, b = 2 }
    table.clear(t, true)
    assert(t.a == nil)
    assert(t.b == nil)
    assert(table.count(t) == 0)

    -- check table.empty
    assert(table.empty({}) == true)
    assert(table.empty({1}) == false)
    assert(table.empty({a=1}) == false)

    -- check table.equal
    assert(table.equal({},{}))
    assert(not table.equal({}, {1}))
    assert(not table.equal({1}, {}))
    assert(table.equal({1,2,3}, {1,2,3}))
    assert(not table.equal({1,2,3}, {1,5,3}))
    assert(not table.equal({1,2}, {1,2,3}))
    assert(not table.equal({1,2,3}, {1,2}))
    assert(table.equal({a=1,b=2},{a=1,b=2}))
    assert(not table.equal({a=1,b=3},{a=1,b=2}))

    
end

function unittests.vectors()
    assert(true == table.isarray{1,2,3})
    assert(false == table.isarray{1,2,nil,3})

    -- check vector.removeall
    t = {1,2,3,4,5,1,2,3,4,5}
    assert(true == array.removeall(t, 2))
    assert(table.equal(t, {1,3,4,5,1,3,4,5}))
    assert(false == array.removeall(t, 7))
    assert(table.equal(t, {1,3,4,5,1,3,4,5}))
    assert(true == array.removeall(t, 5))
    assert(table.equal(t, {1,3,4,1,3,4}))
    assert(true == array.removeall(t, 1))
    assert(table.equal(t, {3,4,3,4}))

    -- check vector.push
    t = {}
    array.push(t, 1)
    assert(table.equal(t, {1}))
    array.push(t, 2)
    assert(table.equal(t, {1,2}))
    array.push(t, 3)
    assert(table.equal(t, {1,2,3}))

    -- check vector.pop
    t = {1,2,3}
    assert(3 == array.pop(t))
    assert(table.equal(t, {1,2}))
    assert(2 == array.pop(t))
    assert(table.equal(t, {1}))
    assert(1 == array.pop(t))
    assert(table.equal(t, {}))
    assert(nil == array.pop(t))
    assert(table.equal(t, {}))
end


function unittests.deque()
    local d = deque.create()
    for i=1,7 do
        for j=1,i do
            d:pushr(j)
        end
        for j=1,i do
            assert(d:nthl(j) == j)
        end
        for j=1,i do
            assert(d:popl() == j)
        end
    end
    for i=1,7 do
        for j=1,i do
            d:pushl(j)
        end
        for j=1,i do
            assert(d:nthr(j) == j)
        end
        for j=1,i do
            assert(d:popr() == j)
        end
    end
    for i=1,7 do
        for j=1,i do
            d:pushr(j)
        end
        for j=1,i do
            assert(d:popr() == i+1-j)
        end
    end
end
