From d7974c56a05542ba7b18c9ad6db170f2ebf6dff7 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Sat, 19 Mar 2016 22:25:53 -0700 Subject: [PATCH] tests/tail: Add a test for tail'ing large files This tests both large files and iterating backwards through the file when we need to search backwards further than our BUFFER_SIZE. --- tests/tail.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/tail.rs b/tests/tail.rs index 8b9dc94a3..b83f8848e 100644 --- a/tests/tail.rs +++ b/tests/tail.rs @@ -1,3 +1,5 @@ +use std::io::Write; + #[macro_use] mod common; @@ -7,6 +9,9 @@ static UTIL_NAME: &'static str = "tail"; static INPUT: &'static str = "foobar.txt"; +static BIG: &'static str = "big.txt"; + +static BIG_EXPECTED: &'static str = "big_single_big_args.expected"; #[test] fn test_stdin_default() { @@ -21,3 +26,34 @@ fn test_single_default() { let result = ucmd.arg(INPUT).run(); assert_eq!(result.stdout, at.read("foobar_single_default.expected")); } + +const BIG_LINES: usize = 1_000_000; +const BIG_N_ARG: usize = 100_000; + +fn generate_big_test_files(at: &AtPath) { + let mut big_input = at.make_file(BIG); + for i in 0..BIG_LINES { + write!(&mut big_input, "Line {}\n", i).expect("Could not write to BIG file"); + } + big_input.flush().expect("Could not flush BIG file"); + + let mut big_expected = at.make_file(BIG_EXPECTED); + for i in (BIG_LINES - BIG_N_ARG)..BIG_LINES { + write!(&mut big_expected, "Line {}\n", i).expect("Could not write to BIG_EXPECTED file"); + } + big_expected.flush().expect("Could not flush BIG_EXPECTED file"); +} + +fn cleanup_big_test_files(at: &AtPath) { + at.cleanup(BIG); + at.cleanup(BIG_EXPECTED); +} + +#[test] +fn test_single_big_args() { + let (at, mut ucmd) = testing(UTIL_NAME); + generate_big_test_files(&at); + let result = ucmd.arg(BIG).arg("-n").arg(format!("{}", BIG_N_ARG)).run(); + assert_eq!(result.stdout, at.read(BIG_EXPECTED)); + cleanup_big_test_files(&at); +}