Debian Codegraph @

tests/by-util/test_tsort.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 #![allow(clippy::cast_possible_wrap)]
7
8 use uutests::at_and_ucmd;
9 use uutests::new_ucmd;
10
11 #[test]
12 #[cfg(target_os = "linux")]
13 fn test_tsort_non_utf8_paths() {
14 use std::os::unix::ffi::OsStringExt;
15 let (at, mut ucmd) = at_and_ucmd!();
16
17 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
18 std::fs::write(at.plus(&filename), b"a b\nb c\n").unwrap();
19
20 ucmd.arg(&filename).succeeds().stdout_is("a\nb\nc\n");
21 }
22
23 #[test]
24 fn test_invalid_arg() {
25 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
26 }
27 #[test]
28 fn test_sort_call_graph() {
29 new_ucmd!()
30 .arg("call_graph.txt")
31 .succeeds()
32 .stdout_is_fixture("call_graph.expected");
33 }
34
35 #[test]
36 fn test_sort_self_loop() {
37 new_ucmd!()
38 .pipe_in("first first\nfirst second second second")
39 .succeeds()
40 .stdout_only("first\nsecond\n");
41 }
42
43 #[test]
44 fn test_sort_floating_nodes() {
45 new_ucmd!()
46 .pipe_in("d d\nc c\na a\nb b")
47 .succeeds()
48 .stdout_only("a\nb\nc\nd\n");
49 }
50
51 #[test]
52 fn test_no_such_file() {
53 new_ucmd!()
54 .arg("invalid_file_txt")
55 .fails()
56 .stderr_contains("No such file or directory");
57 }
58
59 #[test]
60 fn test_version_flag() {
61 let version_short = new_ucmd!().arg("-V").succeeds();
62 let version_long = new_ucmd!().arg("--version").succeeds();
63
64 assert_eq!(version_short.stdout_str(), version_long.stdout_str());
65 }
66
67 #[test]
68 fn test_help_flag() {
69 let help_short = new_ucmd!().arg("-h").succeeds();
70 let help_long = new_ucmd!().arg("--help").succeeds();
71
72 assert_eq!(help_short.stdout_str(), help_long.stdout_str());
73 }
74
75 #[test]
76 fn test_multiple_arguments() {
77 new_ucmd!()
78 .arg("call_graph.txt")
79 .arg("invalid_file")
80 .fails()
81 .stderr_contains("extra operand 'invalid_file'");
82 }
83
84 #[test]
85 fn test_error_on_dir() {
86 let (at, mut ucmd) = at_and_ucmd!();
87 at.mkdir("tsort_test_dir");
88 ucmd.arg("tsort_test_dir")
89 .fails()
90 .stderr_contains("tsort: tsort_test_dir: read error: Is a directory");
91 }
92
93 #[test]
94 fn test_split_on_any_whitespace() {
95 new_ucmd!()
96 .pipe_in("a\nb\n")
97 .succeeds()
98 .stdout_only("a\nb\n");
99 }
100
101 #[test]
102 fn test_cycle() {
103 // The graph looks like: a --> b <==> c --> d
104 new_ucmd!()
105 .pipe_in("a b b c c d c b")
106 .fails_with_code(1)
107 .stdout_is("a\nb\nc\nd\n")
108 .stderr_is("tsort: -: input contains a loop:\ntsort: b\ntsort: c\n");
109 }
110
111 #[test]
112 fn test_two_cycles() {
113 // The graph looks like:
114 //
115 // a
116 // |
117 // V
118 // c <==> b <==> d
119 //
120 new_ucmd!()
121 .pipe_in("a b b c c b b d d b")
122 .fails_with_code(1)
123 .stdout_is("a\nb\nd\nc\n")
124 .stderr_is("tsort: -: input contains a loop:\ntsort: b\ntsort: c\ntsort: -: input contains a loop:\ntsort: b\ntsort: d\n");
125 }
126
127 #[test]
128 fn test_long_loop_no_stack_overflow() {
129 use std::fmt::Write;
130 const N: usize = 100_000;
131 let mut input = String::new();
132 for v in 0..N {
133 let next = (v + 1) % N;
134 let _ = write!(input, "{v} {next} ");
135 }
136 new_ucmd!()
137 .pipe_in(input)
138 .fails_with_code(1)
139 .stderr_contains("tsort: -: input contains a loop");
140 }
141
142 #[test]
143 fn test_loop_for_iterative_dfs_correctness() {
144 let input = r"
145 A B
146 B C
147 C B
148 C D
149 D A
150 ";
151
152 new_ucmd!()
153 .pipe_in(input)
154 .fails_with_code(1)
155 .stderr_contains("tsort: -: input contains a loop:\ntsort: B\ntsort: C");
156 }
157
158 const TSORT_LOOP_STDERR: &str = "tsort: f: input contains a loop:\ntsort: s\ntsort: t\n";
159 const TSORT_LOOP_STDERR_AC: &str = "tsort: f: input contains a loop:\ntsort: a\ntsort: b\ntsort: f: input contains a loop:\ntsort: a\ntsort: c\n";
160 const TSORT_ODD_ERROR: &str = "tsort: -: input contains an odd number of tokens\n";
161 const TSORT_EXTRA_OPERAND_ERROR: &str =
162 "tsort: extra operand 'g'\nTry 'tsort --help' for more information.\n";
163
164 #[test]
165 fn test_cycle_loop_from_file() {
166 let (at, mut ucmd) = at_and_ucmd!();
167 at.write("f", "t b\nt s\ns t\n");
168
169 ucmd.arg("f")
170 .fails_with_code(1)
171 .stdout_is("s\nt\nb\n")
172 .stderr_is(TSORT_LOOP_STDERR);
173 }
174
175 #[test]
176 fn test_cycle_loop_with_extra_node_from_file() {
177 let (at, mut ucmd) = at_and_ucmd!();
178 at.write("f", "t x\nt s\ns t\n");
179
180 ucmd.arg("f")
181 .fails_with_code(1)
182 .stdout_is("s\nt\nx\n")
183 .stderr_is(TSORT_LOOP_STDERR);
184 }
185
186 #[test]
187 fn test_cycle_loop_multiple_loops_from_file() {
188 let (at, mut ucmd) = at_and_ucmd!();
189 at.write("f", "a a\na b\na c\nc a\nb a\n");
190
191 ucmd.arg("f")
192 .fails_with_code(1)
193 .stdout_is("a\nc\nb\n")
194 .stderr_is(TSORT_LOOP_STDERR_AC);
195 }
196
197 #[test]
198 fn test_posix_graph_examples() {
199 new_ucmd!()
200 .pipe_in("a b c c d e\ng g\nf g e f\nh h\n")
201 .succeeds()
202 .stdout_only("a\nc\nd\nh\nb\ne\nf\ng\n");
203
204 new_ucmd!()
205 .pipe_in("b a\nd c\nz h x h r h\n")
206 .succeeds()
207 .stdout_only("b\nd\nr\nx\nz\na\nc\nh\n");
208 }
209
210 #[test]
211 fn test_linear_tree_graphs() {
212 new_ucmd!()
213 .pipe_in("a b b c c d d e e f f g\n")
214 .succeeds()
215 .stdout_only("a\nb\nc\nd\ne\nf\ng\n");
216
217 new_ucmd!()
218 .pipe_in("a b b c c d d e e f f g\nc x x y y z\n")
219 .succeeds()
220 .stdout_only("a\nb\nc\nx\nd\ny\ne\nz\nf\ng\n");
221
222 new_ucmd!()
223 .pipe_in("a b b c c d d e e f f g\nc x x y y z\nf r r s s t\n")
224 .succeeds()
225 .stdout_only("a\nb\nc\nx\nd\ny\ne\nz\nf\nr\ng\ns\nt\n");
226 }
227
228 #[test]
229 fn test_odd_number_of_tokens() {
230 new_ucmd!()
231 .pipe_in("a\n")
232 .fails_with_code(1)
233 .stderr_only(TSORT_ODD_ERROR);
234 }
235
236 #[test]
237 fn test_only_one_input_file() {
238 let (at, mut ucmd) = at_and_ucmd!();
239 at.write("f", "");
240 at.write("g", "");
241
242 ucmd.arg("f")
243 .arg("g")
244 .fails_with_code(1)
245 .stderr_only(TSORT_EXTRA_OPERAND_ERROR);
246 }
247
248 #[test]
249 fn test_nonexistent_file_error_includes_filename() {
250 new_ucmd!()
251 .arg("nosuchfile.txt")
252 .fails_with_code(1)
253 .stderr_only("tsort: nosuchfile.txt: No such file or directory\n");
254 }
255
256 #[test]
257 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
258 fn test_write_error() {
259 use std::fs::OpenOptions;
260
261 let (at, mut ucmd) = at_and_ucmd!();
262
263 at.write("input", "a d\n");
264
265 let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap();
266
267 ucmd.arg("input")
268 .set_stdout(dev_full)
269 .fails()
270 .stderr_contains("write error")
271 .stderr_contains("No space left on device");
272 }
273
274 #[test]
275 fn test_invalid_utf8_input() {
276 let (at, mut ucmd) = at_and_ucmd!();
277
278 // Equivalent to:
279 //
280 // hrllo
281 // awuyues
282 // apple
283 // iphone
284 // iphone
285 // a
286 // \xff
287 // \xff
288
289 std::fs::write(
290 at.plus("a"),
291 b"hrllo\nawuyues\napple\niphone\niphone\na\n\xff\n\xff\n",
292 )
293 .unwrap();
294
295 ucmd.arg("a")
296 .succeeds()
297 .stdout_only_bytes(b"apple\nhrllo\n\xff\niphone\nawuyues\na\n");
298 }