Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions oscars/src/collectors/mark_sweep/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,40 @@ mod gc_edge_cases {
collector.collect();
}
}

/// Tests for the standalone WeakGc pointer behavior.
mod weak_gc_tests {
use crate::collectors::mark_sweep::MarkSweepGarbageCollector;
use crate::collectors::mark_sweep::cell::GcRefCell;
use crate::collectors::mark_sweep::pointers::{Gc, WeakGc};

#[test]
fn weak_gc_can_be_created_from_strong() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need both tests in this situation. Just creating the WeakGc without testing it with drops and collections doesn't really serve us any purpose

let collector = &mut MarkSweepGarbageCollector::default()
.with_arena_size(256)
.with_heap_threshold(512);

let strong = Gc::new_in(GcRefCell::new(42u64), collector);

let _weak = WeakGc::new_in(&strong, collector);
}

#[test]
fn weak_gc_ephemeron_swept_after_strong_drop() {
let collector = &mut MarkSweepGarbageCollector::default()
.with_arena_size(256)
.with_heap_threshold(512);

let strong = Gc::new_in(GcRefCell::new(42u64), collector);
let _weak = WeakGc::new_in(&strong, collector);

drop(strong);
collector.collect();

assert_eq!(
collector.allocator.borrow().arenas_len(),
0,
"WeakGc ephemeron leaked after strong reference dropped"
);
}
}