aboutsummaryrefslogtreecommitdiff
path: root/tests/cast_slice_tests.rs
blob: 2838d46de5497d05a4a95e91254c0c11bca4a40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use core::mem::size_of;

use bytemuck::*;

#[test]
fn test_try_cast_slice() {
  // some align4 data
  let u32_slice: &[u32] = &[4, 5, 6];
  // the same data as align1
  let the_bytes: &[u8] = try_cast_slice(u32_slice).unwrap();

  assert_eq!(
    u32_slice.as_ptr() as *const u32 as usize,
    the_bytes.as_ptr() as *const u8 as usize
  );
  assert_eq!(
    u32_slice.len() * size_of::<u32>(),
    the_bytes.len() * size_of::<u8>()
  );

  // by taking one byte off the front, we're definitely mis-aligned for u32.
  let mis_aligned_bytes = &the_bytes[1..];
  assert_eq!(
    try_cast_slice::<u8, u32>(mis_aligned_bytes),
    Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
  );

  // by taking one byte off the end, we're aligned but would have slop bytes for
  // u32
  let the_bytes_len_minus1 = the_bytes.len() - 1;
  let slop_bytes = &the_bytes[..the_bytes_len_minus1];
  assert_eq!(
    try_cast_slice::<u8, u32>(slop_bytes),
    Err(PodCastError::OutputSliceWouldHaveSlop)
  );

  // if we don't mess with it we can up-alignment cast
  try_cast_slice::<u8, u32>(the_bytes).unwrap();
}

#[test]
fn test_try_cast_slice_mut() {
  // some align4 data
  let u32_slice: &mut [u32] = &mut [4, 5, 6];
  let u32_len = u32_slice.len();
  let u32_ptr = u32_slice.as_ptr();

  // the same data as align1
  let the_bytes: &mut [u8] = try_cast_slice_mut(u32_slice).unwrap();
  let the_bytes_len = the_bytes.len();
  let the_bytes_ptr = the_bytes.as_ptr();

  assert_eq!(
    u32_ptr as *const u32 as usize,
    the_bytes_ptr as *const u8 as usize
  );
  assert_eq!(u32_len * size_of::<u32>(), the_bytes_len * size_of::<u8>());

  // by taking one byte off the front, we're definitely mis-aligned for u32.
  let mis_aligned_bytes = &mut the_bytes[1..];
  assert_eq!(
    try_cast_slice_mut::<u8, u32>(mis_aligned_bytes),
    Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
  );

  // by taking one byte off the end, we're aligned but would have slop bytes for
  // u32
  let the_bytes_len_minus1 = the_bytes.len() - 1;
  let slop_bytes = &mut the_bytes[..the_bytes_len_minus1];
  assert_eq!(
    try_cast_slice_mut::<u8, u32>(slop_bytes),
    Err(PodCastError::OutputSliceWouldHaveSlop)
  );

  // if we don't mess with it we can up-alignment cast
  try_cast_slice_mut::<u8, u32>(the_bytes).unwrap();
}

#[test]
fn test_types() {
  let _: i32 = cast(1.0_f32);
  let _: &mut i32 = cast_mut(&mut 1.0_f32);
  let _: &i32 = cast_ref(&1.0_f32);
  let _: &[i32] = cast_slice(&[1.0_f32]);
  let _: &mut [i32] = cast_slice_mut(&mut [1.0_f32]);
  //
  let _: Result<i32, PodCastError> = try_cast(1.0_f32);
  let _: Result<&mut i32, PodCastError> = try_cast_mut(&mut 1.0_f32);
  let _: Result<&i32, PodCastError> = try_cast_ref(&1.0_f32);
  let _: Result<&[i32], PodCastError> = try_cast_slice(&[1.0_f32]);
  let _: Result<&mut [i32], PodCastError> = try_cast_slice_mut(&mut [1.0_f32]);
}

