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
committerLennart Poettering <lennart@poettering.net>2017-12-05 13:49:12 +0100
commitbb8ad9eacaaf08078ad08e289c4cef08c7c38663 (patch)
treefb03ac6998d850aea41debd76d37e54bf10436b3 /src/test/test-string-util.c
parent1cfdbe293f0e6266fc63bd807393f0813b8119f0 (diff)
downloadsystemd-bb8ad9eacaaf08078ad08e289c4cef08c7c38663.tar.gz
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 7a14b8efd3..07ee98465c 100644
--- a/src/test/test-string-util.c
+++ b/src/test/test-string-util.c
@@ -104,9 +104,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) {
@@ -399,6 +427,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();