1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Add tests for cat and fix a couple issues

This commit is contained in:
Jordi Boggiano 2013-10-22 15:31:51 +02:00
parent 61a232eb0a
commit 13fd72e1f2
5 changed files with 95 additions and 4 deletions

View file

@ -9,4 +9,11 @@ build:
sh -c 'rustc --out-dir build/ cat/cat.rs'
sh -c 'rustc --out-dir build/ whoami/whoami.rs'
test:
rm -rf tmp
mkdir tmp
sh -c 'rustc -o tmp/cat_test cat/test.rs'
sh -c 'tmp/cat_test'
rm -rf tmp
.PHONY: build

View file

@ -102,21 +102,21 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
let writer = stdout();
if NumberNone != number || show_nonprint || show_ends || show_tabs || squeeze_blank {
let mut counter: uint = 1;
let is_numbering = number == NumberAll || number == NumberNonEmpty;
for path in files.iter() {
let reader = match open(path.to_owned()) {
Some(f) => f,
None => { continue }
};
let mut counter: uint = 1;
let mut at_line_start = true;
let is_numbering = number == NumberAll || number == NumberNonEmpty;
loop {
let buf = reader.read_bytes(2);
for byte in buf.iter() {
if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) {
writer.write_str(format!("{0:6u} ", counter));
writer.write_str(format!("{0:6u}\t", counter));
counter += 1;
at_line_start = false;
}

BIN
cat/fixtures/256.txt Normal file

Binary file not shown.

5
cat/fixtures/alpha.txt Normal file
View file

@ -0,0 +1,5 @@
abcde
fghij
klmno
pqrst
uvwxyz

79
cat/test.rs Normal file
View file

@ -0,0 +1,79 @@
use std::rt::io::process::{Process, ProcessConfig, CreatePipe, Ignored};
use std::rt::io::{Reader, Writer};
use std::rt::io::pipe::PipeStream;
use std::str;
fn main() {
test_output_multi_files_print_all_chars();
test_stdin_squeeze();
test_stdin_number_non_blank();
}
fn read_all(input: &mut Reader) -> ~str {
let mut ret = ~"";
let mut buf = [0, ..1024];
loop {
match input.read(buf) {
None => { break }
Some(n) => { ret = ret + str::from_utf8(buf.slice_to(n)); }
}
}
return ret;
}
fn test_output_multi_files_print_all_chars() {
let output = PipeStream::new().unwrap();
let io = ~[Ignored,
CreatePipe(output, false, true)];
let args = ProcessConfig {
program: "build/cat",
args: [~"cat/fixtures/alpha.txt", ~"cat/fixtures/256.txt", ~"-A", ~"-n"],
env: None,
cwd: None,
io: io,
};
let mut p = Process::new(args).expect("proc fail");
let out = read_all(p.io[1].get_mut_ref() as &mut Reader);
assert_eq!(p.wait(), 0);
assert_eq!(out, ~" 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^VM-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:M-;M-<M-=M->M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?");
}
fn test_stdin_squeeze() {
let input = PipeStream::new().unwrap();
let output = PipeStream::new().unwrap();
let io = ~[CreatePipe(input, true, false),
CreatePipe(output, false, true)];
let args = ProcessConfig {
program: "build/cat",
args: [~"-A"],
env: None,
cwd: None,
io: io,
};
let mut p = Process::new(args).expect("proc fail");
p.io[0].get_mut_ref().write("\x00\x01\x02".as_bytes());
p.io[0] = None; // close stdin;
let out = read_all(p.io[1].get_mut_ref() as &mut Reader);
assert_eq!(p.wait(), 0);
assert_eq!(out, ~"^@^A^B");
}
fn test_stdin_number_non_blank() {
let input = PipeStream::new().unwrap();
let output = PipeStream::new().unwrap();
let io = ~[CreatePipe(input, true, false),
CreatePipe(output, false, true)];
let args = ProcessConfig {
program: "build/cat",
args: [~"-b", ~"-"],
env: None,
cwd: None,
io: io,
};
let mut p = Process::new(args).expect("proc fail");
p.io[0].get_mut_ref().write("\na\nb\n\n\nc".as_bytes());
p.io[0] = None; // close stdin;
let out = read_all(p.io[1].get_mut_ref() as &mut Reader);
assert_eq!(p.wait(), 0);
assert_eq!(out, ~"\n 1\ta\n 2\tb\n\n\n 3\tc");
}