Skip to content

Commit 2aabc60

Browse files
author
lif
committed
unit test for generic Align4::as_slice
1 parent 1cc1831 commit 2aabc60

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,28 @@ mod test {
308308
assert_eq!(a.as_u16_slice(), &[0x100_u16.to_le(), 0x302_u16.to_le()]);
309309
assert_eq!(a.as_u32_slice(), &[0x3020100_u32.to_le()]);
310310
}
311+
312+
#[test_case]
313+
fn align4_as_generic() {
314+
// with padding
315+
#[repr(C, align(4))]
316+
#[derive(PartialEq, Debug)]
317+
struct FiveByte([u8; 5]);
318+
319+
assert_eq!(
320+
Align4(*b"hello...world...").as_slice::<FiveByte>(),
321+
&[FiveByte(*b"hello"), FiveByte(*b"world")]
322+
);
323+
324+
// and without
325+
#[repr(C, align(2))]
326+
#[derive(PartialEq, Debug)]
327+
struct ThreeHalfWords(u16, u16, u16);
328+
329+
assert_eq!(
330+
Align4([0x11u8, 0x11u8, 0x22u8, 0x22u8, 0x33u8, 0x33u8])
331+
.as_slice::<ThreeHalfWords>(),
332+
&[ThreeHalfWords(0x1111, 0x2222, 0x3333)]
333+
);
334+
}
311335
}

0 commit comments

Comments
 (0)