1 #![allow(clippy::uninlined_format_args)]
2 //! The integration tests seem to always have `std` linked, so things that would
3 //! depend on that can go here.
4
5 use bytemuck::*;
6 use core::num::NonZeroU8;
7
8 #[test]
test_transparent_vtabled()9 fn test_transparent_vtabled() {
10 use core::fmt::Display;
11
12 #[repr(transparent)]
13 struct DisplayTraitObj(dyn Display);
14
15 unsafe impl TransparentWrapper<dyn Display> for DisplayTraitObj {}
16
17 impl Display for DisplayTraitObj {
18 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19 self.0.fmt(f)
20 }
21 }
22
23 let v = DisplayTraitObj::wrap_ref(&5i32);
24 let s = format!("{}", v);
25 assert_eq!(s, "5");
26
27 let mut x = 100i32;
28 let v_mut = DisplayTraitObj::wrap_mut(&mut x);
29 let s = format!("{}", v_mut);
30 assert_eq!(s, "100");
31 }
32
33 #[test]
34 #[cfg(feature = "extern_crate_alloc")]
test_large_box_alloc()35 fn test_large_box_alloc() {
36 type SuperPage = [[u8; 4096]; 4096];
37 let _: Box<SuperPage> = try_zeroed_box().unwrap();
38 }
39
40 #[test]
41 #[cfg(feature = "extern_crate_alloc")]
test_zero_sized_box_alloc()42 fn test_zero_sized_box_alloc() {
43 #[repr(align(4096))]
44 struct Empty;
45 unsafe impl Zeroable for Empty {}
46 let _: Box<Empty> = try_zeroed_box().unwrap();
47 }
48
49 #[test]
50 #[cfg(feature = "extern_crate_alloc")]
test_try_from_box_bytes()51 fn test_try_from_box_bytes() {
52 // Different layout: target alignment is greater than source alignment.
53 assert_eq!(
54 try_from_box_bytes::<u32>(Box::new([0u8; 4]).into()).map_err(|(x, _)| x),
55 Err(PodCastError::AlignmentMismatch)
56 );
57
58 // Different layout: target alignment is less than source alignment.
59 assert_eq!(
60 try_from_box_bytes::<u32>(Box::new(0u64).into()).map_err(|(x, _)| x),
61 Err(PodCastError::AlignmentMismatch)
62 );
63
64 // Different layout: target size is greater than source size.
65 assert_eq!(
66 try_from_box_bytes::<[u32; 2]>(Box::new(0u32).into()).map_err(|(x, _)| x),
67 Err(PodCastError::SizeMismatch)
68 );
69
70 // Different layout: target size is less than source size.
71 assert_eq!(
72 try_from_box_bytes::<u32>(Box::new([0u32; 2]).into()).map_err(|(x, _)| x),
73 Err(PodCastError::SizeMismatch)
74 );
75
76 // Round trip: alignment is equal to size.
77 assert_eq!(*from_box_bytes::<u32>(Box::new(1000u32).into()), 1000u32);
78
79 // Round trip: alignment is divider of size.
80 assert_eq!(&*from_box_bytes::<[u8; 5]>(Box::new(*b"hello").into()), b"hello");
81
82 // It's ok for T to have uninitialized bytes.
83 #[cfg(feature = "derive")]
84 {
85 #[derive(Debug, Copy, Clone, PartialEq, Eq, AnyBitPattern)]
86 struct Foo(u8, u16);
87 assert_eq!(
88 *from_box_bytes::<Foo>(Box::new([0xc5c5u16; 2]).into()),
89 Foo(0xc5u8, 0xc5c5u16)
90 );
91 }
92 }
93
94 #[test]
95 #[cfg(feature = "extern_crate_alloc")]
test_box_bytes_of()96 fn test_box_bytes_of() {
97 assert_eq!(&*box_bytes_of(Box::new(*b"hello")), b"hello");
98
99 #[cfg(target_endian = "big")]
100 assert_eq!(&*box_bytes_of(Box::new(0x12345678)), b"\x12\x34\x56\x78");
101 #[cfg(target_endian = "little")]
102 assert_eq!(&*box_bytes_of(Box::new(0x12345678)), b"\x78\x56\x34\x12");
103
104 // It's ok for T to have invalid bit patterns.
105 assert_eq!(&*box_bytes_of(Box::new(NonZeroU8::new(0xc5))), b"\xc5");
106 }
107