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.
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.
11 // Test inclusive range syntax.
13 #![feature(inclusive_range_syntax, inclusive_range, step_by)]
15 use std
::ops
::{RangeInclusive, RangeToInclusive}
;
17 fn foo() -> isize { 42 }
19 // Test that range syntax works in return statements
20 fn return_range_to() -> RangeToInclusive
<i32> { return ...1; }
24 for i
in 0_usize
...10 {
25 assert
!(i
>= 0 && i
<= 10);
28 assert_eq
!(count
, 55);
31 let mut range
= 0_usize
...10;
33 assert
!(i
>= 0 && i
<= 10);
36 assert_eq
!(count
, 55);
39 for i
in (0_usize
...10).step_by(2) {
40 assert
!(i
>= 0 && i
<= 10 && i
% 2 == 0);
43 assert_eq
!(count
, 30);
45 let _
= 0_usize
...4+4-3;
48 let _
= { &42...&100 }
; // references to literals are OK
51 // Test we can use two different types with a common supertype.
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";
64 assert_eq
!(&vec
[3...6], &[3, 4, 5, 6]);
65 assert_eq
!(&vec
[ ...6], &[0, 1, 2, 3, 4, 5, 6]);
67 assert_eq
!(&slice
[3...6], &[3, 4, 5, 6]);
68 assert_eq
!(&slice
[ ...6], &[0, 1, 2, 3, 4, 5, 6]);
70 assert_eq
!(&string
[3...6], "lo w");
71 assert_eq
!(&string
[ ...6], "hello w");
73 assert_eq
!(&stir
[3...6], "lo w");
74 assert_eq
!(&stir
[ ...6], "hello w");
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)));
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 }
);
87 assert_eq
!(long
.len(), 255);
88 assert_eq
!(short
.len(), 0);
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));
98 assert_eq
!(long
.next(), Some(i
));
100 assert_eq
!(long
, RangeInclusive
::Empty { at: 251 }
);
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 }
);
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 }
);
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());
126 assert_eq
!(format
!("{:?}", 0...10), "0...10");
127 assert_eq
!(format
!("{:?}", ...10), "...10");
128 assert_eq
!(format
!("{:?}", long
), "[empty range @ 251]");