Skip to content

Commit

Permalink
Merge pull request ipaddress-gem#21 from Adam21e/find_adjacent
Browse files Browse the repository at this point in the history
Find adjacent
  • Loading branch information
Adam21e authored Jan 21, 2019
2 parents 3f32248 + b86b002 commit d370a36
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
25 changes: 25 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
c
[split.map{|i| i.to_string} - [current_subnet]][0]
c
[current_subnet]
split - [current_subnet]
split[0] == current_subnet
current_subnet
split - [current_subnet]
current_subnet
split
prefix
prfix
c
self.prefix
prefix
self
@prefix
prefix
continue
c
@prefix.host_prefix
@prefix
current_subnet
n
current_subnet
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [Unreleased]

### Enhancements
* Added 'find\_adjacent_subnet' to IPv6

## 0.10.0

### Enhancements
Expand Down
16 changes: 16 additions & 0 deletions lib/ipaddress/ipv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,22 @@ def allocate(skip=0)
self.class.parse_u32(network_u32+@allocator, @prefix)
end

#
# Finds the adjacent block to a subnet.
#
# Example:
#
# ip = IPAddress("10.0.0.0/24")
# ip.find_adjacent_subnet
# #=> "10.0.1.0/24"
#
def find_adjacent_subnet
return false if prefix == 0
current_subnet = to_string
self.prefix = @prefix - 1
(split.map{|i| i.to_string} - [current_subnet])[0]
end

#
# private methods
#
Expand Down
16 changes: 16 additions & 0 deletions lib/ipaddress/ipv6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,22 @@ def allocate(skip=0)
self.class.parse_u128(next_ip, @prefix)
end

#
# Finds the adjacent block to a subnet.
#
# Example:
#
# ip = IPAddress("2001:db8::/32")
# ip.find_adjacent_subnet
# #=> "2001:db9::/32"
#
def find_adjacent_subnet
return false if prefix == 0
current_subnet = to_string
self.prefix = @prefix - 1
(split.map{|i| i.to_string} - [current_subnet])[0]
end

private

def newprefix(num)
Expand Down
14 changes: 12 additions & 2 deletions test/ipaddress/ipv4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,16 @@ def test_allocate_will_raise_stopiteration
end
end

end # class IPv4Test
def test_finds_adjacent_subnet
ip = @klass.new("10.0.0.0/24")
assert_equal "10.0.1.0/24", ip.find_adjacent_subnet
refute @klass.new("10.0.0.0/0").find_adjacent_subnet
assert_equal "10.0.0.0/8", @klass.new("11.0.0.0/8").find_adjacent_subnet
assert_equal "172.16.0.0/16", @klass.new("172.17.0.0/16").find_adjacent_subnet
assert_equal "192.168.4.0/24", @klass.new("192.168.5.0/24").find_adjacent_subnet
assert_equal "192.168.100.4/30", @klass.new("192.168.100.0/30").find_adjacent_subnet
assert_equal "192.168.1.2/31", @klass.new("192.168.1.0/31").find_adjacent_subnet
assert_equal "192.168.2.5/32", @klass.new("192.168.2.4/32").find_adjacent_subnet
end

end # class IPv4Test
9 changes: 9 additions & 0 deletions test/ipaddress/ipv6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ def test_group_updates
assert_equal "2001:db8:4d2:0:8:800:200c:417a/64", ip.to_string
end

def test_finds_adjacent_subnet
refute @klass.new("::/0").find_adjacent_subnet
assert_equal "2001:db9::/32", @klass.new("2001:db8::/32").find_adjacent_subnet
assert_equal "2001:db8:0:1::/64", @klass.new("2001:db8::/64").find_adjacent_subnet
assert_equal "2001:db8:8:2000::/51", @klass.new("2001:db8:8::/51").find_adjacent_subnet
# assert_equal "", @klass.new("").find_adjacent_subnet
# assert_equal "", @klass.new("").find_adjacent_subnet
end

end # class IPv6Test

class IPv6UnspecifiedTest < Minitest::Test
Expand Down

0 comments on commit d370a36

Please sign in to comment.