1 | use futures::executor::block_on; |
2 | use futures::future::{Future, FutureExt}; |
3 | use futures::io::{AsyncReadExt, Cursor}; |
4 | use futures::stream::{self, StreamExt, TryStreamExt}; |
5 | use futures::task::Poll; |
6 | use futures_test::io::AsyncReadTestExt; |
7 | use futures_test::task::noop_context; |
8 | |
9 | #[test] |
10 | fn read_to_string() { |
11 | let mut c = Cursor::new(&b"" [..]); |
12 | let mut v = String::new(); |
13 | assert_eq!(block_on(c.read_to_string(&mut v)).unwrap(), 0); |
14 | assert_eq!(v, "" ); |
15 | |
16 | let mut c = Cursor::new(&b"1" [..]); |
17 | let mut v = String::new(); |
18 | assert_eq!(block_on(c.read_to_string(&mut v)).unwrap(), 1); |
19 | assert_eq!(v, "1" ); |
20 | |
21 | let mut c = Cursor::new(&b" \xff" [..]); |
22 | let mut v = String::new(); |
23 | assert!(block_on(c.read_to_string(&mut v)).is_err()); |
24 | } |
25 | |
26 | #[test] |
27 | fn interleave_pending() { |
28 | fn run<F: Future + Unpin>(mut f: F) -> F::Output { |
29 | let mut cx = noop_context(); |
30 | loop { |
31 | if let Poll::Ready(x) = f.poll_unpin(&mut cx) { |
32 | return x; |
33 | } |
34 | } |
35 | } |
36 | let mut buf = stream::iter(vec![&b"12" [..], &b"33" [..], &b"3" [..]]) |
37 | .map(Ok) |
38 | .into_async_read() |
39 | .interleave_pending(); |
40 | |
41 | let mut v = String::new(); |
42 | assert_eq!(run(buf.read_to_string(&mut v)).unwrap(), 5); |
43 | assert_eq!(v, "12333" ); |
44 | } |
45 | |