Skip to content
Snippets Groups Projects
Commit e5c90394 authored by Romain Tartière's avatar Romain Tartière
Browse files

Fix <=> for IPv4 and IPv6.

parent 064502c6
No related branches found
No related tags found
No related merge requests found
...@@ -509,6 +509,7 @@ module IPAddress; ...@@ -509,6 +509,7 @@ module IPAddress;
# #=> ["10.100.100.1/8","10.100.100.1/16","172.16.0.1/16"] # #=> ["10.100.100.1/8","10.100.100.1/16","172.16.0.1/16"]
# #
def <=>(oth) def <=>(oth)
return nil unless oth.is_a?(self.class)
return prefix <=> oth.prefix if to_u32 == oth.to_u32 return prefix <=> oth.prefix if to_u32 == oth.to_u32
to_u32 <=> oth.to_u32 to_u32 <=> oth.to_u32
end end
......
...@@ -487,6 +487,7 @@ module IPAddress; ...@@ -487,6 +487,7 @@ module IPAddress;
# #=> ["2001:db8:1::1/64","2001:db8:1::1/65","2001:db8:2::1/64"] # #=> ["2001:db8:1::1/64","2001:db8:1::1/65","2001:db8:2::1/64"]
# #
def <=>(oth) def <=>(oth)
return nil unless oth.is_a?(self.class)
return prefix <=> oth.prefix if to_u128 == oth.to_u128 return prefix <=> oth.prefix if to_u128 == oth.to_u128
to_u128 <=> oth.to_u128 to_u128 <=> oth.to_u128
end end
......
...@@ -372,6 +372,12 @@ class IPv4Test < Minitest::Test ...@@ -372,6 +372,12 @@ class IPv4Test < Minitest::Test
ip3 = @klass.new("10.0.0.0/8") ip3 = @klass.new("10.0.0.0/8")
arr = ["10.0.0.0/8","10.0.0.0/16","10.0.0.0/24"] arr = ["10.0.0.0/8","10.0.0.0/16","10.0.0.0/24"]
assert_equal arr, [ip1,ip2,ip3].sort.map{|s| s.to_string} assert_equal arr, [ip1,ip2,ip3].sort.map{|s| s.to_string}
# compare with alien thing
ip1 = @klass.new('127.0.0.1')
ip2 = IPAddress::IPv6.new('::1')
not_ip = String
assert_equal nil, ip1 <=> ip2
assert_equal nil, ip1 <=> not_ip
end end
def test_method_minus def test_method_minus
......
...@@ -254,6 +254,12 @@ class IPv6Test < Minitest::Test ...@@ -254,6 +254,12 @@ class IPv6Test < Minitest::Test
arr = ["2001:db8:1::1/64","2001:db8:1::1/65", arr = ["2001:db8:1::1/64","2001:db8:1::1/65",
"2001:db8:1::2/64","2001:db8:2::1/64"] "2001:db8:1::2/64","2001:db8:2::1/64"]
assert_equal arr, [ip1,ip2,ip3,ip4].sort.map{|s| s.to_string} assert_equal arr, [ip1,ip2,ip3,ip4].sort.map{|s| s.to_string}
# compare with alien thing
ip1 = @klass.new('::1')
ip2 = IPAddress::IPv4.new('127.0.0.1')
not_ip = String
assert_equal nil, ip1 <=> ip2
assert_equal nil, ip1 <=> not_ip
end end
def test_classmethod_expand def test_classmethod_expand
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment