From eea6d2f6f84b6267c794e37e86f0cf273a56eacc Mon Sep 17 00:00:00 2001 From: Adam21e Date: Mon, 21 Jan 2019 16:14:22 +1100 Subject: [PATCH 1/2] Add find_adjacent_subnet function --- .byebug_history | 25 +++++++++++++++++++++++++ lib/ipaddress/ipv4.rb | 16 ++++++++++++++++ lib/ipaddress/ipv6.rb | 16 ++++++++++++++++ test/ipaddress/ipv4_test.rb | 14 ++++++++++++-- test/ipaddress/ipv6_test.rb | 9 +++++++++ 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 .byebug_history diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 0000000..3c43a82 --- /dev/null +++ b/.byebug_history @@ -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 diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 63868a0..8937981 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -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 # diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb index 952568b..03c28cd 100644 --- a/lib/ipaddress/ipv6.rb +++ b/lib/ipaddress/ipv6.rb @@ -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) diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb index 93c85bf..8cebdca 100644 --- a/test/ipaddress/ipv4_test.rb +++ b/test/ipaddress/ipv4_test.rb @@ -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 diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb index be9c4e5..eb77bc2 100644 --- a/test/ipaddress/ipv6_test.rb +++ b/test/ipaddress/ipv6_test.rb @@ -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 From b86b0026d5b392f1f6b0a7acd82d95f24ea7e02f Mon Sep 17 00:00:00 2001 From: Adam21e Date: Mon, 21 Jan 2019 16:16:37 +1100 Subject: [PATCH 2/2] Update changelog for find_adjacent_subnet --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7147c6b..c921abe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## [Unreleased] +### Enhancements +* Added 'find\_adjacent_subnet' to IPv6 + ## 0.10.0 ### Enhancements