Skip to content

Commit 5f2b046

Browse files
committed
Improve core::ops coverage
1 parent 565a9ca commit 5f2b046

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#![feature(next_index)]
8282
#![feature(non_exhaustive_omitted_patterns_lint)]
8383
#![feature(numfmt)]
84+
#![feature(one_sided_range)]
8485
#![feature(option_reduce)]
8586
#![feature(pattern)]
8687
#![feature(peekable_next_if_map)]

library/coretests/tests/ops.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ mod control_flow;
22
mod from_residual;
33

44
use core::ops::{
5-
Bound, Deref, DerefMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
5+
Bound, Deref, DerefMut, OneSidedRange, OneSidedRangeBound, Range, RangeBounds, RangeFrom,
6+
RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
67
};
78

89
// Test the Range structs and syntax.
@@ -70,6 +71,35 @@ fn test_range_to_inclusive() {
7071
let _ = RangeToInclusive { end: 42 };
7172
}
7273

74+
#[test]
75+
fn test_range_contains() {
76+
assert!(!(1u32..5).contains(&0u32));
77+
assert!((1u32..5).contains(&1u32));
78+
assert!((1u32..5).contains(&4u32));
79+
assert!(!(1u32..5).contains(&5u32));
80+
assert!(!(1u32..5).contains(&6u32));
81+
}
82+
83+
#[test]
84+
fn test_range_to_contains() {
85+
assert!(!(1u32..=5).contains(&0));
86+
assert!((1u32..=5).contains(&1));
87+
assert!((1u32..=5).contains(&4));
88+
assert!((1u32..=5).contains(&5));
89+
assert!(!(1u32..=5).contains(&6));
90+
}
91+
92+
// This test covers `RangeBounds::contains` when the start is excluded.
93+
#[test]
94+
fn test_range_bounds_contains() {
95+
let r = (Bound::Excluded(1u32), Bound::Included(5u32));
96+
assert!(!r.contains(&0));
97+
assert!(!r.contains(&1));
98+
assert!(r.contains(&3));
99+
assert!(r.contains(&5));
100+
assert!(!r.contains(&6));
101+
}
102+
73103
#[test]
74104
fn test_range_is_empty() {
75105
assert!(!(0.0..10.0).is_empty());
@@ -91,6 +121,34 @@ fn test_range_is_empty() {
91121
assert!((f32::NAN..=f32::NAN).is_empty());
92122
}
93123

124+
#[test]
125+
fn test_range_inclusive_end_bound() {
126+
let mut r = 1u32..=1;
127+
r.next().unwrap();
128+
assert!(!r.contains(&1));
129+
}
130+
131+
#[test]
132+
fn test_range_bounds() {
133+
let r = (Bound::Included(1u32), Bound::Excluded(5u32));
134+
assert!(!r.contains(&0));
135+
assert!(r.contains(&1));
136+
assert!(r.contains(&3));
137+
assert!(!r.contains(&5));
138+
assert!(!r.contains(&6));
139+
140+
let r = (Bound::<u32>::Unbounded, Bound::Unbounded);
141+
assert!(r.contains(&0));
142+
assert!(r.contains(&u32::MAX));
143+
}
144+
145+
#[test]
146+
fn test_one_sided_range_bound() {
147+
assert!(matches!((..1u32).bound(), (OneSidedRangeBound::End, 1)));
148+
assert!(matches!((1u32..).bound(), (OneSidedRangeBound::StartInclusive, 1)));
149+
assert!(matches!((..=1u32).bound(), (OneSidedRangeBound::EndInclusive, 1)));
150+
}
151+
94152
#[test]
95153
fn test_bound_cloned_unbounded() {
96154
assert_eq!(Bound::<&u32>::Unbounded.cloned(), Bound::Unbounded);
@@ -240,3 +298,17 @@ fn deref_on_ref() {
240298
fn test_not_never() {
241299
if !return () {}
242300
}
301+
302+
#[test]
303+
fn test_fmt() {
304+
let mut r = 1..=1;
305+
assert_eq!(format!("{:?}", r), "1..=1");
306+
r.next().unwrap();
307+
assert_eq!(format!("{:?}", r), "1..=1 (exhausted)");
308+
309+
assert_eq!(format!("{:?}", 1..1), "1..1");
310+
assert_eq!(format!("{:?}", 1..), "1..");
311+
assert_eq!(format!("{:?}", ..1), "..1");
312+
assert_eq!(format!("{:?}", ..=1), "..=1");
313+
assert_eq!(format!("{:?}", ..), "..");
314+
}

0 commit comments

Comments
 (0)