Tests and benches

This commit is contained in:
Andrew 2023-05-09 09:14:57 +12:00
parent a0e979d009
commit 5e052e5f62
8 changed files with 137 additions and 41 deletions

View file

@ -158,7 +158,7 @@ fn reductions_rgba_to_grayscale_alpha_16(b: &mut Bencher) {
));
let png = PngData::new(&input, false).unwrap();
b.iter(|| color::reduce_rgb_to_grayscale(&png.raw));
b.iter(|| color::reduced_rgb_to_grayscale(&png.raw));
}
#[bench]
@ -168,7 +168,7 @@ fn reductions_rgba_to_grayscale_alpha_8(b: &mut Bencher) {
));
let png = PngData::new(&input, false).unwrap();
b.iter(|| color::reduce_rgb_to_grayscale(&png.raw));
b.iter(|| color::reduced_rgb_to_grayscale(&png.raw));
}
#[bench]
@ -179,7 +179,7 @@ fn reductions_rgba_to_grayscale_16(b: &mut Bencher) {
let png = PngData::new(&input, false).unwrap();
b.iter(|| {
color::reduce_rgb_to_grayscale(&png.raw)
color::reduced_rgb_to_grayscale(&png.raw)
.and_then(|r| alpha::reduced_alpha_channel(&r, false))
});
}
@ -192,7 +192,7 @@ fn reductions_rgba_to_grayscale_8(b: &mut Bencher) {
let png = PngData::new(&input, false).unwrap();
b.iter(|| {
color::reduce_rgb_to_grayscale(&png.raw)
color::reduced_rgb_to_grayscale(&png.raw)
.and_then(|r| alpha::reduced_alpha_channel(&r, false))
});
}
@ -204,7 +204,7 @@ fn reductions_rgb_to_grayscale_16(b: &mut Bencher) {
));
let png = PngData::new(&input, false).unwrap();
b.iter(|| color::reduce_rgb_to_grayscale(&png.raw));
b.iter(|| color::reduced_rgb_to_grayscale(&png.raw));
}
#[bench]
@ -212,7 +212,7 @@ fn reductions_rgb_to_grayscale_8(b: &mut Bencher) {
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_grayscale_8.png"));
let png = PngData::new(&input, false).unwrap();
b.iter(|| color::reduce_rgb_to_grayscale(&png.raw));
b.iter(|| color::reduced_rgb_to_grayscale(&png.raw));
}
#[bench]
@ -220,7 +220,7 @@ fn reductions_rgba_to_palette_8(b: &mut Bencher) {
let input = test::black_box(PathBuf::from("tests/files/rgba_8_should_be_palette_8.png"));
let png = PngData::new(&input, false).unwrap();
b.iter(|| color::reduce_to_palette(&png.raw));
b.iter(|| color::reduced_to_indexed(&png.raw));
}
#[bench]
@ -228,7 +228,27 @@ fn reductions_rgb_to_palette_8(b: &mut Bencher) {
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_palette_8.png"));
let png = PngData::new(&input, false).unwrap();
b.iter(|| color::reduce_to_palette(&png.raw));
b.iter(|| color::reduced_to_indexed(&png.raw));
}
#[bench]
fn reductions_grayscale_8_to_palette_8(b: &mut Bencher) {
let input = test::black_box(PathBuf::from(
"tests/files/grayscale_8_should_be_palette_8.png",
));
let png = PngData::new(&input, false).unwrap();
b.iter(|| color::reduced_to_indexed(&png.raw));
}
#[bench]
fn reductions_palette_8_to_grayscale_8(b: &mut Bencher) {
let input = test::black_box(PathBuf::from(
"tests/files/palette_8_should_be_grayscale_8.png",
));
let png = PngData::new(&input, false).unwrap();
b.iter(|| color::indexed_to_channels(&png.raw));
}
#[bench]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 195 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View file

@ -427,8 +427,8 @@ fn interlacing_0_to_1_small_files() {
};
assert_eq!(png.raw.ihdr.interlaced, Interlacing::Adam7);
assert_eq!(png.raw.ihdr.color_type.png_header_code(), INDEXED);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::One);
assert_eq!(png.raw.ihdr.color_type.png_header_code(), RGB);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight);
remove_file(output).ok();
}
@ -461,8 +461,8 @@ fn interlacing_1_to_0_small_files() {
};
assert_eq!(png.raw.ihdr.interlaced, Interlacing::None);
assert_eq!(png.raw.ihdr.color_type.png_header_code(), INDEXED);
// the depth can't be asserted reliably, because on such small file different zlib implementations pick different depth as the best
assert_eq!(png.raw.ihdr.color_type.png_header_code(), RGB);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight);
remove_file(output).ok();
}

