diff --git a/src/hex.rs b/src/hex.rs index 0fbad1d..a4b16d1 100644 --- a/src/hex.rs +++ b/src/hex.rs @@ -22,32 +22,6 @@ pub fn to_string(b: &[u8]) -> String { s } -/// Encode an unsigned 64-bit value as a hexadecimal string. -pub fn to_string_u64(mut i: u64, skip_leading_zeroes: bool) -> String { - let mut s = String::with_capacity(16); - for _ in 0..16 { - let ii = i >> 60; - if ii != 0 || !s.is_empty() || !skip_leading_zeroes { - s.push(HEX_CHARS[ii as usize] as char); - } - i = i.wrapping_shl(4); - } - s -} - -/// Encode an unsigned 64-bit value as a hexadecimal ASCII string. -pub fn to_vec_u64(mut i: u64, skip_leading_zeroes: bool) -> Vec { - let mut s = Vec::with_capacity(16); - for _ in 0..16 { - let ii = i >> 60; - if ii != 0 || !s.is_empty() || !skip_leading_zeroes { - s.push(HEX_CHARS[ii as usize]); - } - i = i.wrapping_shl(4); - } - s -} - /// Decode a hex string, ignoring all non-hexadecimal characters. pub fn from_string(s: &str) -> Vec { let mut b: Vec = Vec::with_capacity((s.len() / 2) + 1); @@ -78,6 +52,19 @@ pub fn from_string(s: &str) -> Vec { b } +/// Encode an unsigned 64-bit value as a hexadecimal string. +pub fn to_string_u64(mut i: u64, skip_leading_zeroes: bool) -> String { + let mut s = String::with_capacity(16); + for _ in 0..16 { + let ii = i >> 60; + if ii != 0 || !s.is_empty() || !skip_leading_zeroes { + s.push(HEX_CHARS[ii as usize] as char); + } + i = i.wrapping_shl(4); + } + s +} + pub fn from_string_u64(s: &str) -> u64 { let mut n = 0u64; let mut byte = 0_u8; @@ -110,6 +97,19 @@ pub fn from_string_u64(s: &str) -> u64 { n } +/// Encode an unsigned 64-bit value as a hexadecimal ASCII string. +pub fn to_vec_u64(mut i: u64, skip_leading_zeroes: bool) -> Vec { + let mut s = Vec::with_capacity(16); + for _ in 0..16 { + let ii = i >> 60; + if ii != 0 || !s.is_empty() || !skip_leading_zeroes { + s.push(HEX_CHARS[ii as usize]); + } + i = i.wrapping_shl(4); + } + s +} + /// Encode bytes from 'b' into hex characters in 'dest' and return the number of hex characters written. /// This will panic if the destination slice is smaller than twice the length of the source. pub fn to_hex_bytes(b: &[u8], dest: &mut [u8]) -> usize { @@ -122,3 +122,73 @@ pub fn to_hex_bytes(b: &[u8], dest: &mut [u8]) -> usize { } j } + +#[cfg(test)] +mod tests { + use crate::hex::*; + + #[test] + fn to_and_from_string() { + let res_str = to_string(&[1, 2, 3, 4]); + assert_eq!("01020304", res_str); + let res_vec = from_string(&res_str); + assert_eq!(vec![1, 2, 3, 4], res_vec); + } + + #[test] + fn to_and_from_string_u64() { + // + let mut i: u64 = std::u64::MAX; + let mut res_str = to_string_u64(i, false); + assert_eq!(res_str, "ffffffffffffffff"); + // + i = std::u64::MIN; + res_str = to_string_u64(i, false); + assert_eq!(res_str, "0000000000000000"); + res_str = to_string_u64(i, true); + assert_eq!(res_str, ""); + // + i = 0x400; + res_str = to_string_u64(i, true); + assert_eq!(res_str, "400"); + // + i = from_string_u64("0x400"); + assert_eq!(i, 0x400); + // + i = from_string_u64("0x0"); + assert_eq!(i, 0x0); + // + i = from_string_u64("ffffffffffffffff"); + assert_eq!(i, std::u64::MAX); + // + i = from_string_u64("ff"); + assert_eq!(i, 255); + res_str = to_string_u64(i, true); + assert_eq!(i, 255); + assert_eq!(res_str, "ff"); + } + + #[test] + fn test_to_vec_u64() { + // Max + let mut v = to_vec_u64(std::u64::MAX, false); + assert_eq!( + vec![102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102], + v + ); + // Arbitrary non-zero value + v = to_vec_u64(1024, true); + assert_eq!(vec![52, 48, 48], v); // '4' '0' '0' + // Min + v = to_vec_u64(std::u64::MIN, false); + assert_eq!(vec![48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48], v); + } + + #[test] + fn test_to_hex_bytes() { + let mut dest: [u8; 6] = [0; 6]; + let num_hex_bytes = to_hex_bytes(&[52, 48, 48], &mut dest); + assert_eq!(num_hex_bytes, 6); + assert_eq!(dest, [51, 52, 51, 48, 51, 48]); + } +} diff --git a/src/inetaddress.rs b/src/inetaddress.rs index c73399b..c5003b8 100644 --- a/src/inetaddress.rs +++ b/src/inetaddress.rs @@ -1214,6 +1214,45 @@ mod tests { } } + #[test] + fn eq() { + let ip4_a = InetAddress::from_str("1.2.3.4/1234").unwrap(); + let ip6_a = InetAddress::from_str("2603:6010:6e00:1118:d92a:ab88:4dfb:670a/1234").unwrap(); + let ip4_b = InetAddress::from_str("1.2.3.4/1234").unwrap(); + let ip6_b = InetAddress::from_str("2603:6010:6e00:1118:d92a:ab88:4dfb:670a/1234").unwrap(); + let ip4_c = InetAddress::from_str("1.2.3.5/1234").unwrap(); + let ip6_c = InetAddress::from_str("3603:6010:6e00:1118:d92a:ab88:4dfb:670a/1234").unwrap(); + assert!(!ip4_a.clone().eq(&ip6_a)); + assert!(ip4_a.eq(&ip4_b)); + assert!(ip6_a.eq(&ip6_b)); + assert!(!ip6_a.eq(&ip6_c)); + assert!(!ip4_a.eq(&ip4_c)); + } + + #[test] + fn try_into() { + // Convert to various types and back + let ip4 = InetAddress::from_str("1.2.3.4/1234").unwrap(); + let ipaddr4: IpAddr = ip4.try_into().unwrap(); + assert!(ipaddr4.is_ipv4()); + let new_ip4 = InetAddress::from(ipaddr4); + assert!(new_ip4.is_ipv4()); + + let ip6 = InetAddress::from_str("2603:6010:6e00:1118:d92a:ab88:4dfb:670a/1234").unwrap(); + let ipaddr6: IpAddr = ip6.try_into().unwrap(); + assert!(ipaddr6.is_ipv6()); + let new_ip6 = InetAddress::from(ipaddr6); + assert!(new_ip6.is_ipv6()); + } + + #[test] + fn tofrom_bytes() { + let ip4 = InetAddress::from_str("1.2.3.4/1234").unwrap(); + let bytes = ip4.to_bytes(); + let ip4_from_bytes = InetAddress::from_bytes(&bytes).unwrap(); + assert!(ip4.eq(&ip4_from_bytes)); + } + #[test] fn ipv6_string() { let ip = InetAddress::from_str("2603:6010:6e00:1118:d92a:ab88:4dfb:670a/1234").unwrap(); diff --git a/src/ringbuffer.rs b/src/ringbuffer.rs index a591366..ea407c3 100644 --- a/src/ringbuffer.rs +++ b/src/ringbuffer.rs @@ -91,7 +91,6 @@ impl<'a, T, const C: usize> Iterator for RingBufferIterator<'a, T, C> { #[cfg(test)] mod tests { - use std::default; use super::*; diff --git a/src/str.rs b/src/str.rs index 56684d1..7d38216 100644 --- a/src/str.rs +++ b/src/str.rs @@ -6,8 +6,6 @@ * https://www.zerotier.com/ */ -use serde::de::Unexpected; - use crate::hex::HEX_CHARS; /// Escape non-ASCII-printable characters in a string.