Skip to content

Commit

Permalink
add test to check for regressions in the frame iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiOnGit committed May 5, 2024
1 parent be6a3d8 commit ca0dc9d
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/regression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{fs::File, io::BufReader, path::PathBuf};
use std::{
fs::{self, File},
io::{BufReader, Cursor},
path::PathBuf,
};

use image::{codecs::webp::WebPDecoder, AnimationDecoder};

const BASE_PATH: [&str; 2] = [".", "tests"];
const IMAGE_DIR: &str = "images";
Expand Down Expand Up @@ -37,6 +43,29 @@ fn check_regressions() {
let _ = image::open(path);
})
}
/// Check that the WEBP frames iterator returns the right amount of frames.
#[test]
fn check_webp_frames_regressions() {
let path: PathBuf = BASE_PATH
.iter()
.collect::<PathBuf>()
.join(IMAGE_DIR)
.join("webp/extended_images")
.join("*.webp");
let pattern = &*format!("{}", path.display());
for path in glob::glob(pattern).unwrap().filter_map(Result::ok) {
let bytes = fs::read(path).unwrap();
let cursor = Cursor::new(&bytes);
let frame_count = image_webp::WebPDecoder::new(cursor.clone())
.unwrap()
.num_frames() as usize;
let decoder = WebPDecoder::new(cursor).unwrap();
// The `take` guards against a potentially infinitely running iterator.
// Since we take `frame_count + 1`, we can assume that the last iteration already returns `None`.
let actual_frame_count = decoder.into_frames().take(frame_count + 1).count();
assert_eq!(actual_frame_count, frame_count);
}
}

/// Check that BMP files with large values could cause OOM issues are rejected.
///
Expand Down

0 comments on commit ca0dc9d

Please sign in to comment.