2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <common/string-utils/string-utils.h>
16 /* Number of TAP tests in this file */
19 static void test_one_split(const char *input
, char delim
, int escape_delim
,
24 char * const *substring
;
27 substrings
= strutils_split(input
, delim
, escape_delim
);
29 va_start(vl
, escape_delim
);
31 for (substring
= substrings
; *substring
; substring
++) {
32 const char *expected_substring
= va_arg(vl
, const char *);
34 diag(" got `%s`, expecting `%s`", *substring
, expected_substring
);
36 if (!expected_substring
) {
41 if (strcmp(*substring
, expected_substring
) != 0) {
47 strutils_free_null_terminated_array_of_strings(substrings
);
49 ok(all_ok
, "strutils_split() produces the expected substrings: `%s` (delim. `%c`, escape `%d`)",
50 input
, delim
, escape_delim
);
53 static void test_split(void)
55 test_one_split("a/b/c/d/e", '/', false, "a", "b", "c", "d", "e", NULL
);
56 test_one_split("a/b//d/e", '/', false, "a", "b", "", "d", "e", NULL
);
57 test_one_split("/b/c/d/e", '/', false, "", "b", "c", "d", "e", NULL
);
58 test_one_split("a/b/c/d/", '/', false, "a", "b", "c", "d", "", NULL
);
59 test_one_split("/b/c/d/", '/', false, "", "b", "c", "d", "", NULL
);
60 test_one_split("", '/', false, "", NULL
);
61 test_one_split("/", '/', false, "", "", NULL
);
62 test_one_split("//", '/', false, "", "", "", NULL
);
63 test_one_split("hello+world", '+', false, "hello", "world", NULL
);
64 test_one_split("hello\\+world", '+', false, "hello\\", "world", NULL
);
65 test_one_split("hello\\+world", '+', true, "hello+world", NULL
);
66 test_one_split("hello\\++world", '+', true, "hello+", "world", NULL
);
67 test_one_split("hello\\\\++world", '+', true, "hello\\\\", "", "world", NULL
);
68 test_one_split("hello+world\\", '+', false, "hello", "world\\", NULL
);
69 test_one_split("hello+world\\", '+', true, "hello", "world\\", NULL
);
70 test_one_split("\\+", '+', false, "\\", "", NULL
);
71 test_one_split("\\+", '+', true, "+", NULL
);
74 static void test_one_is_star_at_the_end_only_glob_pattern(const char *pattern
, bool expected
)
76 ok(strutils_is_star_at_the_end_only_glob_pattern(pattern
) == expected
,
77 "strutils_is_star_at_the_end_only_glob_pattern() returns the expected result: `%s` -> %d",
81 static void test_is_star_at_the_end_only_glob_pattern(void)
83 test_one_is_star_at_the_end_only_glob_pattern("allo*", true);
84 test_one_is_star_at_the_end_only_glob_pattern("allo\\\\*", true);
85 test_one_is_star_at_the_end_only_glob_pattern("allo", false);
86 test_one_is_star_at_the_end_only_glob_pattern("al*lo", false);
87 test_one_is_star_at_the_end_only_glob_pattern("al\\*lo", false);
88 test_one_is_star_at_the_end_only_glob_pattern("*allo", false);
89 test_one_is_star_at_the_end_only_glob_pattern("al*lo*", false);
90 test_one_is_star_at_the_end_only_glob_pattern("allo**", false);
91 test_one_is_star_at_the_end_only_glob_pattern("allo*\\*", false);
92 test_one_is_star_at_the_end_only_glob_pattern("allo\\*", false);
95 static void test_one_is_star_glob_pattern(const char *pattern
, bool expected
)
97 ok(strutils_is_star_glob_pattern(pattern
) == expected
,
98 "strutils_is_star_glob_pattern() returns the expected result: `%s` -> %d",
102 static void test_is_star_glob_pattern(void)
104 test_one_is_star_glob_pattern("allo*", true);
105 test_one_is_star_glob_pattern("*allo", true);
106 test_one_is_star_glob_pattern("*allo*", true);
107 test_one_is_star_glob_pattern("*al*lo*", true);
108 test_one_is_star_glob_pattern("al\\**lo", true);
109 test_one_is_star_glob_pattern("al\\*l*o", true);
110 test_one_is_star_glob_pattern("all*o\\", true);
111 test_one_is_star_glob_pattern("*", true);
112 test_one_is_star_glob_pattern("\\\\*", true);
113 test_one_is_star_glob_pattern("allo", false);
114 test_one_is_star_glob_pattern("allo\\*", false);
115 test_one_is_star_glob_pattern("al\\*lo", false);
116 test_one_is_star_glob_pattern("\\*allo", false);
117 test_one_is_star_glob_pattern("\\*", false);
118 test_one_is_star_glob_pattern("allo\\", false);
121 static void test_one_normalize_star_glob_pattern(const char *pattern
,
122 const char *expected
)
124 char *rw_pattern
= strdup(pattern
);
127 strutils_normalize_star_glob_pattern(rw_pattern
);
128 ok(strcmp(rw_pattern
, expected
) == 0,
129 "strutils_normalize_star_glob_pattern() produces the expected result: `%s` -> `%s`",
134 static void test_normalize_star_glob_pattern(void)
136 test_one_normalize_star_glob_pattern("salut", "salut");
137 test_one_normalize_star_glob_pattern("sal*ut", "sal*ut");
138 test_one_normalize_star_glob_pattern("sal**ut", "sal*ut");
139 test_one_normalize_star_glob_pattern("sal***ut", "sal*ut");
140 test_one_normalize_star_glob_pattern("*salut", "*salut");
141 test_one_normalize_star_glob_pattern("**salut", "*salut");
142 test_one_normalize_star_glob_pattern("***salut", "*salut");
143 test_one_normalize_star_glob_pattern("salut*", "salut*");
144 test_one_normalize_star_glob_pattern("salut**", "salut*");
145 test_one_normalize_star_glob_pattern("salut***", "salut*");
146 test_one_normalize_star_glob_pattern("sa\\*lut", "sa\\*lut");
147 test_one_normalize_star_glob_pattern("sa\\**lut", "sa\\**lut");
148 test_one_normalize_star_glob_pattern("sa*\\**lut", "sa*\\**lut");
149 test_one_normalize_star_glob_pattern("sa*\\***lut", "sa*\\**lut");
150 test_one_normalize_star_glob_pattern("\\*salu**t", "\\*salu*t");
151 test_one_normalize_star_glob_pattern("\\*salut**", "\\*salut*");
152 test_one_normalize_star_glob_pattern("\\*salut**\\*", "\\*salut*\\*");
153 test_one_normalize_star_glob_pattern("\\*salut", "\\*salut");
154 test_one_normalize_star_glob_pattern("\\***salut", "\\**salut");
155 test_one_normalize_star_glob_pattern("salut\\", "salut\\");
156 test_one_normalize_star_glob_pattern("salut\\**", "salut\\**");
157 test_one_normalize_star_glob_pattern("salut\\\\*", "salut\\\\*");
158 test_one_normalize_star_glob_pattern("salut\\\\***", "salut\\\\*");
159 test_one_normalize_star_glob_pattern("*", "*");
160 test_one_normalize_star_glob_pattern("**", "*");
161 test_one_normalize_star_glob_pattern("***", "*");
162 test_one_normalize_star_glob_pattern("**\\***", "*\\**");
165 int main(int argc
, char **argv
)
167 plan_tests(NUM_TESTS
);
168 diag("String utils unit tests");
169 test_normalize_star_glob_pattern();
170 test_is_star_glob_pattern();
171 test_is_star_at_the_end_only_glob_pattern();
174 return exit_status();
This page took 0.066556 seconds and 4 git commands to generate.