1 #![warn(rust_2018_idioms)]
2
3 use bytes::{Buf, BufMut, Bytes, BytesMut};
4
5 use std::usize;
6
7 const LONG: &'static [u8] = b"mary had a little lamb, little lamb, little lamb";
8 const SHORT: &'static [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]
split_off_to_at_gt_len()291 fn 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]
truncate()315 fn 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]
freeze_clone_shared()327 fn 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]
freeze_clone_unique()336 fn 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]
freeze_after_advance()345 fn 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]
freeze_after_advance_arc()357 fn 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]
freeze_after_split_to()369 fn 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]
freeze_after_truncate()379 fn 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]
freeze_after_truncate_arc()389 fn 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]
freeze_after_split_off()401 fn 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]
fns_defined_for_bytes_mut()411 fn fns_defined_for_bytes_mut() {
412 let mut bytes = BytesMut::from(&b"hello world"[..]);
413
414 bytes.as_ptr();
415 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]
reserve_convert()423 fn 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]
reserve_growth()440 fn 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(), 128);
447 }
448
449 #[test]
reserve_allocates_at_least_original_capacity()450 fn 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
reserve_max_original_capacity_value()465 fn 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]
reserve_vec_recycling()481 fn 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]
reserve_in_arc_unique_does_not_overallocate()495 fn 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]
reserve_in_arc_unique_doubles()507 fn 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]
reserve_in_arc_nonunique_does_not_overallocate()519 fn reserve_in_arc_nonunique_does_not_overallocate() {
520 let mut bytes = BytesMut::with_capacity(1000);
521 let _copy = bytes.split();
522
523 // now bytes is Arc and refcount == 2
524
525 assert_eq!(1000, bytes.capacity());
526 bytes.reserve(2001);
527 assert_eq!(2001, bytes.capacity());
528 }
529
530 #[test]
extend_mut()531 fn extend_mut() {
532 let mut bytes = BytesMut::with_capacity(0);
533 bytes.extend(LONG);
534 assert_eq!(*bytes, LONG[..]);
535 }
536
537 #[test]
extend_from_slice_mut()538 fn extend_from_slice_mut() {
539 for &i in &[3, 34] {
540 let mut bytes = BytesMut::new();
541 bytes.extend_from_slice(&LONG[..i]);
542 bytes.extend_from_slice(&LONG[i..]);
543 assert_eq!(LONG[..], *bytes);
544 }
545 }
546
547 #[test]
extend_mut_without_size_hint()548 fn extend_mut_without_size_hint() {
549 let mut bytes = BytesMut::with_capacity(0);
550 let mut long_iter = LONG.iter();
551
552 // Use iter::from_fn since it doesn't know a size_hint
553 bytes.extend(std::iter::from_fn(|| long_iter.next()));
554 assert_eq!(*bytes, LONG[..]);
555 }
556
557 #[test]
from_static()558 fn from_static() {
559 let mut a = Bytes::from_static(b"ab");
560 let b = a.split_off(1);
561
562 assert_eq!(a, b"a"[..]);
563 assert_eq!(b, b"b"[..]);
564 }
565
566 #[test]
advance_static()567 fn advance_static() {
568 let mut a = Bytes::from_static(b"hello world");
569 a.advance(6);
570 assert_eq!(a, &b"world"[..]);
571 }
572
573 #[test]
advance_vec()574 fn advance_vec() {
575 let mut a = Bytes::from(b"hello world boooo yah world zomg wat wat".to_vec());
576 a.advance(16);
577 assert_eq!(a, b"o yah world zomg wat wat"[..]);
578
579 a.advance(4);
580 assert_eq!(a, b"h world zomg wat wat"[..]);
581
582 a.advance(6);
583 assert_eq!(a, b"d zomg wat wat"[..]);
584 }
585
586 #[test]
advance_bytes_mut()587 fn advance_bytes_mut() {
588 let mut a = BytesMut::from("hello world boooo yah world zomg wat wat");
589 a.advance(16);
590 assert_eq!(a, b"o yah world zomg wat wat"[..]);
591
592 a.advance(4);
593 assert_eq!(a, b"h world zomg wat wat"[..]);
594
595 // Reserve some space.
596 a.reserve(1024);
597 assert_eq!(a, b"h world zomg wat wat"[..]);
598
599 a.advance(6);
600 assert_eq!(a, b"d zomg wat wat"[..]);
601 }
602
603 #[test]
604 #[should_panic]
advance_past_len()605 fn advance_past_len() {
606 let mut a = BytesMut::from("hello world");
607 a.advance(20);
608 }
609
610 #[test]
611 // Only run these tests on little endian systems. CI uses qemu for testing
612 // big endian... and qemu doesn't really support threading all that well.
613 #[cfg(any(miri, target_endian = "little"))]
stress()614 fn stress() {
615 // Tests promoting a buffer from a vec -> shared in a concurrent situation
616 use std::sync::{Arc, Barrier};
617 use std::thread;
618
619 const THREADS: usize = 8;
620 const ITERS: usize = if cfg!(miri) { 100 } else { 1_000 };
621
622 for i in 0..ITERS {
623 let data = [i as u8; 256];
624 let buf = Arc::new(Bytes::copy_from_slice(&data[..]));
625
626 let barrier = Arc::new(Barrier::new(THREADS));
627 let mut joins = Vec::with_capacity(THREADS);
628
629 for _ in 0..THREADS {
630 let c = barrier.clone();
631 let buf = buf.clone();
632
633 joins.push(thread::spawn(move || {
634 c.wait();
635 let buf: Bytes = (*buf).clone();
636 drop(buf);
637 }));
638 }
639
640 for th in joins {
641 th.join().unwrap();
642 }
643
644 assert_eq!(*buf, data[..]);
645 }
646 }
647
648 #[test]
partial_eq_bytesmut()649 fn partial_eq_bytesmut() {
650 let bytes = Bytes::from(&b"The quick red fox"[..]);
651 let bytesmut = BytesMut::from(&b"The quick red fox"[..]);
652 assert!(bytes == bytesmut);
653 assert!(bytesmut == bytes);
654 let bytes2 = Bytes::from(&b"Jumped over the lazy brown dog"[..]);
655 assert!(bytes2 != bytesmut);
656 assert!(bytesmut != bytes2);
657 }
658
659 /*
660 #[test]
661 fn bytes_unsplit_basic() {
662 let buf = Bytes::from(&b"aaabbbcccddd"[..]);
663
664 let splitted = buf.split_off(6);
665 assert_eq!(b"aaabbb", &buf[..]);
666 assert_eq!(b"cccddd", &splitted[..]);
667
668 buf.unsplit(splitted);
669 assert_eq!(b"aaabbbcccddd", &buf[..]);
670 }
671
672 #[test]
673 fn bytes_unsplit_empty_other() {
674 let buf = Bytes::from(&b"aaabbbcccddd"[..]);
675
676 // empty other
677 let other = Bytes::new();
678
679 buf.unsplit(other);
680 assert_eq!(b"aaabbbcccddd", &buf[..]);
681 }
682
683 #[test]
684 fn bytes_unsplit_empty_self() {
685 // empty self
686 let mut buf = Bytes::new();
687
688 let mut other = Bytes::with_capacity(64);
689 other.extend_from_slice(b"aaabbbcccddd");
690
691 buf.unsplit(other);
692 assert_eq!(b"aaabbbcccddd", &buf[..]);
693 }
694
695 #[test]
696 fn bytes_unsplit_arc_different() {
697 let mut buf = Bytes::with_capacity(64);
698 buf.extend_from_slice(b"aaaabbbbeeee");
699
700 buf.split_off(8); //arc
701
702 let mut buf2 = Bytes::with_capacity(64);
703 buf2.extend_from_slice(b"ccccddddeeee");
704
705 buf2.split_off(8); //arc
706
707 buf.unsplit(buf2);
708 assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
709 }
710
711 #[test]
712 fn bytes_unsplit_arc_non_contiguous() {
713 let mut buf = Bytes::with_capacity(64);
714 buf.extend_from_slice(b"aaaabbbbeeeeccccdddd");
715
716 let mut buf2 = buf.split_off(8); //arc
717
718 let buf3 = buf2.split_off(4); //arc
719
720 buf.unsplit(buf3);
721 assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
722 }
723
724 #[test]
725 fn bytes_unsplit_two_split_offs() {
726 let mut buf = Bytes::with_capacity(64);
727 buf.extend_from_slice(b"aaaabbbbccccdddd");
728
729 let mut buf2 = buf.split_off(8); //arc
730 let buf3 = buf2.split_off(4); //arc
731
732 buf2.unsplit(buf3);
733 buf.unsplit(buf2);
734 assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
735 }
736
737 #[test]
738 fn bytes_unsplit_overlapping_references() {
739 let mut buf = Bytes::with_capacity(64);
740 buf.extend_from_slice(b"abcdefghijklmnopqrstuvwxyz");
741 let mut buf0010 = buf.slice(0..10);
742 let buf1020 = buf.slice(10..20);
743 let buf0515 = buf.slice(5..15);
744 buf0010.unsplit(buf1020);
745 assert_eq!(b"abcdefghijklmnopqrst", &buf0010[..]);
746 assert_eq!(b"fghijklmno", &buf0515[..]);
747 }
748 */
749
750 #[test]
bytes_mut_unsplit_basic()751 fn bytes_mut_unsplit_basic() {
752 let mut buf = BytesMut::with_capacity(64);
753 buf.extend_from_slice(b"aaabbbcccddd");
754
755 let splitted = buf.split_off(6);
756 assert_eq!(b"aaabbb", &buf[..]);
757 assert_eq!(b"cccddd", &splitted[..]);
758
759 buf.unsplit(splitted);
760 assert_eq!(b"aaabbbcccddd", &buf[..]);
761 }
762
763 #[test]
bytes_mut_unsplit_empty_other()764 fn bytes_mut_unsplit_empty_other() {
765 let mut buf = BytesMut::with_capacity(64);
766 buf.extend_from_slice(b"aaabbbcccddd");
767
768 // empty other
769 let other = BytesMut::new();
770
771 buf.unsplit(other);
772 assert_eq!(b"aaabbbcccddd", &buf[..]);
773 }
774
775 #[test]
bytes_mut_unsplit_empty_self()776 fn bytes_mut_unsplit_empty_self() {
777 // empty self
778 let mut buf = BytesMut::new();
779
780 let mut other = BytesMut::with_capacity(64);
781 other.extend_from_slice(b"aaabbbcccddd");
782
783 buf.unsplit(other);
784 assert_eq!(b"aaabbbcccddd", &buf[..]);
785 }
786
787 #[test]
bytes_mut_unsplit_arc_different()788 fn bytes_mut_unsplit_arc_different() {
789 let mut buf = BytesMut::with_capacity(64);
790 buf.extend_from_slice(b"aaaabbbbeeee");
791
792 let _ = buf.split_off(8); //arc
793
794 let mut buf2 = BytesMut::with_capacity(64);
795 buf2.extend_from_slice(b"ccccddddeeee");
796
797 let _ = buf2.split_off(8); //arc
798
799 buf.unsplit(buf2);
800 assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
801 }
802
803 #[test]
bytes_mut_unsplit_arc_non_contiguous()804 fn bytes_mut_unsplit_arc_non_contiguous() {
805 let mut buf = BytesMut::with_capacity(64);
806 buf.extend_from_slice(b"aaaabbbbeeeeccccdddd");
807
808 let mut buf2 = buf.split_off(8); //arc
809
810 let buf3 = buf2.split_off(4); //arc
811
812 buf.unsplit(buf3);
813 assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
814 }
815
816 #[test]
bytes_mut_unsplit_two_split_offs()817 fn bytes_mut_unsplit_two_split_offs() {
818 let mut buf = BytesMut::with_capacity(64);
819 buf.extend_from_slice(b"aaaabbbbccccdddd");
820
821 let mut buf2 = buf.split_off(8); //arc
822 let buf3 = buf2.split_off(4); //arc
823
824 buf2.unsplit(buf3);
825 buf.unsplit(buf2);
826 assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
827 }
828
829 #[test]
from_iter_no_size_hint()830 fn from_iter_no_size_hint() {
831 use std::iter;
832
833 let mut expect = vec![];
834
835 let actual: Bytes = iter::repeat(b'x')
836 .scan(100, |cnt, item| {
837 if *cnt >= 1 {
838 *cnt -= 1;
839 expect.push(item);
840 Some(item)
841 } else {
842 None
843 }
844 })
845 .collect();
846
847 assert_eq!(&actual[..], &expect[..]);
848 }
849
test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8])850 fn test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8]) {
851 let slice = &(bytes.as_ref()[start..end]);
852 let sub = bytes.slice_ref(&slice);
853 assert_eq!(&sub[..], expected);
854 }
855
856 #[test]
slice_ref_works()857 fn slice_ref_works() {
858 let bytes = Bytes::from(&b"012345678"[..]);
859
860 test_slice_ref(&bytes, 0, 0, b"");
861 test_slice_ref(&bytes, 0, 3, b"012");
862 test_slice_ref(&bytes, 2, 6, b"2345");
863 test_slice_ref(&bytes, 7, 9, b"78");
864 test_slice_ref(&bytes, 9, 9, b"");
865 }
866
867 #[test]
slice_ref_empty()868 fn slice_ref_empty() {
869 let bytes = Bytes::from(&b""[..]);
870 let slice = &(bytes.as_ref()[0..0]);
871
872 let sub = bytes.slice_ref(&slice);
873 assert_eq!(&sub[..], b"");
874 }
875
876 #[test]
slice_ref_empty_subslice()877 fn slice_ref_empty_subslice() {
878 let bytes = Bytes::from(&b"abcde"[..]);
879 let subbytes = bytes.slice(0..0);
880 let slice = &subbytes[..];
881 // The `slice` object is derived from the original `bytes` object
882 // so `slice_ref` should work.
883 assert_eq!(Bytes::new(), bytes.slice_ref(slice));
884 }
885
886 #[test]
887 #[should_panic]
slice_ref_catches_not_a_subset()888 fn slice_ref_catches_not_a_subset() {
889 let bytes = Bytes::from(&b"012345678"[..]);
890 let slice = &b"012345"[0..4];
891
892 bytes.slice_ref(slice);
893 }
894
895 #[test]
slice_ref_not_an_empty_subset()896 fn slice_ref_not_an_empty_subset() {
897 let bytes = Bytes::from(&b"012345678"[..]);
898 let slice = &b""[0..0];
899
900 assert_eq!(Bytes::new(), bytes.slice_ref(slice));
901 }
902
903 #[test]
empty_slice_ref_not_an_empty_subset()904 fn empty_slice_ref_not_an_empty_subset() {
905 let bytes = Bytes::new();
906 let slice = &b"some other slice"[0..0];
907
908 assert_eq!(Bytes::new(), bytes.slice_ref(slice));
909 }
910
911 #[test]
bytes_buf_mut_advance()912 fn bytes_buf_mut_advance() {
913 let mut bytes = BytesMut::with_capacity(1024);
914
915 unsafe {
916 let ptr = bytes.chunk_mut().as_mut_ptr();
917 assert_eq!(1024, bytes.chunk_mut().len());
918
919 bytes.advance_mut(10);
920
921 let next = bytes.chunk_mut().as_mut_ptr();
922 assert_eq!(1024 - 10, bytes.chunk_mut().len());
923 assert_eq!(ptr.offset(10), next);
924
925 // advance to the end
926 bytes.advance_mut(1024 - 10);
927
928 // The buffer size is doubled
929 assert_eq!(1024, bytes.chunk_mut().len());
930 }
931 }
932
933 #[test]
bytes_buf_mut_reuse_when_fully_consumed()934 fn bytes_buf_mut_reuse_when_fully_consumed() {
935 use bytes::{Buf, BytesMut};
936 let mut buf = BytesMut::new();
937 buf.reserve(8192);
938 buf.extend_from_slice(&[0u8; 100][..]);
939
940 let p = &buf[0] as *const u8;
941 buf.advance(100);
942
943 buf.reserve(8192);
944 buf.extend_from_slice(b" ");
945
946 assert_eq!(&buf[0] as *const u8, p);
947 }
948
949 #[test]
950 #[should_panic]
bytes_reserve_overflow()951 fn bytes_reserve_overflow() {
952 let mut bytes = BytesMut::with_capacity(1024);
953 bytes.put_slice(b"hello world");
954
955 bytes.reserve(usize::MAX);
956 }
957
958 #[test]
bytes_with_capacity_but_empty()959 fn bytes_with_capacity_but_empty() {
960 // See https://github.com/tokio-rs/bytes/issues/340
961 let vec = Vec::with_capacity(1);
962 let _ = Bytes::from(vec);
963 }
964