Use assert_matches! instead of if let {} else

This commit is contained in:
Kornel 2021-08-07 12:51:58 +01:00
parent 215712283f
commit 7dca8eb565
5 changed files with 275 additions and 295 deletions

View file

@ -1,4 +1,5 @@
#![feature(allocator_api)] #![feature(allocator_api)]
#![feature(assert_matches)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(cow_is_borrowed)] #![feature(cow_is_borrowed)]
#![feature(const_cow_is_borrowed)] #![feature(const_cow_is_borrowed)]

View file

@ -1,3 +1,4 @@
use std::assert_matches::assert_matches;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::Cell; use std::cell::Cell;
use std::collections::TryReserveErrorKind::*; use std::collections::TryReserveErrorKind::*;
@ -713,35 +714,32 @@ fn test_try_reserve() {
if guards_against_isize { if guards_against_isize {
// Check isize::MAX + 1 does count as overflow // Check isize::MAX + 1 does count as overflow
if let Err(CapacityOverflow) = assert_matches!(
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
} else { "isize::MAX + 1 should trigger an overflow!"
panic!("isize::MAX + 1 should trigger an overflow!") );
}
// Check usize::MAX does count as overflow // Check usize::MAX does count as overflow
if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()) assert_matches!(
{ empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
} else { Err(CapacityOverflow),
panic!("usize::MAX should trigger an overflow!") "usize::MAX should trigger an overflow!"
} );
} else { } else {
// Check isize::MAX + 1 is an OOM // Check isize::MAX + 1 is an OOM
if let Err(AllocError { .. }) = assert_matches!(
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
{ Err(AllocError { .. }),
} else { "isize::MAX + 1 should trigger an OOM!"
panic!("isize::MAX + 1 should trigger an OOM!") );
}
// Check usize::MAX is an OOM // Check usize::MAX is an OOM
if let Err(AllocError { .. }) = assert_matches!(
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()) empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
{ Err(AllocError { .. }),
} else { "usize::MAX should trigger an OOM!"
panic!("usize::MAX should trigger an OOM!") );
}
} }
} }
@ -756,23 +754,24 @@ fn test_try_reserve() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) assert_matches!(
{ ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) "isize::MAX + 1 should trigger an OOM!"
{ );
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
} }
// Should always overflow in the add-to-len // Should always overflow in the add-to-len
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) { assert_matches!(
} else { ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
panic!("usize::MAX should trigger an overflow!") Err(CapacityOverflow),
} "usize::MAX should trigger an overflow!"
);
} }
} }
@ -801,33 +800,29 @@ fn test_try_reserve_exact() {
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
} else { "isize::MAX + 1 should trigger an overflow!"
panic!("isize::MAX + 1 should trigger an overflow!") );
}
if let Err(CapacityOverflow) = assert_matches!(
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"usize::MAX should trigger an overflow!"
);
} else { } else {
panic!("usize::MAX should trigger an overflow!") assert_matches!(
} empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
if let Err(AllocError { .. }) = assert_matches!(
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
{ Err(AllocError { .. }),
} else { "usize::MAX should trigger an OOM!"
panic!("usize::MAX should trigger an OOM!") );
}
} }
} }
@ -845,25 +840,23 @@ fn test_try_reserve_exact() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
}
if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
{
} else {
panic!("usize::MAX should trigger an overflow!")
} }
assert_matches!(
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
Err(CapacityOverflow),
"usize::MAX should trigger an overflow!"
);
} }
} }

View file

