Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
75 changes: 74 additions & 1 deletion library/coretests/tests/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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!("{:?}", ..), "..");
}
Loading