1 // run-pass
2 // Test inclusive range syntax.
3 #![allow(unused_braces)]
4 #![allow(unused_comparisons)]
5
6 use std::ops::RangeToInclusive;
7
foo() -> isize8 fn foo() -> isize { 42 }
9
10 // Test that range syntax works in return statements
return_range_to() -> RangeToInclusive<i32>11 pub fn return_range_to() -> RangeToInclusive<i32> { return ..=1; }
12
13 #[derive(Debug)]
14 struct P(u8);
15
main()16 pub fn main() {
17 let mut count = 0;
18 for i in 0_usize..=10 {
19 assert!(i >= 0 && i <= 10);
20 count += i;
21 }
22 assert_eq!(count, 55);
23
24 let mut count = 0;
25 let range = 0_usize..=10;
26 for i in range {
27 assert!(i >= 0 && i <= 10);
28 count += i;
29 }
30 assert_eq!(count, 55);
31
32 let mut count = 0;
33 for i in (0_usize..=10).step_by(2) {
34 assert!(i >= 0 && i <= 10 && i % 2 == 0);
35 count += i;
36 }
37 assert_eq!(count, 30);
38
39 let _ = 0_usize..=4+4-3;
40 let _ = 0..=foo();
41
42 let _ = { &42..=&100 }; // references to literals are OK
43 let _ = ..=42_usize;
44
45 // Test we can use two different types with a common supertype.
46 let x = &42;
47 {
48 let y = 42;
49 let _ = x..=&y;
50 }
51
52 // test collection indexing
53 let vec = (0..=10).collect::<Vec<_>>();
54 let slice: &[_] = &*vec;
55 let string = String::from("hello world");
56 let stir = "hello world";
57
58 assert_eq!(&vec[3..=6], &[3, 4, 5, 6]);
59 assert_eq!(&vec[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
60
61 assert_eq!(&slice[3..=6], &[3, 4, 5, 6]);
62 assert_eq!(&slice[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
63
64 assert_eq!(&string[3..=6], "lo w");
65 assert_eq!(&string[ ..=6], "hello w");
66
67 assert_eq!(&stir[3..=6], "lo w");
68 assert_eq!(&stir[ ..=6], "hello w");
69
70 // test the size hints and emptying
71 let mut long = 0..=255u8;
72 let mut short = 42..=42u8;
73 assert_eq!(long.size_hint(), (256, Some(256)));
74 assert_eq!(short.size_hint(), (1, Some(1)));
75 long.next();
76 short.next();
77 assert_eq!(long.size_hint(), (255, Some(255)));
78 assert_eq!(short.size_hint(), (0, Some(0)));
79 assert!(short.is_empty());
80
81 assert_eq!(long.len(), 255);
82 assert_eq!(short.len(), 0);
83
84 // test iterating backwards
85 assert_eq!(long.next_back(), Some(255));
86 assert_eq!(long.next_back(), Some(254));
87 assert_eq!(long.next_back(), Some(253));
88 assert_eq!(long.next(), Some(1));
89 assert_eq!(long.next(), Some(2));
90 assert_eq!(long.next_back(), Some(252));
91 for i in 3..=251 {
92 assert_eq!(long.next(), Some(i));
93 }
94 assert!(long.is_empty());
95
96 // check underflow
97 let mut narrow = 1..=0;
98 assert_eq!(narrow.next_back(), None);
99 assert!(narrow.is_empty());
100 let mut zero = 0u8..=0;
101 assert_eq!(zero.next_back(), Some(0));
102 assert_eq!(zero.next_back(), None);
103 assert!(zero.is_empty());
104 let mut high = 255u8..=255;
105 assert_eq!(high.next_back(), Some(255));
106 assert_eq!(high.next_back(), None);
107 assert!(high.is_empty());
108
109 // what happens if you have a nonsense range?
110 let mut nonsense = 10..=5;
111 assert_eq!(nonsense.next(), None);
112 assert!(nonsense.is_empty());
113
114 // output
115 assert_eq!(format!("{:?}", 0..=10), "0..=10");
116 assert_eq!(format!("{:?}", ..=10), "..=10");
117 assert_eq!(format!("{:?}", 9..=6), "9..=6");
118
119 // ensure that constructing a RangeInclusive does not need PartialOrd bound
120 assert_eq!(format!("{:?}", P(1)..=P(2)), "P(1)..=P(2)");
121 }
122