1use futures::executor::block_on;
2use futures::io::{AsyncWriteExt, LineWriter};
3use std::io;
4
5#[test]
6fn line_writer() {
7 let mut writer = LineWriter::new(Vec::new());
8
9 block_on(writer.write(&[0])).unwrap();
10 assert_eq!(*writer.get_ref(), []);
11
12 block_on(writer.write(&[1])).unwrap();
13 assert_eq!(*writer.get_ref(), []);
14
15 block_on(writer.flush()).unwrap();
16 assert_eq!(*writer.get_ref(), [0, 1]);
17
18 block_on(writer.write(&[0, b'\n', 1, b'\n', 2])).unwrap();
19 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n']);
20
21 block_on(writer.flush()).unwrap();
22 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);
23
24 block_on(writer.write(&[3, b'\n'])).unwrap();
25 assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
26}
27
28#[test]
29fn line_vectored() {
30 let mut line_writer = LineWriter::new(Vec::new());
31 assert_eq!(
32 block_on(line_writer.write_vectored(&[
33 io::IoSlice::new(&[]),
34 io::IoSlice::new(b"\n"),
35 io::IoSlice::new(&[]),
36 io::IoSlice::new(b"a"),
37 ]))
38 .unwrap(),
39 2
40 );
41 assert_eq!(line_writer.get_ref(), b"\n");
42
43 assert_eq!(
44 block_on(line_writer.write_vectored(&[
45 io::IoSlice::new(&[]),
46 io::IoSlice::new(b"b"),
47 io::IoSlice::new(&[]),
48 io::IoSlice::new(b"a"),
49 io::IoSlice::new(&[]),
50 io::IoSlice::new(b"c"),
51 ]))
52 .unwrap(),
53 3
54 );
55 assert_eq!(line_writer.get_ref(), b"\n");
56 block_on(line_writer.flush()).unwrap();
57 assert_eq!(line_writer.get_ref(), b"\nabac");
58 assert_eq!(block_on(line_writer.write_vectored(&[])).unwrap(), 0);
59
60 assert_eq!(
61 block_on(line_writer.write_vectored(&[
62 io::IoSlice::new(&[]),
63 io::IoSlice::new(&[]),
64 io::IoSlice::new(&[]),
65 io::IoSlice::new(&[]),
66 ]))
67 .unwrap(),
68 0
69 );
70
71 assert_eq!(block_on(line_writer.write_vectored(&[io::IoSlice::new(b"a\nb")])).unwrap(), 3);
72 assert_eq!(line_writer.get_ref(), b"\nabaca\nb");
73}
74