Skip to content

Commit 361a2f9

Browse files
committed
Update HashMap::try_reserve test to version from hashbrown
1 parent 88149d1 commit 361a2f9

File tree

1 file changed

+15
-5
lines changed
  • library/std/src/collections/hash/map

1 file changed

+15
-5
lines changed

library/std/src/collections/hash/map/tests.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -828,11 +828,21 @@ fn test_try_reserve() {
828828
"usize::MAX should trigger an overflow!"
829829
);
830830

831-
assert_matches!(
832-
empty_bytes.try_reserve(MAX_USIZE / 8).map_err(|e| e.kind()),
833-
Err(AllocError { .. }),
834-
"usize::MAX / 8 should trigger an OOM!"
835-
);
831+
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE / 16).map_err(|e| e.kind()) {
832+
} else {
833+
// This may succeed if there is enough free memory. Attempt to
834+
// allocate a few more hashmaps to ensure the allocation will fail.
835+
let mut empty_bytes2: HashMap<u8, u8> = HashMap::new();
836+
let _ = empty_bytes2.try_reserve(MAX_USIZE / 16);
837+
let mut empty_bytes3: HashMap<u8, u8> = HashMap::new();
838+
let _ = empty_bytes3.try_reserve(MAX_USIZE / 16);
839+
let mut empty_bytes4: HashMap<u8, u8> = HashMap::new();
840+
assert_matches!(
841+
empty_bytes4.try_reserve(MAX_USIZE / 16).map_err(|e| e.kind()),
842+
Err(AllocError { .. }),
843+
"usize::MAX / 16 should trigger an OOM!"
844+
);
845+
}
836846
}
837847

838848
#[test]

0 commit comments

Comments
 (0)