Skip to content

Commit 701a6a4

Browse files
committed
tests/lapi: table tests
1 parent bfac9d7 commit 701a6a4

13 files changed

+365
-0
lines changed

.luacheckrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ files["tests/capi/luaL_loadbuffer_proto/preamble.lua"] = {
55
"211",
66
},
77
}
8+
9+
globals = {
10+
table = {
11+
fields = { "create" }
12+
}
13+
}

tests/lapi/lib.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ local function random_locale(fdp)
9696
return fdp:oneof(locales)
9797
end
9898

99+
local function random_table(fdp)
100+
local max = fdp:consume_integer(0, MAX_INT64)
101+
local t = {}
102+
for i = 1, max do
103+
t[i] = i
104+
end
105+
return t
106+
end
107+
99108
return {
100109
approx_equal = approx_equal,
101110
bitwise_op = bitwise_op,
@@ -111,4 +120,5 @@ return {
111120

112121
-- FDP.
113122
random_locale = random_locale,
123+
random_table = random_table,
114124
}

tests/lapi/table_concat_test.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--[=[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.6 – Table Manipulation
6+
https://www.lua.org/manual/5.1/manual.html#5.5
7+
8+
Infinite loop on table lookup,
9+
https://github.com/LuaJIT/LuaJIT/issues/494
10+
11+
Synopsis: table.concat(list [, sep [, i [, j]]])
12+
--]=]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf, _size)
17+
local len = string.len(buf)
18+
local tbl = {}
19+
buf:gsub(".", function(c)
20+
local pos_end = table.getn(tbl)
21+
table.insert(tbl, pos_end + 1, c)
22+
assert(tbl[pos_end + 1] == c)
23+
end)
24+
assert(table.getn(tbl), len)
25+
assert(buf == table.concat(tbl))
26+
end
27+
28+
local args = {
29+
artifact_prefix = "table_concat_",
30+
}
31+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/table_create_test.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.6 – Table Manipulation
6+
https://www.lua.org/manual/5.1/manual.html#5.5
7+
8+
Synopsis:
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MAX_INT = test_lib.MAX_INT
14+
15+
-- PUC Rio Lua only.
16+
if test_lib.lua_version() == "LuaJIT" then
17+
print("Unsupported version.")
18+
os.exit(0)
19+
end
20+
21+
local function TestOneInput(buf)
22+
-- New function 'table.create',
23+
-- https://github.com/lua/lua/commit/3e9dbe143d3338f5f13a5e421ea593adff482da0
24+
local fdp = luzer.FuzzedDataProvider(buf)
25+
local len = fdp:consume_integer(0, MAX_INT)
26+
local tbl = table.create(len) -- luacheck: no unused
27+
tbl = nil
28+
collectgarbage()
29+
collectgarbage()
30+
end
31+
32+
local args = {
33+
artifact_prefix = "table_create_",
34+
}
35+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/table_foreach_test.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
Bad loop initialization in table.foreach(),
6+
https://github.com/LuaJIT/LuaJIT/issues/844
7+
8+
string.dump(table.foreach) will trigger an assert,
9+
https://github.com/LuaJIT/LuaJIT/issues/1038
10+
11+
Synopsis:
12+
]]
13+
14+
local luzer = require("luzer")
15+
local test_lib = require("lib")
16+
17+
-- Function `table.foreach` is deprecated in Lua 5.1.
18+
if test_lib.lua_current_version_ge_than(5, 1) then
19+
print("Unsupported version.")
20+
os.exit(0)
21+
end
22+
23+
local function TestOneInput(buf, _size)
24+
local fdp = luzer.FuzzedDataProvider(buf)
25+
local tbl = test_lib.random_table(fdp)
26+
local fn = function() end
27+
table.foreach(tbl, fn)
28+
end
29+
30+
local args = {
31+
artifact_prefix = "table_foreach_",
32+
}
33+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/table_insert_test.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2024, Sergey Bronnikov.
4+
5+
6.6 – Table Manipulation
6+
https://www.lua.org/manual/5.1/manual.html#5.5
7+
8+
Synopsis: table.insert (list, [pos,] value)
9+
]]
10+
11+
local luzer = require("luzer")
12+
13+
local function TestOneInput(buf, _size)
14+
local len = string.len(buf)
15+
local tbl = {}
16+
buf:gsub(".", function(c)
17+
local pos_end = table.getn(tbl)
18+
table.insert(tbl, pos_end, c)
19+
-- FIXME
20+
-- assert(tbl[pos_end + 1] == c)
21+
end)
22+
assert(table.getn(tbl), len)
23+
-- FIXME
24+
-- assert(buf == table.concat(tbl))
25+
end
26+
27+
local args = {
28+
artifact_prefix = "table_insert_",
29+
}
30+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/table_maxn_test.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
Synopsis:
6+
]]
7+
8+
local luzer = require("luzer")
9+
local test_lib = require("lib")
10+
11+
-- LuaJIT only.
12+
if test_lib.lua_version() ~= "LuaJIT" then
13+
print("Unsupported version.")
14+
os.exit(0)
15+
end
16+
17+
local function TestOneInput(buf)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local tbl = test_lib.random_table(fdp)
20+
table.maxn(tbl)
21+
end
22+
23+
local args = {
24+
artifact_prefix = "table_maxn_",
25+
}
26+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/table_move_test.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.6 – Table Manipulation
6+
https://www.lua.org/manual/5.1/manual.html#5.5
7+
8+
Synopsis: table.move(a1, f, e, t [,a2])
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
14+
local function TestOneInput(buf, _size)
15+
local fdp = luzer.FuzzedDataProvider(buf)
16+
local tbl = test_lib.random_table(fdp)
17+
table.move(tbl, 1, #tbl, 1, {})
18+
end
19+
20+
local args = {
21+
artifact_prefix = "table_move_",
22+
}
23+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/table_new_test.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
lua_createtable()
6+
7+
Creates a new empty table and pushes it onto the stack. The new
8+
table has space pre-allocated for narr array elements and nrec
9+
non-array elements. This pre-allocation is useful when you know
10+
exactly how many elements the table will have.
11+
12+
http://www.lua.org/manual/5.1/manual.html
13+
https://github.com/LuaJIT/LuaJIT/blob/8635cbabf3094c4d8bd00578c7d812bea87bb2d3/doc/extensions.html#L210-L216
14+
15+
Bad lookup generated by lj_record_idx in GC64,
16+
https://github.com/LuaJIT/LuaJIT/issues/840
17+
18+
Synopsis: table.new(narray, nhash)
19+
]]
20+
21+
local luzer = require("luzer")
22+
local test_lib = require("lib")
23+
local MAX_INT = test_lib.MAX_INT
24+
25+
-- PUC Rio Lua only.
26+
if test_lib.lua_version() == "LuaJIT" then
27+
print("Unsupported version.")
28+
os.exit(0)
29+
end
30+
31+
local function TestOneInput(buf)
32+
local fdp = luzer.FuzzedDataProvider(buf)
33+
local nhash = fdp:consume_integer(0, MAX_INT)
34+
local narray = fdp:consume_integer(0, MAX_INT)
35+
table.new(narray, nhash)
36+
end
37+
38+
local args = {
39+
artifact_prefix = "table_new_",
40+
}
41+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/table_pack_test.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.6 – Table Manipulation
6+
https://www.lua.org/manual/5.1/manual.html#5.5
7+
8+
Synopsis: table.pack(...)
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
14+
-- PUC Rio Lua only.
15+
if test_lib.lua_version() == "LuaJIT" then
16+
print("Unsupported version.")
17+
os.exit(0)
18+
end
19+
20+
local function TestOneInput(buf, _size)
21+
local fdp = luzer.FuzzedDataProvider(buf)
22+
local tbl = test_lib.random_table(fdp)
23+
table.pack(unpack(tbl))
24+
end
25+
26+
local args = {
27+
artifact_prefix = "table_pack_",
28+
}
29+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)