1 use crate::linked_list;
2 use crate::FrameAllocator;
3 use crate::Heap;
4 use crate::LockedHeapWithRescue;
5 use core::alloc::GlobalAlloc;
6 use core::alloc::Layout;
7 use core::mem::size_of;
8
9 #[test]
test_linked_list()10 fn test_linked_list() {
11 let mut value1: usize = 0;
12 let mut value2: usize = 0;
13 let mut value3: usize = 0;
14 let mut list = linked_list::LinkedList::new();
15 unsafe {
16 list.push(&mut value1 as *mut usize);
17 list.push(&mut value2 as *mut usize);
18 list.push(&mut value3 as *mut usize);
19 }
20
21 // Test links
22 assert_eq!(value3, &value2 as *const usize as usize);
23 assert_eq!(value2, &value1 as *const usize as usize);
24 assert_eq!(value1, 0);
25
26 // Test iter
27 let mut iter = list.iter();
28 assert_eq!(iter.next(), Some(&mut value3 as *mut usize));
29 assert_eq!(iter.next(), Some(&mut value2 as *mut usize));
30 assert_eq!(iter.next(), Some(&mut value1 as *mut usize));
31 assert_eq!(iter.next(), None);
32
33 // Test iter_mut
34
35 let mut iter_mut = list.iter_mut();
36 assert_eq!(iter_mut.next().unwrap().pop(), &mut value3 as *mut usize);
37
38 // Test pop
39 assert_eq!(list.pop(), Some(&mut value2 as *mut usize));
40 assert_eq!(list.pop(), Some(&mut value1 as *mut usize));
41 assert_eq!(list.pop(), None);
42 }
43
44 #[test]
test_empty_heap()45 fn test_empty_heap() {
46 let mut heap = Heap::<32>::new();
47 assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
48 }
49
50 #[test]
test_heap_add()51 fn test_heap_add() {
52 let mut heap = Heap::<32>::new();
53 assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
54
55 let space: [usize; 100] = [0; 100];
56 unsafe {
57 heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize);
58 }
59 let addr = heap.alloc(Layout::from_size_align(1, 1).unwrap());
60 assert!(addr.is_ok());
61 }
62
63 #[test]
test_heap_oom()64 fn test_heap_oom() {
65 let mut heap = Heap::<32>::new();
66 let space: [usize; 100] = [0; 100];
67 unsafe {
68 heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize);
69 }
70
71 assert!(heap
72 .alloc(Layout::from_size_align(100 * size_of::<usize>(), 1).unwrap())
73 .is_err());
74 assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_ok());
75 }
76
77 #[test]
test_heap_oom_rescue()78 fn test_heap_oom_rescue() {
79 static mut SPACE: [usize; 100] = [0; 100];
80 let heap = LockedHeapWithRescue::new(|heap: &mut Heap<32>, _layout: &Layout| unsafe {
81 heap.add_to_heap(SPACE.as_ptr() as usize, SPACE.as_ptr().add(100) as usize);
82 });
83
84 unsafe {
85 assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()) as usize != 0);
86 }
87 }
88
89 #[test]
test_heap_alloc_and_free()90 fn test_heap_alloc_and_free() {
91 let mut heap = Heap::<32>::new();
92 assert!(heap.alloc(Layout::from_size_align(1, 1).unwrap()).is_err());
93
94 let space: [usize; 100] = [0; 100];
95 unsafe {
96 heap.add_to_heap(space.as_ptr() as usize, space.as_ptr().add(100) as usize);
97 }
98 for _ in 0..100 {
99 let addr = heap.alloc(Layout::from_size_align(1, 1).unwrap()).unwrap();
100 heap.dealloc(addr, Layout::from_size_align(1, 1).unwrap());
101 }
102 }
103
104 #[test]
test_empty_frame_allocator()105 fn test_empty_frame_allocator() {
106 let mut frame = FrameAllocator::<32>::new();
107 assert!(frame.alloc(1).is_none());
108 }
109
110 #[test]
test_frame_allocator_add()111 fn test_frame_allocator_add() {
112 let mut frame = FrameAllocator::<32>::new();
113 assert!(frame.alloc(1).is_none());
114
115 frame.insert(0..3);
116 let num = frame.alloc(1);
117 assert_eq!(num, Some(2));
118 let num = frame.alloc(2);
119 assert_eq!(num, Some(0));
120 assert!(frame.alloc(1).is_none());
121 assert!(frame.alloc(2).is_none());
122 }
123
124 #[test]
test_frame_allocator_allocate_large()125 fn test_frame_allocator_allocate_large() {
126 let mut frame = FrameAllocator::<32>::new();
127 assert_eq!(frame.alloc(10_000_000_000), None);
128 }
129
130 #[test]
test_frame_allocator_add_large_size_split()131 fn test_frame_allocator_add_large_size_split() {
132 let mut frame = FrameAllocator::<32>::new();
133
134 frame.insert(0..10_000_000_000);
135
136 assert_eq!(frame.alloc(0x8000_0001), None);
137 assert_eq!(frame.alloc(0x8000_0000), Some(0x8000_0000));
138 assert_eq!(frame.alloc(0x8000_0000), Some(0x1_0000_0000));
139 }
140
141 #[test]
test_frame_allocator_add_large_size()142 fn test_frame_allocator_add_large_size() {
143 let mut frame = FrameAllocator::<33>::new();
144
145 frame.insert(0..10_000_000_000);
146 assert_eq!(frame.alloc(0x8000_0001), Some(0x1_0000_0000));
147 }
148
149 #[test]
test_frame_allocator_alloc_and_free()150 fn test_frame_allocator_alloc_and_free() {
151 let mut frame = FrameAllocator::<32>::new();
152 assert!(frame.alloc(1).is_none());
153
154 frame.add_frame(0, 1024);
155 for _ in 0..100 {
156 let addr = frame.alloc(512).unwrap();
157 frame.dealloc(addr, 512);
158 }
159 }
160
161 #[test]
test_frame_allocator_alloc_and_free_complex()162 fn test_frame_allocator_alloc_and_free_complex() {
163 let mut frame = FrameAllocator::<32>::new();
164 frame.add_frame(100, 1024);
165 for _ in 0..10 {
166 let addr = frame.alloc(1).unwrap();
167 frame.dealloc(addr, 1);
168 }
169 let addr1 = frame.alloc(1).unwrap();
170 let addr2 = frame.alloc(1).unwrap();
171 assert_ne!(addr1, addr2);
172 }
173
174 #[test]
test_frame_allocator_aligned()175 fn test_frame_allocator_aligned() {
176 let mut frame = FrameAllocator::<32>::new();
177 frame.add_frame(1, 64);
178 assert_eq!(
179 frame.alloc_aligned(Layout::from_size_align(2, 4).unwrap()),
180 Some(4)
181 );
182 assert_eq!(
183 frame.alloc_aligned(Layout::from_size_align(2, 2).unwrap()),
184 Some(2)
185 );
186 assert_eq!(
187 frame.alloc_aligned(Layout::from_size_align(2, 1).unwrap()),
188 Some(8)
189 );
190 assert_eq!(
191 frame.alloc_aligned(Layout::from_size_align(1, 16).unwrap()),
192 Some(16)
193 );
194 }
195