Compare commits
2 Commits
5d8462c6e4
...
782e4a18a7
Author | SHA1 | Date |
---|---|---|
Juhani Krekelä | 782e4a18a7 | |
Juhani Krekelä | 5a1d6850e3 |
|
@ -85,6 +85,17 @@ fn get_conversion(unit: NonMetric) -> Conversion {
|
|||
from: inch_from * inch_from,
|
||||
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to * 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::SquareMetre },
|
||||
},
|
||||
// Volume
|
||||
NonMetric::CubicInch => Conversion {
|
||||
offset: 0.0,
|
||||
from: inch_from * inch_from * inch_from,
|
||||
to: MetricQuantity { amount: inch_to * inch_to * inch_to, unit: Metric::CubicMetre },
|
||||
},
|
||||
NonMetric::CubicFoot => Conversion {
|
||||
offset: 0.0,
|
||||
from: inch_from * inch_from * inch_from,
|
||||
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::CubicMetre },
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -144,6 +155,15 @@ mod test {
|
|||
run_tests(&tests, Metric::SquareMetre);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn volume() {
|
||||
let tests = [
|
||||
Test(NonMetric::CubicInch, 1.6387064e-5),
|
||||
Test(NonMetric::CubicFoot, 0.028316846592),
|
||||
];
|
||||
run_tests(&tests, Metric::CubicMetre);
|
||||
}
|
||||
|
||||
fn run_tests(tests: &[Test], unit: Metric) {
|
||||
for test in tests {
|
||||
let from = NonMetricQuantity {
|
||||
|
|
|
@ -95,6 +95,17 @@ fn prefixed_unit(quantity: MetricQuantity) -> PrefixedUnit {
|
|||
return PrefixedUnit(0.001 * 0.001, "mm²");
|
||||
}
|
||||
}
|
||||
Metric::CubicMetre => {
|
||||
if absolute >= 1000.0 * 1000.0 * 1000.0 {
|
||||
return PrefixedUnit(1000.0 * 1000.0 * 1000.0, "km³");
|
||||
} else if absolute >= 1.0 {
|
||||
return PrefixedUnit(1.0, "m³");
|
||||
} else if absolute >= 0.000_001 { // 0.01 * 0.01 * 0.01 rounds wrong
|
||||
return PrefixedUnit(0.000_001, "cm³");
|
||||
} else {
|
||||
return PrefixedUnit(0.001 * 0.001 * 0.001, "mm³");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -341,4 +352,44 @@ mod test {
|
|||
unit: Metric::SquareMetre,
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cubic_metres() {
|
||||
assert_eq!(PrefixedUnit(0.000_000_001, "mm³"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.000_000_000_1,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(0.000_000_001, "mm³"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.000_000_001,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(0.000_000_001, "mm³"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.000_000_1,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(0.000_001, "cm³"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.000_001,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(0.000_001, "cm³"), prefixed_unit(MetricQuantity {
|
||||
amount: 0.1,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(1.0, "m³"), prefixed_unit(MetricQuantity {
|
||||
amount: 1.0,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(1.0, "m³"), prefixed_unit(MetricQuantity {
|
||||
amount: 100_000_000.0,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(1_000_000_000.0, "km³"), prefixed_unit(MetricQuantity {
|
||||
amount: 1_000_000_000.0,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
assert_eq!(PrefixedUnit(1_000_000_000.0, "km³"), prefixed_unit(MetricQuantity {
|
||||
amount: 10_000_000_000.0,
|
||||
unit: Metric::CubicMetre,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -81,6 +81,9 @@ fn unit_to_name(unit: NonMetric) -> &'static str {
|
|||
NonMetric::SquareFoot => "square feet",
|
||||
NonMetric::Acre => "acres",
|
||||
NonMetric::SquareMile => "square miles",
|
||||
// Volume
|
||||
NonMetric::CubicInch => "cubic inches",
|
||||
NonMetric::CubicFoot => "cubic feet",
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -119,5 +122,8 @@ mod test {
|
|||
assert_eq!(run("1 ft²"), Ok("929 cm²".to_string()));
|
||||
assert_eq!(run("1 acre"), Ok("4 047 m²".to_string()));
|
||||
assert_eq!(run("1 mi²"), Ok("2.59 km²".to_string()));
|
||||
// Volume
|
||||
assert_eq!(run("1 in³"), Ok("16.39 cm³".to_string()));
|
||||
assert_eq!(run("1 ft³"), Ok("28 317 cm³".to_string()));
|
||||
}
|
||||
}
|
||||
|
|
54
src/parse.rs
54
src/parse.rs
|
@ -158,6 +158,33 @@ fn parse_unit(input: String) -> Result<NonMetric, ParseError> {
|
|||
"miles^2" => Ok(NonMetric::SquareMile),
|
||||
"mi^2" => Ok(NonMetric::SquareMile),
|
||||
|
||||
// Volume
|
||||
"cubic inch" => Ok(NonMetric::CubicInch),
|
||||
"cubic inches" => Ok(NonMetric::CubicInch),
|
||||
"cubic in" => Ok(NonMetric::CubicInch),
|
||||
"cu inch" => Ok(NonMetric::CubicInch),
|
||||
"cu inches" => Ok(NonMetric::CubicInch),
|
||||
"cu in" => Ok(NonMetric::CubicInch),
|
||||
"inch³" => Ok(NonMetric::CubicInch),
|
||||
"inches³" => Ok(NonMetric::CubicInch),
|
||||
"in³" => Ok(NonMetric::CubicInch),
|
||||
"inch^3" => Ok(NonMetric::CubicInch),
|
||||
"inches^3" => Ok(NonMetric::CubicInch),
|
||||
"in^3" => Ok(NonMetric::CubicInch),
|
||||
|
||||
"cubic foot" => Ok(NonMetric::CubicFoot),
|
||||
"cubic feet" => Ok(NonMetric::CubicFoot),
|
||||
"cubic ft" => Ok(NonMetric::CubicFoot),
|
||||
"cu foot" => Ok(NonMetric::CubicFoot),
|
||||
"cu feet" => Ok(NonMetric::CubicFoot),
|
||||
"cu ft" => Ok(NonMetric::CubicFoot),
|
||||
"foot³" => Ok(NonMetric::CubicFoot),
|
||||
"feet³" => Ok(NonMetric::CubicFoot),
|
||||
"ft³" => Ok(NonMetric::CubicFoot),
|
||||
"foot^3" => Ok(NonMetric::CubicFoot),
|
||||
"feet^3" => Ok(NonMetric::CubicFoot),
|
||||
"ft^3" => Ok(NonMetric::CubicFoot),
|
||||
|
||||
_ => Err(ParseError::UnknownUnit(input)),
|
||||
}
|
||||
}
|
||||
|
@ -365,6 +392,33 @@ mod test {
|
|||
assert_eq!(parse_unit("miles^2".to_string()), Ok(NonMetric::SquareMile));
|
||||
assert_eq!(parse_unit("mi^2".to_string()), Ok(NonMetric::SquareMile));
|
||||
|
||||
// Volume
|
||||
assert_eq!(parse_unit("cubic inch".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("cubic inches".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("cubic in".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("cu inch".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("cu inches".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("cu in".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("inch³".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("inches³".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("in³".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("inch^3".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("inches^3".to_string()), Ok(NonMetric::CubicInch));
|
||||
assert_eq!(parse_unit("in^3".to_string()), Ok(NonMetric::CubicInch));
|
||||
|
||||
assert_eq!(parse_unit("cubic foot".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("cubic feet".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("cubic ft".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("cu foot".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("cu feet".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("cu ft".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("foot³".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("feet³".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("ft³".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("foot^3".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("feet^3".to_string()), Ok(NonMetric::CubicFoot));
|
||||
assert_eq!(parse_unit("ft^3".to_string()), Ok(NonMetric::CubicFoot));
|
||||
|
||||
// Unknown unit
|
||||
assert_eq!(parse_unit("hutenosa".to_string()), Err(ParseError::UnknownUnit("hutenosa".to_string())));
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ pub enum Metric {
|
|||
Gram,
|
||||
Celsius,
|
||||
SquareMetre,
|
||||
CubicMetre,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
|
@ -24,6 +25,9 @@ pub enum NonMetric {
|
|||
SquareFoot,
|
||||
Acre,
|
||||
SquareMile,
|
||||
// Volume
|
||||
CubicInch,
|
||||
CubicFoot,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
|
|
Loading…
Reference in New Issue