Skip to content

Commit

Permalink
pseudo: improve overflow message; add overflow test
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Jan 13, 2024
1 parent ed59d80 commit d971f1e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/cmd/pseudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
});
if overflowed {
return fail_incorrectusage_clierror!(
"Overflowed. The increment value is too large. The maximum value for \
--increment is {}.",
u64::MAX - curr_counter
"Overflowed. The counter is larger than u64::MAX {}. The last valid counter \
is {curr_counter}.",
u64::MAX
);
}
record = replace_column_value(&record, column_index, &new_value.to_string());
Expand Down Expand Up @@ -175,9 +175,9 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
});
if overflowed {
return fail_incorrectusage_clierror!(
"Overflowed. The increment value is too large. The maximum value for \
--increment is {}.",
u64::MAX - curr_counter
"Overflowed. The counter is larger than u64::MAX({}). The last valid counter \
is {curr_counter}.",
u64::MAX
);
}

Expand Down
41 changes: 41 additions & 0 deletions tests/test_pseudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,44 @@ fn pseudo_formatstr_start_increment() {
];
assert_eq!(got, expected);
}

#[test]
fn pseudo_overflow() {
let wrk = Workdir::new("pseudo_overflow");
wrk.create(
"data.csv",
vec![
svec!["name", "colors"],
svec!["Mary", "yellow"],
svec!["John", "blue"],
svec!["Mary", "purple"],
svec!["Sue", "orange"],
svec!["John", "magenta"],
svec!["Mary", "cyan"],
],
);
let close_to_max = std::u64::MAX - 10;
let mut cmd = wrk.command("pseudo");
cmd.arg("name")
.args(["--formatstr", "ID-{}"])
.args(["--start", close_to_max.to_string().as_str()])
.args(["--increment", "5"])
.arg("data.csv");

wrk.assert_err(&mut cmd);
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["name", "colors"],
svec!["ID-18446744073709551605", "yellow"],
svec!["ID-18446744073709551610", "blue"],
svec!["ID-18446744073709551605", "purple"],
];
assert_eq!(got, expected);

let got_err = wrk.output_stderr(&mut cmd);
assert_eq!(
got_err,
"usage error: Overflowed. The counter is larger than u64::MAX(18446744073709551615). The \
last valid counter is 18446744073709551615.\n"
);
}

0 comments on commit d971f1e

Please sign in to comment.