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