Skip to content

Commit efc3963

Browse files
authoredJul 17, 2024··
test(allocator): Merge test (#9267)
1 parent a92ce51 commit efc3963

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed
 

‎crates/swc_allocator/tests/apis.rs ‎crates/swc_allocator/tests/alloction.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use criterion::black_box;
2-
use swc_allocator::Allocator;
2+
use swc_allocator::{boxed::Box, Allocator, FastAlloc};
33

44
#[test]
55
fn direct_alloc_std() {
@@ -33,3 +33,31 @@ fn direct_alloc_in_scope() {
3333
vec.push(item);
3434
}
3535
}
36+
37+
#[test]
38+
fn escape() {
39+
let allocator = Allocator::default();
40+
41+
let obj = {
42+
let _guard = unsafe { allocator.guard() };
43+
Box::new(1234)
44+
};
45+
46+
assert_eq!(*obj, 1234);
47+
// It should not segfault, because the allocator is still alive.
48+
drop(obj);
49+
}
50+
51+
#[test]
52+
fn global_allocator_escape() {
53+
let allocator = Allocator::default();
54+
let obj = {
55+
let _guard = unsafe { allocator.guard() };
56+
Box::new_in(1234, FastAlloc::global())
57+
};
58+
59+
assert_eq!(*obj, 1234);
60+
drop(allocator);
61+
// Object created with global allocator should outlive the allocator.
62+
drop(obj);
63+
}

‎crates/swc_allocator/tests/escape.rs

-29
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.