Skip to content

Commit 90bacf6

Browse files
committed
tests/lapi: add math tests
The patch adds a tests for Lua math functions.
1 parent 425cd57 commit 90bacf6

21 files changed

+670
-1
lines changed

tests/lapi/lib.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,27 @@ local function bitwise_op(op_name)
5050
end
5151
end
5252

53+
local function approx_equal(a, b, epsilon)
54+
local abs = math.abs
55+
return abs(a - b) <= ((abs(a) < abs(b) and abs(b) or abs(a)) * epsilon)
56+
end
57+
58+
local function definitely_lt(a, b, epsilon)
59+
local abs = math.abs
60+
return (b - a) > ((abs(a) < abs(b) and abs(b) or abs(a)) * epsilon)
61+
end
62+
63+
local function definitely_gt(a, b, epsilon)
64+
local abs = math.abs
65+
return (a - b) > ((abs(a) < abs(b) and abs(b) or abs(a)) * epsilon)
66+
end
67+
5368
return {
54-
lua_version = lua_version,
69+
approx_equal = approx_equal,
5570
bitwise_op = bitwise_op,
71+
definitely_gt = definitely_gt,
72+
definitely_lt= definitely_lt,
73+
lua_version = lua_version,
5674
MAX_INT64 = MAX_INT64,
5775
MIN_INT64 = MIN_INT64,
5876
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: 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.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+
-- FIXME: assert(y <= math.pi)
22+
-- FIXME: assert(y == math.pi - math.acos(-x))
23+
-- FIXME: assert(math.cos(y) == x)
24+
end
25+
26+
local args = {
27+
artifact_prefix = "math_acos_",
28+
}
29+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/math_asin_test.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
-- FIXME: assert(math.sin(y) == x)
23+
end
24+
25+
local args = {
26+
artifact_prefix = "math_asin_",
27+
}
28+
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)