1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// ---------------------------------------------------------------------------
// Copyright:   (c) 2022 ff. Michael Amrhein (michael@adrhinum.de)
// License:     This program is part of a larger application. For license
//              details please read the file LICENSE.TXT provided together
//              with the application.
// ---------------------------------------------------------------------------
// $Source: src/speed.rs $
// $Revision: 2022-04-16T08:18:48+02:00 $

//! Definition of derived quantity `Speed`.

use crate::{duration::Duration, length::Length, prelude::*};

#[quantity(Length / Duration)]
#[ref_unit(Meter_per_Second, "m/s", NONE, "Reference unit of quantity `Speed`")]
#[unit(Kilometer_per_Hour, "km/h", 0.2777777777777778, "km/h")]
#[unit(Miles_per_Hour, "mph", 0.44704, "mi/h")]
/// Magnitude of the change of an objects position per unit of time
///
/// Definition: Length/Duration
///
/// Reference unit: Meter per Second ('m/s')
///
/// Predefined units:
///
/// | Symbol | Name                  | Definition        | Equivalent in 'm/s' |
/// |--------|-----------------------|-------------------|---------------------|
/// | km/h   | Kilometer per Hour    | km/h              | 5/18                |
/// | mph    | Miles per Hour        | mi/h              | 0.44704             |
pub struct Speed {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        assert_almost_eq,
        duration::{MINUTE, SECOND},
        length::{KILOMETER, METER, MILE},
    };

    #[test]
    fn test_speed() {
        assert_eq!(<Speed as HasRefUnit>::REF_UNIT, SpeedUnit::REF_UNIT);
        assert!(METER_PER_SECOND.is_ref_unit());
        let amnt: AmountT = Amnt!(235.4);
        let v = amnt * KILOMETER_PER_HOUR;
        assert_eq!(v.amount(), amnt);
        assert_eq!(v.unit(), KILOMETER_PER_HOUR);
        #[cfg(feature = "std")]
        assert_eq!(v.to_string(), "235.4 km/h");
    }

    #[test]
    fn test_length_div_duration() {
        let al: AmountT = Amnt!(35.1);
        let l = al * MILE;
        let at: AmountT = Amnt!(13.);
        let t = at * MINUTE;
        let v = l / t;
        assert_almost_eq!(v.amount(), al / at * Amnt!(0.44704) * Amnt!(60.));
        assert_eq!(v.unit(), METER_PER_SECOND);
    }

    #[test]
    fn test_speed_mul_duration() {
        let av: AmountT = Amnt!(2.94);
        let v = av * METER_PER_SECOND;
        let at = Amnt!(0.7);
        let t = at * MINUTE;
        let l = v * t;
        assert_almost_eq!(l.amount(), av * at * Amnt!(60.));
        assert_eq!(l.unit(), METER);
    }

    #[test]
    fn test_length_div_speed() {
        let al = Amnt!(0.7);
        let l = al * KILOMETER;
        let av: AmountT = Amnt!(2.94);
        let v = av * METER_PER_SECOND;
        let t = l / v;
        assert_almost_eq!(t.amount(), al * Amnt!(1000.) / av);
        assert_eq!(t.unit(), SECOND);
    }
}