1 use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
2 use core::ops::{Deref, DerefMut};
4 // Test the Range structs and syntax.
8 let r = Range { start: 2, end: 10 };
10 for (i, ri) in r.enumerate() {
11 assert_eq!(ri, i + 2);
12 assert!(ri >= 2 && ri < 10);
19 fn test_range_from() {
20 let r = RangeFrom { start: 2 };
22 for (i, ri) in r.take(10).enumerate() {
23 assert_eq!(ri, i + 2);
24 assert!(ri >= 2 && ri < 12);
27 assert_eq!(count, 10);
33 let _ = RangeTo { end: 42 };
37 fn test_full_range() {
43 fn test_range_inclusive() {
44 let mut r = RangeInclusive::new(1i8, 2);
45 assert_eq!(r.next(), Some(1));
46 assert_eq!(r.next(), Some(2));
47 assert_eq!(r.next(), None);
49 r = RangeInclusive::new(127i8, 127);
50 assert_eq!(r.next(), Some(127));
51 assert_eq!(r.next(), None);
53 r = RangeInclusive::new(-128i8, -128);
54 assert_eq!(r.next_back(), Some(-128));
55 assert_eq!(r.next_back(), None);
58 r = RangeInclusive::new(1, -1);
59 assert_eq!(r.size_hint(), (0, Some(0)));
60 assert_eq!(r.next(), None);
64 fn test_range_to_inclusive() {
66 let _ = RangeToInclusive { end: 42 };
70 fn test_range_is_empty() {
71 assert!(!(0.0..10.0).is_empty());
72 assert!((-0.0..0.0).is_empty());
73 assert!((10.0..0.0).is_empty());
75 assert!(!(f32::NEG_INFINITY..f32::INFINITY).is_empty());
76 assert!((f32::EPSILON..f32::NAN).is_empty());
77 assert!((f32::NAN..f32::EPSILON).is_empty());
78 assert!((f32::NAN..f32::NAN).is_empty());
80 assert!(!(0.0..=10.0).is_empty());
81 assert!(!(-0.0..=0.0).is_empty());
82 assert!((10.0..=0.0).is_empty());
84 assert!(!(f32::NEG_INFINITY..=f32::INFINITY).is_empty());
85 assert!((f32::EPSILON..=f32::NAN).is_empty());
86 assert!((f32::NAN..=f32::EPSILON).is_empty());
87 assert!((f32::NAN..=f32::NAN).is_empty());
91 fn test_bound_cloned_unbounded() {
92 assert_eq!(Bound::<&u32>::Unbounded.cloned(), Bound::Unbounded);
96 fn test_bound_cloned_included() {
97 assert_eq!(Bound::Included(&3).cloned(), Bound::Included(3));
101 fn test_bound_cloned_excluded() {
102 assert_eq!(Bound::Excluded(&3).cloned(), Bound::Excluded(3));
106 #[allow(unused_comparisons)]
108 fn test_range_syntax() {
110 for i in 0_usize..10 {
111 assert!(i >= 0 && i < 10);
114 assert_eq!(count, 45);
117 let mut range = 0_usize..10;
119 assert!(i >= 0 && i < 10);
122 assert_eq!(count, 45);
125 let mut rf = 3_usize..;
126 for i in rf.take(10) {
127 assert!(i >= 3 && i < 13);
130 assert_eq!(count, 75);
132 let _ = 0_usize..4 + 4 - 3;
139 let _ = { &42..&100 }; // references to literals are OK
142 // Test we can use two different types with a common supertype.
152 fn test_range_syntax_in_return_statement() {
153 fn return_range_to() -> RangeTo<i32> {
156 fn return_full_range() -> RangeFull {
163 fn range_structural_match() {
164 // test that all range types can be structurally matched upon
166 const RANGE: Range<usize> = 0..1000;
172 const RANGE_FROM: RangeFrom<usize> = 0..;
178 const RANGE_FULL: RangeFull = ..;
183 const RANGE_INCLUSIVE: RangeInclusive<usize> = 0..=999;
184 match RANGE_INCLUSIVE {
185 RANGE_INCLUSIVE => {}
189 const RANGE_TO: RangeTo<usize> = ..1000;
195 const RANGE_TO_INCLUSIVE: RangeToInclusive<usize> = ..=999;
196 match RANGE_TO_INCLUSIVE {
197 RANGE_TO_INCLUSIVE => {}
202 // Test Deref implementations
205 fn deref_mut_on_ref() {
206 // Test that `&mut T` implements `DerefMut<T>`
208 fn inc<T: Deref<Target = isize> + DerefMut>(mut t: T) {
212 let mut x: isize = 5;
219 // Test that `&T` and `&mut T` implement `Deref<T>`
221 fn deref<U: Copy, T: Deref<Target = U>>(t: T) -> U {
229 let mut x: isize = 4;
230 let y = deref(&mut x);