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