| 1 |
-- fpconv tests |
| 2 |
|
| 3 |
context("Fpconv printf functions", function() |
| 4 |
local ok, ffi = pcall(require, "ffi") |
| 5 |
local is_luajit_ffi = ok |
| 6 |
if not ok then |
| 7 |
ffi = require("cffi") |
| 8 |
end |
| 9 |
|
| 10 |
-- cffi-lua doesn't support variadic functions properly, skip these tests |
| 11 |
if not is_luajit_ffi then |
| 12 |
test("Skipped: cffi-lua does not support variadic functions", function() |
| 13 |
-- This test suite requires LuaJIT FFI for variadic function support |
| 14 |
end) |
| 15 |
return |
| 16 |
end |
| 17 |
local niter_fuzz = 100000 |
| 18 |
local function small_double() |
| 19 |
return math.random() |
| 20 |
end |
| 21 |
local function large_double() |
| 22 |
return math.random() * math.random(2^52) |
| 23 |
end |
| 24 |
local function huge_double() |
| 25 |
return math.random(2^52) * math.random(2^52) |
| 26 |
end |
| 27 |
local function tiny_double() |
| 28 |
return math.random() / math.random(2^52) |
| 29 |
end |
| 30 |
ffi.cdef[[ |
| 31 |
int snprintf(char *str, size_t size, const char *format, ...); |
| 32 |
long rspamd_snprintf(char *str, size_t size, const char *format, ...); |
| 33 |
long rspamd_printf(const char *format, ...); |
| 34 |
]] |
| 35 |
local benchmarks = { |
| 36 |
{'tiny fixed', small_double, '%f'}, |
| 37 |
{'small fixed', tiny_double, '%f'}, |
| 38 |
{'large fixed', large_double, '%.3f'}, |
| 39 |
{'huge fixed', huge_double, '%.3f'}, |
| 40 |
{'tiny scientific', small_double, '%g'}, |
| 41 |
{'small scientific', tiny_double, '%g'}, |
| 42 |
{'large scientific', large_double, '%g'}, |
| 43 |
{'huge scientific', huge_double, '%g'}, |
| 44 |
} |
| 45 |
|
| 46 |
local generic = { |
| 47 |
{0, '%f', '0'}, |
| 48 |
{0, '%.1f', '0.0'}, |
| 49 |
{0, '%.2f', '0.00'}, |
| 50 |
{0, '%.32f', '0.000000000000000000000000000'}, -- max |
| 51 |
{0, '%.150f', '0.000000000000000000000000000'}, -- too large |
| 52 |
{1/3, '%f', '0.3333333333333333'}, |
| 53 |
{1/3, '%.1f', '0.3'}, |
| 54 |
{1/3, '%.2f', '0.33'}, |
| 55 |
{-1/3, '%.32f', '-0.333333333333333300000000000'}, |
| 56 |
{-1/3, '%.150f', '-0.333333333333333300000000000'}, |
| 57 |
{-3.6817595395344857e-68, '%f', '-3.6817595395344857e-68'}, |
| 58 |
{3.5844466002796428e+298, '%f', '3.5844466002796428e+298'}, |
| 59 |
{9223372036854775808, '%f', '9223372036854776000'}, -- 2^63 with precision lost |
| 60 |
{2^50 + 0.2, '%f', '1125899906842624.3'}, -- 2^50 with precision lost |
| 61 |
{2^50 + 0.2, '%.2f', '1125899906842624.30'}, -- 2^50 with precision lost |
| 62 |
{-3.6817595395344857e-68, '%.3f', '-0.000'}, -- not enough precision |
| 63 |
{3.5844466002796428e+298, '%.3f', '3.5844466002796428e+298'}, |
| 64 |
{9223372036854775808, '%.3f', '9223372036854776000.000'}, -- 2^63 with precision lost |
| 65 |
{math.huge, '%f', 'inf'}, |
| 66 |
{-math.huge, '%f', '-inf'}, |
| 67 |
{0.0/0.0, '%f', 'nan'}, |
| 68 |
{math.huge, '%.1f', 'inf'}, |
| 69 |
{-math.huge, '%.2f', '-inf'}, |
| 70 |
{0.0/0.0, '%.3f', 'nan'}, |
| 71 |
{math.huge, '%g', 'inf'}, |
| 72 |
{-math.huge, '%g', '-inf'}, |
| 73 |
{0.0/0.0, '%g', 'nan'}, |
| 74 |
} |
| 75 |
|
| 76 |
local buf = ffi.new("char[64]") |
| 77 |
local buf2 = ffi.new("char[64]") |
| 78 |
|
| 79 |
for i,c in ipairs(generic) do |
| 80 |
test("Generic fp test fmt: " .. c[2] .. '; ' .. tostring(c[1]), function() |
| 81 |
ffi.C.rspamd_snprintf(buf, 64, c[2], c[1]) |
| 82 |
local sbuf = ffi.string(buf) |
| 83 |
assert_equal(sbuf, c[3], c[3] .. " but test returned " .. sbuf) |
| 84 |
end) |
| 85 |
end |
| 86 |
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then |
| 87 |
for _,c in ipairs(benchmarks) do |
| 88 |
test("Fuzz fp test " .. c[1], function() |
| 89 |
for _=1,niter_fuzz do |
| 90 |
local sign = 1 |
| 91 |
if math.random() > 0.5 then |
| 92 |
sign = -1 |
| 93 |
end |
| 94 |
local d = c[2]() * sign |
| 95 |
ffi.C.snprintf(buf, 64, c[3], d) |
| 96 |
ffi.C.rspamd_snprintf(buf2, 64, c[3], d) |
| 97 |
|
| 98 |
local sbuf = ffi.string(buf) |
| 99 |
local sbuf2 = ffi.string(buf2) |
| 100 |
|
| 101 |
assert_less_than(math.abs(d - tonumber(sbuf2))/math.abs(d), |
| 102 |
0.00001, |
| 103 |
string.format('rspamd emitted: %s, libc emitted: %s, original number: %g', |
| 104 |
sbuf2, sbuf, d)) |
| 105 |
end |
| 106 |
end) |
| 107 |
end |
| 108 |
end |
| 109 |
end) |