test/ipaddress/ipv4_test.rb in ipaddress-0.7.5 vs test/ipaddress/ipv4_test.rb in ipaddress-0.8.0

- old
+ new

@@ -302,34 +302,40 @@ def test_method_reverse assert_equal "1.10.16.172.in-addr.arpa", @ip.reverse end - def test_method_comparabble + def test_method_compare ip1 = @klass.new("10.1.1.1/8") ip2 = @klass.new("10.1.1.1/16") ip3 = @klass.new("172.16.1.1/14") ip4 = @klass.new("10.1.1.1/8") - # ip1 should be major than ip2 - assert_equal true, ip1 > ip2 - assert_equal false, ip1 < ip2 - assert_equal false, ip2 > ip1 - # ip2 should be minor than ip3 + # ip2 should be greater than ip1 + assert_equal true, ip1 < ip2 + assert_equal false, ip1 > ip2 + assert_equal false, ip2 < ip1 + # ip2 should be less than ip3 assert_equal true, ip2 < ip3 assert_equal false, ip2 > ip3 - # ip1 should be minor than ip3 + # ip1 should be less than ip3 assert_equal true, ip1 < ip3 assert_equal false, ip1 > ip3 assert_equal false, ip3 < ip1 # ip1 should be equal to itself assert_equal true, ip1 == ip1 # ip1 should be equal to ip4 assert_equal true, ip1 == ip4 # test sorting - arr = ["10.1.1.1/16","10.1.1.1/8","172.16.1.1/14"] + arr = ["10.1.1.1/8","10.1.1.1/16","172.16.1.1/14"] assert_equal arr, [ip1,ip2,ip3].sort.map{|s| s.to_string} + # test same prefix + ip1 = @klass.new("10.0.0.0/24") + ip2 = @klass.new("10.0.0.0/16") + ip3 = @klass.new("10.0.0.0/8") + 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} end def test_method_minus ip1 = @klass.new("10.1.1.1/8") ip2 = @klass.new("10.1.1.10/8") @@ -369,44 +375,60 @@ assert_equal 16, ip.prefix.to_i ip.netmask = "255.255.255.0" assert_equal 24, ip.prefix.to_i end - def test_method_subnet - assert_raise(ArgumentError) {@ip.subnet(0)} - assert_raise(ArgumentError) {@ip.subnet(257)} + def test_method_split + assert_raise(ArgumentError) {@ip.split(0)} + assert_raise(ArgumentError) {@ip.split(257)} + + assert_equal @ip.network, @ip.split(1).first + + arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", + "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", + "172.16.10.192/27", "172.16.10.224/27"] + assert_equal arr, @network.split(8).map {|s| s.to_string} + arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", + "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", + "172.16.10.192/26"] + assert_equal arr, @network.split(7).map {|s| s.to_string} + arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", + "172.16.10.96/27", "172.16.10.128/26", "172.16.10.192/26"] + assert_equal arr, @network.split(6).map {|s| s.to_string} + arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", + "172.16.10.96/27", "172.16.10.128/25"] + assert_equal arr, @network.split(5).map {|s| s.to_string} + arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", + "172.16.10.192/26"] + assert_equal arr, @network.split(4).map {|s| s.to_string} + arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/25"] + assert_equal arr, @network.split(3).map {|s| s.to_string} + arr = ["172.16.10.0/25", "172.16.10.128/25"] + assert_equal arr, @network.split(2).map {|s| s.to_string} + arr = ["172.16.10.0/24"] + assert_equal arr, @network.split(1).map {|s| s.to_string} + end - arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", - "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", - "172.16.10.192/27", "172.16.10.224/27"] - assert_equal arr, @network.subnet(8).map {|s| s.to_string} - arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", - "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", - "172.16.10.192/26"] - assert_equal arr, @network.subnet(7).map {|s| s.to_string} - arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", - "172.16.10.96/27", "172.16.10.128/26", "172.16.10.192/26"] - assert_equal arr, @network.subnet(6).map {|s| s.to_string} - arr = ["172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", - "172.16.10.96/27", "172.16.10.128/25"] - assert_equal arr, @network.subnet(5).map {|s| s.to_string} - arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", - "172.16.10.192/26"] - assert_equal arr, @network.subnet(4).map {|s| s.to_string} - arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/25"] - assert_equal arr, @network.subnet(3).map {|s| s.to_string} - arr = ["172.16.10.0/25", "172.16.10.128/25"] - assert_equal arr, @network.subnet(2).map {|s| s.to_string} - arr = ["172.16.10.0/24"] - assert_equal arr, @network.subnet(1).map {|s| s.to_string} - end - - def test_method_supernet - assert_raise(ArgumentError) {@ip.supernet(0)} - assert_raise(ArgumentError) {@ip.supernet(24)} - assert_equal "172.16.10.0/23", @ip.supernet(23).to_string - assert_equal "172.16.8.0/22", @ip.supernet(22).to_string - end + def test_method_subnet + assert_raise(ArgumentError) {@network.subnet(23)} + assert_raise(ArgumentError) {@network.subnet(33)} + assert_nothing_raised {@ip.subnet(30)} + arr = ["172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", + "172.16.10.192/26"] + assert_equal arr, @network.subnet(26).map {|s| s.to_string} + arr = ["172.16.10.0/25", "172.16.10.128/25"] + assert_equal arr, @network.subnet(25).map {|s| s.to_string} + arr = ["172.16.10.0/24"] + assert_equal arr, @network.subnet(24).map {|s| s.to_string} + end + + def test_method_supernet + assert_raise(ArgumentError) {@ip.supernet(24)} + assert_equal "0.0.0.0/0", @ip.supernet(0).to_string + assert_equal "0.0.0.0/0", @ip.supernet(-2).to_string + assert_equal "172.16.10.0/23", @ip.supernet(23).to_string + assert_equal "172.16.8.0/22", @ip.supernet(22).to_string + end def test_classmethod_parse_u32 @decimal_values.each do |addr,int| ip = @klass.parse_u32(int) ip.prefix = addr.split("/").last.to_i