Skip to content

Commit 49999c2

Browse files
adpaco-awstedinski
authored andcommitted
Add a test for discriminant_value (rust-lang#1021)
* Add a test for `discriminant_value` * Split into 4 proofs
1 parent 2ac4b91 commit 49999c2

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that `discriminant_value` returns the expected results
5+
// for different cases
6+
#![feature(core_intrinsics)]
7+
use std::intrinsics::discriminant_value;
8+
9+
// A standard enum with variants containing fields
10+
enum MyError {
11+
Error1(i32),
12+
Error2(&'static str),
13+
Error3 { description: String, code: u32 },
14+
}
15+
16+
#[kani::proof]
17+
fn test_standard_enum() {
18+
// Check that the values go from 0 to `num_variants - 1`
19+
assert!(discriminant_value(&MyError::Error1) == 0);
20+
assert!(discriminant_value(&MyError::Error2("bar")) == 1);
21+
assert!(
22+
discriminant_value(&MyError::Error3 { description: "some_error".to_string(), code: 3 })
23+
== 2
24+
);
25+
}
26+
27+
// An enum that assigns constant values to some variants
28+
enum Constants {
29+
A = 2,
30+
B = 5,
31+
C,
32+
}
33+
34+
#[kani::proof]
35+
fn test_constants_enum() {
36+
// Check that the values are equal to the constants assigned
37+
assert!(discriminant_value(&Ordering::Less) == -1);
38+
assert!(discriminant_value(&Ordering::Equal) == 0);
39+
assert!(discriminant_value(&Ordering::Greater) == 1);
40+
}
41+
42+
// An enum that assigns constant values (one of them negative) to all variants
43+
enum Ordering {
44+
Less = -1,
45+
Equal = 0,
46+
Greater = 1,
47+
}
48+
49+
#[kani::proof]
50+
fn test_ordering_enum() {
51+
// Check that the values are equal to the constants assigned
52+
// and the non-assigned value follows from the assigned ones
53+
assert!(discriminant_value(&Constants::A) == 2);
54+
assert!(discriminant_value(&Constants::B) == 5);
55+
assert!(discriminant_value(&Constants::C) == 6);
56+
}
57+
58+
#[kani::proof]
59+
fn test_no_enum() {
60+
// Check that the value is 0 if the type has no discriminant
61+
assert!(discriminant_value(&2) == 0);
62+
}

0 commit comments

Comments
 (0)