]> git.lizzy.rs Git - rust.git/blob - src/liballoc/tests/heap.rs
7bc1aac7c8b59643723339646360a8abaf439564
[rust.git] / src / liballoc / tests / heap.rs
1 #![cfg(not(miri))]
2
3 use std::alloc::{Global, Alloc, Layout, System};
4
5 /// Issue #45955.
6 #[test]
7 fn alloc_system_overaligned_request() {
8     check_overalign_requests(System)
9 }
10
11 #[test]
12 fn std_heap_overaligned_request() {
13     check_overalign_requests(Global)
14 }
15
16 fn check_overalign_requests<T: Alloc>(mut allocator: T) {
17     let size = 8;
18     let align = 16; // greater than size
19     let iterations = 100;
20     unsafe {
21         let pointers: Vec<_> = (0..iterations).map(|_| {
22             allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
23         }).collect();
24         for &ptr in &pointers {
25             assert_eq!((ptr.as_ptr() as usize) % align, 0,
26                        "Got a pointer less aligned than requested")
27         }
28
29         // Clean up
30         for &ptr in &pointers {
31             allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
32         }
33     }
34 }