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

tail: Clean up and test suffix multiplier

Makes `parse_size` return a `Result` where the `Err` part indicates whether
there was a parsing error, or the parse size is too big to store. Also makes the
value parsed a `u64` rather than a `usize`.

Adds unit tests for `parse_size` and integration tests using the suffix
multiplier in a number passed with the `-n` flag.
This commit is contained in:
Nick Fitzgerald 2016-04-02 14:16:11 -07:00
parent ed6c87b3d3
commit 3972c6eb53
2 changed files with 137 additions and 38 deletions

View file

@ -1,3 +1,6 @@
extern crate uu_tail;
use uu_tail::parse_size;
use std::io::Write;
#[macro_use]
@ -108,3 +111,63 @@ fn test_bytes_big() {
assert_eq!(actual_char, expected_char);
}
}
#[test]
fn test_parse_size() {
// No suffix.
assert_eq!(Ok(1234), parse_size("1234"));
// kB is 1000
assert_eq!(Ok(9 * 1000), parse_size("9kB"));
// K is 1024
assert_eq!(Ok(2 * 1024), parse_size("2K"));
let suffixes = [
('M', 2u32),
('G', 3u32),
('T', 4u32),
('P', 5u32),
('E', 6u32),
];
for &(c, exp) in &suffixes {
let s = format!("2{}B", c);
assert_eq!(Ok(2 * (1000 as u64).pow(exp)), parse_size(&s));
let s = format!("2{}", c);
assert_eq!(Ok(2 * (1024 as u64).pow(exp)), parse_size(&s));
}
// Sizes that are too big.
assert!(parse_size("1Z").is_err());
assert!(parse_size("1Y").is_err());
// Bad number
assert!(parse_size("328hdsf3290").is_err());
}
#[test]
fn test_lines_with_size_suffix() {
const FILE: &'static str = "test_lines_with_size_suffix.txt";
const EXPECTED_FILE: &'static str = "test_lines_with_size_suffix_expected.txt";
const LINES: usize = 3_000;
const N_ARG: usize = 2 * 1024;
let (at, mut ucmd) = testing(UTIL_NAME);
let mut big_input = at.make_scoped_file(FILE);
for i in 0..LINES {
writeln!(&mut big_input, "Line {}", i).expect("Could not write to FILE");
}
big_input.flush().expect("Could not flush FILE");
let mut big_expected = at.make_scoped_file(EXPECTED_FILE);
for i in (LINES - N_ARG)..LINES {
writeln!(&mut big_expected, "Line {}", i).expect("Could not write to EXPECTED_FILE");
}
big_expected.flush().expect("Could not flush EXPECTED_FILE");
let result = ucmd.arg(FILE).arg("-n").arg("2K").run();
assert_eq!(result.stdout, at.read(EXPECTED_FILE));
}