Skip to content

Commit 6eed0b7

Browse files
committed
test: compare contracts to ensure adding unrelated contracts has no impact
Ref: #18 NOTE: the hash value differs, but callee_sorted_by_file_func is identical. thread 'compare_contract' panicked at tests/snapshots.rs:78:5: assertion failed: `(left == right)` Diff < left / right > : <929236091286910200016542852190015891876 >160785969163118787202231336083974075582
1 parent 3e3c950 commit 6eed0b7

File tree

8 files changed

+2633
-4
lines changed

8 files changed

+2633
-4
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tracing-error = "0.2"
2121
[dev-dependencies]
2222
assert_cmd = "2.0.16"
2323
expect-test = "1.5.1"
24+
pretty_assertions = "1.4.1"
2425

2526
[lints.rust]
2627
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kani)'] }

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ pub struct Callee {
2424
pub file: String,
2525
pub func: String,
2626
}
27+
28+
impl SerFunction {
29+
/// Even though callees are in insertion order, to make them easy to check,
30+
/// sort by file and function.
31+
pub fn callee_sorted_by_file_func(&self) -> Vec<[&str; 2]> {
32+
let mut callees: Vec<_> = self.callees.iter().collect();
33+
callees.sort_by(|a, b| (&a.file, &a.func).cmp(&(&b.file, &b.func)));
34+
callees.into_iter().map(|c| [&*c.file, &*c.func]).collect()
35+
}
36+
}

tests/compare/contract1.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[cfg(kani)]
2+
mod verify {
3+
#[kani::requires(a > 0)]
4+
pub fn contract(a: u8) {}
5+
6+
#[kani::proof]
7+
pub fn f() {
8+
contract(0);
9+
}
10+
}

tests/compare/contract2.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[cfg(kani)]
2+
mod verify {
3+
#[kani::requires(a > 0)]
4+
pub fn contract(a: u8) {}
5+
6+
#[kani::proof]
7+
pub fn f() {
8+
contract(0);
9+
}
10+
11+
#[kani::requires(a > 0)]
12+
pub fn g(a: u8) {}
13+
}

tests/snapshots.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use assert_cmd::Command;
22
use distributed_verification::SerFunction;
33
use expect_test::expect_file;
4+
use pretty_assertions::assert_eq;
45
use std::fs::{copy, remove_file};
56

67
fn cmd(args: &[&str]) -> String {
@@ -26,9 +27,10 @@ fn standard_proofs_with_contracts() {
2627
expect_file!["./snapshots/standard_proofs_with_contracts.json"].assert_eq(&json);
2728
}
2829

29-
fn get_hash(text: &str, start: &str) -> String {
30-
let v: Vec<SerFunction> = serde_json::from_str(text).unwrap();
31-
v.into_iter().find(|f| f.func.starts_with(start)).unwrap().hash
30+
fn get(text: &str, start: &str) -> SerFunction {
31+
let json = &text[text.find("[\n").unwrap()..];
32+
let v: Vec<SerFunction> = serde_json::from_str(json).unwrap();
33+
v.into_iter().find(|f| f.func.starts_with(start)).unwrap()
3234
}
3335

3436
#[test]
@@ -49,5 +51,29 @@ fn compare_proof() {
4951
let f = "pub fn f()";
5052
// For the same proof (w.r.t same path and body),
5153
// the hash value must be the same.
52-
assert_eq!(get_hash(text1, f), get_hash(text2, f));
54+
assert_eq!(get(text1, f).hash, get(text2, f).hash);
55+
}
56+
57+
#[test]
58+
fn compare_contract() {
59+
let file = "tests/compare/contract.rs";
60+
61+
copy("tests/compare/contract1.rs", file).unwrap();
62+
let text1 = &cmd(&["tests/compare/contract.rs"]);
63+
expect_file!["./snapshots/contract1.json"].assert_eq(text1);
64+
65+
copy("tests/compare/contract2.rs", file).unwrap();
66+
let text2 = &cmd(&["tests/compare/contract.rs"]);
67+
expect_file!["./snapshots/contract2.json"].assert_eq(text2);
68+
69+
remove_file(file).unwrap();
70+
71+
let f = "pub fn f()";
72+
let f1 = get(text1, f);
73+
let f2 = get(text2, f);
74+
let callees1 = f1.callee_sorted_by_file_func();
75+
let callees2 = f2.callee_sorted_by_file_func();
76+
77+
assert_eq!(callees1, callees2);
78+
assert_eq!(f1.hash, f2.hash);
5379
}

tests/snapshots/contract1.json

Lines changed: 1268 additions & 0 deletions
Large diffs are not rendered by default.

tests/snapshots/contract2.json

Lines changed: 1278 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)