aboutsummaryrefslogtreecommitdiff
path: root/tests/test_buf_mut.rs
blob: 33aa6803862d7e40d1294958e872cce12a81e5d6 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#![warn(rust_2018_idioms)]

use bytes::buf::UninitSlice;
use bytes::{BufMut, BytesMut};
use core::fmt::Write;
use core::mem::MaybeUninit;
use core::usize;

#[test]
fn test_vec_as_mut_buf() {
    let mut buf = Vec::with_capacity(64);

    assert_eq!(buf.remaining_mut(), isize::MAX as usize);

    assert!(buf.chunk_mut().len() >= 64);

    buf.put(&b"zomg"[..]);

    assert_eq!(&buf, b"zomg");

    assert_eq!(buf.remaining_mut(), isize::MAX as usize - 4);
    assert_eq!(buf.capacity(), 64);

    for _ in 0..16 {
        buf.put(&b"zomg"[..]);
    }

    assert_eq!(buf.len(), 68);
}

#[test]
fn test_vec_put_bytes() {
    let mut buf = Vec::new();
    buf.push(17);
    buf.put_bytes(19, 2);
    assert_eq!([17, 19, 19], &buf[..]);
}

#[test]
fn test_put_u8() {
    let mut buf = Vec::with_capacity(8);
    buf.put_u8(33);
    assert_eq!(b"\x21", &buf[..]);
}

#[test]
fn test_put_u16() {
    let mut buf = Vec::with_capacity(8);
    buf.put_u16(8532);
    assert_eq!(b"\x21\x54", &buf[..]);

    buf.clear();
    buf.put_u16_le(8532);
    assert_eq!(b"\x54\x21", &buf[..]);
}

#[test]
fn test_put_int() {
    let mut buf = Vec::with_capacity(8);
    buf.put_int(0x1020304050607080, 3);
    assert_eq!(b"\x60\x70\x80", &buf[..]);
}

#[test]
#[should_panic]
fn test_put_int_nbytes_overflow() {
    let mut buf = Vec::with_capacity(8);
    buf.put_int(0x1020304050607080, 9);
}

#[test]
fn test_put_int_le() {
    let mut buf = Vec::with_capacity(8);
    buf.put_int_le(0x1020304050607080, 3);
    assert_eq!(b"\x80\x70\x60", &buf[..]);
}

#[test]
#[should_panic]
fn test_put_int_le_nbytes_overflow() {
    let mut buf = Vec::with_capacity(8);
    buf.put_int_le(0x1020304050607080, 9);
}

#[test]
#[should_panic(expected = "cannot advance")]
fn test_vec_advance_mut() {
    // Verify fix for #354
    let mut buf = Vec::with_capacity(8);
    unsafe {
        buf.advance_mut(12);
    }
}

#[test]
fn test_clone() {
    let mut buf = BytesMut::with_capacity(100);
    buf.write_str("this is a test").unwrap();
    let buf2 = buf.clone();

    buf.write_str(" of our emergency broadcast system").unwrap();
    assert!(buf != buf2);
}

fn do_test_slice_small<T: ?Sized>(make: impl Fn(&mut [u8]) -> &mut T)
where
    for<'r> &'r mut T: BufMut,
{
    let mut buf = [b'X'; 8];

    let mut slice = make(&mut buf[..]);
    slice.put_bytes(b'A', 2);
    slice.put_u8(b'B');
    slice.put_slice(b"BCC");
    assert_eq!(2, slice.remaining_mut());
    assert_eq!(b"AABBCCXX", &buf[..]);

    let mut slice = make(&mut buf[..]);
    slice.put_u32(0x61626364);
    assert_eq!(4, slice.remaining_mut());
    assert_eq!(b"abcdCCXX", &buf[..]);

    let mut slice = make(&mut buf[..]);
    slice.put_u32_le(0x30313233);
    assert_eq!(4, slice.remaining_mut());
    assert_eq!(b"3210CCXX", &buf[..]);
}

