Skip to content

Commit

Permalink
add tests for io::readchars and io::readchar
Browse files Browse the repository at this point in the history
Additionally reformat so that 'make check' passes.
  • Loading branch information
Grahame Bowland committed Jan 9, 2012
1 parent bcc2563 commit ba69477
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/libstd/io.rs
Expand Up @@ -130,26 +130,29 @@ obj new_reader(rdr: buf_reader) {
val += next & 63 as uint;
}
// See str::char_at
val += (b0 << (w + 1u as u8) as uint) << (w - 1u) * 6u - w - 1u;
val += (b0 << (w + 1u as u8) as uint)
<< (w - 1u) * 6u - w - 1u;
chars += [ val as char ];
}
ret (i, 0u);
}
let buf: [u8] = [];
let chars: [char] = [];
let nbread = n; // might need more bytes, but reading n will never over-read
// might need more bytes, but reading n will never over-read
let nbread = n;
while nbread > 0u {
let data = self.read_bytes(nbread);
let data = self.read_bytes(nbread);
if vec::len(data) == 0u {
// eof - FIXME should we do something if we're split in a unicode char?
// eof - FIXME should we do something if
// we're split in a unicode char?
break;
}
buf += data;
let (offset, nbreq) = chars_from_buf(buf, chars);
let ncreq = n - vec::len(chars);
// again we either know we need a certain number of bytes to complete a
// character, or we make sure we don't over-read by reading 1-byte per char
// needed
// again we either know we need a certain number of bytes
// to complete a character, or we make sure we don't
// over-read by reading 1-byte per char needed
nbread = if ncreq > nbreq { ncreq } else { nbreq };
if nbread > 0u {
buf = vec::slice(buf, offset, vec::len(buf));
Expand Down
48 changes: 48 additions & 0 deletions src/test/stdtest/io.rs
Expand Up @@ -23,6 +23,54 @@ fn test_simple() {
assert (str::eq(frood, frood2));
}

#[test]
fn test_readchars_empty() {
let inp : io::reader = io::string_reader("");
let res : [char] = inp.read_chars(128u);
assert(vec::len(res) == 0u);
}

#[test]
fn test_readchars_wide() {
let wide_test = "生锈的汤匙切肉汤hello生锈的汤匙切肉汤";
let ivals : [int] = [
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748,
104, 101, 108, 108, 111,
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748];
fn check_read_ln(len : uint, s: str, ivals: [int]) {
let inp : io::reader = io::string_reader(s);
let res : [char] = inp.read_chars(len);
if (len <= vec::len(ivals)) {
assert(vec::len(res) == len);
}
assert(vec::slice(ivals, 0u, vec::len(res)) ==
vec::map(res, {|x| x as int}));
}
let i = 0u;
while i < 8u {
check_read_ln(i, wide_test, ivals);
i += 1u;
}
// check a long read for good measure
check_read_ln(128u, wide_test, ivals);
}

#[test]
fn test_readchar() {
let inp : io::reader = io::string_reader("生");
let res : char = inp.read_char();
assert(res as int == 29983);
}

#[test]
fn test_readchar_empty() {
let inp : io::reader = io::string_reader("");
let res : char = inp.read_char();
assert(res as int == -1);
}

#[test]
fn file_reader_not_exist() {
alt io::file_reader("not a file") {
Expand Down

0 comments on commit ba69477

Please sign in to comment.