From a99b8e2e0b8d100e4d7ba3cf898a3af38b35ce3d Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 28 Mar 2019 14:02:45 -0700 Subject: [PATCH] buf: impl size_hint for str types + reorg tests (#1012) --- tokio-buf/src/str.rs | 16 +++ tokio-buf/tests/buf_stream.rs | 67 +------------ tokio-buf/tests/buf_stream_ext.rs | 156 ------------------------------ tokio-buf/tests/chain.rs | 44 +++++++++ tokio-buf/tests/collect.rs | 68 +++++++++++++ tokio-buf/tests/limit.rs | 66 +++++++++++++ tokio-buf/tests/size_hint.rs | 42 ++++++++ tokio-buf/tests/string.rs | 39 ++++++++ 8 files changed, 279 insertions(+), 219 deletions(-) delete mode 100644 tokio-buf/tests/buf_stream_ext.rs create mode 100644 tokio-buf/tests/chain.rs create mode 100644 tokio-buf/tests/collect.rs create mode 100644 tokio-buf/tests/limit.rs create mode 100644 tokio-buf/tests/size_hint.rs create mode 100644 tokio-buf/tests/string.rs diff --git a/tokio-buf/src/str.rs b/tokio-buf/src/str.rs index b8a6eb643..8af76950c 100644 --- a/tokio-buf/src/str.rs +++ b/tokio-buf/src/str.rs @@ -1,5 +1,6 @@ use never::Never; use BufStream; +use SizeHint; use futures::Poll; @@ -20,6 +21,10 @@ impl BufStream for String { Ok(Some(buf).into()) } + + fn size_hint(&self) -> SizeHint { + size_hint(&self[..]) + } } impl BufStream for &'static str { @@ -36,4 +41,15 @@ impl BufStream for &'static str { Ok(Some(buf).into()) } + + fn size_hint(&self) -> SizeHint { + size_hint(&self[..]) + } +} + +fn size_hint(s: &str) -> SizeHint { + let mut hint = SizeHint::new(); + hint.set_lower(s.len() as u64); + hint.set_upper(s.len() as u64); + hint } diff --git a/tokio-buf/tests/buf_stream.rs b/tokio-buf/tests/buf_stream.rs index c116dac02..35a3f938d 100644 --- a/tokio-buf/tests/buf_stream.rs +++ b/tokio-buf/tests/buf_stream.rs @@ -1,66 +1,7 @@ -extern crate bytes; -extern crate futures; extern crate tokio_buf; -use bytes::Buf; -use futures::Async::*; -use tokio_buf::{BufStream, SizeHint}; +use tokio_buf::BufStream; -#[macro_use] -mod support; - -// ===== test `SizeHint` ===== - -#[test] -fn size_hint() { - let hint = SizeHint::new(); - assert_eq!(hint.lower(), 0); - assert!(hint.upper().is_none()); - - let mut hint = SizeHint::new(); - hint.set_lower(100); - assert_eq!(hint.lower(), 100); - assert!(hint.upper().is_none()); - - let mut hint = SizeHint::new(); - hint.set_upper(200); - assert_eq!(hint.lower(), 0); - assert_eq!(hint.upper(), Some(200)); - - let mut hint = SizeHint::new(); - hint.set_lower(100); - hint.set_upper(100); - assert_eq!(hint.lower(), 100); - assert_eq!(hint.upper(), Some(100)); -} - -#[test] -#[should_panic] -fn size_hint_lower_bigger_than_upper() { - let mut hint = SizeHint::new(); - hint.set_upper(100); - hint.set_lower(200); -} - -#[test] -#[should_panic] -fn size_hint_upper_less_than_lower() { - let mut hint = SizeHint::new(); - hint.set_lower(200); - hint.set_upper(100); -} - -// ===== BufStream impelmentations for misc types ===== - -#[test] -fn str_buf_stream() { - let mut bs = "hello world".to_string(); - assert_buf_eq!(bs.poll_buf(), "hello world"); - assert!(bs.is_empty()); - assert_none!(bs.poll_buf()); - - let mut bs = "hello world"; - assert_buf_eq!(bs.poll_buf(), "hello world"); - assert!(bs.is_empty()); - assert_none!(bs.poll_buf()); -} +// Ensures that `BufStream` can be a trait object +#[allow(dead_code)] +fn obj(_: &mut BufStream) {} diff --git a/tokio-buf/tests/buf_stream_ext.rs b/tokio-buf/tests/buf_stream_ext.rs deleted file mode 100644 index cda2aa95b..000000000 --- a/tokio-buf/tests/buf_stream_ext.rs +++ /dev/null @@ -1,156 +0,0 @@ -#![cfg(feature = "util")] - -extern crate bytes; -extern crate futures; -extern crate tokio_buf; - -use bytes::{Buf, Bytes}; -use futures::Async::*; -use futures::Future; -use tokio_buf::{BufStream, BufStreamExt}; - -#[macro_use] -mod support; - -use support::*; - -// ===== test `chain()` ===== - -#[test] -fn chain() { - // Chain one with one - // - let mut bs = one("hello").chain(one("world")); - - assert_buf_eq!(bs.poll_buf(), "hello"); - assert_buf_eq!(bs.poll_buf(), "world"); - assert_none!(bs.poll_buf()); - - // Chain multi with multi - let mut bs = list(&["foo", "bar"]).chain(list(&["baz", "bok"])); - - assert_buf_eq!(bs.poll_buf(), "foo"); - assert_buf_eq!(bs.poll_buf(), "bar"); - assert_buf_eq!(bs.poll_buf(), "baz"); - assert_buf_eq!(bs.poll_buf(), "bok"); - assert_none!(bs.poll_buf()); - - // Chain includes a not ready call - // - let mut bs = new_mock(&[Ok(Ready("foo")), Ok(NotReady), Ok(Ready("bar"))]).chain(one("baz")); - - assert_buf_eq!(bs.poll_buf(), "foo"); - assert_not_ready!(bs.poll_buf()); - assert_buf_eq!(bs.poll_buf(), "bar"); - assert_buf_eq!(bs.poll_buf(), "baz"); - assert_none!(bs.poll_buf()); -} - -// ===== Test `collect()` ===== - -macro_rules! test_collect_impl { - ($t:ty $(, $capacity:ident)*) => { - // While unfortunate, this test makes some assumptions on vec's resizing - // behavior. - // - // Collect one - // - let bs = one("hello world"); - - let vec: $t = bs.collect().wait().unwrap(); - - assert_eq!(vec, &b"hello world"[..]); - $( assert_eq!(vec.$capacity(), 64); )* - - // Collect one, with size hint - // - let mut bs = one("hello world"); - bs.size_hint.set_lower(11); - - let vec: $t = bs.collect().wait().unwrap(); - - assert_eq!(vec, &b"hello world"[..]); - $( assert_eq!(vec.$capacity(), 64); )* - - // Collect one, with size hint - // - let mut bs = one("hello world"); - bs.size_hint.set_lower(10); - - let vec: $t = bs.collect().wait().unwrap(); - - assert_eq!(vec, &b"hello world"[..]); - $( assert_eq!(vec.$capacity(), 64); )* - - // Collect many - // - let bs = list(&["hello", " ", "world", ", one two three"]); - - let vec: $t = bs.collect().wait().unwrap(); - - assert_eq!(vec, &b"hello world, one two three"[..]); - } -} - -#[test] -fn collect_vec() { - test_collect_impl!(Vec, capacity); -} - -#[test] -fn collect_bytes() { - test_collect_impl!(Bytes); -} - -// ===== Test limit() ===== - -#[test] -fn limit() { - // Not limited - - let res = one("hello world") - .limit(100) - .collect::>() - .wait() - .unwrap(); - - assert_eq!(res, b"hello world"); - - let res = list(&["hello", " ", "world"]) - .limit(100) - .collect::>() - .wait() - .unwrap(); - - assert_eq!(res, b"hello world"); - - let res = list(&["hello", " ", "world"]) - .limit(11) - .collect::>() - .wait() - .unwrap(); - - assert_eq!(res, b"hello world"); - - // Limited - - let res = one("hello world").limit(5).collect::>().wait(); - - assert!(res.is_err()); - - let res = one("hello world").limit(10).collect::>().wait(); - - assert!(res.is_err()); - - let mut bs = list(&["hello", " ", "world"]).limit(9); - - assert_buf_eq!(bs.poll_buf(), "hello"); - assert_buf_eq!(bs.poll_buf(), " "); - assert!(bs.poll_buf().is_err()); - - let mut bs = list(&["hello", " ", "world"]); - bs.size_hint.set_lower(11); - let mut bs = bs.limit(9); - - assert!(bs.poll_buf().is_err()); -} diff --git a/tokio-buf/tests/chain.rs b/tokio-buf/tests/chain.rs new file mode 100644 index 000000000..fcac0280f --- /dev/null +++ b/tokio-buf/tests/chain.rs @@ -0,0 +1,44 @@ +#![cfg(feature = "util")] + +extern crate bytes; +extern crate futures; +extern crate tokio_buf; + +use bytes::Buf; +use futures::Async::*; +use tokio_buf::{BufStream, BufStreamExt}; + +#[macro_use] +mod support; + +use support::*; + +#[test] +fn chain() { + // Chain one with one + // + let mut bs = one("hello").chain(one("world")); + + assert_buf_eq!(bs.poll_buf(), "hello"); + assert_buf_eq!(bs.poll_buf(), "world"); + assert_none!(bs.poll_buf()); + + // Chain multi with multi + let mut bs = list(&["foo", "bar"]).chain(list(&["baz", "bok"])); + + assert_buf_eq!(bs.poll_buf(), "foo"); + assert_buf_eq!(bs.poll_buf(), "bar"); + assert_buf_eq!(bs.poll_buf(), "baz"); + assert_buf_eq!(bs.poll_buf(), "bok"); + assert_none!(bs.poll_buf()); + + // Chain includes a not ready call + // + let mut bs = new_mock(&[Ok(Ready("foo")), Ok(NotReady), Ok(Ready("bar"))]).chain(one("baz")); + + assert_buf_eq!(bs.poll_buf(), "foo"); + assert_not_ready!(bs.poll_buf()); + assert_buf_eq!(bs.poll_buf(), "bar"); + assert_buf_eq!(bs.poll_buf(), "baz"); + assert_none!(bs.poll_buf()); +} diff --git a/tokio-buf/tests/collect.rs b/tokio-buf/tests/collect.rs new file mode 100644 index 000000000..dd367c2d1 --- /dev/null +++ b/tokio-buf/tests/collect.rs @@ -0,0 +1,68 @@ +#![cfg(feature = "util")] + +extern crate bytes; +extern crate futures; +extern crate tokio_buf; + +use bytes::Bytes; +use futures::Future; +use tokio_buf::BufStreamExt; + +#[macro_use] +mod support; + +use support::*; + +macro_rules! test_collect_impl { + ($t:ty $(, $capacity:ident)*) => { + // While unfortunate, this test makes some assumptions on vec's resizing + // behavior. + // + // Collect one + // + let bs = one("hello world"); + + let vec: $t = bs.collect().wait().unwrap(); + + assert_eq!(vec, &b"hello world"[..]); + $( assert_eq!(vec.$capacity(), 64); )* + + // Collect one, with size hint + // + let mut bs = one("hello world"); + bs.size_hint.set_lower(11); + + let vec: $t = bs.collect().wait().unwrap(); + + assert_eq!(vec, &b"hello world"[..]); + $( assert_eq!(vec.$capacity(), 64); )* + + // Collect one, with size hint + // + let mut bs = one("hello world"); + bs.size_hint.set_lower(10); + + let vec: $t = bs.collect().wait().unwrap(); + + assert_eq!(vec, &b"hello world"[..]); + $( assert_eq!(vec.$capacity(), 64); )* + + // Collect many + // + let bs = list(&["hello", " ", "world", ", one two three"]); + + let vec: $t = bs.collect().wait().unwrap(); + + assert_eq!(vec, &b"hello world, one two three"[..]); + } +} + +#[test] +fn collect_vec() { + test_collect_impl!(Vec, capacity); +} + +#[test] +fn collect_bytes() { + test_collect_impl!(Bytes); +} diff --git a/tokio-buf/tests/limit.rs b/tokio-buf/tests/limit.rs new file mode 100644 index 000000000..058f2e108 --- /dev/null +++ b/tokio-buf/tests/limit.rs @@ -0,0 +1,66 @@ +#![cfg(feature = "util")] + +extern crate bytes; +extern crate futures; +extern crate tokio_buf; + +use bytes::Buf; +use futures::Async::*; +use futures::Future; +use tokio_buf::{BufStream, BufStreamExt}; + +#[macro_use] +mod support; + +use support::*; + +#[test] +fn limit() { + // Not limited + + let res = one("hello world") + .limit(100) + .collect::>() + .wait() + .unwrap(); + + assert_eq!(res, b"hello world"); + + let res = list(&["hello", " ", "world"]) + .limit(100) + .collect::>() + .wait() + .unwrap(); + + assert_eq!(res, b"hello world"); + + let res = list(&["hello", " ", "world"]) + .limit(11) + .collect::>() + .wait() + .unwrap(); + + assert_eq!(res, b"hello world"); + + // Limited + + let res = one("hello world").limit(5).collect::>().wait(); + + assert!(res.is_err()); + + let res = one("hello world").limit(10).collect::>().wait(); + + assert!(res.is_err()); + + let mut bs = list(&["hello", " ", "world"]).limit(9); + + assert_buf_eq!(bs.poll_buf(), "hello"); + assert_buf_eq!(bs.poll_buf(), " "); + assert!(bs.poll_buf().is_err()); + + let mut bs = list(&["hello", " ", "world"]); + bs.size_hint.set_lower(11); + let mut bs = bs.limit(9); + + assert!(bs.poll_buf().is_err()); +} diff --git a/tokio-buf/tests/size_hint.rs b/tokio-buf/tests/size_hint.rs new file mode 100644 index 000000000..45f867655 --- /dev/null +++ b/tokio-buf/tests/size_hint.rs @@ -0,0 +1,42 @@ +extern crate tokio_buf; + +use tokio_buf::SizeHint; + +#[test] +fn size_hint() { + let hint = SizeHint::new(); + assert_eq!(hint.lower(), 0); + assert!(hint.upper().is_none()); + + let mut hint = SizeHint::new(); + hint.set_lower(100); + assert_eq!(hint.lower(), 100); + assert!(hint.upper().is_none()); + + let mut hint = SizeHint::new(); + hint.set_upper(200); + assert_eq!(hint.lower(), 0); + assert_eq!(hint.upper(), Some(200)); + + let mut hint = SizeHint::new(); + hint.set_lower(100); + hint.set_upper(100); + assert_eq!(hint.lower(), 100); + assert_eq!(hint.upper(), Some(100)); +} + +#[test] +#[should_panic] +fn size_hint_lower_bigger_than_upper() { + let mut hint = SizeHint::new(); + hint.set_upper(100); + hint.set_lower(200); +} + +#[test] +#[should_panic] +fn size_hint_upper_less_than_lower() { + let mut hint = SizeHint::new(); + hint.set_lower(200); + hint.set_upper(100); +} diff --git a/tokio-buf/tests/string.rs b/tokio-buf/tests/string.rs new file mode 100644 index 000000000..b54d7d304 --- /dev/null +++ b/tokio-buf/tests/string.rs @@ -0,0 +1,39 @@ +extern crate bytes; +extern crate futures; +extern crate tokio_buf; + +use bytes::Buf; +use futures::Async::*; +use std::fmt; +use tokio_buf::BufStream; + +#[macro_use] +mod support; + +fn test_hello_world(mut bs: B) +where + B: BufStream + fmt::Debug, + B::Item: fmt::Debug, + B::Error: fmt::Debug, +{ + let hint = bs.size_hint(); + assert_eq!(hint.lower(), 11); + assert_eq!(hint.upper(), Some(11)); + + assert_buf_eq!(bs.poll_buf(), "hello world"); + + let hint = bs.size_hint(); + assert_eq!(hint.lower(), 0); + assert_eq!(hint.upper(), Some(0)); + assert_none!(bs.poll_buf()); +} + +#[test] +fn string() { + test_hello_world("hello world".to_string()); +} + +#[test] +fn str() { + test_hello_world("hello world"); +}