use std::error::Error;
use std::fmt;
#[derive(Debug)]
pub enum SphericalCowError {
NegativeRadius,
NegativeExtents,
Uncontained,
NoneSetF,
NoneFront,
}
impl fmt::Display for SphericalCowError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SphericalCowError::NegativeRadius => write!(f, "Supplied radius is negative."),
SphericalCowError::NegativeExtents => write!(f, "A supplied half extent is negative."),
SphericalCowError::Uncontained => {
write!(f, "Sphere is not contained within bounding geometry.")
}
SphericalCowError::NoneSetF => {
write!(f, "Returned none when choosing value from set f.")
}
SphericalCowError::NoneFront => {
write!(f, "Returned none when choosing value from front.")
}
}
}
}
impl Error for SphericalCowError {}
#[test]
fn error_display_negative_radius() {
use crate::shapes::Sphere;
use nalgebra::Point3;
let err = Sphere::new(Point3::origin(), -1.).unwrap_err();
assert_eq!(format!("{}", err), format!("Supplied radius is negative."));
}
#[test]
fn error_display_negative_extent() {
use crate::shapes::Cuboid;
let err = Cuboid::new(1., 1., -1.).unwrap_err();
assert_eq!(
format!("{}", err),
format!("A supplied half extent is negative.")
);
}
#[test]
fn error_display_containment() {
use crate::init_spheres;
use crate::shapes::Sphere;
use nalgebra::Point3;
let container = Sphere::new(Point3::origin(), 0.1).unwrap();
let err = init_spheres(&[10., 15., 20.], &container).unwrap_err();
assert_eq!(
format!("{}", err),
format!("Sphere is not contained within bounding geometry.")
);
}
#[test]
fn error_display_empty_values() {
use crate::shapes::Sphere;
use rand::prelude::SliceRandom;
use rand::thread_rng;
let empty_vec: Vec<Sphere> = Vec::new();
let mut rng = thread_rng();
let mut err = empty_vec
.choose(&mut rng)
.ok_or(SphericalCowError::NoneSetF)
.unwrap_err();
assert_eq!(
format!("{}", err),
format!("Returned none when choosing value from set f.")
);
err = empty_vec
.choose(&mut rng)
.ok_or(SphericalCowError::NoneFront)
.unwrap_err();
assert_eq!(
format!("{}", err),
format!("Returned none when choosing value from front.")
);
}