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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*----------------------------------------------------------------------------------------------------------
 *  Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/piot/tick-id-rs
 *  Licensed under the MIT License. See LICENSE in the project root for license information.
 *--------------------------------------------------------------------------------------------------------*/
//! The value type TickId that specifies a specific tick in a simulation.
//!
//! Usually a tick is 16 ms, but can be any integer period of time.

use core::fmt;
use std::ops::{Add, AddAssign, Sub, SubAssign};

#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct TickId(pub u32);

impl fmt::Display for TickId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TickId: {}", self.0)
    }
}

impl TickId {
    pub fn new(value: u32) -> Self {
        TickId(value)
    }

    pub fn value(&self) -> u32 {
        self.0
    }
}

impl Add<u32> for TickId {
    type Output = TickId;

    fn add(self, other: u32) -> TickId {
        if self.0.checked_add(other).is_none() {
            panic!("tick overflow. tried to do {} + {}", self.0, other)
        }
        TickId(self.0 + other)
    }
}

impl AddAssign<u32> for TickId {
    fn add_assign(&mut self, other: u32) {
        self.0 += other;
    }
}

impl Sub<u32> for TickId {
    type Output = TickId;

    fn sub(self, other: u32) -> TickId {
        if other > self.0 {
            panic!("tick underflow. tried to do {} - {}", self.0, other)
        }
        Self(self.0 - other)
    }
}

impl SubAssign<u32> for TickId {
    fn sub_assign(&mut self, other: u32) {
        self.0 -= other;
    }
}

impl Sub<TickId> for TickId {
    type Output = i32;

    fn sub(self, other: TickId) -> i32 {
        (self.0 - other.0) as i32
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tick_id_display() {
        let tick_id = TickId(42);
        let result = tick_id.to_string();
        assert_eq!(result, "TickId: 42");

        println!("output: {}", result);
    }

    #[test]
    fn test_add() {
        let first = TickId(42);
        let result = first + 99;
        assert_eq!(result, TickId(141));
        assert_eq!(result.to_string(), "TickId: 141");

        println!("output: {}", result);
    }


    #[test]
    fn test_sub() {
        let first = TickId(12414);
        let second = TickId(1144);
        let result = first - second;
        assert_eq!(result, 11270);
    }

    #[test]
    fn test_greater() {
        let first = TickId(12414);
        let second = TickId(1144);
        assert_eq!(first > second, true);
    }

    #[test]
    fn test_less() {
        let first = TickId(12414);
        let second = TickId(1144);
        assert_eq!(first < second, false);
        assert_eq!(second < first, true);
    }

    #[test]
    fn test_less_or_equal() {
        let first = TickId(1144);
        let second = TickId(1144);
        assert_eq!(first <= second, true);
    }

    #[test]
    fn test_add_assign() {
        let mut first = TickId(1144);
        first += 1;
        assert_eq!(first.value(), 1145);
    }

    #[test]
    fn test_sub_assign() {
        let mut first = TickId(1144);
        first -= 1;
        assert_eq!(first.value(), 1143);
    }


    #[test]
    fn test_new() {
        let tick_id = TickId::new(12414);
        assert_eq!(tick_id.value(), 12414);
    }

    #[test]
    #[should_panic]
    fn test_sub_underflow() {
        let first = TickId(42);
        let second = TickId(99);
        let _ = first - second;
    }

    #[test]
    #[should_panic]
    fn test_add_overflow() {
        let first = TickId(u32::MAX);
        let _ = first + 1;
    }
}