Last active
June 24, 2021 17:33
-
-
Save JasonLG1979/69ed65d91c64f39de4435b3720258d02 to your computer and use it in GitHub Desktop.
Test to see if Vec capacity can be trusted in Rust.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
// This may take a while to run... | |
let ten_mega_bytes: usize = 10_000_001; | |
println!("Testing Vec `with_capacity`, `reserve`, and `reserve_exact` in range 0..{} (ten megabytes)", ten_mega_bytes); | |
for i in 0..ten_mega_bytes { | |
let mut with_capacity: Vec<u8> = Vec::with_capacity(i); | |
let mut reserve: Vec<u8> = Vec::new(); | |
reserve.reserve(i); | |
let mut reserve_exact: Vec<u8> = Vec::new(); | |
reserve_exact.reserve_exact(i); | |
let with_cap_capacity = with_capacity.capacity(); | |
let reserve_capacity = reserve.capacity(); | |
let reserve_exact_capacity = reserve_exact.capacity(); | |
if with_cap_capacity != i { | |
println!("Setting capacity with `with_capacity` failed, expected {}, but got: {}", i, with_cap_capacity); | |
} | |
if reserve_capacity != i { | |
println!("Setting capacity with `reserve` failed, expected {}, but got: {}", i, reserve_capacity); | |
} | |
if reserve_exact_capacity != i { | |
println!("Setting capacity with `reserve_exact` failed, expected {}, but got: {}", i, reserve_exact_capacity); | |
} | |
with_capacity.shrink_to_fit(); | |
reserve.shrink_to_fit(); | |
reserve_exact.shrink_to_fit(); | |
with_capacity.reserve_exact(i); | |
reserve.reserve_exact(i); | |
reserve_exact.reserve_exact(i); | |
let with_cap_capacity = with_capacity.capacity(); | |
let reserve_capacity = reserve.capacity(); | |
let reserve_exact_capacity = reserve_exact.capacity(); | |
if with_cap_capacity != i { | |
println!("Setting capacity with `reserve_exact` after shirnk failed, expected {}, but got: {}", i, with_cap_capacity); | |
} | |
if reserve_capacity != i { | |
println!("Setting capacity with `reserve_exact` after shirnk failed, expected {}, but got: {}", i, reserve_capacity); | |
} | |
if reserve_exact_capacity != i { | |
println!("Setting capacity with `reserve_exact` after shirnk failed, expected {}, but got: {}", i, reserve_exact_capacity); | |
} | |
with_capacity.shrink_to_fit(); | |
reserve.shrink_to_fit(); | |
reserve_exact.shrink_to_fit(); | |
with_capacity.reserve(i); | |
reserve.reserve(i); | |
reserve_exact.reserve(i); | |
let with_cap_capacity = with_capacity.capacity(); | |
let reserve_capacity = reserve.capacity(); | |
let reserve_exact_capacity = reserve_exact.capacity(); | |
if with_cap_capacity != i { | |
println!("Setting capacity with `reserve` after shirnk failed, expected {}, but got: {}", i, with_cap_capacity); | |
} | |
if reserve_capacity != i { | |
println!("Setting capacity with `reserve` after shirnk failed, expected {}, but got: {}", i, reserve_capacity); | |
} | |
if reserve_exact_capacity != i { | |
println!("Setting capacity with `reserve` after shirnk failed, expected {}, but got: {}", i, reserve_exact_capacity); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment