Commit | Line | Data |
---|---|---|
dbfea1ab | 1 | /* |
9d16b343 | 2 | * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com> |
dbfea1ab | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: GPL-2.0-only |
dbfea1ab | 5 | * |
dbfea1ab PP |
6 | */ |
7 | ||
8 | #include <stdlib.h> | |
9 | #include <stdbool.h> | |
10 | #include <assert.h> | |
11 | #include <string.h> | |
12 | #include <stdarg.h> | |
13 | #include <common/string-utils/string-utils.h> | |
14 | #include <tap/tap.h> | |
15 | ||
16 | /* Number of TAP tests in this file */ | |
17 | #define NUM_TESTS 69 | |
18 | ||
3f322f1a | 19 | static void test_one_split(const char *input, char delim, int escape_delim, |
dbfea1ab PP |
20 | ...) |
21 | { | |
22 | va_list vl; | |
23 | char **substrings; | |
24 | char * const *substring; | |
25 | bool all_ok = true; | |
26 | ||
27 | substrings = strutils_split(input, delim, escape_delim); | |
28 | assert(substrings); | |
29 | va_start(vl, escape_delim); | |
30 | ||
31 | for (substring = substrings; *substring; substring++) { | |
32 | const char *expected_substring = va_arg(vl, const char *); | |
33 | ||
34 | diag(" got `%s`, expecting `%s`", *substring, expected_substring); | |
35 | ||
36 | if (!expected_substring) { | |
37 | all_ok = false; | |
38 | break; | |
39 | } | |
40 | ||
41 | if (strcmp(*substring, expected_substring) != 0) { | |
42 | all_ok = false; | |
43 | break; | |
44 | } | |
45 | } | |
46 | ||
47 | strutils_free_null_terminated_array_of_strings(substrings); | |
48 | va_end(vl); | |
49 | ok(all_ok, "strutils_split() produces the expected substrings: `%s` (delim. `%c`, escape `%d`)", | |
50 | input, delim, escape_delim); | |
51 | } | |
52 | ||
53 | static void test_split(void) | |
54 | { | |
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); | |
72 | } | |
73 | ||
74 | static void test_one_is_star_at_the_end_only_glob_pattern(const char *pattern, bool expected) | |
75 | { | |
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", | |
78 | pattern, expected); | |
79 | } | |
80 | ||
81 | static void test_is_star_at_the_end_only_glob_pattern(void) | |
82 | { | |
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); | |
93 | } | |
94 | ||
95 | static void test_one_is_star_glob_pattern(const char *pattern, bool expected) | |
96 | { | |
97 | ok(strutils_is_star_glob_pattern(pattern) == expected, | |
98 | "strutils_is_star_glob_pattern() returns the expected result: `%s` -> %d", | |
99 | pattern, expected); | |
100 | } | |
101 | ||
102 | static void test_is_star_glob_pattern(void) | |
103 | { | |
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); | |
119 | } | |
120 | ||
121 | static void test_one_normalize_star_glob_pattern(const char *pattern, | |
122 | const char *expected) | |
123 | { | |
124 | char *rw_pattern = strdup(pattern); | |
125 | ||
126 | assert(rw_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`", | |
130 | pattern, expected); | |
131 | free(rw_pattern); | |
132 | } | |
133 | ||
134 | static void test_normalize_star_glob_pattern(void) | |
135 | { | |
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("**\\***", "*\\**"); | |
163 | } | |
164 | ||
165 | int main(int argc, char **argv) | |
166 | { | |
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(); | |
172 | test_split(); | |
173 | ||
174 | return exit_status(); | |
175 | } |