add tests for io::readchars and io::readchar
Additionally reformat so that 'make check' passes.
This commit is contained in:
parent
bcc25634e6
commit
ba694775f5
2 changed files with 58 additions and 7 deletions
|
@ -130,26 +130,29 @@ obj new_reader(rdr: buf_reader) {
|
||||||
val += next & 63 as uint;
|
val += next & 63 as uint;
|
||||||
}
|
}
|
||||||
// See str::char_at
|
// 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 ];
|
chars += [ val as char ];
|
||||||
}
|
}
|
||||||
ret (i, 0u);
|
ret (i, 0u);
|
||||||
}
|
}
|
||||||
let buf: [u8] = [];
|
let buf: [u8] = [];
|
||||||
let chars: [char] = [];
|
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 {
|
while nbread > 0u {
|
||||||
let data = self.read_bytes(nbread);
|
let data = self.read_bytes(nbread);
|
||||||
if vec::len(data) == 0u {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
buf += data;
|
buf += data;
|
||||||
let (offset, nbreq) = chars_from_buf(buf, chars);
|
let (offset, nbreq) = chars_from_buf(buf, chars);
|
||||||
let ncreq = n - vec::len(chars);
|
let ncreq = n - vec::len(chars);
|
||||||
// again we either know we need a certain number of bytes to complete a
|
// again we either know we need a certain number of bytes
|
||||||
// character, or we make sure we don't over-read by reading 1-byte per char
|
// to complete a character, or we make sure we don't
|
||||||
// needed
|
// over-read by reading 1-byte per char needed
|
||||||
nbread = if ncreq > nbreq { ncreq } else { nbreq };
|
nbread = if ncreq > nbreq { ncreq } else { nbreq };
|
||||||
if nbread > 0u {
|
if nbread > 0u {
|
||||||
buf = vec::slice(buf, offset, vec::len(buf));
|
buf = vec::slice(buf, offset, vec::len(buf));
|
||||||
|
|
|
@ -23,6 +23,54 @@ fn test_simple() {
|
||||||
assert (str::eq(frood, frood2));
|
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]
|
#[test]
|
||||||
fn file_reader_not_exist() {
|
fn file_reader_not_exist() {
|
||||||
alt io::file_reader("not a file") {
|
alt io::file_reader("not a file") {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue