]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/range_inclusive.rs
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / test / run-pass / range_inclusive.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test inclusive range syntax.
12
13 use std::ops::{RangeInclusive, RangeToInclusive};
14
15 fn foo() -> isize { 42 }
16
17 // Test that range syntax works in return statements
18 fn return_range_to() -> RangeToInclusive<i32> { return ..=1; }
19
20 pub fn main() {
21 let mut count = 0;
22 for i in 0_usize..=10 {
23 assert!(i >= 0 && i <= 10);
24 count += i;
25 }
26 assert_eq!(count, 55);
27
28 let mut count = 0;
29 let mut range = 0_usize..=10;
30 for i in range {
31 assert!(i >= 0 && i <= 10);
32 count += i;
33 }
34 assert_eq!(count, 55);
35
36 let mut count = 0;
37 for i in (0_usize..=10).step_by(2) {
38 assert!(i >= 0 && i <= 10 && i % 2 == 0);
39 count += i;
40 }
41 assert_eq!(count, 30);
42
43 let _ = 0_usize..=4+4-3;
44 let _ = 0..=foo();
45
46 let _ = { &42..=&100 }; // references to literals are OK
47 let _ = ..=42_usize;
48
49 // Test we can use two different types with a common supertype.
50 let x = &42;
51 {
52 let y = 42;
53 let _ = x..=&y;
54 }
55
56 // test collection indexing
57 let vec = (0..=10).collect::<Vec<_>>();
58 let slice: &[_] = &*vec;
59 let string = String::from("hello world");
60 let stir = "hello world";
61
62 assert_eq!(&vec[3..=6], &[3, 4, 5, 6]);
63 assert_eq!(&vec[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
64
65 assert_eq!(&slice[3..=6], &[3, 4, 5, 6]);
66 assert_eq!(&slice[ ..=6], &[0, 1, 2, 3, 4, 5, 6]);
67
68 assert_eq!(&string[3..=6], "lo w");
69 assert_eq!(&string[ ..=6], "hello w");
70
71 assert_eq!(&stir[3..=6], "lo w");
72 assert_eq!(&stir[ ..=6], "hello w");
73
74 // test the size hints and emptying
75 let mut long = 0..=255u8;
76 let mut short = 42..=42u8;
77 assert_eq!(long.size_hint(), (256, Some(256)));
78 assert_eq!(short.size_hint(), (1, Some(1)));
79 long.next();
80 short.next();
81 assert_eq!(long.size_hint(), (255, Some(255)));
82 assert_eq!(short.size_hint(), (0, Some(0)));
83 assert_eq!(short, 1..=0);
84
85 assert_eq!(long.len(), 255);
86 assert_eq!(short.len(), 0);
87
88 // test iterating backwards
89 assert_eq!(long.next_back(), Some(255));
90 assert_eq!(long.next_back(), Some(254));
91 assert_eq!(long.next_back(), Some(253));
92 assert_eq!(long.next(), Some(1));
93 assert_eq!(long.next(), Some(2));
94 assert_eq!(long.next_back(), Some(252));
95 for i in 3..=251 {
96 assert_eq!(long.next(), Some(i));
97 }
98 assert_eq!(long, 1..=0);
99
100 // check underflow
101 let mut narrow = 1..=0;
102 assert_eq!(narrow.next_back(), None);
103 assert_eq!(narrow, 1..=0);
104 let mut zero = 0u8..=0;
105 assert_eq!(zero.next_back(), Some(0));
106 assert_eq!(zero.next_back(), None);
107 assert_eq!(zero, 1..=0);
108 let mut high = 255u8..=255;
109 assert_eq!(high.next_back(), Some(255));
110 assert_eq!(high.next_back(), None);
111 assert_eq!(high, 1..=0);
112
113 // what happens if you have a nonsense range?
114 let mut nonsense = 10..=5;
115 assert_eq!(nonsense.next(), None);
116 assert_eq!(nonsense, 10..=5);
117
118 // output
119 assert_eq!(format!("{:?}", 0..=10), "0..=10");
120 assert_eq!(format!("{:?}", ..=10), "..=10");
121 assert_eq!(format!("{:?}", long), "1..=0");
122 }