Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ip/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def split
# stop when subnets reach their smallest possible size (i.e. 31 for IP4)
def divide_by_subnets(number_subnets)
nets = []
return nets if split.empty?
nets << self
loop do
new_nets = []
Expand All @@ -236,6 +237,7 @@ def divide_by_subnets(number_subnets)
# subdivide a larger subnet into smaller subnets by number of hosts
def divide_by_hosts(number_hosts)
nets = []
return nets if split.empty?
nets << self
while number_hosts <= (nets[0].split[0].size - 2) &&
nets[0].pfxlen <= (self.class::ADDR_BITS - 1)
Expand Down
16 changes: 16 additions & 0 deletions test/ip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ class IPTest < Minitest::Test
IP.new('1.2.3.0/24').split
end

it 'has divide_by_subnets returns empty array when single IPv4' do
assert_equal [], IP.new('1.2.3.4').divide_by_subnets(1)
end

it 'has divide_by_subnets returns empty array when single IPv6' do
assert_equal [], IP.new('1:0:2:0:0:0:0:0').divide_by_subnets(1)
end

it 'has divide_by_subnets be exact' do
assert_equal [IP.new('1.2.3.0/26'), IP.new('1.2.3.64/26'),
IP.new('1.2.3.128/26'), IP.new('1.2.3.192/26')],
Expand All @@ -198,6 +206,14 @@ class IPTest < Minitest::Test
IP.new('1.2.3.0/24').divide_by_hosts(68)
end

it 'has divide_by_hosts returns empty array when single IPv4' do
assert_equal [], IP.new('1.2.3.4/32').divide_by_hosts(68)
end

it 'has divide_by_hosts returns empty array when single IPv6' do
assert_equal [], IP.new('1:0:2:0:0:0:0:0').divide_by_subnets(1)
end

it 'has size' do
assert_equal 256, @addr.size
end
Expand Down