summaryrefslogtreecommitdiffstats
path: root/vendor/futures/tests/io_line_writer.rs
blob: b483e0ff77dfc641786834e3480a51c08506eefb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use futures::executor::block_on;
use futures::io::{AsyncWriteExt, LineWriter};
use std::io;

#[test]
fn line_writer() {
    let mut writer = LineWriter::new(Vec::new());

    block_on(writer.write(&[0])).unwrap();
    assert_eq!(*writer.get_ref(), []);

    block_on(writer.write(&[1])).unwrap();
    assert_eq!(*writer.get_ref(), []);

    block_on(writer.flush()).unwrap();
    assert_eq!(*writer.get_ref(), [0, 1]);

    block_on(writer.write(&[0, b'\n', 1, b'\n', 2])).unwrap();
    assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n']);

    block_on(writer.flush()).unwrap();
    assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);

    block_on(writer.write(&[3, b'\n'])).unwrap();
    assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
}

#[test]
fn line_vectored() {
    let mut line_writer = LineWriter::new(Vec::new());
    assert_eq!(
        block_on(line_writer.write_vectored(&[
            io::IoSlice::new(&[]),
            io::IoSlice::new(b"\n"),
            io::IoSlice::new(&[]),
            io::IoSlice::new(b"a"),
        ]))
        .unwrap(),
        2
    );
    assert_eq!(line_writer.get_ref(), b"\n");

    assert_eq!(
        block_on(line_writer.write_vectored(&[
            io::IoSlice::new(&[]),
            io::IoSlice::new(b"b"),
            io::IoSlice::new(&[]),
            io::IoSlice::new(b"a"),
            io::IoSlice::new(&[]),
            io::IoSlice::new(b"c"),
        ]))
        .unwrap(),
        3
    );
    assert_eq!(line_writer.get_ref(), b"\n");
    block_on(line_writer.flush()).unwrap();
    assert_eq!(line_writer.get_ref(), b"\nabac");
    assert_eq!(block_on(line_writer.write_vectored(&[])).unwrap(), 0);

    assert_eq!(
        block_on(line_writer.write_vectored(&[
            io::IoSlice::new(&[]),
            io::IoSlice::new(&[]),
            io::IoSlice::new(&[]),
            io::IoSlice::new(&[]),
        ]))
        .unwrap(),
        0
    );

    assert_eq!(block_on(line_writer.write_vectored(&[io::IoSlice::new(b"a\nb")])).unwrap(), 3);
    assert_eq!(line_writer.get_ref(), b"\nabaca\nb");
}