Commit 48964e08 authored by François Perrad's avatar François Perrad
Browse files

some tests

parent e9971969
#! /usr/bin/lua
--
-- lua-Harness : <https://fperrad.frama.io/lua-Harness/>
--
-- Copyright (C) 2009-2018, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--
--[[
=head1 Lua test suite
=head2 Synopsis
% prove 000-sanity.t
=head2 Description
=cut
]]
function f (n)
return n + 1
end
function g (m, p)
return m + p
end
print('1..9')
print("ok 1 -")
print('ok', 2, "- list")
print("ok " .. tostring(3) .. " - concatenation")
i = 4
print("ok " .. tostring(i) .. " - var")
i = i + 1
print("ok " .. tostring(i) .. " - var incr")
print("ok " .. tostring(i+1) .. " - expr")
j = f(i + 1)
print("ok " .. tostring(j) .. " - call f")
k = g(i, 3)
print("ok " .. tostring(k) .. " - call g")
local print = print
print("ok 9 - local")
-- Local Variables:
-- mode: lua
-- lua-indent-level: 4
-- fill-column: 100
-- End:
-- vim: ft=lua expandtab shiftwidth=4:
#! /usr/bin/lua
--
-- lua-Harness : <https://fperrad.frama.io/lua-Harness/>
--
-- Copyright (C) 2009-2018, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--
--[[
=head1 Lua if statement
=head2 Synopsis
% prove 001-if.t
=head2 Description
See section "Control Structures" in "Reference Manual"
L<https://www.lua.org/manual/5.1/manual.html#2.4.4>,
L<https://www.lua.org/manual/5.2/manual.html#3.3.4>,
L<https://www.lua.org/manual/5.3/manual.html#3.3.4>
=cut
]]
print("1..6")
if true then
print("ok 1")
else
print("not ok 1")
end
if not true then
print("not ok 2")
else
print("ok 2")
end
a = 12
b = 34
if a < b then
print("ok 3")
else
print("not ok 3")
end
a = 0
b = 4
if a < b then
print("ok 4")
elseif a == b then
print("not ok 4")
else
print("not ok 4")
end
a = 5
b = 5
if a < b then
print("not ok 5")
elseif a == b then
print("ok 5")
else
print("not ok 5")
end
a = 10
b = 6
if a < b then
print("not ok 6")
elseif a == b then
print("not ok 6")
else
print("ok 6")
end
-- Local Variables:
-- mode: lua
-- lua-indent-level: 4
-- fill-column: 100
-- End:
-- vim: ft=lua expandtab shiftwidth=4:
#! /usr/bin/lua
--
-- lua-Harness : <https://fperrad.frama.io/lua-Harness/>
--
-- Copyright (C) 2009-2018, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--
--[[
=head1 Lua table
=head2 Synopsis
% prove 002-table.t
=head2 Description
See section "Tables" in "Programming in Lua".
=cut
]]
print("1..8")
a = {"ok 1", "ok 2", "ok 3"}
print(a[1])
i = 2
print(a[i])
print(a[i+1])
if #a == 3 then
print("ok 4 - len")
else
print("not ok 4")
end
if a[7] == nil then
print("ok 5")
else
print("not ok 5")
end
t = {a=10, b=100}
if t['a'] == 10 then
print("ok 6")
else
print("not ok 6")
end
if t.b == 100 then
print("ok 7")
else
print("not ok 7")
end
if t.z == nil then
print("ok 8")
else
print("not ok 8")
end
-- Local Variables:
-- mode: lua
-- lua-indent-level: 4
-- fill-column: 100
-- End:
-- vim: ft=lua expandtab shiftwidth=4:
#! /usr/bin/lua
--
-- lua-Harness : <https://fperrad.frama.io/lua-Harness/>
--
-- Copyright (C) 2009-2018, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--
--[[
=head1 Lua while statement
=head2 Synopsis
% prove 011-while.t
=head2 Description
See section "Control Structures" in "Reference Manual"
L<https://www.lua.org/manual/5.1/manual.html#2.4.4>,
L<https://www.lua.org/manual/5.2/manual.html#3.3.4>,
L<https://www.lua.org/manual/5.3/manual.html#3.3.4>
=cut
]]
print("1..11")
do
local a = {}
local i = 1
while a[i] do
i = i + 1
end
if i == 1 then
print("ok 1 - while empty")
else
print("not ok 1 - " .. i)
end
end
do
local a = {"ok 2 - while ", "ok 3", "ok 4"}
local i = 1
while a[i] do
print(a[i])
i = i + 1
end
end
do
local a = {"ok 5 - with break", "ok 6", "stop", "more"}
local i = 1
while a[i] do
if a[i] == 'stop' then break end
print(a[i])
i = i + 1
end
if i == 3 then
print("ok 7 - break")
else
print("not ok 7 - " .. i)
end
end
do
local x = 3
local i = 1
while i<=x do
print("ok " .. tostring(7+i))
i = i + 1
end
if i == 4 then
print("ok 11")
else
print("not ok 11 - " .. tostring(i))
end
end
-- Local Variables:
-- mode: lua
-- lua-indent-level: 4
-- fill-column: 100
-- End:
-- vim: ft=lua expandtab shiftwidth=4:
#! /usr/bin/lua
--
-- lua-Harness : <https://fperrad.frama.io/lua-Harness/>
--
-- Copyright (C) 2009-2018, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--
--[[
=head1 Lua repeat statement
=head2 Synopsis
% prove 012-repeat.t
=head2 Description
See section "Control Structures" in "Reference Manual"
L<https://www.lua.org/manual/5.1/manual.html#2.4.4>,
L<https://www.lua.org/manual/5.2/manual.html#3.3.4>,
L<https://www.lua.org/manual/5.3/manual.html#3.3.4>
=cut
]]
print("1..8")
do
local a = {"ok 1 - repeat", "ok 2", "ok 3"}
local i = 0
repeat
i = i + 1
if a[i] then
print(a[i])
end
until not a[i]
if i == 4 then
print("ok 4")
else
print("not ok 4 - " .. i)
end
end
do
local a = {"ok 5 - with break", "ok 6", 'stop', 'more'}
local i = 0
repeat
i = i + 1
if a[i] == 'stop' then break end
print(a[i])
until not a[i]
if a[i] == 'stop' then
print("ok 7 - break")
else
print("not ok 7 - " .. a[i])
end
end
do
local function f () return true end
local i = 1
repeat
local v = f()
if i == 1 then
print("ok 8 - scope")
else
print("not ok")
break
end
i = i + 1
until v
end
-- Local Variables:
-- mode: lua
-- lua-indent-level: 4
-- fill-column: 100
-- End:
-- vim: ft=lua expandtab shiftwidth=4:
#! /usr/bin/lua
--
-- lua-Harness : <https://fperrad.frama.io/lua-Harness/>
--
-- Copyright (C) 2009-2018, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--
--[[
=head1 Lua for statement
=head2 Synopsis
% prove 014-fornum.t
=head2 Description
See section "For Statement" in "Reference Manual"
L<https://www.lua.org/manual/5.1/manual.html#2.4.5>,
L<https://www.lua.org/manual/5.2/manual.html#3.3.5>,
L<https://www.lua.org/manual/5.3/manual.html#3.3.5>
=cut
--]]
print("1..36")
for i = 1.0, 3.0, 0.5 do
print("ok " .. tostring(2*i-1) .. " - for 1.0, 3.0, 0.5")
end
for i = 1.0, 3.0, 0.5 do
local function f ()
print("ok " .. tostring(2*i+4) .. " - for 1.0, 3.0, 0.5 lex")
end
f()
end
local function f (i)
print("ok " .. tostring(2*i+9) .. " - for 1.0, 3.0, 0.5 !lex")
end
for i = 1.0, 3.0, 0.5 do
f(i)
end
for i = 3, 5 do
print("ok " .. tostring(13+i) .. " - for 3, 5")
i = i + 1
end
for i = 5, 1, -1 do
print("ok " .. tostring(24-i) .. " - for 5, 1, -1")
end
for i = 5, 5 do
print("ok " .. tostring(19+i) .. " - for 5, 5")
end
for i = 5, 5, -1 do
print("ok " .. tostring(20+i) .. " - for 5, 5, -1")
end
do
local v = false
for i = 5, 3 do
v = true
end
if v then
print("not ok 26 - for 5, 3")
else
print("ok 26 - for 5, 3")
end
end
do
local v = false
for i = 5, 7, -1 do
v = true
end
if v then
print("not ok 27 - for 5, 7, -1")
else
print("ok 27 - for 5, 7, -1")
end
end
do
local v = false
for i = 5, 7, 0 do
v = true
break -- avoid infinite loop with LuaJIT
end
if jit then
print("not ok 28 - for 5, 7, 0 # TODO # LuaJIT intentional.")
elseif v then
print("not ok 28 - for 5, 7, 0")
else
print("ok 28 - for 5, 7, 0")
end
end
do
local v = nil
for i = 1, 10, 2 do
if i > 4 then break end
print("ok " .. tostring((i+57)/2) .. " - for break")
v = i
end
if v == 3 then
print("ok 31 - break")
else
print("not ok 31 - " .. v)
end
end
do
local function first() return 1 end
local function limit() return 8 end
local function step() return 2 end
for i = first(), limit(), step() do
print("ok " .. tostring((i+63)/2) .. " - with functions")
end
end
do
local a = {}
for i = 1, 10 do
a[i] = function () return i end
end
local v = a[5]()
if v == 5 then
print("ok 36 - for & upval")
else
print("not ok 36 - for & upval")
print("#", v)
end
end
-- Local Variables:
-- mode: lua
-- lua-indent-level: 4
-- fill-column: 100
-- End:
-- vim: ft=lua expandtab shiftwidth=4:
#! /usr/bin/lua
--
-- lua-Harness : <https://fperrad.frama.io/lua-Harness/>
--
-- Copyright (C) 2009-2018, Perrad Francois
--
-- This code is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--
--[[
=head1 Lua for statement
=head2 Synopsis
% prove 015-forlist.t
=head2 Description
See section "For Statement" in "Reference Manual"
L<https://www.lua.org/manual/5.1/manual.html#2.4.5>,
L<https://www.lua.org/manual/5.2/manual.html#3.3.5>,
L<https://www.lua.org/manual/5.3/manual.html#3.3.5>
=cut
--]]
print("1..18")
do
local a = {"ok 1 - for ipairs", "ok 2 - for ipairs", "ok 3 - for ipairs"}
for _, v in ipairs(a) do
print(v)
end
for i, v in ipairs(a) do
print("ok " .. tostring(3+i) .. " - for ipairs")
end
for k in pairs(a) do
print("ok " .. tostring(6+k) .. " - for pairs")
end
end
do
local r = false
local t = {a=10, b=100}
for i, v in ipairs(t) do
print(i, v)
r = true
end
if r then
print("not ok 10 - for ipairs (hash)")
else
print("ok 10 - for ipairs (hash)")
end
local i = 1
for k in pairs(t) do
if k == 'a' or k == 'b' then
print("ok " .. tostring(10+i) .. " - for pairs (hash)")
else
print("not ok " .. tostring(10+i) .. " - " .. k)
end
i = i + 1
end
end
do
local a = {"ok 13 - for break", "ok 14 - for break", "stop", "more"}
local i
for _, v in ipairs(a) do
if v == "stop" then break end
print(v)
i = _
end
if i == 2 then
print("ok 15 - break")
else
print("not ok 15 - " .. i)
end
end
do
local a = {"ok 16 - for & upval", "ok 17 - for & upval", "ok 18 - for & upval"}
local b = {}
for i, v in ipairs(a) do
b[i] = function () return v end
end
for i, v in ipairs(a) do