| 690 | |
| 691 | #[crate::test] |
| 692 | fn test_last_segment_empty() { |
| 693 | let segments = vec![vec![1, 2], vec![3, 4, 5, 6], vec![]]; |
| 694 | let mut s: SegmentedBytes = segments.clone().into_iter().collect(); |
| 695 | |
| 696 | assert_eq!(s.len(), 6); |
| 697 | assert_eq!(s.remaining(), 6); |
| 698 | |
| 699 | // Read and advanced past the first chunk. |
| 700 | let first_chunk = s.chunk(); |
| 701 | assert_eq!(first_chunk, [1, 2]); |
| 702 | s.advance(first_chunk.len()); |
| 703 | |
| 704 | assert_eq!(s.remaining(), 4); |
| 705 | |
| 706 | // Read and advance past the second chunk. |
| 707 | let second_chunk = s.chunk(); |
| 708 | assert_eq!(second_chunk, [3, 4, 5, 6]); |
| 709 | s.advance(second_chunk.len()); |
| 710 | |
| 711 | // No bytes should remain. |
| 712 | assert_eq!(s.remaining(), 0); |
| 713 | assert!(s.chunk().is_empty()); |
| 714 | |
| 715 | // Recreate SegmentedBytes. |
| 716 | let s: SegmentedBytes = segments.into_iter().collect(); |
| 717 | let mut reader = s.reader(); |
| 718 | |
| 719 | // We should be able to read the first chunk. |
| 720 | let mut buf = [0; 4]; |
| 721 | let bytes_read = reader.read(&mut buf).unwrap(); |
| 722 | assert_eq!(bytes_read, 2); |
| 723 | assert_eq!(buf, [1, 2, 0, 0]); |
| 724 | |
| 725 | // And we should be able to read the second chunk without issue. |
| 726 | let bytes_read = reader.read(&mut buf).unwrap(); |
| 727 | assert_eq!(bytes_read, 4); |
| 728 | assert_eq!(buf, [3, 4, 5, 6]); |
| 729 | |
| 730 | // Reading again shouldn't provide any bytes. |
| 731 | let bytes_read = reader.read(&mut buf).unwrap(); |
| 732 | assert_eq!(bytes_read, 0); |
| 733 | // Buffer shouldn't change. |
| 734 | assert_eq!(buf, [3, 4, 5, 6]); |
| 735 | |
| 736 | // Seek backwards and read again. |
| 737 | reader.seek(SeekFrom::Current(-2)).unwrap(); |
| 738 | let bytes_read = reader.read(&mut buf).unwrap(); |
| 739 | assert_eq!(bytes_read, 2); |
| 740 | assert_eq!(buf, [5, 6, 5, 6]); |
| 741 | } |
| 742 | |
| 743 | #[crate::test] |
| 744 | #[cfg_attr(miri, ignore)] // slow |