From 1cddbe97a256eaea262a345e4eb6f454bc23ae9b Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Wed, 16 Jan 2008 06:07:59 +0000 Subject: [PATCH] Add new test case for stack handling. --- src/test/java/org/luaj/vm/LuaJTest.java | 6 +++- src/test/res/stack.lua | 46 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/res/stack.lua diff --git a/src/test/java/org/luaj/vm/LuaJTest.java b/src/test/java/org/luaj/vm/LuaJTest.java index f7fdd729..384f0b31 100644 --- a/src/test/java/org/luaj/vm/LuaJTest.java +++ b/src/test/java/org/luaj/vm/LuaJTest.java @@ -131,8 +131,12 @@ public class LuaJTest extends TestCase { runTest( "simplemetatables" ); } + public void testStack() throws IOException, InterruptedException { + runTest( "stack" ); + } + public void testStrLib() throws IOException, InterruptedException { - runTest( "strlib" ); + runTest( "strlib" ); } public void testTable() throws IOException, InterruptedException { diff --git a/src/test/res/stack.lua b/src/test/res/stack.lua new file mode 100644 index 00000000..c7a94983 --- /dev/null +++ b/src/test/res/stack.lua @@ -0,0 +1,46 @@ +-- This test case computes and outputs the sums of the first n +-- integers for n from 1 to 100 in the most ridiculous way possible. + +-- The point is to exercise code related to varargs & argument passing +-- in the hopes of detecting bug(s) in stack resizing code. + +local function upto(n) + local t = {} + for i = 1, n do + t[i] = i + end + return unpack(t) +end + +local function map(f, ...) + local args = { ... } + local result = {} + local i = 1 + while args[i] do + result[i] = f(args[i]) + i = i + 1 + end + return unpack(result) +end + +local function join(sep, ...) + return table.concat({ ... }, sep) +end + +local i = 1 +local j = 1 +while i < 200 do + local params = join(", ", map(function(x) return("x"..x) end, upto(i))) + local body = join(" + ", map(function(x) return("x"..x) end, upto(i))) + local func = "function f(" .. params .. ")\n return(" .. body .. ")\nend" + + local args = join(", ", map(function(x) return tostring(x) end, upto(i))) + + local chunk = func .. "\nreturn f( " .. args .. " )" + + local co = coroutine.create(assert(loadstring(chunk, "test #"..i))) + + print(coroutine.resume(co)) + i = i + j + j = j + 1 +end