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