summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bytemuck/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/bytemuck/tests
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/bytemuck/tests')
-rw-r--r--third_party/rust/bytemuck/tests/cast_slice_tests.rs90
-rw-r--r--third_party/rust/bytemuck/tests/doc_tests.rs121
-rw-r--r--third_party/rust/bytemuck/tests/std_tests.rs29
3 files changed, 240 insertions, 0 deletions
diff --git a/third_party/rust/bytemuck/tests/cast_slice_tests.rs b/third_party/rust/bytemuck/tests/cast_slice_tests.rs
new file mode 100644
index 0000000000..1177a7fb0b
--- /dev/null
+++ b/third_party/rust/bytemuck/tests/cast_slice_tests.rs
@@ -0,0 +1,90 @@
+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]);
+}
diff --git a/third_party/rust/bytemuck/tests/doc_tests.rs b/third_party/rust/bytemuck/tests/doc_tests.rs
new file mode 100644
index 0000000000..e5a80db44f
--- /dev/null
+++ b/third_party/rust/bytemuck/tests/doc_tests.rs
@@ -0,0 +1,121 @@
+//! Cargo miri doesn't run doctests yet, so we duplicate these here. It's
+//! probably not that important to sweat keeping these perfectly up to date, but
+//! we should try to catch the cases where the primary tests are doctests.
+use bytemuck::*;
+
+// Miri doesn't run on doctests, so... copypaste to the rescue.
+#[test]
+fn test_transparent_slice() {
+ #[repr(transparent)]
+ struct Slice<T>([T]);
+
+ unsafe impl<T> TransparentWrapper<[T]> for Slice<T> {}
+
+ let s = Slice::wrap_ref(&[1u32, 2, 3]);
+ assert_eq!(&s.0, &[1, 2, 3]);
+
+ let mut buf = [1, 2, 3u8];
+ let _sm = Slice::wrap_mut(&mut buf);
+}
+
+#[test]
+fn test_transparent_basic() {
+ #[derive(Default)]
+ struct SomeStruct(u32);
+
+ #[repr(transparent)]
+ struct MyWrapper(SomeStruct);
+
+ unsafe impl TransparentWrapper<SomeStruct> for MyWrapper {}
+
+ // interpret a reference to &SomeStruct as a &MyWrapper
+ let thing = SomeStruct::default();
+ let wrapped_ref: &MyWrapper = MyWrapper::wrap_ref(&thing);
+
+ // Works with &mut too.
+ let mut mut_thing = SomeStruct::default();
+ let wrapped_mut: &mut MyWrapper = MyWrapper::wrap_mut(&mut mut_thing);
+ let _ = (wrapped_ref, wrapped_mut);
+}
+
+// Work around miri not running doctests
+#[test]
+fn test_contiguous_doc() {
+ #[repr(u8)]
+ #[derive(Debug, Copy, Clone, PartialEq)]
+ enum Foo {
+ A = 0,
+ B = 1,
+ C = 2,
+ D = 3,
+ E = 4,
+ }
+ unsafe impl Contiguous for Foo {
+ type Int = u8;
+ const MIN_VALUE: u8 = Foo::A as u8;
+ const MAX_VALUE: u8 = Foo::E as u8;
+ }
+
+ assert_eq!(Foo::from_integer(3).unwrap(), Foo::D);
+ assert_eq!(Foo::from_integer(8), None);
+ assert_eq!(Foo::C.into_integer(), 2);
+ assert_eq!(Foo::B.into_integer(), Foo::B as u8);
+}
+
+#[test]
+fn test_offsetof_vertex() {
+ #[repr(C)]
+ struct Vertex {
+ pos: [f32; 2],
+ uv: [u16; 2],
+ color: [u8; 4],
+ }
+ unsafe impl Zeroable for Vertex {}
+
+ let pos = offset_of!(Zeroable::zeroed(), Vertex, pos);
+ let uv = offset_of!(Zeroable::zeroed(), Vertex, uv);
+ let color = offset_of!(Zeroable::zeroed(), Vertex, color);
+
+ assert_eq!(pos, 0);
+ assert_eq!(uv, 8);
+ assert_eq!(color, 12);
+}
+
+#[test]
+fn test_offsetof_nonpod() {
+ #[derive(Default)]
+ struct Foo {
+ a: u8,
+ b: &'static str,
+ c: i32,
+ }
+
+ let a_offset = offset_of!(Default::default(), Foo, a);
+ let b_offset = offset_of!(Default::default(), Foo, b);
+ let c_offset = offset_of!(Default::default(), Foo, c);
+
+ assert_ne!(a_offset, b_offset);
+ assert_ne!(b_offset, c_offset);
+ // We can't check against hardcoded values for a repr(Rust) type,
+ // but prove to ourself this way.
+
+ let foo = Foo::default();
+ // Note: offsets are in bytes.
+ let as_bytes = &foo as *const _ as *const u8;
+
+ // we're using wrapping_offset here becasue it's not worth
+ // the unsafe block, but it would be valid to use `add` instead,
+ // as it cannot overflow.
+ assert_eq!(
+ &foo.a as *const _ as usize,
+ as_bytes.wrapping_add(a_offset) as usize
+ );
+ assert_eq!(
+ &foo.b as *const _ as usize,
+ as_bytes.wrapping_add(b_offset) as usize
+ );
+ assert_eq!(
+ &foo.c as *const _ as usize,
+ as_bytes.wrapping_add(c_offset) as usize
+ );
+}
diff --git a/third_party/rust/bytemuck/tests/std_tests.rs b/third_party/rust/bytemuck/tests/std_tests.rs
new file mode 100644
index 0000000000..0e0fb48710
--- /dev/null
+++ b/third_party/rust/bytemuck/tests/std_tests.rs
@@ -0,0 +1,29 @@
+//! The integration tests seem to always have `std` linked, so things that would
+//! depend on that can go here.
+
+use bytemuck::*;
+
+#[test]
+fn test_transparent_vtabled() {
+ use core::fmt::Display;
+
+ #[repr(transparent)]
+ struct DisplayTraitObj(dyn Display);
+
+ unsafe impl TransparentWrapper<dyn Display> for DisplayTraitObj {}
+
+ impl Display for DisplayTraitObj {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ self.0.fmt(f)
+ }
+ }
+
+ let v = DisplayTraitObj::wrap_ref(&5i32);
+ let s = format!("{}", v);
+ assert_eq!(s, "5");
+
+ let mut x = 100i32;
+ let v_mut = DisplayTraitObj::wrap_mut(&mut x);
+ let s = format!("{}", v_mut);
+ assert_eq!(s, "100");
+}