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