Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

Added this Changelog.

### Changed

- Merged in [#36](https://github.com/rust-embedded-community/rust-measurements/pull/36) to adjust bounds on `Measurements::pick_appropriate_units()`, which changes the return value for cases when the value is 1.0.

## [0.10.2]

Merged in [#17](https://github.com/thejpster/rust-measurements/pull/17) to add:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["James O'Cull <[email protected]>",
"Jonathan Pallant <[email protected]>",
"Hannah McLaughlin <[email protected]>",
"Danilo Bargen <[email protected]>",
"Tim Alberdingk Thijm <[email protected]>",
"Tim Alberdingk Thijm <tthijm@cs.princeton.edu>",
]
documentation = "https://docs.rs/crate/measurements"
repository = "https://github.com/thejpster/rust-measurements"
Expand Down
2 changes: 1 addition & 1 deletion src/measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub trait Measurement {
fn pick_appropriate_units(&self, list: &[(&'static str, f64)]) -> (&'static str, f64) {
for &(unit, ref scale) in list.iter().rev() {
let value = self.as_base_units() / scale;
if value > 1.0 || value < -1.0 {
if value >= 1.0 || value <= -1.0 {
return (unit, value);
}
}
Expand Down
52 changes: 52 additions & 0 deletions tests/get_appropriate_units.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
extern crate measurements;

use measurements::{mass::Mass, test_utils::assert_almost_eq, Measurement};

// Macro for testing `get_appropriate_units()`.
// Specify the name of the test, the initial value (in kg) to be
// passed in and the units that should be returned when
// `get_appropriate_units()` is called.
// An additional factor term may be given for cases when a
// unit conversion takes place.
macro_rules! test_from_kg {
($name:ident, $value:expr, $unit:expr) => {
#[test]
fn $name() {
let mass = Mass::from_kilograms($value);
let (unit, v) = mass.get_appropriate_units();
assert_eq!(unit, $unit);
assert_almost_eq($value, v);
}
};
($name:ident, $value:expr, $unit:expr, $factor:expr) => {
#[test]
fn $name() {
let mass = Mass::from_kilograms($value);
let (unit, v) = mass.get_appropriate_units();
assert_eq!(unit, $unit);
assert_almost_eq($value * $factor, v);
}
};
}

test_from_kg!(one_kg_keeps_unit, 1.0, "kg");
test_from_kg!(minus_one_kg_keeps_unit, -1.0, "kg");
test_from_kg!(more_than_one_kg_keeps_unit, 1.0 + 0.001, "kg");
test_from_kg!(
less_than_one_kg_changes_unit_to_grams,
1.0 - 0.001,
"g",
1000.0
);
test_from_kg!(
one_thousand_kg_changes_unit_to_tonnes,
1000.0,
"tonnes",
0.001
);
test_from_kg!(
one_thousandth_of_kg_changes_unit_to_grams,
0.001,
"g",
1000.0
);