MCPcopy Create free account
hub / github.com/apache/arrow-rs / test_shrink_to_fit

Function test_shrink_to_fit

arrow-buffer/src/buffer/immutable.rs:698–723  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

696
697 #[test]
698 fn test_shrink_to_fit() {
699 let original = Buffer::from(&[0, 1, 2, 3, 4, 5, 6, 7]);
700 assert_eq!(original.as_slice(), &[0, 1, 2, 3, 4, 5, 6, 7]);
701 assert_eq!(original.capacity(), 64);
702
703 let slice = original.slice_with_length(2, 3);
704 drop(original); // Make sure the buffer isn't shared (or shrink_to_fit won't work)
705 assert_eq!(slice.as_slice(), &[2, 3, 4]);
706 assert_eq!(slice.capacity(), 64);
707
708 let mut shrunk = slice;
709 shrunk.shrink_to_fit();
710 assert_eq!(shrunk.as_slice(), &[2, 3, 4]);
711 assert_eq!(shrunk.capacity(), 5); // shrink_to_fit is allowed to keep the elements before the offset
712
713 // Test that we can handle empty slices:
714 let empty_slice = shrunk.slice_with_length(1, 0);
715 drop(shrunk); // Make sure the buffer isn't shared (or shrink_to_fit won't work)
716 assert_eq!(empty_slice.as_slice(), &[]);
717 assert_eq!(empty_slice.capacity(), 5);
718
719 let mut shrunk_empty = empty_slice;
720 shrunk_empty.shrink_to_fit();
721 assert_eq!(shrunk_empty.as_slice(), &[]);
722 assert_eq!(shrunk_empty.capacity(), 0);
723 }
724
725 #[test]
726 #[should_panic(expected = "the offset of the new Buffer cannot exceed the existing length")]

Callers

nothing calls this directly

Calls 2

slice_with_lengthMethod · 0.80
shrink_to_fitMethod · 0.45

Tested by

no test coverage detected