Skip to content

Commit 59bcc96

Browse files
committed
test: assert_unique_hash for proofs
cc #35
1 parent 7f9de12 commit 59bcc96

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

tests/compare.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use distributed_verification::SerFunction;
21
use std::fs::{copy, remove_file};
32

43
mod utils;
5-
use utils::{assert_eq, cmd, expect_file};
4+
use utils::{assert_eq, *};
65

76
fn get(text: &str, start: &str) -> SerFunction {
87
let json = &text[text.find("[\n").unwrap()..];

tests/proofs.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use indexmap::IndexMap;
2+
use std::path::{Path, PathBuf};
3+
14
mod utils;
2-
use utils::*;
5+
use utils::{assert_eq, *};
36

4-
#[test]
5-
fn test_proofs() -> eyre::Result<()> {
7+
pub fn get_proofs() -> Result<Vec<PathBuf>> {
68
let mut proofs = vec![];
79
for entry in std::fs::read_dir("tests/proofs")? {
810
let entry = entry?;
@@ -13,8 +15,14 @@ fn test_proofs() -> eyre::Result<()> {
1315
proofs.push(path);
1416
}
1517
}
16-
1718
proofs.sort();
19+
Ok(proofs)
20+
}
21+
22+
#[test]
23+
fn test_proofs() -> Result<()> {
24+
let proofs = get_proofs()?;
25+
1826
expect![[r#"
1927
[
2028
"tests/proofs/ad_hoc.rs",
@@ -26,15 +34,40 @@ fn test_proofs() -> eyre::Result<()> {
2634
"#]]
2735
.assert_debug_eq(&proofs);
2836

37+
let mut v_json = Vec::<Vec<SerFunction>>::with_capacity(proofs.len());
2938
for path in &proofs {
3039
let file_stem = path.file_stem().and_then(|f| f.to_str()).unwrap();
31-
let json = cmd(&[&format!("tests/proofs/{file_stem}.rs")]);
32-
expect_file![format!("./snapshots/{file_stem}.json")].assert_eq(&json);
40+
let text = cmd(&[&format!("tests/proofs/{file_stem}.rs")]);
41+
expect_file![format!("./snapshots/{file_stem}.json")].assert_eq(&text);
42+
v_json.push(serde_json::from_str(&text).unwrap());
3343
}
3444

45+
assert_unique_hash(&proofs, &v_json);
46+
3547
Ok(())
3648
}
3749

50+
fn assert_unique_hash(proofs: &[PathBuf], v_json: &[Vec<SerFunction>]) {
51+
let mut map = IndexMap::<&str, Vec<(&Path, &str)>>::new();
52+
for (v, proof) in v_json.iter().zip(proofs) {
53+
for json in v {
54+
let def_id = &*json.def_id;
55+
let item = (&**proof, def_id);
56+
map.entry(&json.hash)
57+
.and_modify(|v_item| v_item.push(item))
58+
.or_insert_with(|| vec![item]);
59+
}
60+
}
61+
for (hash, v_item) in &map {
62+
let v_item_len = v_item.len();
63+
assert_eq!(
64+
v_item_len, 1,
65+
"{hash} should only correspond to single item, but \
66+
{v_item_len} items have the hash value:\n{v_item:?}",
67+
);
68+
}
69+
}
70+
3871
#[test]
3972
/// Make sure latest kani is installed through cargo.
4073
fn kani_installed() {

tests/utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use assert_cmd::Command;
22

3-
#[allow(unused_imports)] // seems a bug in clippy
3+
pub use distributed_verification::SerFunction;
4+
#[allow(unused_imports)] // seems a bug in clippy: RA only recognizes the usage in compare.rs
45
pub use expect_test::{expect, expect_file};
56
#[allow(unused_imports)] // seems a bug in clippy
7+
pub use eyre::Result;
8+
#[allow(unused_imports)] // seems a bug in clippy
69
pub use pretty_assertions::assert_eq;
710

811
pub fn cmd(args: &[&str]) -> String {

0 commit comments

Comments
 (0)