File: path.c.in

package info (click to toggle)
drgn 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,852 kB
  • sloc: python: 74,992; ansic: 54,589; awk: 423; makefile: 351; sh: 99
file content (264 lines) | stat: -rw-r--r-- 8,396 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later

#include <inttypes.h>
#include <string.h>

#include "test_util.h"
#include "../array.h"
#include "../cleanup.h"
#include "../path.h"
#include "../pp.h"

#suite path

#tcase path_iterator

static void
assert_path_iterator_no_combinations_impl(struct nstring *in_components,
					  size_t num_in_components,
					  const struct nstring *out_components,
					  size_t num_out_components,
					  const char *msg)
{
	struct path_iterator it = {
		.components = in_components,
		.num_components = num_in_components,
	};
	struct nstring component;
	for (size_t i = 0; i < num_out_components; i++) {
		const struct nstring *expected = &out_components[i];
		ck_assert_msg(path_iterator_next(&it, &component.str,
						 &component.len),
			      "%s: expected '%.*s', got end", msg,
			      (int)expected->len, expected->str);
		ck_assert_msg(nstring_eq(&component, expected),
			      "%s: expected '%.*s', got '%.*s'",
			      msg, (int)expected->len, expected->str,
			      (int)component.len, component.str);
	}
	ck_assert_msg(!path_iterator_next(&it, &component.str, &component.len),
		      "%s: expected end, got '%.*s'", msg, (int)component.len,
		      component.str);
}

#define to_nstring(_, x) { x, strlen(x) },
#define to_nstring_array(...)	\
	(struct nstring []){ PP_MAP(to_nstring,, __VA_ARGS__) }
#define to_const_nstring_array(...)	\
	(const struct nstring []){ PP_MAP(to_nstring,, __VA_ARGS__) }

