Skip to content

Commit 95ab938

Browse files
authored
Merge pull request #89 from Rigidity/test-cov-sep2
Test HashedPtr
2 parents 6217d0a + bb1b8f0 commit 95ab938

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

crates/chia-sdk-driver/src/hashed_ptr.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,103 @@ impl ToTreeHash for HashedPtr {
8383
self.tree_hash
8484
}
8585
}
86+
87+
#[cfg(test)]
88+
mod tests {
89+
use super::*;
90+
91+
#[test]
92+
fn test_nil_hashed_ptr() {
93+
let allocator = Allocator::new();
94+
let atom = allocator.atom(HashedPtr::NIL.ptr);
95+
assert!(atom.as_ref().is_empty());
96+
97+
assert_eq!(HashedPtr::NIL, HashedPtr::NIL);
98+
assert_eq!(HashedPtr::NIL.ptr(), NodePtr::NIL);
99+
assert_eq!(
100+
HashedPtr::NIL.tree_hash(),
101+
tree_hash(&allocator, NodePtr::NIL)
102+
);
103+
}
104+
105+
#[test]
106+
fn test_hashed_ptr() -> anyhow::Result<()> {
107+
let mut allocator = Allocator::new();
108+
109+
let ptr = ["Hello", " ", "world", "!"].to_clvm(&mut allocator)?;
110+
let hashed_ptr = HashedPtr::from_ptr(&allocator, ptr);
111+
assert_eq!(hashed_ptr.ptr(), ptr);
112+
assert_eq!(hashed_ptr.tree_hash(), tree_hash(&allocator, ptr));
113+
assert_eq!(hashed_ptr, hashed_ptr);
114+
assert_eq!(hashed_ptr, HashedPtr::new(ptr, hashed_ptr.tree_hash()));
115+
116+
Ok(())
117+
}
118+
119+
#[test]
120+
fn test_hashed_ptr_roundtrip() -> anyhow::Result<()> {
121+
let mut allocator = Allocator::new();
122+
123+
let ptr = "hello".to_clvm(&mut allocator)?;
124+
let hashed_ptr = HashedPtr::from_ptr(&allocator, ptr);
125+
126+
let new_ptr = hashed_ptr.to_clvm(&mut allocator)?;
127+
assert_eq!(ptr, new_ptr);
128+
129+
let new_hashed_ptr = HashedPtr::from_clvm(&allocator, new_ptr)?;
130+
assert_eq!(hashed_ptr, new_hashed_ptr);
131+
132+
Ok(())
133+
}
134+
135+
#[test]
136+
fn test_hashed_ptr_to_treehash() -> anyhow::Result<()> {
137+
let mut allocator = Allocator::new();
138+
139+
let ptr = "hello".to_clvm(&mut allocator)?;
140+
let hashed_ptr = HashedPtr::from_ptr(&allocator, ptr);
141+
let tree_hash = ToTreeHash::tree_hash(&hashed_ptr);
142+
assert_eq!(tree_hash, hashed_ptr.tree_hash());
143+
144+
Ok(())
145+
}
146+
147+
#[test]
148+
fn test_hashed_ptr_order() -> anyhow::Result<()> {
149+
let mut allocator = Allocator::new();
150+
151+
let mut ptrs = Vec::new();
152+
153+
for i in 0..5 {
154+
let ptr = i.to_clvm(&mut allocator)?;
155+
ptrs.push(HashedPtr::from_ptr(&allocator, ptr));
156+
}
157+
158+
ptrs.sort();
159+
160+
let hashes: Vec<TreeHash> = ptrs.into_iter().map(|ptr| ptr.tree_hash()).collect();
161+
162+
assert_eq!(
163+
hashes,
164+
[
165+
TreeHash::new(hex!(
166+
"4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a"
167+
)),
168+
TreeHash::new(hex!(
169+
"9dcf97a184f32623d11a73124ceb99a5709b083721e878a16d78f596718ba7b2"
170+
)),
171+
TreeHash::new(hex!(
172+
"a12871fee210fb8619291eaea194581cbd2531e4b23759d225f6806923f63222"
173+
)),
174+
TreeHash::new(hex!(
175+
"a8d5dd63fba471ebcb1f3e8f7c1e1879b7152a6e7298a91ce119a63400ade7c5"
176+
)),
177+
TreeHash::new(hex!(
178+
"c79b932e1e1da3c0e098e5ad2c422937eb904a76cf61d83975a74a68fbb04b99"
179+
))
180+
]
181+
);
182+
183+
Ok(())
184+
}
185+
}

0 commit comments

Comments
 (0)