summaryrefslogtreecommitdiff
path: root/src/test/test-string-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-11-28 16:37:53 +0100
committerSven Eden <yamakuzure@gmx.net>2017-11-28 16:37:53 +0100
commit14728b9406dccbf6a74f21aaa1cc524273edbb15 (patch)
tree4fc519fe247975bf439aa82531799b18eb6de4de /src/test/test-string-util.c
parent8a5a474e846f8810cb3861ed869da35db74f9a97 (diff)
string-util: rework strextend() to optionally inset separators between each appended string
This adds a new flavour of strextend(), called strextend_with_separator(), which takes an optional separator string. If specified, the separator is inserted between each appended string, as well as before the first one, but only if the original string was non-empty. This new call is particularly useful when appending new options to mount option strings and suchlike, which need to be comma-separated, and initially start out from an empty string.
Diffstat (limited to 'src/test/test-string-util.c')
-rw-r--r--src/test/test-string-util.c35
1 files changed, 32 insertions, 3 deletions
diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c
index 3938012d2..12340f075 100644
--- a/src/test/test-string-util.c
+++ b/src/test/test-string-util.c
@@ -105,9 +105,37 @@ static void test_strstrip(void) {
}
static void test_strextend(void) {
- _cleanup_free_ char *str = strdup("0123");
- strextend(&str, "456", "78", "9", NULL);
- assert_se(streq(str, "0123456789"));
+ _cleanup_free_ char *str = NULL;
+
+ assert_se(strextend(&str, NULL));
+ assert_se(streq_ptr(str, ""));
+ assert_se(strextend(&str, "", "0", "", "", "123", NULL));
+ assert_se(streq_ptr(str, "0123"));
+ assert_se(strextend(&str, "456", "78", "9", NULL));
+ assert_se(streq_ptr(str, "0123456789"));
+}
+
+static void test_strextend_with_separator(void) {
+ _cleanup_free_ char *str = NULL;
+
+ assert_se(strextend_with_separator(&str, NULL, NULL));
+ assert_se(streq_ptr(str, ""));
+ str = mfree(str);
+
+ assert_se(strextend_with_separator(&str, "...", NULL));
+ assert_se(streq_ptr(str, ""));
+ assert_se(strextend_with_separator(&str, "...", NULL));
+ assert_se(streq_ptr(str, ""));
+ str = mfree(str);
+
+ assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc", NULL));
+ assert_se(streq_ptr(str, "axyzbbxyzccc"));
+ str = mfree(str);
+
+ assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234", NULL));
+ assert_se(streq_ptr(str, "start,,1,234"));
+ assert_se(strextend_with_separator(&str, ";", "more", "5", "678", NULL));
+ assert_se(streq_ptr(str, "start,,1,234;more;5;678"));
}
static void test_strrep(void) {
@@ -357,6 +385,7 @@ int main(int argc, char *argv[]) {
test_streq_ptr();
test_strstrip();
test_strextend();
+ test_strextend_with_separator();
test_strrep();
test_strappend();
test_string_has_cc();