From 47d755a57029c062e66b287f5619e87f9b264926 Mon Sep 17 00:00:00 2001 From: Rerumu Date: Thu, 16 Jun 2022 16:30:45 -0400 Subject: [PATCH] Set up test code per version --- .../{assertion.lua => luajit_assert.lua} | 51 ++++++----- dev-test/tests/luajit_translate.rs | 26 +----- dev-test/tests/luau_assert.lua | 84 +++++++++++++++++++ dev-test/tests/luau_translate.rs | 26 +----- 4 files changed, 116 insertions(+), 71 deletions(-) rename dev-test/tests/{assertion.lua => luajit_assert.lua} (52%) create mode 100644 dev-test/tests/luau_assert.lua diff --git a/dev-test/tests/assertion.lua b/dev-test/tests/luajit_assert.lua similarity index 52% rename from dev-test/tests/assertion.lua rename to dev-test/tests/luajit_assert.lua index 2dae3d6..5d8cd04 100644 --- a/dev-test/tests/assertion.lua +++ b/dev-test/tests/luajit_assert.lua @@ -6,50 +6,36 @@ local LUA_NAN_CANONICAL = -(0 / 0) local LUA_NAN_DEFAULT = -(0 / 0) local LUA_INFINITY = math.huge -local function is_number_equality(lhs, rhs) +local function is_number_equal(lhs, rhs) if type(lhs) ~= "number" or type(rhs) ~= "number" then return false - elseif lhs ~= lhs and rhs ~= rhs then - return true end return math.abs(lhs - rhs) < 0.00001 or string.format("%.3g", lhs) == string.format("%.3g", rhs) end -local function assert_eq(lhs, rhs, message, level) - if lhs == rhs or is_number_equality(lhs, rhs) then +local function assert_eq(lhs, rhs, level) + if lhs == rhs or is_number_equal(lhs, rhs) then return end - if message then - message = ": " .. message - else - message = "" - end - lhs = tostring(lhs) rhs = tostring(rhs) level = (level or 1) + 1 - error(lhs .. " ~= " .. rhs .. message, level) + error(lhs .. " ~= " .. rhs, level) end -local function assert_neq(lhs, rhs, message, level) - if lhs ~= rhs and not is_number_equality(lhs, rhs) then +local function assert_neq(lhs, rhs, level) + if lhs ~= rhs and not is_number_equal(lhs, rhs) then return end - if message then - message = ": " .. message - else - message = "" - end - lhs = tostring(lhs) rhs = tostring(rhs) level = (level or 1) + 1 - error(lhs .. " == " .. rhs .. message, level) + error(lhs .. " == " .. rhs, level) end local function raw_invoke(func, ...) @@ -63,8 +49,8 @@ local function assert_trap(func, ...) end local function assert_return(data, wanted) - for i, v in ipairs(wanted) do - assert_eq(data[i], v, "Result mismatch at " .. i, 2) + for i, v in wanted do + assert_eq(data[i], v, 2) end end @@ -73,3 +59,22 @@ local function assert_exhaustion(func, ...) error("Failed to exhaust", 2) end end + +linked.spectest = { + func_list = { + print = print, + print_f32 = print, + print_f64 = print, + print_f64_f64 = print, + print_i32 = print, + print_i32_f32 = print, + }, + global_list = { + global_f32 = { value = 666 }, + global_f64 = { value = 666 }, + global_i32 = { value = 666 }, + global_i64 = { value = 666LL }, + }, + table_list = { table = { data = {} } }, + memory_list = { memory = rt.allocator.new(1, 2) }, +} diff --git a/dev-test/tests/luajit_translate.rs b/dev-test/tests/luajit_translate.rs index a18f7d7..770ebe8 100644 --- a/dev-test/tests/luajit_translate.rs +++ b/dev-test/tests/luajit_translate.rs @@ -14,28 +14,7 @@ use target::{get_name_from_id, Target}; mod target; -static ASSERTION: &str = include_str!("assertion.lua"); - -static SPEC_TEST: &str = " -linked.spectest = { - func_list = { - print = print, - print_f32 = print, - print_f64 = print, - print_f64_f64 = print, - print_i32 = print, - print_i32_f32 = print, - }, - global_list = { - global_f32 = { value = 666 }, - global_f64 = { value = 666 }, - global_i32 = { value = 666 }, - global_i64 = { value = 666LL }, - }, - table_list = { table = { data = {} } }, - memory_list = { memory = rt.allocator.new(1, 2) }, -} -"; +static ASSERTION: &str = include_str!("luajit_assert.lua"); struct LuaJIT; @@ -155,9 +134,8 @@ impl Target for LuaJIT { fn write_runtime(w: &mut dyn Write) -> Result<()> { let runtime = codegen_luajit::RUNTIME; - writeln!(w, "{ASSERTION}")?; writeln!(w, "local rt = (function() {runtime} end)()")?; - writeln!(w, "{SPEC_TEST}") + writeln!(w, "{ASSERTION}") } fn write_module(data: &Module, name: Option<&str>, w: &mut dyn Write) -> Result<()> { diff --git a/dev-test/tests/luau_assert.lua b/dev-test/tests/luau_assert.lua new file mode 100644 index 0000000..49c7d74 --- /dev/null +++ b/dev-test/tests/luau_assert.lua @@ -0,0 +1,84 @@ +local loaded = {} +local linked = {} + +local LUA_NAN_ARITHMETIC = -(0 / 0) +local LUA_NAN_CANONICAL = -(0 / 0) +local LUA_NAN_DEFAULT = -(0 / 0) +local LUA_INFINITY = math.huge + +local function is_number_equal(lhs, rhs) + if type(lhs) == "table" and type(rhs) == "table" then + return rt.eq.i64(lhs, rhs) + elseif type(lhs) ~= "number" or type(rhs) ~= "number" then + return false + elseif lhs ~= lhs and rhs ~= rhs then + return true + end + + return math.abs(lhs - rhs) < 0.00001 or string.format("%.3g", lhs) == string.format("%.3g", rhs) +end + +local function assert_eq(lhs, rhs, level) + if lhs == rhs or is_number_equal(lhs, rhs) then + return + end + + lhs = tostring(lhs) + rhs = tostring(rhs) + level = (level or 1) + 1 + + error(lhs .. " ~= " .. rhs, level) +end + +local function assert_neq(lhs, rhs, level) + if lhs ~= rhs and not is_number_equal(lhs, rhs) then + return + end + + lhs = tostring(lhs) + rhs = tostring(rhs) + level = (level or 1) + 1 + + error(lhs .. " == " .. rhs, level) +end + +local function raw_invoke(func, ...) + return func(...) +end + +local function assert_trap(func, ...) + if pcall(func, ...) then + error("Failed to trap", 2) + end +end + +local function assert_return(data, wanted) + for i, v in wanted do + assert_eq(data[i], v, 2) + end +end + +local function assert_exhaustion(func, ...) + if pcall(func, ...) then + error("Failed to exhaust", 2) + end +end + +linked.spectest = { + func_list = { + print = print, + print_f32 = print, + print_f64 = print, + print_f64_f64 = print, + print_i32 = print, + print_i32_f32 = print, + }, + global_list = { + global_f32 = { value = 666 }, + global_f64 = { value = 666 }, + global_i32 = { value = 666 }, + global_i64 = { value = rt.i64.from_u32(666, 0) }, + }, + table_list = { table = { data = {} } }, + memory_list = { memory = rt.allocator.new(1, 2) }, +} diff --git a/dev-test/tests/luau_translate.rs b/dev-test/tests/luau_translate.rs index 380d83a..e9ad3f5 100644 --- a/dev-test/tests/luau_translate.rs +++ b/dev-test/tests/luau_translate.rs @@ -14,28 +14,7 @@ use target::{get_name_from_id, Target}; mod target; -static ASSERTION: &str = include_str!("assertion.lua"); - -static SPEC_TEST: &str = " -linked.spectest = { - func_list = { - print = print, - print_f32 = print, - print_f64 = print, - print_f64_f64 = print, - print_i32 = print, - print_i32_f32 = print, - }, - global_list = { - global_f32 = { value = 666 }, - global_f64 = { value = 666 }, - global_i32 = { value = 666 }, - global_i64 = { value = rt.i64.from_u32(666, 0) }, - }, - table_list = { table = { data = {} } }, - memory_list = { memory = rt.allocator.new(1, 2) }, -} -"; +static ASSERTION: &str = include_str!("luau_assert.lua"); struct Luau; @@ -162,9 +141,8 @@ impl Target for Luau { fn write_runtime(w: &mut dyn Write) -> Result<()> { let runtime = codegen_luau::RUNTIME; - writeln!(w, "{ASSERTION}")?; writeln!(w, "local rt = (function() {runtime} end)()")?; - writeln!(w, "{SPEC_TEST}") + writeln!(w, "{ASSERTION}") } fn write_module(data: &Module, name: Option<&str>, w: &mut dyn Write) -> Result<()> {