From 91820b4a7343719977eec4caefed197812dfa584 Mon Sep 17 00:00:00 2001 From: laentropia Date: Wed, 29 Apr 2026 17:31:43 -0600 Subject: [PATCH] test: tests for sub --- src/lib.rs | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 362a29e..4288d87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -474,7 +474,7 @@ impl Add for Matrix { return Err(MatrixError::InvalidSizeForAdd); } - let mut new_data = Vec::new(); + let mut new_data = Vec::with_capacity(self.data.len()); for i in 0..self.data.len() { new_data.push(self.data[i] + other.data[i]); } @@ -495,7 +495,7 @@ impl Sub for Matrix { return Err(MatrixError::InvalidSizeForSub); } - let mut new_data = Vec::new(); + let mut new_data = Vec::with_capacity(self.data.len()); for i in 0..self.data.len() { new_data.push(self.data[i] - other.data[i]); } @@ -3241,4 +3241,160 @@ mod tests { assert_eq!(result.data, expected); } + + #[test] + fn test_sub_basic() { + let a = Matrix { + rows: 2, + columns: 2, + data: vec![ + Fraction::from(5), + Fraction::from(6), + Fraction::from(7), + Fraction::from(8), + ], + }; + + let b = Matrix { + rows: 2, + columns: 2, + data: vec![ + Fraction::from(1), + Fraction::from(2), + Fraction::from(3), + Fraction::from(4), + ], + }; + + let result = (a - b).unwrap(); + + let expected = vec![ + Fraction::from(4), + Fraction::from(4), + Fraction::from(4), + Fraction::from(4), + ]; + + assert_eq!(result.data, expected); + } + + #[test] + fn test_sub_zero_matrix() { + let a = Matrix { + rows: 2, + columns: 2, + data: vec![ + Fraction::from(1), + Fraction::from(2), + Fraction::from(3), + Fraction::from(4), + ], + }; + + let zero = Matrix::new(2, 2, Fraction::from(0)).unwrap(); + + let result = (a.clone() - zero).unwrap(); + + assert_eq!(result.data, a.data); + } + + #[test] + fn test_sub_self() { + let a = Matrix::new(3, 3, Fraction::from(5)).unwrap(); + + let result = (a.clone() - a).unwrap(); + + assert!(result.data.iter().all(|x| x.is_zero())); + } + + #[test] + fn test_sub_negative_values() { + let a = Matrix { + rows: 2, + columns: 2, + data: vec![ + Fraction::from(1), + Fraction::from(-2), + Fraction::from(3), + Fraction::from(-4), + ], + }; + + let b = Matrix { + rows: 2, + columns: 2, + data: vec![ + Fraction::from(-1), + Fraction::from(2), + Fraction::from(-3), + Fraction::from(4), + ], + }; + + let result = (a - b).unwrap(); + + let expected = vec![ + Fraction::from(2), + Fraction::from(-4), + Fraction::from(6), + Fraction::from(-8), + ]; + + assert_eq!(result.data, expected); + } + + #[test] + fn test_sub_invalid_size() { + let a = Matrix::new(2, 2, Fraction::from(1)).unwrap(); + let b = Matrix::new(3, 2, Fraction::from(1)).unwrap(); + + let result = a - b; + + assert!(matches!(result, Err(MatrixError::InvalidSizeForSub))); + } + + #[test] + fn test_sub_not_commutative() { + let a = Matrix::new(2, 2, Fraction::from(3)).unwrap(); + let b = Matrix::new(2, 2, Fraction::from(1)).unwrap(); + + let res1 = (a.clone() - b.clone()).unwrap(); + let res2 = (b - a).unwrap(); + + assert_ne!(res1.data, res2.data); + } + + #[test] + fn test_sub_anti_symmetry() { + let a = Matrix::new(2, 2, Fraction::from(3)).unwrap(); + let b = Matrix::new(2, 2, Fraction::from(1)).unwrap(); + + let res1 = (a.clone() - b.clone()).unwrap(); + let res2 = (b - a).unwrap(); + + for i in 0..res1.data.len() { + assert_eq!(res1.data[i], -res2.data[i]); + } + } + + #[test] + fn test_sub_fractions() { + let a = Matrix { + rows: 1, + columns: 2, + data: vec![Fraction::new(3, 4).unwrap(), Fraction::new(5, 6).unwrap()], + }; + + let b = Matrix { + rows: 1, + columns: 2, + data: vec![Fraction::new(1, 4).unwrap(), Fraction::new(1, 6).unwrap()], + }; + + let result = (a - b).unwrap(); + + let expected = vec![Fraction::new(1, 2).unwrap(), Fraction::new(2, 3).unwrap()]; + + assert_eq!(result.data, expected); + } }