From cd69736d72e31adfd90545cdd05504d6b38c3a8f Mon Sep 17 00:00:00 2001 From: FreezyLemon Date: Tue, 30 Apr 2024 03:37:10 +0200 Subject: [PATCH] Add tests to pixel.rs (`trait Pixel` and `enum ChromaSampling`) (#35) * Add Pixel and ChromaSampling tests * Fix formatting in plane tests --- src/pixel.rs | 62 ++++++- src/plane.rs | 459 +++++++++++++++++++++++++-------------------------- 2 files changed, 286 insertions(+), 235 deletions(-) diff --git a/src/pixel.rs b/src/pixel.rs index 49615ca..b12ed11 100644 --- a/src/pixel.rs +++ b/src/pixel.rs @@ -8,7 +8,7 @@ // PATENTS file, you can obtain it at www.aomedia.org/license/patent. #[cfg(feature = "serialize")] -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] use wasm_bindgen::prelude::*; @@ -110,7 +110,6 @@ pub trait Pixel: /// Converts stride in pixels to stride in bytes. #[inline] - #[allow(clippy::wrong_self_convention)] fn to_asm_stride(in_stride: usize) -> isize { (in_stride * size_of::()) as isize } @@ -238,6 +237,24 @@ mod test { #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] wasm_bindgen_test_configure!(run_in_browser); + #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)] + #[test] + fn asm_stride() { + let tests = [(7, 7, 14), (12, 12, 24), (1234, 1234, 2468)]; + + for (in_stride, u8_bytes, u16_bytes) in tests { + assert_eq!(u8::to_asm_stride(in_stride), u8_bytes); + assert_eq!(u16::to_asm_stride(in_stride), u16_bytes); + } + } + + #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)] + #[test] + fn type_enum() { + assert!(u8::type_enum() == PixelType::U8); + assert!(u16::type_enum() == PixelType::U16); + } + #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)] #[test] fn chroma_sampling_from_int() { @@ -255,8 +272,45 @@ mod test { assert_eq!(chroma_sampling, converted); let converted_uint = ChromaSampling::from_u32(int as u32); - assert_eq!(chroma_sampling, converted_uint, "FromPrimitive does not return the same result for i32 and u32"); + assert_eq!(chroma_sampling, converted_uint); } } -} + #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)] + #[test] + fn display_chroma_sampling() { + use std::fmt::Write; + + let all_cs = [ + (ChromaSampling::Cs420, "4:2:0"), + (ChromaSampling::Cs422, "4:2:2"), + (ChromaSampling::Cs444, "4:4:4"), + (ChromaSampling::Cs400, "Monochrome"), + ]; + + for (cs, expected) in all_cs { + let mut s = String::new(); + write!(&mut s, "{cs}").expect("can display"); + assert_eq!(&s, expected); + } + } + + #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)] + #[test] + fn chroma_sampling_dimensions() { + let tests = [ + ((1024, 768), ChromaSampling::Cs444, (1024, 768)), + ((1024, 768), ChromaSampling::Cs422, (512, 768)), + ((1024, 768), ChromaSampling::Cs420, (512, 384)), + ((1024, 768), ChromaSampling::Cs400, (0, 0)), + // Check odd width/height + ((1023, 768), ChromaSampling::Cs422, (512, 768)), + ((1023, 767), ChromaSampling::Cs420, (512, 384)), + ]; + + for (luma, cs, expected_chroma) in tests { + let chroma = cs.get_chroma_dimensions(luma.0, luma.1); + assert_eq!(chroma, expected_chroma); + } + } +} diff --git a/src/plane.rs b/src/plane.rs index bd1551d..0d8417a 100644 --- a/src/plane.rs +++ b/src/plane.rs @@ -905,17 +905,17 @@ pub mod test { #[test] fn copy_from_raw_u8() { #[rustfmt::skip] - let mut plane = Plane::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 2, 3, 4, 0, 0, - 0, 0, 8, 7, 6, 5, 0, 0, - 0, 0, 9, 8, 7, 6, 0, 0, - 0, 0, 2, 3, 4, 5, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0], - 8, - ); + let mut plane = Plane::from_slice(&[ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 0, 0, + 0, 0, 8, 7, 6, 5, 0, 0, + 0, 0, 9, 8, 7, 6, 0, 0, + 0, 0, 2, 3, 4, 5, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + ], 8); let input = vec![42u8; 64]; @@ -930,17 +930,17 @@ pub mod test { #[test] fn copy_to_raw_u8() { #[rustfmt::skip] - let plane = Plane::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 2, 3, 4, 0, 0, - 0, 0, 8, 7, 6, 5, 0, 0, - 0, 0, 9, 8, 7, 6, 0, 0, - 0, 0, 2, 3, 4, 5, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0], - 8, - ); + let plane = Plane::from_slice(&[ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 0, 0, + 0, 0, 8, 7, 6, 5, 0, 0, + 0, 0, 9, 8, 7, 6, 0, 0, + 0, 0, 2, 3, 4, 5, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + ], 8); let mut output = vec![42u8; 64]; @@ -955,38 +955,38 @@ pub mod test { #[test] fn test_plane_downsample() { #[rustfmt::skip] - let plane = Plane:: { - data: PlaneData::from_slice(&[ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 2, 3, 4, 0, 0, - 0, 0, 8, 7, 6, 5, 0, 0, - 0, 0, 9, 8, 7, 6, 0, 0, - 0, 0, 2, 3, 4, 5, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - ]), - cfg: PlaneConfig { - stride: 8, - alloc_height: 9, - width: 4, - height: 4, - xdec: 0, - ydec: 0, - xpad: 0, - ypad: 0, - xorigin: 2, - yorigin: 3, - }, - }; + let plane = Plane:: { + data: PlaneData::from_slice(&[ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 0, 0, + 0, 0, 8, 7, 6, 5, 0, 0, + 0, 0, 9, 8, 7, 6, 0, 0, + 0, 0, 2, 3, 4, 5, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + ]), + cfg: PlaneConfig { + stride: 8, + alloc_height: 9, + width: 4, + height: 4, + xdec: 0, + ydec: 0, + xpad: 0, + ypad: 0, + xorigin: 2, + yorigin: 3, + }, + }; let downsampled = plane.downsampled(4, 4); #[rustfmt::skip] - let expected = &[ - 5, 5, - 6, 6, - ]; + let expected = &[ + 5, 5, + 6, 6, + ]; let v: Vec<_> = downsampled.iter().collect(); @@ -997,38 +997,38 @@ pub mod test { #[test] fn test_plane_downsample_odd() { #[rustfmt::skip] - let plane = Plane:: { - data: PlaneData::from_slice(&[ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 2, 3, 4, 0, 0, - 0, 0, 8, 7, 6, 5, 0, 0, - 0, 0, 9, 8, 7, 6, 0, 0, - 0, 0, 2, 3, 4, 5, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - ]), - cfg: PlaneConfig { - stride: 8, - alloc_height: 9, - width: 3, - height: 3, - xdec: 0, - ydec: 0, - xpad: 0, - ypad: 0, - xorigin: 2, - yorigin: 3, - }, - }; + let plane = Plane:: { + data: PlaneData::from_slice(&[ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 0, 0, + 0, 0, 8, 7, 6, 5, 0, 0, + 0, 0, 9, 8, 7, 6, 0, 0, + 0, 0, 2, 3, 4, 5, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + ]), + cfg: PlaneConfig { + stride: 8, + alloc_height: 9, + width: 3, + height: 3, + xdec: 0, + ydec: 0, + xpad: 0, + ypad: 0, + xorigin: 2, + yorigin: 3, + }, + }; let downsampled = plane.downsampled(3, 3); #[rustfmt::skip] - let expected = &[ - 5, 5, - 6, 6, - ]; + let expected = &[ + 5, 5, + 6, 6, + ]; let v: Vec<_> = downsampled.iter().collect(); assert_eq!(&expected[..], &v[..]); @@ -1038,38 +1038,38 @@ pub mod test { #[test] fn test_plane_downscale() { #[rustfmt::skip] - let plane = Plane:: { - data: PlaneData::from_slice(&[ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 1, 4, 5, 0, 0, - 0, 0, 2, 3, 6, 7, 0, 0, - 0, 0, 8, 9, 7, 5, 0, 0, - 0, 0, 9, 8, 3, 1, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - ]), - cfg: PlaneConfig { - stride: 8, - alloc_height: 9, - width: 4, - height: 4, - xdec: 0, - ydec: 0, - xpad: 0, - ypad: 0, - xorigin: 2, - yorigin: 3, - }, - }; + let plane = Plane:: { + data: PlaneData::from_slice(&[ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 4, 5, 0, 0, + 0, 0, 2, 3, 6, 7, 0, 0, + 0, 0, 8, 9, 7, 5, 0, 0, + 0, 0, 9, 8, 3, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + ]), + cfg: PlaneConfig { + stride: 8, + alloc_height: 9, + width: 4, + height: 4, + xdec: 0, + ydec: 0, + xpad: 0, + ypad: 0, + xorigin: 2, + yorigin: 3, + }, + }; let downscaled = plane.downscale::<2>(); #[rustfmt::skip] - let expected = &[ - 2, 6, - 9, 4 - ]; + let expected = &[ + 2, 6, + 9, 4 + ]; let v: Vec<_> = downscaled.iter().collect(); assert_eq!(&expected[..], &v[..]); @@ -1079,37 +1079,37 @@ pub mod test { #[test] fn test_plane_downscale_odd() { #[rustfmt::skip] - let plane = Plane:: { - data: PlaneData::from_slice(&[ - 1, 2, 3, 4, 1, 2, 3, 4, - 0, 0, 8, 7, 6, 5, 8, 7, - 6, 5, 8, 7, 6, 5, 8, 7, - 6, 5, 8, 7, 0, 0, 2, 3, - 4, 5, 0, 0, 9, 8, 7, 6, - 0, 0, 0, 0, 2, 3, 4, 5, - 0, 0, 0, 0, 2, 3, 4, 5, - ]), - cfg: PlaneConfig { - stride: 8, - alloc_height: 7, - width: 8, - height: 7, - xdec: 0, - ydec: 0, - xpad: 0, - ypad: 0, - xorigin: 0, - yorigin: 0, - }, - }; + let plane = Plane:: { + data: PlaneData::from_slice(&[ + 1, 2, 3, 4, 1, 2, 3, 4, + 0, 0, 8, 7, 6, 5, 8, 7, + 6, 5, 8, 7, 6, 5, 8, 7, + 6, 5, 8, 7, 0, 0, 2, 3, + 4, 5, 0, 0, 9, 8, 7, 6, + 0, 0, 0, 0, 2, 3, 4, 5, + 0, 0, 0, 0, 2, 3, 4, 5, + ]), + cfg: PlaneConfig { + stride: 8, + alloc_height: 7, + width: 8, + height: 7, + xdec: 0, + ydec: 0, + xpad: 0, + ypad: 0, + xorigin: 0, + yorigin: 0, + }, + }; let downscaled = plane.downscale::<3>(); #[rustfmt::skip] - let expected = &[ - 4, 5, - 3, 3 - ]; + let expected = &[ + 4, 5, + 3, 3 + ]; let v: Vec<_> = downscaled.iter().collect(); assert_eq!(&expected[..], &v[..]); @@ -1119,40 +1119,40 @@ pub mod test { #[test] fn test_plane_downscale_odd_2() { #[rustfmt::skip] - let plane = Plane:: { - data: PlaneData::from_slice(&[ - 9, 8, 3, 1, 0, 1, 4, 5, 0, 0, - 0, 1, 4, 5, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, - 0, 2, 3, 6, 7, 0, 0, 0, 0, 0, - 0, 0, 8, 9, 7, 5, 0, 0, 0, 0, - 9, 8, 3, 1, 0, 1, 4, 5, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 2, 3, 6, 7, 0, - 0, 0, 0, 0, 0, 0, 8, 9, 7, 5, - 0, 0, 0, 0, 9, 8, 3, 1, 0, 0 - ]), - cfg: PlaneConfig { - stride: 10, - alloc_height: 10, - width: 10, - height: 10, - xdec: 0, - ydec: 0, - xpad: 0, - ypad: 0, - xorigin: 0, - yorigin: 0, - }, - }; + let plane = Plane:: { + data: PlaneData::from_slice(&[ + 9, 8, 3, 1, 0, 1, 4, 5, 0, 0, + 0, 1, 4, 5, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, + 0, 2, 3, 6, 7, 0, 0, 0, 0, 0, + 0, 0, 8, 9, 7, 5, 0, 0, 0, 0, + 9, 8, 3, 1, 0, 1, 4, 5, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 2, 3, 6, 7, 0, + 0, 0, 0, 0, 0, 0, 8, 9, 7, 5, + 0, 0, 0, 0, 9, 8, 3, 1, 0, 0 + ]), + cfg: PlaneConfig { + stride: 10, + alloc_height: 10, + width: 10, + height: 10, + xdec: 0, + ydec: 0, + xpad: 0, + ypad: 0, + xorigin: 0, + yorigin: 0, + }, + }; let downscaled = plane.downscale::<3>(); #[rustfmt::skip] - let expected = &[ - 3, 1, 2, - 4, 4, 1, - 0, 0, 4, - ]; + let expected = &[ + 3, 1, 2, + 4, 4, 1, + 0, 0, 4, + ]; let v: Vec<_> = downscaled.iter().collect(); assert_eq!(&expected[..], &v[..]); @@ -1162,79 +1162,76 @@ pub mod test { #[test] fn test_plane_pad() { #[rustfmt::skip] - let mut plane = Plane:: { - data: PlaneData::from_slice(&[ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 2, 3, 4, 0, 0, - 0, 0, 8, 7, 6, 5, 0, 0, - 0, 0, 9, 8, 7, 6, 0, 0, - 0, 0, 2, 3, 4, 5, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - ]), - cfg: PlaneConfig { - stride: 8, - alloc_height: 9, - width: 4, - height: 4, - xdec: 0, - ydec: 0, - xpad: 0, - ypad: 0, - xorigin: 2, - yorigin: 3, - }, - }; + let mut plane = Plane:: { + data: PlaneData::from_slice(&[ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 0, 0, + 0, 0, 8, 7, 6, 5, 0, 0, + 0, 0, 9, 8, 7, 6, 0, 0, + 0, 0, 2, 3, 4, 5, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + ]), + cfg: PlaneConfig { + stride: 8, + alloc_height: 9, + width: 4, + height: 4, + xdec: 0, + ydec: 0, + xpad: 0, + ypad: 0, + xorigin: 2, + yorigin: 3, + }, + }; plane.pad(4, 4); #[rustfmt::skip] - assert_eq!( - &[ - 1, 1, 1, 2, 3, 4, 4, 4, - 1, 1, 1, 2, 3, 4, 4, 4, - 1, 1, 1, 2, 3, 4, 4, 4, - 1, 1, 1, 2, 3, 4, 4, 4, - 8, 8, 8, 7, 6, 5, 5, 5, - 9, 9, 9, 8, 7, 6, 6, 6, - 2, 2, 2, 3, 4, 5, 5, 5, - 2, 2, 2, 3, 4, 5, 5, 5, - 2, 2, 2, 3, 4, 5, 5, 5, - ][..], - &plane.data[..] - ); + assert_eq!(&[ + 1, 1, 1, 2, 3, 4, 4, 4, + 1, 1, 1, 2, 3, 4, 4, 4, + 1, 1, 1, 2, 3, 4, 4, 4, + 1, 1, 1, 2, 3, 4, 4, 4, + 8, 8, 8, 7, 6, 5, 5, 5, + 9, 9, 9, 8, 7, 6, 6, 6, + 2, 2, 2, 3, 4, 5, 5, 5, + 2, 2, 2, 3, 4, 5, 5, 5, + 2, 2, 2, 3, 4, 5, 5, 5, + ], &plane.data[..]); } #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)] #[test] fn test_pixel_iterator() { #[rustfmt::skip] - let plane = Plane:: { - data: PlaneData::from_slice(&[ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 2, 3, 4, 0, 0, - 0, 0, 8, 7, 6, 5, 0, 0, - 0, 0, 9, 8, 7, 6, 0, 0, - 0, 0, 2, 3, 4, 5, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - ]), - cfg: PlaneConfig { - stride: 8, - alloc_height: 9, - width: 4, - height: 4, - xdec: 0, - ydec: 0, - xpad: 0, - ypad: 0, - xorigin: 2, - yorigin: 3, - }, - }; + let plane = Plane:: { + data: PlaneData::from_slice(&[ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 0, 0, + 0, 0, 8, 7, 6, 5, 0, 0, + 0, 0, 9, 8, 7, 6, 0, 0, + 0, 0, 2, 3, 4, 5, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + ]), + cfg: PlaneConfig { + stride: 8, + alloc_height: 9, + width: 4, + height: 4, + xdec: 0, + ydec: 0, + xpad: 0, + ypad: 0, + xorigin: 2, + yorigin: 3, + }, + }; let pixels: Vec = plane.iter().collect();