Fixed tests

This commit is contained in:
arkpar 2016-01-27 16:58:22 +01:00
parent 40314614f7
commit 60e2b53a1d
4 changed files with 33 additions and 22 deletions

View File

@ -14,9 +14,10 @@
//! fn to_bytes() { //! fn to_bytes() {
//! use util::bytes::ToBytes; //! use util::bytes::ToBytes;
//! //!
//! let a: Vec<u8> = "hello_world".to_bytes(); //! let mut out: Vec<u8> = Vec::new();
//! let b: Vec<u8> = 400u32.to_bytes(); //! "hello_world".to_bytes(&mut out);
//! let c: Vec<u8> = 0xffffffffffffffffu64.to_bytes(); //! 400u32.to_bytes(&mut out);
//! 0xffffffffffffffffu64.to_bytes(&mut out);
//! } //! }
//! //!
//! fn from_bytes() { //! fn from_bytes() {
@ -47,20 +48,29 @@ use elastic_array::*;
/// Vector like object /// Vector like object
pub trait VecLike<T> { pub trait VecLike<T> {
/// Add an element to the collection /// Add an element to the collection
fn push(&mut self, value: T); fn vec_push(&mut self, value: T);
/// Add a slice to the collection /// Add a slice to the collection
fn extend(&mut self, slice: &[T]); fn vec_extend(&mut self, slice: &[T]);
} }
impl<T> VecLike<T> for Vec<T> where T: Copy {
fn vec_push(&mut self, value: T) {
Vec::<T>::push(self, value)
}
fn vec_extend(&mut self, slice: &[T]) {
Vec::<T>::extend_from_slice(self, slice)
}
}
macro_rules! impl_veclike_for_elastic_array { macro_rules! impl_veclike_for_elastic_array {
($from: ident) => { ($from: ident) => {
impl<T> VecLike<T> for $from<T> where T: Copy { impl<T> VecLike<T> for $from<T> where T: Copy {
fn push(&mut self, value: T) { fn vec_push(&mut self, value: T) {
$from::<T>::push(self, value) $from::<T>::push(self, value)
} }
fn extend(&mut self, slice: &[T]) { fn vec_extend(&mut self, slice: &[T]) {
$from::<T>::append_slice(self, slice) $from::<T>::append_slice(self, slice)
} }
@ -207,7 +217,7 @@ pub trait ToBytes {
impl <'a> ToBytes for &'a str { impl <'a> ToBytes for &'a str {
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) { fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
out.extend(self.as_bytes()); out.vec_extend(self.as_bytes());
} }
fn to_bytes_len(&self) -> usize { fn to_bytes_len(&self) -> usize {
@ -217,7 +227,7 @@ impl <'a> ToBytes for &'a str {
impl ToBytes for String { impl ToBytes for String {
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) { fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
out.extend(self.as_bytes()); out.vec_extend(self.as_bytes());
} }
fn to_bytes_len(&self) -> usize { fn to_bytes_len(&self) -> usize {
@ -230,7 +240,7 @@ impl ToBytes for u64 {
let count = self.to_bytes_len(); let count = self.to_bytes_len();
for i in 0..count { for i in 0..count {
let j = count - 1 - i; let j = count - 1 - i;
out.push((*self >> (j * 8)) as u8); out.vec_push((*self >> (j * 8)) as u8);
} }
} }
@ -239,7 +249,7 @@ impl ToBytes for u64 {
impl ToBytes for bool { impl ToBytes for bool {
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) { fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
out.push(if *self { 1u8 } else { 0u8 }) out.vec_push(if *self { 1u8 } else { 0u8 })
} }
fn to_bytes_len(&self) -> usize { 1 } fn to_bytes_len(&self) -> usize { 1 }
@ -269,7 +279,7 @@ macro_rules! impl_uint_to_bytes {
let count = self.to_bytes_len(); let count = self.to_bytes_len();
for i in 0..count { for i in 0..count {
let j = count - 1 - i; let j = count - 1 - i;
out.push(self.byte(j)); out.vec_push(self.byte(j));
} }
} }
fn to_bytes_len(&self) -> usize { (self.bits() + 7) / 8 } fn to_bytes_len(&self) -> usize { (self.bits() + 7) / 8 }
@ -282,7 +292,7 @@ impl_uint_to_bytes!(U128);
impl <T>ToBytes for T where T: FixedHash { impl <T>ToBytes for T where T: FixedHash {
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) { fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
out.extend(self.bytes()); out.vec_extend(self.bytes());
} }
fn to_bytes_len(&self) -> usize { self.bytes().len() } fn to_bytes_len(&self) -> usize { self.bytes().len() }
} }

View File

@ -87,7 +87,7 @@ pub fn decode<T>(bytes: &[u8]) -> T where T: Decodable {
/// ///
/// fn main () { /// fn main () {
/// let animal = "cat"; /// let animal = "cat";
/// let out = encode(&animal); /// let out = encode(&animal).to_vec();
/// assert_eq!(out, vec![0x83, b'c', b'a', b't']); /// assert_eq!(out, vec![0x83, b'c', b'a', b't']);
/// } /// }
/// ``` /// ```
@ -105,7 +105,7 @@ pub fn encode<E>(object: &E) -> ElasticArray1024<u8> where E: Encodable {
/// ///
/// fn main () { /// fn main () {
/// let animals = vec!["cat", "dog"]; /// let animals = vec!["cat", "dog"];
/// let out = encode_list(&animals); /// let out = encode_list(&animals).to_vec();
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']); /// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
/// } /// }
/// ``` /// ```

View File

@ -69,8 +69,9 @@ impl Stream for RlpStream {
let items = list.deref(); let items = list.deref();
self.begin_list(items.len()); self.begin_list(items.len());
for el in items.iter() { for el in items.iter() {
self.append(el); el.rlp_append(self);
} }
self.note_appended(items.len());
self self
} }
@ -240,7 +241,7 @@ impl<T> ByteEncodable for T where T: ToBytes {
impl<'a> ByteEncodable for &'a[u8] { impl<'a> ByteEncodable for &'a[u8] {
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) { fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
out.extend(self) out.vec_extend(self)
} }
fn bytes_len(&self) -> usize { fn bytes_len(&self) -> usize {
@ -250,7 +251,7 @@ impl<'a> ByteEncodable for &'a[u8] {
impl ByteEncodable for Vec<u8> { impl ByteEncodable for Vec<u8> {
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) { fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
out.extend(self.deref()) out.vec_extend(self.deref())
} }
fn bytes_len(&self) -> usize { fn bytes_len(&self) -> usize {

View File

@ -286,10 +286,10 @@ pub trait Stream: Sized {
/// use util::rlp::*; /// use util::rlp::*;
/// ///
/// fn main () { /// fn main () {
/// let mut stream = RlpStream::new_list(2); /// let mut stream = RlpStream::new_list(1);
/// stream.begin_list([&"cat", &"dog"]); /// stream.append_list(&vec!["cat", "dog"]);
/// let out = stream.out(); /// let out = stream.out().to_vec();
/// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]); /// assert_eq!(out, vec![0xc9, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
/// } /// }
/// ``` /// ```
fn append_list<I, E>(&mut self, list: &I) -> &mut Self where I: Deref<Target = [E]>, E: Encodable; fn append_list<I, E>(&mut self, list: &I) -> &mut Self where I: Deref<Target = [E]>, E: Encodable;