Skip to content

Commit

Permalink
test: Better tests, error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
acgetchell committed Sep 13, 2024
1 parent a51a6ef commit 6738259
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 64 deletions.
50 changes: 17 additions & 33 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use na::{ComplexField, Const, OPoint};
use nalgebra as na;
use peroxide::fuga::*;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{collections::HashMap, fmt::Debug, hash::Hash, iter::Sum, ops::Div};
use std::{collections::HashMap, fmt::Debug, hash::Hash, iter::Sum};
use uuid::Uuid;

#[derive(Builder, Clone, Debug, Default, Deserialize, Eq, Serialize)]
Expand Down Expand Up @@ -65,7 +65,7 @@ where
if self
.vertices
.as_ref()
.expect("Must create a Cell with Vertices")
.expect("Must create a Cell with Vertices!")
.len()
> D + 1
{
Expand All @@ -84,7 +84,6 @@ where
T: Clone + ComplexField<RealField = T> + Copy + Default + PartialEq + PartialOrd + Sum,
U: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
V: Clone + Copy + Eq + Hash + Ord + PartialEq + PartialOrd,
for<'a> &'a T: Div<f64>,
f64: From<T>,
[T; D]: Copy + Default + DeserializeOwned + Serialize + Sized,
{
Expand Down Expand Up @@ -858,21 +857,18 @@ mod tests {

#[test]
fn cell_circumcenter_fail() {
let point1 = Point::new([0.0, 0.0, 0.0]);
let point2 = Point::new([1.0, 0.0, 0.0]);
let point3 = Point::new([0.0, 1.0, 0.0]);
let vertex1 = VertexBuilder::default()
.point(point1)
.point(Point::new([0.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex2 = VertexBuilder::default()
.point(point2)
.point(Point::new([1.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex3 = VertexBuilder::default()
.point(point3)
.point(Point::new([0.0, 1.0, 0.0]))
.data(1)
.build()
.unwrap();
Expand All @@ -887,27 +883,23 @@ mod tests {

#[test]
fn cell_circumradius() {
let point1 = Point::new([0.0, 0.0, 0.0]);
let point2 = Point::new([1.0, 0.0, 0.0]);
let point3 = Point::new([0.0, 1.0, 0.0]);
let point4 = Point::new([0.0, 0.0, 1.0]);
let vertex1 = VertexBuilder::default()
.point(point1)
.point(Point::new([0.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex2 = VertexBuilder::default()
.point(point2)
.point(Point::new([1.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex3 = VertexBuilder::default()
.point(point3)
.point(Point::new([0.0, 1.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex4 = VertexBuilder::default()
.point(point4)
.point(Point::new([0.0, 0.0, 1.0]))
.data(2)
.build()
.unwrap();
Expand All @@ -926,27 +918,23 @@ mod tests {

#[test]
fn cell_circumsphere_contains() {
let point1 = Point::new([0.0, 0.0, 0.0]);
let point2 = Point::new([1.0, 0.0, 0.0]);
let point3 = Point::new([0.0, 1.0, 0.0]);
let point4 = Point::new([0.0, 0.0, 1.0]);
let vertex1 = VertexBuilder::default()
.point(point1)
.point(Point::new([0.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex2 = VertexBuilder::default()
.point(point2)
.point(Point::new([1.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex3 = VertexBuilder::default()
.point(point3)
.point(Point::new([0.0, 1.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex4 = VertexBuilder::default()
.point(point4)
.point(Point::new([0.0, 0.0, 1.0]))
.data(2)
.build()
.unwrap();
Expand All @@ -968,27 +956,23 @@ mod tests {

#[test]
fn cell_circumsphere_does_not_contain() {
let point1 = Point::new([0.0, 0.0, 0.0]);
let point2 = Point::new([1.0, 0.0, 0.0]);
let point3 = Point::new([0.0, 1.0, 0.0]);
let point4 = Point::new([0.0, 0.0, 1.0]);
let vertex1 = VertexBuilder::default()
.point(point1)
.point(Point::new([0.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex2 = VertexBuilder::default()
.point(point2)
.point(Point::new([1.0, 0.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex3 = VertexBuilder::default()
.point(point3)
.point(Point::new([0.0, 1.0, 0.0]))
.data(1)
.build()
.unwrap();
let vertex4 = VertexBuilder::default()
.point(point4)
.point(Point::new([0.0, 0.0, 1.0]))
.data(2)
.build()
.unwrap();
Expand Down
17 changes: 14 additions & 3 deletions src/delaunay_core/facet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use super::{cell::Cell, vertex::Vertex};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::hash::Hash;
use thiserror::Error;

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, PartialOrd, Serialize)]
/// The [Facet] struct represents a facet of a d-dimensional simplex.
Expand Down Expand Up @@ -72,13 +73,13 @@ where
/// let facet = Facet::new(cell.clone(), vertex1).unwrap();
/// assert_eq!(facet.cell, cell);
/// ```
pub fn new(cell: Cell<T, U, V, D>, vertex: Vertex<T, U, D>) -> Result<Self, &'static str> {
pub fn new(cell: Cell<T, U, V, D>, vertex: Vertex<T, U, D>) -> Result<Self, anyhow::Error> {
if !cell.vertices.contains(&vertex) {
return Err("The cell does not contain the vertex!");
return Err(FacetError::CellDoesNotContainVertex.into());
}

if cell.vertices.len() == 1 {
return Err("The cell is a 0-simplex with no facet!");
return Err(FacetError::CellIsZeroSimplex.into());
}

Ok(Facet { cell, vertex })
Expand All @@ -94,6 +95,16 @@ where
}
}

/// Error type for facet operations.
#[derive(Debug, Error)]
pub enum FacetError {
/// The cell does not contain the vertex.
#[error("The cell does not contain the vertex!")]
CellDoesNotContainVertex,
/// The cell is a 0-simplex with no facet.
#[error("The cell is a 0-simplex with no facet!")]
CellIsZeroSimplex,
}
#[cfg(test)]
mod tests {

Expand Down
2 changes: 1 addition & 1 deletion src/delaunay_core/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn invert(matrix: &Matrix) -> Result<Matrix, anyhow::Error> {
#[derive(Debug, Error)]
pub enum MatrixError {
/// Matrix is singular.
#[error("Matrix is singular")]
#[error("Matrix is singular!")]
SingularMatrix,
}

Expand Down
27 changes: 0 additions & 27 deletions src/delaunay_core/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,33 +207,6 @@ mod tests {
println!("{:?}", vertex);
}

// #[test]
// fn vertex_new() {
// let vertex: Vertex<f64, Option<()>, 3> = Vertex::new(Point::new([1.0, 2.0, 3.0]));

// assert_eq!(vertex.point.coords, [1.0, 2.0, 3.0]);
// assert_eq!(vertex.dim(), 3);
// assert!(vertex.incident_cell.is_none());
// assert!(vertex.data.is_none());

// // Human readable output for cargo test -- --nocapture
// println!("{:?}", vertex);
// }

// #[test]
// fn vertex_new_with_data() {
// let vertex = Vertex::new_with_data(Point::new([1.0, 2.0, 3.0, 4.0]), "4D");

// assert_eq!(vertex.point.coords, [1.0, 2.0, 3.0, 4.0]);
// assert_eq!(vertex.dim(), 4);
// assert!(vertex.incident_cell.is_none());
// assert!(vertex.data.is_some());
// assert_eq!(vertex.data.unwrap(), "4D");

// // Human readable output for cargo test -- --nocapture
// println!("{:?}", vertex);
// }

#[test]
fn vertex_copy() {
let vertex: Vertex<f64, &str, 4> = VertexBuilder::default()
Expand Down

0 comments on commit 6738259

Please sign in to comment.