about summary refs log tree commit diff
path: root/library/coretests
diff options
context:
space:
mode:
authorChristian Poveda <christian.poveda@ferrous-systems.com>2025-09-10 16:32:04 -0500
committerChristian Poveda <christian.poveda@ferrous-systems.com>2025-09-10 16:32:04 -0500
commit5f2b04652fe006439aaa52271997a69499dca021 (patch)
tree84bbe7be61a75292c89aad98b3d7ccb3f41b2276 /library/coretests
parent565a9ca63e9df4b223fed0da01f15e578acfb538 (diff)
downloadrust-5f2b04652fe006439aaa52271997a69499dca021.tar.gz
rust-5f2b04652fe006439aaa52271997a69499dca021.zip
Improve `core::ops` coverage
Diffstat (limited to 'library/coretests')
-rw-r--r--library/coretests/tests/lib.rs1
-rw-r--r--library/coretests/tests/ops.rs74
2 files changed, 74 insertions, 1 deletions
diff --git a/library/coretests/tests/lib.rs b/library/coretests/tests/lib.rs
index b5658a9970f..1bdaa6965f6 100644
--- a/library/coretests/tests/lib.rs
+++ b/library/coretests/tests/lib.rs
@@ -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)]
diff --git a/library/coretests/tests/ops.rs b/library/coretests/tests/ops.rs
index 501e0f33fe4..921010774e2 100644
--- a/library/coretests/tests/ops.rs
+++ b/library/coretests/tests/ops.rs
@@ -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.
@@ -71,6 +72,35 @@ fn test_range_to_inclusive() {
 }
 
 #[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.
+#[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());
     assert!((-0.0..0.0).is_empty());
@@ -92,6 +122,34 @@ fn test_range_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 +298,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!("{:?}", ..), "..");
+}