diff options
| author | bluemonk <ceresa@gmail.com> | 2011-03-27 13:33:34 +0200 |
|---|---|---|
| committer | bluemonk <ceresa@gmail.com> | 2011-03-27 13:33:34 +0200 |
| commit | 6e6fd3ba1339461d03384852dcb70c2f27cec50f (patch) | |
| tree | 8ff90a96dbed0087f299f608e2e16810a5e4da26 | |
| parent | 0e806ce864eaf1bdfa62401938530ace1b3faba1 (diff) | |
| parent | 477993e783b0c62e1cfb464a0cde2a3ede5451ac (diff) | |
| download | ipaddress-6e6fd3ba1339461d03384852dcb70c2f27cec50f.tar.gz | |
Merge branch 'master' of https://github.com/eahlstrom/ipaddress into eahlstrom-master
| -rw-r--r-- | lib/ipaddress/ipv4.rb | 24 | ||||
| -rw-r--r-- | test/ipaddress/ipv4_test.rb | 6 |
2 files changed, 19 insertions, 11 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 33c2b5c..2333d9b 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -537,6 +537,13 @@ module IPAddress; end # + # Checks that all other IPAddress::IPv4 is included in this object + # + def include_all?(*others) + others.find_all{|oth| include?(oth)}.length == others.length + end + + # # Checks if an IPv4 address objects belongs # to a private network RFC1918 # @@ -942,20 +949,15 @@ module IPAddress; end def aggregate(ip1,ip2) - if ip1.include? ip2 - return [ip1] + return [ip1] if ip1.include? ip2 + + snet = ip1.supernet(ip1.prefix-1) + if snet.include_all?(ip1, ip2) && ((ip1.size + ip2.size) == snet.size) + return [snet] else - snet = ip1.supernet(ip1.prefix-1) - arr1 = ip1.subnet(2**(ip2.prefix-ip1.prefix)).map{|i| i.to_string} - arr2 = snet.subnet(2**(ip2.prefix-snet.prefix)).map{|i| i.to_string} - if (arr2 - [ip2.to_string] - arr1).empty? - return [snet] - else - return [ip1, ip2] - end + return [ip1, ip2] end end - end # class IPv4 end # module IPAddress diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb index 475e678..968ff70 100644 --- a/test/ipaddress/ipv4_test.rb +++ b/test/ipaddress/ipv4_test.rb @@ -456,6 +456,12 @@ class IPv4Test < Test::Unit::TestCase result = ["10.0.1.0/24","10.10.2.0/24","172.16.0.0/23"] assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string} + ips = [ + @klass.new("10.0.0.12/30"), + @klass.new("10.0.100.0/24") + ] + result = ["10.0.0.12/30", "10.0.100.0/24"] + assert_equal result, @klass.summarize(*ips).map{|i| i.to_string} end def test_classmethod_parse_data |
