Skip to content

Commit

Permalink
test(hstr): Add tests (#10043)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Feb 17, 2025
1 parent 8410b59 commit 32b58f0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/hstr/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) struct Metadata {
}

#[derive(Clone)]
pub(crate) struct Item(ThinArc<HeaderWithLength<Metadata>, u8>);
pub(crate) struct Item(pub ThinArc<HeaderWithLength<Metadata>, u8>);

impl Deref for Item {
type Target = <ThinArc<HeaderWithLength<Metadata>, u8> as Deref>::Target;
Expand Down
14 changes: 14 additions & 0 deletions crates/hstr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ impl Atom {
}
}

#[cfg(test)]
impl Atom {
pub(crate) fn ref_count(&self) -> usize {
match self.tag() {
DYNAMIC_TAG => {
let ptr = unsafe { crate::dynamic::deref_from(self.unsafe_data) };

triomphe::ThinArc::strong_count(&ptr.0)
}
_ => 1,
}
}
}

impl PartialEq for Atom {
#[inline(never)]
fn eq(&self, other: &Self) -> bool {
Expand Down
27 changes: 27 additions & 0 deletions crates/hstr/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,30 @@ fn store_multiple() {
assert_eq!(a1.get_hash(), a2.get_hash(), "Same string should be equal");
assert_eq!(a1, a2, "Same string should be equal");
}

#[test]
fn store_ref_count() {
let (store, atoms) = store_with_atoms(vec!["Hello, world!!!!"]);

assert_eq!(atoms[0].ref_count(), 2);
drop(store);
assert_eq!(atoms[0].ref_count(), 1);
}

#[test]
fn store_ref_count_dynamic() {
let (store, atoms) = store_with_atoms(vec!["Hello, world!!!!"]);

let a1 = atoms[0].clone();
let a2 = atoms[0].clone();

assert_eq!(atoms[0].ref_count(), 4);
drop(store);
assert_eq!(atoms[0].ref_count(), 3);

drop(a1);
assert_eq!(atoms[0].ref_count(), 2);

drop(a2);
assert_eq!(atoms[0].ref_count(), 1);
}

0 comments on commit 32b58f0

Please sign in to comment.