require("test/setup")
require("graphics")

local function assertSmallDifference(previous, current, ballValue, ballLabel)
    local difference = math.abs(previous - current)
    local baseError = "Expected a small difference, but received a difference of " .. difference
    local fullDetails = luaunit.prettystr({ previous = previous, current = current, [ballLabel] = ballValue })
    luaunit.assertIsTrue(difference < 2, baseError .. ":\n  " .. luaunit.prettystr(fullDetails))
end

function testNoJumpsInYOffset()
    local startY = -240 * 3
    local atXValues = { -400, 0, 400 }
    for _, xValue in ipairs(atXValues) do
        local _, lastYOffset = getDrawOffset(xValue, startY)
        for ballY = startY, 240 do
            local _, currentYOffset = getDrawOffset(xValue, ballY)
            assertSmallDifference(lastYOffset, currentYOffset, ballY, "ballY")
            lastYOffset = currentYOffset
        end
    end
end

function testNoJumpsInXOffset()
    local startX = -800
    local atYValues = { 240, 0, -240 }
    for _, yValue in ipairs(atYValues) do
        local lastXOffset = getDrawOffset(startX, yValue)
        for ballX = startX, 800 do
            local currentXOffset = getDrawOffset(ballX, yValue)
            assertSmallDifference(lastXOffset, currentXOffset, ballX, "ballX")
            lastXOffset = currentXOffset
        end
    end
end

os.exit(luaunit.LuaUnit.run())
