diff --git a/hyphae/src/heap.rs b/hyphae/src/heap.rs index 6e54603..1e60e3c 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,19 +309,29 @@ 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; let a = Into::>::into(GcTester(&mut flag)); + assert!(!*(*a).0); drop(a); - assert!(flag) + assert!(flag); } #[test] @@ -329,9 +339,21 @@ mod tests { let mut flag = false; let a = Into::>::into(GcTester(&mut flag)).clone(); + assert!(!*(*a).0); drop(a); - assert!(flag) + 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(); + assert!(!*(*reference_holder).0); + let copied_data = reference_holder.deep_copy(); + assert!(!*(*copied_data).0); + assert_eq!(*reference_holder, *copied_data); + drop(reference_holder); + assert!(!*(*copied_data).0); + } }