Skip to content
Snippets Groups Projects
Commit aab8e659 authored by Mike Mackintosh's avatar Mike Mackintosh
Browse files

Merge branch 'fix-first-last' of https://github.com/mikerodrigues/ipaddress...

Merge branch 'fix-first-last' of https://github.com/mikerodrigues/ipaddress into mikerodrigues-fix-first-last
parents c074fd51 b5a200a6
Branches
No related tags found
No related merge requests found
...@@ -309,7 +309,14 @@ module IPAddress; ...@@ -309,7 +309,14 @@ module IPAddress;
# #=> "172.16.10.255" # #=> "172.16.10.255"
# #
def broadcast def broadcast
self.class.parse_u32(broadcast_u32, @prefix) case
when prefix <= 30
self.class.parse_u32(broadcast_u32, @prefix)
when prefix == 31
self.class.parse_u32(-1, @prefix)
when prefix == 32
return self
end
end end
# #
...@@ -363,7 +370,14 @@ module IPAddress; ...@@ -363,7 +370,14 @@ module IPAddress;
# #=> "192.168.100.1" # #=> "192.168.100.1"
# #
def first def first
self.class.parse_u32(network_u32+1, @prefix) case
when prefix <= 30
self.class.parse_u32(network_u32+1, @prefix)
when prefix == 31
self.class.parse_u32(network_u32, @prefix)
when prefix == 32
return self
end
end end
# #
...@@ -388,7 +402,14 @@ module IPAddress; ...@@ -388,7 +402,14 @@ module IPAddress;
# #=> "192.168.100.254" # #=> "192.168.100.254"
# #
def last def last
self.class.parse_u32(broadcast_u32-1, @prefix) case
when prefix <= 30
self.class.parse_u32(broadcast_u32-1, @prefix)
when prefix == 31
self.class.parse_u32(broadcast_u32, @prefix)
when prefix == 32
return self
end
end end
# #
......
...@@ -26,7 +26,8 @@ class IPv4Test < Minitest::Test ...@@ -26,7 +26,8 @@ class IPv4Test < Minitest::Test
"10.0.0.0/8" => "255.0.0.0", "10.0.0.0/8" => "255.0.0.0",
"172.16.0.0/16" => "255.255.0.0", "172.16.0.0/16" => "255.255.0.0",
"192.168.0.0/24" => "255.255.255.0", "192.168.0.0/24" => "255.255.255.0",
"192.168.100.4/30" => "255.255.255.252"} "192.168.100.4/30" => "255.255.255.252",
"192.168.12.4/32" => "255.255.255.255"}
@decimal_values ={ @decimal_values ={
"0.0.0.0/0" => 0, "0.0.0.0/0" => 0,
...@@ -48,13 +49,17 @@ class IPv4Test < Minitest::Test ...@@ -48,13 +49,17 @@ class IPv4Test < Minitest::Test
"10.0.0.0/8" => "10.255.255.255/8", "10.0.0.0/8" => "10.255.255.255/8",
"172.16.0.0/16" => "172.16.255.255/16", "172.16.0.0/16" => "172.16.255.255/16",
"192.168.0.0/24" => "192.168.0.255/24", "192.168.0.0/24" => "192.168.0.255/24",
"192.168.100.4/30" => "192.168.100.7/30"} "192.168.100.4/30" => "192.168.100.7/30",
"192.168.12.3/31" => "255.255.255.255/31",
"10.0.0.1/32" => "10.0.0.1/32"}
@networks = { @networks = {
"10.5.4.3/8" => "10.0.0.0/8", "10.5.4.3/8" => "10.0.0.0/8",
"172.16.5.4/16" => "172.16.0.0/16", "172.16.5.4/16" => "172.16.0.0/16",
"192.168.4.3/24" => "192.168.4.0/24", "192.168.4.3/24" => "192.168.4.0/24",
"192.168.100.5/30" => "192.168.100.4/30"} "192.168.100.5/30" => "192.168.100.4/30",
"192.168.1.3/31" => "192.168.1.2/31",
"192.168.2.5/32" => "192.168.2.5/32"}
@class_a = @klass.new("10.0.0.1/8") @class_a = @klass.new("10.0.0.1/8")
@class_b = @klass.new("172.16.0.1/16") @class_b = @klass.new("172.16.0.1/16")
...@@ -194,6 +199,12 @@ class IPv4Test < Minitest::Test ...@@ -194,6 +199,12 @@ class IPv4Test < Minitest::Test
ip = @klass.new("192.168.100.50/24") ip = @klass.new("192.168.100.50/24")
assert_instance_of @klass, ip.first assert_instance_of @klass, ip.first
assert_equal "192.168.100.1", ip.first.to_s assert_equal "192.168.100.1", ip.first.to_s
ip = @klass.new("192.168.100.50/32")
assert_instance_of @klass, ip.first
assert_equal "192.168.100.50", ip.first.to_s
ip = @klass.new("192.168.100.50/31")
assert_instance_of @klass, ip.first
assert_equal "192.168.100.50", ip.first.to_s
end end
def test_method_last def test_method_last
...@@ -203,6 +214,12 @@ class IPv4Test < Minitest::Test ...@@ -203,6 +214,12 @@ class IPv4Test < Minitest::Test
ip = @klass.new("192.168.100.50/24") ip = @klass.new("192.168.100.50/24")
assert_instance_of @klass, ip.last assert_instance_of @klass, ip.last
assert_equal "192.168.100.254", ip.last.to_s assert_equal "192.168.100.254", ip.last.to_s
ip = @klass.new("192.168.100.50/32")
assert_instance_of @klass, ip.last
assert_equal "192.168.100.50", ip.last.to_s
ip = @klass.new("192.168.100.50/31")
assert_instance_of @klass, ip.last
assert_equal "192.168.100.51", ip.last.to_s
end end
def test_method_each_host def test_method_each_host
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment