Skip to content

Commit

Permalink
Add tests for alloc_slice_fill
Browse files Browse the repository at this point in the history
  • Loading branch information
TethysSvensson committed Nov 18, 2019
1 parent b70b52a commit f18e9f1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/alloc_fill.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use bumpalo::Bump;
use std::alloc::Layout;

#[test]
fn alloc_slice_fill_zero() {
let b = Bump::new();
let layout = Layout::new::<u8>();

let ptr1 = b.alloc_layout(layout);

struct MyZeroSizedType;

b.alloc_slice_copy::<u64>(&[]);
b.alloc_slice_clone::<String>(&[]);
b.alloc_slice_fill_with::<String, _>(0, |_| panic!("should not happen"));
b.alloc_slice_fill_copy(0, 42u64);
b.alloc_slice_fill_clone(0, &"hello".to_string());
b.alloc_slice_fill_default::<String>(0);
b.alloc(MyZeroSizedType);

let ptr2 = b.alloc_layout(layout);
assert_eq!(ptr1.as_ptr() as usize, ptr2.as_ptr() as usize + 1);
}

#[test]
#[should_panic(expected = "out of memory")]
fn alloc_slice_overflow() {
let b = Bump::new();

b.alloc_slice_fill_default::<u64>(usize::max_value());
}
19 changes: 19 additions & 0 deletions tests/quickchecks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,23 @@ quickcheck! {
assert_eq!(sizes.into_iter().sum::<usize>(), 0);
}
}

fn alloc_slices(allocs: Vec<(u8, usize)>) -> () {
let b = Bump::new();
let mut allocated: Vec<(usize, usize)> = vec![];
for (val, len) in allocs {
let len = len % 100;
let s = b.alloc_slice_fill_copy(len, val);

assert_eq!(s.len(), len);
assert!(s.iter().all(|v| v == &val));

let range = (s.as_ptr() as usize, unsafe { s.as_ptr().add(s.len()) } as usize);
for r in &allocated {
let no_overlap = range.1 <= r.0 || r.1 <= range.0;
assert!(no_overlap);
}
allocated.push(range);
}
}
}

0 comments on commit f18e9f1

Please sign in to comment.