ombre: Add byte encoding tests

2063f3d579c79683f53ae8ef0361a37a68924110
Alexis Sellier committed ago 1 parent 8bd2aac7
ombre/src/bytes/encoding.rs +91 -0
94 94
    fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> {
95 95
        reader.read_u32_be()
96 96
    }
97 97
}
98 98
99 +
impl Encode for u64 {
100 +
    fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
101 +
        writer.write_u64_be(*self)
102 +
    }
103 +
}
104 +
105 +
impl Decode for u64 {
106 +
    fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> {
107 +
        reader.read_u64_be()
108 +
    }
109 +
}
110 +
99 111
impl Encode for f32 {
100 112
    fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
101 113
        writer.write_f32_be(*self)
102 114
    }
103 115
}
106 118
    fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> {
107 119
        reader.read_f32_be()
108 120
    }
109 121
}
110 122
123 +
impl Decode for f64 {
124 +
    fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> {
125 +
        reader.read_f64_be()
126 +
    }
127 +
}
128 +
129 +
impl Encode for f64 {
130 +
    fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
131 +
        writer.write_f64_be(*self)
132 +
    }
133 +
}
134 +
111 135
impl Encode for crate::gfx::Rgba8 {
112 136
    fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
113 137
        writer.write_all(&[self.r, self.g, self.b, self.a])?;
114 138
        Ok(mem::size_of::<Self>())
115 139
    }
219 243
        let y: S = S::decode(reader)?;
220 244
221 245
        Ok(crate::math::Point2D::new(x, y))
222 246
    }
223 247
}
248 +
249 +
#[cfg(test)]
250 +
#[allow(clippy::unwrap_used)]
251 +
mod tests {
252 +
    use super::*;
253 +
    use crate::gfx::{Rgba, Rgba8};
254 +
    use crate::math::{Point2D, Vector2D, Vector3D, Vector4D};
255 +
256 +
    fn roundtrip<T: Encode + Decode + PartialEq + std::fmt::Debug>(value: T) {
257 +
        let encoded = serialize(&value).unwrap();
258 +
        let decoded: T = deserialize(&encoded).unwrap();
259 +
260 +
        assert_eq!(value, decoded);
261 +
    }
262 +
263 +
    #[test]
264 +
    fn test_primitive_roundtrip() {
265 +
        roundtrip(42u8);
266 +
        roundtrip(12345u16);
267 +
        roundtrip(1234567890u32);
268 +
        roundtrip(1234567890123456789u64);
269 +
        roundtrip(std::f32::consts::PI);
270 +
        roundtrip(std::f64::consts::PI);
271 +
    }
272 +
273 +
    #[test]
274 +
    fn test_color_roundtrip() {
275 +
        roundtrip(Rgba8::new(255, 128, 64, 192));
276 +
        roundtrip(Rgba::new(1.0f32, 0.5, 0.25, 0.75));
277 +
    }
278 +
279 +
    #[test]
280 +
    fn test_vector_roundtrip() {
281 +
        roundtrip(Vector2D::new(1.0f32, 2.0));
282 +
        roundtrip(Vector3D::new(1.0f32, 2.0, 3.0));
283 +
        roundtrip(Vector4D::new(1.0f32, 2.0, 3.0, 4.0));
284 +
    }
285 +
286 +
    #[test]
287 +
    fn test_point_roundtrip() {
288 +
        roundtrip(Point2D::new(1.0f32, 2.0));
289 +
    }
290 +
291 +
    #[test]
292 +
    fn test_array_roundtrip() {
293 +
        roundtrip([1u8, 2, 3, 4]);
294 +
    }
295 +
296 +
    #[test]
297 +
    fn test_slice() {
298 +
        let slice = &[1u8, 2, 3, 4];
299 +
        let bytes = serialize(slice).unwrap();
300 +
301 +
        assert_eq!(bytes.as_slice(), slice);
302 +
    }
303 +
304 +
    #[test]
305 +
    fn test_deserialize_error() {
306 +
        // Test decoding with insufficient data
307 +
        let result: Result<u32, _> = deserialize(&[1, 2, 3]);
308 +
        assert!(result.is_err());
309 +
310 +
        // Test decoding with extra data
311 +
        let result: Result<u8, _> = deserialize(&[1, 2]);
312 +
        assert!(result.is_err());
313 +
    }
314 +
}