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.
|
|
|
|
|
|
|
|
//! Utility mixins that apply to all Readers and Writers
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
// XXX: Not sure how this should be structured
|
2013-05-08 12:34:47 -05:00
|
|
|
// XXX: Iteration should probably be considered separately
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-09-08 10:01:16 -05:00
|
|
|
use iter::Iterator;
|
2013-10-25 20:08:45 -05:00
|
|
|
use option::Option;
|
2013-12-08 01:12:07 -06:00
|
|
|
use io::Reader;
|
2013-09-01 18:30:21 -05:00
|
|
|
|
|
|
|
/// An iterator that reads a single byte on each iteration,
|
2013-09-03 13:00:32 -05:00
|
|
|
/// until `.read_byte()` returns `None`.
|
|
|
|
///
|
|
|
|
/// # Notes about the Iteration Protocol
|
|
|
|
///
|
|
|
|
/// The `ByteIterator` may yield `None` and thus terminate
|
|
|
|
/// an iteration, but continue to yield elements if iteration
|
|
|
|
/// is attempted again.
|
2013-09-01 18:30:21 -05:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Raises the same conditions as the `read` method, for
|
|
|
|
/// each call to its `.next()` method.
|
2013-09-03 13:00:32 -05:00
|
|
|
/// Yields `None` if the condition is handled.
|
2013-12-08 01:12:07 -06:00
|
|
|
pub struct ByteIterator<'r, T> {
|
|
|
|
priv reader: &'r mut T,
|
2013-09-01 18:30:21 -05:00
|
|
|
}
|
|
|
|
|
2013-12-08 01:12:07 -06:00
|
|
|
impl<'r, R: Reader> ByteIterator<'r, R> {
|
|
|
|
pub fn new(r: &'r mut R) -> ByteIterator<'r, R> {
|
2013-10-25 20:08:45 -05:00
|
|
|
ByteIterator { reader: r }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 01:12:07 -06:00
|
|
|
impl<'r, R: Reader> Iterator<u8> for ByteIterator<'r, R> {
|
2013-09-01 18:30:21 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<u8> {
|
|
|
|
self.reader.read_byte()
|
|
|
|
}
|
2013-05-13 18:56:16 -05:00
|
|
|
}
|
|
|
|
|
2013-11-18 23:15:42 -06:00
|
|
|
pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
|
2013-10-22 01:06:12 -05:00
|
|
|
assert!(size <= 8u);
|
|
|
|
match size {
|
|
|
|
1u => f(&[n as u8]),
|
|
|
|
2u => f(&[n as u8,
|
|
|
|
(n >> 8) as u8]),
|
|
|
|
4u => f(&[n as u8,
|
|
|
|
(n >> 8) as u8,
|
|
|
|
(n >> 16) as u8,
|
|
|
|
(n >> 24) as u8]),
|
|
|
|
8u => f(&[n as u8,
|
|
|
|
(n >> 8) as u8,
|
|
|
|
(n >> 16) as u8,
|
|
|
|
(n >> 24) as u8,
|
|
|
|
(n >> 32) as u8,
|
|
|
|
(n >> 40) as u8,
|
|
|
|
(n >> 48) as u8,
|
|
|
|
(n >> 56) as u8]),
|
|
|
|
_ => {
|
|
|
|
|
|
|
|
let mut bytes: ~[u8] = ~[];
|
|
|
|
let mut i = size;
|
|
|
|
let mut n = n;
|
|
|
|
while i > 0u {
|
|
|
|
bytes.push((n & 255_u64) as u8);
|
|
|
|
n >>= 8_u64;
|
|
|
|
i -= 1u;
|
|
|
|
}
|
|
|
|
f(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:15:42 -06:00
|
|
|
pub fn u64_to_be_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
|
2013-10-22 01:06:12 -05:00
|
|
|
assert!(size <= 8u);
|
|
|
|
match size {
|
|
|
|
1u => f(&[n as u8]),
|
|
|
|
2u => f(&[(n >> 8) as u8,
|
|
|
|
n as u8]),
|
|
|
|
4u => f(&[(n >> 24) as u8,
|
|
|
|
(n >> 16) as u8,
|
|
|
|
(n >> 8) as u8,
|
|
|
|
n as u8]),
|
|
|
|
8u => f(&[(n >> 56) as u8,
|
|
|
|
(n >> 48) as u8,
|
|
|
|
(n >> 40) as u8,
|
|
|
|
(n >> 32) as u8,
|
|
|
|
(n >> 24) as u8,
|
|
|
|
(n >> 16) as u8,
|
|
|
|
(n >> 8) as u8,
|
|
|
|
n as u8]),
|
|
|
|
_ => {
|
|
|
|
let mut bytes: ~[u8] = ~[];
|
|
|
|
let mut i = size;
|
|
|
|
while i > 0u {
|
|
|
|
let shift = ((i - 1u) * 8u) as u64;
|
|
|
|
bytes.push((n >> shift) as u8);
|
|
|
|
i -= 1u;
|
|
|
|
}
|
|
|
|
f(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn u64_from_be_bytes(data: &[u8],
|
|
|
|
start: uint,
|
|
|
|
size: uint)
|
|
|
|
-> u64 {
|
|
|
|
let mut sz = size;
|
|
|
|
assert!((sz <= 8u));
|
|
|
|
let mut val = 0_u64;
|
|
|
|
let mut pos = start;
|
|
|
|
while sz > 0u {
|
|
|
|
sz -= 1u;
|
|
|
|
val += (data[pos] as u64) << ((sz * 8u) as u64);
|
|
|
|
pos += 1u;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2013-05-09 19:37:31 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2013-10-30 20:28:37 -05:00
|
|
|
use option::{None, Option, Some};
|
2013-11-11 00:46:32 -06:00
|
|
|
use io::mem::{MemReader, MemWriter};
|
|
|
|
use io::{Reader, io_error, placeholder_error};
|
|
|
|
use vec::ImmutableVector;
|
2013-10-30 20:28:37 -05:00
|
|
|
|
|
|
|
struct InitialZeroByteReader {
|
|
|
|
count: int,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for InitialZeroByteReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
|
|
|
if self.count == 0 {
|
|
|
|
self.count = 1;
|
|
|
|
Some(0)
|
|
|
|
} else {
|
|
|
|
buf[0] = 10;
|
|
|
|
Some(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn eof(&mut self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EofReader;
|
|
|
|
|
|
|
|
impl Reader for EofReader {
|
|
|
|
fn read(&mut self, _: &mut [u8]) -> Option<uint> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
fn eof(&mut self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ErroringReader;
|
|
|
|
|
|
|
|
impl Reader for ErroringReader {
|
|
|
|
fn read(&mut self, _: &mut [u8]) -> Option<uint> {
|
|
|
|
io_error::cond.raise(placeholder_error());
|
|
|
|
None
|
|
|
|
}
|
|
|
|
fn eof(&mut self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PartialReader {
|
|
|
|
count: int,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for PartialReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
|
|
|
if self.count == 0 {
|
|
|
|
self.count = 1;
|
|
|
|
buf[0] = 10;
|
|
|
|
buf[1] = 11;
|
|
|
|
Some(2)
|
|
|
|
} else {
|
|
|
|
buf[0] = 12;
|
|
|
|
buf[1] = 13;
|
|
|
|
Some(2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn eof(&mut self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ErroringLaterReader {
|
|
|
|
count: int,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for ErroringLaterReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
|
|
|
if self.count == 0 {
|
|
|
|
self.count = 1;
|
|
|
|
buf[0] = 10;
|
|
|
|
Some(1)
|
|
|
|
} else {
|
|
|
|
io_error::cond.raise(placeholder_error());
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn eof(&mut self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ThreeChunkReader {
|
|
|
|
count: int,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for ThreeChunkReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
|
|
|
if self.count == 0 {
|
|
|
|
self.count = 1;
|
|
|
|
buf[0] = 10;
|
|
|
|
buf[1] = 11;
|
|
|
|
Some(2)
|
|
|
|
} else if self.count == 1 {
|
|
|
|
self.count = 2;
|
|
|
|
buf[0] = 12;
|
|
|
|
buf[1] = 13;
|
|
|
|
Some(2)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn eof(&mut self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 19:37:31 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_byte() {
|
|
|
|
let mut reader = MemReader::new(~[10]);
|
|
|
|
let byte = reader.read_byte();
|
|
|
|
assert!(byte == Some(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_byte_0_bytes() {
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = InitialZeroByteReader {
|
|
|
|
count: 0,
|
2013-05-09 19:37:31 -05:00
|
|
|
};
|
|
|
|
let byte = reader.read_byte();
|
|
|
|
assert!(byte == Some(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_byte_eof() {
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = EofReader;
|
2013-05-09 19:37:31 -05:00
|
|
|
let byte = reader.read_byte();
|
|
|
|
assert!(byte == None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_byte_error() {
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = ErroringReader;
|
2013-11-21 19:23:21 -06:00
|
|
|
io_error::cond.trap(|_| {
|
|
|
|
}).inside(|| {
|
2013-05-09 19:37:31 -05:00
|
|
|
let byte = reader.read_byte();
|
|
|
|
assert!(byte == None);
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-05-09 19:37:31 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 18:30:21 -05:00
|
|
|
#[test]
|
|
|
|
fn bytes_0_bytes() {
|
2013-12-08 01:12:07 -06:00
|
|
|
let mut reader = InitialZeroByteReader {
|
2013-10-30 20:28:37 -05:00
|
|
|
count: 0,
|
2013-09-01 18:30:21 -05:00
|
|
|
};
|
|
|
|
let byte = reader.bytes().next();
|
|
|
|
assert!(byte == Some(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bytes_eof() {
|
2013-12-08 01:12:07 -06:00
|
|
|
let mut reader = EofReader;
|
2013-09-01 18:30:21 -05:00
|
|
|
let byte = reader.bytes().next();
|
|
|
|
assert!(byte == None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bytes_error() {
|
2013-12-08 01:12:07 -06:00
|
|
|
let mut reader = ErroringReader;
|
2013-09-01 18:30:21 -05:00
|
|
|
let mut it = reader.bytes();
|
2013-11-21 19:23:21 -06:00
|
|
|
io_error::cond.trap(|_| ()).inside(|| {
|
2013-09-01 18:30:21 -05:00
|
|
|
let byte = it.next();
|
|
|
|
assert!(byte == None);
|
2013-11-21 19:23:21 -06:00
|
|
|
})
|
2013-09-01 18:30:21 -05:00
|
|
|
}
|
|
|
|
|
2013-05-09 19:37:31 -05:00
|
|
|
#[test]
|
|
|
|
fn read_bytes() {
|
|
|
|
let mut reader = MemReader::new(~[10, 11, 12, 13]);
|
|
|
|
let bytes = reader.read_bytes(4);
|
|
|
|
assert!(bytes == ~[10, 11, 12, 13]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_bytes_partial() {
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = PartialReader {
|
|
|
|
count: 0,
|
2013-05-09 19:37:31 -05:00
|
|
|
};
|
|
|
|
let bytes = reader.read_bytes(4);
|
|
|
|
assert!(bytes == ~[10, 11, 12, 13]);
|
|
|
|
}
|
|
|
|
|
2013-05-13 17:23:52 -05:00
|
|
|
#[test]
|
|
|
|
fn read_bytes_eof() {
|
|
|
|
let mut reader = MemReader::new(~[10, 11]);
|
2013-11-21 19:23:21 -06:00
|
|
|
io_error::cond.trap(|_| {
|
|
|
|
}).inside(|| {
|
2013-05-13 18:56:16 -05:00
|
|
|
assert!(reader.read_bytes(4) == ~[10, 11]);
|
2013-11-21 19:23:21 -06:00
|
|
|
})
|
2013-05-13 17:23:52 -05:00
|
|
|
}
|
|
|
|
|
2013-05-09 19:37:31 -05:00
|
|
|
#[test]
|
|
|
|
fn push_bytes() {
|
|
|
|
let mut reader = MemReader::new(~[10, 11, 12, 13]);
|
|
|
|
let mut buf = ~[8, 9];
|
2013-05-13 17:23:52 -05:00
|
|
|
reader.push_bytes(&mut buf, 4);
|
2013-05-09 19:37:31 -05:00
|
|
|
assert!(buf == ~[8, 9, 10, 11, 12, 13]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn push_bytes_partial() {
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = PartialReader {
|
|
|
|
count: 0,
|
2013-05-09 19:37:31 -05:00
|
|
|
};
|
|
|
|
let mut buf = ~[8, 9];
|
2013-05-13 17:23:52 -05:00
|
|
|
reader.push_bytes(&mut buf, 4);
|
2013-05-09 19:37:31 -05:00
|
|
|
assert!(buf == ~[8, 9, 10, 11, 12, 13]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn push_bytes_eof() {
|
|
|
|
let mut reader = MemReader::new(~[10, 11]);
|
|
|
|
let mut buf = ~[8, 9];
|
2013-11-21 19:23:21 -06:00
|
|
|
io_error::cond.trap(|_| {
|
|
|
|
}).inside(|| {
|
2013-05-13 17:23:52 -05:00
|
|
|
reader.push_bytes(&mut buf, 4);
|
2013-05-13 18:56:16 -05:00
|
|
|
assert!(buf == ~[8, 9, 10, 11]);
|
2013-11-21 19:23:21 -06:00
|
|
|
})
|
2013-05-09 19:37:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn push_bytes_error() {
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = ErroringLaterReader {
|
|
|
|
count: 0,
|
2013-05-09 19:37:31 -05:00
|
|
|
};
|
|
|
|
let mut buf = ~[8, 9];
|
2013-11-21 19:23:21 -06:00
|
|
|
io_error::cond.trap(|_| { } ).inside(|| {
|
2013-05-13 17:23:52 -05:00
|
|
|
reader.push_bytes(&mut buf, 4);
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-05-13 18:56:16 -05:00
|
|
|
assert!(buf == ~[8, 9, 10]);
|
2013-05-09 19:37:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn push_bytes_fail_reset_len() {
|
|
|
|
// push_bytes unsafely sets the vector length. This is testing that
|
|
|
|
// upon failure the length is reset correctly.
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = ErroringLaterReader {
|
|
|
|
count: 0,
|
2013-05-09 19:37:31 -05:00
|
|
|
};
|
2013-12-30 19:39:14 -06:00
|
|
|
// FIXME (#7049): Figure out some other way to do this.
|
|
|
|
//let buf = @mut ~[8, 9];
|
2013-11-21 19:23:21 -06:00
|
|
|
(|| {
|
2013-12-30 19:39:14 -06:00
|
|
|
//reader.push_bytes(&mut *buf, 4);
|
2013-11-21 19:23:21 -06:00
|
|
|
}).finally(|| {
|
2013-05-09 19:37:31 -05:00
|
|
|
// NB: Using rtassert here to trigger abort on failure since this is a should_fail test
|
2013-06-10 19:46:49 -05:00
|
|
|
// FIXME: #7049 This fails because buf is still borrowed
|
|
|
|
//rtassert!(*buf == ~[8, 9, 10]);
|
2013-11-21 19:23:21 -06:00
|
|
|
})
|
2013-05-09 19:37:31 -05:00
|
|
|
}
|
|
|
|
|
2013-05-13 18:56:16 -05:00
|
|
|
#[test]
|
|
|
|
fn read_to_end() {
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = ThreeChunkReader {
|
|
|
|
count: 0,
|
2013-05-13 18:56:16 -05:00
|
|
|
};
|
|
|
|
let buf = reader.read_to_end();
|
|
|
|
assert!(buf == ~[10, 11, 12, 13]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn read_to_end_error() {
|
2013-10-30 20:28:37 -05:00
|
|
|
let mut reader = ThreeChunkReader {
|
|
|
|
count: 0,
|
2013-05-13 18:56:16 -05:00
|
|
|
};
|
|
|
|
let buf = reader.read_to_end();
|
|
|
|
assert!(buf == ~[10, 11]);
|
|
|
|
}
|
|
|
|
|
2013-07-12 16:43:57 -05:00
|
|
|
#[test]
|
|
|
|
fn test_read_write_le_mem() {
|
|
|
|
let uints = [0, 1, 2, 42, 10_123, 100_123_456, ::u64::max_value];
|
2013-05-13 21:14:14 -05:00
|
|
|
|
|
|
|
let mut writer = MemWriter::new();
|
2013-07-12 16:43:57 -05:00
|
|
|
for i in uints.iter() {
|
2013-10-25 20:08:45 -05:00
|
|
|
writer.write_le_u64(*i);
|
2013-05-13 21:14:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut reader = MemReader::new(writer.inner());
|
2013-07-12 16:43:57 -05:00
|
|
|
for i in uints.iter() {
|
2013-10-25 20:08:45 -05:00
|
|
|
assert!(reader.read_le_u64() == *i);
|
2013-05-13 21:14:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 16:43:57 -05:00
|
|
|
|
2013-05-13 21:14:14 -05:00
|
|
|
#[test]
|
|
|
|
fn test_read_write_be() {
|
2013-07-12 16:43:57 -05:00
|
|
|
let uints = [0, 1, 2, 42, 10_123, 100_123_456, ::u64::max_value];
|
2013-05-13 21:14:14 -05:00
|
|
|
|
|
|
|
let mut writer = MemWriter::new();
|
2013-07-12 16:43:57 -05:00
|
|
|
for i in uints.iter() {
|
2013-10-25 20:08:45 -05:00
|
|
|
writer.write_be_u64(*i);
|
2013-05-13 21:14:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut reader = MemReader::new(writer.inner());
|
2013-07-12 16:43:57 -05:00
|
|
|
for i in uints.iter() {
|
2013-10-25 20:08:45 -05:00
|
|
|
assert!(reader.read_be_u64() == *i);
|
2013-05-13 21:14:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_be_int_n() {
|
2013-07-12 16:43:57 -05:00
|
|
|
let ints = [::i32::min_value, -123456, -42, -5, 0, 1, ::i32::max_value];
|
2013-05-13 21:14:14 -05:00
|
|
|
|
|
|
|
let mut writer = MemWriter::new();
|
2013-07-12 16:43:57 -05:00
|
|
|
for i in ints.iter() {
|
2013-10-25 20:08:45 -05:00
|
|
|
writer.write_be_i32(*i);
|
2013-05-13 21:14:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut reader = MemReader::new(writer.inner());
|
2013-07-12 16:43:57 -05:00
|
|
|
for i in ints.iter() {
|
2013-05-13 21:14:14 -05:00
|
|
|
// this tests that the sign extension is working
|
|
|
|
// (comparing the values as i32 would not test this)
|
2013-10-25 20:08:45 -05:00
|
|
|
assert!(reader.read_be_int_n(4) == *i as i64);
|
2013-05-13 21:14:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_f32() {
|
|
|
|
//big-endian floating-point 8.1250
|
|
|
|
let buf = ~[0x41, 0x02, 0x00, 0x00];
|
|
|
|
|
|
|
|
let mut writer = MemWriter::new();
|
|
|
|
writer.write(buf);
|
|
|
|
|
|
|
|
let mut reader = MemReader::new(writer.inner());
|
2013-10-25 20:08:45 -05:00
|
|
|
let f = reader.read_be_f32();
|
2013-05-13 21:14:14 -05:00
|
|
|
assert!(f == 8.1250);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_write_f32() {
|
|
|
|
let f:f32 = 8.1250;
|
|
|
|
|
|
|
|
let mut writer = MemWriter::new();
|
2013-10-25 20:08:45 -05:00
|
|
|
writer.write_be_f32(f);
|
|
|
|
writer.write_le_f32(f);
|
2013-05-13 21:14:14 -05:00
|
|
|
|
|
|
|
let mut reader = MemReader::new(writer.inner());
|
2013-10-25 20:08:45 -05:00
|
|
|
assert!(reader.read_be_f32() == 8.1250);
|
|
|
|
assert!(reader.read_le_f32() == 8.1250);
|
2013-07-12 16:43:57 -05:00
|
|
|
}
|
2013-05-13 21:14:14 -05:00
|
|
|
|
2013-05-09 19:37:31 -05:00
|
|
|
}
|