summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2024-09-20 02:09:28 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2024-09-20 22:06:12 +0200
commitffdf4978604672ee7b327a5f65d821daeac26149 (patch)
treeae5f9ee53957eae9eebc5056ee7c65fb7ad4e60b /src/test
parentMerge pull request #34510 from keszybz/mkosi-version-checks (diff)
downloadsystemd-ffdf4978604672ee7b327a5f65d821daeac26149.tar.xz
systemd-ffdf4978604672ee7b327a5f65d821daeac26149.zip
strv: introduce strv_find_closest()
Follow-up for 1e1ac5d53b0f126b6c4419506c7c42b67c07537f.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-strv.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index f7b6ee2bb2..7c25d59bf4 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -1188,4 +1188,31 @@ TEST(strv_rebreak_lines) {
}
}
+TEST(strv_find_closest) {
+ char **l = STRV_MAKE("aaa", "aaaa", "bbb", "ccc");
+
+ /* prefix match */
+ ASSERT_STREQ(strv_find_closest(l, "a"), "aaa");
+ ASSERT_STREQ(strv_find_closest(l, "aa"), "aaa");
+ ASSERT_STREQ(strv_find_closest(l, "aaa"), "aaa");
+ ASSERT_STREQ(strv_find_closest(l, "aaaa"), "aaaa");
+ ASSERT_STREQ(strv_find_closest(l, "b"), "bbb");
+ ASSERT_STREQ(strv_find_closest(l, "bb"), "bbb");
+ ASSERT_STREQ(strv_find_closest(l, "bbb"), "bbb");
+ ASSERT_STREQ(strv_find_closest(l, "c"), "ccc");
+ ASSERT_STREQ(strv_find_closest(l, "cc"), "ccc");
+ ASSERT_STREQ(strv_find_closest(l, "ccc"), "ccc");
+
+ /* levenshtein match */
+ ASSERT_STREQ(strv_find_closest(l, "aab"), "aaa");
+ ASSERT_STREQ(strv_find_closest(l, "abb"), "bbb");
+ ASSERT_STREQ(strv_find_closest(l, "cbc"), "ccc");
+ ASSERT_STREQ(strv_find_closest(l, "aax"), "aaa");
+ ASSERT_STREQ(strv_find_closest(l, "bbbb"), "bbb");
+ ASSERT_STREQ(strv_find_closest(l, "cbbb"), "bbb");
+ ASSERT_STREQ(strv_find_closest(l, "bbbx"), "bbb");
+
+ ASSERT_NULL(strv_find_closest(l, "sfajosajfosdjaofjdsaf"));
+}
+
DEFINE_TEST_MAIN(LOG_INFO);