]> git.lizzy.rs Git - rust.git/blob - library/core/tests/alloc.rs
Auto merge of #95435 - cjgillot:one-name, r=oli-obk
[rust.git] / library / core / tests / alloc.rs
1 use core::alloc::Layout;
2 use core::ptr::NonNull;
3
4 #[test]
5 fn const_unchecked_layout() {
6     const SIZE: usize = 0x2000;
7     const ALIGN: usize = 0x1000;
8     const LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(SIZE, ALIGN) };
9     const DANGLING: NonNull<u8> = LAYOUT.dangling();
10     assert_eq!(LAYOUT.size(), SIZE);
11     assert_eq!(LAYOUT.align(), ALIGN);
12     assert_eq!(Some(DANGLING), NonNull::new(ALIGN as *mut u8));
13 }
14
15 #[test]
16 fn layout_debug_shows_log2_of_alignment() {
17     // `Debug` is not stable, but here's what it does right now
18     let layout = Layout::from_size_align(24576, 8192).unwrap();
19     let s = format!("{:?}", layout);
20     assert_eq!(s, "Layout { size_: 24576, align_: 8192 (1 << 13) }");
21 }
22
23 // Running this normally doesn't do much, but it's also run in Miri, which
24 // will double-check that these are allowed by the validity invariants.
25 #[test]
26 fn layout_accepts_all_valid_alignments() {
27     for align in 0..usize::BITS {
28         let layout = Layout::from_size_align(0, 1_usize << align).unwrap();
29         assert_eq!(layout.align(), 1_usize << align);
30     }
31 }