1 use core::mem::size_of;
2
3 use bytemuck::*;
4
5 #[test]
test_try_cast_slice()6 fn test_try_cast_slice() {
7 // some align4 data
8 let u32_slice: &[u32] = &[4, 5, 6];
9 // the same data as align1
10 let the_bytes: &[u8] = try_cast_slice(u32_slice).unwrap();
11
12 assert_eq!(
13 u32_slice.as_ptr() as *const u32 as usize,
14 the_bytes.as_ptr() as *const u8 as usize
15 );
16 assert_eq!(
17 u32_slice.len() * size_of::<u32>(),
18 the_bytes.len() * size_of::<u8>()
19 );
20
21 // by taking one byte off the front, we're definitely mis-aligned for u32.
22 let mis_aligned_bytes = &the_bytes[1..];
23 assert_eq!(
24 try_cast_slice::<u8, u32>(mis_aligned_bytes),
25 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
26 );
27
28 // by taking one byte off the end, we're aligned but would have slop bytes for u32
29 let the_bytes_len_minus1 = the_bytes.len() - 1;
30 let slop_bytes = &the_bytes[..the_bytes_len_minus1];
31 assert_eq!(
32 try_cast_slice::<u8, u32>(slop_bytes),
33 Err(PodCastError::OutputSliceWouldHaveSlop)
34 );
35
36 // if we don't mess with it we can up-alignment cast
37 try_cast_slice::<u8, u32>(the_bytes).unwrap();
38 }
39
40 #[test]
test_try_cast_slice_mut()41 fn test_try_cast_slice_mut() {
42 // some align4 data
43 let u32_slice: &mut [u32] = &mut [4, 5, 6];
44 let u32_len = u32_slice.len();
45 let u32_ptr = u32_slice.as_ptr();
46
47 // the same data as align1
48 let the_bytes: &mut [u8] = try_cast_slice_mut(u32_slice).unwrap();
49 let the_bytes_len = the_bytes.len();
50 let the_bytes_ptr = the_bytes.as_ptr();
51
52 assert_eq!(
53 u32_ptr as *const u32 as usize,
54 the_bytes_ptr as *const u8 as usize
55 );
56 assert_eq!(u32_len * size_of::<u32>(), the_bytes_len * size_of::<u8>());
57
58 // by taking one byte off the front, we're definitely mis-aligned for u32.
59 let mis_aligned_bytes = &mut the_bytes[1..];
60 assert_eq!(
61 try_cast_slice_mut::<u8, u32>(mis_aligned_bytes),
62 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
63 );
64
65 // by taking one byte off the end, we're aligned but would have slop bytes for u32
66 let the_bytes_len_minus1 = the_bytes.len() - 1;
67 let slop_bytes = &mut the_bytes[..the_bytes_len_minus1];
68 assert_eq!(
69 try_cast_slice_mut::<u8, u32>(slop_bytes),
70 Err(PodCastError::OutputSliceWouldHaveSlop)
71 );
72
73 // if we don't mess with it we can up-alignment cast
74 try_cast_slice_mut::<u8, u32>(the_bytes).unwrap();
75 }
76
77 #[test]
test_types()78 fn test_types() {
79 let _: i32 = cast(1.0_f32);
80 let _: &mut i32 = cast_mut(&mut 1.0_f32);
81 let _: &i32 = cast_ref(&1.0_f32);
82 let _: &[i32] = cast_slice(&[1.0_f32]);
83 let _: &mut [i32] = cast_slice_mut(&mut [1.0_f32]);
84 //
85 let _: Result<i32, PodCastError> = try_cast(1.0_f32);
86 let _: Result<&mut i32, PodCastError> = try_cast_mut(&mut 1.0_f32);
87 let _: Result<&i32, PodCastError> = try_cast_ref(&1.0_f32);
88 let _: Result<&[i32], PodCastError> = try_cast_slice(&[1.0_f32]);
89 let _: Result<&mut [i32], PodCastError> = try_cast_slice_mut(&mut [1.0_f32]);
90 }
91
92 #[test]
test_bytes_of()93 fn test_bytes_of() {
94 assert_eq!(bytes_of(&0xaabbccdd_u32), &0xaabbccdd_u32.to_ne_bytes());
95 assert_eq!(bytes_of_mut(&mut 0xaabbccdd_u32), &mut 0xaabbccdd_u32.to_ne_bytes());
96 let mut a = 0xaabbccdd_u32;
97 let a_addr = &a as *const _ as usize;
98 // ensure addresses match.
99 assert_eq!(bytes_of(&a).as_ptr() as usize, a_addr);
100 assert_eq!(bytes_of_mut(&mut a).as_ptr() as usize, a_addr);
101 }
102
103 #[test]
test_try_from_bytes()104 fn test_try_from_bytes() {
105 let u32s = [0xaabbccdd, 0x11223344_u32];
106 let bytes = bytemuck::cast_slice::<u32, u8>(&u32s);
107 assert_eq!(try_from_bytes::<u32>(&bytes[..4]), Ok(&u32s[0]));
108 assert_eq!(try_from_bytes::<u32>(&bytes[..5]), Err(PodCastError::SizeMismatch));
109 assert_eq!(try_from_bytes::<u32>(&bytes[..3]), Err(PodCastError::SizeMismatch));
110 assert_eq!(try_from_bytes::<u32>(&bytes[1..5]), Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned));
111 }
112
113 #[test]
test_try_from_bytes_mut()114 fn test_try_from_bytes_mut() {
115 let mut abcd = 0xaabbccdd;
116 let mut u32s = [abcd, 0x11223344_u32];
117 let bytes = bytemuck::cast_slice_mut::<u32, u8>(&mut u32s);
118 assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..4]), Ok(&mut abcd));
119 assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..4]), Ok(&mut abcd));
120 assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..5]), Err(PodCastError::SizeMismatch));
121 assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..3]), Err(PodCastError::SizeMismatch));
122 assert_eq!(try_from_bytes::<u32>(&mut bytes[1..5]), Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned));
123 }
124
125 #[test]
test_from_bytes()126 fn test_from_bytes() {
127 let abcd = 0xaabbccdd_u32;
128 let aligned_bytes = bytemuck::bytes_of(&abcd);
129 assert_eq!(from_bytes::<u32>(aligned_bytes), &abcd);
130 assert!(core::ptr::eq(from_bytes(aligned_bytes), &abcd));
131 }
132
133 #[test]
test_from_bytes_mut()134 fn test_from_bytes_mut() {
135 let mut a = 0xaabbccdd_u32;
136 let a_addr = &a as *const _ as usize;
137 let aligned_bytes = bytemuck::bytes_of_mut(&mut a);
138 assert_eq!(*from_bytes_mut::<u32>(aligned_bytes), 0xaabbccdd_u32);
139 assert_eq!(from_bytes_mut::<u32>(aligned_bytes) as *const u32 as usize, a_addr);
140 }
141
142 // like #[should_panic], but can be a part of another test, instead of requiring
143 // it to be it's own test.
144 macro_rules! should_panic {
145 ($ex:expr) => {
146 assert!(
147 std::panic::catch_unwind(|| { let _ = $ex; }).is_err(),
148 concat!("should have panicked: `", stringify!($ex), "`")
149 );
150 };
151 }
152
153 #[test]
test_panics()154 fn test_panics() {
155 should_panic!(cast_slice::<u8, u32>(&[1u8, 2u8]));
156 should_panic!(cast_slice_mut::<u8, u32>(&mut [1u8, 2u8]));
157 should_panic!(from_bytes::<u32>(&[1u8, 2]));
158 should_panic!(from_bytes::<u32>(&[1u8, 2, 3, 4, 5]));
159 should_panic!(from_bytes_mut::<u32>(&mut [1u8, 2]));
160 should_panic!(from_bytes_mut::<u32>(&mut [1u8, 2, 3, 4, 5]));
161 // use cast_slice on some u32s to get some align>=4 bytes, so we can know
162 // we'll give from_bytes unaligned ones.
163 let aligned_bytes = bytemuck::cast_slice::<u32, u8>(&[0, 0]);
164 should_panic!(from_bytes::<u32>(&aligned_bytes[1..5]));
165 }
166