Test the buffered reader and writer in _io.

This commit is contained in:
Roy Frostig 2010-08-20 12:57:38 -07:00
parent af64e4f305
commit 2da4fecacd
3 changed files with 61 additions and 4 deletions

View File

@ -84,7 +84,18 @@ fn new_buf_reader(str path) -> buf_reader {
ret fd_buf_reader(fd, new_buf()); ret fd_buf_reader(fd, new_buf());
} }
type fileflag = tag(append(), create(), truncate()); /**
* FIXME (issue #150): This should be
*
* type fileflag = tag(append(), create(), truncate());
*
* but then the tag value ctors are not found from crate-importers of std, so
* we manually simulate the enum below.
*/
type fileflag = uint;
fn append() -> uint { ret 0u; }
fn create() -> uint { ret 1u; }
fn truncate() -> uint { ret 2u; }
fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer { fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
@ -117,9 +128,13 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
for (fileflag f in flags) { for (fileflag f in flags) {
alt (f) { alt (f) {
case (append()) { fflags |= os.libc_constants.O_APPEND(); } // FIXME (issue #150): cf comment above defn of fileflag type
case (create()) { fflags |= os.libc_constants.O_CREAT(); } //case (append()) { fflags |= os.libc_constants.O_APPEND(); }
case (truncate()) { fflags |= os.libc_constants.O_TRUNC(); } //case (create()) { fflags |= os.libc_constants.O_CREAT(); }
//case (truncate()) { fflags |= os.libc_constants.O_TRUNC(); }
case (0u) { fflags |= os.libc_constants.O_APPEND(); }
case (1u) { fflags |= os.libc_constants.O_CREAT(); }
case (2u) { fflags |= os.libc_constants.O_TRUNC(); }
} }
} }

View File

@ -11,6 +11,22 @@ native "rust" mod rustrt {
fn refcount[T](str s) -> uint; fn refcount[T](str s) -> uint;
} }
fn eq(str a, str b) -> bool {
let uint i = byte_len(a);
if (byte_len(b) != i) {
ret false;
}
while (i > 0u) {
i -= 1u;
auto cha = a.(i);
auto chb = b.(i);
if (cha != chb) {
ret false;
}
}
ret true;
}
fn is_utf8(vec[u8] v) -> bool { fn is_utf8(vec[u8] v) -> bool {
fail; // FIXME fail; // FIXME
} }

View File

@ -0,0 +1,26 @@
// -*- rust -*-
use std;
import std._io;
import std._str;
fn test_simple(str tmpfilebase) {
let str tmpfile = tmpfilebase + ".tmp";
log tmpfile;
let str frood = "A hoopy frood who really knows where his towel is.";
log frood;
{
let _io.buf_writer out = _io.new_buf_writer(tmpfile, vec(_io.create()));
out.write(_str.bytes(frood));
}
let _io.buf_reader inp = _io.new_buf_reader(tmpfile);
let str frood2 = _str.from_bytes(inp.read());
log frood2;
check (_str.eq(frood, frood2));
}
fn main(vec[str] argv) {
test_simple(argv.(0));
}