]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add tests for util_replace_whitespace()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 21 Nov 2018 07:17:29 +0000 (16:17 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 21 Nov 2018 08:30:49 +0000 (17:30 +0900)
src/test/test-libudev.c

index abf79874bd03bc05988eb99ff73e8b07a9b0ef96..258b77b4300d3f28b8d8541ddd5a1498a8404029 100644 (file)
@@ -6,6 +6,7 @@
 #include <sys/epoll.h>
 #include <unistd.h>
 
+#include "alloc-util.h"
 #include "fd-util.h"
 #include "libudev-util.h"
 #include "log.h"
@@ -329,6 +330,51 @@ static void test_hwdb(struct udev *udev, const char *modalias) {
         assert_se(hwdb == NULL);
 }
 
+static void test_util_replace_whitespace_one_len(const char *str, size_t len, const char *expected) {
+        _cleanup_free_ char *result = NULL;
+        int r;
+
+        result = new(char, len + 1);
+        assert_se(result);
+        r = util_replace_whitespace(str, result, len);
+        assert_se((size_t) r == strlen(expected));
+        assert_se(streq(result, expected));
+}
+
+static void test_util_replace_whitespace_one(const char *str, const char *expected) {
+        test_util_replace_whitespace_one_len(str, strlen(str), expected);
+}
+
+static void test_util_replace_whitespace(void) {
+        test_util_replace_whitespace_one("hogehoge", "hogehoge");
+        test_util_replace_whitespace_one("hoge  hoge", "hoge_hoge");
+        test_util_replace_whitespace_one("  hoge  hoge  ", "hoge_hoge");
+        test_util_replace_whitespace_one("     ", "");
+        test_util_replace_whitespace_one("hoge ", "hoge");
+
+        test_util_replace_whitespace_one_len("hoge hoge    ", 9, "hoge_hoge");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 8, "hoge_hog");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 7, "hoge_ho");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 6, "hoge_h");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 5, "hoge");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 4, "hoge");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 3, "hog");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 2, "ho");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 1, "h");
+        test_util_replace_whitespace_one_len("hoge hoge    ", 0, "");
+
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 9, "hoge_hoge");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 8, "hoge_hog");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 7, "hoge_ho");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 6, "hoge_h");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 5, "hoge");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 4, "hoge");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 3, "hog");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 2, "ho");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 1, "h");
+        test_util_replace_whitespace_one_len("         hoge   hoge    ", 0, "");
+}
+
 int main(int argc, char *argv[]) {
         _cleanup_(udev_unrefp) struct udev *udev = NULL;
         bool arg_monitor = false;
@@ -397,7 +443,6 @@ int main(int argc, char *argv[]) {
         test_device_subsys_name(udev, "subsystem", "pci");
         test_device_subsys_name(udev, "drivers", "scsi:sd");
         test_device_subsys_name(udev, "module", "printk");
-
         test_device_parents(udev, syspath);
 
         test_enumerate(udev, subsystem);
@@ -409,5 +454,7 @@ int main(int argc, char *argv[]) {
         if (arg_monitor)
                 test_monitor(udev);
 
+        test_util_replace_whitespace();
+
         return EXIT_SUCCESS;
 }