From 94cd23fe4b863b5cb87d68a25adf00731a67cf9d Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 18 Sep 2024 10:44:18 -0700 Subject: [PATCH] add ratio tests --- ratio_ops/src/lib.rs | 3 +++ ratio_ops/src/tests.rs | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 ratio_ops/src/tests.rs diff --git a/ratio_ops/src/lib.rs b/ratio_ops/src/lib.rs index 96b877a..6786c4f 100644 --- a/ratio_ops/src/lib.rs +++ b/ratio_ops/src/lib.rs @@ -1 +1,4 @@ pub mod ratio; + +#[cfg(test)] +mod tests; diff --git a/ratio_ops/src/tests.rs b/ratio_ops/src/tests.rs new file mode 100644 index 0000000..739313e --- /dev/null +++ b/ratio_ops/src/tests.rs @@ -0,0 +1,58 @@ +use crate::ratio::Ratio; + +macro_rules! test_op{ + ($ratio_op:ident,$op:ident,$a:expr,$b:expr,$c:expr,$d:expr)=>{ + assert_eq!( + Ratio::new($a,$b).$ratio_op(Ratio::new($c,$d)), + (($a as f32)/($b as f32)).$op(&(($c as f32)/($d as f32))) + ); + }; +} + +macro_rules! test_many_ops{ + ($ratio_op:ident,$op:ident)=>{ + test_op!($ratio_op,$op,1,2,3,4); + test_op!($ratio_op,$op,1,2,-3,4); + test_op!($ratio_op,$op,-1,2,-3,4); + test_op!($ratio_op,$op,-1,-2,-3,4); + test_op!($ratio_op,$op,2,1,6,3); + test_op!($ratio_op,$op,-2,1,6,3); + test_op!($ratio_op,$op,2,-1,-6,3); + test_op!($ratio_op,$op,2,1,6,-3); + }; +} + +#[test] +fn test_lt(){ + test_many_ops!(lt_ratio,lt); +} + +#[test] +fn test_gt(){ + test_many_ops!(gt_ratio,gt); +} + +#[test] +fn test_le(){ + test_many_ops!(le_ratio,le); +} + +#[test] +fn test_ge(){ + test_many_ops!(ge_ratio,ge); +} + +#[test] +fn test_eq(){ + test_many_ops!(eq_ratio,eq); +} + +#[test] +fn test_partial_cmp(){ + test_many_ops!(partial_cmp_ratio,partial_cmp); +} + +// #[test] +// fn test_cmp(){ +// test_many_ops!(cmp_ratio,cmp); +// }