From 38ffac78f3bd635adf069e3c30fc62edc5e90f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Mon, 30 Jan 2023 20:41:36 +0100 Subject: core: add function string_levenshtein (issue #1877) --- tests/unit/core/test-core-string.cpp | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'tests/unit/core') diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp index 7c14345d9..671a1327c 100644 --- a/tests/unit/core/test-core-string.cpp +++ b/tests/unit/core/test-core-string.cpp @@ -2540,6 +2540,60 @@ TEST(CoreString, InputForBuffer) config_file_option_reset (config_look_command_chars, 1); } +/* + * Tests functions: + * string_levenshtein + */ + +TEST(CoreString, Levenshtein) +{ + LONGS_EQUAL(0, string_levenshtein (NULL, NULL, 1)); + LONGS_EQUAL(0, string_levenshtein ("", "", 1)); + LONGS_EQUAL(3, string_levenshtein (NULL, "abc", 1)); + LONGS_EQUAL(3, string_levenshtein ("abc", NULL, 1)); + LONGS_EQUAL(3, string_levenshtein ("", "abc", 1)); + LONGS_EQUAL(3, string_levenshtein ("abc", "", 1)); + + LONGS_EQUAL(0, string_levenshtein ("abc", "abc", 1)); + LONGS_EQUAL(1, string_levenshtein ("abc", "ab", 1)); + LONGS_EQUAL(1, string_levenshtein ("ab", "abc", 1)); + LONGS_EQUAL(2, string_levenshtein ("abc", "a", 1)); + LONGS_EQUAL(2, string_levenshtein ("a", "abc", 1)); + LONGS_EQUAL(3, string_levenshtein ("abc", "", 1)); + LONGS_EQUAL(3, string_levenshtein ("", "abc", 1)); + + LONGS_EQUAL(3, string_levenshtein ("abc", "ABC", 1)); + LONGS_EQUAL(3, string_levenshtein ("abc", "AB", 1)); + LONGS_EQUAL(3, string_levenshtein ("ab", "ABC", 1)); + LONGS_EQUAL(3, string_levenshtein ("abc", "A", 1)); + LONGS_EQUAL(3, string_levenshtein ("a", "ABC", 1)); + LONGS_EQUAL(3, string_levenshtein ("abc", "", 1)); + LONGS_EQUAL(3, string_levenshtein ("", "ABC", 1)); + + LONGS_EQUAL(0, string_levenshtein ("abc", "ABC", 0)); + LONGS_EQUAL(1, string_levenshtein ("abc", "AB", 0)); + LONGS_EQUAL(1, string_levenshtein ("ab", "ABC", 0)); + LONGS_EQUAL(2, string_levenshtein ("abc", "A", 0)); + LONGS_EQUAL(2, string_levenshtein ("a", "ABC", 0)); + LONGS_EQUAL(3, string_levenshtein ("abc", "", 0)); + LONGS_EQUAL(3, string_levenshtein ("", "ABC", 0)); + + LONGS_EQUAL(2, string_levenshtein ("response", "respond", 1)); + LONGS_EQUAL(4, string_levenshtein ("response", "resist", 1)); + + LONGS_EQUAL(2, string_levenshtein ("response", "responsive", 1)); + + /* with UTF-8 chars */ + LONGS_EQUAL(1, string_levenshtein ("é", "É", 1)); + LONGS_EQUAL(0, string_levenshtein ("é", "É", 0)); + LONGS_EQUAL(1, string_levenshtein ("é", "à", 1)); + LONGS_EQUAL(1, string_levenshtein ("é", "à", 0)); + LONGS_EQUAL(1, string_levenshtein ("té", "to", 1)); + LONGS_EQUAL(1, string_levenshtein ("noël", "noel", 1)); + LONGS_EQUAL(2, string_levenshtein ("bôô", "boo", 1)); + LONGS_EQUAL(2, string_levenshtein ("界世", "こん", 1)); +} + /* * Tests functions: * string_get_priority_and_name -- cgit v1.2.3