Debian Codegraph @

libdrgn/tests/path.c.in

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <inttypes.h>
5 #include <string.h>
6
7 #include "test_util.h"
8 #include "../array.h"
9 #include "../cleanup.h"
10 #include "../path.h"
11 #include "../pp.h"
12
13 #suite path
14
15 #tcase path_iterator
16
17 static void
18 assert_path_iterator_no_combinations_impl(struct nstring *in_components,
19 size_t num_in_components,
20 const struct nstring *out_components,
21 size_t num_out_components,
22 const char *msg)
23 {
24 struct path_iterator it = {
25 .components = in_components,
26 .num_components = num_in_components,
27 };
28 struct nstring component;
29 for (size_t i = 0; i < num_out_components; i++) {
30 const struct nstring *expected = &out_components[i];
31 ck_assert_msg(path_iterator_next(&it, &component.str,
32 &component.len),
33 "%s: expected '%.*s', got end", msg,
34 (int)expected->len, expected->str);
35 ck_assert_msg(nstring_eq(&component, expected),
36 "%s: expected '%.*s', got '%.*s'",
37 msg, (int)expected->len, expected->str,
38 (int)component.len, component.str);
39 }
40 ck_assert_msg(!path_iterator_next(&it, &component.str, &component.len),
41 "%s: expected end, got '%.*s'", msg, (int)component.len,
42 component.str);
43 }
44
45 #define to_nstring(_, x) { x, strlen(x) },
46 #define to_nstring_array(...) \
47 (struct nstring []){ PP_MAP(to_nstring,, __VA_ARGS__) }
48 #define to_const_nstring_array(...) \
49 (const struct nstring []){ PP_MAP(to_nstring,, __VA_ARGS__) }
50
51 #define assert_path_iterator_no_combinations(in_components, out_components) \
52 assert_path_iterator_no_combinations_impl(to_nstring_array in_components,\
53 PP_NARGS in_components, \
54 to_const_nstring_array out_components,\
55 PP_NARGS out_components, \
56 "Assertion 'path_iterator"#in_components" -> "#out_components" failed")
57
58 static void assert_path_iterator_impl(const struct nstring *in_components,
59 size_t num_in_components,
60 const struct nstring *out_components,
61 size_t num_out_components,
62 const char *msg)
63 {
64 ck_assert_uint_gt(num_in_components, 0);
65 ck_assert_uint_le(num_in_components, 64);
66
67 // Join all in_components with a '/'. We will use substrings of this as
68 // the iterator components.
69 size_t total_length = 0;
70 for (size_t i = 0; i < num_in_components; i++)
71 total_length += in_components[i].len + 1;
72 _cleanup_free_ char *buf = malloc(total_length);
73 ck_assert_ptr_nonnull(buf);
74 char *p = buf;
75 for (size_t i = 0; i < num_in_components; i++) {
76 p = stpcpy(p, in_components[i].str);
77 *(p++) = '/';
78 }
79 p[-1] = '\0';
80
81 struct nstring components[64];
82 // Iterate over all possible combinations. Bit i of mask indicates
83 // whether to join in_components i and i + 1 with a '/'.
84 for (uint64_t mask = 0; mask < 1 << (num_in_components - 1); mask++) {
85 struct path_iterator it = {
86 .components = components,
87 .num_components = 1,
88 };
89 components[0].str = buf;
90 components[0].len = in_components[0].len;
91 for (size_t i = 1; i < num_in_components; i++) {
92 if (mask & (1 << (i - 1))) {
93 components[it.num_components - 1].len +=
94 1 + in_components[i].len;
95 } else {
96 components[it.num_components].str =
97 components[it.num_components - 1].str
98 + components[it.num_components - 1].len
99 + 1;
100 components[it.num_components].len =
101 in_components[i].len;
102 it.num_components++;
103 }
104 }
105
106 struct nstring component;
107 for (size_t i = 0; i < num_out_components; i++) {
108 const struct nstring *expected = &out_components[i];
109 ck_assert_msg(path_iterator_next(&it, &component.str,
110 &component.len),
111 "%s on mask 0x%" PRIx64 ": expected '%.*s', got end",
112 msg, mask, (int)expected->len,
113 expected->str);
114 ck_assert_msg(nstring_eq(&component, expected),
115 "%s on mask 0x%" PRIx64 ": expected '%.*s', got '%.*s'",
116 msg, mask, (int)expected->len,
117 expected->str, (int)component.len,
118 component.str);
119 }
120 ck_assert_msg(!path_iterator_next(&it, &component.str, &component.len),
121 "%s on mask 0x%" PRIx64 ": expected end, got '%.*s'",
122 msg, mask, (int)component.len, component.str);
123 }
124 }
125
126 // Assert that a path_iterator over all possible combinations of joining or not
127 // joining in_components with '/' always yields out_components.
128 #define assert_path_iterator(in_components, out_components) \
129 assert_path_iterator_impl(to_nstring_array in_components, \
130 PP_NARGS in_components, \
131 to_const_nstring_array out_components, \
132 PP_NARGS out_components, \
133 "Assertion 'path_iterator"#in_components" -> "#out_components" failed")
134
135 #test path_iterator_empty
136 {
137 assert_path_iterator_no_combinations((), ());
138 assert_path_iterator_no_combinations((""), ());
139 assert_path_iterator_no_combinations(("", ""), ());
140 }
141
142 #test path_iterator_simple
143 {
144 assert_path_iterator(("a"), ("a"));
145 assert_path_iterator(("abc", "def"), ("def", "abc"));
146 assert_path_iterator(("abc", "def", "ghi"), ("ghi", "def", "abc"));
147 }
148
149 #test path_iterator_root
150 {
151 assert_path_iterator(("/"), (""));
152 assert_path_iterator(("/", ""), (""));
153 assert_path_iterator(("", "/"), (""));
154 assert_path_iterator(("", "/", ""), (""));
155 }
156
157 #test path_iterator_absolute
158 {
159 assert_path_iterator(("/root"), ("root", ""));
160 assert_path_iterator(("/./usr"), ("usr", ""));
161 assert_path_iterator(("/home", "user"), ("user", "home", ""));
162 assert_path_iterator_no_combinations(("foo", "/root"), ("root", ""));
163 }
164
165 #test path_iterator_redundant_slash
166 {
167 assert_path_iterator(("a/"), ("a"));
168 assert_path_iterator(("a//"), ("a"));
169 assert_path_iterator(("//"), (""));
170 assert_path_iterator(("//a"), ("a", ""));
171 assert_path_iterator(("///a"), ("a", ""));
172 }
173
174 #test path_iterator_dot
175 {
176 assert_path_iterator(("a", "."), ("a"));
177 assert_path_iterator((".", "a"), ("a"));
178 assert_path_iterator((".", "a", "."), ("a"));
179 }
180
181 #test path_iterator_dot_dot
182 {
183 assert_path_iterator(("a", "b", ".."), ("a"));
184 assert_path_iterator(("a", "..", "b"), ("b"));
185 }
186
187 #test path_iterator_relative_dot_dot
188 {
189 assert_path_iterator(("..", "one", "two"), ("two", "one", ".."));
190 assert_path_iterator(("one", "..", "..", "two"), ("two", ".."));
191 assert_path_iterator(("one", "two", "..", "..", ".."), (".."));
192 }
193
194 #test path_iterator_dot_dot_above_root
195 {
196 assert_path_iterator(("/..", "one", "two"), ("two", "one", ""));
197 assert_path_iterator(("/one", "..", "..", "two"), ("two", ""));
198 assert_path_iterator(("/one", "two", "..", "..", ".."), (""));
199 }
200
201 #test path_iterator_current_directory
202 {
203 assert_path_iterator(("."), ());
204 assert_path_iterator_no_combinations(("", "."), ());
205 assert_path_iterator((".", ""), ());
206 assert_path_iterator((".", "."), ());
207 assert_path_iterator(("foo", ".."), ());
208 assert_path_iterator(("a", "b", "..", ".."), ());
209 }
210
211 #tcase path_ends_with
212
213 #define assert_path_ends_with_impl(op, haystack, needle) \
214 ck_assert(op path_ends_with(&(struct path_iterator){ \
215 .components = to_nstring_array haystack, \
216 .num_components = PP_NARGS haystack, \
217 }, &(struct path_iterator){ \
218 .components = to_nstring_array needle, \
219 .num_components = PP_NARGS needle, \
220 }))
221
222 #define assert_path_ends_with(haystack, needle) \
223 assert_path_ends_with_impl(, haystack, needle)
224
225 #define assert_not_path_ends_with(haystack, needle) \
226 assert_path_ends_with_impl(!, haystack, needle)
227
228 #test path_ends_with_empty
229 {
230 assert_path_ends_with((), ());
231 assert_path_ends_with(("ab"), ());
232 assert_not_path_ends_with((), ("ab"));
233 }
234
235 #test path_ends_with_one_component
236 {
237 assert_path_ends_with(("ab"), ("ab"));
238 assert_not_path_ends_with(("ab"), ("cd"));
239 }
240
241 #test path_ends_with_multiple_components
242 {
243 assert_path_ends_with(("ab/cd/ef"), ("ef"));
244 assert_path_ends_with(("ab/cd", "ef"), ("cd/ef"));
245 assert_not_path_ends_with(("ab/cd/ef"), ("cd"));
246 assert_not_path_ends_with(("ab/cd/ef"), ("ab/ef"));
247 assert_not_path_ends_with(("ef"), ("ab/cd/ef"));
248 }
249
250 #test path_ends_with_component_substring
251 {
252 assert_not_path_ends_with(("ab/cd/ef"), ("d/ef"));
253 }
254
255 #test path_ends_with_absolute
256 {
257 assert_path_ends_with(("/abc"), ("abc"));
258 assert_not_path_ends_with(("abc"), ("/abc"));
259 }
260
261 #test path_ends_with_absolute_component
262 {
263 assert_not_path_ends_with(("ab/cd", "/ef"), ("cd/ef"));
264 }