2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:ignore (paths) gnutest
|
|
|
|
|
2020-05-25 17:05:26 +00:00
|
|
|
use crate::common::util::*;
|
2017-10-22 07:57:49 +00:00
|
|
|
|
2022-07-24 12:06:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_should_not_round_floats() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["0.99", "1.01", "1.1", "1.22", ".1", "-0.1"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("0.99\n1.01\n1.1\n1.22\n0.1\n-0.1\n");
|
|
|
|
}
|
|
|
|
|
2017-10-22 07:57:49 +00:00
|
|
|
#[test]
|
|
|
|
fn test_from_si() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si"])
|
|
|
|
.pipe_in("1000\n1.1M\n0.1G")
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is("1000\n1100000\n100000000\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_iec() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=iec"])
|
|
|
|
.pipe_in("1024\n1.1M\n0.1G")
|
|
|
|
.run()
|
2021-03-06 17:26:05 +00:00
|
|
|
.stdout_is("1024\n1153434\n107374183\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_iec_i() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=iec-i"])
|
2021-03-06 17:26:05 +00:00
|
|
|
.pipe_in("1.1Mi\n0.1Gi")
|
2017-10-22 07:57:49 +00:00
|
|
|
.run()
|
2021-03-06 17:26:05 +00:00
|
|
|
.stdout_is("1153434\n107374183\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_iec_i_requires_suffix() {
|
2022-07-12 08:58:07 +00:00
|
|
|
let numbers = vec!["1024", "10M"];
|
|
|
|
|
|
|
|
for number in numbers {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=iec-i", number])
|
|
|
|
.fails()
|
|
|
|
.code_is(2)
|
|
|
|
.stderr_is(format!(
|
|
|
|
"numfmt: missing 'i' suffix in input: '{}' (e.g Ki/Mi/Gi)",
|
|
|
|
number
|
|
|
|
));
|
|
|
|
}
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_auto() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in("1K\n1Ki")
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is("1000\n1024\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_si() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--to=si"])
|
|
|
|
.pipe_in("1000\n1100000\n100000000")
|
|
|
|
.run()
|
2021-03-06 17:26:05 +00:00
|
|
|
.stdout_is("1.0K\n1.1M\n100M\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_iec() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--to=iec"])
|
|
|
|
.pipe_in("1024\n1153434\n107374182")
|
|
|
|
.run()
|
2021-03-06 17:26:05 +00:00
|
|
|
.stdout_is("1.0K\n1.2M\n103M\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_iec_i() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--to=iec-i"])
|
|
|
|
.pipe_in("1024\n1153434\n107374182")
|
|
|
|
.run()
|
2021-03-06 17:26:05 +00:00
|
|
|
.stdout_is("1.0Ki\n1.2Mi\n103Mi\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_input_from_free_arguments() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si", "1K", "1.1M", "0.1G"])
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is("1000\n1100000\n100000000\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_padding() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si", "--padding=8"])
|
|
|
|
.pipe_in("1K\n1.1M\n0.1G")
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is(" 1000\n 1100000\n100000000\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_negative_padding() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si", "--padding=-8"])
|
|
|
|
.pipe_in("1K\n1.1M\n0.1G")
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is("1000 \n1100000 \n100000000\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_header() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si", "--header=2"])
|
|
|
|
.pipe_in("header\nheader2\n1K\n1.1M\n0.1G")
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is("header\nheader2\n1000\n1100000\n100000000\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_header_default() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si", "--header"])
|
|
|
|
.pipe_in("header\n1K\n1.1M\n0.1G")
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is("header\n1000\n1100000\n100000000\n");
|
2017-10-22 07:57:49 +00:00
|
|
|
}
|
2018-09-04 15:41:01 +00:00
|
|
|
|
2021-02-14 23:25:28 +00:00
|
|
|
#[test]
|
|
|
|
fn test_header_error_if_non_numeric() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--header=two"])
|
|
|
|
.run()
|
2021-06-19 22:21:14 +00:00
|
|
|
.stderr_is("numfmt: invalid header value 'two'");
|
2021-02-14 23:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_header_error_if_0() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--header=0"])
|
|
|
|
.run()
|
2021-06-19 22:21:14 +00:00
|
|
|
.stderr_is("numfmt: invalid header value '0'");
|
2021-02-14 23:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_header_error_if_negative() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--header=-3"])
|
|
|
|
.run()
|
2021-06-19 22:21:14 +00:00
|
|
|
.stderr_is("numfmt: invalid header value '-3'");
|
2021-02-14 23:25:28 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 15:41:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_negative() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si"])
|
|
|
|
.pipe_in("-1000\n-1.1M\n-0.1G")
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is("-1000\n-1100000\n-100000000\n");
|
2018-09-04 15:41:01 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--to=iec-i"])
|
|
|
|
.pipe_in("-1024\n-1153434\n-107374182")
|
|
|
|
.run()
|
2021-03-06 17:26:05 +00:00
|
|
|
.stdout_is("-1.0Ki\n-1.2Mi\n-103Mi\n");
|
2018-09-04 15:41:01 +00:00
|
|
|
}
|
2018-09-24 13:29:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_op() {
|
|
|
|
new_ucmd!()
|
|
|
|
.pipe_in("1024\n1234567")
|
|
|
|
.run()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_is("1024\n1234567\n");
|
2018-09-24 13:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si", "--to=si"])
|
|
|
|
.pipe_in("10000000K\n0.001K")
|
|
|
|
.run()
|
2021-03-06 17:26:05 +00:00
|
|
|
.stdout_is("10G\n1\n");
|
2018-09-24 13:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_si_to_iec() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=si", "--to=iec", "15334263563K"])
|
|
|
|
.run()
|
2021-03-06 17:26:05 +00:00
|
|
|
.stdout_is("14T\n");
|
2018-09-24 13:29:05 +00:00
|
|
|
}
|
2021-02-14 08:04:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_report_invalid_empty_number_on_empty_stdin() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in("\n")
|
|
|
|
.run()
|
2021-06-19 22:21:14 +00:00
|
|
|
.stderr_is("numfmt: invalid number: ''\n");
|
2021-02-14 08:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_report_invalid_empty_number_on_blank_stdin() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in(" \t \n")
|
|
|
|
.run()
|
2021-06-19 22:21:14 +00:00
|
|
|
.stderr_is("numfmt: invalid number: ''\n");
|
2021-02-14 08:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_report_invalid_suffix_on_stdin() {
|
2022-07-19 12:07:46 +00:00
|
|
|
for c in b'a'..=b'z' {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in(format!("1{}", c as char))
|
|
|
|
.run()
|
|
|
|
.stderr_is(format!(
|
|
|
|
"numfmt: invalid suffix in input: '1{}'\n",
|
|
|
|
c as char
|
|
|
|
));
|
|
|
|
}
|
2021-02-14 08:04:29 +00:00
|
|
|
|
|
|
|
// GNU numfmt reports this one as “invalid number”
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in("NaN")
|
|
|
|
.run()
|
2021-06-19 22:21:14 +00:00
|
|
|
.stderr_is("numfmt: invalid suffix in input: 'NaN'\n");
|
2021-02-14 08:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_report_invalid_number_with_interior_junk() {
|
|
|
|
// GNU numfmt reports this as “invalid suffix”
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in("1x0K")
|
|
|
|
.run()
|
2021-06-19 22:21:14 +00:00
|
|
|
.stderr_is("numfmt: invalid number: '1x0K'\n");
|
2021-02-14 08:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_skip_leading_space_from_stdin() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in(" 2Ki")
|
|
|
|
.run()
|
|
|
|
.stdout_is("2048\n");
|
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
// multi-line
|
2021-02-14 08:04:29 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in("\t1Ki\n 2K")
|
|
|
|
.run()
|
|
|
|
.stdout_is("1024\n2000\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_convert_only_first_number_in_line() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in("1Ki 2M 3G")
|
|
|
|
.run()
|
|
|
|
.stdout_is("1024 2M 3G\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_leading_whitespace_should_imply_padding() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in(" 1K")
|
|
|
|
.run()
|
|
|
|
.stdout_is(" 1000\n");
|
|
|
|
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in(" 202Ki")
|
|
|
|
.run()
|
|
|
|
.stdout_is(" 206848\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_calculate_implicit_padding_per_line() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto"])
|
|
|
|
.pipe_in(" 1Ki\n 2K")
|
|
|
|
.run()
|
|
|
|
.stdout_is(" 1024\n 2000\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_leading_whitespace_in_free_argument_should_imply_padding() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", " 1Ki"])
|
|
|
|
.run()
|
|
|
|
.stdout_is(" 1024\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_calculate_implicit_padding_per_free_argument() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", " 1Ki", " 2K"])
|
|
|
|
.run()
|
|
|
|
.stdout_is(" 1024\n 2000\n");
|
|
|
|
}
|
2021-03-06 17:26:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_si_should_truncate_output() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--to=si"])
|
|
|
|
.pipe_in_fixture("gnutest_si_input.txt")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture("gnutest_si_result.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_iec_should_truncate_output() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--to=iec"])
|
|
|
|
.pipe_in_fixture("gnutest_iec_input.txt")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture("gnutest_iec_result.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_iec_i_should_truncate_output() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--to=iec-i"])
|
|
|
|
.pipe_in_fixture("gnutest_iec_input.txt")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture("gnutest_iec-i_result.txt");
|
|
|
|
}
|
2021-02-28 18:06:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_selected_field() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", "--field", "3", "1K 2K 3K"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1K 2K 3000\n");
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", "--field", "2", "1K 2K 3K"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1K 2000 3K\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_selected_fields() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", "--field", "1,4,3", "1K 2K 3K 4K 5K 6K"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1000 2K 3000 4000 5K 6K\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_succeed_if_selected_field_out_of_range() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", "--field", "9", "1K 2K 3K"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1K 2K 3K\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_selected_field_range() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", "--field", "2-5", "1K 2K 3K 4K 5K 6K"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1K 2000 3000 4000 5000 6K\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_should_succeed_if_range_out_of_bounds() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", "--field", "5-10", "1K 2K 3K 4K 5K 6K"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1K 2K 3K 4K 5000 6000\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_implied_initial_field_value() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", "--field", "-2", "1K 2K 3K"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1000 2000 3K\n");
|
|
|
|
|
|
|
|
// same as above but with the equal sign
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from=auto", "--field=-2", "1K 2K 3K"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1000 2000 3K\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_field_df_example() {
|
|
|
|
// df -B1 | numfmt --header --field 2-4 --to=si
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--header", "--field", "2-4", "--to=si"])
|
|
|
|
.pipe_in_fixture("df_input.txt")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture("df_expected.txt");
|
|
|
|
}
|
2021-03-15 15:20:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_must_not_be_empty() {
|
|
|
|
new_ucmd!().args(&["-d"]).fails();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_must_not_be_more_than_one_character() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--delimiter", "sad"])
|
|
|
|
.fails()
|
|
|
|
.stderr_is("numfmt: the delimiter must be a single character");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_only() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d", ","])
|
|
|
|
.pipe_in("1234,56")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1234,56\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_line_is_field_with_no_delimiter() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d,", "--to=iec"])
|
|
|
|
.pipe_in("123456")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("121K\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_to_si() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d=,", "--to=si"])
|
|
|
|
.pipe_in("1234,56")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1.3K,56\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_skips_leading_whitespace() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d=,", "--to=si"])
|
|
|
|
.pipe_in(" \t 1234,56")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1.3K,56\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_preserves_leading_whitespace_in_unselected_fields() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d=|", "--to=si"])
|
|
|
|
.pipe_in(" 1000| 2000")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1.0K| 2000\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_from_si() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d=,", "--from=si"])
|
|
|
|
.pipe_in("1.2K,56")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1200,56\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_overrides_whitespace_separator() {
|
|
|
|
// GNU numfmt reports this as “invalid suffix”
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d,"])
|
|
|
|
.pipe_in("1 234,56")
|
|
|
|
.fails()
|
2021-06-19 22:21:14 +00:00
|
|
|
.stderr_is("numfmt: invalid number: '1 234'\n");
|
2021-03-15 15:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_with_padding() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d=|", "--to=si", "--padding=5"])
|
|
|
|
.pipe_in("1000|2000")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only(" 1.0K|2000\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delimiter_with_padding_and_fields() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["-d=|", "--to=si", "--padding=5", "--field=-"])
|
|
|
|
.pipe_in("1000|2000")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only(" 1.0K| 2.0K\n");
|
|
|
|
}
|
2021-06-19 22:21:14 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_round() {
|
2022-04-02 08:47:37 +00:00
|
|
|
for (method, exp) in [
|
2021-06-19 22:21:14 +00:00
|
|
|
("from-zero", ["9.1K", "-9.1K", "9.1K", "-9.1K"]),
|
|
|
|
("towards-zero", ["9.0K", "-9.0K", "9.0K", "-9.0K"]),
|
|
|
|
("up", ["9.1K", "-9.0K", "9.1K", "-9.0K"]),
|
|
|
|
("down", ["9.0K", "-9.1K", "9.0K", "-9.1K"]),
|
|
|
|
("nearest", ["9.0K", "-9.0K", "9.1K", "-9.1K"]),
|
|
|
|
] {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&[
|
|
|
|
"--to=si",
|
|
|
|
&format!("--round={}", method),
|
|
|
|
"--",
|
|
|
|
"9001",
|
|
|
|
"-9001",
|
|
|
|
"9099",
|
|
|
|
"-9099",
|
|
|
|
])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only(exp.join("\n") + "\n");
|
|
|
|
}
|
|
|
|
}
|
2022-01-01 20:44:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_suffix_is_added_if_not_supplied() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--suffix=TEST"])
|
|
|
|
.pipe_in("1000")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1000TEST\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_suffix_is_preserved() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--suffix=TEST"])
|
|
|
|
.pipe_in("1000TEST")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1000TEST\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_suffix_is_only_applied_to_selected_field() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--suffix=TEST", "--field=2"])
|
|
|
|
.pipe_in("1000 2000 3000")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("1000 2000TEST 3000\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transform_with_suffix_on_input() {
|
|
|
|
new_ucmd!()
|
2022-01-01 21:55:50 +00:00
|
|
|
.args(&["--suffix=b", "--to=si"])
|
|
|
|
.pipe_in("2000b")
|
2022-01-01 20:44:11 +00:00
|
|
|
.succeeds()
|
2022-01-01 21:55:50 +00:00
|
|
|
.stdout_only("2.0Kb\n");
|
2022-01-01 20:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transform_without_suffix_on_input() {
|
|
|
|
new_ucmd!()
|
2022-01-01 21:55:50 +00:00
|
|
|
.args(&["--suffix=b", "--to=si"])
|
2022-01-01 20:44:11 +00:00
|
|
|
.pipe_in("2000")
|
|
|
|
.succeeds()
|
2022-01-01 21:55:50 +00:00
|
|
|
.stdout_only("2.0Kb\n");
|
2022-01-01 20:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transform_with_suffix_and_delimiter() {
|
|
|
|
new_ucmd!()
|
2022-01-01 21:55:50 +00:00
|
|
|
.args(&["--suffix=b", "--to=si", "-d=|"])
|
|
|
|
.pipe_in("1000b|2000|3000")
|
2022-01-01 20:44:11 +00:00
|
|
|
.succeeds()
|
2022-01-01 21:55:50 +00:00
|
|
|
.stdout_only("1.0Kb|2000|3000\n");
|
2022-01-01 20:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_suffix_with_padding() {
|
|
|
|
new_ucmd!()
|
2022-01-01 21:55:50 +00:00
|
|
|
.args(&["--suffix=pad", "--padding=12"])
|
2022-01-01 20:44:11 +00:00
|
|
|
.pipe_in("1000 2000 3000")
|
|
|
|
.succeeds()
|
2022-01-01 21:55:50 +00:00
|
|
|
.stdout_only(" 1000pad 2000 3000\n");
|
2022-01-01 20:44:11 +00:00
|
|
|
}
|
2022-01-04 02:15:35 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-01-15 10:42:48 +00:00
|
|
|
fn test_invalid_stdin_number_returns_status_2() {
|
2022-01-04 02:15:35 +00:00
|
|
|
new_ucmd!().pipe_in("hello").fails().code_is(2);
|
|
|
|
}
|
2022-01-15 10:42:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_stdin_number_in_middle_of_input() {
|
|
|
|
new_ucmd!().pipe_in("100\nhello\n200").fails().code_is(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_argument_number_returns_status_2() {
|
|
|
|
new_ucmd!().args(&["hello"]).fails().code_is(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_argument_returns_status_1() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--header=hello"])
|
|
|
|
.pipe_in("53478")
|
2022-01-15 19:50:49 +00:00
|
|
|
.ignore_stdin_write_error()
|
2022-01-15 10:42:48 +00:00
|
|
|
.fails()
|
|
|
|
.code_is(1);
|
|
|
|
}
|
2022-07-01 11:34:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_padding_value() {
|
|
|
|
let padding_values = vec!["A", "0"];
|
|
|
|
|
|
|
|
for padding_value in padding_values {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("--padding={}", padding_value))
|
|
|
|
.arg("5")
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains(format!("invalid padding value '{}'", padding_value));
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 08:25:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_unit() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--from-unit=512", "4"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("2048\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_unit() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--to-unit=512", "2048"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("4\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_unit_size() {
|
|
|
|
let commands = vec!["from", "to"];
|
|
|
|
let invalid_sizes = vec!["A", "0", "18446744073709551616"];
|
|
|
|
|
|
|
|
for command in commands {
|
|
|
|
for invalid_size in &invalid_sizes {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("--{}-unit={}", command, invalid_size))
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains(format!("invalid unit size: '{}'", invalid_size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 13:05:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_valid_but_forbidden_suffix() {
|
|
|
|
let numbers = vec!["12K", "12Ki"];
|
|
|
|
|
|
|
|
for number in numbers {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(number)
|
|
|
|
.fails()
|
|
|
|
.code_is(2)
|
|
|
|
.stderr_contains(format!(
|
|
|
|
"rejecting suffix in input: '{}' (consider using --from)",
|
|
|
|
number
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2022-07-03 14:36:43 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=--%f--", "50"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("--50--\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_separate_value() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format", "--%f--", "50"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("--50--\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_padding_with_prefix_and_suffix() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=--%6f--", "50"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("-- 50--\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_negative_padding_with_prefix_and_suffix() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=--%-6f--", "50"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("--50 --\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_format_padding_overriding_padding_option() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%6f", "--padding=10", "1234"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(" 1234\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_format_padding_overriding_implicit_padding() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%6f", " 1234"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(" 1234\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_negative_format_padding_and_suffix() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%-6f", "1234 ?"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("1234 ?\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_zero_padding() {
|
|
|
|
let formats = vec!["%06f", "%0 6f"];
|
|
|
|
|
|
|
|
for format in formats {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&[format!("--format={}", format), String::from("1234")])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("001234\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_zero_padding_and_padding_option() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%06f", "--padding=8", "1234"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(" 001234\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_zero_padding_and_negative_padding_option() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%06f", "--padding=-8", "1234"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("001234 \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_zero_padding_and_implicit_padding() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%06f", " 1234"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(" 001234\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_zero_padding_and_suffix() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%06f", "1234 ?"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("001234 ?\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_precision() {
|
|
|
|
let values = vec![("0.99", "1.0"), ("1", "1.0"), ("1.01", "1.1")];
|
|
|
|
|
|
|
|
for (input, expected) in values {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%.1f", input])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(format!("{}\n", expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
let values = vec![("0.99", "0.99"), ("1", "1.00"), ("1.01", "1.01")];
|
|
|
|
|
|
|
|
for (input, expected) in values {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%.2f", input])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(format!("{}\n", expected));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_precision_and_down_rounding() {
|
|
|
|
let values = vec![("0.99", "0.9"), ("1", "1.0"), ("1.01", "1.0")];
|
|
|
|
|
|
|
|
for (input, expected) in values {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%.1f", input, "--round=down"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(format!("{}\n", expected));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_precision_and_to_arg() {
|
|
|
|
let values = vec![("%.1f", "10.0G"), ("%.4f", "9.9913G")];
|
|
|
|
|
|
|
|
for (format, expected) in values {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&[
|
|
|
|
format!("--format={}", format),
|
|
|
|
"9991239123".to_string(),
|
|
|
|
"--to=si".to_string(),
|
|
|
|
])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(format!("{}\n", expected));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_without_percentage_directive() {
|
|
|
|
let invalid_formats = vec!["", "hello"];
|
|
|
|
|
|
|
|
for invalid_format in invalid_formats {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("--format={}", invalid_format))
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains(format!("format '{}' has no % directive", invalid_format));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_percentage_directive_at_end() {
|
|
|
|
let invalid_format = "hello%";
|
|
|
|
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("--format={}", invalid_format))
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains(format!("format '{}' ends in %", invalid_format));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_too_many_percentage_directives() {
|
|
|
|
let invalid_format = "%f %f";
|
|
|
|
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("--format={}", invalid_format))
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains(format!(
|
|
|
|
"format '{}' has too many % directives",
|
|
|
|
invalid_format
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_invalid_format() {
|
|
|
|
let invalid_formats = vec!["%d", "% -43 f"];
|
|
|
|
|
|
|
|
for invalid_format in invalid_formats {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("--format={}", invalid_format))
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains(format!(
|
|
|
|
"invalid format '{}', directive must be %[0]['][-][N][.][N]f",
|
|
|
|
invalid_format
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_width_overflow() {
|
|
|
|
let invalid_format = "%18446744073709551616f";
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("--format={}", invalid_format))
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains(format!(
|
|
|
|
"invalid format '{}' (width overflow)",
|
|
|
|
invalid_format
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_with_invalid_precision() {
|
|
|
|
let invalid_formats = vec!["%.-1f", "%.+1f", "%. 1f", "%.18446744073709551616f"];
|
|
|
|
|
|
|
|
for invalid_format in invalid_formats {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("--format={}", invalid_format))
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains(format!("invalid precision in format '{}'", invalid_format));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_grouping_conflicts_with_to_option() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["--format=%'f", "--to=si"])
|
|
|
|
.fails()
|
|
|
|
.code_is(1)
|
|
|
|
.stderr_contains("grouping cannot be combined with --to");
|
|
|
|
}
|