Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/integer/z/arithmetic/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Add<&Q> for &Z {

arithmetic_trait_borrowed_to_owned!(Add, add, Z, Q, Q);
arithmetic_trait_mixed_borrowed_owned!(Add, add, Z, Q, Q);
arithmetic_between_types!(Add, add, Z, Q, f32 f64);

impl Add<&Zq> for &Z {
type Output = Zq;
Expand Down Expand Up @@ -381,6 +382,26 @@ mod test_add_between_z_and_q {
use super::Z;
use crate::rational::Q;

/// Ensuring addition between different types is available
#[test]
fn availability() {
let a: Z = Z::from(42);
let b: Q = Q::from((5, 7));

let _: Q = &a + &b;
let _: Q = &a + b.clone();
let _: Q = a.clone() + &b;
let _: Q = a.clone() + b;
let _: Q = &a + 0.5_f32;
let _: Q = &a + 0.5_f64;
let _: Q = a.clone() + 0.5_f32;
let _: Q = a.clone() + 0.5_f64;
let _: Q = 0.5_f32 + &a;
let _: Q = 0.5_f64 + &a;
let _: Q = 0.5_f32 + a.clone();
let _: Q = 0.5_f64 + a.clone();
}

/// Testing addition for [`Z`] and [`Q`]
#[test]
fn add() {
Expand Down
41 changes: 40 additions & 1 deletion src/integer/z/arithmetic/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,17 @@ impl Div for &Z {

arithmetic_trait_borrowed_to_owned!(Div, div, Z, Z, Q);
arithmetic_trait_mixed_borrowed_owned!(Div, div, Z, Z, Q);
arithmetic_between_types!(Div, div, Z, Q, i64 i32 i16 i8 u64 u32 u16 u8);
arithmetic_between_types!(Div, div, Z, Q, i64 i32 i16 i8 u64 u32 u16 u8 f32 f64);

#[test]
fn same() {
let a = Z::from(4) / 2;
let b = 4 / Z::from(2);

println!("{a}, {b}");

assert_eq!(a, b);
}

impl Div<&Q> for &Z {
type Output = Q;
Expand Down Expand Up @@ -583,6 +593,35 @@ mod test_div_between_z_and_q {
use super::Z;
use crate::rational::Q;

/// Ensuring division between different types is available
#[test]
fn availability() {
let a: Z = Z::from(42);
let b: Q = Q::from((5, 7));

let _: Q = &a / &b;
let _: Q = &a / b.clone();
let _: Q = a.clone() / &b;
let _: Q = a.clone() / b;
let _: Q = &a / 0.5_f32;
let _: Q = &a / 0.5_f64;
let _: Q = a.clone() / 0.5_f32;
let _: Q = a.clone() / 0.5_f64;
let _: Q = 0.5_f32 / &a;
let _: Q = 0.5_f64 / &a;
let _: Q = 0.5_f32 / a.clone();
let _: Q = 0.5_f64 / a.clone();
}

/// Ensures that division is performed the right way around
#[test]
fn order_retained() {
let a = Z::from(4);

assert_eq!(2, &a / 2);
assert_eq!(Q::from((1, 2)), 2 / &a);
}

/// Testing division for [`Z`] and [`Q`]
#[test]
fn div() {
Expand Down
21 changes: 21 additions & 0 deletions src/integer/z/arithmetic/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl Mul<&Q> for &Z {

arithmetic_trait_borrowed_to_owned!(Mul, mul, Z, Q, Q);
arithmetic_trait_mixed_borrowed_owned!(Mul, mul, Z, Q, Q);
arithmetic_between_types!(Mul, mul, Z, Q, f32 f64);

impl Mul<&Zq> for &Z {
type Output = Zq;
Expand Down Expand Up @@ -447,6 +448,26 @@ mod test_mul_between_z_and_q {
use super::Z;
use crate::rational::Q;

/// Ensuring multiplication between different types is available
#[test]
fn availability() {
let a: Z = Z::from(42);
let b: Q = Q::from((5, 7));

let _: Q = &a * &b;
let _: Q = &a * b.clone();
let _: Q = a.clone() * &b;
let _: Q = a.clone() * b;
let _: Q = &a * 0.5_f32;
let _: Q = &a * 0.5_f64;
let _: Q = a.clone() * 0.5_f32;
let _: Q = a.clone() * 0.5_f64;
let _: Q = 0.5_f32 * &a;
let _: Q = 0.5_f64 * &a;
let _: Q = 0.5_f32 * a.clone();
let _: Q = 0.5_f64 * a.clone();
}

/// Testing multiplication for [`Z`] and [`Q`]
#[test]
fn mul() {
Expand Down
30 changes: 30 additions & 0 deletions src/integer/z/arithmetic/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl Sub<&Q> for &Z {

arithmetic_trait_borrowed_to_owned!(Sub, sub, Z, Q, Q);
arithmetic_trait_mixed_borrowed_owned!(Sub, sub, Z, Q, Q);
arithmetic_between_types!(Sub, sub, Z, Q, f64 f32);

impl Sub<&Zq> for &Z {
type Output = Zq;
Expand Down Expand Up @@ -397,6 +398,35 @@ mod test_sub_between_z_and_q {
use super::Z;
use crate::rational::Q;

/// Ensuring subtraction between different types is available
#[test]
fn availability() {
let a: Z = Z::from(42);
let b: Q = Q::from((5, 7));

let _: Q = &a - &b;
let _: Q = &a - b.clone();
let _: Q = a.clone() - &b;
let _: Q = a.clone() - b;
let _: Q = &a - 0.5_f32;
let _: Q = &a - 0.5_f64;
let _: Q = a.clone() - 0.5_f32;
let _: Q = a.clone() - 0.5_f64;
let _: Q = 0.5_f32 - &a;
let _: Q = 0.5_f64 - &a;
let _: Q = 0.5_f32 - a.clone();
let _: Q = 0.5_f64 - a.clone();
}

/// Ensures that division is performed the right way around
#[test]
fn order_retained() {
let a = Z::from(4);

assert_eq!(2, &a - 2);
assert_eq!(Q::from((-2, 1)), 2 - &a);
}

/// Testing subtraction for [`Z`] and [`Q`]
#[test]
fn sub() {
Expand Down
4 changes: 2 additions & 2 deletions src/macros/arithmetics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ macro_rules! arithmetic_between_types {
paste::paste! {
#[doc = "Documentation at [`" $type "::" $trait_function "`]."]
fn $trait_function(self, other: &$other_type) -> Self::Output {
self.$trait_function($type::from(*other))
self.$trait_function($output_type::from(*other))
}
}
}
Expand All @@ -145,7 +145,7 @@ macro_rules! arithmetic_between_types {
paste::paste! {
#[doc = "Documentation at [`" $type "::" $trait_function "`]."]
fn $trait_function(self, other: &$type) -> Self::Output {
$type::from(*self).$trait_function(other)
$output_type::from(*self).$trait_function(other)
}
}
}
Expand Down
Loading