print("=== Error Case Tests ===")
print("Note: These tests verify error handling. Some 'errors' are expected.")
print("")
local test_count = 0
local pass_count = 0
print("1. Type errors in arithmetic")
local a = 5 + 3
print("5 + 3 = " .. tostring(a))
local b = "hello" .. " world"
print("'hello' .. ' world' = " .. b)
print("")
print("2. Nil access on tables (safe)")
local t = { x = 1 }
print("t.x = " .. tostring(t.x))
print("t.y = " .. tostring(t.y)) print("t.z = " .. tostring(t.z)) print("")
print("3. __call makes tables callable")
local callable = setmetatable({}, {
__call = function(self, x)
return x * 2
end
})
print("callable(5) = " .. tostring(callable(5)))
print("")
print("4. Length operator on various types")
print("#'hello' = " .. tostring(#"hello"))
print("#{1,2,3} = " .. tostring(#{1,2,3}))
local with_len = setmetatable({}, { __len = function() return 42 end })
print("#with_len = " .. tostring(#with_len))
print("")
print("5. Testing recursion limit handling")
local depth = 0
local max_reached = 0
local recurse
recurse = function()
depth = depth + 1
if depth > max_reached then
max_reached = depth
end
if depth < 50 then recurse()
end
depth = depth - 1
end
recurse()
print("Safe recursion depth reached: " .. tostring(max_reached))
print("")
print("6. Large table handling")
local big = {}
for i = 1, 100 do
big[i] = i * i
end
print("big[1] = " .. tostring(big[1]))
print("big[50] = " .. tostring(big[50]))
print("big[100] = " .. tostring(big[100]))
print("#big = " .. tostring(#big))
print("")
print("7. String edge cases")
print("string.sub('hello', 1, 0) = '" .. string.sub("hello", 1, 0) .. "'") print("string.sub('hello', 10, 20) = '" .. string.sub("hello", 10, 20) .. "'") print("string.sub('hello', -2) = '" .. string.sub("hello", -2) .. "'") print("string.upper('') = '" .. string.upper("") .. "'") print("")
print("8. Math edge cases")
print("math.floor(3.7) = " .. tostring(math.floor(3.7)))
print("math.ceil(3.2) = " .. tostring(math.ceil(3.2)))
print("math.abs(-5) = " .. tostring(math.abs(-5)))
print("math.min(3, 1, 4, 1, 5) = " .. tostring(math.min(3, 1, 4, 1, 5))) print("math.max(3, 1, 4, 1, 5) = " .. tostring(math.max(3, 1, 4, 1, 5))) print("")
print("9. Empty varargs handling")
local count_args = function(...)
return select("#", ...)
end
print("count_args() = " .. tostring(count_args()))
print("count_args(1) = " .. tostring(count_args(1)))
print("count_args(1, 2, 3) = " .. tostring(count_args(1, 2, 3)))
print("")
print("10. Table with nil values")
local sparse = { [1] = "a", [3] = "c", [5] = "e" }
print("sparse[1] = " .. tostring(sparse[1]))
print("sparse[2] = " .. tostring(sparse[2])) print("sparse[3] = " .. tostring(sparse[3]))
print("")
print("11. Boolean edge cases")
print("not nil = " .. tostring(not nil))
print("not false = " .. tostring(not false))
print("not 0 = " .. tostring(not 0)) print("not '' = " .. tostring(not "")) print("nil or 'default' = " .. tostring(nil or "default"))
print("false or 'default' = " .. tostring(false or "default"))
print("0 or 'default' = " .. tostring(0 or "default")) print("")
print("12. Numeric for edge cases")
local sum = 0
for i = 5, 1, -1 do sum = sum + i
end
print("sum 5 down to 1 = " .. tostring(sum))
sum = 0
for i = 1, 0 do sum = sum + i
end
print("sum 1 to 0 (empty) = " .. tostring(sum)) print("")
print("13. ipairs handles false values correctly")
local with_false = {1, false, 3, nil, 5}
local ipairs_result = {}
for i, v in ipairs(with_false) do
ipairs_result[i] = tostring(v)
end
print("ipairs result: " .. table.concat(ipairs_result, ", ")) print("Expected: 1, false, 3 (stops at nil)")
print("")
print("=== All error case tests completed! ===")