Skip to content

Commit e720331

Browse files
committed
add time formatting test
1 parent 86ba0d6 commit e720331

File tree

1 file changed

+62
-12
lines changed

1 file changed

+62
-12
lines changed

dot15d4-frame/src/time.rs

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

1212
impl Instant {
13+
/// Create a new `Instant` from microseconds since the epoch.
1314
pub const fn from_us(us: i64) -> Self {
1415
Self { us }
1516
}
1617

18+
/// Returns the point in time as microseconds since the epoch.
1719
pub const fn as_us(&self) -> i64 {
1820
self.us
1921
}
2022
}
2123

22-
impl core::fmt::Display for Instant {
23-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24-
write!(f, "{:.2}ms", self.as_us() as f32 / 1000.0)
25-
}
26-
}
27-
2824
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
2925
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
3026
pub struct Duration(i64);
3127

3228
impl Duration {
29+
/// Create a new `Duration` from microseconds.
3330
pub const fn from_us(us: i64) -> Self {
3431
Self(us)
3532
}
3633

34+
/// Returns the duration as microseconds.
3735
pub const fn as_us(&self) -> i64 {
3836
self.0
3937
}
4038
}
4139

42-
impl core::fmt::Display for Duration {
43-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
44-
write!(f, "{:.2}ms", self.as_us() as f32 / 1000.0)
45-
}
46-
}
47-
4840
impl core::ops::Sub for Instant {
4941
type Output = Self;
5042

@@ -100,3 +92,61 @@ impl core::ops::Add<Duration> for Duration {
10092
Self::from_us(self.as_us() + rhs.as_us())
10193
}
10294
}
95+
96+
impl core::fmt::Display for Instant {
97+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
98+
write!(f, "{:.2}ms", self.as_us() as f32 / 1000.0)
99+
}
100+
}
101+
102+
impl core::fmt::Display for Duration {
103+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
104+
write!(f, "{:.2}ms", self.as_us() as f32 / 1000.0)
105+
}
106+
}
107+
108+
#[cfg(test)]
109+
mod tests {
110+
use super::*;
111+
112+
#[test]
113+
fn instant() {
114+
let a = Instant::from_us(100);
115+
assert_eq!(a.us, 100);
116+
assert_eq!(a.as_us(), 100);
117+
}
118+
119+
#[test]
120+
fn instant_operations() {
121+
let a = Instant::from_us(100);
122+
let b = Instant::from_us(50);
123+
assert_eq!((a - b).as_us(), 50);
124+
assert_eq!((a - Duration::from_us(50)).as_us(), 50);
125+
assert_eq!((a + Duration::from_us(50)).as_us(), 150);
126+
}
127+
128+
#[test]
129+
fn duration() {
130+
let a = Duration::from_us(100);
131+
assert_eq!(a.0, 100);
132+
assert_eq!(a.as_us(), 100);
133+
}
134+
135+
#[test]
136+
fn duration_operations() {
137+
let a = Duration::from_us(100);
138+
let b = Duration::from_us(50);
139+
assert_eq!((a - b).as_us(), 50);
140+
assert_eq!((a * 2).as_us(), 200);
141+
assert_eq!((a / 2).as_us(), 50);
142+
assert_eq!((a + b).as_us(), 150);
143+
}
144+
145+
#[test]
146+
fn formatting() {
147+
let a = Instant::from_us(100);
148+
let b = Duration::from_us(100);
149+
assert_eq!(format!("{}", a), "0.10ms");
150+
assert_eq!(format!("{}", b), "0.10ms");
151+
}
152+
}

0 commit comments

Comments
 (0)