Skip to content

Commit 67b3fb0

Browse files
committed
test: compare gen_proofs_by_nested_macros that they should have distinct hash
NOTE: the assert_ne! assertion fails thread 'test_compare' panicked at tests/compare.rs:53:9: assertion `left != right` failed: Hash values of "fn" should not equal: SerFunction { hash: "1628665993055122224710783236985879540321", def_id: "DefId { id: 0, name: \"verify::proof1\" }", attrs: [ "#[kanitool::proof]", ], func: SourceCode { name: "verify::proof1", mangled_name: "_ZN27gen_proofs_by_nested_macros6verify6proof117h4d153120b87a21d4E", kind: "Item", file: "tests/compare/gen_proofs_by_nested_macros.rs", src: "fn", before_expansion: Some( "inner!($name, $block)", ), }, callees_len: 6, callees: [], } vs SerFunction { hash: "1628665993055122224710783236985879540321", def_id: "DefId { id: 0, name: \"verify::proof1\" }", attrs: [ "#[kanitool::proof]", ], func: SourceCode { name: "verify::proof1", mangled_name: "_ZN27gen_proofs_by_nested_macros6verify6proof117h4d153120b87a21d4E", kind: "Item", file: "tests/compare/gen_proofs_by_nested_macros.rs", src: "fn", before_expansion: Some( "inner!($name, $block)", ), }, callees_len: 6, callees: [], } left: "1628665993055122224710783236985879540321" right: "1628665993055122224710783236985879540321"
1 parent 264584a commit 67b3fb0

File tree

5 files changed

+246
-11
lines changed

5 files changed

+246
-11
lines changed