#define assert_path_iterator_no_combinations(in_components, out_components)	\
	assert_path_iterator_no_combinations_impl(to_nstring_array in_components,\
						  PP_NARGS in_components,	\
						  to_const_nstring_array out_components,\
						  PP_NARGS out_components,	\
						  "Assertion 'path_iterator"#in_components" -> "#out_components" failed")

static void assert_path_iterator_impl(const struct nstring *in_components,
				      size_t num_in_components,
				      const struct nstring *out_components,
				      size_t num_out_components,
				      const char *msg)
{
	ck_assert_uint_gt(num_in_components, 0);
	ck_assert_uint_le(num_in_components, 64);

	// Join all in_components with a '/'. We will use substrings of this as
	// the iterator components.
	size_t total_length = 0;
	for (size_t i = 0; i < num_in_components; i++)
		total_length += in_components[i].len + 1;
	_cleanup_free_ char *buf = malloc(total_length);
	ck_assert_ptr_nonnull(buf);
	char *p = buf;
	for (size_t i = 0; i < num_in_components; i++) {
		p = stpcpy(p, in_components[i].str);
		*(p++) = '/';
	}
	p[-1] = '\0';

	struct nstring components[64];
	// Iterate over all possible combinations. Bit i of mask indicates
	// whether to join in_components i and i + 1 with a '/'.
	for (uint64_t mask = 0; mask < 1 << (num_in_components - 1); mask++) {
		struct path_iterator it = {
			.components = components,
			.num_components = 1,
		};
		components[0].str = buf;
		components[0].len = in_components[0].len;
		for (size_t i = 1; i < num_in_components; i++) {
			if (mask & (1 << (i - 1))) {
				components[it.num_components - 1].len +=
					1 + in_components[i].len;
			} else {
				components[it.num_components].str =
					components[it.num_components - 1].str
					+ components[it.num_components - 1].len
					+ 1;
				components[it.num_components].len =
					in_components[i].len;
				it.num_components++;
			}
		}

		struct nstring component;
		for (size_t i = 0; i < num_out_components; i++) {
			const struct nstring *expected = &out_components[i];
			ck_assert_msg(path_iterator_next(&it, &component.str,
							 &component.len),
				      "%s on mask 0x%" PRIx64 ": expected '%.*s', got end",
				      msg, mask, (int)expected->len,
				      expected->str);
			ck_assert_msg(nstring_eq(&component, expected),
				      "%s on mask 0x%" PRIx64 ": expected '%.*s', got '%.*s'",
				      msg, mask, (int)expected->len,
				      expected->str, (int)component.len,
				      component.str);
		}
		ck_assert_msg(!path_iterator_next(&it, &component.str, &component.len),
			      "%s on mask 0x%" PRIx64 ": expected end, got '%.*s'",
			      msg, mask, (int)component.len, component.str);
	}
}

// Assert that a path_iterator over all possible combinations of joining or not
// joining in_components with '/' always yields out_components.
#define assert_path_iterator(in_components, out_components)			\
	assert_path_iterator_impl(to_nstring_array in_components,		\
				  PP_NARGS in_components,			\
				  to_const_nstring_array out_components,	\
				  PP_NARGS out_components,			\
				  "Assertion 'path_iterator"#in_components" -> "#out_components" failed")

#test path_iterator_empty
{
	assert_path_iterator_no_combinations((), ());
	assert_path_iterator_no_combinations((""), ());
	assert_path_iterator_no_combinations(("", ""), ());
}

#test path_iterator_simple
{
	assert_path_iterator(("a"), ("a"));
	assert_path_iterator(("abc", "def"), ("def", "abc"));
	assert_path_iterator(("abc", "def", "ghi"), ("ghi", "def", "abc"));
}

#test path_iterator_root
{
	assert_path_iterator(("/"), (""));
	assert_path_iterator(("/", ""), (""));
	assert_path_iterator(("", "/"), (""));
	assert_path_iterator(("", "/", ""), (""));
}

#test path_iterator_absolute
{
	assert_path_iterator(("/root"), ("root", ""));
	assert_path_iterator(("/./usr"), ("usr", ""));
	assert_path_iterator(("/home", "user"), ("user", "home", ""));
	assert_path_iterator_no_combinations(("foo", "/root"), ("root", ""));
}

#test path_iterator_redundant_slash
{
	assert_path_iterator(("a/"), ("a"));
	assert_path_iterator(("a//"), ("a"));
	assert_path_iterator(("//"), (""));
	assert_path_iterator(("//a"), ("a", ""));
	assert_path_iterator(("///a"), ("a", ""));
}

#test path_iterator_dot
{
	assert_path_iterator(("a", "."), ("a"));
	assert_path_iterator((".", "a"), ("a"));
	assert_path_iterator((".", "a", "."), ("a"));
}

#test path_iterator_dot_dot
{
        assert_path_iterator(("a", "b", ".."), ("a"));
        assert_path_iterator(("a", "..", "b"), ("b"));
}

#test path_iterator_relative_dot_dot
{
        assert_path_iterator(("..", "one", "two"), ("two", "one", ".."));
        assert_path_iterator(("one", "..", "..", "two"), ("two", ".."));
        assert_path_iterator(("one", "two", "..", "..", ".."), (".."));
}

#test path_iterator_dot_dot_above_root
{
        assert_path_iterator(("/..", "one", "two"), ("two", "one", ""));
        assert_path_iterator(("/one", "..", "..", "two"), ("two", ""));
        assert_path_iterator(("/one", "two", "..", "..", ".."), (""));
}

#test path_iterator_current_directory
{
        assert_path_iterator(("."), ());
        assert_path_iterator_no_combinations(("", "."), ());
        assert_path_iterator((".", ""), ());
        assert_path_iterator((".", "."), ());
        assert_path_iterator(("foo", ".."), ());
        assert_path_iterator(("a", "b", "..", ".."), ());
}

#tcase path_ends_with

#define assert_path_ends_with_impl(op, haystack, needle)	\
	ck_assert(op path_ends_with(&(struct path_iterator){	\
		.components = to_nstring_array haystack,	\
		.num_components = PP_NARGS haystack,		\
	}, &(struct path_iterator){				\
		.components = to_nstring_array needle,		\
		.num_components = PP_NARGS needle,		\
	}))

#define assert_path_ends_with(haystack, needle)		\
	assert_path_ends_with_impl(, haystack, needle)

#define assert_not_path_ends_with(haystack, needle)	\
	assert_path_ends_with_impl(!, haystack, needle)

#test path_ends_with_empty
{
	assert_path_ends_with((), ());
	assert_path_ends_with(("ab"), ());
	assert_not_path_ends_with((), ("ab"));
}

#test path_ends_with_one_component
{
        assert_path_ends_with(("ab"), ("ab"));
        assert_not_path_ends_with(("ab"), ("cd"));
}

#test path_ends_with_multiple_components
{
        assert_path_ends_with(("ab/cd/ef"), ("ef"));
        assert_path_ends_with(("ab/cd", "ef"), ("cd/ef"));
        assert_not_path_ends_with(("ab/cd/ef"), ("cd"));
        assert_not_path_ends_with(("ab/cd/ef"), ("ab/ef"));
        assert_not_path_ends_with(("ef"), ("ab/cd/ef"));
}

#test path_ends_with_component_substring
{
	assert_not_path_ends_with(("ab/cd/ef"), ("d/ef"));
}

#test path_ends_with_absolute
{
        assert_path_ends_with(("/abc"), ("abc"));
        assert_not_path_ends_with(("abc"), ("/abc"));
}

#test path_ends_with_absolute_component
{
	assert_not_path_ends_with(("ab/cd", "/ef"), ("cd/ef"));
}