mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
seq: compute correct width for scientific notation
Change the way `seq` computes the number of digits needed to print a number so that it works for inputs given in scientific notation. Specifically, this commit parses the input string to determine whether it is an integer, a float in decimal notation, or a float in scientific notation, and then computes the number of integral digits and the number of fractional digits based on that. This also supports floating point negative zero, expressed in both decimal and scientific notation.
This commit is contained in:
parent
88a689075c
commit
2ac5dc0a70
3 changed files with 381 additions and 15 deletions
|
@ -23,6 +23,56 @@ fn test_rejects_non_floats() {
|
|||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_float() {
|
||||
new_ucmd!()
|
||||
.args(&["1e2.3"])
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("invalid floating point argument: '1e2.3'")
|
||||
.stderr_contains("for more information.");
|
||||
new_ucmd!()
|
||||
.args(&["1e2.3", "2"])
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("invalid floating point argument: '1e2.3'")
|
||||
.stderr_contains("for more information.");
|
||||
new_ucmd!()
|
||||
.args(&["1", "1e2.3"])
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("invalid floating point argument: '1e2.3'")
|
||||
.stderr_contains("for more information.");
|
||||
new_ucmd!()
|
||||
.args(&["1e2.3", "2", "3"])
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("invalid floating point argument: '1e2.3'")
|
||||
.stderr_contains("for more information.");
|
||||
new_ucmd!()
|
||||
.args(&["1", "1e2.3", "3"])
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("invalid floating point argument: '1e2.3'")
|
||||
.stderr_contains("for more information.");
|
||||
new_ucmd!()
|
||||
.args(&["1", "2", "1e2.3"])
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("invalid floating point argument: '1e2.3'")
|
||||
.stderr_contains("for more information.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_width_invalid_float() {
|
||||
new_ucmd!()
|
||||
.args(&["-w", "1e2.3"])
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("invalid floating point argument: '1e2.3'")
|
||||
.stderr_contains("for more information.");
|
||||
}
|
||||
|
||||
// ---- Tests for the big integer based path ----
|
||||
|
||||
#[test]
|
||||
|
@ -178,6 +228,90 @@ fn test_width_negative_zero() {
|
|||
.no_stderr();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_width_negative_zero_scientific_notation() {
|
||||
new_ucmd!()
|
||||
.args(&["-w", "-0e0", "1"])
|
||||
.succeeds()
|
||||
.stdout_is("-0\n01\n")
|
||||
.no_stderr();
|
||||
|
||||
new_ucmd!()
|
||||
.args(&["-w", "-0e+1", "1"])
|
||||
.succeeds()
|
||||
.stdout_is("-00\n001\n")
|
||||
.no_stderr();
|
||||
|
||||
new_ucmd!()
|
||||
.args(&["-w", "-0.000e0", "1"])
|
||||
.succeeds()
|
||||
.stdout_is("-0.000\n01.000\n")
|
||||
.no_stderr();
|
||||
|
||||
new_ucmd!()
|
||||
.args(&["-w", "-0.000e-2", "1"])
|
||||
.succeeds()
|
||||
.stdout_is("-0.00000\n01.00000\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"])
|
||||
.succeeds()
|
||||
.stdout_is("-000000\n0000001\n")
|
||||
.no_stderr();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_width_decimal_scientific_notation_increment() {
|
||||
new_ucmd!()
|
||||
.args(&["-w", ".1", "1e-2", ".11"])
|
||||
.succeeds()
|
||||
.stdout_is("0.10\n0.11\n")
|
||||
.no_stderr();
|
||||
|
||||
new_ucmd!()
|
||||
.args(&["-w", ".0", "1.500e-1", ".2"])
|
||||
.succeeds()
|
||||
.stdout_is("0.0000\n0.1500\n")
|
||||
.no_stderr();
|
||||
}
|
||||
|
||||
/// Test that trailing zeros in the start argument contribute to precision.
|
||||
#[test]
|
||||
fn test_width_decimal_scientific_notation_trailing_zeros_start() {
|
||||
new_ucmd!()
|
||||
.args(&["-w", ".1000", "1e-2", ".11"])
|
||||
.succeeds()
|
||||
.stdout_is("0.1000\n0.1100\n")
|
||||
.no_stderr();
|
||||
}
|
||||
|
||||
/// Test that trailing zeros in the increment argument contribute to precision.
|
||||
#[test]
|
||||
fn test_width_decimal_scientific_notation_trailing_zeros_increment() {
|
||||
new_ucmd!()
|
||||
.args(&["-w", "1e-1", "0.0100", ".11"])
|
||||
.succeeds()
|
||||
.stdout_is("0.1000\n0.1100\n")
|
||||
.no_stderr();
|
||||
}
|
||||
|
||||
/// Test that trailing zeros in the end argument do not contribute to width.
|
||||
#[test]
|
||||
fn test_width_decimal_scientific_notation_trailing_zeros_end() {
|
||||
new_ucmd!()
|
||||
.args(&["-w", "1e-1", "1e-2", ".1100"])
|
||||
.succeeds()
|
||||
.stdout_is("0.10\n0.11\n")
|
||||
.no_stderr();
|
||||
}
|
||||
|
||||
// TODO This is duplicated from `test_yes.rs`; refactor them.
|
||||
/// Run `seq`, capture some of the output, close the pipe, and verify it.
|
||||
fn run(args: &[&str], expected: &[u8]) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue