Skip to content

tests/lapi: add math tests #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions tests/lapi/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,31 @@ Test helpers.

-- The function determines a Lua version.
local function lua_version()
local major, minor = _VERSION:match("([%d]+)%.(%d+)")
local version = {
major = tonumber(major),
minor = tonumber(minor),
}
local is_luajit, _ = pcall(require, "jit")
if is_luajit then
return "LuaJIT"
local lua_name = is_luajit and "LuaJIT" or "PUC Rio Lua"
return lua_name, version
end

local function version_ge(version1, version2)
if version1.major ~= version2.major then
return version1.major > version2.major
else
return version1.minor >= version2.minor
end
end

return _VERSION
local function lua_current_version_ge_than(major, minor)
local _, current_version = lua_version()
return version_ge(current_version, { major = major, minor = minor })
end

local function lua_current_version_lt_than(major, minor)
return not lua_current_version_ge_than(major, minor)
end

-- By default `lua_Integer` is ptrdiff_t in Lua 5.1 and Lua 5.2
Expand Down Expand Up @@ -56,9 +75,22 @@ local function bitwise_op(op_name)
end
end

local function math_pow(x, y)
return x ^ y
end

local function approx_equal(a, b, epsilon)
local abs = math.abs
return abs(a - b) <= ((abs(a) < abs(b) and abs(b) or abs(a)) * epsilon)
end

return {
lua_version = lua_version,
approx_equal = approx_equal,
bitwise_op = bitwise_op,
lua_version = lua_version,
lua_current_version_ge_than = lua_current_version_ge_than,
lua_current_version_lt_than = lua_current_version_lt_than,
math_pow = math_pow,
MAX_INT64 = MAX_INT64,
MIN_INT64 = MIN_INT64,
MAX_INT = MAX_INT,
Expand Down
33 changes: 33 additions & 0 deletions tests/lapi/math_abs_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Misleading assertion in asm_fload() for mips,
https://github.com/LuaJIT/LuaJIT/issues/1043

Synopsis: math.abs(x)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local n = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local abs = n
if abs < 0 then
abs = -abs
end
assert(math.abs(n) == abs)
end

local args = {
artifact_prefix = "math_abs_",
}
luzer.Fuzz(TestOneInput, nil, args)
33 changes: 33 additions & 0 deletions tests/lapi/math_acos_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Synopsis: math.acos(x)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local x = fdp:consume_number(-1, 1)
local y = math.acos(x)
assert(y >= 0)
assert(y <= math.pi)
local epsilon = 1^-10
if x ~= 0 then
assert(test_lib.approx_equal(y, math.pi - math.acos(-x), epsilon))
assert(test_lib.approx_equal(math.cos(y), x, epsilon))
end
end

local args = {
artifact_prefix = "math_acos_",
}
luzer.Fuzz(TestOneInput, nil, args)
31 changes: 31 additions & 0 deletions tests/lapi/math_asin_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Synopsis: math.asin(x)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local x = fdp:consume_number(-1, 1)
local y = math.asin(x)
assert(type(y) == "number")
assert(y >= -math.pi / 2)
assert(y <= math.pi / 2)
local epsilon = 1^-10
assert(test_lib.approx_equal(math.sin(y), x, epsilon))
end

local args = {
artifact_prefix = "math_asin_",
}
luzer.Fuzz(TestOneInput, nil, args)
30 changes: 30 additions & 0 deletions tests/lapi/math_atan_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Synopsis: math.atan(y [, x])
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local x = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local y = math.atan(x)
assert(type(y) == "number")
assert(y >= -math.pi / 2)
assert(y <= math.pi / 2)
assert(math.atan(-x) == -y)
end

local args = {
artifact_prefix = "math_atan_",
}
luzer.Fuzz(TestOneInput, nil, args)
39 changes: 39 additions & 0 deletions tests/lapi/math_ceil_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

vm_mips.dasc assumes 32-bit FPU register model,
https://github.com/LuaJIT/LuaJIT/issues/1040

math.ceil fails to return -0 for -1 < x < -0.5,
https://github.com/LuaJIT/LuaJIT/issues/859

ARM64 - corrupted local variable on trace exit / snapshot replay,
https://github.com/LuaJIT/LuaJIT/issues/579

x86/x64: Fix math.ceil(-0.9) result sign,
https://github.com/LuaJIT/LuaJIT/issues/859

Synopsis: math.ceil(x)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local x = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local res = math.ceil(x)
assert(type(res) == "number")
end

local args = {
artifact_prefix = "math_ceil_",
}
luzer.Fuzz(TestOneInput, nil, args)
38 changes: 38 additions & 0 deletions tests/lapi/math_cos_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Synopsis: math.cos(x)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local x = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local cos_x = math.cos(x)
assert(type(cos_x) == "number")
assert(cos_x >= -1 and cos_x <= 1)
local epsilon = 1^-10

local n = fdp:consume_number(0, 100)
-- Calculate the functions of the form `cos(i*pi - x), where
-- i = 1, 3, 5, etc. These functions are equivalent, given the
-- trigonometric identity.
for i = 1, n do
assert(test_lib.approx_equal(
math.abs(math.cos(i * math.pi - x)), math.abs(cos_x), epsilon))
end
end

local args = {
artifact_prefix = "math_cos_",
}
luzer.Fuzz(TestOneInput, nil, args)
27 changes: 27 additions & 0 deletions tests/lapi/math_deg_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Synopsis: math.deg(x)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local a = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local res = math.deg(a)
assert(type(res) == "number")
end

local args = {
artifact_prefix = "math_deg_",
}
luzer.Fuzz(TestOneInput, nil, args)
27 changes: 27 additions & 0 deletions tests/lapi/math_exp_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Synopsis: math.exp(x)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local a = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local res = math.exp(a)
assert(type(res) == "number")
end

local args = {
artifact_prefix = "math_exp_",
}
luzer.Fuzz(TestOneInput, nil, args)
27 changes: 27 additions & 0 deletions tests/lapi/math_floor_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Synopsis: math.floor(x)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local a = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local res = math.floor(a)
assert(type(res) == "number")
end

local args = {
artifact_prefix = "math_floor_",
}
luzer.Fuzz(TestOneInput, nil, args)
28 changes: 28 additions & 0 deletions tests/lapi/math_fmod_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

18 – The Mathematical Library
https://www.lua.org/pil/18.html

6.7 – Mathematical Functions
https://www.lua.org/manual/5.3/manual.html#6.7

Synopsis: math.fmod(x, y)
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local x = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local y = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
local res = math.fmod(x, y)
assert(type(res) == "number")
end

local args = {
artifact_prefix = "math_fmod_",
}
luzer.Fuzz(TestOneInput, nil, args)
Loading