2021-02-07 06:36:05 +00:00
|
|
|
mod control_flow;
|
|
|
|
|
2020-11-07 00:02:15 +00:00
|
|
|
use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
|
2020-11-22 08:08:04 +00:00
|
|
|
use core::ops::{Deref, DerefMut};
|
2014-06-28 20:57:36 +00:00
|
|
|
|
2020-11-04 14:59:04 +00:00
|
|
|
// Test the Range structs and syntax.
|
2014-12-13 03:58:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range() {
|
2015-02-03 13:50:52 +00:00
|
|
|
let r = Range { start: 2, end: 10 };
|
|
|
|
let mut count = 0;
|
2014-12-13 03:58:48 +00:00
|
|
|
for (i, ri) in r.enumerate() {
|
2019-03-15 11:08:23 +00:00
|
|
|
assert_eq!(ri, i + 2);
|
2015-02-03 13:50:52 +00:00
|
|
|
assert!(ri >= 2 && ri < 10);
|
2014-12-16 03:25:33 +00:00
|
|
|
count += 1;
|
2014-12-13 03:58:48 +00:00
|
|
|
}
|
2019-03-15 11:08:23 +00:00
|
|
|
assert_eq!(count, 8);
|
2014-12-13 03:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_from() {
|
2015-02-03 13:50:52 +00:00
|
|
|
let r = RangeFrom { start: 2 };
|
|
|
|
let mut count = 0;
|
2014-12-13 03:58:48 +00:00
|
|
|
for (i, ri) in r.take(10).enumerate() {
|
2019-03-15 11:08:23 +00:00
|
|
|
assert_eq!(ri, i + 2);
|
2015-02-03 13:50:52 +00:00
|
|
|
assert!(ri >= 2 && ri < 12);
|
2014-12-16 03:25:33 +00:00
|
|
|
count += 1;
|
2014-12-13 03:58:48 +00:00
|
|
|
}
|
2019-03-15 11:08:23 +00:00
|
|
|
assert_eq!(count, 10);
|
2014-12-13 03:58:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 04:55:04 +00:00
|
|
|
#[test]
|
|
|
|
fn test_range_to() {
|
|
|
|
// Not much to test.
|
2015-02-03 13:50:52 +00:00
|
|
|
let _ = RangeTo { end: 42 };
|
2014-12-18 04:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 03:58:48 +00:00
|
|
|
#[test]
|
|
|
|
fn test_full_range() {
|
|
|
|
// Not much to test.
|
2015-01-28 05:16:00 +00:00
|
|
|
let _ = RangeFull;
|
2014-12-13 03:58:48 +00:00
|
|
|
}
|
2017-04-24 04:14:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_inclusive() {
|
2018-04-05 21:21:47 +00:00
|
|
|
let mut r = RangeInclusive::new(1i8, 2);
|
2017-04-24 04:14:32 +00:00
|
|
|
assert_eq!(r.next(), Some(1));
|
|
|
|
assert_eq!(r.next(), Some(2));
|
|
|
|
assert_eq!(r.next(), None);
|
|
|
|
|
2018-04-05 21:21:47 +00:00
|
|
|
r = RangeInclusive::new(127i8, 127);
|
2017-04-24 04:14:32 +00:00
|
|
|
assert_eq!(r.next(), Some(127));
|
|
|
|
assert_eq!(r.next(), None);
|
|
|
|
|
2018-04-05 21:21:47 +00:00
|
|
|
r = RangeInclusive::new(-128i8, -128);
|
2017-04-24 04:14:32 +00:00
|
|
|
assert_eq!(r.next_back(), Some(-128));
|
|
|
|
assert_eq!(r.next_back(), None);
|
2017-05-21 12:03:49 +00:00
|
|
|
|
|
|
|
// degenerate
|
2018-04-05 21:21:47 +00:00
|
|
|
r = RangeInclusive::new(1, -1);
|
2017-05-21 12:03:49 +00:00
|
|
|
assert_eq!(r.size_hint(), (0, Some(0)));
|
|
|
|
assert_eq!(r.next(), None);
|
2017-12-29 18:25:40 +00:00
|
|
|
}
|
2018-02-09 09:47:18 +00:00
|
|
|
|
2020-11-07 00:02:15 +00:00
|
|
|
#[test]
|
|
|
|
fn test_range_to_inclusive() {
|
|
|
|
// Not much to test.
|
|
|
|
let _ = RangeToInclusive { end: 42 };
|
|
|
|
}
|
|
|
|
|
2018-02-09 09:47:18 +00:00
|
|
|
#[test]
|
|
|
|
fn test_range_is_empty() {
|
2019-12-07 04:18:12 +00: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 09:47:18 +00:00
|
|
|
|
2020-04-06 20:44:51 +00: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 09:47:18 +00:00
|
|
|
|
2019-12-07 04:18:12 +00: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 09:47:18 +00:00
|
|
|
|
2020-04-06 20:44:51 +00: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 10:11:04 +00:00
|
|
|
}
|
2019-05-31 16:36:37 +00: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));
|
|
|
|
}
|
2020-11-04 14:59:04 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[allow(unused_comparisons)]
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
fn test_range_syntax() {
|
|
|
|
let mut count = 0;
|
|
|
|
for i in 0_usize..10 {
|
|
|
|
assert!(i >= 0 && i < 10);
|
|
|
|
count += i;
|
|
|
|
}
|
|
|
|
assert_eq!(count, 45);
|
|
|
|
|
|
|
|
let mut count = 0;
|
|
|
|
let mut range = 0_usize..10;
|
|
|
|
for i in range {
|
|
|
|
assert!(i >= 0 && i < 10);
|
|
|
|
count += i;
|
|
|
|
}
|
|
|
|
assert_eq!(count, 45);
|
|
|
|
|
|
|
|
let mut count = 0;
|
|
|
|
let mut rf = 3_usize..;
|
|
|
|
for i in rf.take(10) {
|
|
|
|
assert!(i >= 3 && i < 13);
|
|
|
|
count += i;
|
|
|
|
}
|
|
|
|
assert_eq!(count, 75);
|
|
|
|
|
|
|
|
let _ = 0_usize..4 + 4 - 3;
|
|
|
|
|
|
|
|
fn foo() -> isize {
|
|
|
|
42
|
|
|
|
}
|
|
|
|
let _ = 0..foo();
|
|
|
|
|
|
|
|
let _ = { &42..&100 }; // references to literals are OK
|
|
|
|
let _ = ..42_usize;
|
|
|
|
|
|
|
|
// Test we can use two different types with a common supertype.
|
|
|
|
let x = &42;
|
|
|
|
{
|
|
|
|
let y = 42;
|
|
|
|
let _ = x..&y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn test_range_syntax_in_return_statement() {
|
|
|
|
fn return_range_to() -> RangeTo<i32> {
|
|
|
|
return ..1;
|
|
|
|
}
|
|
|
|
fn return_full_range() -> RangeFull {
|
|
|
|
return ..;
|
|
|
|
}
|
|
|
|
// Not much to test.
|
|
|
|
}
|
2020-11-07 00:02:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_structural_match() {
|
|
|
|
// test that all range types can be structurally matched upon
|
|
|
|
|
|
|
|
const RANGE: Range<usize> = 0..1000;
|
|
|
|
match RANGE {
|
|
|
|
RANGE => {}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const RANGE_FROM: RangeFrom<usize> = 0..;
|
|
|
|
match RANGE_FROM {
|
|
|
|
RANGE_FROM => {}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const RANGE_FULL: RangeFull = ..;
|
|
|
|
match RANGE_FULL {
|
|
|
|
RANGE_FULL => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const RANGE_INCLUSIVE: RangeInclusive<usize> = 0..=999;
|
|
|
|
match RANGE_INCLUSIVE {
|
|
|
|
RANGE_INCLUSIVE => {}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const RANGE_TO: RangeTo<usize> = ..1000;
|
|
|
|
match RANGE_TO {
|
|
|
|
RANGE_TO => {}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const RANGE_TO_INCLUSIVE: RangeToInclusive<usize> = ..=999;
|
|
|
|
match RANGE_TO_INCLUSIVE {
|
|
|
|
RANGE_TO_INCLUSIVE => {}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 08:08:04 +00:00
|
|
|
|
|
|
|
// Test Deref implementations
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deref_mut_on_ref() {
|
|
|
|
// Test that `&mut T` implements `DerefMut<T>`
|
|
|
|
|
|
|
|
fn inc<T: Deref<Target = isize> + DerefMut>(mut t: T) {
|
|
|
|
*t += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut x: isize = 5;
|
|
|
|
inc(&mut x);
|
|
|
|
assert_eq!(x, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deref_on_ref() {
|
|
|
|
// Test that `&T` and `&mut T` implement `Deref<T>`
|
|
|
|
|
|
|
|
fn deref<U: Copy, T: Deref<Target = U>>(t: T) -> U {
|
|
|
|
*t
|
|
|
|
}
|
|
|
|
|
|
|
|
let x: isize = 3;
|
|
|
|
let y = deref(&x);
|
|
|
|
assert_eq!(y, 3);
|
|
|
|
|
|
|
|
let mut x: isize = 4;
|
|
|
|
let y = deref(&mut x);
|
|
|
|
assert_eq!(y, 4);
|
|
|
|
}
|
2021-11-22 03:09:14 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[allow(unreachable_code)]
|
|
|
|
fn test_not_never() {
|
|
|
|
if !return () {}
|
|
|
|
}
|