Skip to content

Commit

Permalink
Enable and correct tests for all underlying storage types.
Browse files Browse the repository at this point in the history
Part of #29.
  • Loading branch information
iliekturtles committed Dec 14, 2017
1 parent 260455d commit 3da3c6f
Show file tree
Hide file tree
Showing 11 changed files with 851 additions and 633 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ clippy = { version = "^0", optional = true }
compiletest_rs = { version = "^0", optional = true }

[dev-dependencies]
quickcheck = "0.4.1"
quickcheck = "0.5.0"
approx = "0.1.1"

[features]
Expand Down
6 changes: 3 additions & 3 deletions src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,17 @@ macro_rules! quantity {
$quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: super::to_base::<Dimension, U, V, N>(v),
value: super::to_base::<Dimension, U, V, N>(&v),
}
}

/// Retrieve the value of the quantity in the given measurement unit.
#[inline(always)]
pub fn get<N>(self, _unit: N) -> V
pub fn get<N>(&self, _unit: N) -> V
where
N: Unit + $crate::Conversion<V, T = V::T>,
{
super::from_base::<Dimension, U, V, N>(self.value)
super::from_base::<Dimension, U, V, N>(&self.value)
}

/// Returns the largest integer less than or equal to a number in the given
Expand Down
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
16 changes: 8 additions & 8 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ macro_rules! system {

/// Convert a value from base units to the given unit.
#[inline(always)]
fn from_base<D, U, V, N>(v: V) -> V
fn from_base<D, U, V, N>(v: &V) -> V
where
D: Dimension + ?Sized,
U: Units<V> + ?Sized,
Expand All @@ -242,7 +242,7 @@ macro_rules! system {

/// Convert a value from the given unit to base units.
#[inline(always)]
fn to_base<D, U, V, N>(v: V) -> V
fn to_base<D, U, V, N>(v: &V) -> V
where
D: Dimension + ?Sized,
U: Units<V> + ?Sized,
Expand All @@ -260,7 +260,7 @@ macro_rules! system {

/// Convert a value from one set of base units to a second.
#[inline(always)]
fn change_base<D, Ul, Ur, V>(v: V) -> V
fn change_base<D, Ul, Ur, V>(v: &V) -> V
where
D: Dimension + ?Sized,
Ul: Units<V> + ?Sized,
Expand Down Expand Up @@ -362,7 +362,7 @@ macro_rules! system {
Quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self.value $addsub_op change_base::<D, Ul, Ur, V>(rhs.value),
value: self.value $addsub_op change_base::<D, Ul, Ur, V>(&rhs.value),
}
}
}
Expand All @@ -378,7 +378,7 @@ macro_rules! system {
{
#[inline(always)]
fn $addsubassign_fun(&mut self, rhs: Quantity<D, Ur, V>) {
self.value $addsubassign_op change_base::<D, Ul, Ur, V>(rhs.value);
self.value $addsubassign_op change_base::<D, Ul, Ur, V>(&rhs.value);
}
}

Expand All @@ -403,7 +403,7 @@ macro_rules! system {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self.value
$muldiv_op change_base::<Dr, Ul, Ur, V>(rhs.value),
$muldiv_op change_base::<Dr, Ul, Ur, V>(&rhs.value),
}
}
}
Expand Down Expand Up @@ -770,7 +770,7 @@ macro_rules! system {
Quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self.value % change_base::<D, Ul, Ur, V>(rhs.value)
value: self.value % change_base::<D, Ul, Ur, V>(&rhs.value)
}
}
}
Expand All @@ -785,7 +785,7 @@ macro_rules! system {
{
#[inline(always)]
fn rem_assign(&mut self, rhs: Quantity<D, Ur, V>) {
self.value %= change_base::<D, Ul, Ur, V>(rhs.value)
self.value %= change_base::<D, Ul, Ur, V>(&rhs.value)
}
}

Expand Down
Loading

0 comments on commit 3da3c6f

Please sign in to comment.