diff --git a/tests/lapi/lib.lua b/tests/lapi/lib.lua index a1e6cfee..09933d44 100644 --- a/tests/lapi/lib.lua +++ b/tests/lapi/lib.lua @@ -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 @@ -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, diff --git a/tests/lapi/math_abs_test.lua b/tests/lapi/math_abs_test.lua new file mode 100644 index 00000000..62679608 --- /dev/null +++ b/tests/lapi/math_abs_test.lua @@ -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) diff --git a/tests/lapi/math_acos_test.lua b/tests/lapi/math_acos_test.lua new file mode 100644 index 00000000..5e595dd2 --- /dev/null +++ b/tests/lapi/math_acos_test.lua @@ -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) diff --git a/tests/lapi/math_asin_test.lua b/tests/lapi/math_asin_test.lua new file mode 100644 index 00000000..5e15cb16 --- /dev/null +++ b/tests/lapi/math_asin_test.lua @@ -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) diff --git a/tests/lapi/math_atan_test.lua b/tests/lapi/math_atan_test.lua new file mode 100644 index 00000000..b947d4e5 --- /dev/null +++ b/tests/lapi/math_atan_test.lua @@ -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) diff --git a/tests/lapi/math_ceil_test.lua b/tests/lapi/math_ceil_test.lua new file mode 100644 index 00000000..599bff3f --- /dev/null +++ b/tests/lapi/math_ceil_test.lua @@ -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) diff --git a/tests/lapi/math_cos_test.lua b/tests/lapi/math_cos_test.lua new file mode 100644 index 00000000..b77fedb5 --- /dev/null +++ b/tests/lapi/math_cos_test.lua @@ -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) diff --git a/tests/lapi/math_deg_test.lua b/tests/lapi/math_deg_test.lua new file mode 100644 index 00000000..26f87938 --- /dev/null +++ b/tests/lapi/math_deg_test.lua @@ -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) diff --git a/tests/lapi/math_exp_test.lua b/tests/lapi/math_exp_test.lua new file mode 100644 index 00000000..a6b47f5b --- /dev/null +++ b/tests/lapi/math_exp_test.lua @@ -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) diff --git a/tests/lapi/math_floor_test.lua b/tests/lapi/math_floor_test.lua new file mode 100644 index 00000000..ad6d034f --- /dev/null +++ b/tests/lapi/math_floor_test.lua @@ -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) diff --git a/tests/lapi/math_fmod_test.lua b/tests/lapi/math_fmod_test.lua new file mode 100644 index 00000000..759b7812 --- /dev/null +++ b/tests/lapi/math_fmod_test.lua @@ -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) diff --git a/tests/lapi/math_log_test.lua b/tests/lapi/math_log_test.lua new file mode 100644 index 00000000..30722956 --- /dev/null +++ b/tests/lapi/math_log_test.lua @@ -0,0 +1,70 @@ +--[[ +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 + +Two-parameter logarithm gives incorrect answers for matching inputs, +https://github.com/LuaJIT/LuaJIT/issues/1240 + +Synopsis: math.log(x [, b]) +]] + +local luzer = require("luzer") +local test_lib = require("lib") + +-- The function `math.pow()` has been deprecated in PUC Rio Lua +-- 5.3, see Lua 5.3 Reference Manual, 8.2 – Changes in the +-- Libraries. +-- +-- 1. https://www.lua.org/manual/5.3/manual.html +local pow +if test_lib.lua_current_version_ge_than(5, 3) then + pow = test_lib.math_pow +else + pow = math.pow +end + +local function TestOneInput(buf, _size) + local fdp = luzer.FuzzedDataProvider(buf) + -- The natural logarithm (base e) of x. If x is ±0, + -- returns -Infinity. If x < 0, returns NaN. + local x = fdp:consume_number(0, test_lib.MAX_INT) + local y = fdp:consume_number(0, test_lib.MAX_INT) + local b = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT) + if b < 0 or b == 1 then return -1 end + + local eps = 1^-10 + + -- Product rule. + assert(test_lib.approx_equal( + math.log(x * y, b), math.log(x, b) + math.log(y, b), eps)) + -- Quotient rule. + assert(test_lib.approx_equal( + math.log(x / y, b), math.log(x, b) - math.log(y, b), eps)) + -- Power rule. + assert(test_lib.approx_equal( + math.log(pow(x, y), b), y * math.log(x, b), eps)) + -- Inverse property of logarithm. + assert(test_lib.approx_equal(math.log(pow(b, x), b), x, eps)) + -- Inverse property of exponent. + assert(test_lib.approx_equal(pow(b, math.log(x, b)), x, eps)) + -- Zero rule. + assert(math.log(1, b) == 0) + -- Identity rule. + assert(test_lib.approx_equal(math.log(b, b), 1, eps)) + -- Change of base formula. + local log_b_y = math.log(b, y) + if log_b_y ~= 0 then + assert(test_lib.approx_equal(math.log(x, b), math.log(x, y) / log_b_y, eps)) + end +end + +local args = { + artifact_prefix = "math_log_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_modf_test.lua b/tests/lapi/math_modf_test.lua new file mode 100644 index 00000000..8a18d50b --- /dev/null +++ b/tests/lapi/math_modf_test.lua @@ -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 + +Don't compile math.modf() anymore, +https://github.com/LuaJIT/LuaJIT/commit/d75e2627 + +Synopsis: math.modf(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.modf(x) + assert(type(res) == "number") +end + +local args = { + artifact_prefix = "math_modf_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_pow_test.lua b/tests/lapi/math_pow_test.lua new file mode 100644 index 00000000..aa376292 --- /dev/null +++ b/tests/lapi/math_pow_test.lua @@ -0,0 +1,48 @@ +--[[ +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 + +Revert to trival pow() optimizations to prevent inaccuracies, +https://github.com/LuaJIT/LuaJIT/commit/96d6d503 + +Optimizations for power operator a^b, +https://github.com/LuaJIT/LuaJIT/issues/9 + +Fix pow() optimization inconsistencies, +https://github.com/LuaJIT/LuaJIT/commit/9512d5c1 + +Synopsis: math.pow(x, y) +]] + +local luzer = require("luzer") +local test_lib = require("lib") + +-- The function `math.pow()` has been deprecated in PUC Rio Lua 5.3, +-- see Lua 5.3 Reference Manual, 8.2 – Changes in the Libraries. +-- +-- 1. https://www.lua.org/manual/5.3/manual.html +local pow +if test_lib.lua_current_version_ge_than(5, 3) then + pow = test_lib.math_pow +else + pow = math.pow +end + +local function TestOneInput(buf, _size) + 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 = pow(x, y) + assert(type(res) == "number") +end + +local args = { + artifact_prefix = "math_pow_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_rad_test.lua b/tests/lapi/math_rad_test.lua new file mode 100644 index 00000000..ea871f9a --- /dev/null +++ b/tests/lapi/math_rad_test.lua @@ -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.rad(x) +]] + +local luzer = require("luzer") +local test_lib = require("lib") + +local function TestOneInput(buf, _size) + local fdp = luzer.FuzzedDataProvider(buf) + local x = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT) + local res = math.rad(x) + assert(type(res) == "number") +end + +local args = { + artifact_prefix = "math_rad_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_randomseed_test.lua b/tests/lapi/math_randomseed_test.lua new file mode 100644 index 00000000..1703c200 --- /dev/null +++ b/tests/lapi/math_randomseed_test.lua @@ -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.randomseed([x [, y]]) +--]=====] + +local luzer = require("luzer") +local test_lib = require("lib") + +local function TestOneInput(buf) + local fdp = luzer.FuzzedDataProvider(buf) + local x = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT) + local y = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT) + -- Since Lua 5.4 the function returns the two seed components + -- that were effectively used, so that setting them again + -- repeats the sequence. + local a, b = math.randomseed(x, y) + if test_lib.lua_current_version_ge_than(5, 4) then + assert(type(a) == "number" and type(b) == "number") + end +end + +local args = { + artifact_prefix = "math_randomseed_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_sin_test.lua b/tests/lapi/math_sin_test.lua new file mode 100644 index 00000000..34fed20c --- /dev/null +++ b/tests/lapi/math_sin_test.lua @@ -0,0 +1,40 @@ +--[[ +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.sin(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 sin_x = math.sin(x) + assert(type(sin_x) == "number") + assert(sin_x >= -1 and sin_x <= 1) + local cos_x = math.cos(x) + local epsilon = 1^-10 + assert(test_lib.approx_equal(cos_x^2 + sin_x^2, 1, epsilon)) + + local n = fdp:consume_number(0, 100) + -- Calculate the functions of the form `sin(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.sin(i * math.pi - x)), math.abs(sin_x), epsilon)) + end +end + +local args = { + artifact_prefix = "math_sin_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_sqrt_test.lua b/tests/lapi/math_sqrt_test.lua new file mode 100644 index 00000000..79ead101 --- /dev/null +++ b/tests/lapi/math_sqrt_test.lua @@ -0,0 +1,41 @@ +--[[ +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 + +Sqrt(x) and x^0.5 not interchangeable, +https://github.com/LuaJIT/LuaJIT/issues/684 + +Synopsis: math.sqrt(x) + +See properties in +"ESA/390 Enhanced Floating Point Support: An Overview" +(The Facts of Floating Point Arithmetic) [1]. + +1. http://ftpmirror.your.org/pub/misc/ftp.software.ibm.com/software/websphere/awdtools/hlasm/sh93fpov.pdf +]] + +local luzer = require("luzer") +local test_lib = require("lib") + +local function TestOneInput(buf) + local fdp = luzer.FuzzedDataProvider(buf) + local x = fdp:consume_number(0, test_lib.MAX_INT) + assert(type(x) == "number") + -- Note, `math.sqrt(x)` and `x^0.5` are not interchangeable, + -- see [1]. + -- + -- 1. https://github.com/LuaJIT/LuaJIT/issues/684#issuecomment-822427297 + local epsilon = 1^-10 + assert(test_lib.approx_equal(math.sqrt(x), x^0.5, epsilon)) +end + +local args = { + artifact_prefix = "math_sqrt_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_tan_test.lua b/tests/lapi/math_tan_test.lua new file mode 100644 index 00000000..08174f83 --- /dev/null +++ b/tests/lapi/math_tan_test.lua @@ -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.tan(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 sin_x = math.sin(x) + local cos_x = math.cos(x) + local tan_x = math.tan(x) + + local epsilon = 1^-10 + if cos_x ~= 0 then + assert(test_lib.approx_equal(tan_x, sin_x / cos_x, epsilon)) + end +end + +local args = { + artifact_prefix = "math_tan_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_tointeger_test.lua b/tests/lapi/math_tointeger_test.lua new file mode 100644 index 00000000..d048f2eb --- /dev/null +++ b/tests/lapi/math_tointeger_test.lua @@ -0,0 +1,36 @@ +--[[ +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.tointeger(x) +]] + +local luzer = require("luzer") +local test_lib = require("lib") + +-- The function `math.tointeger()` is available since Lua 5.3. +if test_lib.lua_current_version_lt_than(5, 3) then + print("Unsupported version.") + os.exit(0) +end + +local function TestOneInput(buf) + local fdp = luzer.FuzzedDataProvider(buf) + local max_len = fdp:consume_integer(0, test_lib.MAX_INT) + local x = fdp:consume_string(max_len) + local res = math.tointeger(x) + assert(type(res) == "number" or + res == "fail" or + res == nil) +end + +local args = { + artifact_prefix = "math_tointeger_", +} +luzer.Fuzz(TestOneInput, nil, args) diff --git a/tests/lapi/math_ult_test.lua b/tests/lapi/math_ult_test.lua new file mode 100644 index 00000000..00aa05e2 --- /dev/null +++ b/tests/lapi/math_ult_test.lua @@ -0,0 +1,34 @@ +--[[ +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.ult(m, n) +]] + +local luzer = require("luzer") +local test_lib = require("lib") + +-- The function `math.ult()` is available since Lua 5.3. +if test_lib.lua_current_version_lt_than(5, 3) then + print("Unsupported version.") + os.exit(0) +end + +local function TestOneInput(buf) + local fdp = luzer.FuzzedDataProvider(buf) + local m = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT) + local n = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT) + local res = math.ult(m, n) + assert(type(res) == "boolean") +end + +local args = { + artifact_prefix = "math_ult_", +} +luzer.Fuzz(TestOneInput, nil, args)