Skip to content

Commit 24fe535

Browse files
committed
tests/lapi: add math tests
The patch adds a fuzzing tests for Lua math functions.
1 parent 2a1da10 commit 24fe535

20 files changed

+625
-0
lines changed

tests/lapi/math_abs_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+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Misleading assertion in asm_fload() for mips,
12+
https://github.com/LuaJIT/LuaJIT/issues/1043
13+
14+
Synopsis: math.abs (x)
15+
]]
16+
17+
local luzer = require("luzer")
18+
19+
local function TestOneInput(buf)
20+
local fdp = luzer.FuzzedDataProvider(buf)
21+
local n = fdp:consume_number(-10^53, 10^53)
22+
local abs = n
23+
if abs < 0 then
24+
abs = -abs
25+
end
26+
assert(math.abs(n) == abs)
27+
end
28+
29+
local args = {
30+
max_len = 4096,
31+
artifact_prefix = "math_abs_",
32+
}
33+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_acos_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-2025, Sergey Bronnikov.
4+
5+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Synopsis: math.acos (x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local x = fdp:consume_number(-1, 1)
19+
local y = math.acos(x)
20+
assert(y >= 0)
21+
assert(y <= math.pi)
22+
assert(y == math.pi - math.acos(-x))
23+
assert(math.cos(y) == x)
24+
end
25+
26+
local args = {
27+
max_len = 4096,
28+
artifact_prefix = "math_acos_",
29+
}
30+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_asin_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+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Synopsis: math.asin (x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local x = fdp:consume_number(-1, 1)
19+
local y = math.asin(x)
20+
assert(y >= -math.pi / 2)
21+
assert(y <= math.pi / 2)
22+
assert(math.sin(y) == x)
23+
end
24+
25+
local args = {
26+
max_len = 4096,
27+
artifact_prefix = "math_asin_",
28+
}
29+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_atan_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+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Synopsis: math.atan (y [, x])
12+
]]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local x = fdp:consume_number(-10^53, 10^53)
19+
local y = math.atan(x)
20+
assert(y >= -math.pi / 2)
21+
assert(y <= math.pi / 2)
22+
assert(math.atan(-x) == -y)
23+
end
24+
25+
local args = {
26+
max_len = 4096,
27+
artifact_prefix = "math_atan_",
28+
}
29+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_ceil_test.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
vm_mips.dasc assumes 32-bit FPU register model,
12+
https://github.com/LuaJIT/LuaJIT/issues/1040
13+
14+
math.ceil fails to return -0 for -1 < x < -0.5,
15+
https://github.com/LuaJIT/LuaJIT/issues/859
16+
17+
ARM64 - corrupted local variable on trace exit / snapshot replay,
18+
https://github.com/LuaJIT/LuaJIT/issues/579
19+
20+
x86/x64: Fix math.ceil(-0.9) result sign,
21+
https://github.com/LuaJIT/LuaJIT/issues/859
22+
23+
Synopsis: math.ceil (x)
24+
]]
25+
26+
local luzer = require("luzer")
27+
28+
local function TestOneInput(buf)
29+
local fdp = luzer.FuzzedDataProvider(buf)
30+
local x = fdp:consume_number(-10^53, 10^53)
31+
math.ceil(x)
32+
end
33+
34+
local args = {
35+
max_len = 4096,
36+
artifact_prefix = "math_ceil_",
37+
}
38+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_cos_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+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Synopsis: math.cos (x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local x = fdp:consume_number(-10^53, 10^53)
19+
math.cos(x)
20+
end
21+
22+
local args = {
23+
max_len = 4096,
24+
artifact_prefix = "math_cos_",
25+
}
26+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_deg_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+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Synopsis: math.deg (x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local a = fdp:consume_number(-10^53, 10^53)
19+
math.deg(a)
20+
end
21+
22+
local args = {
23+
max_len = 4096,
24+
artifact_prefix = "math_deg_",
25+
}
26+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_exp_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+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Synopsis: math.exp (x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local a = fdp:consume_number(-10^53, 10^53)
19+
math.exp(a)
20+
end
21+
22+
local args = {
23+
max_len = 4096,
24+
artifact_prefix = "math_exp_",
25+
}
26+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_floor_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+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Synopsis: math.floor (x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local a = fdp:consume_number(-10^53, 10^53)
19+
math.floor(a)
20+
end
21+
22+
local args = {
23+
max_len = 4096,
24+
artifact_prefix = "math_floor_",
25+
}
26+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_fmod_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+
18 – The Mathematical Library
6+
https://www.lua.org/pil/18.html
7+
8+
6.7 – Mathematical Functions
9+
https://www.lua.org/manual/5.3/manual.html#6.7
10+
11+
Synopsis: math.fmod (x, y)
12+
]]
13+
14+
local luzer = require("luzer")
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local x = fdp:consume_number(-10^53, 10^53)
19+
local y = fdp:consume_number(-10^53, 10^53)
20+
math.fmod(x, y)
21+
end
22+
23+
local args = {
24+
max_len = 4096,
25+
artifact_prefix = "math_fmod_",
26+
}
27+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)