use super::util::EventAggregator;
use mun_memory::{
gc::{Event, GcRootPtr, GcRuntime, MarkSweep},
HasStaticType,
};
use std::sync::Arc;
#[test]
fn alloc() {
let runtime = MarkSweep::<EventAggregator<Event>>::default();
let handle = runtime.alloc(i64::type_info());
assert_eq!(&runtime.ptr_type(handle), i64::type_info());
let mut events = runtime.observer().take_all().into_iter();
assert_eq!(events.next(), Some(Event::Allocation(handle)));
assert_eq!(events.next(), None);
}
#[test]
fn collect_simple() {
let runtime = MarkSweep::<EventAggregator<Event>>::default();
let handle = runtime.alloc(i64::type_info());
runtime.collect();
let mut events = runtime.observer().take_all().into_iter();
assert_eq!(events.next(), Some(Event::Allocation(handle)));
assert_eq!(events.next(), Some(Event::Start));
assert_eq!(events.next(), Some(Event::Deallocation(handle)));
assert_eq!(events.next(), Some(Event::End));
assert_eq!(events.next(), None);
}
#[test]
fn collect_rooted() {
let runtime = Arc::new(MarkSweep::<EventAggregator<Event>>::default());
let handle = runtime.alloc(i64::type_info());
let rooted = GcRootPtr::new(&runtime, runtime.alloc(i64::type_info()));
runtime.collect();
runtime.collect();
let rooted_handle = rooted.unroot();
runtime.collect();
let mut events = runtime.observer().take_all().into_iter();
assert_eq!(events.next(), Some(Event::Allocation(handle)));
assert_eq!(events.next(), Some(Event::Allocation(rooted_handle)));
assert_eq!(events.next(), Some(Event::Start));
assert_eq!(events.next(), Some(Event::Deallocation(handle)));
assert_eq!(events.next(), Some(Event::End));
assert_eq!(events.next(), Some(Event::Start));
assert_eq!(events.next(), Some(Event::End));
assert_eq!(events.next(), Some(Event::Start));
assert_eq!(events.next(), Some(Event::Deallocation(rooted_handle)));
assert_eq!(events.next(), Some(Event::End));
assert_eq!(events.next(), None);
}