Skip to content

Commit 72ed47d

Browse files
authored
Unrolled build for #146424
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`
2 parents 5e33838 + 789c840 commit 72ed47d

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-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: 74 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,36 @@ 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+
// which cannot be directly expressed by Rust's built-in range syntax.
94+
#[test]
95+
fn test_range_bounds_contains() {
96+
let r = (Bound::Excluded(1u32), Bound::Included(5u32));
97+
assert!(!r.contains(&0));
98+
assert!(!r.contains(&1));
99+
assert!(r.contains(&3));
100+
assert!(r.contains(&5));
101+
assert!(!r.contains(&6));
102+
}
103+
73104
#[test]
74105
fn test_range_is_empty() {
75106
assert!(!(0.0..10.0).is_empty());
@@ -91,6 +122,34 @@ fn test_range_is_empty() {
91122
assert!((f32::NAN..=f32::NAN).is_empty());
92123
}
93124

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

0 commit comments

Comments
 (0)