chore: cryptarchia unit tests update (#657)

* test: getters for Branches
- derive(PartialEq) for Branch

* fix: format code

* test: Slot genesis and add

* fix: change expect message

* test: add ParentMissing error case
- derive(PartialEq) only for tests

* fix: false positive for ParentMissing

* fix: add PartialEq to derive for Branch

* fix: unwrap once only

* fix: simply create an Id

* fix: remove inappropriate meaning

* fix: simplify unwrap for branches.get()
This commit is contained in:
Roman Zajic 2024-06-27 19:22:54 +02:00 committed by GitHub
parent e48a26a8b5
commit 91854e2db6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 2 deletions

View File

@ -21,7 +21,7 @@ pub struct Branches<Id> {
tips: HashSet<Id>,
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct Branch<Id> {
id: Id,
parent: Id,
@ -131,6 +131,7 @@ where
}
#[derive(Debug, Clone, Error)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Error<Id> {
#[error("Parent block: {0:?} is not know to this node")]
ParentMissing(Id),
@ -220,7 +221,7 @@ where
#[cfg(test)]
pub mod tests {
use super::Cryptarchia;
use super::{Cryptarchia, Slot};
use crate::Config;
use std::hash::{DefaultHasher, Hash, Hasher};
@ -320,4 +321,35 @@ pub mod tests {
res[..8].copy_from_slice(&hash.to_be_bytes());
res
}
#[test]
fn test_getters() {
let engine = Cryptarchia::from_genesis([0; 32], config());
let id_0 = engine.genesis();
// Get branch directly from HashMap
let branch1 = engine.branches.get(&id_0).expect("branch1 should be there");
let branches = engine.branches();
// Get branch using getter
let branch2 = branches.get(&id_0).expect("branch2 should be there");
assert_eq!(branch1, branch2);
assert_eq!(branch1.id(), branch2.id());
assert_eq!(branch1.parent(), branch2.parent());
assert_eq!(branch1.slot(), branch2.slot());
assert_eq!(branch1.length(), branch2.length());
let slot = Slot::genesis();
assert_eq!(slot + 10u64, Slot::from(10));
let id_100 = [100; 32];
assert!(
branches.get(&id_100).is_none(),
"id_100 should not be related to this branch"
);
}
}