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 });
126 struct DropCounter<'a> {
127 count: &'a Cell<u32>,
130 impl Drop for DropCounter<'_> {
132 self.count.set(self.count.get() + 1);
137 fn test_typed_arena_drop_count() {
138 let counter = Cell::new(0);
140 let arena: TypedArena<DropCounter<'_>> = TypedArena::default();
142 // Allocate something with drop glue to make sure it doesn't leak.
143 arena.alloc(DropCounter { count: &counter });
146 assert_eq!(counter.get(), 100);
150 fn test_typed_arena_drop_on_clear() {
151 let counter = Cell::new(0);
152 let mut arena: TypedArena<DropCounter<'_>> = TypedArena::default();
155 // Allocate something with drop glue to make sure it doesn't leak.
156 arena.alloc(DropCounter { count: &counter });
159 assert_eq!(counter.get(), i * 100 + 100);
164 static DROP_COUNTER: Cell<u32> = Cell::new(0)
167 struct SmallDroppable;
169 impl Drop for SmallDroppable {
171 DROP_COUNTER.with(|c| c.set(c.get() + 1));
176 fn test_typed_arena_drop_small_count() {
177 DROP_COUNTER.with(|c| c.set(0));
179 let arena: TypedArena<SmallDroppable> = TypedArena::default();
181 // Allocate something with drop glue to make sure it doesn't leak.
182 arena.alloc(SmallDroppable);
186 assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
190 pub fn bench_noncopy(b: &mut Bencher) {
191 let arena = TypedArena::default();
193 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] })
198 pub fn bench_noncopy_nonarena(b: &mut Bencher) {
201 Box::new(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });