From d508971820625a19a465d4ec20ebbfe56507ef63 Mon Sep 17 00:00:00 2001 From: Mike Boutin Date: Sun, 24 Dec 2017 14:25:03 -0500 Subject: [PATCH] Implement `PartialEq`. Resolves #17. --- src/si/acceleration.rs | 50 +++++++++---------- src/si/area.rs | 49 +++++++++--------- src/si/force.rs | 109 ++++++++++++++++++++--------------------- src/si/frequency.rs | 53 ++++++++++---------- src/si/velocity.rs | 49 +++++++++--------- src/si/volume.rs | 49 +++++++++--------- src/system.rs | 35 +++++++++++++ src/tests.rs | 13 +++-- 8 files changed, 217 insertions(+), 190 deletions(-) diff --git a/src/si/acceleration.rs b/src/si/acceleration.rs index b16e39d7..725d8839 100644 --- a/src/si/acceleration.rs +++ b/src/si/acceleration.rs @@ -76,34 +76,32 @@ mod tests { #[test] fn check_units() { - test(l::yottameter, a::yottameter_per_second_squared); - test(l::zettameter, a::zettameter_per_second_squared); - test(l::exameter, a::exameter_per_second_squared); - test(l::petameter, a::petameter_per_second_squared); - test(l::terameter, a::terameter_per_second_squared); - test(l::gigameter, a::gigameter_per_second_squared); - test(l::megameter, a::megameter_per_second_squared); - test(l::kilometer, a::kilometer_per_second_squared); - test(l::hectometer, a::hectometer_per_second_squared); - test(l::decameter, a::decameter_per_second_squared); - test(l::meter, a::meter_per_second_squared); - test(l::decimeter, a::decimeter_per_second_squared); - test(l::centimeter, a::centimeter_per_second_squared); - test(l::millimeter, a::millimeter_per_second_squared); - test(l::micrometer, a::micrometer_per_second_squared); - test(l::nanometer, a::nanometer_per_second_squared); - test(l::picometer, a::picometer_per_second_squared); - test(l::femtometer, a::femtometer_per_second_squared); - test(l::attometer, a::attometer_per_second_squared); - test(l::zeptometer, a::zeptometer_per_second_squared); - test(l::yoctometer, a::yoctometer_per_second_squared); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); - // TODO #17 Convert to == once PartialEq is implemented. - fn test, A: a::Conversion>(_l: L, a: A) { - Test::assert_eq(&V::one(), + fn test, A: a::Conversion>() { + Test::assert_eq(&Acceleration::new::(V::one()), &(Length::new::(V::one()) / - (Time::new::(V::one()) * Time::new::(V::one()))) - .get(a)); + (Time::new::(V::one()) * Time::new::(V::one())))); } } } diff --git a/src/si/area.rs b/src/si/area.rs index 88c70958..3de25ae5 100644 --- a/src/si/area.rs +++ b/src/si/area.rs @@ -77,38 +77,37 @@ mod tests { fn check_units() { // Values too large for f32. if TypeId::of::() == TypeId::of::() { - test(l::yottameter, a::square_yottameter); - test(l::zettameter, a::square_zettameter); + test::(); + test::(); } - test(l::exameter, a::square_exameter); - test(l::petameter, a::square_petameter); - test(l::terameter, a::square_terameter); - test(l::gigameter, a::square_gigameter); - test(l::megameter, a::square_megameter); - test(l::kilometer, a::square_kilometer); - test(l::hectometer, a::square_hectometer); - test(l::decameter, a::square_decameter); - test(l::meter, a::square_meter); - test(l::decimeter, a::square_decimeter); - test(l::centimeter, a::square_centimeter); - test(l::millimeter, a::square_millimeter); - test(l::micrometer, a::square_micrometer); - test(l::nanometer, a::square_nanometer); - test(l::picometer, a::square_picometer); - test(l::femtometer, a::square_femtometer); - test(l::attometer, a::square_attometer); - test(l::zeptometer, a::square_zeptometer); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); // Values too small for f32. if TypeId::of::() == TypeId::of::() { - test(l::yoctometer, a::square_yoctometer); + test::(); } - // TODO #17 Convert to == once PartialEq is implemented. - fn test, A: a::Conversion>(_l: L, a: A) { - Test::assert_eq(&V::one(), - &(Length::new::(V::one()) * Length::new::(V::one())).get(a)); + fn test, A: a::Conversion>() { + Test::assert_eq(&Area::new::(V::one()), + &(Length::new::(V::one()) * Length::new::(V::one()))); } } } diff --git a/src/si/force.rs b/src/si/force.rs index fedb670c..ae4d2bf2 100644 --- a/src/si/force.rs +++ b/src/si/force.rs @@ -58,71 +58,66 @@ mod tests { #[test] fn check_units() { - test(m::yottagram, l::meter, t::second, f::zettanewton); - test(m::zettagram, l::meter, t::second, f::exanewton); - test(m::exagram, l::meter, t::second, f::petanewton); - test(m::petagram, l::meter, t::second, f::teranewton); - test(m::teragram, l::meter, t::second, f::giganewton); - test(m::gigagram, l::meter, t::second, f::meganewton); - test(m::megagram, l::meter, t::second, f::kilonewton); - test(m::kilogram, l::meter, t::second, f::newton); - test(m::gram, l::meter, t::second, f::millinewton); - test(m::milligram, l::meter, t::second, f::micronewton); - test(m::microgram, l::meter, t::second, f::nanonewton); - test(m::nanogram, l::meter, t::second, f::piconewton); - test(m::picogram, l::meter, t::second, f::femtonewton); - test(m::femtogram, l::meter, t::second, f::attonewton); - test(m::attogram, l::meter, t::second, f::zeptonewton); - test(m::zeptogram, l::meter, t::second, f::yoctonewton); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); - test(m::kilogram, l::yottameter, t::second, f::yottanewton); - test(m::kilogram, l::zettameter, t::second, f::zettanewton); - test(m::kilogram, l::exameter, t::second, f::exanewton); - test(m::kilogram, l::petameter, t::second, f::petanewton); - test(m::kilogram, l::terameter, t::second, f::teranewton); - test(m::kilogram, l::gigameter, t::second, f::giganewton); - test(m::kilogram, l::megameter, t::second, f::meganewton); - test(m::kilogram, l::kilometer, t::second, f::kilonewton); - test(m::kilogram, l::hectometer, t::second, f::hectonewton); - test(m::kilogram, l::decameter, t::second, f::decanewton); - test(m::kilogram, l::meter, t::second, f::newton); - test(m::kilogram, l::decimeter, t::second, f::decinewton); - test(m::kilogram, l::centimeter, t::second, f::centinewton); - test(m::kilogram, l::millimeter, t::second, f::millinewton); - test(m::kilogram, l::micrometer, t::second, f::micronewton); - test(m::kilogram, l::nanometer, t::second, f::nanonewton); - test(m::kilogram, l::picometer, t::second, f::piconewton); - test(m::kilogram, l::femtometer, t::second, f::femtonewton); - test(m::kilogram, l::attometer, t::second, f::attonewton); - test(m::kilogram, l::zeptometer, t::second, f::zeptonewton); - test(m::kilogram, l::meter, t::second, f::newton); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); - test(m::kilogram, l::meter, t::terasecond, f::yoctonewton); - test(m::kilogram, l::meter, t::gigasecond, f::attonewton); - test(m::kilogram, l::meter, t::megasecond, f::piconewton); - test(m::kilogram, l::meter, t::kilosecond, f::micronewton); - test(m::kilogram, l::meter, t::decasecond, f::centinewton); - test(m::kilogram, l::meter, t::decisecond, f::hectonewton); - test(m::kilogram, l::meter, t::millisecond, f::meganewton); - test(m::kilogram, l::meter, t::microsecond, f::teranewton); - test(m::kilogram, l::meter, t::nanosecond, f::exanewton); - test(m::kilogram, l::meter, t::picosecond, f::yottanewton); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); - // TODO #17 Convert to == once PartialEq is implemented. fn test< M: m::Conversion, L: l::Conversion, T: t::Conversion, - F: f::Conversion - >( - _m: M, - _l: L, - _t: T, - f: F - ) { - Test::assert_approx_eq(&V::one(), &((Mass::new::(V::one()) + F: f::Conversion>() + { + Test::assert_approx_eq(&Force::new::(V::one()), + &((Mass::new::(V::one()) * Length::new::(V::one())) - / (Time::new::(V::one()) * Time::new::(V::one()))).get(f)); + / (Time::new::(V::one()) * Time::new::(V::one())))); } } } diff --git a/src/si/frequency.rs b/src/si/frequency.rs index 3a8851ac..e53c3447 100644 --- a/src/si/frequency.rs +++ b/src/si/frequency.rs @@ -55,34 +55,33 @@ mod tests { #[test] fn check_units() { - test(t::yottasecond, f::yoctohertz); - test(t::zettasecond, f::zeptohertz); - test(t::exasecond, f::attohertz); - test(t::petasecond, f::femtohertz); - test(t::terasecond, f::picohertz); - test(t::gigasecond, f::nanohertz); - test(t::megasecond, f::microhertz); - test(t::kilosecond, f::millihertz); - test(t::hectosecond, f::centihertz); - test(t::decasecond, f::decihertz); - test(t::second, f::hertz); - test(t::decisecond, f::decahertz); - test(t::centisecond, f::hectohertz); - test(t::millisecond, f::kilohertz); - test(t::microsecond, f::megahertz); - test(t::nanosecond, f::gigahertz); - test(t::picosecond, f::terahertz); - test(t::femtosecond, f::petahertz); - test(t::attosecond, f::exahertz); - test(t::zeptosecond, f::zettahertz); - test(t::yoctosecond, f::yottahertz); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); - // TODO #17 Convert to == once PartialEq is implemented. - fn test, F: f::Conversion>(t: T, f: F) { - Test::assert_approx_eq(&(V::one() / Time::new::(V::one())).get(f), - &Frequency::new::(V::one()).get(f)); - Test::assert_approx_eq(&Time::new::(V::one()).get(t), - &(V::one() / Frequency::new::(V::one())).get(t)); + fn test, F: f::Conversion>() { + Test::assert_approx_eq(&(V::one() / Time::new::(V::one())), + &Frequency::new::(V::one())); + Test::assert_approx_eq(&Time::new::(V::one()), + &(V::one() / Frequency::new::(V::one()))); } } } diff --git a/src/si/velocity.rs b/src/si/velocity.rs index 22e2e692..3ebf0e90 100644 --- a/src/si/velocity.rs +++ b/src/si/velocity.rs @@ -75,32 +75,31 @@ mod test { #[test] fn check_units() { - test(l::yottameter, v::yottameter_per_second); - test(l::zettameter, v::zettameter_per_second); - test(l::exameter, v::exameter_per_second); - test(l::petameter, v::petameter_per_second); - test(l::terameter, v::terameter_per_second); - test(l::gigameter, v::gigameter_per_second); - test(l::megameter, v::megameter_per_second); - test(l::kilometer, v::kilometer_per_second); - test(l::hectometer, v::hectometer_per_second); - test(l::decameter, v::decameter_per_second); - test(l::meter, v::meter_per_second); - test(l::decimeter, v::decimeter_per_second); - test(l::centimeter, v::centimeter_per_second); - test(l::millimeter, v::millimeter_per_second); - test(l::micrometer, v::micrometer_per_second); - test(l::nanometer, v::nanometer_per_second); - test(l::picometer, v::picometer_per_second); - test(l::femtometer, v::femtometer_per_second); - test(l::attometer, v::attometer_per_second); - test(l::zeptometer, v::zeptometer_per_second); - test(l::yoctometer, v::yoctometer_per_second); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); - // TODO #17 Convert to == once PartialEq is implemented. - fn test, E: v::Conversion>(_l: L, v: E) { - Test::assert_eq(&V::one(), - &(Length::new::(V::one()) / Time::new::(V::one())).get(v)); + fn test, E: v::Conversion>() { + Test::assert_eq(&Velocity::new::(V::one()), + &(Length::new::(V::one()) / Time::new::(V::one()))); } } } diff --git a/src/si/volume.rs b/src/si/volume.rs index c8880b9e..86056662 100644 --- a/src/si/volume.rs +++ b/src/si/volume.rs @@ -81,40 +81,39 @@ mod tests { fn check_units() { // Values too large for f32. if TypeId::of::() == TypeId::of::() { - test(l::yottameter, v::cubic_yottameter); - test(l::zettameter, v::cubic_zettameter); - test(l::exameter, v::cubic_exameter); - test(l::petameter, v::cubic_petameter); + test::(); + test::(); + test::(); + test::(); } - test(l::terameter, v::cubic_terameter); - test(l::gigameter, v::cubic_gigameter); - test(l::megameter, v::cubic_megameter); - test(l::kilometer, v::cubic_kilometer); - test(l::hectometer, v::cubic_hectometer); - test(l::decameter, v::cubic_decameter); - test(l::meter, v::cubic_meter); - test(l::decimeter, v::cubic_decimeter); - test(l::centimeter, v::cubic_centimeter); - test(l::millimeter, v::cubic_millimeter); - test(l::micrometer, v::cubic_micrometer); - test(l::nanometer, v::cubic_nanometer); - test(l::picometer, v::cubic_picometer); - test(l::femtometer, v::cubic_femtometer); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); + test::(); // Values too small for f32. if TypeId::of::() == TypeId::of::() { - test(l::attometer, v::cubic_attometer); - test(l::zeptometer, v::cubic_zeptometer); - test(l::yoctometer, v::cubic_yoctometer); + test::(); + test::(); + test::(); } - // TODO #17 Convert to == once PartialEq is implemented. - fn test, O: v::Conversion>(_l: L, v: O) { - Test::assert_eq(&V::one(), + fn test, O: v::Conversion>() { + Test::assert_eq(&Volume::new::(V::one()), &(Length::new::(V::one()) * Length::new::(V::one()) - * Length::new::(V::one())).get(v)); + * Length::new::(V::one()))); } } } diff --git a/src/system.rs b/src/system.rs index b43d7a2b..1cf109da 100644 --- a/src/system.rs +++ b/src/system.rs @@ -787,6 +787,17 @@ macro_rules! system { } } + impl $crate::lib::cmp::PartialEq for Quantity + where + D: Dimension + ?Sized, + U: Units + ?Sized, + V: $crate::num::Num + $crate::Conversion, + { + fn eq(&self, other: &Self) -> bool { + self.value == other.value + } + } + impl $crate::lib::ops::Neg for Quantity where D: Dimension + ?Sized, @@ -837,6 +848,30 @@ macro_rules! system { } } + #[cfg(test)] + impl $crate::tests::Test for Quantity + where + D: Dimension + ?Sized, + U: Units + ?Sized, + V: $crate::num::Num + $crate::Conversion + $crate::tests::Test, + { + fn assert_eq(lhs: &Self, rhs: &Self) { + $crate::tests::Test::assert_eq(&lhs.value, &rhs.value); + } + + fn assert_approx_eq(lhs: &Self, rhs: &Self) { + $crate::tests::Test::assert_approx_eq(&lhs.value, &rhs.value); + } + + fn eq(lhs: &Self, rhs: &Self) -> bool { + $crate::tests::Test::eq(&lhs.value, &rhs.value) + } + + fn approx_eq(lhs: &Self, rhs: &Self) -> bool { + $crate::tests::Test::approx_eq(&lhs.value, &rhs.value) + } + } + impl $crate::num::Zero for Quantity where D: Dimension + ?Sized, diff --git a/src/tests.rs b/src/tests.rs index 0eec8d41..63ca9859 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -461,9 +461,9 @@ mod system_macro { #[allow(trivial_casts)] fn add(l: A, r: A) -> bool { - Test::eq(&(&*l + &*r), + Test::eq(&Length::new::(*l + *r), &(Length::new::((*l).clone()) - + Length::new::((*r).clone())).get(meter)) + + Length::new::((*r).clone()))) } #[allow(trivial_casts)] @@ -1013,7 +1013,8 @@ mod static_checks { use tests::*; - assert_impl!(q; Quantity, U, V>, Clone, Copy, Send, Sync); + assert_impl!(q; Quantity, U, V>, + Clone, Copy, PartialEq, Send, Sync); } storage_types! { @@ -1021,7 +1022,8 @@ mod static_checks { use tests::*; - assert_impl!(q; Quantity, U, V>, Clone, Copy, Send, Sync, ::lib::hash::Hash); + assert_impl!(q; Quantity, U, V>, + Clone, Copy, PartialEq, Send, Sync, ::lib::hash::Hash); } storage_types! { @@ -1029,6 +1031,7 @@ mod static_checks { use tests::*; - assert_impl!(q; Quantity, U, V>, Clone, Send, Sync, ::lib::hash::Hash); + assert_impl!(q; Quantity, U, V>, + Clone, PartialEq, Send, Sync, ::lib::hash::Hash); } }