View file

@ -546,6 +546,17 @@ fn interlaced_palette_1_should_be_palette_1() {
);
}
#[test]
fn interlaced_palette_8_should_be_grayscale_8() {
test_it_converts(
"tests/files/interlaced_palette_8_should_be_grayscale_8.png",
INDEXED,
BitDepth::Eight,
GRAYSCALE,
BitDepth::Eight,
);
}
#[test]
fn interlaced_grayscale_alpha_16_should_be_grayscale_alpha_16() {
test_it_converts(
@ -651,8 +662,8 @@ fn interlaced_small_files() {
"tests/files/interlaced_small_files.png",
INDEXED,
BitDepth::Eight,
INDEXED,
BitDepth::One,
RGB,
BitDepth::Eight,
);
}

View file

@ -556,6 +556,42 @@ fn palette_4_should_be_palette_2() {
);
}
#[test]
fn palette_8_should_be_grayscale_8() {
test_it_converts(
"tests/files/palette_8_should_be_grayscale_8.png",
false,
INDEXED,
BitDepth::Eight,
GRAYSCALE,
BitDepth::Eight,
);
}
#[test]
fn palette_8_should_be_rgb() {
test_it_converts(
"tests/files/palette_8_should_be_rgb.png",
false,
INDEXED,
BitDepth::Eight,
RGB,
BitDepth::Eight,
);
}
#[test]
fn palette_8_should_be_rgba() {
test_it_converts(
"tests/files/palette_8_should_be_rgba.png",
false,
INDEXED,
BitDepth::Eight,
RGBA,
BitDepth::Eight,
);
}
#[test]
fn palette_2_should_be_palette_2() {
test_it_converts(
@ -808,6 +844,54 @@ fn grayscale_2_should_be_grayscale_1() {
);
}
#[test]
fn grayscale_8_should_be_palette_8() {
test_it_converts(
"tests/files/grayscale_8_should_be_palette_8.png",
false,
GRAYSCALE,
BitDepth::Eight,
INDEXED,
BitDepth::Eight,
);
}
#[test]
fn grayscale_8_should_be_palette_4() {
test_it_converts(
"tests/files/grayscale_8_should_be_palette_4.png",
false,
GRAYSCALE,
BitDepth::Eight,
INDEXED,
BitDepth::Four,
);
}
#[test]
fn grayscale_8_should_be_palette_2() {
test_it_converts(
"tests/files/grayscale_8_should_be_palette_2.png",
false,
GRAYSCALE,
BitDepth::Eight,
INDEXED,
BitDepth::Two,
);
}
#[test]
fn grayscale_8_should_be_palette_1() {
test_it_converts(
"tests/files/grayscale_8_should_be_palette_1.png",
false,
GRAYSCALE,
BitDepth::Eight,
INDEXED,
BitDepth::One,
);
}
#[test]
fn grayscale_alpha_16_should_be_grayscale_trns_16() {
test_it_converts(
@ -834,33 +918,14 @@ fn grayscale_alpha_8_should_be_grayscale_trns_8() {
#[test]
fn small_files() {
let input = PathBuf::from("tests/files/small_files.png");
let (output, opts) = get_opts(&input);
let png = PngData::new(&input, opts.fix_errors).unwrap();
assert_eq!(png.raw.ihdr.color_type.png_header_code(), INDEXED);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight);
match oxipng::optimize(&InFile::Path(input), &output, &opts) {
Ok(_) => (),
Err(x) => panic!("{}", x),
};
let output = output.path().unwrap();
assert!(output.exists());
let png = match PngData::new(output, opts.fix_errors) {
Ok(x) => x,
Err(x) => {
remove_file(output).ok();
panic!("{}", x)
}
};
assert_eq!(png.raw.ihdr.color_type.png_header_code(), INDEXED);
// depth varies depending on zlib implementation used
remove_file(output).ok();
test_it_converts(
"tests/files/small_files.png",
false,
INDEXED,
BitDepth::Eight,
RGB,
BitDepth::Eight,
);
}
#[test]