use matrixcompare::{ DimensionMismatch, ElementsMismatch, Entry, MatrixComparisonFailure, MatrixElementComparisonFailure, }; use std::ops::Range; pub const MATRIX_DIM_RANGE: Range = 0..5; /// Reverses a comparison result. /// /// See docs for `reverse_failure`. pub fn reverse_result( result: Result<(), MatrixComparisonFailure>, ) -> Result<(), MatrixComparisonFailure> { result.map_err(|err| reverse_failure(err)) } /// Reverses the role of left and right in the given failure object. /// /// Only used for testing that comparison is symmetric. /// It is implicitly assumed that the error metric is symmetric. pub fn reverse_failure( failure: MatrixComparisonFailure, ) -> MatrixComparisonFailure { use MatrixComparisonFailure::*; match failure { MismatchedDimensions(dim) => MismatchedDimensions(reverse_dimension_mismatch(dim)), MismatchedElements(elements) => MismatchedElements(reverse_elements_mismatch(elements)), SparseEntryOutOfBounds(entry) => SparseEntryOutOfBounds(reverse_entry(entry)), DuplicateSparseEntry(entry) => SparseEntryOutOfBounds(reverse_entry(entry)), } } fn reverse_dimension_mismatch(dims: DimensionMismatch) -> DimensionMismatch { DimensionMismatch { dim_left: dims.dim_right, dim_right: dims.dim_left, } } fn reverse_elements_mismatch(mismatch: ElementsMismatch) -> ElementsMismatch { ElementsMismatch { comparator_description: mismatch.comparator_description, mismatches: mismatch .mismatches .into_iter() .map(reverse_matrix_element_comparison_failure) .collect(), } } fn reverse_matrix_element_comparison_failure( failure: MatrixElementComparisonFailure, ) -> MatrixElementComparisonFailure { MatrixElementComparisonFailure { left: failure.right, right: failure.left, error: failure.error, row: failure.row, col: failure.col, } } fn reverse_entry(entry: Entry) -> Entry { match entry { Entry::Left(coord) => Entry::Right(coord), Entry::Right(coord) => Entry::Left(coord), } }