diff options
author | Mike Mackintosh <m@zyp.io> | 2015-03-23 16:31:18 -0400 |
---|---|---|
committer | Mike Mackintosh <m@zyp.io> | 2015-03-23 16:31:18 -0400 |
commit | 1f207635f6aa1672789902429cd877aaf843be93 (patch) | |
tree | 8f80b4e29236a81e4ba54cd1b5955c8a4e5aef55 | |
parent | c074fd5198830dedbb9cff511661693895f78f13 (diff) | |
parent | aab8e659031dd7efc5abdf83456687d9268db2a2 (diff) | |
download | ipaddress-1f207635f6aa1672789902429cd877aaf843be93.tar.gz |
Merge branch 'mikerodrigues-fix-first-last' into dev
-rw-r--r-- | lib/ipaddress/ipv4.rb | 27 | ||||
-rw-r--r-- | test/ipaddress/ipv4_test.rb | 23 |
2 files changed, 44 insertions, 6 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 94cc606..500531a 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -309,7 +309,14 @@ module IPAddress; # #=> "172.16.10.255" # 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 # @@ -363,7 +370,14 @@ module IPAddress; # #=> "192.168.100.1" # 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 # @@ -388,7 +402,14 @@ module IPAddress; # #=> "192.168.100.254" # 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 # diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb index 4680898..9f92c94 100644 --- a/test/ipaddress/ipv4_test.rb +++ b/test/ipaddress/ipv4_test.rb @@ -26,7 +26,8 @@ class IPv4Test < Minitest::Test "10.0.0.0/8" => "255.0.0.0", "172.16.0.0/16" => "255.255.0.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 ={ "0.0.0.0/0" => 0, @@ -48,13 +49,17 @@ class IPv4Test < Minitest::Test "10.0.0.0/8" => "10.255.255.255/8", "172.16.0.0/16" => "172.16.255.255/16", "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 = { "10.5.4.3/8" => "10.0.0.0/8", "172.16.5.4/16" => "172.16.0.0/16", "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_b = @klass.new("172.16.0.1/16") @@ -194,6 +199,12 @@ class IPv4Test < Minitest::Test ip = @klass.new("192.168.100.50/24") assert_instance_of @klass, ip.first 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 def test_method_last @@ -203,6 +214,12 @@ class IPv4Test < Minitest::Test ip = @klass.new("192.168.100.50/24") assert_instance_of @klass, ip.last 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 def test_method_each_host |