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