Skip to content

Commit 70494c3

Browse files
committed
Add some tests for the new try_extend checks
1 parent 8230769 commit 70494c3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/test.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,54 @@ fn extend_fragmented_heap() {
495495
// Try to allocate there
496496
assert!(heap.allocate_first_fit(layout_2.clone()).is_ok());
497497
}
498+
499+
/// Ensures that `Heap::extend` fails for very small sizes.
500+
///
501+
/// The size needs to be big enough to hold a hole, otherwise
502+
/// the hole write would result in an out of bounds write.
503+
#[test]
504+
fn small_heap_extension() {
505+
static mut HEAP: [u8; 33] = [0; 33];
506+
let result = unsafe {
507+
let mut heap = Heap::new(HEAP.as_mut_ptr(), 32);
508+
heap.try_extend(1)
509+
};
510+
assert_eq!(result, Err(ExtendError::SizeTooSmall))
511+
}
512+
513+
/// Ensures that `Heap::extend` fails for sizes that are not a multiple of the hole size.
514+
#[test]
515+
fn oddly_sized_heap_extension() {
516+
static mut HEAP: [u8; 33] = [0; 33];
517+
let result = unsafe {
518+
let mut heap = Heap::new(HEAP.as_mut_ptr(), 16);
519+
heap.try_extend(17)
520+
};
521+
assert_eq!(result, Err(ExtendError::OddSize))
522+
}
523+
524+
/// Ensures that heap extension fails when trying to extend an empty heap.
525+
///
526+
/// Empty heaps always have a start pointer of 0.
527+
#[test]
528+
fn extend_empty() {
529+
let result = unsafe {
530+
let mut heap = Heap::empty();
531+
heap.try_extend(16)
532+
};
533+
assert_eq!(result, Err(ExtendError::EmptyHeap))
534+
}
535+
536+
/// Ensures that heap extension fails when trying to extend an oddly-sized heap.
537+
///
538+
/// To extend the heap, we need to place a hole at the old top of the heap. This
539+
/// only works if the top pointer is sufficiently aligned.
540+
#[test]
541+
fn extend_odd_size() {
542+
static mut HEAP: [u8; 33] = [0; 33];
543+
let result = unsafe {
544+
let mut heap = Heap::new(HEAP.as_mut_ptr(), 17);
545+
heap.try_extend(16)
546+
};
547+
assert_eq!(result, Err(ExtendError::OddHeapSize))
548+
}

0 commit comments

Comments
 (0)