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

- old
+ new

@@ -30,10 +30,15 @@ "1080:0:0:0:8:800:200C:417A" => 21932261930451111902915077091070067066, "1080::8:800:200C:417A" => 21932261930451111902915077091070067066} @invalid_ipv6 = [":1:2:3:4:5:6:7", ":1:2:3:4:5:6:7"] + + @networks = { + "2001:db8:1:1:1:1:1:1/32" => "2001:db8::/32", + "2001:db8:1:1:1:1:1::/32" => "2001:db8::/32", + "2001:db8::1/64" => "2001:db8::/64"} @ip = @klass.new "2001:db8::8:800:200c:417a/64" @network = @klass.new "2001:db8:8:800::/64" @arr = [8193,3512,0,0,8,2048,8204,16762] @hex = "20010db80000000000080800200c417a" @@ -120,11 +125,26 @@ end def test_method_network_u128 assert_equal 42540766411282592856903984951653826560, @ip.network_u128 end - + + def test_method_broadcast_u128 + assert_equal 42540766411282592875350729025363378175, @ip.broadcast_u128 + end + + def test_method_size + ip = @klass.new("2001:db8::8:800:200c:417a/64") + assert_equal 2**64, ip.size + ip = @klass.new("2001:db8::8:800:200c:417a/32") + assert_equal 2**96, ip.size + ip = @klass.new("2001:db8::8:800:200c:417a/120") + assert_equal 2**8, ip.size + ip = @klass.new("2001:db8::8:800:200c:417a/124") + assert_equal 2**4, ip.size + end + def test_method_include? assert_equal true, @ip.include?(@ip) # test prefix on same address included = @klass.new "2001:db8::8:800:200c:417a/128" not_included = @klass.new "2001:db8::8:800:200c:417a/46" @@ -184,10 +204,56 @@ def test_method_loopback? assert_equal true, @klass.new("::1").loopback? assert_equal false, @ip.loopback? end - + + def test_method_network + @networks.each do |addr,net| + ip = @klass.new addr + assert_instance_of @klass, ip.network + assert_equal net, ip.network.to_string + end + end + + def test_method_each + ip = @klass.new("2001:db8::4/125") + arr = [] + ip.each {|i| arr << i.compressed} + expected = ["2001:db8::","2001:db8::1","2001:db8::2", + "2001:db8::3","2001:db8::4","2001:db8::5", + "2001:db8::6","2001:db8::7"] + assert_equal expected, arr + end + + def test_method_compare + ip1 = @klass.new("2001:db8:1::1/64") + ip2 = @klass.new("2001:db8:2::1/64") + ip3 = @klass.new("2001:db8:1::2/64") + ip4 = @klass.new("2001:db8:1::1/65") + + # ip2 should be greater than ip1 + assert_equal true, ip2 > ip1 + assert_equal false, ip1 > ip2 + assert_equal false, ip2 < ip1 + # ip3 should be less than ip2 + assert_equal true, ip2 > ip3 + assert_equal false, ip2 < 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 + # ip4 should be greater than ip1 + assert_equal true, ip1 < ip4 + assert_equal false, ip1 > ip4 + # test sorting + arr = ["2001:db8:1::1/64","2001:db8:1::1/65", + "2001:db8:1::2/64","2001:db8:2::1/64"] + assert_equal arr, [ip1,ip2,ip3,ip4].sort.map{|s| s.to_string} + end + def test_classmethod_expand compressed = "2001:db8:0:cd30::" expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000" assert_equal expanded, @klass.expand(compressed) assert_not_equal expanded, @klass.expand("2001:0db8:0:cd3")