@ -1,3 +1,4 @@
use std::assert_matches::assert_matches;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::Cell; use std::cell::Cell;
use std::collections::TryReserveErrorKind::*; use std::collections::TryReserveErrorKind::*;
@ -1488,34 +1489,32 @@ fn test_try_reserve() {
if guards_against_isize { if guards_against_isize {
// Check isize::MAX + 1 does count as overflow // Check isize::MAX + 1 does count as overflow
if let Err(CapacityOverflow) = assert_matches!(
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
} else { "isize::MAX + 1 should trigger an overflow!"
panic!("isize::MAX + 1 should trigger an overflow!") );
}
// Check usize::MAX does count as overflow // Check usize::MAX does count as overflow
if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) assert_matches!(
{ empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
} else { Err(CapacityOverflow),
panic!("usize::MAX should trigger an overflow!") "usize::MAX should trigger an overflow!"
} );
} else { } else {
// Check isize::MAX + 1 is an OOM // Check isize::MAX + 1 is an OOM
if let Err(AllocError { .. }) = assert_matches!(
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
{ Err(AllocError { .. }),
} else { "isize::MAX + 1 should trigger an OOM!"
panic!("isize::MAX + 1 should trigger an OOM!") );
}
// Check usize::MAX is an OOM // Check usize::MAX is an OOM
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) assert_matches!(
{ empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
panic!("usize::MAX should trigger an OOM!") "usize::MAX should trigger an OOM!"
} );
} }
} }
@ -1530,23 +1529,24 @@ fn test_try_reserve() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) assert_matches!(
{ ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) "isize::MAX + 1 should trigger an OOM!"
{ );
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
} }
// Should always overflow in the add-to-len // Should always overflow in the add-to-len
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) { assert_matches!(
} else { ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
panic!("usize::MAX should trigger an overflow!") Err(CapacityOverflow),
} "usize::MAX should trigger an overflow!"
);
} }
{ {
@ -1562,25 +1562,24 @@ fn test_try_reserve() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()) ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
} }
// Should fail in the mul-by-size // Should fail in the mul-by-size
if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()) { assert_matches!(
} else { ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()),
panic!("usize::MAX should trigger an overflow!"); Err(CapacityOverflow),
} "usize::MAX should trigger an overflow!"
);
} }
} }
@ -1609,33 +1608,29 @@ fn test_try_reserve_exact() {
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
} else { "isize::MAX + 1 should trigger an overflow!"
panic!("isize::MAX + 1 should trigger an overflow!") );
}
if let Err(CapacityOverflow) = assert_matches!(
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"usize::MAX should trigger an overflow!"
);
} else { } else {
panic!("usize::MAX should trigger an overflow!") assert_matches!(
} empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
if let Err(AllocError { .. }) = assert_matches!(
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
{ Err(AllocError { .. }),
} else { "usize::MAX should trigger an OOM!"
panic!("usize::MAX should trigger an OOM!") );
}
} }
} }
@ -1653,25 +1648,23 @@ fn test_try_reserve_exact() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
}
if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
{
} else {
panic!("usize::MAX should trigger an overflow!")
} }
assert_matches!(
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
Err(CapacityOverflow),
"usize::MAX should trigger an overflow!"
);
} }
{ {
@ -1688,26 +1681,23 @@ fn test_try_reserve_exact() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()) ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
}
if let Err(CapacityOverflow) =
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind())
{
} else {
panic!("usize::MAX should trigger an overflow!")
} }
assert_matches!(
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind()),
Err(CapacityOverflow),
"usize::MAX should trigger an overflow!"
);
} }
} }

View file

