mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-25 13:24:22 +00:00
Basic run-pass tests for or-patterns
Add some basic run-pass ui tests for or-patterns.
This commit is contained in:
parent
30058df867
commit
0b1ff27cd8
33
src/test/ui/or-patterns/basic-switch.rs
Normal file
33
src/test/ui/or-patterns/basic-switch.rs
Normal file
@ -0,0 +1,33 @@
|
||||
// Test basic or-patterns when the target pattern type will be lowered to a
|
||||
// `Switch` (an `enum`).
|
||||
|
||||
// run-pass
|
||||
|
||||
#![feature(or_patterns)]
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Test {
|
||||
Foo,
|
||||
Bar,
|
||||
Baz,
|
||||
Qux,
|
||||
}
|
||||
|
||||
fn test(x: Option<Test>) -> bool {
|
||||
match x {
|
||||
// most simple case
|
||||
Some(Test::Bar | Test::Qux) => true,
|
||||
// wild case
|
||||
Some(_) => false,
|
||||
// empty case
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert!(!test(Some(Test::Foo)));
|
||||
assert!(test(Some(Test::Bar)));
|
||||
assert!(!test(Some(Test::Baz)));
|
||||
assert!(test(Some(Test::Qux)));
|
||||
assert!(!test(None))
|
||||
}
|
57
src/test/ui/or-patterns/basic-switchint.rs
Normal file
57
src/test/ui/or-patterns/basic-switchint.rs
Normal file
@ -0,0 +1,57 @@
|
||||
// Test basic or-patterns when the target pattern type will be lowered to
|
||||
// a `SwitchInt`. This will happen when the target type is an integer.
|
||||
|
||||
// run-pass
|
||||
|
||||
#![feature(or_patterns)]
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum MatchArm {
|
||||
Arm(usize),
|
||||
Wild,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Foo {
|
||||
One(usize),
|
||||
Two(usize, usize),
|
||||
}
|
||||
|
||||
fn test_foo(x: Foo) -> MatchArm {
|
||||
match x {
|
||||
// normal pattern.
|
||||
Foo::One(0) | Foo::One(1) | Foo::One(2) => MatchArm::Arm(0),
|
||||
// most simple or-pattern.
|
||||
Foo::One(42 | 255) => MatchArm::Arm(1),
|
||||
// multiple or-patterns for one structure.
|
||||
Foo::Two(42 | 255, 1024 | 2048) => MatchArm::Arm(2),
|
||||
// mix of pattern types in one or-pattern (range).
|
||||
//
|
||||
// FIXME(dlrobertson | Nadrieril): Fix or-pattern completeness and
|
||||
// unreachabilitychecks for ranges.
|
||||
Foo::One(100 | 110..=120 | 210..=220) => MatchArm::Arm(3),
|
||||
// multiple or-patterns with wild.
|
||||
Foo::Two(0..=10 | 100..=110, 0 | _) => MatchArm::Arm(4),
|
||||
// wild
|
||||
_ => MatchArm::Wild,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// `Foo` tests.
|
||||
assert_eq!(test_foo(Foo::One(0)), MatchArm::Arm(0));
|
||||
assert_eq!(test_foo(Foo::One(42)), MatchArm::Arm(1));
|
||||
assert_eq!(test_foo(Foo::One(43)), MatchArm::Wild);
|
||||
assert_eq!(test_foo(Foo::One(255)), MatchArm::Arm(1));
|
||||
assert_eq!(test_foo(Foo::One(256)), MatchArm::Wild);
|
||||
assert_eq!(test_foo(Foo::Two(42, 1023)), MatchArm::Wild);
|
||||
assert_eq!(test_foo(Foo::Two(255, 2048)), MatchArm::Arm(2));
|
||||
assert_eq!(test_foo(Foo::One(100)), MatchArm::Arm(3));
|
||||
assert_eq!(test_foo(Foo::One(115)), MatchArm::Arm(3));
|
||||
assert_eq!(test_foo(Foo::One(105)), MatchArm::Wild);
|
||||
assert_eq!(test_foo(Foo::One(215)), MatchArm::Arm(3));
|
||||
assert_eq!(test_foo(Foo::One(121)), MatchArm::Wild);
|
||||
assert_eq!(test_foo(Foo::Two(0, 42)), MatchArm::Arm(4));
|
||||
assert_eq!(test_foo(Foo::Two(100, 0)), MatchArm::Arm(4));
|
||||
assert_eq!(test_foo(Foo::Two(42, 0)), MatchArm::Wild);
|
||||
}
|
19
src/test/ui/or-patterns/mix-with-wild.rs
Normal file
19
src/test/ui/or-patterns/mix-with-wild.rs
Normal file
@ -0,0 +1,19 @@
|
||||
// Test that an or-pattern works with a wild pattern. This tests two things:
|
||||
//
|
||||
// 1) The Wild pattern should cause the pattern to always succeed.
|
||||
// 2) or-patterns should work with simplifyable patterns.
|
||||
|
||||
// run-pass
|
||||
#![feature(or_patterns)]
|
||||
|
||||
pub fn test(x: Option<usize>) -> bool {
|
||||
match x {
|
||||
Some(0 | _) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert!(test(Some(42)));
|
||||
assert!(!test(None));
|
||||
}
|
42
src/test/ui/or-patterns/struct-like.rs
Normal file
42
src/test/ui/or-patterns/struct-like.rs
Normal file
@ -0,0 +1,42 @@
|
||||
// run-pass
|
||||
|
||||
#![feature(or_patterns)]
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Other {
|
||||
One,
|
||||
Two,
|
||||
Three,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Test {
|
||||
Foo { first: usize, second: usize },
|
||||
Bar { other: Option<Other> },
|
||||
Baz,
|
||||
}
|
||||
|
||||
fn test(x: Option<Test>) -> bool {
|
||||
match x {
|
||||
Some(
|
||||
Test::Foo { first: 1024 | 2048, second: 2048 | 4096 }
|
||||
| Test::Bar { other: Some(Other::One | Other::Two) },
|
||||
) => true,
|
||||
// wild case
|
||||
Some(_) => false,
|
||||
// empty case
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert!(test(Some(Test::Foo { first: 1024, second: 4096 })));
|
||||
assert!(!test(Some(Test::Foo { first: 2048, second: 8192 })));
|
||||
assert!(!test(Some(Test::Foo { first: 42, second: 2048 })));
|
||||
assert!(test(Some(Test::Bar { other: Some(Other::One) })));
|
||||
assert!(test(Some(Test::Bar { other: Some(Other::Two) })));
|
||||
assert!(!test(Some(Test::Bar { other: Some(Other::Three) })));
|
||||
assert!(!test(Some(Test::Bar { other: None })));
|
||||
assert!(!test(Some(Test::Baz)));
|
||||
assert!(!test(None));
|
||||
}
|
Loading…
Reference in New Issue
Block a user