Skip to content

Commit

Permalink
feat(hdrimage/error): add HdrImage struct with unit tests
Browse files Browse the repository at this point in the history
New lib dependency added to project
`thiserror`(https://crates.io/crates/thiserror)
Reason: provides a convenient derive macro for
the standard library's `std::error::Error` trait

For now a custom enum error `HdrImageErr` has been defined
inside `error` mod, with only one variant `OutOfBounds`
  • Loading branch information
andros21 committed Mar 11, 2022
1 parent 2153802 commit 532b641
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
thiserror = "1.0.30"
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum HdrImageErr {
#[error("(HdrImage) invalid indexes: {0:?}, hdr image shape: {1:?}")]
OutOfBounds((usize, usize), (usize, usize)),
}
118 changes: 118 additions & 0 deletions src/hdrimage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use std::vec::Vec;

use crate::color::Color;
use crate::error::HdrImageErr;

#[derive(Clone, PartialEq)]
pub struct HdrImage {
width: usize,
height: usize,
pixels: Vec<Color>,
}

impl From<(usize, usize)> for HdrImage {
fn from(shape: (usize, usize)) -> Self {
Self {
width: shape.0,
height: shape.1,
pixels: vec![Color::from(()); shape.0 * shape.1],
}
}
}

impl HdrImage {
fn pixel_offset(&self, x: usize, y: usize) -> usize {
y * self.width + x
}

fn valid_coordinates(&self, x: usize, y: usize) -> bool {
return (x < self.width) & (y < self.height);
}

pub fn get_pixel(&self, x: usize, y: usize) -> Result<Color, HdrImageErr> {
if self.valid_coordinates(x, y) {
Ok(self.pixels[self.pixel_offset(x, y)])
} else {
Err(HdrImageErr::OutOfBounds((x, y), (self.width, self.height)))
}
}

pub fn set_pixel(&mut self, x: usize, y: usize, new_color: Color) -> Result<(), HdrImageErr> {
if self.valid_coordinates(x, y) {
let pixel_offset = self.pixel_offset(x, y);
Ok(self.pixels[pixel_offset] = new_color)
} else {
Err(HdrImageErr::OutOfBounds((x, y), (self.width, self.height)))
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn validate_coordinates_0() {
assert!(HdrImage::from((3, 3)).valid_coordinates(0, 0))
}

#[test]
fn validate_coordinates_1() {
assert!(HdrImage::from((3, 3)).valid_coordinates(1, 2))
}

#[test]
fn validate_coordinates_2() {
assert!(HdrImage::from((3, 3)).valid_coordinates(2, 1))
}

#[test]
fn neg_validate_coordinates_0() {
assert!(!HdrImage::from((3, 3)).valid_coordinates(3, 3))
}

#[test]
fn neg_validate_coordinates_1() {
assert!(!HdrImage::from((3, 3)).valid_coordinates(3, 1))
}

#[test]
fn neg_validate_coordinates_2() {
assert!(!HdrImage::from((3, 3)).valid_coordinates(1, 3))
}

#[test]
fn get_pixel_ok() {
assert_eq!(
HdrImage::from((3, 3)).get_pixel(0, 0).unwrap(),
Color::from(())
)
}

#[test]
fn get_pixel_err() {
assert_eq!(
HdrImage::from((3, 3)).get_pixel(3, 3).unwrap_err(),
HdrImageErr::OutOfBounds((3, 3), (3, 3))
)
}

#[test]
fn set_pixel_ok() {
let color = Color::from((1.0, 1.0, 1.0));
let mut hdrimage = HdrImage::from((3, 3));
assert_eq!(hdrimage.set_pixel(0, 0, color), Ok(()));
assert_eq!(hdrimage.get_pixel(0, 0).unwrap(), color)
}

#[test]
fn set_pixel_err() {
let color = Color::from((1.0, 1.0, 1.0));
let mut hdrimage = HdrImage::from((3, 3));
assert_eq!(
hdrimage.set_pixel(3, 3, color).unwrap_err(),
HdrImageErr::OutOfBounds((3, 3), (3, 3))
);
assert_ne!(hdrimage.get_pixel(0, 0).unwrap(), color)
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod color;
mod error;
mod hdrimage;

fn main() {
println!("Hello, World!")
Expand Down

0 comments on commit 532b641

Please sign in to comment.