Skip to main content

quantities/
volume.rs

1// ---------------------------------------------------------------------------
2// Copyright:   (c) 2022 ff. Michael Amrhein (michael@adrhinum.de)
3// License:     This program is part of a larger application. For license
4//              details please read the file LICENSE.TXT provided together
5//              with the application.
6// ---------------------------------------------------------------------------
7// $Source: src/volume.rs $
8// $Revision: 2022-04-16T08:18:48+02:00 $
9
10//! Definition of derived quantity `Volume`.
11
12use crate::{area::Area, length::Length, prelude::*};
13
14#[quantity(Length * Area)]
15#[ref_unit(Cubic_Meter, "m³", NONE, "Reference unit of quantity `Volume`")]
16#[unit(Cubic_Millimeter, "mm³", NANO, 0.000000001, "mm³")]
17#[unit(Cubic_Centimeter, "cm³", MICRO, 0.000001, "cm³")]
18#[unit(Milliliter, "ml", MICRO, 0.000001, "0.001·l")]
19#[unit(Centiliter, "cl", 0.00001, "0.01·l")]
20#[unit(Cubic_Inch, "in³", 0.000016387064, "in³")]
21#[unit(Deciliter, "dl", 0.0001, "0.1·l")]
22#[unit(Cubic_Decimeter, "dm³", MILLI, 0.001, "dm³")]
23#[unit(Liter, "l", MILLI, 0.001, "0.001·m³")]
24#[unit(Cubic_Foot, "ft³", 0.028316846592, "ft³")]
25#[unit(Cubic_Yard, "yd³", 0.764554857984, "yd³")]
26#[unit(Cubic_Kilometer, "km³", GIGA, 1000000000, "km³")]
27/// The quantity expressing the amount of three-dimensional space enclosed by a
28/// closed surface.
29///
30/// Definition: Length³
31///
32/// Reference unit: Cubic Meter ('m³')
33///
34/// Predefined units:
35///
36/// | Symbol | Name                  | Definition        | Equivalent in 'm³'  |
37/// |--------|-----------------------|-------------------|---------------------|
38/// | mm³    | Cubic Millimeter      | mm³               | 0.000000001         |
39/// | cm³    | Cubic Centimeter      | cm³               | 0.000001            |
40/// | ml     | Milliliter            | 0.001·l           | 0.000001            |
41/// | cl     | Centiliter            | 0.01·l            | 0.00001             |
42/// | in³    | Cubic Inch            | in³               | 0.000016387064      |
43/// | dl     | Deciliter             | 0.1·l             | 0.0001              |
44/// | dm³    | Cubic Decimeter       | dm³               | 0.001               |
45/// | l      | Liter                 | 0.001·m³          | 0.001               |
46/// | ft³    | Cubic Foot            | ft³               | 0.028316846592      |
47/// | yd³    | Cubic Yard            | yd³               | 0.764554857984      |
48/// | km³    | Cubic Kilometer       | km³               | 1000000000          |
49pub struct Volume {}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use crate::{
55        area::{SQUARE_KILOMETER, SQUARE_METER},
56        assert_almost_eq,
57        length::{DECIMETER, MILLIMETER},
58    };
59
60    #[test]
61    fn test_volume() {
62        assert_eq!(<Volume as HasRefUnit>::REF_UNIT, VolumeUnit::REF_UNIT);
63        assert!(CUBIC_METER.is_ref_unit());
64        let amnt: AmountT = Amnt!(29.305);
65        let v = amnt * CUBIC_DECIMETER;
66        assert_eq!(v.amount(), amnt);
67        assert_eq!(v.unit(), CUBIC_DECIMETER);
68        #[cfg(feature = "std")]
69        assert_eq!(v.to_string(), "29.305 dm³");
70    }
71
72    #[test]
73    fn test_length_mul_area() {
74        let amnt: AmountT = Amnt!(2.1);
75        let l = amnt * DECIMETER;
76        let a = l * l;
77        let v = l * a;
78        assert_almost_eq!(v.amount(), amnt * amnt * amnt);
79        assert_eq!(v.unit(), CUBIC_DECIMETER);
80        let b = Amnt!(0.02) * SQUARE_KILOMETER;
81        let h = amnt * DECIMETER;
82        let v = b * h;
83        assert_almost_eq!(v.amount(), Amnt!(2000.) * amnt);
84        assert_eq!(v.unit(), CUBIC_METER);
85    }
86
87    #[test]
88    fn test_volume_div_length() {
89        let amnt: AmountT = Amnt!(-0.42);
90        let v = amnt * LITER;
91        let a = Amnt!(0.7) * SQUARE_METER;
92        let h = v / a;
93        assert_almost_eq!(h.amount(), v.amount() / a.amount());
94        assert_eq!(h.unit(), MILLIMETER);
95    }
96}