• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![warn(rust_2018_idioms)]
2 
3 use bytes::{Buf, BufMut, Bytes, BytesMut};
4 
5 use std::usize;
6 
7 const LONG: &[u8] = b"mary had a little lamb, little lamb, little lamb";
8 const SHORT: &[u8] = b"hello world";
9 
is_sync<T: Sync>()10 fn is_sync<T: Sync>() {}
is_send<T: Send>()11 fn is_send<T: Send>() {}
12 
13 #[test]
test_bounds()14 fn test_bounds() {
15     is_sync::<Bytes>();
16     is_sync::<BytesMut>();
17     is_send::<Bytes>();
18     is_send::<BytesMut>();
19 }
20 
21 #[test]
test_layout()22 fn test_layout() {
23     use std::mem;
24 
25     assert_eq!(
26         mem::size_of::<Bytes>(),
27         mem::size_of::<usize>() * 4,
28         "Bytes size should be 4 words",
29     );
30     assert_eq!(
31         mem::size_of::<BytesMut>(),
32         mem::size_of::<usize>() * 4,
33         "BytesMut should be 4 words",
34     );
35 
36     assert_eq!(
37         mem::size_of::<Bytes>(),
38         mem::size_of::<Option<Bytes>>(),
39         "Bytes should be same size as Option<Bytes>",
40     );
41 
42     assert_eq!(
43         mem::size_of::<BytesMut>(),
44         mem::size_of::<Option<BytesMut>>(),
45         "BytesMut should be same size as Option<BytesMut>",
46     );
47 }
48 
49 #[test]
from_slice()50 fn from_slice() {
51     let a = Bytes::from(&b"abcdefgh"[..]);
52     assert_eq!(a, b"abcdefgh"[..]);
53     assert_eq!(a, &b"abcdefgh"[..]);
54     assert_eq!(a, Vec::from(&b"abcdefgh"[..]));
55     assert_eq!(b"abcdefgh"[..], a);
56     assert_eq!(&b"abcdefgh"[..], a);
57     assert_eq!(Vec::from(&b"abcdefgh"[..]), a);
58 
59     let a = BytesMut::from(&b"abcdefgh"[..]);
60     assert_eq!(a, b"abcdefgh"[..]);
61     assert_eq!(a, &b"abcdefgh"[..]);
62     assert_eq!(a, Vec::from(&b"abcdefgh"[..]));
63     assert_eq!(b"abcdefgh"[..], a);
64     assert_eq!(&b"abcdefgh"[..], a);
65     assert_eq!(Vec::from(&b"abcdefgh"[..]), a);
66 }
67 
68 #[test]
fmt()69 fn fmt() {
70     let a = format!("{:?}", Bytes::from(&b"abcdefg"[..]));
71     let b = "b\"abcdefg\"";
72 
73     assert_eq!(a, b);
74 
75     let a = format!("{:?}", BytesMut::from(&b"abcdefg"[..]));
76     assert_eq!(a, b);
77 }
78 
79 #[test]
fmt_write()80 fn fmt_write() {
81     use std::fmt::Write;
82     use std::iter::FromIterator;
83     let s = String::from_iter((0..10).map(|_| "abcdefg"));
84 
85     let mut a = BytesMut::with_capacity(64);
86     write!(a, "{}", &s[..64]).unwrap();
87     assert_eq!(a, s[..64].as_bytes());
88 
89     let mut b = BytesMut::with_capacity(64);
90     write!(b, "{}", &s[..32]).unwrap();
91     write!(b, "{}", &s[32..64]).unwrap();
92     assert_eq!(b, s[..64].as_bytes());
93 
94     let mut c = BytesMut::with_capacity(64);
95     write!(c, "{}", s).unwrap();
96     assert_eq!(c, s[..].as_bytes());
97 }
98 
99 #[test]
len()100 fn len() {
101     let a = Bytes::from(&b"abcdefg"[..]);
102     assert_eq!(a.len(), 7);
103 
104     let a = BytesMut::from(&b"abcdefg"[..]);
105     assert_eq!(a.len(), 7);
106 
107     let a = Bytes::from(&b""[..]);
108     assert!(a.is_empty());
109 
110     let a = BytesMut::from(&b""[..]);
111     assert!(a.is_empty());
112 }
113 
114 #[test]
index()115 fn index() {
116     let a = Bytes::from(&b"hello world"[..]);
117     assert_eq!(a[0..5], *b"hello");
118 }
119 
120 #[test]
slice()121 fn slice() {
122     let a = Bytes::from(&b"hello world"[..]);
123 
124     let b = a.slice(3..5);
125     assert_eq!(b, b"lo"[..]);
126 
127     let b = a.slice(0..0);
128     assert_eq!(b, b""[..]);
129 
130     let b = a.slice(3..3);
131     assert_eq!(b, b""[..]);
132 
133     let b = a.slice(a.len()..a.len());
134     assert_eq!(b, b""[..]);
135 
136     let b = a.slice(..5);
137     assert_eq!(b, b"hello"[..]);
138 
139     let b = a.slice(3..);
140     assert_eq!(b, b"lo world"[..]);
141 }
142 
143 #[test]
144 #[should_panic]
slice_oob_1()145 fn slice_oob_1() {
146     let a = Bytes::from(&b"hello world"[..]);
147     a.slice(5..44);
148 }
149 
150 #[test]
151 #[should_panic]
slice_oob_2()152 fn slice_oob_2() {
153     let a = Bytes::from(&b"hello world"[..]);
154     a.slice(44..49);
155 }
156 
157 #[test]
split_off()158 fn split_off() {
159     let mut hello = Bytes::from(&b"helloworld"[..]);
160     let world = hello.split_off(5);
161 
162     assert_eq!(hello, &b"hello"[..]);
163     assert_eq!(world, &b"world"[..]);
164 
165     let mut hello = BytesMut::from(&b"helloworld"[..]);
166     let world = hello.split_off(5);
167 
168     assert_eq!(hello, &b"hello"[..]);
169     assert_eq!(world, &b"world"[..]);
170 }
171 
172 #[test]
173 #[should_panic]
split_off_oob()174 fn split_off_oob() {
175     let mut hello = Bytes::from(&b"helloworld"[..]);
176     let _ = hello.split_off(44);
177 }
178 
179 #[test]
split_off_uninitialized()180 fn split_off_uninitialized() {
181     let mut bytes = BytesMut::with_capacity(1024);
182     let other = bytes.split_off(128);
183 
184     assert_eq!(bytes.len(), 0);
185     assert_eq!(bytes.capacity(), 128);
186 
187     assert_eq!(other.len(), 0);
188     assert_eq!(other.capacity(), 896);
189 }
190 
191 #[test]
split_off_to_loop()192 fn split_off_to_loop() {
193     let s = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
194 
195     for i in 0..(s.len() + 1) {
196         {
197             let mut bytes = Bytes::from(&s[..]);
198             let off = bytes.split_off(i);
199             assert_eq!(i, bytes.len());
200             let mut sum = Vec::new();
201             sum.extend(bytes.iter());
202             sum.extend(off.iter());
203             assert_eq!(&s[..], &sum[..]);
204         }
205         {
206             let mut bytes = BytesMut::from(&s[..]);
207             let off = bytes.split_off(i);
208             assert_eq!(i, bytes.len());
209             let mut sum = Vec::new();
210             sum.extend(&bytes);
211             sum.extend(&off);
212             assert_eq!(&s[..], &sum[..]);
213         }
214         {
215             let mut bytes = Bytes::from(&s[..]);
216             let off = bytes.split_to(i);
217             assert_eq!(i, off.len());
218             let mut sum = Vec::new();
219             sum.extend(off.iter());
220             sum.extend(bytes.iter());
221             assert_eq!(&s[..], &sum[..]);
222         }
223         {
224             let mut bytes = BytesMut::from(&s[..]);
225             let off = bytes.split_to(i);
226             assert_eq!(i, off.len());
227             let mut sum = Vec::new();
228             sum.extend(&off);
229             sum.extend(&bytes);
230             assert_eq!(&s[..], &sum[..]);
231         }
232     }
233 }
234 
235 #[test]
split_to_1()236 fn split_to_1() {
237     // Static
238     let mut a = Bytes::from_static(SHORT);
239     let b = a.split_to(4);
240 
241     assert_eq!(SHORT[4..], a);
242     assert_eq!(SHORT[..4], b);
243 
244     // Allocated
245     let mut a = Bytes::copy_from_slice(LONG);
246     let b = a.split_to(4);
247 
248     assert_eq!(LONG[4..], a);
249     assert_eq!(LONG[..4], b);
250 
251     let mut a = Bytes::copy_from_slice(LONG);
252     let b = a.split_to(30);
253 
254     assert_eq!(LONG[30..], a);
255     assert_eq!(LONG[..30], b);
256 }
257 
258 #[test]
split_to_2()259 fn split_to_2() {
260     let mut a = Bytes::from(LONG);
261     assert_eq!(LONG, a);
262 
263     let b = a.split_to(1);
264 
265     assert_eq!(LONG[1..], a);
266     drop(b);
267 }
268 
269 #[test]
270 #[should_panic]
split_to_oob()271 fn split_to_oob() {
272     let mut hello = Bytes::from(&b"helloworld"[..]);
273     let _ = hello.split_to(33);
274 }
275 
276 #[test]
277 #[should_panic]
split_to_oob_mut()278 fn split_to_oob_mut() {
279     let mut hello = BytesMut::from(&b"helloworld"[..]);
280     let _ = hello.split_to(33);
281 }
282 
283 #[test]
284 #[should_panic]
split_to_uninitialized()285 fn split_to_uninitialized() {
286     let mut bytes = BytesMut::with_capacity(1024);
287     let _other = bytes.split_to(128);
288 }
289 
290 #[test]
291 #[ignore = "Android: we unwind differently."]
split_off_to_at_gt_len()292 fn split_off_to_at_gt_len() {
293     fn make_bytes() -> Bytes {
294         let mut bytes = BytesMut::with_capacity(100);
295         bytes.put_slice(&[10, 20, 30, 40]);
296         bytes.freeze()
297     }
298 
299     use std::panic;
300 
301     let _ = make_bytes().split_to(4);
302     let _ = make_bytes().split_off(4);
303 
304     assert!(panic::catch_unwind(move || {
305         let _ = make_bytes().split_to(5);
306     })
307     .is_err());
308 
309     assert!(panic::catch_unwind(move || {
310         let _ = make_bytes().split_off(5);
311     })
312     .is_err());
313 }
314 
315 #[test]
truncate()316 fn truncate() {
317     let s = &b"helloworld"[..];
318     let mut hello = Bytes::from(s);
319     hello.truncate(15);
320     assert_eq!(hello, s);
321     hello.truncate(10);
322     assert_eq!(hello, s);
323     hello.truncate(5);
324     assert_eq!(hello, "hello");
325 }
326 
327 #[test]
freeze_clone_shared()328 fn freeze_clone_shared() {
329     let s = &b"abcdefgh"[..];
330     let b = BytesMut::from(s).split().freeze();
331     assert_eq!(b, s);
332     let c = b.clone();
333     assert_eq!(c, s);
334 }
335 
336 #[test]
freeze_clone_unique()337 fn freeze_clone_unique() {
338     let s = &b"abcdefgh"[..];
339     let b = BytesMut::from(s).freeze();
340     assert_eq!(b, s);
341     let c = b.clone();
342     assert_eq!(c, s);
343 }
344 
345 #[test]
freeze_after_advance()346 fn freeze_after_advance() {
347     let s = &b"abcdefgh"[..];
348     let mut b = BytesMut::from(s);
349     b.advance(1);
350     assert_eq!(b, s[1..]);
351     let b = b.freeze();
352     // Verify fix for #352. Previously, freeze would ignore the start offset
353     // for BytesMuts in Vec mode.
354     assert_eq!(b, s[1..]);
355 }
356 
357 #[test]
freeze_after_advance_arc()358 fn freeze_after_advance_arc() {
359     let s = &b"abcdefgh"[..];
360     let mut b = BytesMut::from(s);
361     // Make b Arc
362     let _ = b.split_to(0);
363     b.advance(1);
364     assert_eq!(b, s[1..]);
365     let b = b.freeze();
366     assert_eq!(b, s[1..]);
367 }
368 
369 #[test]
freeze_after_split_to()370 fn freeze_after_split_to() {
371     let s = &b"abcdefgh"[..];
372     let mut b = BytesMut::from(s);
373     let _ = b.split_to(1);
374     assert_eq!(b, s[1..]);
375     let b = b.freeze();
376     assert_eq!(b, s[1..]);
377 }
378 
379 #[test]
freeze_after_truncate()380 fn freeze_after_truncate() {
381     let s = &b"abcdefgh"[..];
382     let mut b = BytesMut::from(s);
383     b.truncate(7);
384     assert_eq!(b, s[..7]);
385     let b = b.freeze();
386     assert_eq!(b, s[..7]);
387 }
388 
389 #[test]
freeze_after_truncate_arc()390 fn freeze_after_truncate_arc() {
391     let s = &b"abcdefgh"[..];
392     let mut b = BytesMut::from(s);
393     // Make b Arc
394     let _ = b.split_to(0);
395     b.truncate(7);
396     assert_eq!(b, s[..7]);
397     let b = b.freeze();
398     assert_eq!(b, s[..7]);
399 }
400 
401 #[test]
freeze_after_split_off()402 fn freeze_after_split_off() {
403     let s = &b"abcdefgh"[..];
404     let mut b = BytesMut::from(s);
405     let _ = b.split_off(7);
406     assert_eq!(b, s[..7]);
407     let b = b.freeze();
408     assert_eq!(b, s[..7]);
409 }
410 
411 #[test]
fns_defined_for_bytes_mut()412 fn fns_defined_for_bytes_mut() {
413     let mut bytes = BytesMut::from(&b"hello world"[..]);
414 
415     let _ = bytes.as_ptr();
416     let _ = bytes.as_mut_ptr();
417 
418     // Iterator
419     let v: Vec<u8> = bytes.as_ref().iter().cloned().collect();
420     assert_eq!(&v[..], bytes);
421 }
422 
423 #[test]
reserve_convert()424 fn reserve_convert() {
425     // Vec -> Vec
426     let mut bytes = BytesMut::from(LONG);
427     bytes.reserve(64);
428     assert_eq!(bytes.capacity(), LONG.len() + 64);
429 
430     // Arc -> Vec
431     let mut bytes = BytesMut::from(LONG);
432     let a = bytes.split_to(30);
433 
434     bytes.reserve(128);
435     assert!(bytes.capacity() >= bytes.len() + 128);
436 
437     drop(a);
438 }
439 
440 #[test]
reserve_growth()441 fn reserve_growth() {
442     let mut bytes = BytesMut::with_capacity(64);
443     bytes.put("hello world".as_bytes());
444     let _ = bytes.split();
445 
446     bytes.reserve(65);
447     assert_eq!(bytes.capacity(), 117);
448 }
449 
450 #[test]
reserve_allocates_at_least_original_capacity()451 fn reserve_allocates_at_least_original_capacity() {
452     let mut bytes = BytesMut::with_capacity(1024);
453 
454     for i in 0..1020 {
455         bytes.put_u8(i as u8);
456     }
457 
458     let _other = bytes.split();
459 
460     bytes.reserve(16);
461     assert_eq!(bytes.capacity(), 1024);
462 }
463 
464 #[test]
465 #[cfg_attr(miri, ignore)] // Miri is too slow
reserve_max_original_capacity_value()466 fn reserve_max_original_capacity_value() {
467     const SIZE: usize = 128 * 1024;
468 
469     let mut bytes = BytesMut::with_capacity(SIZE);
470 
471     for _ in 0..SIZE {
472         bytes.put_u8(0u8);
473     }
474 
475     let _other = bytes.split();
476 
477     bytes.reserve(16);
478     assert_eq!(bytes.capacity(), 64 * 1024);
479 }
480 
481 #[test]
reserve_vec_recycling()482 fn reserve_vec_recycling() {
483     let mut bytes = BytesMut::with_capacity(16);
484     assert_eq!(bytes.capacity(), 16);
485     let addr = bytes.as_ptr() as usize;
486     bytes.put("0123456789012345".as_bytes());
487     assert_eq!(bytes.as_ptr() as usize, addr);
488     bytes.advance(10);
489     assert_eq!(bytes.capacity(), 6);
490     bytes.reserve(8);
491     assert_eq!(bytes.capacity(), 16);
492     assert_eq!(bytes.as_ptr() as usize, addr);
493 }
494 
495 #[test]
reserve_in_arc_unique_does_not_overallocate()496 fn reserve_in_arc_unique_does_not_overallocate() {
497     let mut bytes = BytesMut::with_capacity(1000);
498     let _ = bytes.split();
499 
500     // now bytes is Arc and refcount == 1
501 
502     assert_eq!(1000, bytes.capacity());
503     bytes.reserve(2001);
504     assert_eq!(2001, bytes.capacity());
505 }
506 
507 #[test]
reserve_in_arc_unique_doubles()508 fn reserve_in_arc_unique_doubles() {
509     let mut bytes = BytesMut::with_capacity(1000);
510     let _ = bytes.split();
511 
512     // now bytes is Arc and refcount == 1
513 
514     assert_eq!(1000, bytes.capacity());
515     bytes.reserve(1001);
516     assert_eq!(2000, bytes.capacity());
517 }
518 
519 #[test]
reserve_in_arc_unique_does_not_overallocate_after_split()520 fn reserve_in_arc_unique_does_not_overallocate_after_split() {
521     let mut bytes = BytesMut::from(LONG);
522     let orig_capacity = bytes.capacity();
523     drop(bytes.split_off(LONG.len() / 2));
524 
525     // now bytes is Arc and refcount == 1
526 
527     let new_capacity = bytes.capacity();
528     bytes.reserve(orig_capacity - new_capacity);
529     assert_eq!(bytes.capacity(), orig_capacity);
530 }
531 
532 #[test]
reserve_in_arc_unique_does_not_overallocate_after_multiple_splits()533 fn reserve_in_arc_unique_does_not_overallocate_after_multiple_splits() {
534     let mut bytes = BytesMut::from(LONG);
535     let orig_capacity = bytes.capacity();
536     for _ in 0..10 {
537         drop(bytes.split_off(LONG.len() / 2));
538 
539         // now bytes is Arc and refcount == 1
540 
541         let new_capacity = bytes.capacity();
542         bytes.reserve(orig_capacity - new_capacity);
543     }
544     assert_eq!(bytes.capacity(), orig_capacity);
545 }
546 
547 #[test]
reserve_in_arc_nonunique_does_not_overallocate()548 fn reserve_in_arc_nonunique_does_not_overallocate() {
549     let mut bytes = BytesMut::with_capacity(1000);
550     let _copy = bytes.split();
551 
552     // now bytes is Arc and refcount == 2
553 
554     assert_eq!(1000, bytes.capacity());
555     bytes.reserve(2001);
556     assert_eq!(2001, bytes.capacity());
557 }
558 
559 /// This function tests `BytesMut::reserve_inner`, where `BytesMut` holds
560 /// a unique reference to the shared vector and decide to reuse it
561 /// by reallocating the `Vec`.
562 #[test]
reserve_shared_reuse()563 fn reserve_shared_reuse() {
564     let mut bytes = BytesMut::with_capacity(1000);
565     bytes.put_slice(b"Hello, World!");
566     drop(bytes.split());
567 
568     bytes.put_slice(b"!123ex123,sadchELLO,_wORLD!");
569     // Use split_off so that v.capacity() - self.cap != off
570     drop(bytes.split_off(9));
571     assert_eq!(&*bytes, b"!123ex123");
572 
573     bytes.reserve(2000);
574     assert_eq!(&*bytes, b"!123ex123");
575     assert_eq!(bytes.capacity(), 2009);
576 }
577 
578 #[test]
extend_mut()579 fn extend_mut() {
580     let mut bytes = BytesMut::with_capacity(0);
581     bytes.extend(LONG);
582     assert_eq!(*bytes, LONG[..]);
583 }
584 
585 #[test]
extend_from_slice_mut()586 fn extend_from_slice_mut() {
587     for &i in &[3, 34] {
588         let mut bytes = BytesMut::new();
589         bytes.extend_from_slice(&LONG[..i]);
590         bytes.extend_from_slice(&LONG[i..]);
591         assert_eq!(LONG[..], *bytes);
592     }
593 }
594 
595 #[test]
extend_mut_from_bytes()596 fn extend_mut_from_bytes() {
597     let mut bytes = BytesMut::with_capacity(0);
598     bytes.extend([Bytes::from(LONG)]);
599     assert_eq!(*bytes, LONG[..]);
600 }
601 
602 #[test]
extend_mut_without_size_hint()603 fn extend_mut_without_size_hint() {
604     let mut bytes = BytesMut::with_capacity(0);
605     let mut long_iter = LONG.iter();
606 
607     // Use iter::from_fn since it doesn't know a size_hint
608     bytes.extend(std::iter::from_fn(|| long_iter.next()));
609     assert_eq!(*bytes, LONG[..]);
610 }
611 
612 #[test]
from_static()613 fn from_static() {
614     let mut a = Bytes::from_static(b"ab");
615     let b = a.split_off(1);
616 
617     assert_eq!(a, b"a"[..]);
618     assert_eq!(b, b"b"[..]);
619 }
620 
621 #[test]
advance_static()622 fn advance_static() {
623     let mut a = Bytes::from_static(b"hello world");
624     a.advance(6);
625     assert_eq!(a, &b"world"[..]);
626 }
627 
628 #[test]
advance_vec()629 fn advance_vec() {
630     let mut a = Bytes::from(b"hello world boooo yah world zomg wat wat".to_vec());
631     a.advance(16);
632     assert_eq!(a, b"o yah world zomg wat wat"[..]);
633 
634     a.advance(4);
635     assert_eq!(a, b"h world zomg wat wat"[..]);
636 
637     a.advance(6);
638     assert_eq!(a, b"d zomg wat wat"[..]);
639 }
640 
641 #[test]
advance_bytes_mut()642 fn advance_bytes_mut() {
643     let mut a = BytesMut::from("hello world boooo yah world zomg wat wat");
644     a.advance(16);
645     assert_eq!(a, b"o yah world zomg wat wat"[..]);
646 
647     a.advance(4);
648     assert_eq!(a, b"h world zomg wat wat"[..]);
649 
650     // Reserve some space.
651     a.reserve(1024);
652     assert_eq!(a, b"h world zomg wat wat"[..]);
653 
654     a.advance(6);
655     assert_eq!(a, b"d zomg wat wat"[..]);
656 }
657 
658 #[test]
659 #[should_panic]
advance_past_len()660 fn advance_past_len() {
661     let mut a = BytesMut::from("hello world");
662     a.advance(20);
663 }
664 
665 #[test]
666 // Only run these tests on little endian systems. CI uses qemu for testing
667 // big endian... and qemu doesn't really support threading all that well.
668 #[cfg(any(miri, target_endian = "little"))]
stress()669 fn stress() {
670     // Tests promoting a buffer from a vec -> shared in a concurrent situation
671     use std::sync::{Arc, Barrier};
672     use std::thread;
673 
674     const THREADS: usize = 8;
675     const ITERS: usize = if cfg!(miri) { 100 } else { 1_000 };
676 
677     for i in 0..ITERS {
678         let data = [i as u8; 256];
679         let buf = Arc::new(Bytes::copy_from_slice(&data[..]));
680 
681         let barrier = Arc::new(Barrier::new(THREADS));
682         let mut joins = Vec::with_capacity(THREADS);
683 
684         for _ in 0..THREADS {
685             let c = barrier.clone();
686             let buf = buf.clone();
687 
688             joins.push(thread::spawn(move || {
689                 c.wait();
690                 let buf: Bytes = (*buf).clone();
691                 drop(buf);
692             }));
693         }
694 
695         for th in joins {
696             th.join().unwrap();
697         }
698 
699         assert_eq!(*buf, data[..]);
700     }
701 }
702 
703 #[test]
partial_eq_bytesmut()704 fn partial_eq_bytesmut() {
705     let bytes = Bytes::from(&b"The quick red fox"[..]);
706     let bytesmut = BytesMut::from(&b"The quick red fox"[..]);
707     assert!(bytes == bytesmut);
708     assert!(bytesmut == bytes);
709     let bytes2 = Bytes::from(&b"Jumped over the lazy brown dog"[..]);
710     assert!(bytes2 != bytesmut);
711     assert!(bytesmut != bytes2);
712 }
713 
714 /*
715 #[test]
716 fn bytes_unsplit_basic() {
717     let buf = Bytes::from(&b"aaabbbcccddd"[..]);
718 
719     let splitted = buf.split_off(6);
720     assert_eq!(b"aaabbb", &buf[..]);
721     assert_eq!(b"cccddd", &splitted[..]);
722 
723     buf.unsplit(splitted);
724     assert_eq!(b"aaabbbcccddd", &buf[..]);
725 }
726 
727 #[test]
728 fn bytes_unsplit_empty_other() {
729     let buf = Bytes::from(&b"aaabbbcccddd"[..]);
730 
731     // empty other
732     let other = Bytes::new();
733 
734     buf.unsplit(other);
735     assert_eq!(b"aaabbbcccddd", &buf[..]);
736 }
737 
738 #[test]
739 fn bytes_unsplit_empty_self() {
740     // empty self
741     let mut buf = Bytes::new();
742 
743     let mut other = Bytes::with_capacity(64);
744     other.extend_from_slice(b"aaabbbcccddd");
745 
746     buf.unsplit(other);
747     assert_eq!(b"aaabbbcccddd", &buf[..]);
748 }
749 
750 #[test]
751 fn bytes_unsplit_arc_different() {
752     let mut buf = Bytes::with_capacity(64);
753     buf.extend_from_slice(b"aaaabbbbeeee");
754 
755     buf.split_off(8); //arc
756 
757     let mut buf2 = Bytes::with_capacity(64);
758     buf2.extend_from_slice(b"ccccddddeeee");
759 
760     buf2.split_off(8); //arc
761 
762     buf.unsplit(buf2);
763     assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
764 }
765 
766 #[test]
767 fn bytes_unsplit_arc_non_contiguous() {
768     let mut buf = Bytes::with_capacity(64);
769     buf.extend_from_slice(b"aaaabbbbeeeeccccdddd");
770 
771     let mut buf2 = buf.split_off(8); //arc
772 
773     let buf3 = buf2.split_off(4); //arc
774 
775     buf.unsplit(buf3);
776     assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
777 }
778 
779 #[test]
780 fn bytes_unsplit_two_split_offs() {
781     let mut buf = Bytes::with_capacity(64);
782     buf.extend_from_slice(b"aaaabbbbccccdddd");
783 
784     let mut buf2 = buf.split_off(8); //arc
785     let buf3 = buf2.split_off(4); //arc
786 
787     buf2.unsplit(buf3);
788     buf.unsplit(buf2);
789     assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
790 }
791 
792 #[test]
793 fn bytes_unsplit_overlapping_references() {
794     let mut buf = Bytes::with_capacity(64);
795     buf.extend_from_slice(b"abcdefghijklmnopqrstuvwxyz");
796     let mut buf0010 = buf.slice(0..10);
797     let buf1020 = buf.slice(10..20);
798     let buf0515 = buf.slice(5..15);
799     buf0010.unsplit(buf1020);
800     assert_eq!(b"abcdefghijklmnopqrst", &buf0010[..]);
801     assert_eq!(b"fghijklmno", &buf0515[..]);
802 }
803 */
804 
805 #[test]
bytes_mut_unsplit_basic()806 fn bytes_mut_unsplit_basic() {
807     let mut buf = BytesMut::with_capacity(64);
808     buf.extend_from_slice(b"aaabbbcccddd");
809 
810     let splitted = buf.split_off(6);
811     assert_eq!(b"aaabbb", &buf[..]);
812     assert_eq!(b"cccddd", &splitted[..]);
813 
814     buf.unsplit(splitted);
815     assert_eq!(b"aaabbbcccddd", &buf[..]);
816 }
817 
818 #[test]
bytes_mut_unsplit_empty_other()819 fn bytes_mut_unsplit_empty_other() {
820     let mut buf = BytesMut::with_capacity(64);
821     buf.extend_from_slice(b"aaabbbcccddd");
822 
823     // empty other
824     let other = BytesMut::new();
825 
826     buf.unsplit(other);
827     assert_eq!(b"aaabbbcccddd", &buf[..]);
828 }
829 
830 #[test]
bytes_mut_unsplit_empty_self()831 fn bytes_mut_unsplit_empty_self() {
832     // empty self
833     let mut buf = BytesMut::new();
834 
835     let mut other = BytesMut::with_capacity(64);
836     other.extend_from_slice(b"aaabbbcccddd");
837 
838     buf.unsplit(other);
839     assert_eq!(b"aaabbbcccddd", &buf[..]);
840 }
841 
842 #[test]
bytes_mut_unsplit_other_keeps_capacity()843 fn bytes_mut_unsplit_other_keeps_capacity() {
844     let mut buf = BytesMut::with_capacity(64);
845     buf.extend_from_slice(b"aabb");
846 
847     // non empty other created "from" buf
848     let mut other = buf.split_off(buf.len());
849     other.extend_from_slice(b"ccddee");
850     buf.unsplit(other);
851 
852     assert_eq!(buf.capacity(), 64);
853 }
854 
855 #[test]
bytes_mut_unsplit_empty_other_keeps_capacity()856 fn bytes_mut_unsplit_empty_other_keeps_capacity() {
857     let mut buf = BytesMut::with_capacity(64);
858     buf.extend_from_slice(b"aabbccddee");
859 
860     // empty other created "from" buf
861     let other = buf.split_off(buf.len());
862     buf.unsplit(other);
863 
864     assert_eq!(buf.capacity(), 64);
865 }
866 
867 #[test]
bytes_mut_unsplit_arc_different()868 fn bytes_mut_unsplit_arc_different() {
869     let mut buf = BytesMut::with_capacity(64);
870     buf.extend_from_slice(b"aaaabbbbeeee");
871 
872     let _ = buf.split_off(8); //arc
873 
874     let mut buf2 = BytesMut::with_capacity(64);
875     buf2.extend_from_slice(b"ccccddddeeee");
876 
877     let _ = buf2.split_off(8); //arc
878 
879     buf.unsplit(buf2);
880     assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
881 }
882 
883 #[test]
bytes_mut_unsplit_arc_non_contiguous()884 fn bytes_mut_unsplit_arc_non_contiguous() {
885     let mut buf = BytesMut::with_capacity(64);
886     buf.extend_from_slice(b"aaaabbbbeeeeccccdddd");
887 
888     let mut buf2 = buf.split_off(8); //arc
889 
890     let buf3 = buf2.split_off(4); //arc
891 
892     buf.unsplit(buf3);
893     assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
894 }
895 
896 #[test]
bytes_mut_unsplit_two_split_offs()897 fn bytes_mut_unsplit_two_split_offs() {
898     let mut buf = BytesMut::with_capacity(64);
899     buf.extend_from_slice(b"aaaabbbbccccdddd");
900 
901     let mut buf2 = buf.split_off(8); //arc
902     let buf3 = buf2.split_off(4); //arc
903 
904     buf2.unsplit(buf3);
905     buf.unsplit(buf2);
906     assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
907 }
908 
909 #[test]
from_iter_no_size_hint()910 fn from_iter_no_size_hint() {
911     use std::iter;
912 
913     let mut expect = vec![];
914 
915     let actual: Bytes = iter::repeat(b'x')
916         .scan(100, |cnt, item| {
917             if *cnt >= 1 {
918                 *cnt -= 1;
919                 expect.push(item);
920                 Some(item)
921             } else {
922                 None
923             }
924         })
925         .collect();
926 
927     assert_eq!(&actual[..], &expect[..]);
928 }
929 
test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8])930 fn test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8]) {
931     let slice = &(bytes.as_ref()[start..end]);
932     let sub = bytes.slice_ref(slice);
933     assert_eq!(&sub[..], expected);
934 }
935 
936 #[test]
slice_ref_works()937 fn slice_ref_works() {
938     let bytes = Bytes::from(&b"012345678"[..]);
939 
940     test_slice_ref(&bytes, 0, 0, b"");
941     test_slice_ref(&bytes, 0, 3, b"012");
942     test_slice_ref(&bytes, 2, 6, b"2345");
943     test_slice_ref(&bytes, 7, 9, b"78");
944     test_slice_ref(&bytes, 9, 9, b"");
945 }
946 
947 #[test]
slice_ref_empty()948 fn slice_ref_empty() {
949     let bytes = Bytes::from(&b""[..]);
950     let slice = &(bytes.as_ref()[0..0]);
951 
952     let sub = bytes.slice_ref(slice);
953     assert_eq!(&sub[..], b"");
954 }
955 
956 #[test]
slice_ref_empty_subslice()957 fn slice_ref_empty_subslice() {
958     let bytes = Bytes::from(&b"abcde"[..]);
959     let subbytes = bytes.slice(0..0);
960     let slice = &subbytes[..];
961     // The `slice` object is derived from the original `bytes` object
962     // so `slice_ref` should work.
963     assert_eq!(Bytes::new(), bytes.slice_ref(slice));
964 }
965 
966 #[test]
967 #[should_panic]
slice_ref_catches_not_a_subset()968 fn slice_ref_catches_not_a_subset() {
969     let bytes = Bytes::from(&b"012345678"[..]);
970     let slice = &b"012345"[0..4];
971 
972     bytes.slice_ref(slice);
973 }
974 
975 #[test]
slice_ref_not_an_empty_subset()976 fn slice_ref_not_an_empty_subset() {
977     let bytes = Bytes::from(&b"012345678"[..]);
978     let slice = &b""[0..0];
979 
980     assert_eq!(Bytes::new(), bytes.slice_ref(slice));
981 }
982 
983 #[test]
empty_slice_ref_not_an_empty_subset()984 fn empty_slice_ref_not_an_empty_subset() {
985     let bytes = Bytes::new();
986     let slice = &b"some other slice"[0..0];
987 
988     assert_eq!(Bytes::new(), bytes.slice_ref(slice));
989 }
990 
991 #[test]
bytes_buf_mut_advance()992 fn bytes_buf_mut_advance() {
993     let mut bytes = BytesMut::with_capacity(1024);
994 
995     unsafe {
996         let ptr = bytes.chunk_mut().as_mut_ptr();
997         assert_eq!(1024, bytes.chunk_mut().len());
998 
999         bytes.advance_mut(10);
1000 
1001         let next = bytes.chunk_mut().as_mut_ptr();
1002         assert_eq!(1024 - 10, bytes.chunk_mut().len());
1003         assert_eq!(ptr.offset(10), next);
1004 
1005         // advance to the end
1006         bytes.advance_mut(1024 - 10);
1007 
1008         // The buffer size is doubled
1009         assert_eq!(1024, bytes.chunk_mut().len());
1010     }
1011 }
1012 
1013 #[test]
bytes_buf_mut_reuse_when_fully_consumed()1014 fn bytes_buf_mut_reuse_when_fully_consumed() {
1015     use bytes::{Buf, BytesMut};
1016     let mut buf = BytesMut::new();
1017     buf.reserve(8192);
1018     buf.extend_from_slice(&[0u8; 100][..]);
1019 
1020     let p = &buf[0] as *const u8;
1021     buf.advance(100);
1022 
1023     buf.reserve(8192);
1024     buf.extend_from_slice(b" ");
1025 
1026     assert_eq!(&buf[0] as *const u8, p);
1027 }
1028 
1029 #[test]
1030 #[should_panic]
bytes_reserve_overflow()1031 fn bytes_reserve_overflow() {
1032     let mut bytes = BytesMut::with_capacity(1024);
1033     bytes.put_slice(b"hello world");
1034 
1035     bytes.reserve(usize::MAX);
1036 }
1037 
1038 #[test]
bytes_with_capacity_but_empty()1039 fn bytes_with_capacity_but_empty() {
1040     // See https://github.com/tokio-rs/bytes/issues/340
1041     let vec = Vec::with_capacity(1);
1042     let _ = Bytes::from(vec);
1043 }
1044 
1045 #[test]
bytes_put_bytes()1046 fn bytes_put_bytes() {
1047     let mut bytes = BytesMut::new();
1048     bytes.put_u8(17);
1049     bytes.put_bytes(19, 2);
1050     assert_eq!([17, 19, 19], bytes.as_ref());
1051 }
1052 
1053 #[test]
box_slice_empty()1054 fn box_slice_empty() {
1055     // See https://github.com/tokio-rs/bytes/issues/340
1056     let empty: Box<[u8]> = Default::default();
1057     let b = Bytes::from(empty);
1058     assert!(b.is_empty());
1059 }
1060 
1061 #[test]
bytes_into_vec()1062 fn bytes_into_vec() {
1063     // Test kind == KIND_VEC
1064     let content = b"helloworld";
1065 
1066     let mut bytes = BytesMut::new();
1067     bytes.put_slice(content);
1068 
1069     let vec: Vec<u8> = bytes.into();
1070     assert_eq!(&vec, content);
1071 
1072     // Test kind == KIND_ARC, shared.is_unique() == True
1073     let mut bytes = BytesMut::new();
1074     bytes.put_slice(b"abcdewe23");
1075     bytes.put_slice(content);
1076 
1077     // Overwrite the bytes to make sure only one reference to the underlying
1078     // Vec exists.
1079     bytes = bytes.split_off(9);
1080 
1081     let vec: Vec<u8> = bytes.into();
1082     assert_eq!(&vec, content);
1083 
1084     // Test kind == KIND_ARC, shared.is_unique() == False
1085     let prefix = b"abcdewe23";
1086 
1087     let mut bytes = BytesMut::new();
1088     bytes.put_slice(prefix);
1089     bytes.put_slice(content);
1090 
1091     let vec: Vec<u8> = bytes.split_off(prefix.len()).into();
1092     assert_eq!(&vec, content);
1093 
1094     let vec: Vec<u8> = bytes.into();
1095     assert_eq!(&vec, prefix);
1096 }
1097 
1098 #[test]
test_bytes_into_vec()1099 fn test_bytes_into_vec() {
1100     // Test STATIC_VTABLE.to_vec
1101     let bs = b"1b23exfcz3r";
1102     let vec: Vec<u8> = Bytes::from_static(bs).into();
1103     assert_eq!(&*vec, bs);
1104 
1105     // Test bytes_mut.SHARED_VTABLE.to_vec impl
1106     eprintln!("1");
1107     let mut bytes_mut: BytesMut = bs[..].into();
1108 
1109     // Set kind to KIND_ARC so that after freeze, Bytes will use bytes_mut.SHARED_VTABLE
1110     eprintln!("2");
1111     drop(bytes_mut.split_off(bs.len()));
1112 
1113     eprintln!("3");
1114     let b1 = bytes_mut.freeze();
1115     eprintln!("4");
1116     let b2 = b1.clone();
1117 
1118     eprintln!("{:#?}", (&*b1).as_ptr());
1119 
1120     // shared.is_unique() = False
1121     eprintln!("5");
1122     assert_eq!(&*Vec::from(b2), bs);
1123 
1124     // shared.is_unique() = True
1125     eprintln!("6");
1126     assert_eq!(&*Vec::from(b1), bs);
1127 
1128     // Test bytes_mut.SHARED_VTABLE.to_vec impl where offset != 0
1129     let mut bytes_mut1: BytesMut = bs[..].into();
1130     let bytes_mut2 = bytes_mut1.split_off(9);
1131 
1132     let b1 = bytes_mut1.freeze();
1133     let b2 = bytes_mut2.freeze();
1134 
1135     assert_eq!(Vec::from(b2), bs[9..]);
1136     assert_eq!(Vec::from(b1), bs[..9]);
1137 }
1138 
1139 #[test]
test_bytes_into_vec_promotable_even()1140 fn test_bytes_into_vec_promotable_even() {
1141     let vec = vec![33u8; 1024];
1142 
1143     // Test cases where kind == KIND_VEC
1144     let b1 = Bytes::from(vec.clone());
1145     assert_eq!(Vec::from(b1), vec);
1146 
1147     // Test cases where kind == KIND_ARC, ref_cnt == 1
1148     let b1 = Bytes::from(vec.clone());
1149     drop(b1.clone());
1150     assert_eq!(Vec::from(b1), vec);
1151 
1152     // Test cases where kind == KIND_ARC, ref_cnt == 2
1153     let b1 = Bytes::from(vec.clone());
1154     let b2 = b1.clone();
1155     assert_eq!(Vec::from(b1), vec);
1156 
1157     // Test cases where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1
1158     assert_eq!(Vec::from(b2), vec);
1159 
1160     // Test cases where offset != 0
1161     let mut b1 = Bytes::from(vec.clone());
1162     let b2 = b1.split_off(20);
1163 
1164     assert_eq!(Vec::from(b2), vec[20..]);
1165     assert_eq!(Vec::from(b1), vec[..20]);
1166 }
1167 
1168 #[test]
test_bytes_vec_conversion()1169 fn test_bytes_vec_conversion() {
1170     let mut vec = Vec::with_capacity(10);
1171     vec.extend(b"abcdefg");
1172     let b = Bytes::from(vec);
1173     let v = Vec::from(b);
1174     assert_eq!(v.len(), 7);
1175     assert_eq!(v.capacity(), 10);
1176 
1177     let mut b = Bytes::from(v);
1178     b.advance(1);
1179     let v = Vec::from(b);
1180     assert_eq!(v.len(), 6);
1181     assert_eq!(v.capacity(), 10);
1182     assert_eq!(v.as_slice(), b"bcdefg");
1183 }
1184 
1185 #[test]
test_bytes_mut_conversion()1186 fn test_bytes_mut_conversion() {
1187     let mut b1 = BytesMut::with_capacity(10);
1188     b1.extend(b"abcdefg");
1189     let b2 = Bytes::from(b1);
1190     let v = Vec::from(b2);
1191     assert_eq!(v.len(), 7);
1192     assert_eq!(v.capacity(), 10);
1193 
1194     let mut b = Bytes::from(v);
1195     b.advance(1);
1196     let v = Vec::from(b);
1197     assert_eq!(v.len(), 6);
1198     assert_eq!(v.capacity(), 10);
1199     assert_eq!(v.as_slice(), b"bcdefg");
1200 }
1201 
1202 #[test]
test_bytes_capacity_len()1203 fn test_bytes_capacity_len() {
1204     for cap in 0..100 {
1205         for len in 0..=cap {
1206             let mut v = Vec::with_capacity(cap);
1207             v.resize(len, 0);
1208             let _ = Bytes::from(v);
1209         }
1210     }
1211 }
1212