From de9785a21d50a6814259bf2f394166d0debf99ad Mon Sep 17 00:00:00 2001 From: Seth Bromberger Date: Sat, 29 Jul 2017 13:52:01 -0700 Subject: [PATCH] test coverage and some 0.6 fixes (#12) --- .travis.yml | 10 ++- REQUIRE | 2 +- src/IPNets.jl | 38 +++------ test/runtests.jl | 202 +++++++++++++++++++++++------------------------ 4 files changed, 118 insertions(+), 134 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0ce3eac..d9a7933 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,16 @@ os: - linux # - osx julia: - - release - - nightly + - 0.6 + - nightly + +matrix: + allow_failures: + - julia: nightly + notifications: email: false + # uncomment the following lines to override the default test script script: - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi diff --git a/REQUIRE b/REQUIRE index 94237c0..137767a 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1 +1 @@ -julia 0.5 +julia 0.6 diff --git a/src/IPNets.jl b/src/IPNets.jl index 3c76ea2..10e8d9f 100644 --- a/src/IPNets.jl +++ b/src/IPNets.jl @@ -20,7 +20,7 @@ width(::Type{IPv6}) = UInt8(128) ################################################## # IPNet ################################################## -abstract IPNet +abstract type IPNet end ################################################## # Network representations @@ -45,24 +45,17 @@ function string(net::IPNet) return s end -function show(io::IO, net::IPNet) - print(io, string(net)) -end - +show(io::IO, net::IPNet) = print(io, string(net)) # IP Networks are ordered first by starting network address # and then by network mask. That is, smaller IP nets (with higher # netmask values) are "less" than larger ones. This corresponds # to secondary reordering by ending address. -function isless{T<:IPNet}(a::T, b::T) - if a.netaddr == b.netaddr - return isless(b.netmask, a.netmask) - else - return isless(a.netaddr, b.netaddr) - end -end +isless(a::T, b::T) where T <: IPNet = a.netaddr == b.netaddr ? + isless(b.netmask, a.netmask) : + isless(a.netaddr, b.netaddr) -function issubset{T<:IPNet}(a::T, b::T) +function issubset(a::T, b::T) where T <: IPNet astart, aend = extrema(a) bstart, bend = extrema(b) return (bstart <= astart <= aend <= bend) @@ -72,7 +65,6 @@ end function in(ipaddr::IPAddr, net::IPNet) typeof(net.netaddr) == typeof(ipaddr) || error("IPAddr is not the same type as IPNet") - netstart = net.netaddr.host numbits = width(typeof(ipaddr)) - net.netmask netend = net.netaddr.host + big(2)^numbits - 1 @@ -80,13 +72,9 @@ function in(ipaddr::IPAddr, net::IPNet) end """Membership test for an IP address within an IP network""" -function contains(net::IPNet, ipaddr::IPAddr) - return in(ipaddr, net) -end - +contains(net::IPNet, ipaddr::IPAddr) = in(ipaddr, net) function getindex(net::IPNet, i::Integer) - t = typeof(net.netaddr) ip = t(net.netaddr.host + i - 1) ip in net || throw(BoundsError()) @@ -97,12 +85,10 @@ end minimum(net::IPNet) = net[1] maximum(net::IPNet) = net[end] extrema(net::IPNet) = (minimum(net), maximum(net)) -getindex(net::IPNet, i) = net[i] getindex(net::IPNet, r::Range) = [net[i] for i in r] -# getindex(net::IPNet, i::(Integer,)) = getindex(net,i[1]) start(net::IPNet) = net[1] -next{T<:IPAddr}(net::IPNet, s::T) = s, T(s.host + 1) -done{T<:IPAddr}(net::IPNet, s::T) = s > net[end] +next(net::IPNet, s::T) where T <: IPAddr = s, T(s.host + 1) +done(net::IPNet, s::T) where T <: IPAddr = s > net[end] ################################################## @@ -127,7 +113,7 @@ end function IPv4Net(ipmask::AbstractString) if search(ipmask,'/') > 0 addrstr, netmaskstr = split(ipmask,"/") - netmask = parse(UInt8,netmaskstr) + netmask = parse(UInt8, netmaskstr) else addrstr = ipmask netmask = width(IPv4) @@ -148,7 +134,7 @@ end IPv4Net(ipaddr::Integer, netmask::Integer) = IPv4Net(IPv4(ipaddr), netmask) # "(x,y)" -IPv4Net{A,M}(tuple::Tuple{A,M}) = IPv4Net(tuple[1],tuple[2]) +IPv4Net(tuple::Tuple{A,M}) where A where M = IPv4Net(tuple[1],tuple[2]) # "1.2.3.0", 24 IPv4Net(netaddr::AbstractString, netmask::Integer) = IPv4Net(IPv4(netaddr), netmask) @@ -211,7 +197,7 @@ IPv6Net(ipaddr::Integer, netmask::Integer) = IPv6Net(IPv6(ipaddr), netmask) # (123872, 128) -IPv6Net{A,M}(tuple::Tuple{A,M}) = IPv6Net(tuple[1],tuple[2]) +IPv6Net(t::Tuple{A,M}) where A where M = IPv6Net(t[1],t[2]) eltype(x::IPv6Net) = IPv6 endof(net::IPv6Net) = UInt128(length(net)) diff --git a/test/runtests.jl b/test/runtests.jl index a8a6cba..8eefc6c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,108 +1,100 @@ using IPNets using Base.Test -ip41 = IPv4("1.2.3.4") -ip42 = IPv4("5.6.7.8") - -n1 = IPv4Net("1.2.3.0/24") -n2 = IPv4Net("1.2.3.4/24") -n3 = IPv4Net("1.2.3.4/26") -n4 = IPv4Net("5.6.7.0/24") -n5 = IPv4Net("1.2.3.4/30") - -ip61 = IPv6("2001:1::4") -ip62 = IPv6("2001:2::8") - -o1 = IPv6Net("2001:1::/64") -o2 = IPv6Net("2001:1::4/64") -o3 = IPv6Net("2001:1::4/68") -o4 = IPv6Net("2001:2::8/64") -o5 = IPv6Net("2001:1::4/126") - -# ipv4 -@test_throws ErrorException IPv4Net(ip41, 33) -@test IPv4Net("1.2.3.4", "255.255.255.0") == n1 -@test IPv4Net("1.2.3.4", 24) == n1 -@test IPv4Net(16909060,24) == n1 -@test IPv4Net((16909060,24)) == n1 - -@test IPv4Net("1.2.3.4") == IPv4Net("1.2.3.4/32") -@test n1 == n2 - -@test isless(n1,n3) == false -@test isless(n1,n4) == true - -@test n1[5] == ip41 -@test isless(ip41, ip42) == true -@test in(ip42, n4) == true -@test_throws ErrorException ip42 in o4 -@test contains(n4, ip42) == true -@test issubset(n3, n2) == true -@test issubset(n1, n2) == true - -@test IPNets._contiguousbitcount(240,UInt8) == 0x04 -@test IPNets._contiguousbitcount(252,UInt8) == 0x06 -@test_throws ErrorException IPNets._contiguousbitcount(241,UInt8) - -@test endswith(string(n5),"(\"1.2.3.4/30\")") == true -@test endswith(sprint(print,n5),"(\"1.2.3.4/30\")") == true -@test endswith(sprint(show,n5),"(\"1.2.3.4/30\")") == true -@test endswith(string(display,n5),"(\"1.2.3.4/30\")") == true -@test size(n5) == (4,) -@test [x for x in n5] == [ip"1.2.3.4", ip"1.2.3.5", ip"1.2.3.6", ip"1.2.3.7"] -@test endof(n5) == 4 -@test minimum(n5) == ip"1.2.3.4" -@test maximum(n5) == ip"1.2.3.7" -@test extrema(n5) == (ip"1.2.3.4",ip"1.2.3.7") -@test getindex(n5,1:2) == [ip"1.2.3.4", ip"1.2.3.5"] -# @test getindex(n5,(1,)) == ip41 -@test_throws BoundsError getindex(n5, 10) -@test IPNets.width(IPv4) == 32 -@test_throws BoundsError IPNets._mask2bits(IPv4, UInt64(33)) - - - -# IPv6 -@test_throws ErrorException IPv6Net(ip61, 129) -@test IPv6Net("2001:1::", 64) == o1 -@test IPv6Net(0x20010001000000000000000000000004,64) == o1 -@test IPv6Net((0x20010001000000000000000000000004,64)) == o1 -@test o1 == o2 - -@test IPv6Net("2001:1::1") == IPv6Net("2001:1::1/128") -@test isless(o1,o3) == false -@test isless(o1,o4) == true - -@test o1[5] == ip61 -@test isless(ip61, ip62) == true -@test in(ip62, o4) == true -@test_throws ErrorException ip62 in n4 -@test contains(o4, ip62) == true - - -@test endswith(string(o5),"(\"2001:1::4/126\")") == true -@test endswith(sprint(print,o5),"(\"2001:1::4/126\")") == true -@test endswith(sprint(show,o5),"(\"2001:1::4/126\")") == true -@test endswith(string(display,o5),"(\"2001:1::4/126\")") == true -@test size(o5) == (4,) -@test [x for x in o5] == [ip"2001:1::4",ip"2001:1::5",ip"2001:1::6",ip"2001:1::7"] -@test endof(o5) == 4 -@test minimum(o5) == ip"2001:1::4" -@test maximum(o5) == ip"2001:1::7" -@test extrema(o5) == (ip"2001:1::4",ip"2001:1::7") -@test getindex(o5,1:2) == [ip"2001:1::4", ip"2001:1::5"] -# @test getindex(o5,(1,)) == ip61 -@test_throws BoundsError getindex(o5, 10) -@test IPNets.width(IPv6) == 128 - -@test_throws BoundsError IPNets._mask2bits(IPv6, UInt64(129)) - -@test netmask(n1) == ip"255.255.255.0" - -@test eltype(n1) == IPv4 -@test eltype(o1) == IPv6 - -@test n5[1] == ip41 -@test getindex(n5,1) == ip41 -@test o5[1] == ip61 -@test getindex(o5,1) == ip61 +@testset "IPNets" begin + ip41 = IPv4("1.2.3.4") + ip42 = IPv4("5.6.7.8") + + n1 = IPv4Net("1.2.3.0/24") + n2 = IPv4Net("1.2.3.4/24") + n3 = IPv4Net("1.2.3.4/26") + n4 = IPv4Net("5.6.7.0/24") + n5 = IPv4Net("1.2.3.4/30") + + ip61 = IPv6("2001:1::4") + ip62 = IPv6("2001:2::8") + + o1 = IPv6Net("2001:1::/64") + o2 = IPv6Net("2001:1::4/64") + o3 = IPv6Net("2001:1::4/68") + o4 = IPv6Net("2001:2::8/64") + o5 = IPv6Net("2001:1::4/126") + + # ipv4 + @testset "IPv4" begin + @test_throws ErrorException IPv4Net(ip41, 33) + @test IPv4Net("1.2.3.4", "255.255.255.0") == n1 + @test IPv4Net("1.2.3.4", 24) == n1 + @test IPv4Net(16909060,24) == n1 + @test IPv4Net((16909060,24)) == n1 + @test IPv4Net("1.2.3.4") == IPv4Net("1.2.3.4/32") + @test n1 == n2 + @test isless(n1,n3) == false + @test isless(n1,n4) == true + @test n1[5] == ip41 + @test isless(ip41, ip42) == true + @test in(ip42, n4) == true + @test_throws ErrorException ip42 in o4 + @test contains(n4, ip42) == true + @test issubset(n3, n2) == true + @test issubset(n1, n2) == true + @test IPNets._contiguousbitcount(240,UInt8) == 0x04 + @test IPNets._contiguousbitcount(252,UInt8) == 0x06 + @test_throws ErrorException IPNets._contiguousbitcount(241,UInt8) + @test endswith(string(n5),"(\"1.2.3.4/30\")") == true + @test endswith(sprint(print,n5),"(\"1.2.3.4/30\")") == true + @test endswith(sprint(show,n5),"(\"1.2.3.4/30\")") == true + @test endswith(string(display,n5),"(\"1.2.3.4/30\")") == true + @test size(n5) == (4,) + @test [x for x in n5] == [ip"1.2.3.4", ip"1.2.3.5", ip"1.2.3.6", ip"1.2.3.7"] + @test endof(n5) == 4 + @test minimum(n5) == ip"1.2.3.4" + @test maximum(n5) == ip"1.2.3.7" + @test extrema(n5) == (ip"1.2.3.4",ip"1.2.3.7") + @test getindex(n5,1:2) == [ip"1.2.3.4", ip"1.2.3.5"] + # @test getindex(n5,(1,)) == ip41 + @test_throws BoundsError getindex(n5, 10) + @test IPNets.width(IPv4) == 32 + @test_throws BoundsError IPNets._mask2bits(IPv4, UInt64(33)) + @test netmask(n1) == ip"255.255.255.0" + @test eltype(n1) == IPv4 + @test n5[1] == ip41 + @test getindex(n5,1) == ip41 + end + + # IPv6 + @testset "IPv6" begin + @test_throws ErrorException IPv6Net(ip61, 129) + @test IPv6Net("2001:1::", 64) == o1 + @test IPv6Net(0x20010001000000000000000000000004,64) == o1 + @test IPv6Net((0x20010001000000000000000000000004,64)) == o1 + @test o1 == o2 + + @test IPv6Net("2001:1::1") == IPv6Net("2001:1::1/128") + @test isless(o1,o3) == false + @test isless(o1,o4) == true + @test o1[5] == ip61 + @test isless(ip61, ip62) == true + @test in(ip62, o4) == true + @test_throws ErrorException ip62 in n4 + @test contains(o4, ip62) == true + @test endswith(string(o5),"(\"2001:1::4/126\")") == true + @test endswith(sprint(print,o5),"(\"2001:1::4/126\")") == true + @test endswith(sprint(show,o5),"(\"2001:1::4/126\")") == true + @test endswith(string(display,o5),"(\"2001:1::4/126\")") == true + @test size(o5) == (4,) + @test [x for x in o5] == [ip"2001:1::4",ip"2001:1::5",ip"2001:1::6",ip"2001:1::7"] + @test endof(o5) == 4 + @test minimum(o5) == ip"2001:1::4" + @test maximum(o5) == ip"2001:1::7" + @test extrema(o5) == (ip"2001:1::4",ip"2001:1::7") + @test getindex(o5,1:2) == [ip"2001:1::4", ip"2001:1::5"] + # @test getindex(o5,(1,)) == ip61 + @test_throws BoundsError getindex(o5, 10) + @test IPNets.width(IPv6) == 128 + @test_throws BoundsError IPNets._mask2bits(IPv6, UInt64(129)) + @test eltype(o1) == IPv6 + @test o5[1] == ip61 + @test getindex(o5,1) == ip61 + end +end \ No newline at end of file