Skip to content

Commit 32b58f0

Browse files
authoredFeb 17, 2025··
test(hstr): Add tests (#10043)
1 parent 8410b59 commit 32b58f0

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
 

‎crates/hstr/src/dynamic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) struct Metadata {
2020
}
2121

2222
#[derive(Clone)]
23-
pub(crate) struct Item(ThinArc<HeaderWithLength<Metadata>, u8>);
23+
pub(crate) struct Item(pub ThinArc<HeaderWithLength<Metadata>, u8>);
2424

2525
impl Deref for Item {
2626
type Target = <ThinArc<HeaderWithLength<Metadata>, u8> as Deref>::Target;

‎crates/hstr/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ impl Atom {
273273
}
274274
}
275275

276+
#[cfg(test)]
277+
impl Atom {
278+
pub(crate) fn ref_count(&self) -> usize {
279+
match self.tag() {
280+
DYNAMIC_TAG => {
281+
let ptr = unsafe { crate::dynamic::deref_from(self.unsafe_data) };
282+
283+
triomphe::ThinArc::strong_count(&ptr.0)
284+
}
285+
_ => 1,
286+
}
287+
}
288+
}
289+
276290
impl PartialEq for Atom {
277291
#[inline(never)]
278292
fn eq(&self, other: &Self) -> bool {

‎crates/hstr/src/tests.rs

+27
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,30 @@ fn store_multiple() {
5555
assert_eq!(a1.get_hash(), a2.get_hash(), "Same string should be equal");
5656
assert_eq!(a1, a2, "Same string should be equal");
5757
}
58+
59+
#[test]
60+
fn store_ref_count() {
61+
let (store, atoms) = store_with_atoms(vec!["Hello, world!!!!"]);
62+
63+
assert_eq!(atoms[0].ref_count(), 2);
64+
drop(store);
65+
assert_eq!(atoms[0].ref_count(), 1);
66+
}
67+
68+
#[test]
69+
fn store_ref_count_dynamic() {
70+
let (store, atoms) = store_with_atoms(vec!["Hello, world!!!!"]);
71+
72+
let a1 = atoms[0].clone();
73+
let a2 = atoms[0].clone();
74+
75+
assert_eq!(atoms[0].ref_count(), 4);
76+
drop(store);
77+
assert_eq!(atoms[0].ref_count(), 3);
78+
79+
drop(a1);
80+
assert_eq!(atoms[0].ref_count(), 2);
81+
82+
drop(a2);
83+
assert_eq!(atoms[0].ref_count(), 1);
84+
}

0 commit comments

Comments
 (0)
Please sign in to comment.