diff --git a/src/lib.rs b/src/lib.rs index d4deeec..78c37ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -426,7 +426,7 @@ mod tests { #[test] fn test_mul_with_negative_denominator_input() { - let a = Fraction::new(2, -3).unwrap(); // -2/3 + let a = Fraction::new(2, -3).unwrap(); let b = Fraction::new(3, 4).unwrap(); let c = a * b; assert_eq!(c.num, -1); @@ -479,8 +479,8 @@ mod tests { #[test] fn test_div_result_reduced() { - let a = Fraction::new(4, 6).unwrap(); // 2/3 - let b = Fraction::new(8, 9).unwrap(); // 8/9 + let a = Fraction::new(4, 6).unwrap(); + let b = Fraction::new(8, 9).unwrap(); let c = (a / b).unwrap(); assert_eq!(c.num, 3); assert_eq!(c.den, 4); @@ -505,8 +505,8 @@ mod tests { #[test] fn test_div_large_numbers() { - let a = Fraction::new(1000, 2000).unwrap(); // 1/2 - let b = Fraction::new(3000, 4000).unwrap(); // 3/4 + let a = Fraction::new(1000, 2000).unwrap(); + let b = Fraction::new(3000, 4000).unwrap(); let c = (a / b).unwrap(); assert_eq!(c.num, 2); assert_eq!(c.den, 3); @@ -514,10 +514,73 @@ mod tests { #[test] fn test_div_sign_normalization() { - let a = Fraction::new(2, -3).unwrap(); // -2/3 + let a = Fraction::new(2, -3).unwrap(); let b = Fraction::new(4, 5).unwrap(); let c = (a / b).unwrap(); assert_eq!(c.num, -5); assert_eq!(c.den, 6); } + + #[test] + fn test_reciprocal_basic() { + let a = Fraction::new(2, 3).unwrap(); + let r = a.reciprocal().unwrap(); + assert_eq!(r.num, 3); + assert_eq!(r.den, 2); + } + + #[test] + fn test_reciprocal_reduces() { + let a = Fraction::new(4, 6).unwrap(); + let r = a.reciprocal().unwrap(); + assert_eq!(r.num, 3); + assert_eq!(r.den, 2); + } + + #[test] + fn test_reciprocal_negative() { + let a = Fraction::new(-2, 3).unwrap(); + let r = a.reciprocal().unwrap(); + assert_eq!(r.num, -3); + assert_eq!(r.den, 2); + } + + #[test] + fn test_reciprocal_negative_denominator_input() { + let a = Fraction::new(2, -3).unwrap(); + let r = a.reciprocal().unwrap(); + assert_eq!(r.num, -3); + assert_eq!(r.den, 2); + } + + #[test] + fn test_reciprocal_of_one() { + let a = Fraction::from(1); + let r = a.reciprocal().unwrap(); + assert_eq!(r.num, 1); + assert_eq!(r.den, 1); + } + + #[test] + fn test_reciprocal_of_negative_one() { + let a = Fraction::new(-1, 1).unwrap(); + let r = a.reciprocal().unwrap(); + assert_eq!(r.num, -1); + assert_eq!(r.den, 1); + } + + #[test] + fn test_reciprocal_of_zero_error() { + let a = Fraction::new(0, 5).unwrap(); + let r = a.reciprocal(); + assert!(matches!(r, Err(FractionError::ZeroDenominator))); + } + + #[test] + fn test_reciprocal_twice_returns_original() { + let a = Fraction::new(7, 9).unwrap(); + let r = a.reciprocal().unwrap().reciprocal().unwrap(); + assert_eq!(r.num, a.num); + assert_eq!(r.den, a.den); + } }