tests: add code generation test for 'for'
@@ -14,13 +14,14 @@ local function generate(ast, modname)
local CC = "gcc"
local CFLAGS = "--std=c99 -O2 -Wall -Ilua/src/ -fPIC"
- return os.execute(string.format([[
+ local cc_cmd = string.format([[
%s %s -shared %s.c lua/src/liblua.a -o %s.so
- ]], CC, CFLAGS, modname, modname))
+ ]], CC, CFLAGS, modname, modname)
+ return os.execute(cc_cmd)
end
local function call(modname, code)
- local cmd = string.format("lua -l %s -e \"%s\"",
+ local cmd = string.format("lua/src/lua -l %s -e \"%s\"",
modname, code)
return os.execute(cmd)
end
@@ -101,6 +102,26 @@ describe("Titan code generator ", function()
assert.truthy(ok, err)
end)
+ it("tests step value in 'for'", function()
+ local code = [[
+ function forstep(f: integer, t: integer, s: integer): integer
+ local v: integer = 0
+ for i = f, t, s do
+ v = v + i
+ end
+ return v
+ end
+ ]]
+ local ast, err = parser.parse(code)
+ assert.truthy(ast, err)
+ local ok, err = checker.check(ast, code, "test.titan")
+ assert.truthy(ok, err)
+ local ok, err = generate(ast, "titan_test")
+ assert.truthy(ok, err)
+ local ok, err = call("titan_test", "x = titan_test.forstep(1,10,2);assert(x==25)")
+ assert.truthy(ok, err)
+ end)
+
it("tests nil element in 'not'", function()
local code = [[
function testset(t: {integer}, i: integer, v: integer): integer