tests/compare.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn get(text: &str, start: &str) -> SerFunction {
1111

1212
const COMPARE: &str = "tests/compare";
1313

14-
fn compare(tmp: &str, v_file: &[&str], f: &str) {
14+
fn compare(tmp: &str, v_file: &[&str], f: &str, assert: impl Fn(&SerFunction, &SerFunction, &str)) {
1515
let len = v_file.len();
1616
assert!(len > 1);
1717
let tmp = format!("{COMPARE}/{tmp}.rs");
@@ -30,20 +30,49 @@ fn compare(tmp: &str, v_file: &[&str], f: &str) {
3030
// the hash value must be the same.
3131
for i in 0..len - 1 {
3232
for j in 1..len {
33-
assert_eq!(
34-
v_func[i].hash, v_func[j].hash,
35-
"Hash values of {f:?} are not equal: {} ≠ {}",
36-
v_file[i], v_file[j]
37-
);
38-
// if v_func[i].hash == v_func[j].hash {
39-
// println!("Hash values of {f:?} are not equal: {} ≠ {}", v_file[i], v_file[j]);
40-
// }
33+
assert(&v_func[i], &v_func[j], f);
4134
}
4235
}
4336
}
4437

4538
#[test]
4639
fn test_compare() {
47-
compare("proof", &["proof1", "proof2"], "pub fn f()");
48-
compare("contract", &["contract1", "contract2"], "pub fn f()");
40+
fn eq(fn1: &SerFunction, fn2: &SerFunction, f: &str) {
41+
assert_eq!(
42+
fn1.hash,
43+
fn2.hash,
44+
"Hash values of {f:?} are not equal: {f1:#?} ≠ {f2:#?}",
45+
f1 = simplify_ser_function(fn1),
46+
f2 = simplify_ser_function(fn2),
47+
);
48+
}
49+
compare("proof", &["proof1", "proof2"], "pub fn f()", eq);
50+
compare("contract", &["contract1", "contract2"], "pub fn f()", eq);
51+
52+
fn not_eq(fn1: &SerFunction, fn2: &SerFunction, f: &str) {
53+
assert_ne!(
54+
fn1.hash,
55+
fn2.hash,
56+
"Hash values of {f:?} should not equal: {f1:#?} vs {f2:#?}",
57+
f1 = simplify_ser_function(fn1),
58+
f2 = simplify_ser_function(fn2),
59+
);
60+
}
61+
compare(
62+
"gen_proofs_by_nested_macros",
63+
&["gen_proofs_by_nested_macros1", "gen_proofs_by_nested_macros2"],
64+
"fn",
65+
not_eq,
66+
);
67+
}
68+
69+
fn simplify_ser_function(fn1: &SerFunction) -> SerFunction {
70+
SerFunction {
71+
hash: fn1.hash.clone(),
72+
def_id: fn1.def_id.clone(),
73+
attrs: fn1.attrs.clone(),
74+
func: fn1.func.clone(),
75+
callees_len: fn1.callees_len,
76+
..Default::default()
77+
}
4978
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
macro_rules! outer {
2+
($name:ident, $block:block) => {
3+
inner!($name, $block);
4+
};
5+
}
6+
7+
macro_rules! inner {
8+
($name:ident, $block:block) => {
9+
#[kani::proof]
10+
fn $name() $block
11+
}
12+
}
13+
14+
#[cfg(kani)]
15+
mod verify {
16+
outer! { proof1, { assert_eq!(kani::any::<u8>(), 0) }}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
macro_rules! outer {
2+
($name:ident, $block:block) => {
3+
inner!($name, $block);
4+
};
5+
}
6+
7+
macro_rules! inner {
8+
($name:ident, $block:block) => {
9+
#[kani::proof]
10+
fn $name() $block
11+
}
12+
}
13+
14+
#[cfg(kani)]
15+
mod verify {
16+
outer! { proof1, { assert_eq!(kani::any::<u8>(), 1) }}
17+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[
2+
{
3+
"hash": "1628665993055122224710783236985879540321",
4+
"def_id": "DefId { id: 0, name: \"verify::proof1\" }",
5+
"attrs": [
6+
"#[kanitool::proof]"
7+
],
8+
"func": {
9+
"name": "verify::proof1",
10+
"mangled_name": "_ZN27gen_proofs_by_nested_macros6verify6proof117h4d153120b87a21d4E",
11+
"kind": "Item",
12+
"file": "tests/compare/gen_proofs_by_nested_macros.rs",
13+
"src": "fn",
14+
"before_expansion": "inner!($name, $block)"
15+
},
16+
"callees_len": 6,
17+
"callees": [
18+
{
19+
"def_id": "DefId { id: 4, name: \"<u8 as kani::Arbitrary>::any\" }",
20+
"func": {
21+
"name": "<u8 as kani::Arbitrary>::any",
22+
"mangled_name": "_ZN38_$LT$u8$u20$as$u20$kani..Arbitrary$GT$3any17hfd29445e7b2cdd95E",
23+
"kind": "Item",
24+
"file": "kani/library/kani_core/src/arbitrary.rs",
25+
"src": "fn any() -> Self {\n // This size_of call does not use generic_const_exprs feature. It's inside a macro, and Self isn't generic.\n unsafe { crate::kani::any_raw_internal::<Self>() }\n }",
26+
"before_expansion": "trivial_arbitrary!(u8)"
27+
}
28+
},
29+
{
30+
"def_id": "DefId { id: 1, name: \"kani::any\" }",
31+
"func": {
32+
"name": "kani::any::<u8>",
33+
"mangled_name": "_ZN4kani3any17h800d752a2e66bb13E",
34+
"kind": "Item",
35+
"file": "kani/library/kani_core/src/lib.rs",
36+
"src": "pub fn any<T: Arbitrary>() -> T {\n T::any()\n }",
37+
"before_expansion": "kani_core::kani_intrinsics!(std)"
38+
}
39+
},
40+
{
41+
"def_id": "DefId { id: 6, name: \"kani::any_raw\" }",
42+
"func": {
43+
"name": "kani::any_raw::<u8>",
44+
"mangled_name": "_ZN4kani7any_raw17hef764f0401a0f588E",
45+
"kind": "Item",
46+
"file": "kani/library/kani_core/src/lib.rs",
47+
"src": "fn any_raw<T: Copy>() -> T {\n kani_intrinsic()\n }",
48+
"before_expansion": "kani_core::kani_intrinsics!(std)"
49+
}
50+
},
51+
{
52+
"def_id": "DefId { id: 5, name: \"kani::any_raw_internal\" }",
53+
"func": {
54+
"name": "kani::any_raw_internal::<u8>",
55+
"mangled_name": "_ZN4kani16any_raw_internal17h86fbfff4755b5586E",
56+
"kind": "Item",
57+
"file": "kani/library/kani_core/src/lib.rs",
58+
"src": "unsafe fn any_raw_internal<T: Copy>() -> T {\n any_raw::<T>()\n }",
59+
"before_expansion": "kani_core::kani_intrinsics!(std)"
60+
}
61+
},
62+
{
63+
"def_id": "DefId { id: 2, name: \"kani::assert\" }",
64+
"func": {
65+
"name": "kani::assert",
66+
"mangled_name": "_ZN4kani6assert17hcc89e8c2e0f13823E",
67+
"kind": "Item",
68+
"file": "kani/library/kani_core/src/lib.rs",
69+
"src": "pub const fn assert(cond: bool, msg: &'static str) {\n let _ = cond;\n let _ = msg;\n }",
70+
"before_expansion": "kani_core::kani_intrinsics!(std)"
71+
}
72+
},
73+
{
74+
"def_id": "DefId { id: 7, name: \"kani::kani_intrinsic\" }",
75+
"func": {
76+
"name": "kani::kani_intrinsic::<u8>",
77+
"mangled_name": "_ZN4kani14kani_intrinsic17h4c94b2d1e6f52b91E",
78+
"kind": "Item",
79+
"file": "kani/library/kani_core/src/lib.rs",
80+
"src": "fn kani_intrinsic<T>() -> T {\n #[allow(clippy::empty_loop)]\n loop {}\n }",
81+
"before_expansion": "kani_core::kani_intrinsics!(std)"
82+
}
83+
}
84+
]
85+
}
86+
]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[
2+
{
3+
"hash": "1628665993055122224710783236985879540321",
4+
"def_id": "DefId { id: 0, name: \"verify::proof1\" }",
5+
"attrs": [
6+
"#[kanitool::proof]"
7+
],
8+
"func": {
9+
"name": "verify::proof1",
10+
"mangled_name": "_ZN27gen_proofs_by_nested_macros6verify6proof117h4d153120b87a21d4E",
11+
"kind": "Item",
12+
"file": "tests/compare/gen_proofs_by_nested_macros.rs",
13+
"src": "fn",
14+
"before_expansion": "inner!($name, $block)"
15+
},
16+
"callees_len": 6,
17+
"callees": [
18+
{
19+
"def_id": "DefId { id: 4, name: \"<u8 as kani::Arbitrary>::any\" }",
20+
"func": {
21+
"name": "<u8 as kani::Arbitrary>::any",
22+
"mangled_name": "_ZN38_$LT$u8$u20$as$u20$kani..Arbitrary$GT$3any17hfd29445e7b2cdd95E",
23+
"kind": "Item",
24+
"file": "kani/library/kani_core/src/arbitrary.rs",
25+
"src": "fn any() -> Self {\n // This size_of call does not use generic_const_exprs feature. It's inside a macro, and Self isn't generic.\n unsafe { crate::kani::any_raw_internal::<Self>() }\n }",
26+
"before_expansion": "trivial_arbitrary!(u8)"
27+
}
28+
},
29+
{
30+
"def_id": "DefId { id: 1, name: \"kani::any\" }",
31+
"func": {
32+
"name": "kani::any::<u8>",
33+
"mangled_name": "_ZN4kani3any17h800d752a2e66bb13E",
34+
"kind": "Item",
35+
"file": "kani/library/kani_core/src/lib.rs",
36+
"src": "pub fn any<T: Arbitrary>() -> T {\n T::any()\n }",
37+
"before_expansion": "kani_core::kani_intrinsics!(std)"
38+
}
39+
},
40+
{
41+
"def_id": "DefId { id: 6, name: \"kani::any_raw\" }",
42+
"func": {
43+
"name": "kani::any_raw::<u8>",
44+
"mangled_name": "_ZN4kani7any_raw17hef764f0401a0f588E",
45+
"kind": "Item",
46+
"file": "kani/library/kani_core/src/lib.rs",
47+
"src": "fn any_raw<T: Copy>() -> T {\n kani_intrinsic()\n }",
48+
"before_expansion": "kani_core::kani_intrinsics!(std)"
49+
}
50+
},
51+
{
52+
"def_id": "DefId { id: 5, name: \"kani::any_raw_internal\" }",
53+
"func": {
54+
"name": "kani::any_raw_internal::<u8>",
55+
"mangled_name": "_ZN4kani16any_raw_internal17h86fbfff4755b5586E",
56+
"kind": "Item",
57+
"file": "kani/library/kani_core/src/lib.rs",
58+
"src": "unsafe fn any_raw_internal<T: Copy>() -> T {\n any_raw::<T>()\n }",
59+
"before_expansion": "kani_core::kani_intrinsics!(std)"
60+
}
61+
},
62+
{
63+
"def_id": "DefId { id: 2, name: \"kani::assert\" }",
64+
"func": {
65+
"name": "kani::assert",
66+
"mangled_name": "_ZN4kani6assert17hcc89e8c2e0f13823E",
67+
"kind": "Item",
68+
"file": "kani/library/kani_core/src/lib.rs",
69+
"src": "pub const fn assert(cond: bool, msg: &'static str) {\n let _ = cond;\n let _ = msg;\n }",
70+
"before_expansion": "kani_core::kani_intrinsics!(std)"
71+
}
72+
},
73+
{
74+
"def_id": "DefId { id: 7, name: \"kani::kani_intrinsic\" }",
75+
"func": {
76+
"name": "kani::kani_intrinsic::<u8>",
77+
"mangled_name": "_ZN4kani14kani_intrinsic17h4c94b2d1e6f52b91E",
78+
"kind": "Item",
79+
"file": "kani/library/kani_core/src/lib.rs",
80+
"src": "fn kani_intrinsic<T>() -> T {\n #[allow(clippy::empty_loop)]\n loop {}\n }",
81+
"before_expansion": "kani_core::kani_intrinsics!(std)"
82+
}
83+
}
84+
]
85+
}
86+
]

0 commit comments

Comments
 (0)