Skip to content

Commit 9fc76e8

Browse files
committed
Add some encoder tests
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1 parent 018962c commit 9fc76e8

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

tests/test_misc.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use qoi::{
22
consts::{QOI_OP_RGB, QOI_OP_RUN},
3-
decode_to_vec, Channels, ColorSpace, Header, Result,
3+
decode_to_vec, Channels, ColorSpace, Error, Header, RawChannels, Result,
44
};
55

66
#[test]
@@ -28,3 +28,76 @@ fn test_big_endian() {
2828
// so we can see it in the CI logs
2929
assert_eq!(u16::to_be_bytes(1), [0, 1]);
3030
}
31+
32+
#[test]
33+
fn test_new_encoder() {
34+
let arr3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; // 2 * 2 * 3
35+
let arr4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; // 2 * 2 * 4
36+
37+
let enc = qoi::Encoder::new(&arr3, 2, 2).unwrap();
38+
assert_eq!(enc.channels(), Channels::Rgb);
39+
40+
let enc = qoi::Encoder::new(&arr4, 2, 2).unwrap();
41+
assert_eq!(enc.channels(), Channels::Rgba);
42+
43+
assert!(matches!(
44+
qoi::Encoder::new(&arr3, 3, 3),
45+
Err(Error::InvalidImageLength { size: 12, width: 3, height: 3 })
46+
));
47+
48+
assert!(matches!(qoi::Encoder::new(&arr3, 1, 1), Err(Error::InvalidChannels { channels: 12 })));
49+
50+
let enc = qoi::Encoder::new_raw(&arr3, 2, 2, 2 * 3, RawChannels::Bgr).unwrap();
51+
assert_eq!(enc.channels(), Channels::Rgb);
52+
let qoi = enc.encode_to_vec().unwrap();
53+
let (_header, res) = decode_to_vec(qoi).unwrap();
54+
assert_eq!(res, [2, 1, 0, 5, 4, 3, 8, 7, 6, 11, 10, 9]);
55+
56+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Rgba).unwrap();
57+
assert_eq!(enc.channels(), Channels::Rgba);
58+
let qoi = enc.encode_to_vec().unwrap();
59+
let (_header, res) = decode_to_vec(qoi).unwrap();
60+
assert_eq!(res, arr4);
61+
62+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgra).unwrap();
63+
assert_eq!(enc.channels(), Channels::Rgba);
64+
let qoi = enc.encode_to_vec().unwrap();
65+
let (_header, res) = decode_to_vec(qoi).unwrap();
66+
assert_eq!(res, [2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15]);
67+
68+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Rgbx).unwrap();
69+
assert_eq!(enc.channels(), Channels::Rgb);
70+
let qoi = enc.encode_to_vec().unwrap();
71+
let (_header, res) = decode_to_vec(qoi).unwrap();
72+
assert_eq!(res, [0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14]);
73+
74+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Xrgb).unwrap();
75+
assert_eq!(enc.channels(), Channels::Rgb);
76+
let qoi = enc.encode_to_vec().unwrap();
77+
let (_header, res) = decode_to_vec(qoi).unwrap();
78+
assert_eq!(res, [1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15]);
79+
80+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgra).unwrap();
81+
assert_eq!(enc.channels(), Channels::Rgba);
82+
let qoi = enc.encode_to_vec().unwrap();
83+
let (_header, res) = decode_to_vec(qoi).unwrap();
84+
assert_eq!(res, [2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15]);
85+
86+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Abgr).unwrap();
87+
assert_eq!(enc.channels(), Channels::Rgba);
88+
let qoi = enc.encode_to_vec().unwrap();
89+
let (_header, res) = decode_to_vec(qoi).unwrap();
90+
assert_eq!(res, [3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12]);
91+
92+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgrx).unwrap();
93+
assert_eq!(enc.channels(), Channels::Rgb);
94+
let qoi = enc.encode_to_vec().unwrap();
95+
let (_header, res) = decode_to_vec(qoi).unwrap();
96+
assert_eq!(res, [2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12]);
97+
98+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Xbgr).unwrap();
99+
assert_eq!(enc.channels(), Channels::Rgb);
100+
let qoi = enc.encode_to_vec().unwrap();
101+
let (_header, res) = decode_to_vec(qoi).unwrap();
102+
assert_eq!(res, [3, 2, 1, 7, 6, 5, 11, 10, 9, 15, 14, 13]);
103+
}

0 commit comments

Comments
 (0)