Debian Codegraph @

tests/by-util/test_csplit.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 use glob::glob;
7 use uutests::at_and_ucmd;
8 use uutests::new_ucmd;
9
10 /// Returns a string of numbers with the given range, each on a new line.
11 /// The upper bound is not included.
12 fn generate(from: u32, to: u32) -> String {
13 (from..to).fold(String::new(), |acc, v| format!("{acc}{v}\n"))
14 }
15
16 #[test]
17 fn test_line_numbers_suppress_matched_final_empty() {
18 // Repro for #7286
19 let (at, mut ucmd) = at_and_ucmd!();
20 ucmd.args(&["--suppress-matched", "-", "2", "4", "6"]) // stdin, split at 2/4/6
21 .pipe_in("1\n2\n3\n4\n5\n6\n")
22 .succeeds()
23 .stdout_only("2\n2\n2\n0\n");
24
25 // Expect four files: xx00:"1\n", xx01:"3\n", xx02:"5\n", xx03:""
26 let count = glob(&at.plus_as_string("xx*"))
27 .expect("there should be splits created")
28 .count();
29 assert_eq!(count, 4);
30 assert_eq!(at.read("xx00"), "1\n");
31 assert_eq!(at.read("xx01"), "3\n");
32 assert_eq!(at.read("xx02"), "5\n");
33 assert_eq!(at.read("xx03"), "");
34 }
35
36 #[test]
37 fn test_line_numbers_suppress_matched_final_empty_elided_with_z() {
38 let (at, mut ucmd) = at_and_ucmd!();
39 ucmd.args(&["--suppress-matched", "-z", "-", "2", "4", "6"]) // elide empty
40 .pipe_in("1\n2\n3\n4\n5\n6\n")
41 .succeeds()
42 .stdout_only("2\n2\n2\n");
43
44 // Expect three files: xx00:"1\n", xx01:"3\n", xx02:"5\n"
45 let count = glob(&at.plus_as_string("xx*"))
46 .expect("there should be splits created")
47 .count();
48 assert_eq!(count, 3);
49 assert_eq!(at.read("xx00"), "1\n");
50 assert_eq!(at.read("xx01"), "3\n");
51 assert_eq!(at.read("xx02"), "5\n");
52 }
53
54 #[test]
55 fn test_up_to_match_suppress_matched_final_empty() {
56 let (at, mut ucmd) = at_and_ucmd!();
57 ucmd.args(&["--suppress-matched", "-", "2", "/a/"])
58 .pipe_in("1\n2\n3\n4\na\n")
59 .succeeds()
60 .stdout_only("2\n4\n0\n");
61
62 let count = glob(&at.plus_as_string("xx*"))
63 .expect("there should be splits created")
64 .count();
65 assert_eq!(count, 3);
66 assert_eq!(at.read("xx00"), "1\n");
67 assert_eq!(at.read("xx01"), "3\n4\n");
68 assert_eq!(at.read("xx02"), "");
69 }
70
71 #[test]
72 fn test_up_to_match_offset_final_empty() {
73 let (at, mut ucmd) = at_and_ucmd!();
74 ucmd.args(&["-", "/a/+1"])
75 .pipe_in("1\na\n")
76 .succeeds()
77 .stdout_only("4\n0\n");
78
79 let count = glob(&at.plus_as_string("xx*"))
80 .expect("there should be splits created")
81 .count();
82 assert_eq!(count, 2);
83 assert_eq!(at.read("xx00"), "1\na\n");
84 assert_eq!(at.read("xx01"), "");
85 }
86
87 #[test]
88 fn test_invalid_arg() {
89 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
90 }
91
92 #[test]
93 fn test_stdin() {
94 let (at, mut ucmd) = at_and_ucmd!();
95 ucmd.args(&["-", "10"])
96 .pipe_in(generate(1, 51))
97 .succeeds()
98 .stdout_only("18\n123\n");
99
100 let count = glob(&at.plus_as_string("xx*"))
101 .expect("there should be splits created")
102 .count();
103 assert_eq!(count, 2);
104 assert_eq!(at.read("xx00"), generate(1, 10));
105 assert_eq!(at.read("xx01"), generate(10, 51));
106 }
107
108 #[test]
109 fn test_up_to_line() {
110 let (at, mut ucmd) = at_and_ucmd!();
111 ucmd.args(&["numbers50.txt", "10"])
112 .succeeds()
113 .stdout_only("18\n123\n");
114
115 let count = glob(&at.plus_as_string("xx*"))
116 .expect("there should be splits created")
117 .count();
118 assert_eq!(count, 2);
119 assert_eq!(at.read("xx00"), generate(1, 10));
120 assert_eq!(at.read("xx01"), generate(10, 51));
121 }
122
123 #[test]
124 fn test_up_to_line_repeat_twice() {
125 let (at, mut ucmd) = at_and_ucmd!();
126 ucmd.args(&["numbers50.txt", "10", "{2}"])
127 .succeeds()
128 .stdout_only("18\n30\n30\n63\n");
129
130 let count = glob(&at.plus_as_string("xx*"))
131 .expect("there should be splits created")
132 .count();
133 assert_eq!(count, 4);
134 assert_eq!(at.read("xx00"), generate(1, 10));
135 assert_eq!(at.read("xx01"), generate(10, 20));
136 assert_eq!(at.read("xx02"), generate(20, 30));
137 assert_eq!(at.read("xx03"), generate(30, 51));
138 }
139
140 #[test]
141 fn test_up_to_line_sequence() {
142 let (at, mut ucmd) = at_and_ucmd!();
143 ucmd.args(&["numbers50.txt", "10", "25"])
144 .succeeds()
145 .stdout_only("18\n45\n78\n");
146
147 let count = glob(&at.plus_as_string("xx*"))
148 .expect("there should be splits created")
149 .count();
150 assert_eq!(count, 3);
151 assert_eq!(at.read("xx00"), generate(1, 10));
152 assert_eq!(at.read("xx01"), generate(10, 25));
153 assert_eq!(at.read("xx02"), generate(25, 51));
154 }
155
156 #[test]
157 fn test_up_to_line_with_non_ascii_repeat() {
158 // we use a different error message than GNU
159 new_ucmd!()
160 .args(&["numbers50.txt", "10", "{𝟚}"])
161 .fails()
162 .stderr_contains("invalid pattern");
163 }
164
165 #[test]
166 fn test_up_to_match() {
167 let (at, mut ucmd) = at_and_ucmd!();
168 ucmd.args(&["numbers50.txt", "/9$/"])
169 .succeeds()
170 .stdout_only("16\n125\n");
171
172 let count = glob(&at.plus_as_string("xx*"))
173 .expect("there should be splits created")
174 .count();
175 assert_eq!(count, 2);
176 assert_eq!(at.read("xx00"), generate(1, 9));
177 assert_eq!(at.read("xx01"), generate(9, 51));
178 }
179
180 #[test]
181 fn test_up_to_match_repeat_twice() {
182 let (at, mut ucmd) = at_and_ucmd!();
183 ucmd.args(&["numbers50.txt", "/9$/", "{2}"])
184 .succeeds()
185 .stdout_only("16\n29\n30\n66\n");
186
187 let count = glob(&at.plus_as_string("xx*"))
188 .expect("there should be splits created")
189 .count();
190 assert_eq!(count, 4);
191 assert_eq!(at.read("xx00"), generate(1, 9));
192 assert_eq!(at.read("xx01"), generate(9, 19));
193 assert_eq!(at.read("xx02"), generate(19, 29));
194 assert_eq!(at.read("xx03"), generate(29, 51));
195 }
196
197 #[test]
198 fn test_up_to_match_sequence() {
199 let (at, mut ucmd) = at_and_ucmd!();
200 ucmd.args(&["numbers50.txt", "/9$/", "/5$/"])
201 .succeeds()
202 .stdout_only("16\n17\n108\n");
203
204 let count = glob(&at.plus_as_string("xx*"))
205 .expect("there should be splits created")
206 .count();
207 assert_eq!(count, 3);
208 assert_eq!(at.read("xx00"), generate(1, 9));
209 assert_eq!(at.read("xx01"), generate(9, 15));
210 assert_eq!(at.read("xx02"), generate(15, 51));
211 }
212
213 #[test]
214 fn test_up_to_match_offset() {
215 for offset in ["3", "+3"] {
216 let (at, mut ucmd) = at_and_ucmd!();
217 ucmd.args(&["numbers50.txt", &format!("/9$/{offset}")])
218 .succeeds()
219 .stdout_only("24\n117\n");
220
221 let count = glob(&at.plus_as_string("xx*"))
222 .expect("there should be splits created")
223 .count();
224 assert_eq!(count, 2);
225 assert_eq!(at.read("xx00"), generate(1, 12));
226 assert_eq!(at.read("xx01"), generate(12, 51));
227 at.remove("xx00");
228 at.remove("xx01");
229 }
230 }
231
232 #[test]
233 fn test_up_to_match_offset_repeat_twice() {
234 let (at, mut ucmd) = at_and_ucmd!();
235 ucmd.args(&["numbers50.txt", "/9$/+3", "{2}"])
236 .succeeds()
237 .stdout_only("24\n30\n30\n57\n");
238
239 let count = glob(&at.plus_as_string("xx*"))
240 .expect("there should be splits created")
241 .count();
242 assert_eq!(count, 4);
243 assert_eq!(at.read("xx00"), generate(1, 12));
244 assert_eq!(at.read("xx01"), generate(12, 22));
245 assert_eq!(at.read("xx02"), generate(22, 32));
246 assert_eq!(at.read("xx03"), generate(32, 51));
247 }
248
249 #[test]
250 fn test_up_to_match_non_ascii_offset() {
251 // we use a different error message than GNU
252 new_ucmd!()
253 .args(&["numbers50.txt", "/9$/𝟚"])
254 .fails()
255 .stderr_contains("invalid pattern");
256 }
257
258 #[test]
259 fn test_up_to_match_negative_offset() {
260 let (at, mut ucmd) = at_and_ucmd!();
261 ucmd.args(&["numbers50.txt", "/9$/-3"])
262 .succeeds()
263 .stdout_only("10\n131\n");
264
265 let count = glob(&at.plus_as_string("xx*"))
266 .expect("there should be splits created")
267 .count();
268 assert_eq!(count, 2);
269 assert_eq!(at.read("xx00"), generate(1, 6));
270 assert_eq!(at.read("xx01"), generate(6, 51));
271 }
272
273 #[test]
274 fn test_up_to_match_negative_offset_min_i32() {
275 new_ucmd!()
276 .args(&["numbers50.txt", "/45/-2147483648"])
277 .fails()
278 .stderr_is("csplit: '/45/-2147483648': line number out of range\n");
279 }
280
281 #[test]
282 fn test_skip_to_match_negative_offset_min_i32() {
283 new_ucmd!()
284 .args(&["numbers50.txt", "%45%-2147483648"])
285 .fails()
286 .stderr_is("csplit: '%45%-2147483648': line number out of range\n");
287 }
288
289 #[test]
290 fn test_up_to_match_negative_offset_repeat_twice() {
291 let (at, mut ucmd) = at_and_ucmd!();
292 ucmd.args(&["numbers50.txt", "/9$/-3", "{2}"])
293 .succeeds()
294 .stdout_only("10\n26\n30\n75\n");
295
296 let count = glob(&at.plus_as_string("xx*"))
297 .expect("there should be splits created")
298 .count();
299 assert_eq!(count, 4);
300 assert_eq!(at.read("xx00"), generate(1, 6));
301 assert_eq!(at.read("xx01"), generate(6, 16));
302 assert_eq!(at.read("xx02"), generate(16, 26));
303 assert_eq!(at.read("xx03"), generate(26, 51));
304 }
305
306 #[test]
307 fn test_up_to_match_repeat_always() {
308 let (at, mut ucmd) = at_and_ucmd!();
309 ucmd.args(&["numbers50.txt", "/9$/", "{*}"])
310 .succeeds()
311 .stdout_only("16\n29\n30\n30\n30\n6\n");
312
313 let count = glob(&at.plus_as_string("xx*"))
314 .expect("there should be splits created")
315 .count();
316 assert_eq!(count, 6);
317 assert_eq!(at.read("xx00"), generate(1, 9));
318 assert_eq!(at.read("xx01"), generate(9, 19));
319 assert_eq!(at.read("xx02"), generate(19, 29));
320 assert_eq!(at.read("xx03"), generate(29, 39));
321 assert_eq!(at.read("xx04"), generate(39, 49));
322 assert_eq!(at.read("xx05"), generate(49, 51));
323 }
324
325 #[test]
326 fn test_up_to_match_repeat_over() {
327 let (at, mut ucmd) = at_and_ucmd!();
328 ucmd.args(&["numbers50.txt", "/9$/", "{50}"])
329 .fails()
330 .stdout_is("16\n29\n30\n30\n30\n6\n")
331 .stderr_is("csplit: '/9$/': match not found on repetition 5\n");
332
333 let count = glob(&at.plus_as_string("xx*"))
334 .expect("there should be splits created")
335 .count();
336 assert_eq!(count, 0);
337
338 let (at, mut ucmd) = at_and_ucmd!();
339 ucmd.args(&["numbers50.txt", "/9$/", "{50}", "-k"])
340 .fails()
341 .stdout_is("16\n29\n30\n30\n30\n6\n")
342 .stderr_is("csplit: '/9$/': match not found on repetition 5\n");
343
344 let count = glob(&at.plus_as_string("xx*"))
345 .expect("there should be splits created")
346 .count();
347 assert_eq!(count, 6);
348 assert_eq!(at.read("xx00"), generate(1, 9));
349 assert_eq!(at.read("xx01"), generate(9, 19));
350 assert_eq!(at.read("xx02"), generate(19, 29));
351 assert_eq!(at.read("xx03"), generate(29, 39));
352 assert_eq!(at.read("xx04"), generate(39, 49));
353 assert_eq!(at.read("xx05"), generate(49, 51));
354 }
355
356 #[test]
357 fn test_skip_to_match() {
358 let (at, mut ucmd) = at_and_ucmd!();
359 ucmd.args(&["numbers50.txt", "%23%"])
360 .succeeds()
361 .stdout_only("84\n");
362
363 let count = glob(&at.plus_as_string("xx*"))
364 .expect("there should be splits created")
365 .count();
366 assert_eq!(count, 1);
367 assert_eq!(at.read("xx00"), generate(23, 51));
368 }
369
370 #[test]
371 fn test_skip_to_match_sequence1() {
372 let (at, mut ucmd) = at_and_ucmd!();
373 ucmd.args(&["numbers50.txt", "%0$%", "%^4%"])
374 .succeeds()
375 .stdout_only("33\n");
376
377 let count = glob(&at.plus_as_string("xx*"))
378 .expect("there should be splits created")
379 .count();
380 assert_eq!(count, 1);
381 assert_eq!(at.read("xx00"), generate(40, 51));
382 }
383
384 #[test]
385 fn test_skip_to_match_sequence2() {
386 let (at, mut ucmd) = at_and_ucmd!();
387 ucmd.args(&["numbers50.txt", "%0$%", "{1}", "%^4%"])
388 .succeeds()
389 .stdout_only("33\n");
390
391 let count = glob(&at.plus_as_string("xx*"))
392 .expect("there should be splits created")
393 .count();
394 assert_eq!(count, 1);
395 assert_eq!(at.read("xx00"), generate(40, 51));
396 }
397
398 #[test]
399 fn test_skip_to_match_sequence3() {
400 let (at, mut ucmd) = at_and_ucmd!();
401 ucmd.args(&["numbers50.txt", "%0$%", "{1}", "/^4/"])
402 .succeeds()
403 .stdout_only("60\n33\n");
404
405 let count = glob(&at.plus_as_string("xx*"))
406 .expect("there should be splits created")
407 .count();
408 assert_eq!(count, 2);
409 assert_eq!(at.read("xx00"), generate(20, 40));
410 assert_eq!(at.read("xx01"), generate(40, 51));
411 }
412
413 #[test]
414 fn test_skip_to_match_sequence4() {
415 let (at, mut ucmd) = at_and_ucmd!();
416 ucmd.args(&["numbers50.txt", "%0$%", "/^4/"])
417 .succeeds()
418 .stdout_only("90\n33\n");
419
420 let count = glob(&at.plus_as_string("xx*"))
421 .expect("there should be splits created")
422 .count();
423 assert_eq!(count, 2);
424 assert_eq!(at.read("xx00"), generate(10, 40));
425 assert_eq!(at.read("xx01"), generate(40, 51));
426 }
427
428 #[test]
429 fn test_skip_to_match_offset() {
430 for offset in ["3", "+3"] {
431 let (at, mut ucmd) = at_and_ucmd!();
432 ucmd.args(&["numbers50.txt", &format!("%23%{offset}")])
433 .succeeds()
434 .stdout_only("75\n");
435
436 let count = glob(&at.plus_as_string("xx*"))
437 .expect("there should be splits created")
438 .count();
439 assert_eq!(count, 1);
440 assert_eq!(at.read("xx00"), generate(26, 51));
441 at.remove("xx00");
442 }
443 }
444
445 #[test]
446 fn test_skip_to_match_offset_suppress_empty() {
447 let (at, mut ucmd) = at_and_ucmd!();
448 ucmd.args(&["-z", "-", "%a%1"])
449 .pipe_in("a\n")
450 .succeeds()
451 .no_output();
452 assert!(!at.file_exists("xx00"));
453 }
454
455 #[test]
456 fn test_skip_to_match_negative_offset() {
457 let (at, mut ucmd) = at_and_ucmd!();
458 ucmd.args(&["numbers50.txt", "%23%-3"])
459 .succeeds()
460 .stdout_only("93\n");
461
462 let count = glob(&at.plus_as_string("xx*"))
463 .expect("there should be splits created")
464 .count();
465 assert_eq!(count, 1);
466 assert_eq!(at.read("xx00"), generate(20, 51));
467 }
468
469 #[test]
470 fn test_skip_to_match_repeat_always() {
471 let (at, mut ucmd) = at_and_ucmd!();
472 ucmd.args(&["numbers50.txt", "%0$%", "{*}"])
473 .succeeds()
474 .no_stdout();
475
476 let count = glob(&at.plus_as_string("xx*")).unwrap().count();
477 assert_eq!(count, 0);
478 }
479
480 #[test]
481 fn test_mix() {
482 let (at, mut ucmd) = at_and_ucmd!();
483 ucmd.args(&["numbers50.txt", "13", "%25%", "/0$/"])
484 .succeeds()
485 .stdout_only("27\n15\n63\n");
486
487 let count = glob(&at.plus_as_string("xx*"))
488 .expect("there should be splits created")
489 .count();
490 assert_eq!(count, 3);
491 assert_eq!(at.read("xx00"), generate(1, 13));
492 assert_eq!(at.read("xx01"), generate(25, 30));
493 assert_eq!(at.read("xx02"), generate(30, 51));
494 }
495
496 #[test]
497 fn test_option_keep() {
498 let (at, mut ucmd) = at_and_ucmd!();
499 ucmd.args(&["-k", "numbers50.txt", "/20/", "/nope/"])
500 .fails()
501 .stderr_is("csplit: '/nope/': match not found\n")
502 .stdout_is("48\n93\n");
503
504 let count = glob(&at.plus_as_string("xx*"))
505 .expect("there should be splits created")
506 .count();
507 assert_eq!(count, 2);
508 assert_eq!(at.read("xx00"), generate(1, 20));
509 assert_eq!(at.read("xx01"), generate(20, 51));
510 }
511
512 #[test]
513 fn test_option_quiet() {
514 for arg in ["-q", "--quiet", "-s", "--silent"] {
515 let (at, mut ucmd) = at_and_ucmd!();
516 ucmd.args(&[arg, "numbers50.txt", "13", "%25%", "/0$/"])
517 .succeeds()
518 .no_stdout();
519
520 let count = glob(&at.plus_as_string("xx*"))
521 .expect("there should be splits created")
522 .count();
523 assert_eq!(count, 3);
524 assert_eq!(at.read("xx00"), generate(1, 13));
525 assert_eq!(at.read("xx01"), generate(25, 30));
526 assert_eq!(at.read("xx02"), generate(30, 51));
527 at.remove("xx00");
528 at.remove("xx01");
529 at.remove("xx02");
530 }
531 }
532
533 #[test]
534 fn test_option_prefix() {
535 let (at, mut ucmd) = at_and_ucmd!();
536 ucmd.args(&["--prefix", "dog", "numbers50.txt", "13", "%25%", "/0$/"])
537 .succeeds()
538 .stdout_only("27\n15\n63\n");
539
540 let count = glob(&at.plus_as_string("dog*"))
541 .expect("there should be splits created")
542 .count();
543 assert_eq!(count, 3);
544 assert_eq!(at.read("dog00"), generate(1, 13));
545 assert_eq!(at.read("dog01"), generate(25, 30));
546 assert_eq!(at.read("dog02"), generate(30, 51));
547 }
548
549 #[test]
550 fn test_negative_offset_at_start() {
551 let (at, mut ucmd) = at_and_ucmd!();
552 ucmd.args(&["-", "/a/-1", "{*}"])
553 .pipe_in("\na\n")
554 .succeeds()
555 .stdout_only("0\n3\n");
556
557 let count = glob(&at.plus_as_string("xx*"))
558 .expect("there should be splits created")
559 .count();
560 assert_eq!(count, 2);
561 assert_eq!(at.read("xx00"), "");
562 assert_eq!(at.read("xx01"), "\na\n");
563 }
564
565 #[test]
566 fn test_up_to_match_option_suppress_matched() {
567 let (at, mut ucmd) = at_and_ucmd!();
568 ucmd.args(&["numbers50.txt", "--suppress-matched", "/0$/", "{*}"])
569 .succeeds()
570 .stdout_only("18\n27\n27\n27\n27\n0\n");
571
572 let count = glob(&at.plus_as_string("xx*"))
573 .expect("there should be splits created")
574 .count();
575 assert_eq!(count, 6);
576 assert_eq!(at.read("xx00"), generate(1, 10));
577 assert_eq!(at.read("xx01"), generate(11, 20));
578 assert_eq!(at.read("xx02"), generate(21, 30));
579 assert_eq!(at.read("xx03"), generate(31, 40));
580 assert_eq!(at.read("xx04"), generate(41, 50));
581 assert_eq!(at.read("xx05"), "");
582 }
583
584 #[test]
585 fn test_up_to_match_offset_option_suppress_matched() {
586 let (at, mut ucmd) = at_and_ucmd!();
587 ucmd.args(&["numbers50.txt", "--suppress-matched", "/10/+4"])
588 .succeeds()
589 .stdout_only("30\n108\n");
590
591 let count = glob(&at.plus_as_string("xx*"))
592 .expect("there should be splits created")
593 .count();
594 assert_eq!(count, 2);
595 assert_eq!(at.read("xx00"), generate(1, 14));
596 assert_eq!(at.read("xx01"), generate(15, 51));
597 }
598
599 #[test]
600 fn test_up_to_match_negative_offset_option_suppress_matched() {
601 let (at, mut ucmd) = at_and_ucmd!();
602 ucmd.args(&["numbers50.txt", "--suppress-matched", "/10/-4"])
603 .succeeds()
604 .stdout_only("10\n129\n");
605
606 let count = glob(&at.plus_as_string("xx*"))
607 .expect("there should be splits created")
608 .count();
609 assert_eq!(count, 2);
610 assert_eq!(at.read("xx00"), generate(1, 6));
611 assert_eq!(at.read("xx01"), generate(7, 51));
612 }
613
614 #[test]
615 fn test_up_to_line_option_suppress_matched() {
616 let (at, mut ucmd) = at_and_ucmd!();
617 ucmd.args(&["numbers50.txt", "--suppress-matched", "10"])
618 .succeeds()
619 .stdout_only("18\n120\n");
620
621 let count = glob(&at.plus_as_string("xx*"))
622 .expect("there should be splits created")
623 .count();
624 assert_eq!(count, 2);
625 assert_eq!(at.read("xx00"), generate(1, 10));
626 assert_eq!(at.read("xx01"), generate(11, 51));
627 }
628
629 #[test]
630 fn test_skip_to_match_option_suppress_matched() {
631 let (at, mut ucmd) = at_and_ucmd!();
632 ucmd.args(&["numbers50.txt", "--suppress-matched", "%0$%"])
633 .succeeds()
634 .stdout_only("120\n");
635
636 let count = glob(&at.plus_as_string("xx*"))
637 .expect("there should be splits created")
638 .count();
639 assert_eq!(count, 1);
640 assert_eq!(at.read("xx00"), generate(11, 51));
641 }
642
643 #[test]
644 fn test_option_elide_empty_file1() {
645 let (at, mut ucmd) = at_and_ucmd!();
646 ucmd.args(&["numbers50.txt", "--suppress-matched", "-z", "/0$/", "{*}"])
647 .succeeds()
648 .stdout_only("18\n27\n27\n27\n27\n");
649
650 let count = glob(&at.plus_as_string("xx*"))
651 .expect("there should be splits created")
652 .count();
653 assert_eq!(count, 5);
654 assert_eq!(at.read("xx00"), generate(1, 10));
655 assert_eq!(at.read("xx01"), generate(11, 20));
656 assert_eq!(at.read("xx02"), generate(21, 30));
657 assert_eq!(at.read("xx03"), generate(31, 40));
658 assert_eq!(at.read("xx04"), generate(41, 50));
659 }
660
661 #[test]
662 fn test_option_elide_empty_file2() {
663 let (at, mut ucmd) = at_and_ucmd!();
664 ucmd.args(&["-", "-z", "/a/-1", "{*}"])
665 .pipe_in("\na\n")
666 .succeeds()
667 .stdout_only("3\n");
668
669 let count = glob(&at.plus_as_string("xx*"))
670 .expect("there should be splits created")
671 .count();
672 assert_eq!(count, 1);
673 assert_eq!(at.read("xx00"), "\na\n");
674 }
675
676 #[test]
677 fn test_up_to_match_context_overflow() {
678 let (at, mut ucmd) = at_and_ucmd!();
679 ucmd.args(&["numbers50.txt", "/45/+10"])
680 .fails()
681 .stdout_is("141\n")
682 .stderr_is("csplit: '/45/+10': line number out of range\n");
683
684 let count = glob(&at.plus_as_string("xx*"))
685 .expect("counting splits")
686 .count();
687 assert_eq!(count, 0);
688 let (at, mut ucmd) = at_and_ucmd!();
689
690 ucmd.args(&["numbers50.txt", "/45/+10", "-k"])
691 .fails()
692 .stdout_is("141\n")
693 .stderr_is("csplit: '/45/+10': line number out of range\n");
694
695 let count = glob(&at.plus_as_string("xx*"))
696 .expect("counting splits")
697 .count();
698 assert_eq!(count, 1);
699 assert_eq!(at.read("xx00"), generate(1, 51));
700 }
701
702 #[test]
703 fn test_skip_to_match_context_underflow() {
704 let (at, mut ucmd) = at_and_ucmd!();
705 ucmd.args(&["numbers50.txt", "%5%-10"])
706 .fails()
707 .stderr_only("csplit: '%5%-10': line number out of range\n");
708
709 let count = glob(&at.plus_as_string("xx*"))
710 .expect("counting splits")
711 .count();
712 assert_eq!(count, 0);
713
714 let (at, mut ucmd) = at_and_ucmd!();
715 ucmd.args(&["numbers50.txt", "%5%-10", "-k"])
716 .fails()
717 .stderr_only("csplit: '%5%-10': line number out of range\n");
718
719 let count = glob(&at.plus_as_string("xx*"))
720 .expect("counting splits")
721 .count();
722 assert_eq!(count, 0);
723 }
724
725 #[test]
726 fn test_skip_to_match_context_overflow() {
727 let (at, mut ucmd) = at_and_ucmd!();
728 ucmd.args(&["numbers50.txt", "%45%+10"])
729 .fails()
730 .stderr_is("csplit: '%45%+10': line number out of range\n");
731
732 let count = glob(&at.plus_as_string("xx*"))
733 .expect("counting splits")
734 .count();
735 assert_eq!(count, 0);
736
737 let (at, mut ucmd) = at_and_ucmd!();
738 ucmd.args(&["numbers50.txt", "%45%+10", "-k"])
739 .fails()
740 .stderr_only("csplit: '%45%+10': line number out of range\n");
741
742 let count = glob(&at.plus_as_string("xx*"))
743 .expect("counting splits")
744 .count();
745 assert_eq!(count, 0);
746 }
747
748 #[test]
749 fn test_up_to_no_match1() {
750 let (at, mut ucmd) = at_and_ucmd!();
751 ucmd.args(&["numbers50.txt", "/4/", "/nope/"])
752 .fails()
753 .stdout_is("6\n135\n")
754 .stderr_is("csplit: '/nope/': match not found\n");
755
756 let count = glob(&at.plus_as_string("xx*"))
757 .expect("counting splits")
758 .count();
759 assert_eq!(count, 0);
760
761 let (at, mut ucmd) = at_and_ucmd!();
762 ucmd.args(&["numbers50.txt", "/4/", "/nope/", "-k"])
763 .fails()
764 .stdout_is("6\n135\n")
765 .stderr_is("csplit: '/nope/': match not found\n");
766
767 let count = glob(&at.plus_as_string("xx*"))
768 .expect("counting splits")
769 .count();
770 assert_eq!(count, 2);
771 assert_eq!(at.read("xx00"), generate(1, 4));
772 assert_eq!(at.read("xx01"), generate(4, 51));
773 }
774
775 #[test]
776 fn test_up_to_no_match2() {
777 let (at, mut ucmd) = at_and_ucmd!();
778 ucmd.args(&["numbers50.txt", "/4/", "/nope/", "{50}"])
779 .fails()
780 .stdout_is("6\n135\n")
781 .stderr_is("csplit: '/nope/': match not found\n");
782
783 let count = glob(&at.plus_as_string("xx*"))
784 .expect("counting splits")
785 .count();
786 assert_eq!(count, 0);
787
788 let (at, mut ucmd) = at_and_ucmd!();
789 ucmd.args(&["numbers50.txt", "/4/", "/nope/", "{50}", "-k"])
790 .fails()
791 .stdout_is("6\n135\n")
792 .stderr_is("csplit: '/nope/': match not found\n");
793
794 let count = glob(&at.plus_as_string("xx*"))
795 .expect("counting splits")
796 .count();
797 assert_eq!(count, 2);
798 assert_eq!(at.read("xx00"), generate(1, 4));
799 assert_eq!(at.read("xx01"), generate(4, 51));
800 }
801
802 #[test]
803 fn test_up_to_no_match3() {
804 let (at, mut ucmd) = at_and_ucmd!();
805 ucmd.args(&["numbers50.txt", "/0$/", "{50}"])
806 .fails()
807 .stdout_is("18\n30\n30\n30\n30\n3\n")
808 .stderr_is("csplit: '/0$/': match not found on repetition 5\n");
809
810 let count = glob(&at.plus_as_string("xx*"))
811 .expect("counting splits")
812 .count();
813 assert_eq!(count, 0);
814
815 let (at, mut ucmd) = at_and_ucmd!();
816 ucmd.args(&["numbers50.txt", "/0$/", "{50}", "-k"])
817 .fails()
818 .stdout_is("18\n30\n30\n30\n30\n3\n")
819 .stderr_is("csplit: '/0$/': match not found on repetition 5\n");
820
821 let count = glob(&at.plus_as_string("xx*"))
822 .expect("counting splits")
823 .count();
824 assert_eq!(count, 6);
825 assert_eq!(at.read("xx00"), generate(1, 10));
826 assert_eq!(at.read("xx01"), generate(10, 20));
827 assert_eq!(at.read("xx02"), generate(20, 30));
828 assert_eq!(at.read("xx03"), generate(30, 40));
829 assert_eq!(at.read("xx04"), generate(40, 50));
830 assert_eq!(at.read("xx05"), "50\n");
831 }
832
833 #[test]
834 fn test_up_to_no_match4() {
835 let (at, mut ucmd) = at_and_ucmd!();
836 ucmd.args(&["numbers50.txt", "/nope/", "/4/"])
837 .fails()
838 .stdout_is("141\n")
839 .stderr_is("csplit: '/nope/': match not found\n");
840
841 let count = glob(&at.plus_as_string("xx*"))
842 .expect("counting splits")
843 .count();
844 assert_eq!(count, 0);
845
846 let (at, mut ucmd) = at_and_ucmd!();
847 ucmd.args(&["numbers50.txt", "/nope/", "/4/", "-k"])
848 .fails()
849 .stdout_is("141\n")
850 .stderr_is("csplit: '/nope/': match not found\n");
851
852 let count = glob(&at.plus_as_string("xx*"))
853 .expect("counting splits")
854 .count();
855 assert_eq!(count, 1);
856 assert_eq!(at.read("xx00"), generate(1, 51));
857 }
858
859 #[test]
860 fn test_up_to_no_match5() {
861 let (at, mut ucmd) = at_and_ucmd!();
862 ucmd.args(&["numbers50.txt", "/nope/", "{*}"])
863 .succeeds()
864 .stdout_only("141\n");
865
866 let count = glob(&at.plus_as_string("xx*"))
867 .expect("counting splits")
868 .count();
869 assert_eq!(count, 1);
870 assert_eq!(at.read("xx00"), generate(1, 51));
871 }
872
873 #[test]
874 fn test_up_to_no_match6() {
875 let (at, mut ucmd) = at_and_ucmd!();
876 ucmd.args(&["numbers50.txt", "/nope/-5"])
877 .fails()
878 .stdout_is("141\n")
879 .stderr_is("csplit: '/nope/-5': match not found\n");
880
881 let count = glob(&at.plus_as_string("xx*"))
882 .expect("counting splits")
883 .count();
884 assert_eq!(count, 0);
885
886 let (at, mut ucmd) = at_and_ucmd!();
887 ucmd.args(&["numbers50.txt", "/nope/-5", "-k"])
888 .fails()
889 .stdout_is("141\n")
890 .stderr_is("csplit: '/nope/-5': match not found\n");
891
892 let count = glob(&at.plus_as_string("xx*"))
893 .expect("counting splits")
894 .count();
895 assert_eq!(count, 1);
896 assert_eq!(at.read("xx00"), generate(1, 51));
897 }
898
899 #[test]
900 fn test_up_to_no_match7() {
901 let (at, mut ucmd) = at_and_ucmd!();
902 ucmd.args(&["numbers50.txt", "/nope/+5"])
903 .fails()
904 .stdout_is("141\n")
905 .stderr_is("csplit: '/nope/+5': match not found\n");
906
907 let count = glob(&at.plus_as_string("xx*"))
908 .expect("counting splits")
909 .count();
910 assert_eq!(count, 0);
911
912 let (at, mut ucmd) = at_and_ucmd!();
913 ucmd.args(&["numbers50.txt", "/nope/+5", "-k"])
914 .fails()
915 .stdout_is("141\n")
916 .stderr_is("csplit: '/nope/+5': match not found\n");
917
918 let count = glob(&at.plus_as_string("xx*"))
919 .expect("counting splits")
920 .count();
921 assert_eq!(count, 1);
922 assert_eq!(at.read("xx00"), generate(1, 51));
923 }
924
925 #[test]
926 fn test_skip_to_no_match1() {
927 let (at, mut ucmd) = at_and_ucmd!();
928 ucmd.args(&["numbers50.txt", "%nope%"])
929 .fails()
930 .stderr_only("csplit: '%nope%': match not found\n");
931
932 let count = glob(&at.plus_as_string("xx*"))
933 .expect("counting splits")
934 .count();
935 assert_eq!(count, 0);
936 }
937
938 #[test]
939 fn test_skip_to_no_match2() {
940 let (at, mut ucmd) = at_and_ucmd!();
941 ucmd.args(&["numbers50.txt", "%nope%", "{50}"])
942 .fails()
943 .stderr_only("csplit: '%nope%': match not found\n");
944
945 let count = glob(&at.plus_as_string("xx*"))
946 .expect("counting splits")
947 .count();
948 assert_eq!(count, 0);
949 }
950
951 #[test]
952 fn test_skip_to_no_match3() {
953 let (at, mut ucmd) = at_and_ucmd!();
954 ucmd.args(&["numbers50.txt", "%0$%", "{50}"])
955 .fails()
956 .stderr_only("csplit: '%0$%': match not found on repetition 5\n");
957
958 let count = glob(&at.plus_as_string("xx*"))
959 .expect("counting splits")
960 .count();
961 assert_eq!(count, 0);
962 }
963
964 #[test]
965 fn test_skip_to_no_match4() {
966 let (at, mut ucmd) = at_and_ucmd!();
967 ucmd.args(&["numbers50.txt", "%nope%", "/4/"])
968 .fails()
969 .stderr_only("csplit: '%nope%': match not found\n");
970
971 let count = glob(&at.plus_as_string("xx*"))
972 .expect("counting splits")
973 .count();
974 assert_eq!(count, 0);
975 }
976
977 #[test]
978 fn test_skip_to_no_match5() {
979 let (at, mut ucmd) = at_and_ucmd!();
980 ucmd.args(&["numbers50.txt", "%nope%", "{*}"])
981 .succeeds()
982 .no_output();
983
984 let count = glob(&at.plus_as_string("xx*"))
985 .expect("counting splits")
986 .count();
987 assert_eq!(count, 0);
988 }
989
990 #[test]
991 fn test_skip_to_no_match6() {
992 let (at, mut ucmd) = at_and_ucmd!();
993 ucmd.args(&["numbers50.txt", "%nope%-5"])
994 .fails()
995 .stderr_only("csplit: '%nope%-5': match not found\n");
996
997 let count = glob(&at.plus_as_string("xx*"))
998 .expect("counting splits")
999 .count();
1000 assert_eq!(count, 0);
1001 }
1002
1003 #[test]
1004 fn test_skip_to_no_match7() {
1005 let (at, mut ucmd) = at_and_ucmd!();
1006 ucmd.args(&["numbers50.txt", "%nope%+5"])
1007 .fails()
1008 .stderr_only("csplit: '%nope%+5': match not found\n");
1009
1010 let count = glob(&at.plus_as_string("xx*"))
1011 .expect("counting splits")
1012 .count();
1013 assert_eq!(count, 0);
1014 }
1015
1016 #[test]
1017 fn test_no_match() {
1018 let (at, mut ucmd) = at_and_ucmd!();
1019 ucmd.args(&["numbers50.txt", "%nope%"])
1020 .fails()
1021 .stderr_only("csplit: '%nope%': match not found\n");
1022
1023 let count = glob(&at.plus_as_string("xx*"))
1024 .expect("counting splits")
1025 .count();
1026 assert_eq!(count, 0);
1027
1028 let (at, mut ucmd) = at_and_ucmd!();
1029 ucmd.args(&["numbers50.txt", "/nope/"])
1030 .fails()
1031 .stdout_is("141\n")
1032 .stderr_is("csplit: '/nope/': match not found\n");
1033
1034 let count = glob(&at.plus_as_string("xx*"))
1035 .expect("counting splits")
1036 .count();
1037 assert_eq!(count, 0);
1038 }
1039
1040 #[test]
1041 fn test_too_small_line_num() {
1042 let (at, mut ucmd) = at_and_ucmd!();
1043 ucmd.args(&["numbers50.txt", "/20/", "10", "/40/"])
1044 .succeeds()
1045 .stdout_only("48\n0\n60\n33\n");
1046
1047 let count = glob(&at.plus_as_string("xx*"))
1048 .expect("there should be splits created")
1049 .count();
1050 assert_eq!(count, 4);
1051 assert_eq!(at.read("xx00"), generate(1, 20));
1052 assert_eq!(at.read("xx01"), "");
1053 assert_eq!(at.read("xx02"), generate(20, 40));
1054 assert_eq!(at.read("xx03"), generate(40, 51));
1055 }
1056
1057 #[test]
1058 fn test_too_small_line_num_equal() {
1059 let (at, mut ucmd) = at_and_ucmd!();
1060 ucmd.args(&["numbers50.txt", "/20/", "20"])
1061 .succeeds()
1062 .stdout_only("48\n0\n93\n");
1063
1064 let count = glob(&at.plus_as_string("xx*"))
1065 .expect("there should be splits created")
1066 .count();
1067 assert_eq!(count, 3);
1068 assert_eq!(at.read("xx00"), generate(1, 20));
1069 assert_eq!(at.read("xx01"), "");
1070 assert_eq!(at.read("xx02"), generate(20, 51));
1071 }
1072
1073 #[test]
1074 fn test_too_small_line_num_elided() {
1075 let (at, mut ucmd) = at_and_ucmd!();
1076 ucmd.args(&["numbers50.txt", "-z", "/20/", "10", "/40/"])
1077 .succeeds()
1078 .stdout_only("48\n60\n33\n");
1079
1080 let count = glob(&at.plus_as_string("xx*"))
1081 .expect("there should be splits created")
1082 .count();
1083 assert_eq!(count, 3);
1084 assert_eq!(at.read("xx00"), generate(1, 20));
1085 assert_eq!(at.read("xx01"), generate(20, 40));
1086 assert_eq!(at.read("xx02"), generate(40, 51));
1087 }
1088
1089 #[test]
1090 fn test_too_small_line_num_negative_offset() {
1091 let (at, mut ucmd) = at_and_ucmd!();
1092 ucmd.args(&["numbers50.txt", "/20/-5", "10", "/40/"])
1093 .succeeds()
1094 .stdout_only("33\n0\n75\n33\n");
1095
1096 let count = glob(&at.plus_as_string("xx*"))
1097 .expect("there should be splits created")
1098 .count();
1099 assert_eq!(count, 4);
1100 assert_eq!(at.read("xx00"), generate(1, 15));
1101 assert_eq!(at.read("xx01"), "");
1102 assert_eq!(at.read("xx02"), generate(15, 40));
1103 assert_eq!(at.read("xx03"), generate(40, 51));
1104 }
1105
1106 #[test]
1107 fn test_too_small_line_num_twice() {
1108 let (at, mut ucmd) = at_and_ucmd!();
1109 ucmd.args(&["numbers50.txt", "/20/", "10", "15", "/40/"])
1110 .succeeds()
1111 .stdout_only("48\n0\n0\n60\n33\n");
1112
1113 let count = glob(&at.plus_as_string("xx*"))
1114 .expect("there should be splits created")
1115 .count();
1116 assert_eq!(count, 5);
1117 assert_eq!(at.read("xx00"), generate(1, 20));
1118 assert_eq!(at.read("xx01"), "");
1119 assert_eq!(at.read("xx02"), "");
1120 assert_eq!(at.read("xx03"), generate(20, 40));
1121 assert_eq!(at.read("xx04"), generate(40, 51));
1122 }
1123
1124 #[test]
1125 fn test_too_small_line_num_repeat() {
1126 let (at, mut ucmd) = at_and_ucmd!();
1127 ucmd.args(&["numbers50.txt", "/20/", "10", "{*}"])
1128 .fails()
1129 .stderr_is("csplit: '10': line number out of range on repetition 5\n")
1130 .stdout_is("48\n0\n0\n30\n30\n30\n3\n");
1131
1132 let count = glob(&at.plus_as_string("xx*"))
1133 .expect("there should be splits created")
1134 .count();
1135 assert_eq!(count, 0);
1136
1137 let (at, mut ucmd) = at_and_ucmd!();
1138 ucmd.args(&["numbers50.txt", "/20/", "10", "{*}", "-k"])
1139 .fails()
1140 .stderr_is("csplit: '10': line number out of range on repetition 5\n")
1141 .stdout_is("48\n0\n0\n30\n30\n30\n3\n");
1142
1143 let count = glob(&at.plus_as_string("xx*"))
1144 .expect("there should be splits created")
1145 .count();
1146 assert_eq!(count, 7);
1147 assert_eq!(at.read("xx00"), generate(1, 20));
1148 assert_eq!(at.read("xx01"), "");
1149 assert_eq!(at.read("xx02"), "");
1150 assert_eq!(at.read("xx03"), generate(20, 30));
1151 assert_eq!(at.read("xx04"), generate(30, 40));
1152 assert_eq!(at.read("xx05"), generate(40, 50));
1153 assert_eq!(at.read("xx06"), "50\n");
1154 }
1155
1156 #[test]
1157 fn test_line_num_out_of_range1() {
1158 let (at, mut ucmd) = at_and_ucmd!();
1159 ucmd.args(&["numbers50.txt", "100"])
1160 .fails()
1161 .stdout_is("141\n")
1162 .stderr_is("csplit: '100': line number out of range\n");
1163
1164 let count = glob(&at.plus_as_string("xx*"))
1165 .expect("there should be splits created")
1166 .count();
1167 assert_eq!(count, 0);
1168
1169 let (at, mut ucmd) = at_and_ucmd!();
1170 ucmd.args(&["numbers50.txt", "100", "-k"])
1171 .fails()
1172 .stdout_is("141\n")
1173 .stderr_is("csplit: '100': line number out of range\n");
1174
1175 let count = glob(&at.plus_as_string("xx*"))
1176 .expect("there should be splits created")
1177 .count();
1178 assert_eq!(count, 1);
1179 assert_eq!(at.read("xx00"), generate(1, 51));
1180 }
1181
1182 #[test]
1183 fn test_line_num_out_of_range2() {
1184 let (at, mut ucmd) = at_and_ucmd!();
1185 ucmd.args(&["numbers50.txt", "10", "100"])
1186 .fails()
1187 .stdout_is("18\n123\n")
1188 .stderr_is("csplit: '100': line number out of range\n");
1189
1190 let count = glob(&at.plus_as_string("xx*"))
1191 .expect("there should be splits created")
1192 .count();
1193 assert_eq!(count, 0);
1194
1195 let (at, mut ucmd) = at_and_ucmd!();
1196 ucmd.args(&["numbers50.txt", "10", "100", "-k"])
1197 .fails()
1198 .stdout_is("18\n123\n")
1199 .stderr_is("csplit: '100': line number out of range\n");
1200
1201 let count = glob(&at.plus_as_string("xx*"))
1202 .expect("there should be splits created")
1203 .count();
1204 assert_eq!(count, 2);
1205 assert_eq!(at.read("xx00"), generate(1, 10));
1206 assert_eq!(at.read("xx01"), generate(10, 51));
1207 }
1208
1209 #[test]
1210 fn test_line_num_out_of_range3() {
1211 let (at, mut ucmd) = at_and_ucmd!();
1212 ucmd.args(&["numbers50.txt", "40", "{2}"])
1213 .fails()
1214 .stdout_is("108\n33\n")
1215 .stderr_is("csplit: '40': line number out of range on repetition 1\n");
1216
1217 let count = glob(&at.plus_as_string("xx*"))
1218 .expect("there should be splits created")
1219 .count();
1220 assert_eq!(count, 0);
1221
1222 let (at, mut ucmd) = at_and_ucmd!();
1223 ucmd.args(&["numbers50.txt", "40", "{2}", "-k"])
1224 .fails()
1225 .stdout_is("108\n33\n")
1226 .stderr_is("csplit: '40': line number out of range on repetition 1\n");
1227
1228 let count = glob(&at.plus_as_string("xx*"))
1229 .expect("there should be splits created")
1230 .count();
1231 assert_eq!(count, 2);
1232 assert_eq!(at.read("xx00"), generate(1, 40));
1233 assert_eq!(at.read("xx01"), generate(40, 51));
1234 }
1235
1236 #[test]
1237 fn test_line_num_out_of_range4() {
1238 let (at, mut ucmd) = at_and_ucmd!();
1239 ucmd.args(&["numbers50.txt", "40", "{*}"])
1240 .fails()
1241 .stdout_is("108\n33\n")
1242 .stderr_is("csplit: '40': line number out of range on repetition 1\n");
1243
1244 let count = glob(&at.plus_as_string("xx*"))
1245 .expect("there should be splits created")
1246 .count();
1247 assert_eq!(count, 0);
1248
1249 let (at, mut ucmd) = at_and_ucmd!();
1250 ucmd.args(&["numbers50.txt", "40", "{*}", "-k"])
1251 .fails()
1252 .stdout_is("108\n33\n")
1253 .stderr_is("csplit: '40': line number out of range on repetition 1\n");
1254
1255 let count = glob(&at.plus_as_string("xx*"))
1256 .expect("there should be splits created")
1257 .count();
1258 assert_eq!(count, 2);
1259 assert_eq!(at.read("xx00"), generate(1, 40));
1260 assert_eq!(at.read("xx01"), generate(40, 51));
1261 }
1262
1263 #[test]
1264 fn test_skip_to_match_negative_offset_before_a_match() {
1265 let (at, mut ucmd) = at_and_ucmd!();
1266 ucmd.args(&["numbers50.txt", "/20/-10", "/15/"])
1267 .fails()
1268 .stdout_is("18\n123\n")
1269 .stderr_is("csplit: '/15/': match not found\n");
1270
1271 let count = glob(&at.plus_as_string("xx*"))
1272 .expect("there should be splits created")
1273 .count();
1274 assert_eq!(count, 0);
1275 }
1276
1277 #[test]
1278 fn test_skip_to_match_negative_offset_before_a_line_num() {
1279 let (at, mut ucmd) = at_and_ucmd!();
1280 ucmd.args(&["numbers50.txt", "/20/-10", "15"])
1281 .succeeds()
1282 .stdout_only("18\n15\n108\n");
1283
1284 let count = glob(&at.plus_as_string("xx*"))
1285 .expect("there should be splits created")
1286 .count();
1287 assert_eq!(count, 3);
1288 assert_eq!(at.read("xx00"), generate(1, 10));
1289 assert_eq!(at.read("xx01"), generate(10, 15));
1290 assert_eq!(at.read("xx02"), generate(15, 51));
1291 }
1292
1293 #[test]
1294 fn test_corner_case1() {
1295 let (at, mut ucmd) = at_and_ucmd!();
1296 ucmd.args(&["numbers50.txt", "/10/", "11"])
1297 .succeeds()
1298 .stdout_only("18\n3\n120\n");
1299
1300 let count = glob(&at.plus_as_string("xx*"))
1301 .expect("there should be splits created")
1302 .count();
1303 assert_eq!(count, 3);
1304 assert_eq!(at.read("xx00"), generate(1, 10));
1305 assert_eq!(at.read("xx01"), "10\n");
1306 assert_eq!(at.read("xx02"), generate(11, 51));
1307 }
1308
1309 #[test]
1310 fn test_corner_case2() {
1311 let (at, mut ucmd) = at_and_ucmd!();
1312 ucmd.args(&["numbers50.txt", "/10/-5", "/10/"])
1313 .fails()
1314 .stderr_is("csplit: '/10/': match not found\n")
1315 .stdout_is("8\n133\n");
1316
1317 let count = glob(&at.plus_as_string("xx*"))
1318 .expect("there should be splits created")
1319 .count();
1320 assert_eq!(count, 0);
1321 }
1322
1323 #[test]
1324 fn test_corner_case3() {
1325 let (at, mut ucmd) = at_and_ucmd!();
1326 ucmd.args(&["numbers50.txt", "/15/-3", "14", "/15/"])
1327 .fails()
1328 .stderr_is("csplit: '/15/': match not found\n")
1329 .stdout_is("24\n6\n111\n");
1330
1331 let count = glob(&at.plus_as_string("xx*"))
1332 .expect("there should be splits created")
1333 .count();
1334 assert_eq!(count, 0);
1335 }
1336
1337 #[test]
1338 fn test_corner_case4() {
1339 let (at, mut ucmd) = at_and_ucmd!();
1340 ucmd.args(&["numbers50.txt", "/20/-10", "/30/-4"])
1341 .succeeds()
1342 .stdout_only("18\n48\n75\n");
1343
1344 let count = glob(&at.plus_as_string("xx*"))
1345 .expect("there should be splits created")
1346 .count();
1347 assert_eq!(count, 3);
1348 assert_eq!(at.read("xx00"), generate(1, 10));
1349 assert_eq!(at.read("xx01"), generate(10, 26));
1350 assert_eq!(at.read("xx02"), generate(26, 51));
1351 }
1352
1353 #[test]
1354 fn test_up_to_match_context_underflow() {
1355 let (at, mut ucmd) = at_and_ucmd!();
1356 ucmd.args(&["numbers50.txt", "/5/-10"])
1357 .fails()
1358 .stdout_is("0\n")
1359 .stderr_is("csplit: '/5/-10': line number out of range\n");
1360
1361 let count = glob(&at.plus_as_string("xx*"))
1362 .expect("counting splits")
1363 .count();
1364 assert_eq!(count, 0);
1365
1366 let (at, mut ucmd) = at_and_ucmd!();
1367 ucmd.args(&["numbers50.txt", "/5/-10", "-k"])
1368 .fails()
1369 .stdout_is("0\n")
1370 .stderr_is("csplit: '/5/-10': line number out of range\n");
1371
1372 let count = glob(&at.plus_as_string("xx*"))
1373 .expect("counting splits")
1374 .count();
1375 assert_eq!(count, 1);
1376 assert_eq!(at.read("xx00"), "");
1377 }
1378
1379 // the offset is out of range because of the first pattern
1380 #[test]
1381 fn test_line_num_range_with_up_to_match1() {
1382 let (at, mut ucmd) = at_and_ucmd!();
1383 ucmd.args(&["numbers50.txt", "10", "/12/-5"])
1384 .fails()
1385 .stderr_is("csplit: '/12/-5': line number out of range\n")
1386 .stdout_is("18\n0\n");
1387
1388 let count = glob(&at.plus_as_string("xx*"))
1389 .expect("there should be splits created")
1390 .count();
1391 assert_eq!(count, 0);
1392
1393 let (at, mut ucmd) = at_and_ucmd!();
1394 ucmd.args(&["numbers50.txt", "10", "/12/-5", "-k"])
1395 .fails()
1396 .stderr_is("csplit: '/12/-5': line number out of range\n")
1397 .stdout_is("18\n0\n");
1398
1399 let count = glob(&at.plus_as_string("xx*"))
1400 .expect("there should be splits created")
1401 .count();
1402 assert_eq!(count, 2);
1403 assert_eq!(at.read("xx00"), generate(1, 10));
1404 assert_eq!(at.read("xx01"), "");
1405 }
1406
1407 // the offset is out of range because more lines are needed than physically available
1408 #[test]
1409 fn test_line_num_range_with_up_to_match2() {
1410 let (at, mut ucmd) = at_and_ucmd!();
1411 ucmd.args(&["numbers50.txt", "10", "/12/-15"])
1412 .fails()
1413 .stderr_is("csplit: '/12/-15': line number out of range\n")
1414 .stdout_is("18\n0\n");
1415
1416 let count = glob(&at.plus_as_string("xx*"))
1417 .expect("there should be splits created")
1418 .count();
1419 assert_eq!(count, 0);
1420
1421 let (at, mut ucmd) = at_and_ucmd!();
1422 ucmd.args(&["numbers50.txt", "10", "/12/-15", "-k"])
1423 .fails()
1424 .stderr_is("csplit: '/12/-15': line number out of range\n")
1425 .stdout_is("18\n0\n");
1426
1427 let count = glob(&at.plus_as_string("xx*"))
1428 .expect("there should be splits created")
1429 .count();
1430 assert_eq!(count, 2);
1431 assert_eq!(at.read("xx00"), generate(1, 10));
1432 assert_eq!(at.read("xx01"), "");
1433 }
1434
1435 // NOTE: output different than gnu's: the pattern /10/ is matched but should not
1436 #[test]
1437 fn test_line_num_range_with_up_to_match3() {
1438 let (at, mut ucmd) = at_and_ucmd!();
1439 ucmd.args(&["numbers50.txt", "10", "/10/", "-k"])
1440 .fails()
1441 .stderr_is("csplit: '/10/': match not found\n")
1442 .stdout_is("18\n123\n");
1443
1444 let count = glob(&at.plus_as_string("xx*"))
1445 .expect("there should be splits created")
1446 .count();
1447 assert_eq!(count, 2);
1448 assert_eq!(at.read("xx00"), generate(1, 10));
1449 assert_eq!(at.read("xx01"), generate(10, 51));
1450
1451 let (at, mut ucmd) = at_and_ucmd!();
1452 ucmd.args(&["numbers50.txt", "/10/", "10"])
1453 .succeeds()
1454 .stdout_only("18\n0\n123\n");
1455
1456 let count = glob(&at.plus_as_string("xx*"))
1457 .expect("there should be splits created")
1458 .count();
1459 assert_eq!(count, 3);
1460 assert_eq!(at.read("xx00"), generate(1, 10));
1461 assert_eq!(at.read("xx01"), "");
1462 assert_eq!(at.read("xx02"), generate(10, 51));
1463 }
1464
1465 #[test]
1466 fn precision_format() {
1467 for f in ["%#6.3x", "%0#6.3x"] {
1468 let (at, mut ucmd) = at_and_ucmd!();
1469 ucmd.args(&["numbers50.txt", "10", "--suffix-format", f])
1470 .succeeds()
1471 .stdout_only("18\n123\n");
1472
1473 let count = glob(&at.plus_as_string("xx*"))
1474 .expect("there should be splits created")
1475 .count();
1476 assert_eq!(count, 2);
1477 assert_eq!(at.read("xx 000"), generate(1, 10));
1478 assert_eq!(at.read("xx 0x001"), generate(10, 51));
1479 }
1480 }
1481
1482 #[test]
1483 fn suffix_format_hyphen_leading_as_separate_arg() {
1484 // A hyphen-leading suffix format passed as its own argument (not
1485 // attached with `-b-%02d`/`=`) must not be mistaken for a new,
1486 // unrecognized flag.
1487 let (at, mut ucmd) = at_and_ucmd!();
1488 ucmd.args(&["numbers50.txt", "10", "--suffix-format", "-%02d"])
1489 .succeeds()
1490 .stdout_only("18\n123\n");
1491
1492 assert_eq!(at.read("xx-00"), generate(1, 10));
1493 assert_eq!(at.read("xx-01"), generate(10, 51));
1494 }
1495
1496 #[test]
1497 fn zero_precision_format() {
1498 let (at, mut ucmd) = at_and_ucmd!();
1499 ucmd.args(&["numbers50.txt", "10", "--suffix-format", "%.0d"])
1500 .succeeds()
1501 .stdout_only("18\n123\n");
1502
1503 let count = glob(&at.plus_as_string("xx*"))
1504 .expect("there should be splits created")
1505 .count();
1506 assert_eq!(count, 2);
1507 assert_eq!(at.read("xx"), generate(1, 10));
1508 assert_eq!(at.read("xx1"), generate(10, 51));
1509 }
1510
1511 #[test]
1512 fn zero_error() {
1513 let (at, mut ucmd) = at_and_ucmd!();
1514 at.touch("in");
1515 ucmd.args(&["in", "0"])
1516 .fails()
1517 .stderr_contains("0: line number must be greater");
1518 }
1519
1520 #[test]
1521 fn no_such_file() {
1522 new_ucmd!()
1523 .args(&["in", "0"])
1524 .fails()
1525 .stderr_contains("cannot open 'in' for reading: No such file or directory");
1526 }
1527
1528 #[test]
1529 fn repeat_everything() {
1530 let (at, mut ucmd) = at_and_ucmd!();
1531 ucmd.args(&[
1532 "numbers50.txt",
1533 "--suppress-matched",
1534 "--suppress-matched",
1535 "-kzsn", // spell-checker:disable-line
1536 "2",
1537 "-szkn3", // spell-checker:disable-line
1538 "-b",
1539 "%03d",
1540 "-b%03x",
1541 "-f",
1542 "xxy_",
1543 "-fxxz_", // spell-checker:disable-line
1544 "/13/",
1545 "9",
1546 "{5}",
1547 ])
1548 .fails_with_code(1)
1549 .no_stdout()
1550 .stderr_only("csplit: '9': line number out of range on repetition 5\n");
1551 let count = glob(&at.plus_as_string("xx*"))
1552 .expect("there should be some splits created")
1553 .count();
1554 assert_eq!(count, 6);
1555 assert_eq!(at.read("xxz_000"), generate(1, 12 + 1));
1556 assert_eq!(at.read("xxz_001"), generate(14, 17 + 1)); // FIXME: GNU starts at 15
1557 assert_eq!(at.read("xxz_002"), generate(19, 26 + 1));
1558 assert_eq!(at.read("xxz_003"), generate(28, 35 + 1));
1559 assert_eq!(at.read("xxz_004"), generate(37, 44 + 1));
1560 assert_eq!(at.read("xxz_005"), generate(46, 50 + 1));
1561 }
1562
1563 #[cfg(unix)]
1564 #[test]
1565 fn test_named_pipe_input_file() {
1566 let (at, mut ucmd) = at_and_ucmd!();
1567
1568 let mut fifo_writer =
1569 create_named_pipe_with_writer(&at.plus_as_string("fifo"), &generate(1, 51));
1570
1571 let result = ucmd.args(&["fifo", "10"]).succeeds();
1572 fifo_writer.kill().unwrap();
1573 fifo_writer.wait().unwrap();
1574 result.stdout_only("18\n123\n");
1575
1576 let count = glob(&at.plus_as_string("xx*"))
1577 .expect("there should be splits created")
1578 .count();
1579 assert_eq!(count, 2);
1580 assert_eq!(at.read("xx00"), generate(1, 10));
1581 assert_eq!(at.read("xx01"), generate(10, 51));
1582 }
1583
1584 #[cfg(unix)]
1585 fn create_named_pipe_with_writer(path: &str, data: &str) -> std::process::Child {
1586 // cSpell:ignore IRWXU
1587 nix::unistd::mkfifo(path, nix::sys::stat::Mode::S_IRWXU).unwrap();
1588 std::process::Command::new("sh")
1589 .arg("-c")
1590 .arg(format!("printf '{data}' > {path}"))
1591 .spawn()
1592 .unwrap()
1593 }
1594
1595 #[test]
1596 fn test_directory_input_file() {
1597 let (at, mut ucmd) = at_and_ucmd!();
1598 at.mkdir("test_directory");
1599
1600 #[cfg(unix)]
1601 ucmd.args(&["test_directory", "1"])
1602 .fails_with_code(1)
1603 .stderr_only("csplit: read error: Is a directory\n");
1604 #[cfg(windows)]
1605 ucmd.args(&["test_directory", "1"])
1606 .fails_with_code(1)
1607 .stderr_only("csplit: cannot open 'test_directory' for reading: Permission denied\n");
1608 }
1609
1610 #[test]
1611 fn test_stdin_no_trailing_newline() {
1612 new_ucmd!()
1613 .args(&["-", "2"])
1614 .pipe_in("a\nb\nc\nd")
1615 .succeeds()
1616 .stdout_only("2\n5\n");
1617 }
1618
1619 #[test]
1620 #[cfg(target_os = "linux")]
1621 fn test_csplit_non_utf8_paths() {
1622 use std::os::unix::ffi::OsStringExt;
1623 let (at, mut ucmd) = at_and_ucmd!();
1624
1625 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
1626 std::fs::write(at.plus(&filename), b"line1\nline2\nline3\nline4\nline5\n").unwrap();
1627
1628 ucmd.arg(&filename).arg("3").succeeds();
1629 }
1630
1631 /// Test write error detection using /dev/full
1632 #[test]
1633 #[cfg(target_os = "linux")]
1634 fn test_write_error_dev_full() {
1635 let (at, mut ucmd) = at_and_ucmd!();
1636 at.symlink_file("/dev/full", "xx01");
1637
1638 ucmd.args(&["-", "2"])
1639 .pipe_in("1\n2\n")
1640 .fails_with_code(1)
1641 .stderr_contains("xx01: No space left on device");
1642
1643 // Files cleaned up by default
1644 assert!(!at.file_exists("xx00"));
1645 }
1646
1647 /// Test write error with -k keeps files
1648 #[test]
1649 #[cfg(target_os = "linux")]
1650 fn test_write_error_dev_full_keep_files() {
1651 let (at, mut ucmd) = at_and_ucmd!();
1652 at.symlink_file("/dev/full", "xx01");
1653
1654 ucmd.args(&["-k", "-", "2"])
1655 .pipe_in("1\n2\n")
1656 .fails_with_code(1)
1657 .stderr_contains("xx01: No space left on device");
1658
1659 assert!(at.file_exists("xx00"));
1660 assert_eq!(at.read("xx00"), "1\n");
1661 }
1662
1663 /// Test that a failed split-file creation reports the filename.
1664 #[test]
1665 #[cfg(unix)]
1666 fn test_create_error_reports_filename() {
1667 // Root can open a mode-000 file for writing, so File::create would not fail.
1668 if rustix::process::geteuid().is_root() {
1669 return;
1670 }
1671 let (at, mut ucmd) = at_and_ucmd!();
1672 at.write("input", "a\nb\nc\n");
1673 // Pre-create the first split with no write permission so File::create fails.
1674 at.touch("xx00");
1675 at.set_mode("xx00", 0o000);
1676
1677 ucmd.args(&["input", "2"])
1678 .fails()
1679 .stderr_is("csplit: xx00: Permission denied\n");
1680 }
1681
1682 #[test]
1683 #[cfg(all(target_os = "linux", not(wasi_runner)))]
1684 fn test_csplit_dev_full_stdout() {
1685 use std::fs::OpenOptions;
1686
1687 let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap();
1688
1689 new_ucmd!()
1690 .args(&["/etc/hosts", "1"])
1691 .set_stdout(dev_full)
1692 .fails_with_code(1)
1693 .stderr_is("csplit: No space left on device\n");
1694 }