Skip to content

Commit ce6013e

Browse files
committed
Add some encoder tests
Signed-off-by: Marc-André Lureau <[email protected]>
1 parent c53fa13 commit ce6013e

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

tests/test_misc.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,81 @@
1+
use qoi::{decode_to_vec, Channels, Error, RawChannels};
2+
13
#[test]
24
fn test_new_decoder() {
35
// this used to fail due to `Bytes` not being `pub`
46
let arr = [0u8];
57
let _ = qoi::Decoder::new(&arr[..]);
68
}
9+
10+
#[test]
11+
fn test_new_encoder() {
12+
let arr3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; // 2 * 2 * 3
13+
let arr4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; // 2 * 2 * 4
14+
15+
let enc = qoi::Encoder::new(&arr3, 2, 2).unwrap();
16+
assert_eq!(enc.channels(), Channels::Rgb);
17+
18+
let enc = qoi::Encoder::new(&arr4, 2, 2).unwrap();
19+
assert_eq!(enc.channels(), Channels::Rgba);
20+
21+
assert!(matches!(
22+
qoi::Encoder::new(&arr3, 3, 3),
23+
Err(Error::InvalidImageLength { size: 12, width: 3, height: 3 })
24+
));
25+
26+
assert!(matches!(qoi::Encoder::new(&arr3, 1, 1), Err(Error::InvalidChannels { channels: 12 })));
27+
28+
let enc = qoi::Encoder::new_raw(&arr3, 2, 2, 2 * 3, RawChannels::Bgr).unwrap();
29+
assert_eq!(enc.channels(), Channels::Rgb);
30+
let qoi = enc.encode_to_vec().unwrap();
31+
let (_header, res) = decode_to_vec(qoi).unwrap();
32+
assert_eq!(res, [2, 1, 0, 5, 4, 3, 8, 7, 6, 11, 10, 9]);
33+
34+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Rgba).unwrap();
35+
assert_eq!(enc.channels(), Channels::Rgba);
36+
let qoi = enc.encode_to_vec().unwrap();
37+
let (_header, res) = decode_to_vec(qoi).unwrap();
38+
assert_eq!(res, arr4);
39+
40+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgra).unwrap();
41+
assert_eq!(enc.channels(), Channels::Rgba);
42+
let qoi = enc.encode_to_vec().unwrap();
43+
let (_header, res) = decode_to_vec(qoi).unwrap();
44+
assert_eq!(res, [2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15]);
45+
46+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Rgbx).unwrap();
47+
assert_eq!(enc.channels(), Channels::Rgb);
48+
let qoi = enc.encode_to_vec().unwrap();
49+
let (_header, res) = decode_to_vec(qoi).unwrap();
50+
assert_eq!(res, [0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14]);
51+
52+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Xrgb).unwrap();
53+
assert_eq!(enc.channels(), Channels::Rgb);
54+
let qoi = enc.encode_to_vec().unwrap();
55+
let (_header, res) = decode_to_vec(qoi).unwrap();
56+
assert_eq!(res, [1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15]);
57+
58+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgra).unwrap();
59+
assert_eq!(enc.channels(), Channels::Rgba);
60+
let qoi = enc.encode_to_vec().unwrap();
61+
let (_header, res) = decode_to_vec(qoi).unwrap();
62+
assert_eq!(res, [2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15]);
63+
64+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Abgr).unwrap();
65+
assert_eq!(enc.channels(), Channels::Rgba);
66+
let qoi = enc.encode_to_vec().unwrap();
67+
let (_header, res) = decode_to_vec(qoi).unwrap();
68+
assert_eq!(res, [3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12]);
69+
70+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgrx).unwrap();
71+
assert_eq!(enc.channels(), Channels::Rgb);
72+
let qoi = enc.encode_to_vec().unwrap();
73+
let (_header, res) = decode_to_vec(qoi).unwrap();
74+
assert_eq!(res, [2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12]);
75+
76+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Xbgr).unwrap();
77+
assert_eq!(enc.channels(), Channels::Rgb);
78+
let qoi = enc.encode_to_vec().unwrap();
79+
let (_header, res) = decode_to_vec(qoi).unwrap();
80+
assert_eq!(res, [3, 2, 1, 7, 6, 5, 11, 10, 9, 15, 14, 13]);
81+
}

0 commit comments

Comments
 (0)