]> git.lizzy.rs Git - rust.git/blob - src/liballoc/tests/heap.rs
rustc_span: return an impl Iterator instead of a Vec from macro_backtrace.
[rust.git] / src / liballoc / tests / heap.rs
1 use std::alloc::{Alloc, Global, Layout, System};
2
3 /// Issue #45955 and #62251.
4 #[test]
5 fn alloc_system_overaligned_request() {
6     check_overalign_requests(System)
7 }
8
9 #[test]
10 fn std_heap_overaligned_request() {
11     check_overalign_requests(Global)
12 }
13
14 fn check_overalign_requests<T: Alloc>(mut allocator: T) {
15     for &align in &[4, 8, 16, 32] {
16         // less than and bigger than `MIN_ALIGN`
17         for &size in &[align / 2, align - 1] {
18             // size less than alignment
19             let iterations = 128;
20             unsafe {
21                 let pointers: Vec<_> = (0..iterations)
22                     .map(|_| {
23                         allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
24                     })
25                     .collect();
26                 for &ptr in &pointers {
27                     assert_eq!(
28                         (ptr.as_ptr() as usize) % align,
29                         0,
30                         "Got a pointer less aligned than requested"
31                     )
32                 }
33
34                 // Clean up
35                 for &ptr in &pointers {
36                     allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
37                 }
38             }
39         }
40     }
41 }