Skip to content

Commit c49fc1a

Browse files
committed
tests/lapi: table tests
1 parent 0508724 commit c49fc1a

14 files changed

+388
-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_clear_test.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
Problem of HREFK with table.clear,
6+
https://github.com/LuaJIT/LuaJIT/issues/792
7+
8+
Double-emitting of IR_NEWREF for the same key on the snap replay,
9+
https://github.com/LuaJIT/LuaJIT/issues/1128
10+
11+
X86/X64 load fusion conflict detection doesn't detect table.clear,
12+
https://github.com/LuaJIT/LuaJIT/issues/1117
13+
14+
Synopsis: table.clear(tab)
15+
]]
16+
17+
local luzer = require("luzer")
18+
local test_lib = require("lib")
19+
20+
-- PUC Rio Lua only.
21+
if test_lib.lua_version() == "LuaJIT" then
22+
os.exit(0)
23+
end
24+
25+
local function TestOneInput(buf)
26+
local fdp = luzer.FuzzedDataProvider(buf)
27+
local tbl = test_lib.random_table(fdp)
28+
table.clear(tbl)
29+
end
30+
31+
local args = {
32+
artifact_prefix = "table_clear_",
33+
}
34+
luzer.Fuzz(TestOneInput, nil, args)

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
os.exit(0)
18+
end
19+
20+
local function TestOneInput(buf)
21+
-- New function 'table.create',
22+
-- https://github.com/lua/lua/commit/3e9dbe143d3338f5f13a5e421ea593adff482da0
23+
local fdp = luzer.FuzzedDataProvider(buf)
24+
local len = fdp:consume_integer(0, MAX_INT)
25+
local tbl = table.create(len) -- luacheck: no unused
26+
tbl = nil
27+
collectgarbage()
28+
collectgarbage()
29+
end
30+
31+
local args = {
32+
artifact_prefix = "table_create_",
33+
}
34+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/table_foreach_test.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
local function TestOneInput(buf, _size)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local tbl = test_lib.random_table(fdp)
20+
local fn = function() end
21+
table.foreach(tbl, fn)
22+
end
23+
24+
local args = {
25+
artifact_prefix = "table_foreach_",
26+
}
27+
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
os.exit(0)
14+
end
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local tbl = test_lib.random_table(fdp)
19+
table.maxn(tbl)
20+
end
21+
22+
local args = {
23+
artifact_prefix = "table_maxn_",
24+
}
25+
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
os.exit(0)
28+
end
29+
30+
local function TestOneInput(buf)
31+
local fdp = luzer.FuzzedDataProvider(buf)
32+
local nhash = fdp:consume_integer(0, MAX_INT)
33+
local narray = fdp:consume_integer(0, MAX_INT)
34+
table.new(narray, nhash)
35+
end
36+
37+
local args = {
38+
artifact_prefix = "table_new_",
39+
}
40+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)