Skip to content

Commit 1072d39

Browse files
committed
Add unit tests for the IDCT function
This adds unit tests with various inputs for the IDCT function. This should make it easier to then optimize the function while making sure we are not altering its functionality.
1 parent 1e54da1 commit 1072d39

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/idct.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,74 @@ fn stbi_f2f(x: f32) -> i32 {
307307
fn stbi_fsh(x: i32) -> i32 {
308308
x << 12
309309
}
310+
311+
#[test]
312+
fn test_dequantize_and_idct_block_8x8() {
313+
let coefficients: [i16; 8 * 8] = [
314+
-14, -39, 58, -2, 3, 3, 0, 1,
315+
11, 27, 4, -3, 3, 0, 1, 0,
316+
-6, -13, -9, -1, -2, -1, 0, 0,
317+
-4, 0, -1, -2, 0, 0, 0, 0,
318+
3, 0, 0, 0, 0, 0, 0, 0,
319+
-3, -2, 0, 0, 0, 0, 0, 0,
320+
0, 0, 0, 0, 0, 0, 0, 0,
321+
0, 0, 0, 0, 0, 0, 0, 0];
322+
323+
let quantization_table: [u16; 8 * 8] = [
324+
8, 6, 5, 8, 12, 20, 26, 31,
325+
6, 6, 7, 10, 13, 29, 30, 28,
326+
7, 7, 8, 12, 20, 29, 35, 28,
327+
7, 9, 11, 15, 26, 44, 40, 31,
328+
9, 11, 19, 28, 34, 55, 52, 39,
329+
12, 18, 28, 32, 41, 52, 57, 46,
330+
25, 32, 39, 44, 52, 61, 60, 51,
331+
36, 46, 48, 49, 56, 50, 52, 50];
332+
let output_linestride: usize = 8;
333+
let mut output = [0u8; 8 * 8];
334+
dequantize_and_idct_block_8x8(
335+
&coefficients,
336+
&quantization_table,
337+
output_linestride,
338+
&mut output);
339+
let expected_output = [
340+
118, 92, 110, 83, 77, 93, 144, 198,
341+
172, 116, 114, 87, 78, 93, 146, 191,
342+
194, 107, 91, 76, 71, 93, 160, 198,
343+
196, 100, 80, 74, 67, 92, 174, 209,
344+
182, 104, 88, 81, 68, 89, 178, 206,
345+
105, 64, 59, 59, 63, 94, 183, 201,
346+
35, 27, 28, 37, 72, 121, 203, 204,
347+
37, 45, 41, 47, 98, 154, 223, 208];
348+
assert_eq!(&output[..], &expected_output[..]);
349+
}
350+
351+
#[test]
352+
fn test_dequantize_and_idct_block_8x8_all_zero() {
353+
let mut output = [0u8; 8 * 8];
354+
dequantize_and_idct_block_8x8(
355+
&[0; 8*8],
356+
&[666; 8*8],
357+
8,
358+
&mut output);
359+
assert_eq!(&output[..], &[128; 8*8][..]);
360+
}
361+
362+
#[test]
363+
fn test_dequantize_and_idct_block_8x8_saturated() {
364+
let mut output = [0u8; 8 * 8];
365+
dequantize_and_idct_block_8x8(
366+
&[std::i16::MAX; 8*8],
367+
&[std::u16::MAX; 8*8],
368+
8,
369+
&mut output);
370+
let expected = [
371+
0, 0, 0, 255, 255, 0, 0, 255,
372+
0, 0, 215, 0, 0, 255, 255, 0,
373+
255, 255, 255, 255, 255, 0, 0, 255,
374+
0, 0, 255, 0, 255, 0, 255, 255,
375+
0, 0, 255, 255, 0, 255, 0, 0,
376+
255, 255, 0, 255, 255, 255, 170, 0,
377+
0, 255, 0, 0, 0, 0, 0, 255,
378+
255, 255, 0, 255, 0, 255, 0, 0];
379+
assert_eq!(&output[..], &expected[..]);
380+
}

0 commit comments

Comments
 (0)