From f41ec0a14cb5c97aa3bf84e3f6cecad6c911c05f Mon Sep 17 00:00:00 2001 From: Ava Affine Date: Mon, 28 Jul 2025 23:36:26 +0000 Subject: [PATCH] fix: #39 Adds a garbage collected deep copy test Signed-off-by: Ava Affine --- hyphae/src/heap.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/hyphae/src/heap.rs b/hyphae/src/heap.rs index 6e54603..0196299 100644 --- a/hyphae/src/heap.rs +++ b/hyphae/src/heap.rs @@ -127,7 +127,7 @@ impl Gc { pub fn deep_copy(&self) -> Gc { Gc(unsafe { NonNull::new(Box::into_raw(Box::new(Rc::from( - (*(self.0.as_ptr())).clone())))) + self.0.as_ref().clone())))) .expect("GC obj deep copy nonnull ptr check") }) } @@ -292,9 +292,9 @@ impl Index for Cons { mod tests { use super::*; - struct GcTester<'a>(&'a mut bool); + #[derive(Debug)] + struct GcTester<'a>(pub &'a mut bool); - // clone implementation leaks memory... but its for test impl Clone for GcTester<'_> { fn clone(&self) -> Self { unsafe { @@ -309,13 +309,22 @@ mod tests { } } - // in non test code Id have to get the box back impl Drop for GcTester<'_> { fn drop(&mut self) { *self.0 = true; } } + impl PartialEq for GcTester<'_> { + fn eq(&self, other: &Self) -> bool { + *(self.0) == *(other.0) + } + + fn ne(&self, other: &Self) -> bool { + *(self.0) != *(other.0) + } + } + #[test] fn test_gc_basic_behavior() { let mut flag = false; @@ -333,5 +342,15 @@ mod tests { assert!(flag) } - // TODO: test deep copy + #[test] + fn test_gc_deep_copy() { + let mut flag = false; + let reference_holder = + Into::>::into(GcTester(&mut flag)).clone(); + let copied_data = reference_holder.deep_copy(); + + assert_eq!(*reference_holder, *copied_data); + drop(reference_holder); + assert!(!*(*copied_data).0); + } }