diff --git a/crates/swc_allocator/tests/apis.rs b/crates/swc_allocator/tests/alloction.rs similarity index 56% rename from crates/swc_allocator/tests/apis.rs rename to crates/swc_allocator/tests/alloction.rs index bf35cea53d35..99934ea4a6c7 100644 --- a/crates/swc_allocator/tests/apis.rs +++ b/crates/swc_allocator/tests/alloction.rs @@ -1,5 +1,5 @@ use criterion::black_box; -use swc_allocator::Allocator; +use swc_allocator::{boxed::Box, Allocator, FastAlloc}; #[test] fn direct_alloc_std() { @@ -33,3 +33,31 @@ fn direct_alloc_in_scope() { vec.push(item); } } + +#[test] +fn escape() { + let allocator = Allocator::default(); + + let obj = { + let _guard = unsafe { allocator.guard() }; + Box::new(1234) + }; + + assert_eq!(*obj, 1234); + // It should not segfault, because the allocator is still alive. + drop(obj); +} + +#[test] +fn global_allocator_escape() { + let allocator = Allocator::default(); + let obj = { + let _guard = unsafe { allocator.guard() }; + Box::new_in(1234, FastAlloc::global()) + }; + + assert_eq!(*obj, 1234); + drop(allocator); + // Object created with global allocator should outlive the allocator. + drop(obj); +} diff --git a/crates/swc_allocator/tests/escape.rs b/crates/swc_allocator/tests/escape.rs deleted file mode 100644 index f12ab5520b0e..000000000000 --- a/crates/swc_allocator/tests/escape.rs +++ /dev/null @@ -1,29 +0,0 @@ -use swc_allocator::{boxed::Box, Allocator, FastAlloc}; - -#[test] -fn escape() { - let allocator = Allocator::default(); - - let obj = { - let _guard = unsafe { allocator.guard() }; - Box::new(1234) - }; - - assert_eq!(*obj, 1234); - // It should not segfault, because the allocator is still alive. - drop(obj); -} - -#[test] -fn global_allocator() { - let allocator = Allocator::default(); - let obj = { - let _guard = unsafe { allocator.guard() }; - Box::new_in(1234, FastAlloc::global()) - }; - - assert_eq!(*obj, 1234); - drop(allocator); - // Object created with global allocator should outlive the allocator. - drop(obj); -}