57 lines
1.5 KiB
Rust
57 lines
1.5 KiB
Rust
|
use rusty_numbers::frac;
|
||
|
|
||
|
#[test]
|
||
|
fn mul_test() {
|
||
|
let frac1 = frac!(1 / 3u8);
|
||
|
let frac2 = frac!(2u8 / 3);
|
||
|
|
||
|
let expected = frac!(2u8 / 9);
|
||
|
|
||
|
assert_eq!(frac1 * frac2, expected);
|
||
|
|
||
|
assert_eq!(frac!(0), frac!(0) * frac!(5 / 8));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn div_test() {
|
||
|
assert_eq!(frac!(1), frac!(1 / 3) / frac!(1 / 3));
|
||
|
assert_eq!(frac!(1 / 9), frac!(1 / 3) / frac!(3));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn add_test() {
|
||
|
assert_eq!(frac!(5 / 8), frac!(5 / 8) + frac!(0));
|
||
|
assert_eq!(frac!(5 / 6), frac!(1 / 3) + frac!(1 / 2), "1/3 + 1/2");
|
||
|
assert_eq!(frac!(1 / 3), frac!(2 / 3) + -frac!(1 / 3), "2/3 + -1/3");
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn sub_test() {
|
||
|
assert_eq!(frac!(5 / 8), frac!(5 / 8) - frac!(0));
|
||
|
assert_eq!(frac!(1 / 6), frac!(1 / 2) - frac!(1 / 3), "1/2 - 1/3");
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn cmp_test() {
|
||
|
assert!(frac!(0) < frac!(1));
|
||
|
assert!(-frac!(5 / 3) < frac!(1 / 10_000));
|
||
|
assert!(frac!(1 / 10_000) > -frac!(10));
|
||
|
assert!(frac!(1 / 3) < frac!(1 / 2));
|
||
|
assert_eq!(frac!(1 / 2), frac!(3 / 6));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn negative_fractions() {
|
||
|
assert_eq!(-frac!(1 / 3), -frac!(2 / 3) + frac!(1 / 3), "-2/3 + 1/3");
|
||
|
assert_eq!(frac!(1), frac!(1 / 3) - -frac!(2 / 3), "1/3 - -2/3");
|
||
|
assert_eq!(-frac!(1), -frac!(2 / 3) - frac!(1 / 3), "-2/3 - +1/3");
|
||
|
assert_eq!(-frac!(1), -frac!(2 / 3) + -frac!(1 / 3), "-2/3 + -1/3");
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn negative_mul_div() {
|
||
|
assert_eq!(-frac!(1 / 12), -frac!(1 / 3) * frac!(1 / 4));
|
||
|
assert_eq!(-frac!(1 / 12), frac!(1 / 3) * -frac!(1 / 4));
|
||
|
assert_eq!(frac!(1 / 12), -frac!(1 / 3) * -frac!(1 / 4));
|
||
|
}
|