Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eql? and hash methods for IPv? and Prefix? classes (fix for #21) #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/ipaddress/ipv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,15 @@ def <=>(oth)
return prefix <=> oth.prefix if to_u32 == oth.to_u32
to_u32 <=> oth.to_u32
end


def eql?(oth)
self == oth
end

def hash
[ to_u32, prefix.to_u32 ].hash
end

#
# Returns the number of IP addresses included
# in the network. It also counts the network
Expand Down Expand Up @@ -680,11 +688,13 @@ def supernet(new_prefix)
# a power of two.
#
def subnet(subprefix)
unless ((@prefix.to_i)..32).include? subprefix
# convert to integer in case subprefix is an IPAddress::Prefix
subprefix_i = subprefix.to_i
unless ((@prefix.to_i)..32).include? subprefix_i
raise ArgumentError, "New prefix must be between #@prefix and 32"
end
Array.new(2**(subprefix[email protected]_i)) do |i|
self.class.parse_u32(network_u32+(i*(2**(32-subprefix))), subprefix)
Array.new(2**(subprefix_i[email protected]_i)) do |i|
self.class.parse_u32(network_u32+(i*(2**(32-subprefix_i))), subprefix_i)
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/ipaddress/ipv6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ def <=>(oth)
to_u128 <=> oth.to_u128
end

def eql?(oth)
self == oth
end

def hash
[ to_u128, prefix.to_u128 ].hash
end

#
# Returns the address portion of an IP in binary format,
# as a string containing a sequence of 0 and 1
Expand Down
10 changes: 9 additions & 1 deletion lib/ipaddress/prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def <=>(oth)
@prefix <=> oth.to_i
end

def eql?(oth)
self == oth
end

def hash
to_i.hash
end

#
# Sums two prefixes or a prefix to a
# number, returns a Fixnum
Expand Down Expand Up @@ -260,6 +268,6 @@ def host_prefix
128 - @prefix
end

end # class Prefix123 < Prefix
end # class Prefix128 < Prefix

end # module IPAddress
17 changes: 16 additions & 1 deletion test/ipaddress/ipv4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,22 @@ def test_classmethod_parse_classful
end
assert_raise(ArgumentError){ @klass.parse_classful("192.168.256.257") }
end


def test_hash_equality
ip1 = @klass.new("10.0.1.1/24")
ip2 = @klass.new("10.0.1.1/24")
assert_equal ip1.hash, ip2.hash
assert_equal ip1.eql?( ip2 ), true
end

def test_subnet_prefix_acceptance
ip = @klass.new("10.0.1.1/24")
ip2 = @klass.new("10.0.1.1/29")
subnet1 = ip.subnet(29)
subnet2 = ip.subnet(ip2.prefix)
assert_equal subnet1, subnet2
end

end # class IPv4Test


7 changes: 7 additions & 0 deletions test/ipaddress/ipv6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,5 +418,12 @@ def test_method_ipv6?
def test_mapped?
assert_equal true, @ip.mapped?
end

def test_hash_equality
ip1 = @klass.new("2001:db8::8:800:200c:417a/64")
ip2 = @klass.new("2001:db8::8:800:200c:417a/64")
assert_equal ip1.hash, ip2.hash
assert_equal ip1.eql?( ip2 ), true
end

end # class IPv6MappedTest
7 changes: 7 additions & 0 deletions test/ipaddress/prefix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,11 @@ def test_method_to_u32
end
end

def test_hash_equality
prefix1 = @klass.new(23)
prefix2 = @klass.new(23)
assert_equal prefix1.hash, prefix2.hash
assert_equal prefix1.eql?( prefix2 ), true
end

end # class Prefix128Test