]> git.lizzy.rs Git - rust.git/blob - library/alloc/tests/heap.rs
Unify Opaque/Projection handling in region outlives code
[rust.git] / library / alloc / tests / heap.rs
1 use std::alloc::{Allocator, 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: Allocator>(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.allocate(Layout::from_size_align(size, align).unwrap()).unwrap()
24                     })
25                     .collect();
26                 for &ptr in &pointers {
27                     assert_eq!(
28                         (ptr.as_non_null_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.deallocate(
37                         ptr.as_non_null_ptr(),
38                         Layout::from_size_align(size, align).unwrap(),
39                     )
40                 }
41             }
42         }
43     }
44 }