Rollup merge of #146424 - ferrocene:pvdrz/improve-ops-coverage, r=workingjubilee

Improve `core::ops` coverage

This PR improves the `core::ops` coverage by adding new tests to `coretests`
This commit is contained in:
Stuart Cook 2025-09-11 14:06:32 +10:00 committed by GitHub
commit 81840271c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 1 deletions

View File

@ -81,6 +81,7 @@
#![feature(next_index)]
#![feature(non_exhaustive_omitted_patterns_lint)]
#![feature(numfmt)]
#![feature(one_sided_range)]
#![feature(option_reduce)]
#![feature(pattern)]
#![feature(peekable_next_if_map)]

View File

@ -2,7 +2,8 @@ mod control_flow;
mod from_residual;
use core::ops::{
Bound, Deref, DerefMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
Bound, Deref, DerefMut, OneSidedRange, OneSidedRangeBound, Range, RangeBounds, RangeFrom,
RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
};
// Test the Range structs and syntax.
@ -70,6 +71,36 @@ fn test_range_to_inclusive() {
let _ = RangeToInclusive { end: 42 };
}
#[test]
fn test_range_contains() {
assert!(!(1u32..5).contains(&0u32));
assert!((1u32..5).contains(&1u32));
assert!((1u32..5).contains(&4u32));
assert!(!(1u32..5).contains(&5u32));
assert!(!(1u32..5).contains(&6u32));
}
#[test]
fn test_range_to_contains() {
assert!(!(1u32..=5).contains(&0));
assert!((1u32..=5).contains(&1));
assert!((1u32..=5).contains(&4));
assert!((1u32..=5).contains(&5));
assert!(!(1u32..=5).contains(&6));
}
// This test covers `RangeBounds::contains` when the start is excluded,
// which cannot be directly expressed by Rust's built-in range syntax.
#[test]
fn test_range_bounds_contains() {
let r = (Bound::Excluded(1u32), Bound::Included(5u32));
assert!(!r.contains(&0));
assert!(!r.contains(&1));
assert!(r.contains(&3));
assert!(r.contains(&5));
assert!(!r.contains(&6));
}
#[test]
fn test_range_is_empty() {
assert!(!(0.0..10.0).is_empty());
@ -91,6 +122,34 @@ fn test_range_is_empty() {
assert!((f32::NAN..=f32::NAN).is_empty());
}
#[test]
fn test_range_inclusive_end_bound() {
let mut r = 1u32..=1;
r.next().unwrap();
assert!(!r.contains(&1));
}
#[test]
fn test_range_bounds() {
let r = (Bound::Included(1u32), Bound::Excluded(5u32));
assert!(!r.contains(&0));
assert!(r.contains(&1));
assert!(r.contains(&3));
assert!(!r.contains(&5));
assert!(!r.contains(&6));
let r = (Bound::<u32>::Unbounded, Bound::Unbounded);
assert!(r.contains(&0));
assert!(r.contains(&u32::MAX));
}
#[test]
fn test_one_sided_range_bound() {
assert!(matches!((..1u32).bound(), (OneSidedRangeBound::End, 1)));
assert!(matches!((1u32..).bound(), (OneSidedRangeBound::StartInclusive, 1)));
assert!(matches!((..=1u32).bound(), (OneSidedRangeBound::EndInclusive, 1)));
}
#[test]
fn test_bound_cloned_unbounded() {
assert_eq!(Bound::<&u32>::Unbounded.cloned(), Bound::Unbounded);
@ -240,3 +299,17 @@ fn deref_on_ref() {
fn test_not_never() {
if !return () {}
}
#[test]
fn test_fmt() {
let mut r = 1..=1;
assert_eq!(format!("{:?}", r), "1..=1");
r.next().unwrap();
assert_eq!(format!("{:?}", r), "1..=1 (exhausted)");
assert_eq!(format!("{:?}", 1..1), "1..1");
assert_eq!(format!("{:?}", 1..), "1..");
assert_eq!(format!("{:?}", ..1), "..1");
assert_eq!(format!("{:?}", ..=1), "..=1");
assert_eq!(format!("{:?}", ..), "..");
}