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