Skip to content

Commit

Permalink
test coverage and some 0.6 fixes (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbromberger authored Jul 29, 2017
1 parent 736670d commit de9785a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 134 deletions.
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
julia 0.5
julia 0.6
38 changes: 12 additions & 26 deletions src/IPNets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ width(::Type{IPv6}) = UInt8(128)
##################################################
# IPNet
##################################################
abstract IPNet
abstract type IPNet end

##################################################
# Network representations
Expand All @@ -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)
Expand All @@ -72,21 +65,16 @@ 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
return netstart <= ipaddr.host <= netend
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())
Expand All @@ -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]


##################################################
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
202 changes: 97 additions & 105 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit de9785a

Please sign in to comment.