test(maths): code coverage
This commit is contained in:
parent
869dca85c0
commit
1e1a8457fa
159
src/maths.rs
159
src/maths.rs
|
@ -3,7 +3,7 @@ use std::ops;
|
|||
pub mod evaluator;
|
||||
pub mod solver;
|
||||
|
||||
//TODO slow ? check Stein's algorithm (binaryGCD)
|
||||
//TODO slow ? check Stein's algorithm (binaryGCD) + tests
|
||||
fn gcd(a: i128, b: i128) -> i128 {
|
||||
if b == 0 {
|
||||
a
|
||||
|
@ -43,6 +43,7 @@ impl Rational {
|
|||
Rational::new(self.numerator, self.denominator * 10)
|
||||
}
|
||||
|
||||
// TODO return string
|
||||
pub fn print(&self) {
|
||||
match (self.numerator, self.denominator) {
|
||||
(_, 1) => print!("{}", self.numerator),
|
||||
|
@ -50,6 +51,7 @@ impl Rational {
|
|||
};
|
||||
}
|
||||
|
||||
// TODO rename or move to better scope, only used in complex div ?
|
||||
pub fn simplify(lhs: Rational, rhs: Rational) -> Self {
|
||||
let res = Rational::new(lhs.numerator * rhs.denominator, lhs.denominator * rhs.numerator);
|
||||
res.reduce()
|
||||
|
@ -183,6 +185,7 @@ impl GaussianRational {
|
|||
GaussianRational::new(self.real, -1 * self.imaginary)
|
||||
}
|
||||
|
||||
// TODO return string, avoid printing real or imag part if zero
|
||||
pub fn print(&self) {
|
||||
self.real.print();
|
||||
print!("+");
|
||||
|
@ -252,6 +255,160 @@ impl ops::Div<GaussianRational> for GaussianRational {
|
|||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn gcd_test() {
|
||||
assert_eq!(gcd(7, 3), 1);
|
||||
assert_eq!(gcd(7, -3), 1);
|
||||
assert_eq!(gcd(-7, 3), -1);
|
||||
assert_eq!(gcd(-7, -3), -1);
|
||||
|
||||
|
||||
assert_eq!(gcd(25, 20), 5);
|
||||
assert_eq!(gcd(25, -20), 5);
|
||||
assert_eq!(gcd(-25, 20), -5);
|
||||
assert_eq!(gcd(-25, -20), -5);
|
||||
|
||||
assert_eq!(gcd(30, 30), 30);
|
||||
}
|
||||
|
||||
mod rational {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new() {
|
||||
let rational = Rational::new(5, 10);
|
||||
assert_eq!(rational, Rational { numerator: 5, denominator: 10 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reduce() {
|
||||
let rational = Rational::new(20, 10);
|
||||
assert_eq!(rational.reduce(), Rational::new(2, 1));
|
||||
|
||||
let rational = Rational::new(7, -3);
|
||||
assert_eq!(rational.reduce(), Rational::new(-7, 3));
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inverse() {
|
||||
let rational = Rational::new(5, 2);
|
||||
assert_eq!(rational.inverse(), Rational::new(2, 5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn left_shift() {
|
||||
let rational = Rational::new(7, 3);
|
||||
assert_eq!(rational.left_shift(), Rational::new(7, 30));
|
||||
}
|
||||
|
||||
// TODO
|
||||
#[test]
|
||||
fn simplify() {
|
||||
let a = Rational::new(10, 3);
|
||||
let b = Rational::new(7, 2);
|
||||
|
||||
assert_eq!(Rational::simplify(a, b), Rational::new(20, 21));
|
||||
|
||||
|
||||
let a = Rational::new(10, 5);
|
||||
let b = Rational::new(5, 2);
|
||||
|
||||
assert_eq!(Rational::simplify(a, b), Rational::new(4, 5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_zero() {
|
||||
let rational = Rational::new(0, 4);
|
||||
assert_eq!(rational.is_zero(), true);
|
||||
|
||||
let rational = Rational::new(3, 4);
|
||||
assert_eq!(rational.is_zero(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn getters() {
|
||||
let rational = Rational::new(5, 7);
|
||||
assert_eq!(rational.numerator(), 5);
|
||||
assert_eq!(rational.denominator(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ops() {
|
||||
let a = Rational::new(5, 7);
|
||||
let b = Rational::new(12, 14);
|
||||
let n = 37;
|
||||
|
||||
assert_eq!(a + b, Rational::new(11, 7));
|
||||
assert_eq!(a - b, Rational::new(-1, 7));
|
||||
assert_eq!(a * b, Rational::new(30, 49));
|
||||
assert_eq!(a / b, Rational::new(5, 6));
|
||||
|
||||
assert_eq!(a + n, Rational::new(264, 7));
|
||||
assert_eq!(n + a, Rational::new(264, 7));
|
||||
assert_eq!(a - n, Rational::new(-254, 7));
|
||||
assert_eq!(n - a, Rational::new(254, 7));
|
||||
assert_eq!(a * n, Rational::new(185, 7));
|
||||
assert_eq!(n * a, Rational::new(185, 7));
|
||||
assert_eq!(a / n, Rational::new(5, 259));
|
||||
assert_eq!(n / a, Rational::new(259, 5));
|
||||
}
|
||||
}
|
||||
|
||||
mod gaussian_rational {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new() {
|
||||
let gaussian_rational = GaussianRational::new(Rational::new(5, 7), Rational::new(8, 3));
|
||||
assert_eq!(gaussian_rational, GaussianRational {
|
||||
real: Rational::new(5, 7),
|
||||
imaginary: Rational::new(8, 3)
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conjugate() {
|
||||
let gaussian_rational = GaussianRational::new(Rational::new(5, 7), Rational::new(8, 3));
|
||||
assert_eq!(gaussian_rational.conjugate(), GaussianRational::new(Rational::new(5, 7), Rational::new(-8, 3)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_real() {
|
||||
let gaussian_rational = GaussianRational::new(Rational::new(5, 7), Rational::new(8, 3));
|
||||
assert_eq!(gaussian_rational.is_real(), false);
|
||||
|
||||
let gaussian_rational = GaussianRational::new(Rational::new(5, 7), Rational::new(0, 3));
|
||||
assert_eq!(gaussian_rational.is_real(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn getters() {
|
||||
let gaussian_rational = GaussianRational::new(Rational::new(5, 7), Rational::new(8, 3));
|
||||
assert_eq!(gaussian_rational.real(), Rational::new(5, 7));
|
||||
assert_eq!(gaussian_rational.imaginary(), Rational::new(8, 3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ops() {
|
||||
let a = GaussianRational::new(Rational::new(5, 7), Rational::new(8, 3));
|
||||
let b = GaussianRational::new(Rational::new(8, 7), Rational::new(7, 3));
|
||||
let n = 3;
|
||||
|
||||
assert_eq!(a + b, GaussianRational::new(Rational::new(13, 7), Rational::new(5, 1)));
|
||||
assert_eq!(a - b, GaussianRational::new(Rational::new(-3, 7), Rational::new(1, 3)));
|
||||
assert_eq!(a * b, GaussianRational::new(Rational::new(-2384, 441), Rational::new(33, 7)));
|
||||
assert_eq!(a / b, GaussianRational::new(Rational::new(3104, 2977), Rational::new(609, 2977)));
|
||||
|
||||
assert_eq!(a * n, GaussianRational::new(Rational::new(15, 7), Rational::new(8, 1)));
|
||||
assert_eq!(n * a, GaussianRational::new(Rational::new(15, 7), Rational::new(8, 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub struct SquareRoot {
|
||||
rational: Rational,
|
||||
|
|
Loading…
Reference in New Issue