1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

seq: use print_seq_integers() regardless of last

Ensure that the `print_seq_integers()` function is called when the first
number and the increment are integers, regardless of the type of the
last value specified.
This commit is contained in:
Jeffrey Finkelstein 2021-09-16 23:02:02 -04:00
parent 522d4d39e3
commit bfb1327ad4
2 changed files with 147 additions and 8 deletions

View file

@ -67,6 +67,18 @@ impl Number {
Number::F64(n) => n, Number::F64(n) => n,
} }
} }
/// Convert this number into a bigint, consuming it.
///
/// For floats, this returns the [`BigInt`] corresponding to the
/// floor of the number.
fn into_bigint(self) -> BigInt {
match self {
Number::MinusZero => BigInt::zero(),
Number::F64(x) => BigInt::from(x.floor() as i64),
Number::BigInt(n) => n,
}
}
} }
impl FromStr for Number { impl FromStr for Number {
@ -197,25 +209,38 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
crash_if_err!(1, slice.parse()) crash_if_err!(1, slice.parse())
}; };
let is_negative_zero_f64 = |x: f64| x == -0.0 && x.is_sign_negative() && largest_dec == 0;
let result = match (first, last, increment) { let result = match (first, last, increment) {
(Number::MinusZero, Number::BigInt(last), Number::BigInt(increment)) => print_seq_integers( // For example, `seq -0 1 2` or `seq -0 1 2.0`.
(BigInt::zero(), increment, last), (Number::MinusZero, last, Number::BigInt(increment)) => print_seq_integers(
(BigInt::zero(), increment, last.into_bigint()),
options.separator, options.separator,
options.terminator, options.terminator,
options.widths, options.widths,
padding, padding,
true, true,
), ),
(Number::BigInt(first), Number::BigInt(last), Number::BigInt(increment)) => { // For example, `seq -0e0 1 2` or `seq -0e0 1 2.0`.
(Number::F64(x), last, Number::BigInt(increment)) if is_negative_zero_f64(x) => {
print_seq_integers( print_seq_integers(
(first, increment, last), (BigInt::zero(), increment, last.into_bigint()),
options.separator, options.separator,
options.terminator, options.terminator,
options.widths, options.widths,
padding, padding,
false, true,
) )
} }
// For example, `seq 0 1 2` or `seq 0 1 2.0`.
(Number::BigInt(first), last, Number::BigInt(increment)) => print_seq_integers(
(first, increment, last.into_bigint()),
options.separator,
options.terminator,
options.widths,
padding,
false,
),
// For example, `seq 0 0.5 1` or `seq 0.0 0.5 1` or `seq 0.0 0.5 1.0`.
(first, last, increment) => print_seq( (first, last, increment) => print_seq(
(first.into_f64(), increment.into_f64(), last.into_f64()), (first.into_f64(), increment.into_f64(), last.into_f64()),
largest_dec, largest_dec,

View file

@ -199,6 +199,16 @@ fn test_preserve_negative_zero_start() {
.succeeds() .succeeds()
.stdout_is("-0\n1\n") .stdout_is("-0\n1\n")
.no_stderr(); .no_stderr();
new_ucmd!()
.args(&["-0", "1", "2"])
.succeeds()
.stdout_is("-0\n1\n2\n")
.no_stderr();
new_ucmd!()
.args(&["-0", "1", "2.0"])
.succeeds()
.stdout_is("-0\n1\n2\n")
.no_stderr();
} }
#[test] #[test]
@ -226,6 +236,50 @@ fn test_width_negative_zero() {
.succeeds() .succeeds()
.stdout_is("-0\n01\n") .stdout_is("-0\n01\n")
.no_stderr(); .no_stderr();
new_ucmd!()
.args(&["-w", "-0", "1", "2"])
.succeeds()
.stdout_is("-0\n01\n02\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0", "1", "2.0"])
.succeeds()
.stdout_is("-0\n01\n02\n")
.no_stderr();
}
#[test]
fn test_width_negative_zero_decimal_notation() {
new_ucmd!()
.args(&["-w", "-0.0", "1"])
.succeeds()
.stdout_is("-0.0\n01.0\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.0", "1.0"])
.succeeds()
.stdout_is("-0.0\n01.0\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.0", "1", "2"])
.succeeds()
.stdout_is("-0.0\n01.0\n02.0\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.0", "1", "2.0"])
.succeeds()
.stdout_is("-0.0\n01.0\n02.0\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.0", "1.0", "2"])
.succeeds()
.stdout_is("-0.0\n01.0\n02.0\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.0", "1.0", "2.0"])
.succeeds()
.stdout_is("-0.0\n01.0\n02.0\n")
.no_stderr();
} }
#[test] #[test]
@ -235,29 +289,63 @@ fn test_width_negative_zero_scientific_notation() {
.succeeds() .succeeds()
.stdout_is("-0\n01\n") .stdout_is("-0\n01\n")
.no_stderr(); .no_stderr();
new_ucmd!()
.args(&["-w", "-0e0", "1", "2"])
.succeeds()
.stdout_is("-0\n01\n02\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0e0", "1", "2.0"])
.succeeds()
.stdout_is("-0\n01\n02\n")
.no_stderr();
new_ucmd!() new_ucmd!()
.args(&["-w", "-0e+1", "1"]) .args(&["-w", "-0e+1", "1"])
.succeeds() .succeeds()
.stdout_is("-00\n001\n") .stdout_is("-00\n001\n")
.no_stderr(); .no_stderr();
new_ucmd!()
.args(&["-w", "-0e+1", "1", "2"])
.succeeds()
.stdout_is("-00\n001\n002\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0e+1", "1", "2.0"])
.succeeds()
.stdout_is("-00\n001\n002\n")
.no_stderr();
new_ucmd!() new_ucmd!()
.args(&["-w", "-0.000e0", "1"]) .args(&["-w", "-0.000e0", "1"])
.succeeds() .succeeds()
.stdout_is("-0.000\n01.000\n") .stdout_is("-0.000\n01.000\n")
.no_stderr(); .no_stderr();
new_ucmd!()
.args(&["-w", "-0.000e0", "1", "2"])
.succeeds()
.stdout_is("-0.000\n01.000\n02.000\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.000e0", "1", "2.0"])
.succeeds()
.stdout_is("-0.000\n01.000\n02.000\n")
.no_stderr();
new_ucmd!() new_ucmd!()
.args(&["-w", "-0.000e-2", "1"]) .args(&["-w", "-0.000e-2", "1"])
.succeeds() .succeeds()
.stdout_is("-0.00000\n01.00000\n") .stdout_is("-0.00000\n01.00000\n")
.no_stderr(); .no_stderr();
new_ucmd!() new_ucmd!()
.args(&["-w", "-0.000e5", "1"]) .args(&["-w", "-0.000e-2", "1", "2"])
.succeeds() .succeeds()
.stdout_is("-000000\n0000001\n") .stdout_is("-0.00000\n01.00000\n02.00000\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.000e-2", "1", "2.0"])
.succeeds()
.stdout_is("-0.00000\n01.00000\n02.00000\n")
.no_stderr(); .no_stderr();
new_ucmd!() new_ucmd!()
@ -265,6 +353,32 @@ fn test_width_negative_zero_scientific_notation() {
.succeeds() .succeeds()
.stdout_is("-000000\n0000001\n") .stdout_is("-000000\n0000001\n")
.no_stderr(); .no_stderr();
new_ucmd!()
.args(&["-w", "-0.000e5", "1", "2"])
.succeeds()
.stdout_is("-000000\n0000001\n0000002\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.000e5", "1", "2.0"])
.succeeds()
.stdout_is("-000000\n0000001\n0000002\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.000e5", "1"])
.succeeds()
.stdout_is("-000000\n0000001\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.000e5", "1", "2"])
.succeeds()
.stdout_is("-000000\n0000001\n0000002\n")
.no_stderr();
new_ucmd!()
.args(&["-w", "-0.000e5", "1", "2.0"])
.succeeds()
.stdout_is("-000000\n0000001\n0000002\n")
.no_stderr();
} }
#[test] #[test]