Skip to content

Commit 9607675

Browse files
authored
Merge pull request #20 from os-checker/tests-diff
Chore: Simplify testing for proofs and snapshots folders
2 parents 585e479 + 2649a73 commit 9607675

File tree

12 files changed

+138
-117
lines changed

12 files changed

+138
-117
lines changed

.github/workflows/test.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Run Tests
2+
3+
on:
4+
push:
5+
branches:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
13+
- name: Run tests
14+
run: cargo test -- --nocapture

src/functions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct Function {
5252
/// Raw function string, including name, signature, and body.
5353
func: String,
5454
/// Recursive fnction calls inside the body.
55-
/// This set has insertion order, which is nice to check out call contexts.
55+
/// The elements are sorted by file path and fn source code to keep hash value stable.
5656
callees: IndexSet<Instance>,
5757
}
5858

src/functions/serialization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct SerFunction {
1919
attrs: Vec<String>,
2020
/// Raw function string, including name, signature, and body.
2121
func: String,
22-
/// Recursive fnction calls inside the body.
22+
/// Recursive function calls inside the proof.
2323
callees: Vec<Callee>,
2424
}
2525

src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,3 @@ 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.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use distributed_verification::SerFunction;
2+
use std::fs::{copy, remove_file};
3+
4+
mod utils;
5+
use utils::{assert_eq, cmd, expect_file};
6+
7+
fn get(text: &str, start: &str) -> SerFunction {
8+
let json = &text[text.find("[\n").unwrap()..];
9+
let v: Vec<SerFunction> = serde_json::from_str(json).unwrap();
10+
v.into_iter().find(|f| f.func.starts_with(start)).unwrap()
11+
}
12+
13+
const COMPARE: &str = "tests/compare";
14+
15+
fn compare(tmp: &str, v_file: &[&str], f: &str) {
16+
let len = v_file.len();
17+
assert!(len > 1);
18+
let tmp = format!("{COMPARE}/{tmp}.rs");
19+
20+
let mut v_func = vec![];
21+
for ele in v_file {
22+
copy(format!("{COMPARE}/{ele}.rs"), &tmp).unwrap();
23+
let text = cmd(&[&tmp]);
24+
expect_file![format!("./snapshots/{ele}.json")].assert_eq(&text);
25+
v_func.push(get(&text, f));
26+
}
27+
28+
remove_file(tmp).unwrap();
29+
30+
// For the same proof (w.r.t same path and body),
31+
// the hash value must be the same.
32+
for i in 0..len - 1 {
33+
for j in 1..len {
34+
assert_eq!(
35+
v_func[i].hash, v_func[j].hash,
36+
"Hash values are not equal: {} ≠ {}",
37+
v_file[i], v_file[j]
38+
);
39+
}
40+
}
41+
}
42+
43+
#[test]
44+
fn test_compare() {
45+
compare("proof", &["proof1", "proof2"], "pub fn f()");
46+
compare("contract", &["contract1", "contract2"], "pub fn f()");
47+
}

tests/proofs.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
mod utils;
2+
use utils::*;
3+
4+
#[test]
5+
fn test_proofs() -> eyre::Result<()> {
6+
let mut proofs = vec![];
7+
for entry in std::fs::read_dir("tests/proofs")? {
8+
let entry = entry?;
9+
let path = entry.path();
10+
if path.is_file()
11+
&& path.extension().and_then(|ext| Some(ext.to_str()? == "rs")).unwrap_or(false)
12+
{
13+
proofs.push(path);
14+
}
15+
}
16+
17+
expect![[r#"
18+
[
19+
"tests/proofs/standard_proofs_with_contracts.rs",
20+
"tests/proofs/standard_proofs.rs",
21+
]
22+
"#]]
23+
.assert_debug_eq(&proofs);
24+
25+
for path in &proofs {
26+
let file_stem = path.file_stem().and_then(|f| f.to_str()).unwrap();
27+
let json = cmd(&[&format!("tests/proofs/{file_stem}.rs")]);
28+
expect_file![format!("./snapshots/{file_stem}.json")].assert_eq(&json);
29+
}
30+
31+
Ok(())
32+
}
File renamed without changes.

tests/snapshots.rs

Lines changed: 0 additions & 81 deletions
This file was deleted.

tests/snapshots/standard_proofs.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
[
22
{
3-
"hash": "42109808322873561223875447740690337681",
3+
"hash": "46975737574545431849805961234274153517",
44
"def_id": "DefId { id: 1, name: \"verify::standard_proof_empty\" }",
5-
"file": "tests/standard_proofs.rs",
5+
"file": "tests/proofs/standard_proofs.rs",
66
"attrs": [
77
"#[kanitool::proof]"
88
],
99
"func": "fn standard_proof_empty() {}",
1010
"callees": []
1111
},
1212
{
13-
"hash": "119935654822495658609677948466451012436",
13+
"hash": "154269283551974763216917429380903124467",
1414
"def_id": "DefId { id: 0, name: \"verify::standard_proof\" }",
15-
"file": "tests/standard_proofs.rs",
15+
"file": "tests/proofs/standard_proofs.rs",
1616
"attrs": [
1717
"#[kanitool::proof]"
1818
],
@@ -51,9 +51,9 @@
5151
]
5252
},
5353
{
54-
"hash": "353125589356689115114026576863231049982",
54+
"hash": "58801092890548413495612860413695445693",
5555
"def_id": "DefId { id: 2, name: \"verify::recursive_callees\" }",
56-
"file": "tests/standard_proofs.rs",
56+
"file": "tests/proofs/standard_proofs.rs",
5757
"attrs": [
5858
"#[kanitool::proof]"
5959
],
@@ -596,12 +596,12 @@
596596
},
597597
{
598598
"def_id": "DefId { id: 4, name: \"m::func1\" }",
599-
"file": "tests/standard_proofs.rs",
599+
"file": "tests/proofs/standard_proofs.rs",
600600
"func": "pub fn func1() {\n let _a = \"\".trim();\n }"
601601
},
602602
{
603603
"def_id": "DefId { id: 3, name: \"top_level\" }",
604-
"file": "tests/standard_proofs.rs",
604+
"file": "tests/proofs/standard_proofs.rs",
605605
"func": "pub fn top_level() {\n m::func1();\n}"
606606
}
607607
]

0 commit comments

Comments
 (0)