diff --git a/src/base/dimension.rs b/src/base/dimension.rs index 5d1d1bd9..112996d3 100644 --- a/src/base/dimension.rs +++ b/src/base/dimension.rs @@ -241,7 +241,7 @@ impl NamedDim for typenum::U1 { type Name = U1; } -macro_rules! named_dimension( +macro_rules! named_dimension ( ($($D: ident),* $(,)*) => {$( /// A type level dimension. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 95dda818..8bc02b21 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1137,7 +1137,7 @@ impl + IsNotStaticOne, S: Storage where DefaultAllocator: Allocator, DimSum> { assert!(self.is_square(), "Only square matrices can currently be transformed to homogeneous coordinates."); let dim = DimSum::::from_usize(self.nrows() + 1); - let mut res = MatrixN::identity_generic(dim, dim); + let mut res = MatrixN::identity_generic(dim, dim); res.generic_slice_mut::((0, 0), self.data.shape()).copy_from(&self); res } @@ -1344,18 +1344,19 @@ where S: Storage, {} -impl PartialEq for Matrix +impl PartialEq> for Matrix where - N: Scalar, + N: Scalar + PartialEq, + C: Dim, + C2: Dim, + R: Dim, + R2: Dim, S: Storage, + S2: Storage { #[inline] - fn eq(&self, right: &Matrix) -> bool { - assert!( - self.shape() == right.shape(), - "Matrix equality test dimension mismatch." - ); - self.iter().zip(right.iter()).all(|(l, r)| l == r) + fn eq(&self, right: &Matrix) -> bool { + self.shape() == right.shape() && self.iter().zip(right.iter()).all(|(l, r)| l == r) } } diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index e4fb4d0c..6fad5e8c 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -1,12 +1,15 @@ use num::{One, Zero}; use std::cmp::Ordering; -use na::dimension::{U15, U8}; +use na::dimension::{U15, U8, U2, U4}; use na::{ self, DMatrix, DVector, Matrix2, Matrix2x3, Matrix2x4, Matrix3, Matrix3x2, Matrix3x4, Matrix4, Matrix4x3, Matrix4x5, Matrix5, Matrix6, MatrixMN, RowVector3, RowVector4, RowVector5, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6, }; +use typenum::{UInt, UTerm}; +use serde_json::error::Category::Data; +use typenum::bit::{B0, B1}; #[test] fn iter() { @@ -1047,3 +1050,62 @@ mod finite_dim_inner_space_tests { true } } + +#[test] +fn partial_eq_shape_mismatch() { + let a = Matrix2::new(1, 2, 3, 4); + let b = Matrix2x3::new(1, 2, 3, 4, 5, 6); + assert_ne!(a, b); + assert_ne!(b, a); +} + +#[test] +fn partial_eq_different_types() { + // Ensure comparability of several types of Matrices + let dynamic_mat = DMatrix::from_row_slice(2, 4, &[1, 2, 3, 4, 5, 6, 7, 8]); + let static_mat = Matrix2x4::new(1, 2, 3, 4, 5, 6, 7, 8); + + let mut typenum_static_mat = MatrixMN::::zeros(); + let mut slice = typenum_static_mat.slice_mut((0,0), (2, 4)); + slice += static_mat; + + let fslice_of_dmat = dynamic_mat.fixed_slice::(0, 0); + let dslice_of_dmat = dynamic_mat.slice((0, 0), (2, 2)); + let fslice_of_smat = static_mat.fixed_slice::(0, 0); + let dslice_of_smat = static_mat.slice((0, 0), (2, 2)); + + assert_eq!(dynamic_mat, static_mat); + assert_eq!(static_mat, dynamic_mat); + + assert_eq!(dynamic_mat, slice); + assert_eq!(slice, dynamic_mat); + + assert_eq!(static_mat, slice); + assert_eq!(slice, static_mat); + + assert_eq!(fslice_of_dmat, dslice_of_dmat); + assert_eq!(dslice_of_dmat, fslice_of_dmat); + + assert_eq!(fslice_of_dmat, fslice_of_smat); + assert_eq!(fslice_of_smat, fslice_of_dmat); + + assert_eq!(fslice_of_dmat, dslice_of_smat); + assert_eq!(dslice_of_smat, fslice_of_dmat); + + assert_eq!(dslice_of_dmat, fslice_of_smat); + assert_eq!(fslice_of_smat, dslice_of_dmat); + + assert_eq!(dslice_of_dmat, dslice_of_smat); + assert_eq!(dslice_of_smat, dslice_of_dmat); + + assert_eq!(fslice_of_smat, dslice_of_smat); + assert_eq!(dslice_of_smat, fslice_of_smat); + + assert_ne!(dynamic_mat, dslice_of_smat); + assert_ne!(dslice_of_smat, dynamic_mat); + + // TODO - implement those comparisons + // assert_ne!(static_mat, typenum_static_mat); + //assert_ne!(typenum_static_mat, static_mat); + +} diff --git a/tests/core/serde.rs b/tests/core/serde.rs index c209e35c..781ace77 100644 --- a/tests/core/serde.rs +++ b/tests/core/serde.rs @@ -14,7 +14,8 @@ macro_rules! test_serde( fn $test() { let v: $ty = rand::random(); let serialized = serde_json::to_string(&v).unwrap(); - assert_eq!(v, serde_json::from_str(&serialized).unwrap()); + let deserialized: $ty = serde_json::from_str(&serialized).unwrap(); + assert_eq!(v, deserialized); } )*} ); @@ -23,7 +24,8 @@ macro_rules! test_serde( fn serde_dmatrix() { let v: DMatrix = DMatrix::new_random(3, 4); let serialized = serde_json::to_string(&v).unwrap(); - assert_eq!(v, serde_json::from_str(&serialized).unwrap()); + let deserialized: DMatrix = serde_json::from_str(&serialized).unwrap(); + assert_eq!(v, deserialized); } test_serde!(