pub fn tobytcp_prefix(num: usize) -> [u8; 8] {
num.to_be_bytes()
}
pub fn tobytcp_len(buf: [u8; 8]) -> u64 {
u64::from_be_bytes(buf)
}
#[cfg(test)]
mod tests {
#[test]
fn tobytcp_prefix_three_bytes() {
for i in 0..16777216 {
assert_eq!(
[
0,
0,
0,
0,
0,
(i / 65536) as u8,
(i / 256) as u8,
(i % 256) as u8
],
super::tobytcp_prefix(i as usize)
);
}
}
#[test]
fn tobytcp_prefix_spotchecks() {
assert_eq!([0, 0, 0, 0, 0, 0, 1, 0], super::tobytcp_prefix(256));
assert_eq!([0, 0, 0, 0, 0, 0, 1, 1], super::tobytcp_prefix(257));
assert_eq!([0, 0, 0, 0, 0, 0, 2, 0], super::tobytcp_prefix(512));
assert_eq!([0, 0, 0, 0, 0, 0, 2, 89], super::tobytcp_prefix(601));
assert_eq!([0, 0, 0, 0, 0, 0, 4, 0], super::tobytcp_prefix(1024));
assert_eq!([0, 0, 0, 0, 0, 0, 8, 0], super::tobytcp_prefix(2048));
assert_eq!([0, 0, 0, 0, 0, 0, 9, 9], super::tobytcp_prefix(2313));
assert_eq!(
[0, 0, 79, 63, 202, 21, 42, 239],
super::tobytcp_prefix(87135391918831)
);
}
#[test]
fn tobytcp_len_three_bytes() {
for i in 0..16777216 {
assert_eq!(
super::tobytcp_len([
0,
0,
0,
0,
0,
(i / 65536) as u8,
(i / 256) as u8,
(i % 256) as u8
]),
i
);
}
}
#[test]
fn tobytcp_len_spotchecks() {
assert_eq!(super::tobytcp_len([0, 0, 0, 0, 0, 0, 1, 0]), 256);
assert_eq!(super::tobytcp_len([0, 0, 0, 0, 0, 0, 1, 1]), 257);
assert_eq!(super::tobytcp_len([0, 0, 0, 0, 0, 0, 2, 0]), 512);
assert_eq!(super::tobytcp_len([0, 0, 0, 0, 0, 0, 2, 89]), 601);
assert_eq!(super::tobytcp_len([0, 0, 0, 0, 0, 0, 4, 0]), 1024);
assert_eq!(super::tobytcp_len([0, 0, 0, 0, 0, 0, 8, 0]), 2048);
assert_eq!(super::tobytcp_len([0, 0, 0, 0, 0, 0, 9, 9]), 2313);
assert_eq!(
super::tobytcp_len([0, 0, 79, 63, 202, 21, 42, 239]),
87135391918831
);
}
#[test]
fn tobytcp_len_and_prefix_match_three_bytes() {
for i in 0..16777216 {
assert_eq!(super::tobytcp_len(super::tobytcp_prefix(i as usize)), i);
}
}
#[test]
fn tobytcp_len_and_prefix_match_spotchecks() {
assert_eq!(super::tobytcp_len(super::tobytcp_prefix(256 as usize)), 256);
assert_eq!(
super::tobytcp_len(super::tobytcp_prefix(132415 as usize)),
132415
);
assert_eq!(
super::tobytcp_len(super::tobytcp_prefix(3199481 as usize)),
3199481
);
assert_eq!(super::tobytcp_len(super::tobytcp_prefix(3 as usize)), 3);
}
}