1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#!/bin/sh
# This is a smoke test for the basic functionality of the lua C api.
set -e
rm -f libluatest.c testscript.lua libluatest
# Create a c file that runs a lua file directly, then extracts a function and
# runs it on its own with a custom argument input.
cat <<EOF > libluatest.c
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_loadfile(L, "testscript.lua");
// Run file directly - 3 dot sets
lua_pcall(L, 0, LUA_MULTRET, 0);
// Run function directly - 4 dot sets
lua_getglobal(L, "print_dots");
lua_pushnumber(L, 4);
lua_call(L, 1, 0);
lua_close(L);
return 0;
}
EOF
# Create a lua file with a recursive function that prints a triangle of dots
# with an argument for the initial number of dots to print.
cat <<EOF > testscript.lua
function print_dots(num)
for i=1,num do io.write('.') end
io.write('\n')
if num > 0 then print_dots(num - 1) end
end
print_dots(3)
EOF
do_test()
{
# Compile with lua api includes.
rm -f libluatest
$1 -Wall -Werror -o libluatest libluatest.c -l$2 -I/usr/include/lua5.4
# Run the c file then test the printed output. It should first print a
# triangle starting with 3 dots since print_dots(3) is in the lua file.
# Then it should print a triangle starting with 4 dots since 4 is pushed
# onto the stack when the function is called directly by the c file.
test_output=$(./libluatest)
correct_output="...
..
.
....
...
..
."
if [ "$test_output" != "$correct_output" ]; then
echo "Error: expected:
$correct_output
received:
$test_output"
exit 1
fi
}
do_test gcc lua5.4
do_test g++ lua5.4-c++
rm -f libluatest.c testscript.lua libluatest
|