Skip to content

Commit 75883db

Browse files
committed
Adjust tests for new Layout-based API
1 parent 33ce057 commit 75883db

File tree

1 file changed

+91
-64
lines changed

1 file changed

+91
-64
lines changed

src/test.rs

+91-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::prelude::v1::*;
22
use std::mem::{size_of, align_of};
3+
use alloc::allocator::Layout;
34
use super::*;
45

56
fn new_heap() -> Heap {
@@ -26,23 +27,25 @@ fn new_max_heap() -> Heap {
2627
#[test]
2728
fn empty() {
2829
let mut heap = Heap::empty();
29-
assert!(heap.allocate_first_fit(1, 1).is_none());
30+
let layout = Layout::from_size_align(1, 1).unwrap();
31+
assert!(heap.allocate_first_fit(layout.clone()).is_err());
3032
}
3133

3234
#[test]
3335
fn oom() {
3436
let mut heap = new_heap();
35-
let size = heap.size() + 1;
36-
let addr = heap.allocate_first_fit(size, align_of::<usize>());
37-
assert!(addr.is_none());
37+
let layout = Layout::from_size_align(heap.size() + 1, align_of::<usize>());
38+
let addr = heap.allocate_first_fit(layout.unwrap());
39+
assert!(addr.is_err());
3840
}
3941

4042
#[test]
4143
fn allocate_double_usize() {
4244
let mut heap = new_heap();
4345
let size = size_of::<usize>() * 2;
44-
let addr = heap.allocate_first_fit(size, align_of::<usize>());
45-
assert!(addr.is_some());
46+
let layout = Layout::from_size_align(size, align_of::<usize>());
47+
let addr = heap.allocate_first_fit(layout.unwrap());
48+
assert!(addr.is_ok());
4649
let addr = addr.unwrap() as usize;
4750
assert!(addr == heap.bottom);
4851
let (hole_addr, hole_size) = heap.holes.first_hole().expect("ERROR: no hole left");
@@ -58,11 +61,12 @@ fn allocate_double_usize() {
5861
fn allocate_and_free_double_usize() {
5962
let mut heap = new_heap();
6063

61-
let x = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap();
64+
let layout = Layout::from_size_align(size_of::<usize>() * 2, align_of::<usize>()).unwrap();
65+
let x = heap.allocate_first_fit(layout.clone()).unwrap();
6266
unsafe {
6367
*(x as *mut (usize, usize)) = (0xdeafdeadbeafbabe, 0xdeafdeadbeafbabe);
6468

65-
heap.deallocate(x, size_of::<usize>() * 2, align_of::<usize>());
69+
heap.deallocate(x, layout.clone());
6670
assert_eq!((*(heap.bottom as *const Hole)).size, heap.size);
6771
assert!((*(heap.bottom as *const Hole)).next.is_none());
6872
}
@@ -71,18 +75,18 @@ fn allocate_and_free_double_usize() {
7175
#[test]
7276
fn deallocate_right_before() {
7377
let mut heap = new_heap();
74-
let size = size_of::<usize>() * 5;
78+
let layout = Layout::from_size_align(size_of::<usize>() * 5, 1).unwrap();
7579

76-
let x = heap.allocate_first_fit(size, 1).unwrap();
77-
let y = heap.allocate_first_fit(size, 1).unwrap();
78-
let z = heap.allocate_first_fit(size, 1).unwrap();
80+
let x = heap.allocate_first_fit(layout.clone()).unwrap();
81+
let y = heap.allocate_first_fit(layout.clone()).unwrap();
82+
let z = heap.allocate_first_fit(layout.clone()).unwrap();
7983

8084
unsafe {
81-
heap.deallocate(y, size, 1);
82-
assert_eq!((*(y as *const Hole)).size, size);
83-
heap.deallocate(x, size, 1);
84-
assert_eq!((*(x as *const Hole)).size, size * 2);
85-
heap.deallocate(z, size, 1);
85+
heap.deallocate(y, layout.clone());
86+
assert_eq!((*(y as *const Hole)).size, layout.size());
87+
heap.deallocate(x, layout.clone());
88+
assert_eq!((*(x as *const Hole)).size, layout.size() * 2);
89+
heap.deallocate(z, layout.clone());
8690
assert_eq!((*(x as *const Hole)).size, heap.size);
8791
}
8892
}
@@ -91,17 +95,18 @@ fn deallocate_right_before() {
9195
fn deallocate_right_behind() {
9296
let mut heap = new_heap();
9397
let size = size_of::<usize>() * 5;
98+
let layout = Layout::from_size_align(size, 1).unwrap();
9499

95-
let x = heap.allocate_first_fit(size, 1).unwrap();
96-
let y = heap.allocate_first_fit(size, 1).unwrap();
97-
let z = heap.allocate_first_fit(size, 1).unwrap();
100+
let x = heap.allocate_first_fit(layout.clone()).unwrap();
101+
let y = heap.allocate_first_fit(layout.clone()).unwrap();
102+
let z = heap.allocate_first_fit(layout.clone()).unwrap();
98103

99104
unsafe {
100-
heap.deallocate(x, size, 1);
105+
heap.deallocate(x, layout.clone());
101106
assert_eq!((*(x as *const Hole)).size, size);
102-
heap.deallocate(y, size, 1);
107+
heap.deallocate(y, layout.clone());
103108
assert_eq!((*(x as *const Hole)).size, size * 2);
104-
heap.deallocate(z, size, 1);
109+
heap.deallocate(z, layout.clone());
105110
assert_eq!((*(x as *const Hole)).size, heap.size);
106111
}
107112
}
@@ -110,21 +115,22 @@ fn deallocate_right_behind() {
110115
fn deallocate_middle() {
111116
let mut heap = new_heap();
112117
let size = size_of::<usize>() * 5;
118+
let layout = Layout::from_size_align(size, 1).unwrap();
113119

114-
let x = heap.allocate_first_fit(size, 1).unwrap();
115-
let y = heap.allocate_first_fit(size, 1).unwrap();
116-
let z = heap.allocate_first_fit(size, 1).unwrap();
117-
let a = heap.allocate_first_fit(size, 1).unwrap();
120+
let x = heap.allocate_first_fit(layout.clone()).unwrap();
121+
let y = heap.allocate_first_fit(layout.clone()).unwrap();
122+
let z = heap.allocate_first_fit(layout.clone()).unwrap();
123+
let a = heap.allocate_first_fit(layout.clone()).unwrap();
118124

119125
unsafe {
120-
heap.deallocate(x, size, 1);
126+
heap.deallocate(x, layout.clone());
121127
assert_eq!((*(x as *const Hole)).size, size);
122-
heap.deallocate(z, size, 1);
128+
heap.deallocate(z, layout.clone());
123129
assert_eq!((*(x as *const Hole)).size, size);
124130
assert_eq!((*(z as *const Hole)).size, size);
125-
heap.deallocate(y, size, 1);
131+
heap.deallocate(y, layout.clone());
126132
assert_eq!((*(x as *const Hole)).size, size * 3);
127-
heap.deallocate(a, size, 1);
133+
heap.deallocate(a, layout.clone());
128134
assert_eq!((*(x as *const Hole)).size, heap.size);
129135
}
130136
}
@@ -133,14 +139,16 @@ fn deallocate_middle() {
133139
fn reallocate_double_usize() {
134140
let mut heap = new_heap();
135141

136-
let x = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap();
142+
let layout = Layout::from_size_align(size_of::<usize>() * 2, align_of::<usize>()).unwrap();
143+
144+
let x = heap.allocate_first_fit(layout.clone()).unwrap();
137145
unsafe {
138-
heap.deallocate(x, size_of::<usize>() * 2, align_of::<usize>());
146+
heap.deallocate(x, layout.clone());
139147
}
140148

141-
let y = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap();
149+
let y = heap.allocate_first_fit(layout.clone()).unwrap();
142150
unsafe {
143-
heap.deallocate(y, size_of::<usize>() * 2, align_of::<usize>());
151+
heap.deallocate(y, layout.clone());
144152
}
145153

146154
assert_eq!(x, y);
@@ -152,53 +160,63 @@ fn allocate_multiple_sizes() {
152160
let base_size = size_of::<usize>();
153161
let base_align = align_of::<usize>();
154162

155-
let x = heap.allocate_first_fit(base_size * 2, base_align).unwrap();
156-
let y = heap.allocate_first_fit(base_size * 7, base_align).unwrap();
163+
let layout_1 = Layout::from_size_align(base_size * 2, base_align).unwrap();
164+
let layout_2 = Layout::from_size_align(base_size * 7, base_align).unwrap();
165+
let layout_3 = Layout::from_size_align(base_size * 3, base_align * 4).unwrap();
166+
let layout_4 = Layout::from_size_align(base_size * 4, base_align).unwrap();
167+
168+
let x = heap.allocate_first_fit(layout_1.clone()).unwrap();
169+
let y = heap.allocate_first_fit(layout_2.clone()).unwrap();
157170
assert_eq!(y as usize, x as usize + base_size * 2);
158-
let z = heap.allocate_first_fit(base_size * 3, base_align * 4).unwrap();
171+
let z = heap.allocate_first_fit(layout_3.clone()).unwrap();
159172
assert_eq!(z as usize % (base_size * 4), 0);
160173

161174
unsafe {
162-
heap.deallocate(x, base_size * 2, base_align);
175+
heap.deallocate(x, layout_1.clone());
163176
}
164177

165-
let a = heap.allocate_first_fit(base_size * 4, base_align).unwrap();
166-
let b = heap.allocate_first_fit(base_size * 2, base_align).unwrap();
178+
let a = heap.allocate_first_fit(layout_4.clone()).unwrap();
179+
let b = heap.allocate_first_fit(layout_1.clone()).unwrap();
167180
assert_eq!(b, x);
168181

169182
unsafe {
170-
heap.deallocate(y, base_size * 7, base_align);
171-
heap.deallocate(z, base_size * 3, base_align * 4);
172-
heap.deallocate(a, base_size * 4, base_align);
173-
heap.deallocate(b, base_size * 2, base_align);
183+
heap.deallocate(y, layout_2);
184+
heap.deallocate(z, layout_3);
185+
heap.deallocate(a, layout_4);
186+
heap.deallocate(b, layout_1);
174187
}
175188
}
176189

177190
#[test]
178191
fn allocate_usize() {
179192
let mut heap = new_heap();
180193

181-
assert!(heap.allocate_first_fit(size_of::<usize>(), 1).is_some());
194+
let layout = Layout::from_size_align(size_of::<usize>(), 1).unwrap();
195+
196+
assert!(heap.allocate_first_fit(layout.clone()).is_ok());
182197
}
183198

184199
#[test]
185200
fn allocate_usize_in_bigger_block() {
186201
let mut heap = new_heap();
187202

188-
let x = heap.allocate_first_fit(size_of::<usize>() * 2, 1).unwrap();
189-
let y = heap.allocate_first_fit(size_of::<usize>() * 2, 1).unwrap();
203+
let layout_1 = Layout::from_size_align(size_of::<usize>() * 2, 1).unwrap();
204+
let layout_2 = Layout::from_size_align(size_of::<usize>(), 1).unwrap();
205+
206+
let x = heap.allocate_first_fit(layout_1.clone()).unwrap();
207+
let y = heap.allocate_first_fit(layout_1.clone()).unwrap();
190208
unsafe {
191-
heap.deallocate(x, size_of::<usize>() * 2, 1);
209+
heap.deallocate(x, layout_1.clone());
192210
}
193211

194-
let z = heap.allocate_first_fit(size_of::<usize>(), 1);
195-
assert!(z.is_some());
212+
let z = heap.allocate_first_fit(layout_2.clone());
213+
assert!(z.is_ok());
196214
let z = z.unwrap();
197215
assert_eq!(x, z);
198216

199217
unsafe {
200-
heap.deallocate(y, size_of::<usize>() * 2, 1);
201-
heap.deallocate(z, size_of::<usize>(), 1);
218+
heap.deallocate(y, layout_1.clone());
219+
heap.deallocate(z, layout_2);
202220
}
203221
}
204222

@@ -207,10 +225,13 @@ fn allocate_usize_in_bigger_block() {
207225
fn align_from_small_to_big() {
208226
let mut heap = new_heap();
209227

228+
let layout_1 = Layout::from_size_align(28, 4).unwrap();
229+
let layout_2 = Layout::from_size_align(8, 8).unwrap();
230+
210231
// allocate 28 bytes so that the heap end is only 4 byte aligned
211-
assert!(heap.allocate_first_fit(28, 4).is_some());
232+
assert!(heap.allocate_first_fit(layout_1.clone()).is_ok());
212233
// try to allocate a 8 byte aligned block
213-
assert!(heap.allocate_first_fit(8, 8).is_some());
234+
assert!(heap.allocate_first_fit(layout_2.clone()).is_ok());
214235
}
215236

216237
#[test]
@@ -222,34 +243,40 @@ fn extend_empty_heap() {
222243
}
223244

224245
// Try to allocate full heap after extend
225-
assert!(heap.allocate_first_fit(2048, 1).is_some());
246+
let layout = Layout::from_size_align(2048, 1).unwrap();
247+
assert!(heap.allocate_first_fit(layout.clone()).is_ok());
226248
}
227249

228250
#[test]
229251
fn extend_full_heap() {
230252
let mut heap = new_max_heap();
231253

254+
let layout = Layout::from_size_align(1024, 1).unwrap();
255+
232256
// Allocate full heap, extend and allocate again to the max
233-
assert!(heap.allocate_first_fit(1024, 1).is_some());
257+
assert!(heap.allocate_first_fit(layout.clone()).is_ok());
234258
unsafe {
235259
heap.extend(1024);
236260
}
237-
assert!(heap.allocate_first_fit(1024, 1).is_some());
261+
assert!(heap.allocate_first_fit(layout.clone()).is_ok());
238262
}
239263

240264
#[test]
241265
fn extend_fragmented_heap() {
242266
let mut heap = new_max_heap();
243267

244-
let alloc1 = heap.allocate_first_fit(512, 1);
245-
let alloc2 = heap.allocate_first_fit(512, 1);
268+
let layout_1 = Layout::from_size_align(512, 1).unwrap();
269+
let layout_2 = Layout::from_size_align(1024, 1).unwrap();
270+
271+
let alloc1 = heap.allocate_first_fit(layout_1.clone());
272+
let alloc2 = heap.allocate_first_fit(layout_1.clone());
246273

247-
assert!(alloc1.is_some());
248-
assert!(alloc2.is_some());
274+
assert!(alloc1.is_ok());
275+
assert!(alloc2.is_ok());
249276

250277
unsafe {
251278
// Create a hole at the beginning of the heap
252-
heap.deallocate(alloc1.unwrap(), 512, 1);
279+
heap.deallocate(alloc1.unwrap(), layout_1.clone());
253280
}
254281

255282
unsafe {
@@ -258,5 +285,5 @@ fn extend_fragmented_heap() {
258285

259286
// We got additional 1024 bytes hole at the end of the heap
260287
// Try to allocate there
261-
assert!(heap.allocate_first_fit(1024, 1).is_some());
288+
assert!(heap.allocate_first_fit(layout_2.clone()).is_ok());
262289
}

0 commit comments

Comments
 (0)