summaryrefslogtreecommitdiffstats
path: root/src/test/test-strv.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2024-04-26 17:40:32 +0200
committerLennart Poettering <lennart@poettering.net>2024-06-17 09:20:21 +0200
commitaca093018c5d2cd8a63129cab67941fe1b8fd850 (patch)
tree7fd7cbd054dc18727f2f48909fcdba19bf3d61e8 /src/test/test-strv.c
parentutf8: export utf8_char_console_width() (diff)
downloadsystemd-aca093018c5d2cd8a63129cab67941fe1b8fd850.tar.xz
systemd-aca093018c5d2cd8a63129cab67941fe1b8fd850.zip
strv: add new helper strv_rebreak_lines() with a simple line breaking algorithm
Diffstat (limited to 'src/test/test-strv.c')
-rw-r--r--src/test/test-strv.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index 28b8b2270c..65afefed3b 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -1055,4 +1055,62 @@ TEST(strv_extend_many) {
assert_se(strv_equal(l, STRV_MAKE("foo", "bar", "waldo", "quux", "1", "2", "3", "4", "yes", "no")));
}
+TEST(strv_rebreak_lines) {
+ _cleanup_strv_free_ char **l = NULL;
+
+ assert_se(strv_rebreak_lines(NULL, SIZE_MAX, &l) >= 0);
+ assert_se(strv_equal(l, NULL));
+ l = strv_free(l);
+
+ assert_se(strv_rebreak_lines(STRV_MAKE(""), SIZE_MAX, &l) >= 0);
+ assert_se(strv_equal(l, STRV_MAKE("")));
+ l = strv_free(l);
+
+ assert_se(strv_rebreak_lines(STRV_MAKE("", ""), SIZE_MAX, &l) >= 0);
+ assert_se(strv_equal(l, STRV_MAKE("", "")));
+ l = strv_free(l);
+
+ assert_se(strv_rebreak_lines(STRV_MAKE("foo"), SIZE_MAX, &l) >= 0);
+ assert_se(strv_equal(l, STRV_MAKE("foo")));
+ l = strv_free(l);
+
+ assert_se(strv_rebreak_lines(STRV_MAKE("foo", "bar"), SIZE_MAX, &l) >= 0);
+ assert_se(strv_equal(l, STRV_MAKE("foo", "bar")));
+ l = strv_free(l);
+
+ assert_se(strv_rebreak_lines(STRV_MAKE("Foo fOo foO FOo", "bar Bar bAr baR BAr"), 10, &l) >= 0);
+ assert_se(strv_equal(l, STRV_MAKE("Foo fOo", "foO FOo", "bar Bar", "bAr baR", "BAr")));
+ l = strv_free(l);
+
+ assert_se(strv_rebreak_lines(STRV_MAKE(" foo ",
+ " foo bar waldo quux "),
+ 10, &l) >= 0);
+ assert_se(strv_equal(l, STRV_MAKE(" foo",
+ " foo",
+ "bar",
+ "waldo quux")));
+ l = strv_free(l);
+
+ assert_se(strv_rebreak_lines(STRV_MAKE(" ",
+ "\tfoo bar\t",
+ "FOO\tBAR"),
+ 10, &l) >= 0);
+ assert_se(strv_equal(l, STRV_MAKE("",
+ "\tfoo",
+ "bar",
+ "FOO",
+ "BAR")));
+ l = strv_free(l);
+
+ /* Now make sure that breaking the lines a 2nd time does not modify the output anymore */
+ for (size_t i = 1; i < 100; i++) {
+ _cleanup_strv_free_ char **a = NULL, **b = NULL;
+
+ assert_se(strv_rebreak_lines(STRV_MAKE("foobar waldo waldo quux piep\tschnurz pimm"), i, &a) >= 0);
+ assert_se(strv_rebreak_lines(a, i, &b) >= 0);
+
+ assert_se(strv_equal(a, b));
+ }
+}
+
DEFINE_TEST_MAIN(LOG_INFO);