Skip to content

Commit 2212660

Browse files
committed
tests/lapi: add math tests
The patch adds a tests for Lua math functions.
1 parent 4b0b59f commit 2212660

21 files changed

+622
-0
lines changed

tests/lapi/lib.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ end
4242
return {
4343
lua_version = lua_version,
4444
bitwise_op = bitwise_op,
45+
4546
MAX_INT = MAX_INT,
4647
MIN_INT = MIN_INT,
4748
MAX_INT64 = MAX_INT64,

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.MAX_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+
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+
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+
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.MAX_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: 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+
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.MAX_INT, test_lib.MAX_INT)
32+
math.ceil(x)
33+
end
34+
35+
local args = {
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+
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.MAX_INT, test_lib.MAX_INT)
20+
math.cos(x)
21+
end
22+
23+
local args = {
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+
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.MAX_INT, test_lib.MAX_INT)
20+
math.deg(a)
21+
end
22+
23+
local args = {
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+
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.MAX_INT, test_lib.MAX_INT)
20+
math.exp(a)
21+
end
22+
23+
local args = {
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+
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.MAX_INT, test_lib.MAX_INT)
20+
math.floor(a)
21+
end
22+
23+
local args = {
24+
artifact_prefix = "math_floor_",
25+
}
26+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)