2014-03-22 07:55:46 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Tests that match expression handles overlapped literal and range
|
|
|
|
// properly in the presence of guard function.
|
|
|
|
|
|
|
|
fn val() -> uint { 1 }
|
|
|
|
|
|
|
|
static CONST: uint = 1;
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
lit_shadow_range();
|
|
|
|
range_shadow_lit();
|
|
|
|
range_shadow_range();
|
|
|
|
multi_pats_shadow_lit();
|
|
|
|
multi_pats_shadow_range();
|
|
|
|
lit_shadow_multi_pats();
|
|
|
|
range_shadow_multi_pats();
|
2014-05-04 17:16:16 -05:00
|
|
|
misc();
|
2014-03-22 07:55:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lit_shadow_range() {
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(2i, match 1i {
|
|
|
|
1 if false => 1i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1..2 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
let x = 0i;
|
|
|
|
assert_eq!(2i, match x+1 {
|
2014-06-27 14:30:25 -05:00
|
|
|
0 => 0i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 if false => 1,
|
|
|
|
1..2 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
assert_eq!(2i, match val() {
|
2014-06-27 14:30:25 -05:00
|
|
|
1 if false => 1i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1..2 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
assert_eq!(2i, match CONST {
|
2014-06-27 14:30:25 -05:00
|
|
|
0 => 0i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 if false => 1,
|
|
|
|
1..2 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
|
|
|
// value is out of the range of second arm, should match wildcard pattern
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(3i, match 3i {
|
|
|
|
1 if false => 1i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1..2 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_shadow_lit() {
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(2i, match 1i {
|
|
|
|
1..2 if false => 1i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
let x = 0i;
|
|
|
|
assert_eq!(2i, match x+1 {
|
2014-06-27 14:30:25 -05:00
|
|
|
0 => 0i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1..2 if false => 1,
|
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
assert_eq!(2i, match val() {
|
2014-06-27 14:30:25 -05:00
|
|
|
1..2 if false => 1i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
assert_eq!(2i, match CONST {
|
2014-06-27 14:30:25 -05:00
|
|
|
0 => 0i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1..2 if false => 1,
|
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
|
|
|
|
// ditto
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(3i, match 3i {
|
|
|
|
1..2 if false => 1i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 => 2,
|
|
|
|
_ => 3
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_shadow_range() {
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(2i, match 1i {
|
|
|
|
0..2 if false => 1i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1..3 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
let x = 0i;
|
|
|
|
assert_eq!(2i, match x+1 {
|
2014-03-22 07:55:46 -05:00
|
|
|
100 => 0,
|
|
|
|
0..2 if false => 1,
|
|
|
|
1..3 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
assert_eq!(2i, match val() {
|
2014-03-22 07:55:46 -05:00
|
|
|
0..2 if false => 1,
|
|
|
|
1..3 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
assert_eq!(2i, match CONST {
|
2014-03-22 07:55:46 -05:00
|
|
|
100 => 0,
|
|
|
|
0..2 if false => 1,
|
|
|
|
1..3 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
|
|
|
|
// ditto
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(3i, match 5i {
|
|
|
|
0..2 if false => 1i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1..3 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multi_pats_shadow_lit() {
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(2i, match 1i {
|
|
|
|
100 => 0i,
|
2014-03-22 07:55:46 -05:00
|
|
|
0 | 1..10 if false => 1,
|
|
|
|
1 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn multi_pats_shadow_range() {
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(2i, match 1i {
|
|
|
|
100 => 0i,
|
2014-03-22 07:55:46 -05:00
|
|
|
0 | 1..10 if false => 1,
|
|
|
|
1..3 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lit_shadow_multi_pats() {
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(2i, match 1i {
|
|
|
|
100 => 0i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1 if false => 1,
|
|
|
|
0 | 1..10 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_shadow_multi_pats() {
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(2i, match 1i {
|
|
|
|
100 => 0i,
|
2014-03-22 07:55:46 -05:00
|
|
|
1..3 if false => 1,
|
|
|
|
0 | 1..10 => 2,
|
|
|
|
_ => 3,
|
|
|
|
});
|
|
|
|
}
|
2014-05-04 17:16:16 -05:00
|
|
|
|
|
|
|
fn misc() {
|
|
|
|
enum Foo {
|
|
|
|
Bar(uint, bool)
|
|
|
|
}
|
|
|
|
// This test basically mimics how trace_macros! macro is implemented,
|
|
|
|
// which is a rare combination of vector patterns, multiple wild-card
|
|
|
|
// patterns and guard functions.
|
|
|
|
let r = match [Bar(0, false)].as_slice() {
|
2014-04-21 16:58:52 -05:00
|
|
|
[Bar(_, pred)] if pred => 1i,
|
|
|
|
[Bar(_, pred)] if !pred => 2i,
|
|
|
|
_ => 0i,
|
2014-05-04 17:16:16 -05:00
|
|
|
};
|
2014-06-27 14:30:25 -05:00
|
|
|
assert_eq!(2i, r);
|
2014-05-04 17:16:16 -05:00
|
|
|
}
|