Skip to content

Commit

Permalink
Improve testing of shrink_to_fit
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 26, 2024
1 parent b843ae2 commit bc8c761
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions arrow-buffer/src/buffer/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,15 +581,25 @@ mod tests {
assert_eq!(original.capacity(), 64);

let slice = original.slice(3);
drop(original); // Make sure the buffer isn't shared (or shrink_to_fit won't work)
assert_eq!(slice.len(), 4);
assert_eq!(slice.capacity(), 64);

drop(original);

let mut shrunk = slice;
shrunk.shrink_to_fit();
assert_eq!(shrunk.len(), 4);
assert_eq!(shrunk.capacity(), 4);

// Test that we can handle empty slices:
let empty_slice = shrunk.slice(4);
drop(shrunk); // Make sure the buffer isn't shared (or shrink_to_fit won't work)
assert_eq!(empty_slice.len(), 0);
assert_eq!(empty_slice.capacity(), 4);

let mut shrunk_empty = empty_slice;
shrunk_empty.shrink_to_fit();
assert_eq!(shrunk_empty.len(), 0);
assert_eq!(shrunk_empty.capacity(), 1); // `Buffer` and `Bytes` doesn't support 0-capacity, so we shrink to 1
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion arrow/tests/shrink_to_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn test_shrink_to_fit_after_concat() {
);
assert!(
bytes_allocated_by_this_thread <= expected_len + expected_len / 100,
"We shouldn't have more than 1% memory overhead. In fact, we are using {bytes_allocated_by_this_thread}B of memory for {expected_len}B of data"
"We shouldn't have more than 1% memory overhead. In fact, we are using {bytes_allocated_by_this_thread} B of memory for {expected_len} B of data"
);
}

Expand Down

0 comments on commit bc8c761

Please sign in to comment.