ombre: Add `Image` encoding tests
6dc4b2973a90247f7cb116763db7034f6add1653
1 parent
2063f3d5
ombre/src/gfx/image.rs
+33 -0
| 37 | 37 | /// | .... | |
|
| 38 | 38 | /// '---------' |
|
| 39 | 39 | /// |
|
| 40 | 40 | #[derive(Clone)] |
|
| 41 | 41 | pub struct Image { |
|
| 42 | + | /// Image size in pixels. |
|
| 42 | 43 | pub size: Size<u32>, |
|
| 44 | + | /// Image pixel buffer. |
|
| 43 | 45 | pub pixels: Arc<[Rgba8]>, |
|
| 44 | 46 | } |
|
| 45 | 47 | ||
| 46 | 48 | impl std::fmt::Debug for Image { |
|
| 47 | 49 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 167 | 169 | let image = Image::decode(&mut cursor)?; |
|
| 168 | 170 | ||
| 169 | 171 | Ok(image) |
|
| 170 | 172 | } |
|
| 171 | 173 | } |
|
| 174 | + | ||
| 175 | + | #[cfg(test)] |
|
| 176 | + | #[allow(clippy::unwrap_used)] |
|
| 177 | + | mod tests { |
|
| 178 | + | use super::*; |
|
| 179 | + | use crate::bytes::encoding::{deserialize, serialize}; |
|
| 180 | + | ||
| 181 | + | #[test] |
|
| 182 | + | fn test_image_roundtrip() { |
|
| 183 | + | let original = Image::new( |
|
| 184 | + | vec![Rgba8::RED, Rgba8::GREEN, Rgba8::BLUE, Rgba8::WHITE], |
|
| 185 | + | Size::<u32>::new(2, 2), |
|
| 186 | + | ); |
|
| 187 | + | ||
| 188 | + | let encoded = serialize(&original).unwrap(); |
|
| 189 | + | let decoded: Image = deserialize(&encoded).unwrap(); |
|
| 190 | + | ||
| 191 | + | assert_eq!(original, decoded); |
|
| 192 | + | assert_eq!(original.size, decoded.size); |
|
| 193 | + | assert_eq!(original.pixels.len(), decoded.pixels.len()); |
|
| 194 | + | } |
|
| 195 | + | ||
| 196 | + | #[test] |
|
| 197 | + | fn test_empty_image_roundtrip() { |
|
| 198 | + | let original = Image::empty(); |
|
| 199 | + | let encoded = serialize(&original).unwrap(); |
|
| 200 | + | let decoded: Image = deserialize(&encoded).unwrap(); |
|
| 201 | + | ||
| 202 | + | assert_eq!(original, decoded); |
|
| 203 | + | } |
|
| 204 | + | } |