aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua
diff options
context:
space:
mode:
authorAnton Yuzhaninov <citrin+git@citrin.ru>2022-02-26 13:56:36 +0000
committerAnton Yuzhaninov <citrin+git@citrin.ru>2022-02-26 13:56:36 +0000
commit431b3b892f0dd29581406dcc4ed690358d5780ca (patch)
tree58997fbb1db98a33f6eea12ba550ec5789ea3616 /test/lua
parentba7f6982b0b582b56e48da778784315b5d63d2be (diff)
downloadrspamd-431b3b892f0dd29581406dcc4ed690358d5780ca.tar.gz
rspamd-431b3b892f0dd29581406dcc4ed690358d5780ca.zip
[Minor] Microoptimize lua_util.str_endswith
Use find to check string suffix instead of sub (which involves string interning of a returned string). Benchmarks with LuaJIT 2.1.0 shows that an option with find is significantly faster. While here added unit test for this function.
Diffstat (limited to 'test/lua')
-rw-r--r--test/lua/unit/lua_util.misc.lua33
1 files changed, 32 insertions, 1 deletions
diff --git a/test/lua/unit/lua_util.misc.lua b/test/lua/unit/lua_util.misc.lua
index b19b4d6f1..bab44a38b 100644
--- a/test/lua/unit/lua_util.misc.lua
+++ b/test/lua/unit/lua_util.misc.lua
@@ -27,4 +27,35 @@ context("Lua util - callback_from_string", function()
assert_false(ret)
end)
end
-end) \ No newline at end of file
+end)
+
+context("Lua util - str_endswith", function()
+ local ending = {
+ {'a', 'a'},
+ {'ab', 'b'},
+ {'ab', 'ab'},
+ {'abc', 'bc'},
+ {'any', ''},
+ }
+ local not_ending = {
+ {'a', 'b'},
+ {'', 'a'},
+ {'ab', 'a'},
+ {'ab', 'ba'},
+ {'ab', 'lab'},
+ {'abc', 'ab'},
+ {'abcd', 'bc'},
+ {'a', 'A'},
+ {'aB', 'b'},
+ }
+ for _, c in ipairs(ending) do
+ test(string.format('True case: str_endswith("%s", "%s")', c[1], c[2]), function()
+ assert_true(util.str_endswith(c[1], c[2]))
+ end)
+ end
+ for _, c in ipairs(not_ending) do
+ test(string.format('False case: str_endswith("%s", "%s")', c[1], c[2]), function()
+ assert_false(util.str_endswith(c[1], c[2]))
+ end)
+ end
+end)