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 { inner: arena.alloc_inner(|| Inner { value: 10 }) });
58 assert_eq!(result.inner.value, 10);
63 let arena = TypedArena::default();
65 arena.alloc(Point { x: 1, y: 2, z: 3 });
70 pub fn bench_copy(b: &mut Bencher) {
71 let arena = TypedArena::default();
72 b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
76 pub fn bench_copy_nonarena(b: &mut Bencher) {
78 let _: Box<_> = Box::new(Point { x: 1, y: 2, z: 3 });
89 pub fn test_noncopy() {
90 let arena = TypedArena::default();
92 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });
97 pub fn test_typed_arena_zero_sized() {
98 let arena = TypedArena::default();
105 pub fn test_typed_arena_clear() {
106 let mut arena = TypedArena::default();
110 arena.alloc(Point { x: 1, y: 2, z: 3 });
116 pub fn bench_typed_arena_clear(b: &mut Bencher) {
117 let mut arena = TypedArena::default();
119 arena.alloc(Point { x: 1, y: 2, z: 3 });
125 pub fn bench_typed_arena_clear_100(b: &mut Bencher) {
126 let mut arena = TypedArena::default();
129 arena.alloc(Point { x: 1, y: 2, z: 3 });
137 struct DropCounter<'a> {
138 count: &'a Cell<u32>,
141 impl Drop for DropCounter<'_> {
143 self.count.set(self.count.get() + 1);
148 fn test_typed_arena_drop_count() {
149 let counter = Cell::new(0);
151 let arena: TypedArena<DropCounter<'_>> = TypedArena::default();
153 // Allocate something with drop glue to make sure it doesn't leak.
154 arena.alloc(DropCounter { count: &counter });
157 assert_eq!(counter.get(), 100);
161 fn test_typed_arena_drop_on_clear() {
162 let counter = Cell::new(0);
163 let mut arena: TypedArena<DropCounter<'_>> = TypedArena::default();
166 // Allocate something with drop glue to make sure it doesn't leak.
167 arena.alloc(DropCounter { count: &counter });
170 assert_eq!(counter.get(), i * 100 + 100);
175 static DROP_COUNTER: Cell<u32> = Cell::new(0)
178 struct SmallDroppable;
180 impl Drop for SmallDroppable {
182 DROP_COUNTER.with(|c| c.set(c.get() + 1));
187 fn test_typed_arena_drop_small_count() {
188 DROP_COUNTER.with(|c| c.set(0));
190 let arena: TypedArena<SmallDroppable> = TypedArena::default();
192 // Allocate something with drop glue to make sure it doesn't leak.
193 arena.alloc(SmallDroppable);
197 assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
201 pub fn bench_noncopy(b: &mut Bencher) {
202 let arena = TypedArena::default();
204 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] })
209 pub fn bench_noncopy_nonarena(b: &mut Bencher) {
212 Box::new(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });