fix: #39 Adds a garbage collected deep copy test
All checks were successful
per-push tests / build (push) Successful in 41s
per-push tests / test-frontend (push) Successful in 40s
per-push tests / test-utility (push) Successful in 42s
per-push tests / test-backend (push) Successful in 40s
per-push tests / timed-decomposer-parse (push) Successful in 37s
All checks were successful
per-push tests / build (push) Successful in 41s
per-push tests / test-frontend (push) Successful in 40s
per-push tests / test-utility (push) Successful in 42s
per-push tests / test-backend (push) Successful in 40s
per-push tests / timed-decomposer-parse (push) Successful in 37s
Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
parent
7579d81d7b
commit
f41ec0a14c
1 changed files with 24 additions and 5 deletions
|
|
@ -127,7 +127,7 @@ impl<T: Clone> Gc<T> {
|
|||
pub fn deep_copy(&self) -> Gc<T> {
|
||||
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<usize> 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::<Gc<GcTester>>::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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue