Rapid spam filtering system https://rspamd.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.3 KiB

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