Skip to content

Commit 86ba0d6

Browse files
committed
dot15d4-frame: add tests for time module
1 parent 8de13dd commit 86ba0d6

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

dot15d4/src/time.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,33 @@ pub struct Instant {
1010
}
1111

1212
impl Instant {
13-
pub const fn now() -> Self {
14-
Self { us: 0 }
15-
}
16-
13+
/// Create a new `Instant` from microseconds since the epoch.
1714
pub const fn from_us(us: i64) -> Self {
1815
Self { us }
1916
}
2017

18+
/// Returns the point in time as microseconds since the epoch.
2119
pub const fn as_us(&self) -> i64 {
2220
self.us
2321
}
2422
}
2523

26-
impl core::fmt::Display for Instant {
27-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28-
write!(f, "{:.2}ms", self.as_us() as f32 / 1000.0)
29-
}
30-
}
31-
3224
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
3325
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
3426
pub struct Duration(i64);
3527

3628
impl Duration {
29+
/// Create a new `Duration` from microseconds.
3730
pub const fn from_us(us: i64) -> Self {
3831
Self(us)
3932
}
4033

34+
/// Returns the duration as microseconds.
4135
pub const fn as_us(&self) -> i64 {
4236
self.0
4337
}
4438
}
4539

46-
impl core::fmt::Display for Duration {
47-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48-
write!(f, "{:.2}ms", self.as_us() as f32 / 1000.0)
49-
}
50-
}
51-
5240
impl core::ops::Sub for Instant {
5341
type Output = Self;
5442

@@ -104,3 +92,41 @@ impl core::ops::Add<Duration> for Duration {
10492
Self::from_us(self.as_us() + rhs.as_us())
10593
}
10694
}
95+
96+
#[cfg(test)]
97+
mod tests {
98+
use super::*;
99+
100+
#[test]
101+
fn instant() {
102+
let a = Instant::from_us(100);
103+
assert_eq!(a.us, 100);
104+
assert_eq!(a.as_us(), 100);
105+
}
106+
107+
#[test]
108+
fn instant_operations() {
109+
let a = Instant::from_us(100);
110+
let b = Instant::from_us(50);
111+
assert_eq!((a - b).as_us(), 50);
112+
assert_eq!((a - Duration::from_us(50)).as_us(), 50);
113+
assert_eq!((a + Duration::from_us(50)).as_us(), 150);
114+
}
115+
116+
#[test]
117+
fn duration() {
118+
let a = Duration::from_us(100);
119+
assert_eq!(a.0, 100);
120+
assert_eq!(a.as_us(), 100);
121+
}
122+
123+
#[test]
124+
fn duration_operations() {
125+
let a = Duration::from_us(100);
126+
let b = Duration::from_us(50);
127+
assert_eq!((a - b).as_us(), 50);
128+
assert_eq!((a * 2).as_us(), 200);
129+
assert_eq!((a / 2).as_us(), 50);
130+
assert_eq!((a + b).as_us(), 150);
131+
}
132+
}

0 commit comments

Comments
 (0)