7 #[derive(Debug, Eq, PartialEq)]
15 pub fn test_unused() {
16 let arena: TypedArena<Point> = TypedArena::default();
17 assert!(arena.chunks.borrow().is_empty());
21 fn test_arena_alloc_nested() {
33 struct Wrap<'a>(TypedArena<EI<'a>>);
36 fn alloc_inner<F: Fn() -> Inner>(&self, f: F) -> &Inner {
37 let r: &EI<'_> = self.0.alloc(EI::I(f()));
38 if let &EI::I(ref i) = r {
44 fn alloc_outer<F: Fn() -> Outer<'a>>(&self, f: F) -> &Outer<'_> {
45 let r: &EI<'_> = self.0.alloc(EI::O(f()));
46 if let &EI::O(ref o) = r {
54 let arena = Wrap(TypedArena::default());
56 let result = arena.alloc_outer(|| Outer {
57 inner: arena.alloc_inner(|| Inner { value: 10 }),
60 assert_eq!(result.inner.value, 10);
65 let arena = TypedArena::default();
67 arena.alloc(Point { x: 1, y: 2, z: 3 });
72 pub fn bench_copy(b: &mut Bencher) {
73 let arena = TypedArena::default();
74 b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
78 pub fn bench_copy_nonarena(b: &mut Bencher) {
80 let _: Box<_> = Box::new(Point { x: 1, y: 2, z: 3 });
91 pub fn test_noncopy() {
92 let arena = TypedArena::default();
95 string: "hello world".to_string(),
96 array: vec![1, 2, 3, 4, 5],
102 pub fn test_typed_arena_zero_sized() {
103 let arena = TypedArena::default();
110 pub fn test_typed_arena_clear() {
111 let mut arena = TypedArena::default();
115 arena.alloc(Point { x: 1, y: 2, z: 3 });
121 pub fn bench_typed_arena_clear(b: &mut Bencher) {
122 let mut arena = TypedArena::default();
124 arena.alloc(Point { x: 1, y: 2, z: 3 });
131 struct DropCounter<'a> {
132 count: &'a Cell<u32>,
135 impl Drop for DropCounter<'_> {
137 self.count.set(self.count.get() + 1);
142 fn test_typed_arena_drop_count() {
143 let counter = Cell::new(0);
145 let arena: TypedArena<DropCounter<'_>> = TypedArena::default();
147 // Allocate something with drop glue to make sure it doesn't leak.
148 arena.alloc(DropCounter { count: &counter });
151 assert_eq!(counter.get(), 100);
155 fn test_typed_arena_drop_on_clear() {
156 let counter = Cell::new(0);
157 let mut arena: TypedArena<DropCounter<'_>> = TypedArena::default();
160 // Allocate something with drop glue to make sure it doesn't leak.
161 arena.alloc(DropCounter { count: &counter });
164 assert_eq!(counter.get(), i * 100 + 100);
169 static DROP_COUNTER: Cell<u32> = Cell::new(0)
172 struct SmallDroppable;
174 impl Drop for SmallDroppable {
176 DROP_COUNTER.with(|c| c.set(c.get() + 1));
181 fn test_typed_arena_drop_small_count() {
182 DROP_COUNTER.with(|c| c.set(0));
184 let arena: TypedArena<SmallDroppable> = TypedArena::default();
186 // Allocate something with drop glue to make sure it doesn't leak.
187 arena.alloc(SmallDroppable);
191 assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
195 pub fn bench_noncopy(b: &mut Bencher) {
196 let arena = TypedArena::default();
198 arena.alloc(Noncopy {
199 string: "hello world".to_string(),
200 array: vec![1, 2, 3, 4, 5],
206 pub fn bench_noncopy_nonarena(b: &mut Bencher) {
208 let _: Box<_> = Box::new(Noncopy {
209 string: "hello world".to_string(),
210 array: vec![1, 2, 3, 4, 5],