#2 Completed the implementation of create_subnet function, all subnet network addresses are now ALL correctly appended to the vector. Would like to add some more resilient unit testing at some point down the line to verify the accuracy / robustness of the function

This commit is contained in:
Luke Else 2023-05-15 20:26:49 +01:00
parent 8c88d7945a
commit 625233ab86

View File

@ -85,14 +85,15 @@ impl Network {
// Determine the networking gaps // Determine the networking gaps
//If there is only 1 network, returning the base network will be sufficient //If there is only 1 network, returning the base network will be sufficient
if num_networks > 1 { if num_networks > 1 {
let network_spacing: u32 = u32::pow(2, 32 - network_class as u32 - num_borrowed_bits as u32); //Get the base network and next added address as u32's
let mut base_address = u32::from(IpAddr::from_arr(&base_network)?);
let subnet_mask = u32::from(Network::gen_subnet_mask(cidr)?);
let addr_increment = u32::pow(2, subnet_mask.trailing_zeros());
for i in 0..=(num_borrowed_bits/8) as u8{ for _ in 0..num_networks-1 {
//Add remaming subnets (-1 is because we have already added the first address) //Increment address and append to list
for _ in 0..(num_networks-1) { base_address += addr_increment;
base_network[(most_sig_octet + i) as usize] += network_spacing as u8; networks.push(IpAddr::from(base_address));
networks.push(IpAddr::from_arr(&base_network.clone())?);
}
} }
} }
@ -256,8 +257,12 @@ mod tests {
assert_eq!(networks.len(), 2); assert_eq!(networks.len(), 2);
assert_eq!(networks.first().unwrap(), &IpAddr::V4(127, 0, 0, 0)); assert_eq!(networks.first().unwrap(), &IpAddr::V4(127, 0, 0, 0));
let networks = Network::create_subnet(&IpAddr::V4(168, 20, 0, 0), 17).unwrap();
assert_eq!(networks.len(), 2);
assert_eq!(networks.last().unwrap(), &IpAddr::V4(168, 20, 128, 0));
let networks = Network::create_subnet(&IpAddr::V4(127, 0, 0, 0), 17).unwrap(); let networks = Network::create_subnet(&IpAddr::V4(127, 0, 0, 0), 17).unwrap();
assert_eq!(networks.last().unwrap(), &IpAddr::V4(127, 0, 0, 0)); assert_eq!(networks.len(), 512);
assert_eq!(networks.len(), 1); assert_eq!(networks.last().unwrap(), &IpAddr::V4(127, 255, 128, 0));
} }
} }