rust/library/core/tests/ops.rs

97 lines
2.3 KiB
Rust
Raw Normal View History

use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo};
2014-12-13 04:58:48 +01:00
// Test the Range structs without the syntactic sugar.
#[test]
fn test_range() {
let r = Range { start: 2, end: 10 };
let mut count = 0;
2014-12-13 04:58:48 +01:00
for (i, ri) in r.enumerate() {
2019-03-15 12:08:23 +01:00
assert_eq!(ri, i + 2);
assert!(ri >= 2 && ri < 10);
2014-12-16 04:25:33 +01:00
count += 1;
2014-12-13 04:58:48 +01:00
}
2019-03-15 12:08:23 +01:00
assert_eq!(count, 8);
2014-12-13 04:58:48 +01:00
}
#[test]
fn test_range_from() {
let r = RangeFrom { start: 2 };
let mut count = 0;
2014-12-13 04:58:48 +01:00
for (i, ri) in r.take(10).enumerate() {
2019-03-15 12:08:23 +01:00
assert_eq!(ri, i + 2);
assert!(ri >= 2 && ri < 12);
2014-12-16 04:25:33 +01:00
count += 1;
2014-12-13 04:58:48 +01:00
}
2019-03-15 12:08:23 +01:00
assert_eq!(count, 10);
2014-12-13 04:58:48 +01:00
}
#[test]
fn test_range_to() {
// Not much to test.
let _ = RangeTo { end: 42 };
}
2014-12-13 04:58:48 +01:00
#[test]
fn test_full_range() {
// Not much to test.
let _ = RangeFull;
2014-12-13 04:58:48 +01:00
}
#[test]
fn test_range_inclusive() {
let mut r = RangeInclusive::new(1i8, 2);
assert_eq!(r.next(), Some(1));
assert_eq!(r.next(), Some(2));
assert_eq!(r.next(), None);
r = RangeInclusive::new(127i8, 127);
assert_eq!(r.next(), Some(127));
assert_eq!(r.next(), None);
r = RangeInclusive::new(-128i8, -128);
assert_eq!(r.next_back(), Some(-128));
assert_eq!(r.next_back(), None);
// degenerate
r = RangeInclusive::new(1, -1);
assert_eq!(r.size_hint(), (0, Some(0)));
assert_eq!(r.next(), None);
}
#[test]
fn test_range_is_empty() {
assert!(!(0.0..10.0).is_empty());
assert!((-0.0..0.0).is_empty());
assert!((10.0..0.0).is_empty());
2020-04-06 22:44:51 +02:00
assert!(!(f32::NEG_INFINITY..f32::INFINITY).is_empty());
assert!((f32::EPSILON..f32::NAN).is_empty());
assert!((f32::NAN..f32::EPSILON).is_empty());
assert!((f32::NAN..f32::NAN).is_empty());
assert!(!(0.0..=10.0).is_empty());
assert!(!(-0.0..=0.0).is_empty());
assert!((10.0..=0.0).is_empty());
2020-04-06 22:44:51 +02:00
assert!(!(f32::NEG_INFINITY..=f32::INFINITY).is_empty());
assert!((f32::EPSILON..=f32::NAN).is_empty());
assert!((f32::NAN..=f32::EPSILON).is_empty());
assert!((f32::NAN..=f32::NAN).is_empty());
2018-02-09 11:11:04 +01:00
}
2019-05-31 18:36:37 +02:00
#[test]
fn test_bound_cloned_unbounded() {
assert_eq!(Bound::<&u32>::Unbounded.cloned(), Bound::Unbounded);
}
#[test]
fn test_bound_cloned_included() {
assert_eq!(Bound::Included(&3).cloned(), Bound::Included(3));
}
#[test]
fn test_bound_cloned_excluded() {
assert_eq!(Bound::Excluded(&3).cloned(), Bound::Excluded(3));
}