Skip to content

Commit 813ee32

Browse files
committed
tests/lapi: add math tests
The patch adds a tests for Lua math functions.
1 parent d4b91f6 commit 813ee32

21 files changed

+676
-1
lines changed

tests/lapi/lib.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,27 @@ local function bitwise_op(op_name)
5656
end
5757
end
5858

59+
local function approx_equal(a, b, epsilon)
60+
local abs = math.abs
61+
return abs(a - b) <= ((abs(a) < abs(b) and abs(b) or abs(a)) * epsilon)
62+
end
63+
64+
local function definitely_lt(a, b, epsilon)
65+
local abs = math.abs
66+
return (b - a) > ((abs(a) < abs(b) and abs(b) or abs(a)) * epsilon)
67+
end
68+
69+
local function definitely_gt(a, b, epsilon)
70+
local abs = math.abs
71+
return (a - b) > ((abs(a) < abs(b) and abs(b) or abs(a)) * epsilon)
72+
end
73+
5974
return {
60-
lua_version = lua_version,
75+
approx_equal = approx_equal,
6176
bitwise_op = bitwise_op,
77+
definitely_gt = definitely_gt,
78+
definitely_lt= definitely_lt,
79+
lua_version = lua_version,
6280
MAX_INT64 = MAX_INT64,
6381
MIN_INT64 = MIN_INT64,
6482
MAX_INT = MAX_INT,

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+
local test_lib = require("lib")
19+
20+
local function TestOneInput(buf)
21+
local fdp = luzer.FuzzedDataProvider(buf)
22+
local n = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
23+
local abs = n
24+
if abs < 0 then
25+
abs = -abs
26+
end
27+
assert(math.abs(n) == abs)
28+
end
29+
30+
local args = {
31+
artifact_prefix = "math_abs_",
32+
}
33+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_acos_test.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
local test_lib = require("lib")
16+
17+
local function TestOneInput(buf)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local x = fdp:consume_number(-1, 1)
20+
local y = math.acos(x)
21+
assert(y >= 0)
22+
assert(y <= math.pi)
23+
if x ~= 0 then
24+
assert(test_lib.approx_equal(y, math.pi - math.acos(-x), 0.01))
25+
assert(test_lib.approx_equal(math.cos(y), x, 0.01))
26+
end
27+
end
28+
29+
local args = {
30+
artifact_prefix = "math_acos_",
31+
}
32+
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+
local test_lib = require("lib")
16+
17+
local function TestOneInput(buf)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local x = fdp:consume_number(-1, 1)
20+
local y = math.asin(x)
21+
assert(y >= -math.pi / 2)
22+
assert(y <= math.pi / 2)
23+
assert(test_lib.approx_equal(math.sin(y), x, 0.01))
24+
end
25+
26+
local args = {
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+
local test_lib = require("lib")
16+
17+
local function TestOneInput(buf)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local x = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
20+
local y = math.atan(x)
21+
assert(y >= -math.pi / 2)
22+
assert(y <= math.pi / 2)
23+
assert(math.atan(-x) == -y)
24+
end
25+
26+
local args = {
27+
artifact_prefix = "math_atan_",
28+
}
29+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_ceil_test.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
local test_lib = require("lib")
28+
29+
local function TestOneInput(buf)
30+
local fdp = luzer.FuzzedDataProvider(buf)
31+
local x = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
32+
local res = math.ceil(x)
33+
assert(type(res) == "number")
34+
end
35+
36+
local args = {
37+
artifact_prefix = "math_ceil_",
38+
}
39+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_cos_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.cos(x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
local test_lib = require("lib")
16+
17+
local function TestOneInput(buf)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local x = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
20+
local res = math.cos(x)
21+
assert(type(res) == "number")
22+
end
23+
24+
local args = {
25+
artifact_prefix = "math_cos_",
26+
}
27+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_deg_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.deg(x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
local test_lib = require("lib")
16+
17+
local function TestOneInput(buf)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local a = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
20+
local res = math.deg(a)
21+
assert(type(res) == "number")
22+
end
23+
24+
local args = {
25+
artifact_prefix = "math_deg_",
26+
}
27+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_exp_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.exp(x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
local test_lib = require("lib")
16+
17+
local function TestOneInput(buf)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local a = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
20+
local res = math.exp(a)
21+
assert(type(res) == "number")
22+
end
23+
24+
local args = {
25+
artifact_prefix = "math_exp_",
26+
}
27+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_floor_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.floor(x)
12+
]]
13+
14+
local luzer = require("luzer")
15+
local test_lib = require("lib")
16+
17+
local function TestOneInput(buf)
18+
local fdp = luzer.FuzzedDataProvider(buf)
19+
local a = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
20+
local res = math.floor(a)
21+
assert(type(res) == "number")
22+
end
23+
24+
local args = {
25+
artifact_prefix = "math_floor_",
26+
}
27+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)