File tree 1 file changed +15
-5
lines changed
library/std/src/collections/hash/map
1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -828,11 +828,21 @@ fn test_try_reserve() {
828
828
"usize::MAX should trigger an overflow!"
829
829
) ;
830
830
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
+ }
836
846
}
837
847
838
848
#[ test]
You can’t perform that action at this time.
0 commit comments