From 2da4fecacd1b61e9e090e93c33394712fa7f066c Mon Sep 17 00:00:00 2001 From: Roy Frostig Date: Fri, 20 Aug 2010 12:57:38 -0700 Subject: [PATCH] Test the buffered reader and writer in _io. --- src/lib/_io.rs | 23 +++++++++++++++++++---- src/lib/_str.rs | 16 ++++++++++++++++ src/test/run-pass/lib-io.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 src/test/run-pass/lib-io.rs diff --git a/src/lib/_io.rs b/src/lib/_io.rs index 2fb225ddc02..0d968c5c358 100644 --- a/src/lib/_io.rs +++ b/src/lib/_io.rs @@ -84,7 +84,18 @@ fn new_buf_reader(str path) -> buf_reader { 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 { @@ -117,9 +128,13 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer { for (fileflag f in flags) { alt (f) { - case (append()) { fflags |= os.libc_constants.O_APPEND(); } - case (create()) { fflags |= os.libc_constants.O_CREAT(); } - case (truncate()) { fflags |= os.libc_constants.O_TRUNC(); } + // FIXME (issue #150): cf comment above defn of fileflag type + //case (append()) { fflags |= os.libc_constants.O_APPEND(); } + //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(); } } } diff --git a/src/lib/_str.rs b/src/lib/_str.rs index 807edf314bb..a29e1daac6b 100644 --- a/src/lib/_str.rs +++ b/src/lib/_str.rs @@ -11,6 +11,22 @@ native "rust" mod rustrt { 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 { fail; // FIXME } diff --git a/src/test/run-pass/lib-io.rs b/src/test/run-pass/lib-io.rs new file mode 100644 index 00000000000..663944356c5 --- /dev/null +++ b/src/test/run-pass/lib-io.rs @@ -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)); +}