test: compare compiled modules with the Lua 5.4 interpreter
The generated code can only be trusted if it behaves like the reference implementation, so the same script runs both ways and the outputs are diffed.
This commit is contained in:
5 files changed
+250
No files matched your search
@@ -0,0 +1,8 @@
|
||||
add_lua_package(luacpp_conformance_lua
|
||||
ROOT "${CMAKE_CURRENT_SOURCE_DIR}/lua"
|
||||
MODULES core.lua)
|
||||
|
||||
add_executable(luacpp_conformance core_main.cpp)
|
||||
target_link_libraries(luacpp_conformance PRIVATE luacpp_conformance_lua)
|
||||
|
||||
add_test(NAME luacpp_conformance COMMAND luacpp_conformance)
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
# Compares the compiled module output with the reference Lua 5.4 interpreter.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
BINARY="${1:-build/tests/luacpp_conformance}"
|
||||
|
||||
if ! command -v lua5.4 >/dev/null 2>&1 && ! command -v lua >/dev/null 2>&1; then
|
||||
echo "no Lua interpreter found; skipping reference comparison" >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
LUA_BIN="$(command -v lua5.4 || command -v lua)"
|
||||
"${LUA_BIN}" tests/reference.lua tests/lua/core.lua > /tmp/luacpp_reference.txt
|
||||
"${BINARY}" > /tmp/luacpp_compiled.txt
|
||||
diff -u /tmp/luacpp_reference.txt /tmp/luacpp_compiled.txt
|
||||
echo "OK: compiled module matches the reference interpreter ($(wc -l < /tmp/luacpp_compiled.txt) checks)"
|
||||
@@ -0,0 +1,21 @@
|
||||
// Prints the conformance results produced by the compiled core.lua module.
|
||||
#include <iostream>
|
||||
|
||||
#include <luacpp/rt.hpp>
|
||||
|
||||
#include "core.hpp"
|
||||
|
||||
int main() {
|
||||
try {
|
||||
luacpp::State state;
|
||||
luacpp::Value results = state.require("core");
|
||||
int n = static_cast<int>(results.length());
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
std::cout << results.at(i).as_string() << "\n";
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << "conformance: " << error.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
-- Conformance suite: exercises the supported Lua subset and returns an array
|
||||
-- of "name=value" strings so the compiled module can be compared byte for byte
|
||||
-- with the reference Lua interpreter.
|
||||
local results = {}
|
||||
|
||||
local function add(name, value)
|
||||
results[#results + 1] = name .. "=" .. tostring(value)
|
||||
end
|
||||
|
||||
-- numbers
|
||||
add("add", 2 + 3)
|
||||
add("div", 1 / 2)
|
||||
add("idiv", 7 // 2)
|
||||
add("mod", 7 % 3)
|
||||
add("pow", 2 ^ 10)
|
||||
add("unm", -3)
|
||||
add("band", 0xFF & 0x0F)
|
||||
add("shl", 1 << 10)
|
||||
add("int_type", math.type(7))
|
||||
add("float_type", math.type(7.0))
|
||||
add("floor", math.floor(3.9))
|
||||
|
||||
-- strings
|
||||
local s = "Buffy"
|
||||
add("upper", s:upper())
|
||||
add("sub", string.sub(s, 2, 4))
|
||||
add("slen", #s)
|
||||
add("concat", s .. " " .. 42)
|
||||
add("escape", "a\tb\nc")
|
||||
add("fmt", string.format("%s-%d-%.2f", "x", 7, 1.5))
|
||||
|
||||
-- tables
|
||||
local t = { 10, 20, 30, x = 5 }
|
||||
t[#t + 1] = 40
|
||||
add("tlen", #t)
|
||||
add("tidx", t[4])
|
||||
add("tfield", t.x)
|
||||
local keyed = { ["a" .. "b"] = "ab", [2 + 3] = "five" }
|
||||
add("keyed", keyed.ab .. "/" .. keyed[5])
|
||||
local nested = { a = { b = { c = 1 } } }
|
||||
add("nested", nested.a.b.c)
|
||||
|
||||
-- control flow
|
||||
local sum = 0
|
||||
for i = 1, 10 do
|
||||
sum = sum + i
|
||||
end
|
||||
add("for_sum", sum)
|
||||
|
||||
local wsum, wi = 0, 0
|
||||
while wi < 5 do
|
||||
wi = wi + 1
|
||||
wsum = wsum + wi
|
||||
end
|
||||
add("while_sum", wsum)
|
||||
|
||||
local rsum, rj = 0, 0
|
||||
repeat
|
||||
rj = rj + 1
|
||||
rsum = rsum + rj
|
||||
until rj >= 3
|
||||
add("repeat_sum", rsum)
|
||||
|
||||
local found
|
||||
for _, v in ipairs({ 5, 6, 7 }) do
|
||||
if v == 6 then
|
||||
found = v
|
||||
break
|
||||
end
|
||||
end
|
||||
add("break", found)
|
||||
|
||||
local fsum = 0
|
||||
for _, v in pairs({ a = 1, b = 2, c = 3 }) do
|
||||
fsum = fsum + v
|
||||
end
|
||||
add("pairs_sum", fsum)
|
||||
|
||||
local total = 0
|
||||
for i = 10, 1, -3 do
|
||||
total = total + i
|
||||
end
|
||||
add("for_neg", total)
|
||||
|
||||
-- functions, varargs, multiple returns
|
||||
local function two()
|
||||
return 1, 2
|
||||
end
|
||||
local a, b = two()
|
||||
add("multi_return", a + b)
|
||||
|
||||
local function sum_all(...)
|
||||
local acc = 0
|
||||
for _, v in ipairs({ ... }) do
|
||||
acc = acc + v
|
||||
end
|
||||
return acc
|
||||
end
|
||||
add("varargs", sum_all(1, 2, 3, 4))
|
||||
|
||||
local function swap(x, y)
|
||||
return y, x
|
||||
end
|
||||
local p, q = swap("left", "right")
|
||||
add("swap_returns", p .. "/" .. q)
|
||||
|
||||
local function with_default(x, y)
|
||||
y = y or 10
|
||||
return x + y
|
||||
end
|
||||
add("default_arg", with_default(5))
|
||||
|
||||
local function fact(n)
|
||||
if n <= 1 then
|
||||
return 1
|
||||
end
|
||||
return n * fact(n - 1)
|
||||
end
|
||||
add("recursion", fact(6))
|
||||
|
||||
-- closures capture mutable locals
|
||||
local function counter()
|
||||
local n = 0
|
||||
return function()
|
||||
n = n + 1
|
||||
return n
|
||||
end
|
||||
end
|
||||
local c = counter()
|
||||
c()
|
||||
c()
|
||||
add("closure", c())
|
||||
|
||||
local function adder(x)
|
||||
return function(y)
|
||||
return x + y
|
||||
end
|
||||
end
|
||||
add("closure_nested", adder(10)(5))
|
||||
|
||||
-- methods and metatables
|
||||
local point = { x = 1, y = 2 }
|
||||
function point:norm2()
|
||||
return self.x * self.x + self.y * self.y
|
||||
end
|
||||
add("method", point:norm2())
|
||||
|
||||
local mt = {}
|
||||
mt.__index = mt
|
||||
function mt.new(v)
|
||||
return setmetatable({ value = v }, mt)
|
||||
end
|
||||
function mt:double()
|
||||
return self.value * 2
|
||||
end
|
||||
mt.__add = function(lhs, rhs)
|
||||
return mt.new(lhs.value + rhs.value)
|
||||
end
|
||||
mt.__tostring = function(obj)
|
||||
return "box(" .. obj.value .. ")"
|
||||
end
|
||||
local box1, box2 = mt.new(3), mt.new(4)
|
||||
add("meta_method", (box1 + box2):double())
|
||||
add("meta_tostring", tostring(box1))
|
||||
|
||||
-- table library driven by a Lua callback
|
||||
local list = { 5, 2, 9, 1 }
|
||||
table.sort(list, function(x, y)
|
||||
return x > y
|
||||
end)
|
||||
add("sort", table.concat(list, ","))
|
||||
|
||||
-- errors
|
||||
local ok, err = pcall(function()
|
||||
error("boom")
|
||||
end)
|
||||
add("pcall", tostring(ok) .. ":" .. tostring(err):match("boom%a*"))
|
||||
|
||||
-- multiple assignment swap
|
||||
local u, v = 1, 2
|
||||
u, v = v, u
|
||||
add("swap_vars", u .. "/" .. v)
|
||||
|
||||
-- goto / labels
|
||||
local visited = 0
|
||||
for k = 1, 5 do
|
||||
if k == 3 then
|
||||
goto continue
|
||||
end
|
||||
visited = visited + 1
|
||||
::continue::
|
||||
end
|
||||
add("goto", visited)
|
||||
|
||||
add("tonumber", tonumber("42") + 1)
|
||||
add("select_count", select("#", 1, 2, 3))
|
||||
|
||||
return results
|
||||
@@ -0,0 +1,6 @@
|
||||
-- Reference driver: runs the same Lua module with the stock interpreter.
|
||||
local path = arg[1] or "tests/lua/core.lua"
|
||||
local results = dofile(path)
|
||||
for i = 1, #results do
|
||||
print(results[i])
|
||||
end
|
||||
Reference in new issue
Block a user