From 14b1313eaf49b55bded69a3f4c470e652c3adb2f Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sat, 28 Nov 2015 20:16:19 +0100 Subject: [PATCH 1/3] comm: fix EOF detection --- src/comm/comm.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/comm/comm.rs b/src/comm/comm.rs index ee37afd94..84b8ddb55 100644 --- a/src/comm/comm.rs +++ b/src/comm/comm.rs @@ -72,6 +72,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { (false, true) => Ordering::Greater, (true , false) => Ordering::Less, (true , true) => match(&na, &nb) { + (&Ok(0), &Ok(0)) => break, (&Ok(0), _) => Ordering::Greater, (_, &Ok(0)) => Ordering::Less, _ => ra.cmp(&rb), From 8a6768e9bdea59c86662baa29a62989663d7cc05 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sat, 28 Nov 2015 20:35:43 +0100 Subject: [PATCH 2/3] comm: clear buffers between read_line calls --- src/comm/comm.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/comm/comm.rs b/src/comm/comm.rs index 84b8ddb55..75528942b 100644 --- a/src/comm/comm.rs +++ b/src/comm/comm.rs @@ -86,6 +86,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { ensure_nl(ra); print!("{}{}", delim[1], ra); } + ra.clear(); na = a.read_line(ra); }, Ordering::Greater => { @@ -93,6 +94,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { ensure_nl(rb); print!("{}{}", delim[2], rb); } + rb.clear(); nb = b.read_line(rb); }, Ordering::Equal => { @@ -100,6 +102,8 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { ensure_nl(ra); print!("{}{}", delim[3], ra); } + ra.clear(); + rb.clear(); na = a.read_line(ra); nb = b.read_line(rb); } From 2f4ae615edef2c6853a7f088467eb029ccd26642 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sat, 28 Nov 2015 20:36:29 +0100 Subject: [PATCH 3/3] comm: add basic tests --- Makefile | 1 + tests/comm.rs | 47 +++++++++++++++++++++++++ tests/fixtures/comm/a | 2 ++ tests/fixtures/comm/ab.expected | 3 ++ tests/fixtures/comm/ab1.expected | 2 ++ tests/fixtures/comm/ab2.expected | 2 ++ tests/fixtures/comm/ab3.expected | 2 ++ tests/fixtures/comm/aempty.expected | 2 ++ tests/fixtures/comm/b | 2 ++ tests/fixtures/comm/empty | 0 tests/fixtures/comm/emptyempty.expected | 0 11 files changed, 63 insertions(+) create mode 100644 tests/comm.rs create mode 100644 tests/fixtures/comm/a create mode 100644 tests/fixtures/comm/ab.expected create mode 100644 tests/fixtures/comm/ab1.expected create mode 100644 tests/fixtures/comm/ab2.expected create mode 100644 tests/fixtures/comm/ab3.expected create mode 100644 tests/fixtures/comm/aempty.expected create mode 100644 tests/fixtures/comm/b create mode 100644 tests/fixtures/comm/empty create mode 100644 tests/fixtures/comm/emptyempty.expected diff --git a/Makefile b/Makefile index 958828545..68979e6b2 100644 --- a/Makefile +++ b/Makefile @@ -114,6 +114,7 @@ TEST_PROGS := \ basename \ cat \ cksum \ + comm \ cp \ cut \ dirname \ diff --git a/tests/comm.rs b/tests/comm.rs new file mode 100644 index 000000000..390d50b89 --- /dev/null +++ b/tests/comm.rs @@ -0,0 +1,47 @@ +#[macro_use] mod common; + +use common::util; + +static UTIL_NAME: &'static str = "comm"; + +#[test] +fn test_comm_ab_no_args() { + let (at, mut ucmd) = util::testing(UTIL_NAME); + let result = ucmd.args(&["a", "b"]).run(); + assert_eq!(result.stdout, at.read("ab.expected")); +} + +#[test] +fn test_comm_ab_dash_one() { + let (at, mut ucmd) = util::testing(UTIL_NAME); + let result = ucmd.args(&["a", "b", "-1"]).run(); + assert_eq!(result.stdout, at.read("ab1.expected")); +} + +#[test] +fn test_comm_ab_dash_two() { + let (at, mut ucmd) = util::testing(UTIL_NAME); + let result = ucmd.args(&["a", "b", "-2"]).run(); + assert_eq!(result.stdout, at.read("ab2.expected")); +} + +#[test] +fn test_comm_ab_dash_three() { + let (at, mut ucmd) = util::testing(UTIL_NAME); + let result = ucmd.args(&["a", "b", "-3"]).run(); + assert_eq!(result.stdout, at.read("ab3.expected")); +} + +#[test] +fn test_comm_aempty() { + let (at, mut ucmd) = util::testing(UTIL_NAME); + let result = ucmd.args(&["a", "empty"]).run(); + assert_eq!(result.stdout, at.read("aempty.expected")); +} + +#[test] +fn test_comm_emptyempty() { + let (at, mut ucmd) = util::testing(UTIL_NAME); + let result = ucmd.args(&["empty", "empty"]).run(); + assert_eq!(result.stdout, at.read("emptyempty.expected")); +} diff --git a/tests/fixtures/comm/a b/tests/fixtures/comm/a new file mode 100644 index 000000000..58f424cdb --- /dev/null +++ b/tests/fixtures/comm/a @@ -0,0 +1,2 @@ +a +z diff --git a/tests/fixtures/comm/ab.expected b/tests/fixtures/comm/ab.expected new file mode 100644 index 000000000..0efac0666 --- /dev/null +++ b/tests/fixtures/comm/ab.expected @@ -0,0 +1,3 @@ +a + b + z diff --git a/tests/fixtures/comm/ab1.expected b/tests/fixtures/comm/ab1.expected new file mode 100644 index 000000000..7c81c8ba7 --- /dev/null +++ b/tests/fixtures/comm/ab1.expected @@ -0,0 +1,2 @@ +b + z diff --git a/tests/fixtures/comm/ab2.expected b/tests/fixtures/comm/ab2.expected new file mode 100644 index 000000000..c98cd0da6 --- /dev/null +++ b/tests/fixtures/comm/ab2.expected @@ -0,0 +1,2 @@ +a + z diff --git a/tests/fixtures/comm/ab3.expected b/tests/fixtures/comm/ab3.expected new file mode 100644 index 000000000..1898bc804 --- /dev/null +++ b/tests/fixtures/comm/ab3.expected @@ -0,0 +1,2 @@ +a + b diff --git a/tests/fixtures/comm/aempty.expected b/tests/fixtures/comm/aempty.expected new file mode 100644 index 000000000..58f424cdb --- /dev/null +++ b/tests/fixtures/comm/aempty.expected @@ -0,0 +1,2 @@ +a +z diff --git a/tests/fixtures/comm/b b/tests/fixtures/comm/b new file mode 100644 index 000000000..63d7d7058 --- /dev/null +++ b/tests/fixtures/comm/b @@ -0,0 +1,2 @@ +b +z diff --git a/tests/fixtures/comm/empty b/tests/fixtures/comm/empty new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/comm/emptyempty.expected b/tests/fixtures/comm/emptyempty.expected new file mode 100644 index 000000000..e69de29bb