1#![feature(allocator_api)]
2#![feature(slice_ptr_get)]
3#![feature(liballoc_internals)]
4extern crate alloc;
5
6pub mod arena;
7pub mod collections;
8
9pub use arena::Arena;
10
11#[cfg(test)]
12mod tests {
13 use crate::collections::{ArenaHashMap, ArenaOrderedHashMap, ArenaVec, ArenaVecDeque};
14 use crate::{rumtk_arena_hashmap, rumtk_arena_orderedhashmap, rumtk_arena_vec, rumtk_arena_vecdeque, Arena};
15 use std::alloc::Allocator;
16 use std::alloc::Layout;
17 use std::ptr::NonNull;
18
19 #[test]
20 fn test_arena_simple_allocation() {
21 let arena = Arena::new();
22 let v: &str = unsafe { arena.write("hello world").unwrap().as_ref() };
23
24 assert_eq!(v, "hello world", "Failed to allocate and fill a small vector!");
25 }
26
27 #[test]
28 fn test_arena_simple_reallocation_address() {
29 let arena = Arena::new();
30 let old_layout = Layout::from_size_align(4, 1).unwrap();
31 let new_layout = Layout::from_size_align(8, 1).unwrap();
32 let v = unsafe { arena.allocate(old_layout).unwrap() };
33 let v2 = unsafe { arena.grow(v.cast(), old_layout, new_layout).unwrap() };
34 let v3 = unsafe { arena.grow(v2.cast(), new_layout, new_layout).unwrap() };
35
36 assert_eq!(v2, v3, "Failed to reallocate without invalidating pointer!");
37 }
38
39 #[test]
40 fn test_arena_simple_reallocation() {
41 let arena = Arena::new();
42 let old_layout = Layout::from_size_align(4, 4).unwrap();
43 let new_layout = Layout::from_size_align(8, 4).unwrap();
44 let v: NonNull<[u8]> = unsafe { arena.allocate(old_layout).unwrap() };
45 let v2: NonNull<[u8]> = unsafe { arena.grow(v.cast(), old_layout, new_layout).unwrap() };
46
47 assert_eq!(v.addr(), v2.addr(), "Failed to reallocate without invalidating pointer!");
48 }
49
50 #[test]
51 fn test_arena_simple_vec_allocation() {
52 let arena = Arena::new();
53 let mut v = Vec::<usize, &Arena>::with_capacity_in(10, &arena);
54
55 v.push(10);
56 v.push(10);
57
58 assert_eq!(v, [10, 10], "Failed to allocate and fill a small vector!");
59 }
60
61 #[test]
62 fn test_arena_simple_vec_reallocation() {
63 let arena = Arena::new();
64 let mut v = Vec::<usize, &Arena>::with_capacity_in(1, &arena);
65
66 v.push(10);
67 v.push(10);
68
69 assert_eq!(v, [10, 10], "Failed to reallocate and fill a small vector!");
70 }
71
72 #[test]
73 fn test_arena_allocate_more_than_allowed() {
74 let arena = Arena::with_capacity(5);
75 let v = arena.commit(10);
76
77 assert!(v.is_err(), "Arena did not emit error upon allocation of byte count higher than current capacity.");
78 }
79
80 #[test]
81 fn test_arena_create_vec_with_macro() {
82 let arena = Arena::with_capacity(5);
83 let v: ArenaVec<String> = rumtk_arena_vec!(&arena);
84
85 assert!(v.is_empty(), "Failed to create vector with arena allocation enabled.");
86 }
87
88 #[test]
89 fn test_arena_create_vec_with_macro_with_items() {
90 let arena = Arena::with_capacity(50);
91 let expected = &["Hello", "World", "!"];
92 let v: ArenaVec<&str> = rumtk_arena_vec!(expected.clone(), &arena);
93
94 assert_eq!(v.as_slice(), expected, "Failed to create vector with arena allocation enabled and item slice.");
95 }
96
97 #[test]
98 fn test_arena_create_vecdeque_with_macro() {
99 let arena = Arena::with_capacity(5);
100 let v: ArenaVecDeque<String> = rumtk_arena_vecdeque!(&arena);
101
102 assert!(v.is_empty(), "Failed to create vector with arena allocation enabled.");
103 }
104
105 #[test]
106 fn test_arena_create_vecdeque_with_macro_with_items() {
107 let arena = Arena::with_capacity(50);
108 let expected = ["Hello", "World", "!"];
109 let mut v: ArenaVecDeque<&str> = rumtk_arena_vecdeque!(expected.clone(), &arena);
110
111 assert_eq!(v.pop_front(), Some(expected[0]), "Failed to create queue with arena allocation enabled and item slice.");
112 }
113
114 #[test]
115 fn test_arena_create_hashmap_with_macro() {
116 let arena = Arena::with_capacity(5);
117 let v: ArenaHashMap<&str, &str> = rumtk_arena_hashmap!(&arena);
118
119 assert!(v.is_empty(), "Failed to create vector with arena allocation enabled.");
120 }
121
122 #[test]
123 fn test_arena_create_orderedhashmap_with_macro() {
124 let arena = Arena::with_capacity(5);
125 let v: ArenaOrderedHashMap<&str, &str> = rumtk_arena_orderedhashmap!(&arena);
126
127 assert!(v.is_empty(), "Failed to create vector with arena allocation enabled.");
128 }
129
130 #[test]
131 fn test_arena_create_hashmap_with_macro_with_items() {
132 let arena = Arena::with_capacity(120);
133 let expected = [(0, "Hello"), (1, "World"), (2, "!")];
134 let v: ArenaHashMap<usize, &str> = rumtk_arena_hashmap!(expected.clone(), &arena);
135
136 assert_eq!(v[&0], expected[0].1, "Failed to create hashmap with arena allocation enabled and item slice.");
137 }
138
139 #[test]
140 fn test_arena_create_orderedhashmap_with_macro_with_items() {
141 let arena = Arena::with_capacity(500);
142 let expected = [(5, "Hello"), (1, "World"), (3, "!")];
143 let v: ArenaOrderedHashMap<usize, &str> = rumtk_arena_orderedhashmap!(expected.clone(), &arena);
144
145 let mut order: Vec<(usize, &str)> = Vec::new();
146 for k in v.keys() {
147 order.push((k.clone(), v.get(&k).unwrap()));
148 }
149 println!("{:?}", order);
150
151 assert_eq!(order.as_slice(), expected, "Failed to create hashmap with arena allocation enabled and item slice.");
152 }
153}