2013-04-17 19:55:21 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Readers and Writers for in-memory buffers
|
2014-01-15 15:25:09 -06:00
|
|
|
|
2013-11-12 19:14:22 -06:00
|
|
|
use cmp::max;
|
2013-05-24 21:35:29 -05:00
|
|
|
use cmp::min;
|
2014-01-15 15:25:09 -06:00
|
|
|
use container::Container;
|
|
|
|
use option::{Option, Some, None};
|
|
|
|
use super::{Reader, Writer, Seek, Buffer, IoError, SeekStyle, io_error,
|
|
|
|
OtherIoError};
|
2013-05-24 21:35:29 -05:00
|
|
|
use vec;
|
2014-01-28 16:40:36 -06:00
|
|
|
use vec::{Vector, ImmutableVector, MutableVector, OwnedCloneableVector};
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
/// Writes to an owned, growable byte vector
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::io::MemWriter;
|
|
|
|
///
|
|
|
|
/// let mut w = MemWriter::new();
|
|
|
|
/// w.write([0, 1, 2]);
|
|
|
|
///
|
|
|
|
/// assert_eq!(w.unwrap(), ~[0, 1, 2]);
|
|
|
|
/// ```
|
2013-04-17 19:55:21 -05:00
|
|
|
pub struct MemWriter {
|
2013-10-21 19:11:42 -05:00
|
|
|
priv buf: ~[u8],
|
|
|
|
priv pos: uint,
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemWriter {
|
2013-11-30 07:58:27 -06:00
|
|
|
/// Create a new `MemWriter`.
|
2013-10-21 19:11:42 -05:00
|
|
|
pub fn new() -> MemWriter {
|
2013-11-30 07:58:27 -06:00
|
|
|
MemWriter::with_capacity(128)
|
|
|
|
}
|
|
|
|
/// Create a new `MemWriter`, allocating at least `n` bytes for
|
|
|
|
/// the internal buffer.
|
|
|
|
pub fn with_capacity(n: uint) -> MemWriter {
|
|
|
|
MemWriter { buf: vec::with_capacity(n), pos: 0 }
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Acquires an immutable reference to the underlying buffer of this
|
|
|
|
/// `MemWriter`.
|
|
|
|
///
|
|
|
|
/// No method is exposed for acquiring a mutable reference to the buffer
|
|
|
|
/// because it could corrupt the state of this `MemWriter`.
|
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a [u8] { self.buf.as_slice() }
|
|
|
|
|
|
|
|
/// Unwraps this `MemWriter`, returning the underlying buffer
|
|
|
|
pub fn unwrap(self) -> ~[u8] { self.buf }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for MemWriter {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn write(&mut self, buf: &[u8]) {
|
2013-10-21 19:11:42 -05:00
|
|
|
// Make sure the internal buffer is as least as big as where we
|
|
|
|
// currently are
|
|
|
|
let difference = self.pos as i64 - self.buf.len() as i64;
|
|
|
|
if difference > 0 {
|
|
|
|
self.buf.grow(difference as uint, &0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out what bytes will be used to overwrite what's currently
|
|
|
|
// there (left), and what will be appended on the end (right)
|
|
|
|
let cap = self.buf.len() - self.pos;
|
|
|
|
let (left, right) = if cap <= buf.len() {
|
|
|
|
(buf.slice_to(cap), buf.slice_from(cap))
|
|
|
|
} else {
|
|
|
|
(buf, &[])
|
|
|
|
};
|
|
|
|
|
|
|
|
// Do the necessary writes
|
|
|
|
if left.len() > 0 {
|
2013-12-15 05:23:11 -06:00
|
|
|
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), left);
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
|
|
|
if right.len() > 0 {
|
|
|
|
self.buf.push_all(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bump us forward
|
|
|
|
self.pos += buf.len();
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-11-12 19:14:22 -06:00
|
|
|
// FIXME(#10432)
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for MemWriter {
|
2013-10-21 19:11:42 -05:00
|
|
|
fn tell(&self) -> u64 { self.pos as u64 }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-21 19:11:42 -05:00
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) {
|
2013-11-12 19:14:22 -06:00
|
|
|
// compute offset as signed and clamp to prevent overflow
|
|
|
|
let offset = match style {
|
|
|
|
SeekSet => { 0 }
|
|
|
|
SeekEnd => { self.buf.len() }
|
|
|
|
SeekCur => { self.pos }
|
|
|
|
} as i64;
|
|
|
|
|
|
|
|
self.pos = max(0, offset+pos) as uint;
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
/// Reads from an owned byte vector
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::io::MemReader;
|
|
|
|
///
|
|
|
|
/// let mut r = MemReader::new(~[0, 1, 2]);
|
|
|
|
///
|
|
|
|
/// assert_eq!(r.read_to_end(), ~[0, 1, 2]);
|
|
|
|
/// ```
|
2013-04-17 19:55:21 -05:00
|
|
|
pub struct MemReader {
|
2013-09-26 23:22:22 -05:00
|
|
|
priv buf: ~[u8],
|
|
|
|
priv pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemReader {
|
2014-01-07 22:05:33 -06:00
|
|
|
/// Creates a new `MemReader` which will read the buffer given. The buffer
|
|
|
|
/// can be re-acquired through `unwrap`
|
2013-04-17 19:55:21 -05:00
|
|
|
pub fn new(buf: ~[u8]) -> MemReader {
|
|
|
|
MemReader {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
2014-01-09 23:03:18 -06:00
|
|
|
/// Tests whether this reader has read all bytes in its buffer.
|
|
|
|
///
|
|
|
|
/// If `true`, then this will no longer return bytes from `read`.
|
|
|
|
pub fn eof(&self) -> bool { self.pos == self.buf.len() }
|
|
|
|
|
2014-01-07 22:05:33 -06:00
|
|
|
/// Acquires an immutable reference to the underlying buffer of this
|
|
|
|
/// `MemReader`.
|
|
|
|
///
|
|
|
|
/// No method is exposed for acquiring a mutable reference to the buffer
|
|
|
|
/// because it could corrupt the state of this `MemReader`.
|
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a [u8] { self.buf.as_slice() }
|
|
|
|
|
|
|
|
/// Unwraps this `MemReader`, returning the underlying buffer
|
|
|
|
pub fn unwrap(self) -> ~[u8] { self.buf }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for MemReader {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
2014-01-09 23:03:18 -06:00
|
|
|
if self.eof() { return None }
|
2013-04-20 19:25:00 -05:00
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
|
|
|
let input = self.buf.slice(self.pos, self.pos + write_len);
|
2013-06-27 04:48:50 -05:00
|
|
|
let output = buf.mut_slice(0, write_len);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(input.len(), output.len());
|
2013-12-15 05:23:11 -06:00
|
|
|
vec::bytes::copy_memory(output, input);
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
self.pos += write_len;
|
|
|
|
assert!(self.pos <= self.buf.len());
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-04-20 19:25:00 -05:00
|
|
|
return Some(write_len);
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for MemReader {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn tell(&self) -> u64 { self.pos as u64 }
|
2013-10-21 15:08:31 -05:00
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl Buffer for MemReader {
|
|
|
|
fn fill<'a>(&'a mut self) -> &'a [u8] { self.buf.slice_from(self.pos) }
|
|
|
|
fn consume(&mut self, amt: uint) { self.pos += amt; }
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
/// Writes to a fixed-size byte slice
|
2013-11-12 19:14:22 -06:00
|
|
|
///
|
|
|
|
/// If a write will not fit in the buffer, it raises the `io_error`
|
|
|
|
/// condition and does not write any data.
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::io::BufWriter;
|
|
|
|
///
|
|
|
|
/// let mut buf = [0, ..4];
|
|
|
|
/// {
|
|
|
|
/// let mut w = BufWriter::new(buf);
|
|
|
|
/// w.write([0, 1, 2]);
|
|
|
|
/// }
|
|
|
|
/// assert_eq!(buf, [0, 1, 2, 0]);
|
|
|
|
/// ```
|
2013-12-10 01:16:18 -06:00
|
|
|
pub struct BufWriter<'a> {
|
|
|
|
priv buf: &'a mut [u8],
|
2013-09-26 23:22:22 -05:00
|
|
|
priv pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> BufWriter<'a> {
|
2013-04-17 19:55:21 -05:00
|
|
|
pub fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> {
|
|
|
|
BufWriter {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Writer for BufWriter<'a> {
|
2013-11-12 19:14:22 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) {
|
|
|
|
// raises a condition if the entire write does not fit in the buffer
|
|
|
|
let max_size = self.buf.len();
|
|
|
|
if self.pos >= max_size || (self.pos + buf.len()) > max_size {
|
|
|
|
io_error::cond.raise(IoError {
|
|
|
|
kind: OtherIoError,
|
|
|
|
desc: "Trying to write past end of buffer",
|
|
|
|
detail: None
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-12-15 05:23:11 -06:00
|
|
|
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos), buf);
|
2013-11-12 19:14:22 -06:00
|
|
|
self.pos += buf.len();
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-11-12 19:14:22 -06:00
|
|
|
// FIXME(#10432)
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Seek for BufWriter<'a> {
|
2013-11-12 19:14:22 -06:00
|
|
|
fn tell(&self) -> u64 { self.pos as u64 }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-11-12 19:14:22 -06:00
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) {
|
|
|
|
// compute offset as signed and clamp to prevent overflow
|
|
|
|
let offset = match style {
|
|
|
|
SeekSet => { 0 }
|
|
|
|
SeekEnd => { self.buf.len() }
|
|
|
|
SeekCur => { self.pos }
|
|
|
|
} as i64;
|
|
|
|
|
|
|
|
self.pos = max(0, offset+pos) as uint;
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
/// Reads from a fixed-size byte slice
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::io::BufReader;
|
|
|
|
///
|
|
|
|
/// let mut buf = [0, 1, 2, 3];
|
|
|
|
/// let mut r = BufReader::new(buf);
|
|
|
|
///
|
|
|
|
/// assert_eq!(r.read_to_end(), ~[0, 1, 2, 3]);
|
|
|
|
/// ```
|
2013-12-10 01:16:18 -06:00
|
|
|
pub struct BufReader<'a> {
|
|
|
|
priv buf: &'a [u8],
|
2013-09-26 23:22:22 -05:00
|
|
|
priv pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> BufReader<'a> {
|
2014-01-15 15:25:09 -06:00
|
|
|
/// Creates a new buffered reader which will read the specified buffer
|
2013-04-17 19:55:21 -05:00
|
|
|
pub fn new<'a>(buf: &'a [u8]) -> BufReader<'a> {
|
|
|
|
BufReader {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
2014-01-09 23:03:18 -06:00
|
|
|
|
|
|
|
/// Tests whether this reader has read all bytes in its buffer.
|
|
|
|
///
|
|
|
|
/// If `true`, then this will no longer return bytes from `read`.
|
|
|
|
pub fn eof(&self) -> bool { self.pos == self.buf.len() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Reader for BufReader<'a> {
|
2013-09-02 13:23:57 -05:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
2014-01-09 23:03:18 -06:00
|
|
|
if self.eof() { return None }
|
2013-09-02 13:23:57 -05:00
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
|
|
|
let input = self.buf.slice(self.pos, self.pos + write_len);
|
|
|
|
let output = buf.mut_slice(0, write_len);
|
|
|
|
assert_eq!(input.len(), output.len());
|
2013-12-15 05:23:11 -06:00
|
|
|
vec::bytes::copy_memory(output, input);
|
2013-09-02 13:23:57 -05:00
|
|
|
}
|
|
|
|
self.pos += write_len;
|
|
|
|
assert!(self.pos <= self.buf.len());
|
|
|
|
|
|
|
|
return Some(write_len);
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Seek for BufReader<'a> {
|
2013-09-02 13:23:57 -05:00
|
|
|
fn tell(&self) -> u64 { self.pos as u64 }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Buffer for BufReader<'a> {
|
2013-11-13 13:17:58 -06:00
|
|
|
fn fill<'a>(&'a mut self) -> &'a [u8] { self.buf.slice_from(self.pos) }
|
|
|
|
fn consume(&mut self, amt: uint) { self.pos += amt; }
|
|
|
|
}
|
|
|
|
|
2013-04-20 19:25:00 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
2013-11-11 00:46:32 -06:00
|
|
|
use io::*;
|
2013-04-20 19:25:00 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_writer() {
|
|
|
|
let mut writer = MemWriter::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(writer.tell(), 0);
|
2013-04-20 19:25:00 -05:00
|
|
|
writer.write([0]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(writer.tell(), 1);
|
2013-04-20 19:25:00 -05:00
|
|
|
writer.write([1, 2, 3]);
|
|
|
|
writer.write([4, 5, 6, 7]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(writer.tell(), 8);
|
2014-01-07 22:05:33 -06:00
|
|
|
assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7]);
|
2013-10-21 19:11:42 -05:00
|
|
|
|
|
|
|
writer.seek(0, SeekSet);
|
|
|
|
assert_eq!(writer.tell(), 0);
|
|
|
|
writer.write([3, 4]);
|
2014-01-07 22:05:33 -06:00
|
|
|
assert_eq!(writer.get_ref(), [3, 4, 2, 3, 4, 5, 6, 7]);
|
2013-10-21 19:11:42 -05:00
|
|
|
|
|
|
|
writer.seek(1, SeekCur);
|
|
|
|
writer.write([0, 1]);
|
2014-01-07 22:05:33 -06:00
|
|
|
assert_eq!(writer.get_ref(), [3, 4, 2, 0, 1, 5, 6, 7]);
|
2013-10-21 19:11:42 -05:00
|
|
|
|
|
|
|
writer.seek(-1, SeekEnd);
|
|
|
|
writer.write([1, 2]);
|
2014-01-07 22:05:33 -06:00
|
|
|
assert_eq!(writer.get_ref(), [3, 4, 2, 0, 1, 5, 6, 1, 2]);
|
2013-10-21 19:11:42 -05:00
|
|
|
|
|
|
|
writer.seek(1, SeekEnd);
|
|
|
|
writer.write([1]);
|
2014-01-07 22:05:33 -06:00
|
|
|
assert_eq!(writer.get_ref(), [3, 4, 2, 0, 1, 5, 6, 1, 2, 0, 1]);
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
|
2013-11-12 19:14:22 -06:00
|
|
|
#[test]
|
|
|
|
fn test_buf_writer() {
|
|
|
|
let mut buf = [0 as u8, ..8];
|
|
|
|
{
|
|
|
|
let mut writer = BufWriter::new(buf);
|
|
|
|
assert_eq!(writer.tell(), 0);
|
|
|
|
writer.write([0]);
|
|
|
|
assert_eq!(writer.tell(), 1);
|
|
|
|
writer.write([1, 2, 3]);
|
|
|
|
writer.write([4, 5, 6, 7]);
|
|
|
|
assert_eq!(writer.tell(), 8);
|
|
|
|
}
|
|
|
|
assert_eq!(buf, [0, 1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_seek() {
|
|
|
|
let mut buf = [0 as u8, ..8];
|
|
|
|
{
|
|
|
|
let mut writer = BufWriter::new(buf);
|
|
|
|
assert_eq!(writer.tell(), 0);
|
|
|
|
writer.write([1]);
|
|
|
|
assert_eq!(writer.tell(), 1);
|
|
|
|
|
|
|
|
writer.seek(2, SeekSet);
|
|
|
|
assert_eq!(writer.tell(), 2);
|
|
|
|
writer.write([2]);
|
|
|
|
assert_eq!(writer.tell(), 3);
|
|
|
|
|
|
|
|
writer.seek(-2, SeekCur);
|
|
|
|
assert_eq!(writer.tell(), 1);
|
|
|
|
writer.write([3]);
|
|
|
|
assert_eq!(writer.tell(), 2);
|
|
|
|
|
|
|
|
writer.seek(-1, SeekEnd);
|
|
|
|
assert_eq!(writer.tell(), 7);
|
|
|
|
writer.write([4]);
|
|
|
|
assert_eq!(writer.tell(), 8);
|
|
|
|
|
|
|
|
}
|
|
|
|
assert_eq!(buf, [1, 3, 2, 0, 0, 0, 0, 4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_error() {
|
|
|
|
let mut buf = [0 as u8, ..2];
|
|
|
|
let mut writer = BufWriter::new(buf);
|
|
|
|
writer.write([0]);
|
|
|
|
|
|
|
|
let mut called = false;
|
2013-11-21 19:23:21 -06:00
|
|
|
io_error::cond.trap(|err| {
|
2013-11-12 19:14:22 -06:00
|
|
|
assert_eq!(err.kind, OtherIoError);
|
|
|
|
called = true;
|
2013-11-21 19:23:21 -06:00
|
|
|
}).inside(|| {
|
2013-11-12 19:14:22 -06:00
|
|
|
writer.write([0, 0]);
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-11-12 19:14:22 -06:00
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
|
2013-04-20 19:25:00 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mem_reader() {
|
|
|
|
let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
let mut buf = [];
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(reader.read(buf), Some(0));
|
|
|
|
assert_eq!(reader.tell(), 0);
|
2013-04-20 19:25:00 -05:00
|
|
|
let mut buf = [0];
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(reader.read(buf), Some(1));
|
|
|
|
assert_eq!(reader.tell(), 1);
|
|
|
|
assert_eq!(buf, [0]);
|
2013-04-20 19:25:00 -05:00
|
|
|
let mut buf = [0, ..4];
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(reader.read(buf), Some(4));
|
|
|
|
assert_eq!(reader.tell(), 5);
|
|
|
|
assert_eq!(buf, [1, 2, 3, 4]);
|
|
|
|
assert_eq!(reader.read(buf), Some(3));
|
|
|
|
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
|
|
|
|
assert_eq!(reader.read(buf), None);
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
2013-09-02 13:23:57 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_reader() {
|
|
|
|
let in_buf = ~[0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
let mut reader = BufReader::new(in_buf);
|
|
|
|
let mut buf = [];
|
|
|
|
assert_eq!(reader.read(buf), Some(0));
|
|
|
|
assert_eq!(reader.tell(), 0);
|
|
|
|
let mut buf = [0];
|
|
|
|
assert_eq!(reader.read(buf), Some(1));
|
|
|
|
assert_eq!(reader.tell(), 1);
|
|
|
|
assert_eq!(buf, [0]);
|
|
|
|
let mut buf = [0, ..4];
|
|
|
|
assert_eq!(reader.read(buf), Some(4));
|
|
|
|
assert_eq!(reader.tell(), 5);
|
|
|
|
assert_eq!(buf, [1, 2, 3, 4]);
|
|
|
|
assert_eq!(reader.read(buf), Some(3));
|
|
|
|
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
|
|
|
|
assert_eq!(reader.read(buf), None);
|
|
|
|
}
|
2013-08-24 02:22:40 -05:00
|
|
|
|
2013-11-13 13:36:21 -06:00
|
|
|
#[test]
|
|
|
|
fn test_read_char() {
|
2014-01-15 13:39:08 -06:00
|
|
|
let b = bytes!("Việt");
|
|
|
|
let mut r = BufReader::new(b);
|
2013-11-13 13:36:21 -06:00
|
|
|
assert_eq!(r.read_char(), Some('V'));
|
|
|
|
assert_eq!(r.read_char(), Some('i'));
|
|
|
|
assert_eq!(r.read_char(), Some('ệ'));
|
|
|
|
assert_eq!(r.read_char(), Some('t'));
|
|
|
|
assert_eq!(r.read_char(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_bad_char() {
|
2014-01-15 13:39:08 -06:00
|
|
|
let b = bytes!(0x80);
|
|
|
|
let mut r = BufReader::new(b);
|
2013-11-13 13:36:21 -06:00
|
|
|
assert_eq!(r.read_char(), None);
|
|
|
|
}
|
2013-12-08 13:12:25 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_strings() {
|
|
|
|
let mut writer = MemWriter::new();
|
|
|
|
writer.write_str("testing");
|
|
|
|
writer.write_line("testing");
|
|
|
|
writer.write_str("testing");
|
2014-01-07 22:05:33 -06:00
|
|
|
let mut r = BufReader::new(writer.get_ref());
|
2013-12-08 13:12:25 -06:00
|
|
|
assert_eq!(r.read_to_str(), ~"testingtesting\ntesting");
|
|
|
|
}
|
|
|
|
|
2014-01-04 07:26:03 -06:00
|
|
|
#[test]
|
|
|
|
fn test_write_char() {
|
|
|
|
let mut writer = MemWriter::new();
|
|
|
|
writer.write_char('a');
|
|
|
|
writer.write_char('\n');
|
|
|
|
writer.write_char('ệ');
|
2014-01-07 22:05:33 -06:00
|
|
|
let mut r = BufReader::new(writer.get_ref());
|
2014-01-04 07:26:03 -06:00
|
|
|
assert_eq!(r.read_to_str(), ~"a\nệ");
|
|
|
|
}
|
|
|
|
|
2013-12-08 13:12:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_read_whole_string_bad() {
|
|
|
|
let buf = [0xff];
|
|
|
|
let mut r = BufReader::new(buf);
|
|
|
|
match result(|| r.read_to_str()) {
|
|
|
|
Ok(..) => fail!(),
|
|
|
|
Err(..) => {}
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|