Debian Codegraph @

tests/by-util/test_seq.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 // spell-checker:ignore lmnop xlmnop
7
8 use rstest::rstest;
9 use uutests::new_ucmd;
10 #[cfg(unix)]
11 use uutests::util::TestScenario;
12 #[cfg(unix)]
13 use uutests::util_name;
14
15 #[test]
16 fn test_invalid_arg() {
17 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
18 }
19
20 #[test]
21 fn test_no_args() {
22 new_ucmd!()
23 .fails_with_code(1)
24 .stderr_contains("missing operand");
25 }
26
27 #[test]
28 fn test_format_and_equal_width() {
29 new_ucmd!()
30 .args(&["-w", "-f", "%f", "1"])
31 .fails_with_code(1)
32 .stderr_contains("format string may not be specified");
33 }
34
35 #[test]
36 fn test_format_precision_too_large() {
37 // A precision near usize::MAX used to overflow the precision arithmetic
38 // shared with the float formatters; it must be rejected as invalid.
39 for spec in ["%.18446744073709551615e", "%.9999999999999999999a"] {
40 new_ucmd!()
41 .args(&[format!("--format={spec}"), "1".to_string()])
42 .fails_with_code(1)
43 .no_stdout()
44 .stderr_contains("invalid precision");
45 }
46 }
47
48 #[test]
49 fn test_format_extreme_exponent_does_not_overflow() {
50 // A value with an exponent that does not fit in an i32 used to overflow the
51 // hex-float exponent math. GNU seq rejects such astronomically large
52 // magnitudes outright (unlike printf, which tolerates them as inf/0), so it
53 // must now fail cleanly with a parse error instead of panicking.
54 new_ucmd!()
55 .args(&[
56 "--format=%a",
57 "5e8123456789012345678",
58 "5e8123456789012345678",
59 ])
60 .fails_with_code(1)
61 .no_stdout()
62 .usage_error("invalid floating point argument: '5e8123456789012345678'");
63 }
64
65 #[test]
66 fn test_equal_width_huge_exponent_does_not_overflow() {
67 // Equal-width padding adds the integral-digit count to the precision; a value
68 // with an astronomically large exponent used to overflow that arithmetic.
69 // GNU seq rejects such magnitudes as an invalid argument rather than
70 // accepting them, so it must fail cleanly instead of panicking or succeeding.
71 new_ucmd!()
72 .args(&["-w", "1e9223372036854775807", "1e-9223372036854775807", "1"])
73 .fails_with_code(1)
74 .no_stdout()
75 .usage_error("invalid floating point argument: '1e9223372036854775807'");
76 }
77
78 #[test]
79 fn test_hex_rejects_sign_after_identifier() {
80 new_ucmd!()
81 .args(&["0x-123ABC"])
82 .fails()
83 .no_stdout()
84 .usage_error("invalid floating point argument: '0x-123ABC'");
85 new_ucmd!()
86 .args(&["0x+123ABC"])
87 .fails()
88 .no_stdout()
89 .usage_error("invalid floating point argument: '0x+123ABC'");
90
91 new_ucmd!()
92 .args(&["--", "-0x-123ABC"])
93 .fails()
94 .no_stdout()
95 .usage_error("invalid floating point argument: '-0x-123ABC'");
96 new_ucmd!()
97 .args(&["--", "-0x+123ABC"])
98 .fails()
99 .no_stdout()
100 .usage_error("invalid floating point argument: '-0x+123ABC'");
101
102 // test without "--" => argument parsed as (invalid) flag
103 new_ucmd!()
104 .args(&["-0x-123ABC"])
105 .fails()
106 .no_stdout()
107 .usage_error("invalid floating point argument: '-0x-123ABC'");
108 new_ucmd!()
109 .args(&["-0x+123ABC"])
110 .fails()
111 .no_stdout()
112 .usage_error("invalid floating point argument: '-0x+123ABC'");
113 }
114
115 #[test]
116 fn test_hex_lowercase_uppercase() {
117 new_ucmd!()
118 .args(&["0xa", "0xA"])
119 .succeeds()
120 .stdout_is("10\n");
121 new_ucmd!()
122 .args(&["0Xa", "0XA"])
123 .succeeds()
124 .stdout_is("10\n");
125 }
126
127 #[test]
128 fn test_hex_big_number() {
129 new_ucmd!()
130 .args(&[
131 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
132 "0x100000000000000000000000000000000",
133 ])
134 .succeeds()
135 .stdout_is(
136 "340282366920938463463374607431768211455\n340282366920938463463374607431768211456\n",
137 );
138 }
139
140 #[test]
141 fn test_hex_identifier_in_wrong_place() {
142 new_ucmd!()
143 .args(&["1234ABCD0x"])
144 .fails()
145 .no_stdout()
146 .usage_error("invalid floating point argument: '1234ABCD0x'");
147 }
148
149 #[test]
150 fn test_rejects_nan() {
151 new_ucmd!()
152 .arg("NaN")
153 .fails()
154 .usage_error("invalid 'not-a-number' argument: 'NaN'");
155 }
156
157 #[test]
158 fn test_rejects_non_floats() {
159 new_ucmd!()
160 .arg("foo")
161 .fails()
162 .usage_error("invalid floating point argument: 'foo'");
163 }
164
165 #[test]
166 fn test_accepts_option_argument_directly() {
167 new_ucmd!()
168 .arg("-s,")
169 .arg("2")
170 .succeeds()
171 .stdout_is("1,2\n");
172 }
173
174 #[test]
175 fn test_option_with_detected_negative_argument() {
176 new_ucmd!()
177 .arg("-s,")
178 .args(&["-1", "2"])
179 .succeeds()
180 .stdout_is("-1,0,1,2\n");
181 }
182
183 #[test]
184 fn test_negative_number_as_separator() {
185 new_ucmd!()
186 .arg("-s")
187 .args(&["-1", "2"])
188 .succeeds()
189 .stdout_is("1-12\n");
190 }
191
192 #[test]
193 fn test_invalid_float() {
194 new_ucmd!()
195 .args(&["1e2.3"])
196 .fails()
197 .no_stdout()
198 .usage_error("invalid floating point argument: '1e2.3'");
199 new_ucmd!()
200 .args(&["1e2.3", "2"])
201 .fails()
202 .no_stdout()
203 .usage_error("invalid floating point argument: '1e2.3'");
204 new_ucmd!()
205 .args(&["1", "1e2.3"])
206 .fails()
207 .no_stdout()
208 .usage_error("invalid floating point argument: '1e2.3'");
209 new_ucmd!()
210 .args(&["1e2.3", "2", "3"])
211 .fails()
212 .no_stdout()
213 .usage_error("invalid floating point argument: '1e2.3'");
214 new_ucmd!()
215 .args(&["1", "1e2.3", "3"])
216 .fails()
217 .no_stdout()
218 .usage_error("invalid floating point argument: '1e2.3'");
219 new_ucmd!()
220 .args(&["1", "2", "1e2.3"])
221 .fails()
222 .no_stdout()
223 .usage_error("invalid floating point argument: '1e2.3'");
224 }
225
226 #[test]
227 fn test_width_invalid_float() {
228 new_ucmd!()
229 .args(&["-w", "1e2.3"])
230 .fails()
231 .no_stdout()
232 .usage_error("invalid floating point argument: '1e2.3'");
233 }
234
235 #[test]
236 #[cfg(unix)]
237 fn test_sigpipe_ignored_reports_write_error() {
238 let scene = TestScenario::new(util_name!());
239 let seq_bin = scene.bin_path.clone().into_os_string();
240 let script = "trap '' PIPE; { \"$SEQ_BIN\" seq inf 2>err; echo $? >code; } | head -n1";
241 let result = scene.cmd_shell(script).env("SEQ_BIN", &seq_bin).succeeds();
242
243 assert_eq!(result.stdout_str(), "1\n");
244
245 let err_contents = scene.fixtures.read("err");
246 assert!(
247 err_contents.contains("seq: write error: Broken pipe"),
248 "stderr missing write error message: {err_contents:?}"
249 );
250 assert_eq!(scene.fixtures.read("code"), "1\n");
251 }
252
253 // ---- Tests for the big integer based path ----
254
255 #[test]
256 fn test_count_up() {
257 new_ucmd!()
258 .args(&["10"])
259 .succeeds()
260 .stdout_is("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n");
261 }
262
263 #[test]
264 fn test_count_down() {
265 new_ucmd!()
266 .args(&["--", "5", "-1", "1"])
267 .succeeds()
268 .stdout_is("5\n4\n3\n2\n1\n");
269 new_ucmd!()
270 .args(&["5", "-1", "1"])
271 .succeeds()
272 .stdout_is("5\n4\n3\n2\n1\n");
273 }
274
275 #[test]
276 fn test_separator_and_terminator() {
277 new_ucmd!()
278 .args(&["-s", ",", "-t", "!", "2", "6"])
279 .succeeds()
280 .stdout_is("2,3,4,5,6!");
281 new_ucmd!()
282 .args(&["-s", ",", "2", "6"])
283 .succeeds()
284 .stdout_is("2,3,4,5,6\n");
285 new_ucmd!()
286 .args(&["-s", "", "2", "6"])
287 .succeeds()
288 .stdout_is("23456\n");
289 new_ucmd!()
290 .args(&["-s", "\n", "2", "6"])
291 .succeeds()
292 .stdout_is("2\n3\n4\n5\n6\n");
293 new_ucmd!()
294 .args(&["-s", "\\n", "2", "6"])
295 .succeeds()
296 .stdout_is("2\\n3\\n4\\n5\\n6\n");
297 }
298
299 #[test]
300 #[cfg(unix)]
301 fn test_separator_non_utf8() {
302 use std::{ffi::OsString, os::unix::ffi::OsStringExt};
303
304 fn create_arg(prefix: &[u8]) -> OsString {
305 let separator = [0xFF, 0xFE];
306 OsString::from_vec([prefix, &separator].concat())
307 }
308
309 let short = create_arg(b"-s");
310 let long = create_arg(b"--separator=");
311 let expected = [b'1', 0xFF, 0xFE, b'2', b'\n'];
312
313 for arg in [short, long] {
314 new_ucmd!()
315 .arg(&arg)
316 .arg("2")
317 .succeeds()
318 .stdout_is_bytes(expected);
319 }
320 }
321
322 #[test]
323 #[cfg(unix)]
324 fn test_terminator_non_utf8() {
325 use std::{ffi::OsString, os::unix::ffi::OsStringExt};
326
327 fn create_arg(prefix: &[u8]) -> OsString {
328 let terminator = [0xFF, 0xFE];
329 OsString::from_vec([prefix, &terminator].concat())
330 }
331
332 let short = create_arg(b"-t");
333 let long = create_arg(b"--terminator=");
334 let expected = [b'1', b'\n', b'2', 0xFF, 0xFE];
335
336 for arg in [short, long] {
337 new_ucmd!()
338 .arg(&arg)
339 .arg("2")
340 .succeeds()
341 .stdout_is_bytes(expected);
342 }
343 }
344
345 #[test]
346 fn test_equalize_widths() {
347 let args = ["-w", "--equal-width"];
348 for arg in args {
349 new_ucmd!()
350 .args(&[arg, "5", "10"])
351 .succeeds()
352 .stdout_is("05\n06\n07\n08\n09\n10\n");
353 }
354 }
355
356 #[test]
357 fn test_seq_wrong_arg() {
358 new_ucmd!().args(&["-w", "5", "10", "33", "32"]).fails();
359 }
360
361 #[test]
362 fn test_zero_step() {
363 new_ucmd!().args(&["10", "0", "32"]).fails();
364 }
365
366 #[test]
367 fn test_big_numbers() {
368 new_ucmd!()
369 .args(&[
370 "1000000000000000000000000000",
371 "1000000000000000000000000001",
372 ])
373 .succeeds()
374 .stdout_only("1000000000000000000000000000\n1000000000000000000000000001\n");
375 }
376
377 // ---- Tests for the floating point based path ----
378
379 #[test]
380 fn test_count_up_floats() {
381 new_ucmd!()
382 .args(&["10.0"])
383 .succeeds()
384 .stdout_is("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n");
385 }
386
387 #[test]
388 fn test_count_down_floats() {
389 new_ucmd!()
390 .args(&["--", "5", "-1.0", "1"])
391 .succeeds()
392 .stdout_is("5.0\n4.0\n3.0\n2.0\n1.0\n");
393 new_ucmd!()
394 .args(&["5", "-1", "1.0"])
395 .succeeds()
396 .stdout_is("5\n4\n3\n2\n1\n");
397 }
398
399 #[test]
400 fn test_separator_and_terminator_floats() {
401 new_ucmd!()
402 .args(&["-s", ",", "-t", "!", "2.0", "6"])
403 .succeeds()
404 .stdout_is("2.0,3.0,4.0,5.0,6.0!");
405 new_ucmd!()
406 .args(&["-s", "", "-t", "!", "2.0", "6"])
407 .succeeds()
408 .stdout_is("2.03.04.05.06.0!");
409 }
410
411 #[test]
412 fn test_equalize_widths_floats() {
413 new_ucmd!()
414 .args(&["-w", "5", "10.0"])
415 .succeeds()
416 .stdout_is("05\n06\n07\n08\n09\n10\n");
417 }
418
419 #[test]
420 fn test_seq_wrong_arg_floats() {
421 new_ucmd!().args(&["-w", "5", "10.0", "33", "32"]).fails();
422 }
423
424 #[test]
425 fn test_zero_step_floats() {
426 new_ucmd!().args(&["10.0", "0", "32"]).fails();
427 }
428
429 #[test]
430 fn test_preserve_negative_zero_start() {
431 new_ucmd!()
432 .args(&["-0", "1"])
433 .succeeds()
434 .stdout_only("-0\n1\n");
435 new_ucmd!()
436 .args(&["-0", "1", "2"])
437 .succeeds()
438 .stdout_only("-0\n1\n2\n");
439 new_ucmd!()
440 .args(&["-0", "1", "2.0"])
441 .succeeds()
442 .stdout_only("-0\n1\n2\n");
443 }
444
445 #[test]
446 fn test_drop_negative_zero_end() {
447 new_ucmd!()
448 .args(&["1", "-1", "-0"])
449 .succeeds()
450 .stdout_only("1\n0\n");
451 }
452
453 #[test]
454 fn test_width_scientific_notation() {
455 new_ucmd!()
456 .args(&["-w", "999", "1e3"])
457 .succeeds()
458 .stdout_only("0999\n1000\n");
459 new_ucmd!()
460 .args(&["-w", "999", "1E3"])
461 .succeeds()
462 .stdout_only("0999\n1000\n");
463 }
464
465 #[test]
466 fn test_width_negative_zero() {
467 new_ucmd!()
468 .args(&["-w", "-0", "1"])
469 .succeeds()
470 .stdout_only("-0\n01\n");
471 new_ucmd!()
472 .args(&["-w", "-0", "1", "2"])
473 .succeeds()
474 .stdout_only("-0\n01\n02\n");
475 new_ucmd!()
476 .args(&["-w", "-0", "1", "2.0"])
477 .succeeds()
478 .stdout_only("-0\n01\n02\n");
479 }
480
481 #[test]
482 fn test_width_negative_zero_decimal_notation() {
483 new_ucmd!()
484 .args(&["-w", "-0.0", "1"])
485 .succeeds()
486 .stdout_only("-0.0\n01.0\n");
487 new_ucmd!()
488 .args(&["-w", "-0.0", "1.0"])
489 .succeeds()
490 .stdout_only("-0.0\n01.0\n");
491 new_ucmd!()
492 .args(&["-w", "-0.0", "1", "2"])
493 .succeeds()
494 .stdout_only("-0.0\n01.0\n02.0\n");
495 new_ucmd!()
496 .args(&["-w", "-0.0", "1", "2.0"])
497 .succeeds()
498 .stdout_only("-0.0\n01.0\n02.0\n");
499 new_ucmd!()
500 .args(&["-w", "-0.0", "1.0", "2"])
501 .succeeds()
502 .stdout_only("-0.0\n01.0\n02.0\n");
503 new_ucmd!()
504 .args(&["-w", "-0.0", "1.0", "2.0"])
505 .succeeds()
506 .stdout_only("-0.0\n01.0\n02.0\n");
507 }
508
509 #[test]
510 fn test_width_negative_zero_scientific_notation() {
511 new_ucmd!()
512 .args(&["-w", "-0e0", "1"])
513 .succeeds()
514 .stdout_only("-0\n01\n");
515 new_ucmd!()
516 .args(&["-w", "-0e0", "1", "2"])
517 .succeeds()
518 .stdout_only("-0\n01\n02\n");
519 new_ucmd!()
520 .args(&["-w", "-0e0", "1", "2.0"])
521 .succeeds()
522 .stdout_only("-0\n01\n02\n");
523
524 new_ucmd!()
525 .args(&["-w", "-0e+1", "1"])
526 .succeeds()
527 .stdout_only("-00\n001\n");
528 new_ucmd!()
529 .args(&["-w", "-0e+1", "1", "2"])
530 .succeeds()
531 .stdout_only("-00\n001\n002\n");
532 new_ucmd!()
533 .args(&["-w", "-0e+1", "1", "2.0"])
534 .succeeds()
535 .stdout_only("-00\n001\n002\n");
536
537 new_ucmd!()
538 .args(&["-w", "-0.000e0", "1"])
539 .succeeds()
540 .stdout_only("-0.000\n01.000\n");
541 new_ucmd!()
542 .args(&["-w", "-0.000e0", "1", "2"])
543 .succeeds()
544 .stdout_only("-0.000\n01.000\n02.000\n");
545 new_ucmd!()
546 .args(&["-w", "-0.000e0", "1", "2.0"])
547 .succeeds()
548 .stdout_only("-0.000\n01.000\n02.000\n");
549
550 new_ucmd!()
551 .args(&["-w", "-0.000e-2", "1"])
552 .succeeds()
553 .stdout_only("-0.00000\n01.00000\n");
554 new_ucmd!()
555 .args(&["-w", "-0.000e-2", "1", "2"])
556 .succeeds()
557 .stdout_only("-0.00000\n01.00000\n02.00000\n");
558 new_ucmd!()
559 .args(&["-w", "-0.000e-2", "1", "2.0"])
560 .succeeds()
561 .stdout_only("-0.00000\n01.00000\n02.00000\n");
562
563 new_ucmd!()
564 .args(&["-w", "-0.000e5", "1"])
565 .succeeds()
566 .stdout_only("-000000\n0000001\n");
567 new_ucmd!()
568 .args(&["-w", "-0.000e5", "1", "2"])
569 .succeeds()
570 .stdout_only("-000000\n0000001\n0000002\n");
571 new_ucmd!()
572 .args(&["-w", "-0.000e5", "1", "2.0"])
573 .succeeds()
574 .stdout_only("-000000\n0000001\n0000002\n");
575
576 new_ucmd!()
577 .args(&["-w", "-0.000e5", "1"])
578 .succeeds()
579 .stdout_only("-000000\n0000001\n");
580 new_ucmd!()
581 .args(&["-w", "-0.000e5", "1", "2"])
582 .succeeds()
583 .stdout_only("-000000\n0000001\n0000002\n");
584 new_ucmd!()
585 .args(&["-w", "-0.000e5", "1", "2.0"])
586 .succeeds()
587 .stdout_only("-000000\n0000001\n0000002\n");
588 }
589
590 #[test]
591 fn test_width_decimal_scientific_notation_increment() {
592 new_ucmd!()
593 .args(&["-w", ".1", "1e-2", ".11"])
594 .succeeds()
595 .stdout_only("0.10\n0.11\n");
596
597 new_ucmd!()
598 .args(&["-w", ".0", "1.500e-1", ".2"])
599 .succeeds()
600 .stdout_only("0.0000\n0.1500\n");
601 }
602
603 /// Test that trailing zeros in the start argument contribute to precision.
604 #[test]
605 fn test_width_decimal_scientific_notation_trailing_zeros_start() {
606 new_ucmd!()
607 .args(&["-w", ".1000", "1e-2", ".11"])
608 .succeeds()
609 .stdout_only("0.1000\n0.1100\n");
610 }
611
612 /// Test that trailing zeros in the increment argument contribute to precision.
613 #[test]
614 fn test_width_decimal_scientific_notation_trailing_zeros_increment() {
615 new_ucmd!()
616 .args(&["-w", "1e-1", "0.0100", ".11"])
617 .succeeds()
618 .stdout_only("0.1000\n0.1100\n");
619 }
620
621 #[test]
622 fn test_width_negative_decimal_notation() {
623 new_ucmd!()
624 .args(&["-w", "-.1", ".1", ".11"])
625 .succeeds()
626 .stdout_only("-0.1\n00.0\n00.1\n");
627 }
628
629 #[test]
630 fn test_width_negative_scientific_notation() {
631 new_ucmd!()
632 .args(&["-w", "-1e-3", "1"])
633 .succeeds()
634 .stdout_only("-0.001\n00.999\n");
635 new_ucmd!()
636 .args(&["-w", "-1.e-3", "1"])
637 .succeeds()
638 .stdout_only("-0.001\n00.999\n");
639 new_ucmd!()
640 .args(&["-w", "-1.0e-4", "1"])
641 .succeeds()
642 .stdout_only("-0.00010\n00.99990\n");
643 new_ucmd!()
644 .args(&["-w", "-.1e2", "10", "100"])
645 .succeeds()
646 .stdout_only(
647 "-010
648 0000
649 0010
650 0020
651 0030
652 0040
653 0050
654 0060
655 0070
656 0080
657 0090
658 0100
659 ",
660 );
661 new_ucmd!()
662 .args(&["-w", "-0.1e2", "10", "100"])
663 .succeeds()
664 .stdout_only(
665 "-010
666 0000
667 0010
668 0020
669 0030
670 0040
671 0050
672 0060
673 0070
674 0080
675 0090
676 0100
677 ",
678 );
679 }
680
681 /// Test that trailing zeros in the end argument do not contribute to width.
682 #[test]
683 fn test_width_decimal_scientific_notation_trailing_zeros_end() {
684 new_ucmd!()
685 .args(&["-w", "1e-1", "1e-2", ".1100"])
686 .succeeds()
687 .stdout_only("0.10\n0.11\n");
688 }
689
690 #[test]
691 fn test_width_floats() {
692 new_ucmd!()
693 .args(&["-w", "9.0", "10.0"])
694 .succeeds()
695 .stdout_only("09.0\n10.0\n");
696 }
697
698 /// Test infinite sequences - these produce endless output, so we check they start correctly
699 /// and terminate with SIGPIPE on Unix (or succeed on non-Unix where pipe behavior differs).
700 #[rstest]
701 #[case::neg_inf(
702 &["--", "-inf", "0"],
703 b"-inf\n-inf\n-inf\n"
704 )]
705 #[case::neg_infinity(
706 &["--", "-infinity", "0"],
707 b"-inf\n-inf\n-inf\n"
708 )]
709 #[case::inf(
710 &["inf"],
711 b"1\n2\n3\n"
712 )]
713 #[case::infinity(
714 &["infinity"],
715 b"1\n2\n3\n"
716 )]
717 #[case::inf_width(
718 &["-w", "1.000", "inf", "inf"],
719 b"1.000\n inf\n inf\n inf\n"
720 )]
721 #[case::neg_inf_width(
722 &["-w", "1.000", "-inf", "-inf"],
723 b"1.000\n -inf\n -inf\n -inf\n"
724 )]
725 #[case::precision_inf(
726 &["1", "1.2", "inf"],
727 b"1.0\n2.2\n3.4\n"
728 )]
729 #[case::equalize_width_inf(
730 &["-w", "1", "1.2", "inf"],
731 b"1.0\n2.2\n3.4\n"
732 )]
733 fn test_infinite_sequence(#[case] args: &[&str], #[case] expected_start: &[u8]) {
734 let result = new_ucmd!()
735 .args(args)
736 .run_stdout_starts_with(expected_start);
737 #[cfg(unix)]
738 result.signal_name_is("PIPE");
739 #[cfg(not(unix))]
740 result.success();
741 }
742
743 #[test]
744 fn test_ignore_leading_whitespace() {
745 new_ucmd!().arg(" 1").succeeds().stdout_only("1\n");
746 }
747
748 #[test]
749 fn test_trailing_whitespace_error() {
750 // In some locales, the GNU error message has curly quotes (‘)
751 // instead of straight quotes ('). We just test the straight single
752 // quotes.
753 new_ucmd!()
754 .arg("1 ")
755 .fails()
756 .usage_error("invalid floating point argument: '1 '");
757 }
758
759 #[test]
760 fn test_negative_zero_int_start_float_increment() {
761 new_ucmd!()
762 .args(&["-0", "0.1", "0.1"])
763 .succeeds()
764 .stdout_only("-0.0\n0.1\n");
765 }
766
767 #[test]
768 fn test_float_precision_increment() {
769 new_ucmd!()
770 .args(&["999", "0.1", "1000.1"])
771 .succeeds()
772 .stdout_only(
773 "999.0
774 999.1
775 999.2
776 999.3
777 999.4
778 999.5
779 999.6
780 999.7
781 999.8
782 999.9
783 1000.0
784 1000.1
785 ",
786 );
787 }
788
789 /// Test for floating point precision issues.
790 #[test]
791 fn test_negative_increment_decimal() {
792 new_ucmd!()
793 .args(&["0.1", "-0.1", "-0.2"])
794 .succeeds()
795 .stdout_only("0.1\n0.0\n-0.1\n-0.2\n");
796 }
797
798 #[test]
799 fn test_zero_not_first() {
800 new_ucmd!()
801 .args(&["-w", "-0.1", "0.1", "0.1"])
802 .succeeds()
803 .stdout_only("-0.1\n00.0\n00.1\n");
804 }
805
806 #[test]
807 fn test_rounding_end() {
808 new_ucmd!()
809 .args(&["1", "-1", "0.1"])
810 .succeeds()
811 .stdout_only("1\n");
812 }
813
814 #[test]
815 fn test_parse_error_float() {
816 new_ucmd!()
817 .arg("lmnop")
818 .fails()
819 .usage_error("invalid floating point argument: 'lmnop'");
820 }
821
822 #[test]
823 fn test_parse_error_hex() {
824 new_ucmd!()
825 .arg("0xlmnop")
826 .fails()
827 .usage_error("invalid floating point argument: '0xlmnop'");
828 }
829
830 #[test]
831 fn test_format_option() {
832 new_ucmd!()
833 .args(&["-f", "%.2f", "0.0", "0.1", "0.5"])
834 .succeeds()
835 .stdout_only("0.00\n0.10\n0.20\n0.30\n0.40\n0.50\n");
836 }
837
838 #[test]
839 fn test_format_option_default_precision() {
840 new_ucmd!()
841 .args(&["-f", "%f", "0", "0.7", "2"])
842 .succeeds()
843 .stdout_only("0.000000\n0.700000\n1.400000\n");
844 }
845
846 #[test]
847 fn test_format_option_default_precision_short() {
848 new_ucmd!()
849 .args(&["-f", "%g", "0", "0.987654321", "2"])
850 .succeeds()
851 .stdout_only("0\n0.987654\n1.97531\n");
852 }
853
854 #[test]
855 fn test_format_option_default_precision_scientific() {
856 new_ucmd!()
857 .args(&["-f", "%E", "0", "0.7", "2"])
858 .succeeds()
859 .stdout_only("0.000000E+00\n7.000000E-01\n1.400000E+00\n");
860 }
861
862 #[test]
863 fn test_auto_precision() {
864 new_ucmd!()
865 .args(&["1", "0x1p-1", "2"])
866 .succeeds()
867 .stdout_only("1\n1.5\n2\n");
868 }
869
870 #[test]
871 fn test_undefined() {
872 new_ucmd!()
873 .args(&["1e-9223372036854775808"])
874 .succeeds()
875 .no_output();
876 }
877
878 #[test]
879 fn test_invalid_float_point_fail_properly() {
880 // Note that we support arguments that are much bigger than what GNU coreutils supports.
881 // Tests below use exponents larger than we support (i64)
882 new_ucmd!()
883 .args(&["66000e0000000000000000000000000000000000000000000000000000092233720368547758070"])
884 .fails()
885 .no_stdout()
886 .usage_error("invalid floating point argument: '66000e0000000000000000000000000000000000000000000000000000092233720368547758070'");
887 new_ucmd!()
888 .args(&["-1.1e92233720368547758070"])
889 .fails()
890 .no_stdout()
891 .usage_error("invalid floating point argument: '-1.1e92233720368547758070'");
892 new_ucmd!()
893 .args(&["-.1e92233720368547758070"])
894 .fails()
895 .no_stdout()
896 .usage_error("invalid floating point argument: '-.1e92233720368547758070'");
897 }
898
899 #[test]
900 fn test_invalid_zero_increment_value() {
901 new_ucmd!()
902 .args(&["0", "0", "1"])
903 .fails()
904 .no_stdout()
905 .usage_error("invalid Zero increment value: '0'");
906 }
907
908 #[test]
909 fn test_power_of_ten_display() {
910 new_ucmd!()
911 .args(&["-f", "%.2g", "10", "10"])
912 .succeeds()
913 .stdout_only("10\n");
914 }
915
916 #[test]
917 fn test_default_g_precision() {
918 new_ucmd!()
919 .args(&["-f", "%010g", "1e5", "1e5"])
920 .succeeds()
921 .stdout_only("0000100000\n");
922 new_ucmd!()
923 .args(&["-f", "%010g", "1e6", "1e6"])
924 .succeeds()
925 .stdout_only("000001e+06\n");
926 }
927
928 #[test]
929 fn test_invalid_format() {
930 new_ucmd!()
931 .args(&["-f", "%%g", "1"])
932 .fails()
933 .no_stdout()
934 .stderr_contains("format '%%g' has no % directive");
935 new_ucmd!()
936 .args(&["-f", "%g%g", "1"])
937 .fails()
938 .no_stdout()
939 .stderr_contains("format '%g%g' has too many % directives");
940 new_ucmd!()
941 .args(&["-f", "%g%", "1"])
942 .fails()
943 .no_stdout()
944 .stderr_contains("format '%g%' has too many % directives");
945 new_ucmd!()
946 .args(&["-f", "%", "1"])
947 .fails()
948 .no_stdout()
949 .stderr_contains("format '%' ends in %");
950 }
951
952 #[test]
953 fn test_parse_scientific_zero() {
954 new_ucmd!()
955 .args(&["0e15", "1"])
956 .succeeds()
957 .stdout_only("0\n1\n");
958 new_ucmd!()
959 .args(&["0.0e15", "1"])
960 .succeeds()
961 .stdout_only("0\n1\n");
962 new_ucmd!()
963 .args(&["0", "1"])
964 .succeeds()
965 .stdout_only("0\n1\n");
966 new_ucmd!()
967 .args(&["-w", "0e15", "1"])
968 .succeeds()
969 .stdout_only("0000000000000000\n0000000000000001\n");
970 new_ucmd!()
971 .args(&["-w", "0.0e15", "1"])
972 .succeeds()
973 .stdout_only("0000000000000000\n0000000000000001\n");
974 new_ucmd!()
975 .args(&["-w", "0", "1"])
976 .succeeds()
977 .stdout_only("0\n1\n");
978 }
979
980 #[test]
981 fn test_parse_valid_hexadecimal_float_two_args() {
982 let test_cases = [
983 (["0x1p-1", "2"], "0.5\n1.5\n"),
984 (["0x.8p16", "32768"], "32768\n"),
985 (["0xffff.4p-4", "4096"], "4095.95\n"),
986 (["0xA.A9p-1", "6"], "5.33008\n"),
987 (["0xa.a9p-1", "6"], "5.33008\n"),
988 (["0xffffffffffp-30", "1024"], "1024\n"), // spell-checker:disable-line
989 ([" 0XA.A9P-1", "6"], "5.33008\n"),
990 ([" 0xee.", " 0xef."], "238\n239\n"),
991 ];
992
993 for (input_arguments, expected_output) in &test_cases {
994 new_ucmd!()
995 .args(input_arguments)
996 .succeeds()
997 .stdout_only(expected_output);
998 }
999 }
1000
1001 #[test]
1002 fn test_parse_valid_hexadecimal_float_three_args() {
1003 let test_cases = [
1004 (["0x3.4p-1", "0x4p-1", "4"], "1.625\n3.625\n"),
1005 (
1006 ["-0x.ep-3", "-0x.1p-3", "-0x.fp-3"],
1007 "-0.109375\n-0.117188\n",
1008 ),
1009 ];
1010
1011 for (input_arguments, expected_output) in &test_cases {
1012 new_ucmd!()
1013 .args(input_arguments)
1014 .succeeds()
1015 .stdout_only(expected_output);
1016 }
1017 }
1018
1019 #[test]
1020 fn test_seq_float_precision_edge_cases() {
1021 // Known sequence values for verification
1022 new_ucmd!()
1023 .args(&[".64999", "1e-7", ".6499901"])
1024 .succeeds()
1025 .stdout_only("0.6499900\n0.6499901\n");
1026
1027 new_ucmd!()
1028 .args(&["0", "0.000002", "0.000006"])
1029 .succeeds()
1030 .stdout_only("0.000000\n0.000002\n0.000004\n0.000006\n");
1031 }
1032
1033 #[test]
1034 fn test_parse_out_of_bounds_exponents() {
1035 // The value 1e-9223372036854775808 is used in GNU Coreutils and BigDecimal tests to verify
1036 // overflows and undefined behavior. Let's test the value too.
1037 new_ucmd!()
1038 .args(&["1e-9223372036854775808"])
1039 .succeeds()
1040 .stdout_only("");
1041
1042 // GNU seq supports arbitrarily small exponents (and treats the value as 0).
1043 new_ucmd!()
1044 .args(&["1e-922337203685477580800000000", "1"])
1045 .succeeds()
1046 .stdout_only("0\n1\n");
1047
1048 // Check we can also underflow to -0.0.
1049 new_ucmd!()
1050 .args(&["-1e-922337203685477580800000000", "1"])
1051 .succeeds()
1052 .stdout_only("-0\n1\n");
1053 }
1054
1055 #[ignore = ""]
1056 #[test]
1057 fn test_parse_valid_hexadecimal_float_format_issues() {
1058 // These tests detect differences in the representation of floating-point values with GNU seq.
1059 // There are two key areas to investigate:
1060 //
1061 // 1. GNU seq uses long double (80-bit) types for values, while the current implementation
1062 // relies on f64 (64-bit). This can lead to differences due to varying precision. However, it's
1063 // likely not the primary cause, as even double (64-bit) values can differ when compared to
1064 // f64.
1065 //
1066 // 2. GNU seq uses the %Lg format specifier for printing (see the "get_default_format" function
1067 // ). It appears that Rust lacks a direct equivalent for this format. Additionally, %Lg
1068 // can use %f (floating) or %e (scientific) depending on the precision. There also seem to be
1069 // some differences in the behavior of C and Rust when displaying floating-point or scientific
1070 // notation, at least without additional configuration.
1071 //
1072 // It makes sense to begin by experimenting with formats and attempting to replicate
1073 // the printf("%Lg",...) behavior. Another area worth investigating is glibc, as reviewing its
1074 // code may help uncover additional corner cases or test data that could reveal more issues.
1075
1076 //Test output: 0.00000000992804416455328464508056640625
1077 new_ucmd!()
1078 .args(&["0xa.a9p-30", "1"])
1079 .succeeds()
1080 .stdout_only("9.92804e-09\n1\n");
1081 }
1082
1083 // GNU `seq` manual states that, when the parameters "all use a fixed point
1084 // decimal representation", the format should be `%.pf`, where the precision
1085 // is inferred from parameters. Else, `%g` is used.
1086 //
1087 // This is understandable, as translating hexadecimal precision to decimal precision
1088 // is not straightforward or intuitive to the user. There are some exceptions though,
1089 // if a mix of hexadecimal _integers_ and decimal floats are provided.
1090 //
1091 // The way precision is inferred is said to be able to "represent the output numbers
1092 // exactly". In practice, this means that trailing zeros in first/increment number are
1093 // considered, but not in the last number. This makes sense if we take that last number
1094 // as a "bound", and not really part of input/output.
1095 #[test]
1096 fn test_precision_corner_cases() {
1097 // Mixed input with integer hex still uses precision in decimal float
1098 new_ucmd!()
1099 .args(&["0x1", "0.90", "3"])
1100 .succeeds()
1101 .stdout_is("1.00\n1.90\n2.80\n");
1102
1103 // Mixed input with hex float reverts to %g
1104 new_ucmd!()
1105 .args(&["0x1.00", "0.90", "3"])
1106 .succeeds()
1107 .stdout_is("1\n1.9\n2.8\n");
1108
1109 // Even if it's the last number.
1110 new_ucmd!()
1111 .args(&["1", "1.20", "0x3.000000"])
1112 .succeeds()
1113 .stdout_is("1\n2.2\n");
1114
1115 // Otherwise, precision in last number is ignored.
1116 new_ucmd!()
1117 .args(&["1", "1.20", "3.000000"])
1118 .succeeds()
1119 .stdout_is("1.00\n2.20\n");
1120 }
1121
1122 // GNU `seq` manual only makes guarantees about `-w` working if the
1123 // provided numbers "all use a fixed point decimal representation",
1124 // and guides the user to use `-f` for other cases.
1125 #[test]
1126 fn test_equalize_widths_corner_cases() {
1127 // Mixed input with hexadecimal does behave as expected
1128 new_ucmd!()
1129 .args(&["-w", "0x1", "5.2", "9"])
1130 .succeeds()
1131 .stdout_is("1.0\n6.2\n");
1132
1133 // Confusingly, the number of integral digits in the last number is
1134 // used to pad the output numbers, while it is ignored for precision
1135 // computation.
1136 //
1137 // This problem has been reported on list here:
1138 // "bug#77179: seq incorrectly(?) pads output when last parameter magnitude"
1139 // https://lists.gnu.org/archive/html/bug-coreutils/2025-03/msg00028.html
1140 //
1141 // TODO: This case could be handled correctly, consider fixing this in
1142 // `uutils` implementation. Output should probably be "1.0\n6.2\n".
1143 new_ucmd!()
1144 .args(&["-w", "0x1", "5.2", "10.0000"])
1145 .succeeds()
1146 .stdout_is("01.0\n06.2\n");
1147
1148 // But if we fixed the case above, we need to make sure we still pad
1149 // if the last number in the output requires an extra digit.
1150 new_ucmd!()
1151 .args(&["-w", "0x1", "5.2", "15.0000"])
1152 .succeeds()
1153 .stdout_is("01.0\n06.2\n11.4\n");
1154
1155 // GNU `seq` bails out if any hex float is in the output.
1156 // Unlike the precision corner cases above, it is harder to justify
1157 // this behavior for hexadecimal float inputs, as it is always be
1158 // possible to output numbers with a fixed width.
1159 //
1160 // This problem has been reported on list here:
1161 // "bug#76070: Subject: seq, hexadecimal args and equal width"
1162 // https://lists.gnu.org/archive/html/bug-coreutils/2025-02/msg00007.html
1163 //
1164 // TODO: These cases could be handled correctly, consider fixing this in
1165 // `uutils` implementation.
1166 // If we ignore hexadecimal precision, the output should be "1.0\n6.2\n".
1167 new_ucmd!()
1168 .args(&["-w", "0x1.0000", "5.2", "10"])
1169 .succeeds()
1170 .stdout_is("1\n6.2\n");
1171 // The equivalent `seq -w 1.0625 1.00002 3` correctly pads the first number: "1.06250\n2.06252\n"
1172 new_ucmd!()
1173 .args(&["-w", "0x1.1", "1.00002", "3"])
1174 .succeeds()
1175 .stdout_is("1.0625\n2.06252\n");
1176 }
1177
1178 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
1179 mod diagnostics {
1180 use super::*;
1181
1182 #[cfg(unix)]
1183 #[test]
1184 fn test_snippet_points_at_the_failing_conversion() {
1185 let result = new_ucmd!()
1186 .terminal_sim_stderr()
1187 .args(&["-f", "%5.2c", "1", "3"])
1188 .fails_with_code(1);
1189
1190 assert_eq!(
1191 result.stderr_as_displayed(),
1192 "\
1193 seq: %5.2c: invalid conversion specification
1194 ╭─[ seq:1:8 ]
1195 │
1196 1 │ seq -f %5.2c 1 3
1197 │ ─────
1198 │
1199 │ Help: a format holds exactly one float conversion: %f, %e, %g or %a, as in -f%.3f
1200 ───╯"
1201 );
1202 }
1203
1204 #[cfg(unix)]
1205 #[test]
1206 fn test_snippet_points_inside_a_glued_short_option() {
1207 // `-f%q` is split for clap, but the report echoes what was typed.
1208 let result = new_ucmd!()
1209 .terminal_sim_stderr()
1210 .args(&["-f%q", "1", "3"])
1211 .fails_with_code(1);
1212 let stderr = result.stderr_as_displayed();
1213
1214 assert!(stderr.contains("1 │ seq -f%q 1 3"), "{stderr}");
1215 assert!(stderr.contains("seq:1:7"), "{stderr}");
1216 }
1217
1218 #[cfg(unix)]
1219 #[test]
1220 fn test_snippet_underlines_a_format_with_no_directive() {
1221 let result = new_ucmd!()
1222 .terminal_sim_stderr()
1223 .args(&["--format=abc", "1", "3"])
1224 .fails_with_code(1);
1225 let stderr = result.stderr_as_displayed();
1226
1227 assert!(stderr.contains("seq:1:14"), "{stderr}");
1228 assert!(stderr.contains("has no % directive"), "{stderr}");
1229 }
1230
1231 #[test]
1232 fn test_plain_message_when_stderr_is_a_pipe() {
1233 new_ucmd!()
1234 .args(&["-f", "%5.2c", "1", "3"])
1235 .fails_with_code(1)
1236 .stderr_is("seq: %5.2c: invalid conversion specification\n");
1237 }
1238 }