This commit is contained in:
Roman Zajic 2024-06-24 10:04:02 +02:00 committed by GitHub
commit 1f5587c8e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 57 additions and 1 deletions

View File

@ -22,6 +22,7 @@ pub struct Branches<Id> {
}
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Branch<Id> {
id: Id,
parent: Id,
@ -131,6 +132,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 +222,7 @@ where
#[cfg(test)]
pub mod tests {
use super::Cryptarchia;
use super::{Cryptarchia, Error, Slot};
use crate::Config;
use std::hash::{DefaultHasher, Hash, Hasher};
@ -320,4 +322,58 @@ 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 parent = engine.genesis();
// Get branch directly from HashMap
let branch1 = engine
.branches
.get(&parent)
.ok_or("At least one branch should be there");
let branches = engine.branches();
// Get branch using getter
let branch2 = branches
.get(&parent)
.ok_or("At least one branch should be there");
assert_eq!(branch1, branch2);
assert_eq!(
branch1.expect("id is not set").id(),
branch2.expect("id is not set").id()
);
assert_eq!(
branch1.expect("parent is not set").parent(),
branch2.expect("parent is not set").parent()
);
assert_eq!(
branch1.expect("slot is not set").slot(),
branch2.expect("slot is not set").slot()
);
assert_eq!(
branch1.expect("length is not set").length(),
branch2.expect("length is not set").length()
);
let slot = Slot::genesis();
assert_eq!(slot + 10u64, Slot::from(10));
let strange_engine = Cryptarchia::from_genesis([100; 32], config());
let not_a_parent = strange_engine.genesis();
_ = match branches
.get(&not_a_parent)
.ok_or(Error::ParentMissing(not_a_parent))
{
Ok(_) => panic!("Parent should not be related to this branch"),
Err(e) => {
assert_ne!(e, Error::ParentMissing(parent));
}
};
}
}