ddp_rs/protocol/
timecode.rs1#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Default)]
2pub struct TimeCode(pub Option<u32>);
3
4impl TimeCode {
5 pub fn from_4_bytes(bytes: [u8; 4]) -> Self {
6 TimeCode(Some(u32::from_be_bytes(bytes)))
7 }
8
9 pub fn to_bytes(&self) -> [u8; 4] {
10 self.0.unwrap_or(0u32).to_be_bytes()
11 }
12}
13
14#[cfg(test)]
15mod tests {
16 use super::*;
17
18 #[test]
19 fn test_timecode_default() {
20 let tc = TimeCode::default();
21 assert_eq!(tc.0, None);
22 }
23
24 #[test]
25 fn test_timecode_from_4_bytes() {
26 let bytes = [0x00, 0x00, 0x30, 0x39]; let tc = TimeCode::from_4_bytes(bytes);
28 assert_eq!(tc.0, Some(12345));
29 }
30
31 #[test]
32 fn test_timecode_from_4_bytes_zero() {
33 let bytes = [0x00, 0x00, 0x00, 0x00];
34 let tc = TimeCode::from_4_bytes(bytes);
35 assert_eq!(tc.0, Some(0));
36 }
37
38 #[test]
39 fn test_timecode_from_4_bytes_max() {
40 let bytes = [0xFF, 0xFF, 0xFF, 0xFF];
41 let tc = TimeCode::from_4_bytes(bytes);
42 assert_eq!(tc.0, Some(u32::MAX));
43 }
44
45 #[test]
46 fn test_timecode_to_bytes_some() {
47 let tc = TimeCode(Some(12345));
48 let bytes = tc.to_bytes();
49 assert_eq!(bytes, [0x00, 0x00, 0x30, 0x39]);
50 }
51
52 #[test]
53 fn test_timecode_to_bytes_none() {
54 let tc = TimeCode(None);
55 let bytes = tc.to_bytes();
56 assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00]);
57 }
58
59 #[test]
60 fn test_timecode_to_bytes_zero() {
61 let tc = TimeCode(Some(0));
62 let bytes = tc.to_bytes();
63 assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00]);
64 }
65
66 #[test]
67 fn test_timecode_to_bytes_max() {
68 let tc = TimeCode(Some(u32::MAX));
69 let bytes = tc.to_bytes();
70 assert_eq!(bytes, [0xFF, 0xFF, 0xFF, 0xFF]);
71 }
72
73 #[test]
74 fn test_timecode_roundtrip() {
75 let original = TimeCode(Some(98765));
76 let bytes = original.to_bytes();
77 let roundtrip = TimeCode::from_4_bytes(bytes);
78 assert_eq!(original, roundtrip);
79 }
80
81 #[test]
82 fn test_timecode_roundtrip_various_values() {
83 let test_values = vec![0, 1, 255, 256, 65535, 65536, 16777215, 16777216, u32::MAX];
84
85 for value in test_values {
86 let original = TimeCode(Some(value));
87 let bytes = original.to_bytes();
88 let roundtrip = TimeCode::from_4_bytes(bytes);
89 assert_eq!(original, roundtrip, "Failed roundtrip for value {}", value);
90 }
91 }
92
93 #[test]
94 fn test_timecode_equality() {
95 let tc1 = TimeCode(Some(100));
96 let tc2 = TimeCode(Some(100));
97 let tc3 = TimeCode(Some(200));
98
99 assert_eq!(tc1, tc2);
100 assert_ne!(tc1, tc3);
101 }
102
103 #[test]
104 fn test_timecode_clone() {
105 let tc1 = TimeCode(Some(12345));
106 let tc2 = tc1.clone();
107 assert_eq!(tc1, tc2);
108 }
109
110 #[test]
111 fn test_timecode_copy() {
112 let tc1 = TimeCode(Some(12345));
113 let tc2 = tc1; assert_eq!(tc1, tc2);
115 assert_eq!(tc1.0, Some(12345)); }
117
118 #[test]
119 fn test_timecode_debug_format() {
120 let tc = TimeCode(Some(12345));
121 let debug_str = format!("{:?}", tc);
122 assert_eq!(debug_str, "TimeCode(Some(12345))");
123 }
124
125 #[test]
126 fn test_timecode_none_debug_format() {
127 let tc = TimeCode(None);
128 let debug_str = format!("{:?}", tc);
129 assert_eq!(debug_str, "TimeCode(None)");
130 }
131
132 #[test]
133 fn test_timecode_big_endian_encoding() {
134 let tc = TimeCode(Some(0x12345678));
136 let bytes = tc.to_bytes();
137 assert_eq!(bytes[0], 0x12); assert_eq!(bytes[1], 0x34);
139 assert_eq!(bytes[2], 0x56);
140 assert_eq!(bytes[3], 0x78); }
142}