Skip to content

Commit

Permalink
Implement PartialEq.
Browse files Browse the repository at this point in the history
Resolves #17.
  • Loading branch information
iliekturtles committed Jan 18, 2018
1 parent 8e55b23 commit f807ccd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## [Unreleased]

### Added
* [#17](https://github.com/iliekturtles/uom/issues/17) Implement `PartialEq`.
* [#26](https://github.com/iliekturtles/uom/issues/26) Implement `num::Zero`.
* [#35](https://github.com/iliekturtles/uom/issues/35) Implement `num::Saturating`.
* [#37](https://github.com/iliekturtles/uom/issues/35) Implement `serde::Serialize` and
Expand Down
18 changes: 18 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,24 @@ macro_rules! system {
}
}

impl<D, Ul, Ur, V> $crate::lib::cmp::PartialEq<Quantity<D, Ur, V>> for Quantity<D, Ul, V>
where
D: Dimension + ?Sized,
Ul: Units<V> + ?Sized,
Ur: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V>,
{
#[inline(always)]
fn eq(&self, other: &Quantity<D, Ur, V>) -> bool {
self.value == change_base::<D, Ul, Ur, V>(&other.value)
}

#[inline(always)]
fn ne(&self, other: &Quantity<D, Ur, V>) -> bool {
self.value != change_base::<D, Ul, Ur, V>(&other.value)
}
}

impl<D, Ul, Ur, V> $crate::lib::ops::Rem<Quantity<D, Ur, V>> for Quantity<D, Ul, V>
where
D: Dimension + ?Sized,
Expand Down
52 changes: 49 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,18 @@ mod system_macro {
&((*l).clone() / Length::new::<meter>((*r).clone())).value))
}

#[allow(trivial_casts)]
fn eq(l: A<V>, r: A<V>) -> bool {
(*l == *r)
== (Length::new::<meter>((*l).clone()) == Length::new::<meter>((*r).clone()))
}

#[allow(trivial_casts)]
fn ne(l: A<V>, r: A<V>) -> bool {
(*l != *r)
== (Length::new::<meter>((*l).clone()) != Length::new::<meter>((*r).clone()))
}

#[allow(trivial_casts)]
fn rem(l: A<V>, r: A<V>) -> TestResult {
if *r == V::zero() {
Expand Down Expand Up @@ -811,6 +823,37 @@ mod system_macro {
}

mod quantities_macro {
storage_types! {
use tests::*;

mod f { Q!(tests, super::V); }
mod k { Q!(tests, super::V, (kilometer, kilogram)); }

quickcheck! {
#[allow(trivial_casts)]
fn eq(l: A<V>, r: A<V>) -> bool {
let a = *l == *r;
let b = f::Length::new::<meter>((*l).clone())
== k::Length::new::<meter>((*r).clone());
let c = k::Length::new::<meter>((*l).clone())
== f::Length::new::<meter>((*r).clone());

a == b && a == c
}

#[allow(trivial_casts)]
fn ne(l: A<V>, r: A<V>) -> bool {
let a = *l != *r;
let b = f::Length::new::<meter>((*l).clone())
!= k::Length::new::<meter>((*r).clone());
let c = k::Length::new::<meter>((*l).clone())
!= f::Length::new::<meter>((*r).clone());

a == b && a == c
}
}
}

mod fractional {
storage_types! {
types: Float, Ratio;
Expand Down Expand Up @@ -1063,22 +1106,25 @@ mod static_checks {

use tests::*;

assert_impl!(q; Quantity<Q<Z0, Z0>, U<V>, V>, Clone, Copy, Send, Sync);
assert_impl!(q; Quantity<Q<Z0, Z0>, U<V>, V>,
Clone, Copy, PartialEq, Send, Sync);
}

storage_types! {
types: PrimInt, Rational, Rational32, Rational64;

use tests::*;

assert_impl!(q; Quantity<Q<Z0, Z0>, U<V>, V>, Clone, Copy, Send, Sync, ::lib::hash::Hash);
assert_impl!(q; Quantity<Q<Z0, Z0>, U<V>, V>,
Clone, Copy, PartialEq, Send, Sync, ::lib::hash::Hash);
}

storage_types! {
types: BigInt, BigUint, BigRational;

use tests::*;

assert_impl!(q; Quantity<Q<Z0, Z0>, U<V>, V>, Clone, Send, Sync, ::lib::hash::Hash);
assert_impl!(q; Quantity<Q<Z0, Z0>, U<V>, V>,
Clone, PartialEq, Send, Sync, ::lib::hash::Hash);
}
}

0 comments on commit f807ccd

Please sign in to comment.