Skip to content

Commit 9d972e8

Browse files
committed
Fix tests and move them to seperate module
1 parent 1a4e6b0 commit 9d972e8

File tree

3 files changed

+194
-196
lines changed

3 files changed

+194
-196
lines changed

src/hole.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,18 @@ impl HoleList {
5959

6060
#[cfg(test)]
6161
pub fn first_hole(&self) -> Option<(usize, usize)> {
62-
if let Some(first) = self.first.next.as_ref() {
63-
Some((**first as usize, unsafe { first.get().size }))
64-
} else {
65-
None
66-
}
62+
self.first.next.as_ref().map(|hole| (**hole as usize, unsafe { hole.get().size }))
6763
}
6864
}
6965

66+
/// A block containing free memory. It points to the next hole and thus forms a linked list.
67+
#[cfg(not(test))]
68+
struct Hole {
69+
size: usize,
70+
next: Option<Unique<Hole>>,
71+
}
72+
73+
#[cfg(test)]
7074
pub struct Hole {
7175
pub size: usize,
7276
pub next: Option<Unique<Hole>>,

src/lib.rs

Lines changed: 2 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern crate std;
99
use hole::HoleList;
1010

1111
mod hole;
12+
#[cfg(test)]
13+
mod test;
1214

1315
pub struct Heap {
1416
bottom: usize,
@@ -64,194 +66,3 @@ fn align_down(value: usize, align: usize) -> usize {
6466
fn align_up(value: usize, align: usize) -> usize {
6567
align_down(value + align - 1, align)
6668
}
67-
68-
#[cfg(test)]
69-
mod test {
70-
use std::prelude::v1::*;
71-
use std::mem::{size_of, align_of};
72-
use super::*;
73-
use super::hole::*;
74-
75-
fn new_heap() -> Heap {
76-
const HEAP_SIZE: usize = 1000;
77-
let heap_space = Box::into_raw(Box::new([0u8; HEAP_SIZE]));
78-
79-
let heap_bottom = heap_space as usize;
80-
let heap_top = heap_bottom + HEAP_SIZE;
81-
let heap = Heap::new(heap_bottom, heap_top);
82-
assert!(heap.bottom == heap_bottom);
83-
assert!(heap.top == heap_top);
84-
heap
85-
}
86-
87-
#[test]
88-
fn allocate_double_usize() {
89-
let mut heap = new_heap();
90-
let size = size_of::<usize>() * 2;
91-
let addr = heap.allocate_first_fit(size, align_of::<usize>());
92-
assert!(addr.is_some());
93-
let addr = addr.unwrap() as usize;
94-
assert!(addr == heap.bottom);
95-
let (hole_addr, hole_size) = heap.holes.first_hole().expect("ERROR: no hole left");
96-
assert!(hole_addr == heap.bottom + size);
97-
assert!(hole_size == heap.top - heap.bottom - size);
98-
99-
unsafe {
100-
assert_eq!((*((addr + size) as *const Hole)).size,
101-
heap.top - heap.bottom - size);
102-
}
103-
}
104-
105-
#[test]
106-
fn allocate_and_free_double_usize() {
107-
let mut heap = new_heap();
108-
109-
let x = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap();
110-
unsafe {
111-
*(x as *mut (usize, usize)) = (0xdeafdeadbeafbabe, 0xdeafdeadbeafbabe);
112-
}
113-
heap.deallocate(x, size_of::<usize>() * 2, align_of::<usize>());
114-
115-
unsafe {
116-
assert_eq!((*(heap.bottom as *const Hole)).size, heap.top - heap.bottom);
117-
assert!((*(heap.bottom as *const Hole)).next.is_none());
118-
}
119-
}
120-
121-
#[test]
122-
fn deallocate_right_before() {
123-
let mut heap = new_heap();
124-
let size = size_of::<usize>() * 5;
125-
126-
let x = heap.allocate_first_fit(size, 1).unwrap();
127-
let y = heap.allocate_first_fit(size, 1).unwrap();
128-
let z = heap.allocate_first_fit(size, 1).unwrap();
129-
130-
heap.deallocate(y, size, 1);
131-
unsafe {
132-
assert_eq!((*(y as *const Hole)).size, size);
133-
}
134-
heap.deallocate(x, size, 1);
135-
unsafe {
136-
assert_eq!((*(x as *const Hole)).size, size * 2);
137-
}
138-
heap.deallocate(z, size, 1);
139-
unsafe {
140-
assert_eq!((*(x as *const Hole)).size, heap.top - heap.bottom);
141-
}
142-
}
143-
144-
#[test]
145-
fn deallocate_right_behind() {
146-
let mut heap = new_heap();
147-
let size = size_of::<usize>() * 5;
148-
149-
let x = heap.allocate_first_fit(size, 1).unwrap();
150-
let y = heap.allocate_first_fit(size, 1).unwrap();
151-
let z = heap.allocate_first_fit(size, 1).unwrap();
152-
153-
heap.deallocate(x, size, 1);
154-
unsafe {
155-
assert_eq!((*(x as *const Hole)).size, size);
156-
}
157-
heap.deallocate(y, size, 1);
158-
unsafe {
159-
assert_eq!((*(x as *const Hole)).size, size * 2);
160-
}
161-
heap.deallocate(z, size, 1);
162-
unsafe {
163-
assert_eq!((*(x as *const Hole)).size, heap.top - heap.bottom);
164-
}
165-
}
166-
167-
#[test]
168-
fn deallocate_middle() {
169-
let mut heap = new_heap();
170-
let size = size_of::<usize>() * 5;
171-
172-
let x = heap.allocate_first_fit(size, 1).unwrap();
173-
let y = heap.allocate_first_fit(size, 1).unwrap();
174-
let z = heap.allocate_first_fit(size, 1).unwrap();
175-
let a = heap.allocate_first_fit(size, 1).unwrap();
176-
177-
heap.deallocate(x, size, 1);
178-
unsafe {
179-
assert_eq!((*(x as *const Hole)).size, size);
180-
}
181-
heap.deallocate(z, size, 1);
182-
unsafe {
183-
assert_eq!((*(x as *const Hole)).size, size);
184-
assert_eq!((*(z as *const Hole)).size, size);
185-
}
186-
heap.deallocate(y, size, 1);
187-
unsafe {
188-
assert_eq!((*(x as *const Hole)).size, size * 3);
189-
}
190-
heap.deallocate(a, size, 1);
191-
unsafe {
192-
assert_eq!((*(x as *const Hole)).size, heap.top - heap.bottom);
193-
}
194-
}
195-
196-
#[test]
197-
fn reallocate_double_usize() {
198-
let mut heap = new_heap();
199-
200-
let x = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap();
201-
heap.deallocate(x, size_of::<usize>() * 2, align_of::<usize>());
202-
203-
let y = heap.allocate_first_fit(size_of::<usize>() * 2, align_of::<usize>()).unwrap();
204-
heap.deallocate(y, size_of::<usize>() * 2, align_of::<usize>());
205-
206-
assert_eq!(x, y);
207-
}
208-
209-
#[test]
210-
fn allocate_multiple_sizes() {
211-
let mut heap = new_heap();
212-
let base_size = size_of::<usize>();
213-
let base_align = align_of::<usize>();
214-
215-
let x = heap.allocate_first_fit(base_size * 2, base_align).unwrap();
216-
let y = heap.allocate_first_fit(base_size * 7, base_align).unwrap();
217-
assert_eq!(y as usize, x as usize + base_size * 2);
218-
let z = heap.allocate_first_fit(base_size * 3, base_align * 4).unwrap();
219-
assert_eq!(z as usize % (base_size * 4), 0);
220-
221-
heap.deallocate(x, base_size * 2, base_align);
222-
223-
let a = heap.allocate_first_fit(base_size * 4, base_align).unwrap();
224-
let b = heap.allocate_first_fit(base_size * 2, base_align).unwrap();
225-
assert_eq!(b, x);
226-
227-
heap.deallocate(y, base_size * 7, base_align);
228-
heap.deallocate(z, base_size * 3, base_align * 4);
229-
heap.deallocate(a, base_size * 4, base_align);
230-
heap.deallocate(b, base_size * 2, base_align);
231-
}
232-
233-
#[test]
234-
fn allocate_usize() {
235-
let mut heap = new_heap();
236-
237-
assert!(heap.allocate_first_fit(size_of::<usize>(), 1).is_some());
238-
}
239-
240-
241-
#[test]
242-
fn allocate_usize_in_bigger_block() {
243-
let mut heap = new_heap();
244-
245-
let x = heap.allocate_first_fit(size_of::<usize>() * 2, 1).unwrap();
246-
let y = heap.allocate_first_fit(size_of::<usize>() * 2, 1).unwrap();
247-
heap.deallocate(x, size_of::<usize>() * 2, 1);
248-
249-
let z = heap.allocate_first_fit(size_of::<usize>(), 1);
250-
assert!(z.is_some());
251-
let z = z.unwrap();
252-
assert_eq!(x, z);
253-
254-
heap.deallocate(y, size_of::<usize>() * 2, 1);
255-
heap.deallocate(z, size_of::<usize>(), 1);
256-
}
257-
}

0 commit comments

Comments
 (0)