2019-12-06 22:18:12 -06:00
|
|
|
use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo};
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2014-12-12 21:58:48 -06:00
|
|
|
// Test the Range structs without the syntactic sugar.
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let r = Range { start: 2, end: 10 };
|
|
|
|
let mut count = 0;
|
2014-12-12 21:58:48 -06:00
|
|
|
for (i, ri) in r.enumerate() {
|
2019-03-15 06:08:23 -05:00
|
|
|
assert_eq!(ri, i + 2);
|
2015-02-03 07:50:52 -06:00
|
|
|
assert!(ri >= 2 && ri < 10);
|
2014-12-15 21:25:33 -06:00
|
|
|
count += 1;
|
2014-12-12 21:58:48 -06:00
|
|
|
}
|
2019-03-15 06:08:23 -05:00
|
|
|
assert_eq!(count, 8);
|
2014-12-12 21:58:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_from() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let r = RangeFrom { start: 2 };
|
|
|
|
let mut count = 0;
|
2014-12-12 21:58:48 -06:00
|
|
|
for (i, ri) in r.take(10).enumerate() {
|
2019-03-15 06:08:23 -05:00
|
|
|
assert_eq!(ri, i + 2);
|
2015-02-03 07:50:52 -06:00
|
|
|
assert!(ri >= 2 && ri < 12);
|
2014-12-15 21:25:33 -06:00
|
|
|
count += 1;
|
2014-12-12 21:58:48 -06:00
|
|
|
}
|
2019-03-15 06:08:23 -05:00
|
|
|
assert_eq!(count, 10);
|
2014-12-12 21:58:48 -06:00
|
|
|
}
|
|
|
|
|
2014-12-17 22:55:04 -06:00
|
|
|
#[test]
|
|
|
|
fn test_range_to() {
|
|
|
|
// Not much to test.
|
2015-02-03 07:50:52 -06:00
|
|
|
let _ = RangeTo { end: 42 };
|
2014-12-17 22:55:04 -06:00
|
|
|
}
|
|
|
|
|
2014-12-12 21:58:48 -06:00
|
|
|
#[test]
|
|
|
|
fn test_full_range() {
|
|
|
|
// Not much to test.
|
2015-01-27 23:16:00 -06:00
|
|
|
let _ = RangeFull;
|
2014-12-12 21:58:48 -06:00
|
|
|
}
|
2017-04-23 23:14:32 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_inclusive() {
|
2018-04-05 16:21:47 -05:00
|
|
|
let mut r = RangeInclusive::new(1i8, 2);
|
2017-04-23 23:14:32 -05:00
|
|
|
assert_eq!(r.next(), Some(1));
|
|
|
|
assert_eq!(r.next(), Some(2));
|
|
|
|
assert_eq!(r.next(), None);
|
|
|
|
|
2018-04-05 16:21:47 -05:00
|
|
|
r = RangeInclusive::new(127i8, 127);
|
2017-04-23 23:14:32 -05:00
|
|
|
assert_eq!(r.next(), Some(127));
|
|
|
|
assert_eq!(r.next(), None);
|
|
|
|
|
2018-04-05 16:21:47 -05:00
|
|
|
r = RangeInclusive::new(-128i8, -128);
|
2017-04-23 23:14:32 -05:00
|
|
|
assert_eq!(r.next_back(), Some(-128));
|
|
|
|
assert_eq!(r.next_back(), None);
|
2017-05-21 07:03:49 -05:00
|
|
|
|
|
|
|
// degenerate
|
2018-04-05 16:21:47 -05:00
|
|
|
r = RangeInclusive::new(1, -1);
|
2017-05-21 07:03:49 -05:00
|
|
|
assert_eq!(r.size_hint(), (0, Some(0)));
|
|
|
|
assert_eq!(r.next(), None);
|
2017-12-29 12:25:40 -06:00
|
|
|
}
|
2018-02-09 03:47:18 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_is_empty() {
|
2019-12-06 22:18:12 -06:00
|
|
|
assert!(!(0.0..10.0).is_empty());
|
|
|
|
assert!((-0.0..0.0).is_empty());
|
|
|
|
assert!((10.0..0.0).is_empty());
|
2018-02-09 03:47:18 -06:00
|
|
|
|
2020-04-06 15:44:51 -05: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 03:47:18 -06:00
|
|
|
|
2019-12-06 22:18:12 -06:00
|
|
|
assert!(!(0.0..=10.0).is_empty());
|
|
|
|
assert!(!(-0.0..=0.0).is_empty());
|
|
|
|
assert!((10.0..=0.0).is_empty());
|
2018-02-09 03:47:18 -06:00
|
|
|
|
2020-04-06 15:44:51 -05: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 04:11:04 -06:00
|
|
|
}
|
2019-05-31 11:36:37 -05: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));
|
|
|
|
}
|