#[test]
fn test_bytes_of() {
  assert_eq!(bytes_of(&0xaabbccdd_u32), &0xaabbccdd_u32.to_ne_bytes());
  assert_eq!(
    bytes_of_mut(&mut 0xaabbccdd_u32),
    &mut 0xaabbccdd_u32.to_ne_bytes()
  );
  let mut a = 0xaabbccdd_u32;
  let a_addr = &a as *const _ as usize;
  // ensure addresses match.
  assert_eq!(bytes_of(&a).as_ptr() as usize, a_addr);
  assert_eq!(bytes_of_mut(&mut a).as_ptr() as usize, a_addr);
}

#[test]
fn test_try_from_bytes() {
  let u32s = [0xaabbccdd, 0x11223344_u32];
  let bytes = bytemuck::cast_slice::<u32, u8>(&u32s);
  assert_eq!(try_from_bytes::<u32>(&bytes[..4]), Ok(&u32s[0]));
  assert_eq!(
    try_from_bytes::<u32>(&bytes[..5]),
    Err(PodCastError::SizeMismatch)
  );
  assert_eq!(
    try_from_bytes::<u32>(&bytes[..3]),
    Err(PodCastError::SizeMismatch)
  );
  assert_eq!(
    try_from_bytes::<u32>(&bytes[1..5]),
    Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
  );
}

#[test]
fn test_try_from_bytes_mut() {
  let mut abcd = 0xaabbccdd;
  let mut u32s = [abcd, 0x11223344_u32];
  let bytes = bytemuck::cast_slice_mut::<u32, u8>(&mut u32s);
  assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..4]), Ok(&mut abcd));
  assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..4]), Ok(&mut abcd));
  assert_eq!(
    try_from_bytes_mut::<u32>(&mut bytes[..5]),
    Err(PodCastError::SizeMismatch)
  );
  assert_eq!(
    try_from_bytes_mut::<u32>(&mut bytes[..3]),
    Err(PodCastError::SizeMismatch)
  );
  assert_eq!(
    try_from_bytes::<u32>(&bytes[1..5]),
    Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
  );
}

#[test]
fn test_from_bytes() {
  let abcd = 0xaabbccdd_u32;
  let aligned_bytes = bytemuck::bytes_of(&abcd);
  assert_eq!(from_bytes::<u32>(aligned_bytes), &abcd);
  assert!(core::ptr::eq(from_bytes(aligned_bytes), &abcd));
}

#[test]
fn test_from_bytes_mut() {
  let mut a = 0xaabbccdd_u32;
  let a_addr = &a as *const _ as usize;
  let aligned_bytes = bytemuck::bytes_of_mut(&mut a);
  assert_eq!(*from_bytes_mut::<u32>(aligned_bytes), 0xaabbccdd_u32);
  assert_eq!(
    from_bytes_mut::<u32>(aligned_bytes) as *const u32 as usize,
    a_addr
  );
}

// like #[should_panic], but can be a part of another test, instead of requiring
// it to be it's own test.
macro_rules! should_panic {
  ($ex:expr) => {
    assert!(
      std::panic::catch_unwind(|| {
        let _ = $ex;
      })
      .is_err(),
      concat!("should have panicked: `", stringify!($ex), "`")
    );
  };
}

#[test]
fn test_panics() {
  should_panic!(cast_slice::<u8, u32>(&[1u8, 2u8]));
  should_panic!(cast_slice_mut::<u8, u32>(&mut [1u8, 2u8]));
  should_panic!(from_bytes::<u32>(&[1u8, 2]));
  should_panic!(from_bytes::<u32>(&[1u8, 2, 3, 4, 5]));
  should_panic!(from_bytes_mut::<u32>(&mut [1u8, 2]));
  should_panic!(from_bytes_mut::<u32>(&mut [1u8, 2, 3, 4, 5]));
  // use cast_slice on some u32s to get some align>=4 bytes, so we can know
  // we'll give from_bytes unaligned ones.
  let aligned_bytes = bytemuck::cast_slice::<u32, u8>(&[0, 0]);
  should_panic!(from_bytes::<u32>(&aligned_bytes[1..5]));
}