From a570dc384279bf9b38ff0cc9d4635c2081aa3147 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 6 Mar 2021 11:21:41 +0530 Subject: add image manip apis --- tests/image.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/image.rs (limited to 'tests') diff --git a/tests/image.rs b/tests/image.rs new file mode 100644 index 0000000..2006a3b --- /dev/null +++ b/tests/image.rs @@ -0,0 +1,49 @@ +use obi::Image; + +#[test] +fn image_indexing() { + let (width, height) = (30, 50); + let img = Image::new(width, height); + for x in 0..width { + for y in 0..height { + assert_eq!((y * width + x) as usize, img.index(x, y).unwrap()); + } + } +} + +#[test] +#[should_panic] +fn x_value_out_of_bounds() { + let img = Image::new(30, 50); + img.index(30, 0).unwrap(); +} + +#[test] +#[should_panic] +fn y_value_out_of_bounds() { + let img = Image::new(30, 50); + img.index(0, 50).unwrap(); +} + +#[test] +fn pixel_setter_and_getters() { + let mut img = Image::new(100, 200); + img.set(2, 3, true).unwrap(); + assert!(img.get(2, 3).unwrap()); + assert_eq!(1, img.data.iter().filter(|&&x| x).count()); +} + +#[test] +fn flip_bits() { + let (width, height) = (100, 200); + let mut img = Image::new(width, height); + img.set(2, 3, true).expect("Indexing error"); + for i in 0..width { + for j in 0..height { + img.flip(i, j).unwrap(); + } + } + let mut expected = Image::new_with(width, height, true); + expected.set(2, 3, false).expect("Indexing error"); + assert_eq!(expected.data, img.data); +} -- cgit v1.2.3