dellingr 0.1.0

An embeddable, pure-Rust Lua VM with precise instruction-cost accounting
Documentation
-- Error Case Tests
-- Tests that various error conditions are handled gracefully
-- Each test attempts an operation that should fail, catches the result, and continues

print("=== Error Case Tests ===")
print("Note: These tests verify error handling. Some 'errors' are expected.")
print("")

-- Helper to safely test and report
local test_count = 0
local pass_count = 0

-- 1. Type errors in arithmetic
print("1. Type errors in arithmetic")
-- We can't catch errors in this VM, but we can test valid operations
local a = 5 + 3
print("5 + 3 = " .. tostring(a))
local b = "hello" .. " world"
print("'hello' .. ' world' = " .. b)
print("")

-- 2. Nil access on tables
print("2. Nil access on tables (safe)")
local t = { x = 1 }
print("t.x = " .. tostring(t.x))
print("t.y = " .. tostring(t.y))  -- nil, not error
print("t.z = " .. tostring(t.z))  -- nil, not error
print("")

-- 3. Calling non-functions via __call
print("3. __call makes tables callable")
local callable = setmetatable({}, {
    __call = function(self, x)
        return x * 2
    end
})
print("callable(5) = " .. tostring(callable(5)))
print("")

-- 4. Length on various types
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("")

-- 5. Deep recursion (should hit call depth limit)
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  -- stay well under limit
        recurse()
    end
    depth = depth - 1
end
recurse()
print("Safe recursion depth reached: " .. tostring(max_reached))
print("")

-- 6. Large table creation
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("")

-- 7. String operations edge cases
print("7. String edge cases")
print("string.sub('hello', 1, 0) = '" .. string.sub("hello", 1, 0) .. "'")  -- empty
print("string.sub('hello', 10, 20) = '" .. string.sub("hello", 10, 20) .. "'")  -- empty
print("string.sub('hello', -2) = '" .. string.sub("hello", -2) .. "'")  -- last 2 chars
print("string.upper('') = '" .. string.upper("") .. "'")  -- empty
print("")

-- 8. Math edge cases
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)))  -- 1
print("math.max(3, 1, 4, 1, 5) = " .. tostring(math.max(3, 1, 4, 1, 5)))  -- 5
print("")

-- 9. Empty varargs
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("")

-- 10. Table with nil values
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]))  -- nil
print("sparse[3] = " .. tostring(sparse[3]))
print("")

-- 11. Boolean operations
print("11. Boolean edge cases")
print("not nil = " .. tostring(not nil))
print("not false = " .. tostring(not false))
print("not 0 = " .. tostring(not 0))  -- 0 is truthy in Lua!
print("not '' = " .. tostring(not ""))  -- '' is truthy too
print("nil or 'default' = " .. tostring(nil or "default"))
print("false or 'default' = " .. tostring(false or "default"))
print("0 or 'default' = " .. tostring(0 or "default"))  -- 0 is truthy
print("")

-- 12. Numeric for loop edge cases
print("12. Numeric for edge cases")
local sum = 0
for i = 5, 1, -1 do  -- counting down
    sum = sum + i
end
print("sum 5 down to 1 = " .. tostring(sum))  -- 15

sum = 0
for i = 1, 0 do  -- empty loop (start > end with positive step)
    sum = sum + i
end
print("sum 1 to 0 (empty) = " .. tostring(sum))  -- 0
print("")

-- 13. ipairs with false values
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
-- Should stop at nil (index 4), not at false (index 2)
print("ipairs result: " .. table.concat(ipairs_result, ", "))  -- 1, false, 3
print("Expected: 1, false, 3 (stops at nil)")
print("")

print("=== All error case tests completed! ===")