@ -1,3 +1,4 @@
use std::assert_matches::assert_matches;
use std::collections::TryReserveErrorKind::*; use std::collections::TryReserveErrorKind::*;
use std::collections::{vec_deque::Drain, VecDeque}; use std::collections::{vec_deque::Drain, VecDeque};
use std::fmt::Debug; use std::fmt::Debug;
@ -1181,28 +1182,28 @@ fn test_try_reserve() {
if guards_against_isize { if guards_against_isize {
// Check isize::MAX + 1 does count as overflow // Check isize::MAX + 1 does count as overflow
if let Err(CapacityOverflow) = assert_matches!(
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
} else { "isize::MAX + 1 should trigger an overflow!"
panic!("isize::MAX + 1 should trigger an overflow!") );
}
// Check usize::MAX does count as overflow // Check usize::MAX does count as overflow
if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) assert_matches!(
{ empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
} else { Err(CapacityOverflow),
panic!("usize::MAX should trigger an overflow!") "usize::MAX should trigger an overflow!"
} );
} else { } else {
// Check isize::MAX is an OOM // Check isize::MAX is an OOM
// VecDeque starts with capacity 7, always adds 1 to the capacity // VecDeque starts with capacity 7, always adds 1 to the capacity
// and also rounds the number to next power of 2 so this is the // and also rounds the number to next power of 2 so this is the
// furthest we can go without triggering CapacityOverflow // furthest we can go without triggering CapacityOverflow
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()) { assert_matches!(
} else { empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()),
panic!("isize::MAX + 1 should trigger an OOM!") Err(AllocError { .. }),
} "isize::MAX + 1 should trigger an OOM!"
);
} }
} }
@ -1217,23 +1218,24 @@ fn test_try_reserve() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) assert_matches!(
{ ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) "isize::MAX + 1 should trigger an OOM!"
{ );
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
} }
// Should always overflow in the add-to-len // Should always overflow in the add-to-len
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) { assert_matches!(
} else { ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
panic!("usize::MAX should trigger an overflow!") Err(CapacityOverflow),
} "usize::MAX should trigger an overflow!"
);
} }
{ {
@ -1249,25 +1251,24 @@ fn test_try_reserve() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()) ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
} }
// Should fail in the mul-by-size // Should fail in the mul-by-size
if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()) { assert_matches!(
} else { ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()),
panic!("usize::MAX should trigger an overflow!"); Err(CapacityOverflow),
} "usize::MAX should trigger an overflow!"
);
} }
} }
@ -1296,30 +1297,27 @@ fn test_try_reserve_exact() {
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
} else { "isize::MAX + 1 should trigger an overflow!"
panic!("isize::MAX + 1 should trigger an overflow!") );
}
if let Err(CapacityOverflow) = assert_matches!(
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
} else { "usize::MAX should trigger an overflow!"
panic!("usize::MAX should trigger an overflow!") );
}
} else { } else {
// Check isize::MAX is an OOM // Check isize::MAX is an OOM
// VecDeque starts with capacity 7, always adds 1 to the capacity // VecDeque starts with capacity 7, always adds 1 to the capacity
// and also rounds the number to next power of 2 so this is the // and also rounds the number to next power of 2 so this is the
// furthest we can go without triggering CapacityOverflow // furthest we can go without triggering CapacityOverflow
if let Err(AllocError { .. }) = assert_matches!(
empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()) empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()),
{ Err(AllocError { .. }),
} else { "isize::MAX + 1 should trigger an OOM!"
panic!("isize::MAX + 1 should trigger an OOM!") );
}
} }
} }
@ -1337,25 +1335,23 @@ fn test_try_reserve_exact() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
}
if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
{
} else {
panic!("usize::MAX should trigger an overflow!")
} }
assert_matches!(
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
Err(CapacityOverflow),
"usize::MAX should trigger an overflow!"
);
} }
{ {
@ -1372,26 +1368,23 @@ fn test_try_reserve_exact() {
panic!("isize::MAX shouldn't trigger an overflow!"); panic!("isize::MAX shouldn't trigger an overflow!");
} }
if guards_against_isize { if guards_against_isize {
if let Err(CapacityOverflow) = assert_matches!(
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()) ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
{ Err(CapacityOverflow),
"isize::MAX + 1 should trigger an overflow!"
);
} else { } else {
panic!("isize::MAX + 1 should trigger an overflow!"); assert_matches!(
} ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
} else { Err(AllocError { .. }),
if let Err(AllocError { .. }) = "isize::MAX + 1 should trigger an OOM!"
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()) );
{
} else {
panic!("isize::MAX + 1 should trigger an OOM!")
}
}
if let Err(CapacityOverflow) =
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind())
{
} else {
panic!("usize::MAX should trigger an overflow!")
} }
assert_matches!(
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind()),
Err(CapacityOverflow),
"usize::MAX should trigger an overflow!"
);
} }
} }

View file

@ -1,6 +1,7 @@
use super::Entry::{Occupied, Vacant}; use super::Entry::{Occupied, Vacant};
use super::HashMap; use super::HashMap;
use super::RandomState; use super::RandomState;
use crate::assert_matches::assert_matches;
use crate::cell::RefCell; use crate::cell::RefCell;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use realstd::collections::TryReserveErrorKind::*; use realstd::collections::TryReserveErrorKind::*;
@ -821,15 +822,17 @@ fn test_try_reserve() {
const MAX_USIZE: usize = usize::MAX; const MAX_USIZE: usize = usize::MAX;
if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) { assert_matches!(
} else { empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
panic!("usize::MAX should trigger an overflow!"); Err(CapacityOverflow),
} "usize::MAX should trigger an overflow!"
);
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE / 8).map_err(|e| e.kind()) { assert_matches!(
} else { empty_bytes.try_reserve(MAX_USIZE / 8).map_err(|e| e.kind()),
panic!("usize::MAX / 8 should trigger an OOM!") Err(AllocError { .. }),
} "usize::MAX / 8 should trigger an OOM!"
);
} }
#[test] #[test]