rust/tests/run-pass/enums.rs

60 lines
1.3 KiB
Rust
Raw Normal View History

2016-09-30 10:45:52 +02:00
enum MyEnum {
MyEmptyVariant,
MyNewtypeVariant(i32),
MyTupleVariant(i32, i32),
MyStructVariant {
my_first_field: i32,
my_second_field: i32,
}
}
fn test(me: MyEnum) {
match me {
MyEnum::MyEmptyVariant => {},
MyEnum::MyNewtypeVariant(ref val) => assert_eq!(val, &42),
MyEnum::MyTupleVariant(ref a, ref b) => {
assert_eq!(a, &43);
assert_eq!(b, &44);
},
MyEnum::MyStructVariant { ref my_first_field, ref my_second_field } => {
assert_eq!(my_first_field, &45);
assert_eq!(my_second_field, &46);
},
}
}
fn discriminant_overflow() {
// Tests for https://github.com/rust-lang/rust/issues/62138.
#[repr(u8)]
#[allow(dead_code)]
enum WithWraparoundInvalidValues {
X = 1,
Y = 254,
}
#[allow(dead_code)]
enum Foo {
A,
B,
C(WithWraparoundInvalidValues),
}
let x = Foo::B;
2019-08-10 18:38:41 +02:00
match x {
Foo::B => {},
_ => panic!(),
}
}
2016-09-30 10:45:52 +02:00
fn main() {
test(MyEnum::MyEmptyVariant);
test(MyEnum::MyNewtypeVariant(42));
test(MyEnum::MyTupleVariant(43, 44));
test(MyEnum::MyStructVariant{
my_first_field: 45,
my_second_field: 46,
});
discriminant_overflow();
2016-09-30 10:45:52 +02:00
}