Skip to content

Commit

Permalink
Create Test trait to do type specific comparisons.
Browse files Browse the repository at this point in the history
Handles equality and approximate equality for all underlying storage
types. Tests don't need to be type specific or include approximation
logic for floating point numbers. Part of #29.
  • Loading branch information
iliekturtles committed Dec 21, 2017
1 parent fcfd9f6 commit 7058f33
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 281 deletions.
11 changes: 5 additions & 6 deletions src/si/acceleration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ quantity! {
#[cfg(test)]
mod tests {
storage_types! {
types: Float;

use num::One;
use si::quantities::*;
use si::acceleration as a;
use si::length as l;
use si::time as t;
use tests::Test;

#[test]
fn check_dimension() {
Expand Down Expand Up @@ -101,10 +100,10 @@ mod tests {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<L: l::Conversion<V>, A: a::Conversion<V>>(_l: L, a: A) {
assert_eq!(V::one(),
(Length::new::<L>(V::one()) /
(Time::new::<t::second>(V::one()) * Time::new::<t::second>(V::one())))
.get(a));
Test::assert_eq(&V::one(),
&(Length::new::<L>(V::one()) /
(Time::new::<t::second>(V::one()) * Time::new::<t::second>(V::one())))
.get(a));
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/si/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ quantity! {
#[cfg(test)]
mod tests {
storage_types! {
types: Float;

use stdlib::any::TypeId;
use num::{Float, One};
use num::One;
use si::quantities::*;
use si::area as a;
use si::length as l;
use tests::Test;

#[test]
fn check_dimension() {
Expand Down Expand Up @@ -108,9 +107,8 @@ mod tests {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<L: l::Conversion<V>, A: a::Conversion<V>>(_l: L, a: A) {
assert_ulps_eq!(V::one(),
(Length::new::<L>(V::one()) * Length::new::<L>(V::one())).get(a),
epsilon = V::epsilon());
Test::assert_eq(&V::one(),
&(Length::new::<L>(V::one()) * Length::new::<L>(V::one())).get(a));
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/si/force.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ quantity! {
#[cfg(test)]
mod tests {
storage_types! {
types: Float;

use num::{Float, One};
use num::One;
use si::quantities::*;
use si::force as f;
use si::length as l;
use si::mass as m;
use si::time as t;
use tests::Test;

#[test]
fn check_dimension() {
Expand Down Expand Up @@ -121,10 +120,9 @@ mod tests {
_t: T,
f: F
) {
assert_ulps_eq!(V::one(), ((Mass::new::<M>(V::one())
Test::assert_approx_eq(&V::one(), &((Mass::new::<M>(V::one())
* Length::new::<L>(V::one()))
/ (Time::new::<T>(V::one()) * Time::new::<T>(V::one()))).get(f),
epsilon = V::epsilon());
/ (Time::new::<T>(V::one()) * Time::new::<T>(V::one()))).get(f));
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/si/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ quantity! {
#[cfg(test)]
mod tests {
storage_types! {
types: Float;

use num::{Float, One};
use num::One;
use si::quantities::*;
use si::frequency as f;
use si::time as t;
use tests::Test;

#[test]
fn check_dimension() {
Expand Down Expand Up @@ -80,10 +79,10 @@ mod tests {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<T: t::Conversion<V>, F: f::Conversion<V>>(t: T, f: F) {
assert_ulps_eq!((V::one() / Time::new::<T>(V::one())).get(f),
Frequency::new::<F>(V::one()).get(f), epsilon = V::epsilon());
assert_ulps_eq!(Time::new::<T>(V::one()).get(t),
(V::one() / Frequency::new::<F>(V::one())).get(t), epsilon = V::epsilon());
Test::assert_approx_eq(&(V::one() / Time::new::<T>(V::one())).get(f),
&Frequency::new::<F>(V::one()).get(f));
Test::assert_approx_eq(&Time::new::<T>(V::one()).get(t),
&(V::one() / Frequency::new::<F>(V::one())).get(t));
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/si/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ mod tests {

use si::quantities::*;
use si::length::meter;
use tests::Test;

quickcheck! {
#[allow(trivial_casts)]
fn hypot(l: V, r: V) -> bool {
l.hypot(r) == Length::new::<meter>(l).hypot(Length::new::<meter>(r)).get(meter)
Test::eq(&l.hypot(r),
&Length::new::<meter>(l).hypot(Length::new::<meter>(r)).get(meter))
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/si/velocity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ quantity! {
#[cfg(test)]
mod test {
storage_types! {
types: Float;

use num::One;
use si::quantities::*;
use si::length as l;
use si::time as t;
use si::velocity as v;
use tests::Test;

#[test]
fn check_dimension() {
let _: Velocity<V> = Length::new::<l::meter>(V::one()) / Time::new::<t::second>(V::one());
let _: Velocity<V> = Length::new::<l::meter>(V::one())
/ Time::new::<t::second>(V::one());
}

#[test]
Expand Down Expand Up @@ -99,8 +99,8 @@ mod test {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<L: l::Conversion<V>, E: v::Conversion<V>>(_l: L, v: E) {
assert_eq!(V::one(),
(Length::new::<L>(V::one()) / Time::new::<t::second>(V::one())).get(v));
Test::assert_eq(&V::one(),
&(Length::new::<L>(V::one()) / Time::new::<t::second>(V::one())).get(v));
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/si/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ quantity! {
#[cfg(test)]
mod tests {
storage_types! {
types: Float;

use stdlib::any::TypeId;
use num::{Float, One};
use num::One;
use si::quantities::*;
use si::area as a;
use si::volume as v;
use si::length as l;
use tests::Test;

#[test]
fn check_dimension() {
Expand Down Expand Up @@ -112,11 +111,10 @@ mod tests {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<L: l::Conversion<V>, O: v::Conversion<V>>(_l: L, v: O) {
assert_ulps_eq!(V::one(),
(Length::new::<L>(V::one())
Test::assert_eq(&V::one(),
&(Length::new::<L>(V::one())
* Length::new::<L>(V::one())
* Length::new::<L>(V::one())).get(v),
epsilon = V::epsilon());
* Length::new::<L>(V::one())).get(v));
}
}
}
Expand Down
Loading

0 comments on commit 7058f33

Please sign in to comment.