Added tests for div and rem

This commit is contained in:
Fabian Schmidt 2024-11-26 15:40:38 +01:00
parent ed886762d0
commit 50b2aa8932

View File

@ -57,6 +57,27 @@ impl Number {
remainder.digits.remove(0);
}
Ok((res, remainder))
//if n2 == 0.into() {
// return Err(Box::from("Division by 0"));
//}
//let n1_len = n1.digits.len();
//let n2_len = n2.digits.len();
//if n2_len > n1_len {
// return Ok((Number::from(0), n1));
//}
//let sign = match (n1.sign, n2.sign) {
// (Sign::Positif, Sign::Positif) | (Sign::Negatif, Sign::Negatif) => Sign::Positif,
// (Sign::Positif, Sign::Negatif) | (Sign::Negatif, Sign::Positif) => Sign::Negatif,
//};
//let mut res = Number::from(0);
//let mut remainder = n1;
//loop {}
//if res.digits != vec![0] {
// res.sign = sign;
//}
//Ok((res, remainder))
}
pub fn gcd(mut a: Number, mut b: Number) -> Number {
@ -110,3 +131,83 @@ impl RemAssign for Number {
}
}
}
#[cfg(test)]
mod test_number_div_rem {
use core::panic;
use crate::number::Number;
#[test]
fn zero_denominator() {
let a = Number::from(9);
let b = Number::from(0);
if Number::div_with_rem(a, b).is_ok() {
panic!("Division by 0 not allowed");
}
}
#[test]
fn div_rem_positif() {
let a = Number::from(9);
let b = Number::from(9);
let res = (Number::from(1), Number::from(0));
assert_eq!(res, Number::div_with_rem(a, b).unwrap());
}
#[test]
fn div_rem_negatif() {
let a = Number::from(-9);
let b = Number::from(-9);
let res = (Number::from(1), Number::from(0));
assert_eq!(res, Number::div_with_rem(a, b).unwrap());
}
#[test]
fn div_rem_positif_with_negatif() {
let a = Number::from(9);
let b = Number::from(-9);
let res = (Number::from(-1), Number::from(0));
assert_eq!(res, Number::div_with_rem(a, b).unwrap());
}
#[test]
fn div_rem_negatif_with_positif() {
let a = Number::from(-9);
let b = Number::from(9);
let res = (Number::from(-1), Number::from(0));
assert_eq!(res, Number::div_with_rem(a, b).unwrap());
}
#[test]
fn div_rem_eleven_x_ten() {
let a = Number::from(11);
let b = Number::from(10);
let res = (Number::from(1), Number::from(1));
assert_eq!(res, Number::div_with_rem(a, b).unwrap());
}
#[test]
fn div_rem_eleven_x_eleven() {
let a = Number::from(10);
let b = Number::from(11);
let res = (Number::from(0), Number::from(10));
assert_eq!(res, Number::div_with_rem(a, b).unwrap());
}
#[test]
fn div_rem_big_1() {
//let a = Number::from("123");
//let b = Number::from("32");
//let res = (Number::from(3), Number::from(27));
//assert_eq!(res, Number::div_with_rem(a, b).unwrap());
}
#[test]
fn div_rem_big_2() {
//let a = Number::from("123456789");
//let b = Number::from("123");
//let res = (Number::from(1003713), Number::from(90));
//assert_eq!(res, Number::div_with_rem(a, b).unwrap());
}
}