fn do_test_slice_large<T: ?Sized>(make: impl Fn(&mut [u8]) -> &mut T)
where
    for<'r> &'r mut T: BufMut,
{
    const LEN: usize = 100;
    const FILL: [u8; LEN] = [b'Y'; LEN];

    let test = |fill: &dyn Fn(&mut &mut T, usize)| {
        for buf_len in 0..LEN {
            let mut buf = [b'X'; LEN];
            for fill_len in 0..=buf_len {
                let mut slice = make(&mut buf[..buf_len]);
                fill(&mut slice, fill_len);
                assert_eq!(buf_len - fill_len, slice.remaining_mut());
                let (head, tail) = buf.split_at(fill_len);
                assert_eq!(&FILL[..fill_len], head);
                assert!(tail.iter().all(|b| *b == b'X'));
            }
        }
    };

    test(&|slice, fill_len| slice.put_slice(&FILL[..fill_len]));
    test(&|slice, fill_len| slice.put_bytes(FILL[0], fill_len));
}

fn do_test_slice_put_slice_panics<T: ?Sized>(make: impl Fn(&mut [u8]) -> &mut T)
where
    for<'r> &'r mut T: BufMut,
{
    let mut buf = [b'X'; 4];
    let mut slice = make(&mut buf[..]);
    slice.put_slice(b"12345");
}

fn do_test_slice_put_bytes_panics<T: ?Sized>(make: impl Fn(&mut [u8]) -> &mut T)
where
    for<'r> &'r mut T: BufMut,
{
    let mut buf = [b'X'; 4];
    let mut slice = make(&mut buf[..]);
    slice.put_bytes(b'1', 5);
}

#[test]
fn test_slice_buf_mut_small() {
    do_test_slice_small(|x| x);
}

#[test]
fn test_slice_buf_mut_large() {
    do_test_slice_large(|x| x);
}

#[test]
#[should_panic]
fn test_slice_buf_mut_put_slice_overflow() {
    do_test_slice_put_slice_panics(|x| x);
}

#[test]
#[should_panic]
fn test_slice_buf_mut_put_bytes_overflow() {
    do_test_slice_put_bytes_panics(|x| x);
}

fn make_maybe_uninit_slice(slice: &mut [u8]) -> &mut [MaybeUninit<u8>] {
    // SAFETY: [u8] has the same layout as [MaybeUninit<u8>].
    unsafe { core::mem::transmute(slice) }
}

#[test]
fn test_maybe_uninit_buf_mut_small() {
    do_test_slice_small(make_maybe_uninit_slice);
}

#[test]
fn test_maybe_uninit_buf_mut_large() {
    do_test_slice_large(make_maybe_uninit_slice);
}

#[test]
#[should_panic]
fn test_maybe_uninit_buf_mut_put_slice_overflow() {
    do_test_slice_put_slice_panics(make_maybe_uninit_slice);
}

#[test]
#[should_panic]
fn test_maybe_uninit_buf_mut_put_bytes_overflow() {
    do_test_slice_put_bytes_panics(make_maybe_uninit_slice);
}

#[allow(unused_allocation)] // This is intentional.
#[test]
fn test_deref_bufmut_forwards() {
    struct Special;

    unsafe impl BufMut for Special {
        fn remaining_mut(&self) -> usize {
            unreachable!("remaining_mut");
        }

        fn chunk_mut(&mut self) -> &mut UninitSlice {
            unreachable!("chunk_mut");
        }

        unsafe fn advance_mut(&mut self, _: usize) {
            unreachable!("advance");
        }

        fn put_u8(&mut self, _: u8) {
            // specialized!
        }
    }

    // these should all use the specialized method
    Special.put_u8(b'x');
    (&mut Special as &mut dyn BufMut).put_u8(b'x');
    (Box::new(Special) as Box<dyn BufMut>).put_u8(b'x');
    Box::new(Special).put_u8(b'x');
}

#[test]
#[should_panic]
fn write_byte_panics_if_out_of_bounds() {
    let mut data = [b'b', b'a', b'r'];

    let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
    slice.write_byte(4, b'f');
}

#[test]
#[should_panic]
fn copy_from_slice_panics_if_different_length_1() {
    let mut data = [b'b', b'a', b'r'];

    let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
    slice.copy_from_slice(b"a");
}

#[test]
#[should_panic]
fn copy_from_slice_panics_if_different_length_2() {
    let mut data = [b'b', b'a', b'r'];

    let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
    slice.copy_from_slice(b"abcd");
}