Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Commit 0b5b714

Browse files
authored
Add crate tests (#10)
* Add tests/vec.rs * Add default generic to `vec::` structs * Use global allocator instead of `AbortAlloc` * Add heap tests * Add uninitialized_zero_size_box tests * Add string tests (as much as possible) * Don't use alloc_wg::string::String in vec tests as of missing features * Fix string and heap tests Those tests failed as `to_string` is not possible for downstream crates
1 parent 1503f4b commit 0b5b714

File tree

6 files changed

+2170
-5
lines changed

6 files changed

+2170
-5
lines changed

src/boxed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@ impl<T, A: AllocRef> Box<[T], A> {
398398
let layout = NonZeroLayout::array::<mem::MaybeUninit<T>>(len)?;
399399
a.alloc(layout)
400400
.map_err(|inner| CollectionAllocErr::AllocError { layout, inner })?
401+
.cast()
401402
};
402403
unsafe {
403-
let slice = slice::from_raw_parts_mut(ptr.cast().as_ptr(), len);
404+
let slice = slice::from_raw_parts_mut(ptr.as_ptr(), len);
404405
Ok(Box::from_raw_in(
405406
NonNull::from(slice).as_ptr(),
406407
a.get_build_alloc(),

src/vec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,7 +2833,7 @@ impl<T> Drop for IntoIter<T> {
28332833
///
28342834
/// [`drain`]: struct.Vec.html#method.drain
28352835
/// [`Vec`]: struct.Vec.html
2836-
pub struct Drain<'a, T, A: DeallocRef> {
2836+
pub struct Drain<'a, T, A: DeallocRef = AbortAlloc<Global>> {
28372837
/// Index of tail to preserve
28382838
tail_start: usize,
28392839
/// Length of tail
@@ -2926,7 +2926,7 @@ impl<T, A: DeallocRef> FusedIterator for Drain<'_, T, A> {}
29262926
/// [`splice()`]: struct.Vec.html#method.splice
29272927
/// [`Vec`]: struct.Vec.html
29282928
#[derive(Debug)]
2929-
pub struct Splice<'a, I: Iterator + 'a, A>
2929+
pub struct Splice<'a, I: Iterator + 'a, A = AbortAlloc<Global>>
29302930
where
29312931
A: ReallocRef<Error = crate::Never>,
29322932
{
@@ -3049,7 +3049,7 @@ where
30493049

30503050
/// An iterator produced by calling `drain_filter` on Vec.
30513051
// #[derive(Debug)]
3052-
pub struct DrainFilter<'a, T, F, A: DeallocRef>
3052+
pub struct DrainFilter<'a, T, F, A: DeallocRef = AbortAlloc<Global>>
30533053
where
30543054
F: FnMut(&mut T) -> bool,
30553055
{
@@ -3112,7 +3112,7 @@ where
31123112
F: FnMut(&mut T) -> bool,
31133113
{
31143114
fn drop(&mut self) {
3115-
struct BackshiftOnDrop<'a, 'b, T, F, A: DeallocRef>
3115+
struct BackshiftOnDrop<'a, 'b, T, F, A: DeallocRef = AbortAlloc<Global>>
31163116
where
31173117
F: FnMut(&mut T) -> bool,
31183118
{

tests/boxed.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use alloc_wg::boxed::Box;
2+
use core::{mem::MaybeUninit, ptr::NonNull};
3+
4+
#[test]
5+
fn unitialized_zero_size_box() {
6+
assert_eq!(
7+
&*Box::<()>::new_uninit() as *const _,
8+
NonNull::<MaybeUninit<()>>::dangling().as_ptr(),
9+
);
10+
assert_eq!(
11+
Box::<[()]>::new_uninit_slice(4).as_ptr(),
12+
NonNull::<MaybeUninit<()>>::dangling().as_ptr(),
13+
);
14+
assert_eq!(
15+
Box::<[String]>::new_uninit_slice(0).as_ptr(),
16+
NonNull::<MaybeUninit<String>>::dangling().as_ptr(),
17+
);
18+
}

tests/heap.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use alloc_wg::alloc::{AllocRef, Global, NonZeroLayout};
2+
use core::fmt::Debug;
3+
4+
/// Issue #45955 and #62251.
5+
#[test]
6+
#[cfg(feature = "std")]
7+
fn alloc_system_overaligned_request() {
8+
check_overalign_requests(std::alloc::System)
9+
}
10+
11+
#[test]
12+
fn std_heap_overaligned_request() {
13+
check_overalign_requests(Global)
14+
}
15+
16+
fn check_overalign_requests<T: AllocRef>(mut allocator: T)
17+
where
18+
T::Error: Debug,
19+
{
20+
for &align in &[4, 8, 16, 32] {
21+
// less than and bigger than `MIN_ALIGN`
22+
for &size in &[align / 2, align - 1] {
23+
// size less than alignment
24+
let iterations = 128;
25+
unsafe {
26+
let pointers: Vec<_> = (0..iterations)
27+
.map(|_| {
28+
allocator
29+
.alloc(NonZeroLayout::from_size_align(size, align).unwrap())
30+
.unwrap()
31+
})
32+
.collect();
33+
for &ptr in &pointers {
34+
assert_eq!(
35+
(ptr.as_ptr() as usize) % align,
36+
0,
37+
"Got a pointer less aligned than requested"
38+
)
39+
}
40+
41+
// Clean up
42+
for &ptr in &pointers {
43+
allocator.dealloc(ptr, NonZeroLayout::from_size_align(size, align).unwrap())
44+
}
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)