Skip to content

Commit 69719df

Browse files
committed
test inclusive ranges
Mostly copy the tests from half-open ranges, adding some more for DoubleEndedIterator and ExactSizeIterator. Also thoroughly (I think) test that the feature gates are working.
1 parent 37a4cb3 commit 69719df

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// Make sure that #![feature(inclusive_range)] is required.
12+
13+
#![feature(inclusive_range_syntax)]
14+
// #![feature(inclusive_range)]
15+
16+
pub fn main() {
17+
let _: std::ops::RangeInclusive<_> = 1...10;
18+
//~^ ERROR use of unstable library feature 'inclusive_range'
19+
}
20+
21+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
// Make sure that inclusive ranges with no end point don't parse.
12+
13+
#![feature(inclusive_range_syntax, inclusive_range)]
14+
15+
pub fn main() {
16+
for _ in 1... {}
17+
} //~ ERROR expected one of
18+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
// Make sure that #![feature(inclusive_range_syntax)] is required.
12+
13+
// #![feature(inclusive_range_syntax, inclusive_range)]
14+
15+
macro_rules! m {
16+
() => { for _ in 1...10 {} } //~ ERROR inclusive range syntax is experimental
17+
}
18+
19+
#[cfg(nope)]
20+
fn f() {}
21+
#[cfg(not(nope))]
22+
fn f() {
23+
for _ in 1...10 {} //~ ERROR inclusive range syntax is experimental
24+
}
25+
26+
#[cfg(nope)]
27+
macro_rules! n { () => {} }
28+
#[cfg(not(nope))]
29+
macro_rules! n {
30+
() => { for _ in 1...10 {} } //~ ERROR inclusive range syntax is experimental
31+
}
32+
33+
macro_rules! o {
34+
() => {{
35+
#[cfg(nope)]
36+
fn g() {}
37+
#[cfg(not(nope))]
38+
fn g() {
39+
for _ in 1...10 {} //~ ERROR inclusive range syntax is experimental
40+
}
41+
42+
g();
43+
}}
44+
}
45+
46+
#[cfg(nope)]
47+
macro_rules! p { () => {} }
48+
#[cfg(not(nope))]
49+
macro_rules! p {
50+
() => {{
51+
#[cfg(nope)]
52+
fn h() {}
53+
#[cfg(not(nope))]
54+
fn h() {
55+
for _ in 1...10 {} //~ ERROR inclusive range syntax is experimental
56+
}
57+
58+
h();
59+
}}
60+
}
61+
62+
pub fn main() {
63+
for _ in 1...10 {} //~ ERROR inclusive range syntax is experimental
64+
for _ in ...10 {} //~ ERROR inclusive range syntax is experimental
65+
66+
f(); // not allowed in cfg'ed functions
67+
68+
m!(); // not allowed in macros
69+
n!(); // not allowed in cfg'ed macros
70+
o!(); // not allowed in macros that output cfgs
71+
p!(); // not allowed in cfg'ed macros that output cfgs
72+
}
73+
74+

src/test/run-pass/range_inclusive.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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)]
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+
/* FIXME
39+
let mut count = 0;
40+
for i in (0_usize...10).step_by(2) {
41+
assert!(i >= 0 && i <= 10 && i % 2 == 0);
42+
count += i;
43+
}
44+
assert_eq!(count, 30);
45+
*/
46+
47+
let _ = 0_usize...4+4-3;
48+
let _ = 0...foo();
49+
50+
let _ = { &42...&100 }; // references to literals are OK
51+
let _ = ...42_usize;
52+
53+
// Test we can use two different types with a common supertype.
54+
let x = &42;
55+
{
56+
let y = 42;
57+
let _ = x...&y;
58+
}
59+
60+
// test the size hints and emptying
61+
let mut long = 0...255u8;
62+
let mut short = 42...42;
63+
assert_eq!(long.size_hint(), (256, Some(256)));
64+
assert_eq!(short.size_hint(), (1, Some(1)));
65+
long.next();
66+
short.next();
67+
assert_eq!(long.size_hint(), (255, Some(255)));
68+
assert_eq!(short.size_hint(), (0, Some(0)));
69+
assert_eq!(short, RangeInclusive::Empty { at: 42 });
70+
71+
assert_eq!(long.len(), 255);
72+
assert_eq!(short.len(), 0);
73+
74+
// test iterating backwards
75+
assert_eq!(long.next_back(), Some(255));
76+
assert_eq!(long.next_back(), Some(254));
77+
assert_eq!(long.next_back(), Some(253));
78+
assert_eq!(long.next(), Some(1));
79+
assert_eq!(long.next(), Some(2));
80+
assert_eq!(long.next_back(), Some(252));
81+
for i in 3...251 {
82+
assert_eq!(long.next(), Some(i));
83+
}
84+
assert_eq!(long, RangeInclusive::Empty { at: 251 });
85+
86+
// what happens if you have a nonsense range?
87+
let mut nonsense = 10...5;
88+
assert_eq!(nonsense.next(), None);
89+
assert_eq!(nonsense, RangeInclusive::Empty { at: 10 });
90+
91+
// conversion
92+
assert_eq!(0...9, (0..10).into());
93+
assert_eq!(0...0, (0..1).into());
94+
assert_eq!(RangeInclusive::Empty { at: 1 }, (1..0).into());
95+
96+
// output
97+
assert_eq!(format!("{:?}", 0...10), "0...10");
98+
assert_eq!(format!("{:?}", ...10), "...10");
99+
assert_eq!(format!("{:?}", long), "[empty range @ 251]");
100+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 that you only need the syntax gate if you don't mention the structs.
12+
13+
#![feature(inclusive_range_syntax)]
14+
15+
fn main() {
16+
let mut count = 0;
17+
for i in 0_usize...10 {
18+
assert!(i >= 0 && i <= 10);
19+
count += i;
20+
}
21+
assert_eq!(count, 55);
22+
}
23+

0 commit comments

Comments
 (0)