Skip to content

Commit

Permalink
added tests for set_edgeval! and get_edgeval with key
Browse files Browse the repository at this point in the history
  • Loading branch information
simonschoelly committed Nov 25, 2018
1 parent 5d02260 commit f441849
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 87 deletions.
4 changes: 3 additions & 1 deletion src/SimpleValueGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ abstract type AbstractSimpleValueGraph{V<:Integer, E_VAL} <: AbstractGraph{V} en
const TupleOrNamedTuple = Union{Tuple, NamedTuple} # TODO maybe somewhere else?

const default_edgeval_type = Float64
edgeval_type(g::AbstractSimpleValueGraph{V, E_VAL}) where {V, E_VAL} = E_VAL
default_edgeval(E_VAL) = oneunit(E_VAL)
default_edgeval(::Type{Nothing}) = nothing
default_edgeval(T::Type{<:TupleOrNamedTuple}) = T( default_edgeval(U) for U in T.types )

default_zero_edgeval(::Type{Nothing}) = nothing
default_zero_edgeval(E_VAL) = zero(E_VAL)
default_zero_edgeval(E_VAL::Type{<:TupleOrNamedTuple}) = E_VAL( default_zero_edgeval(T) for T in E_VAL.types )


eltype(::AbstractSimpleValueGraph{V}) where {V} = V
edgetype(::AbstractSimpleValueGraph{V, E_VAL}) where {V, E_VAL} = SimpleValueEdge{V, E_VAL}
edgeval_type(g::AbstractSimpleValueGraph{V, E_VAL}) where {V, E_VAL} = E_VAL
edgeval_type(::Type{<:AbstractSimpleValueGraph{V, E_VAL}}) where {V, E_VAL} = E_VAL

edges(g::AbstractSimpleValueGraph) = SimpleValueEdgeIter(g)

Expand Down
6 changes: 3 additions & 3 deletions src/matrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function weights(g::AbstractSimpleValueGraph, key)
end

function getindex(A::SimpleValueMatrix{E_VAL, Nothing}, s::Integer, d::Integer) where {E_VAL}
return get_edgeval(A.g, s, d)
return something(get_edgeval(A.g, s, d), default_zero_edgeval(E_VAL), Some(nothing))
end

function getindex(A::SimpleValueMatrix, s::Integer, d::Integer)
return get_edgeval(A.g, s, d, A.key)
function getindex(A::SimpleValueMatrix{E_VAL}, s::Integer, d::Integer) where {E_VAL}
return something(get_edgeval(A.g, s, d, A.key), default_zero_edgeval(E_VAL)[A.key], Some(nothing))
end

function size(A::SimpleValueMatrix)
Expand Down
32 changes: 28 additions & 4 deletions src/operators.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function blockdiag(::Type{SimpleValueGraph{V, E_VAL}}, iter::AbstractGraph{<:Integer}...) where {V<:Integer, E_VAL}
n::V = zero(V)
function blockdiag(::Type{<:SimpleValueGraph{V, E_VAL}}, iter::AbstractGraph{<:Integer}...) where {V, E_VAL}
n::V = V(0)
for g in iter
n += nv(g)
# TODO check for overflow
Expand All @@ -13,7 +13,7 @@ function blockdiag(::Type{SimpleValueGraph{V, E_VAL}}, iter::AbstractGraph{<:Int
w = weights(g)
for u in vertices(g)
for v in neighbors(g, u)
add_edge!(resultg, V(u) + Δ, T(v) + Δ, convert(V, w[u, v]))
add_edge!(resultg, V(u) + Δ, V(v) + Δ, convert(E_VAL, w[u, v]))
end
end
Δ += nv(g)
Expand All @@ -22,7 +22,31 @@ function blockdiag(::Type{SimpleValueGraph{V, E_VAL}}, iter::AbstractGraph{<:Int
return resultg
end

blockdiag(g::SimpleValueGraph, iter::AbstractGraph...) = blockdiag(typeof(g), g, iter...)
function blockdiag(::Type{<:SimpleValueDiGraph{V, E_VAL}}, iter::AbstractGraph{<:Integer}...) where {V, E_VAL}
n::V = V(0)
for g in iter
n += nv(g)
# TODO check for overflow
end

resultg = SimpleValueDiGraph(n, E_VAL)

# TODO this is not very efficient
Δ::V = zero(V)
for g in iter
w = weights(g)
for u in vertices(g)
for v in outneighbors(g, u)
add_edge!(resultg, V(u) + Δ, V(v) + Δ, convert(E_VAL, w[u, v]))
end
end
Δ += nv(g)
end

return resultg
end

blockdiag(g::AbstractSimpleValueGraph, iter::AbstractGraph...) = blockdiag(typeof(g), g, iter...)

#=
function complement(g::SimpleValueGraph{T, U})
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const EdgeValContainer{T} = Union{Nothing,
}


create_edgeval_list(nv, E_VAL::Type{Nothing}) = Nothing
create_edgeval_list(nv, E_VAL::Type{Nothing}) = nothing
create_edgeval_list(nv, E_VAL::Type) = Adjlist{E_VAL}(nv)
create_edgeval_list(nv, E_VAL::Type{<:Tuple}) = Tuple(Adjlist{T}(nv) for T in E_VAL.parameters)
create_edgeval_list(nv, E_VAL::Type{<:NamedTuple}) = NamedTuple{Tuple(E_VAL.names)}(Adjlist{T}(nv) for T in E_VAL.types)
Expand Down
119 changes: 41 additions & 78 deletions test/interface.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Base.Iterators: product
using LinearAlgebra: issymmetric
import SimpleValueGraphs.TupleOrNamedTuple

pbar = Progress(14, 0.2, "interface")
pbar = Progress(12, 0.2, "interface")

@testset "Interface" begin

Expand Down Expand Up @@ -146,6 +144,26 @@ end

next!(pbar)

@testset "get_edgeval & set_edgeval! with key undirected" begin
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
n = 5
m = 6
k = 10
gs = erdos_renyi(V(5), 6)
gv = SimpleValueGraph(gs, E_VAL)
for i = 1:k
u = rand(1:n)
v = rand(1:n)
w = rand_sample(E_VAL)
key = rand(1:length(E_VAL.parameters))
set_edgeval!(gv, u, v, key, w[key])
@test get_edgeval(gv, u, v, key) == (has_edge(gs, u, v) ? w[key] : nothing)
end
end
end

next!(pbar)

@testset "get_edgeval & set_edgeval! directed" begin
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
n = 5
Expand All @@ -165,6 +183,26 @@ end

next!(pbar)

@testset "get_edgeval & set_edgeval! with key directed" begin
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
n = 5
m = 6
k = 10
gs = erdos_renyi(V(5), 6, is_directed=true)
gv = SimpleValueDiGraph(gs, E_VAL)
for i = 1:k
u = rand(1:n)
v = rand(1:n)
w = rand_sample(E_VAL)
key = rand(1:length(E_VAL.parameters))
set_edgeval!(gv, u, v, key, w[key])
@test get_edgeval(gv, u, v, key) == (has_edge(gs, u, v) ? w[key] : nothing)
end
end
end

next!(pbar)

@testset "add_vertex! undirected" begin
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
g = SimpleValueGraph(CompleteGraph(V(4)), E_VAL)
Expand Down Expand Up @@ -195,79 +233,4 @@ end

next!(pbar)

# TODO this should be in a file test/operators.jl
@testset "map_edgevals! undirected" begin
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
g = SimpleValueGraph(CycleGraph(V(5)), E_VAL)
u = rand(vertices(g))
w1 = rand_sample(E_VAL)
w2 = rand_sample(E_VAL)
map_edgevals!(g) do s, d, value
return (u (s, d)) ? w1 : w2
end
@test all(edges(g)) do e
return val(e) == (u (src(e), dst(e)) ? w1 : w2)
end
end
end

next!(pbar)

# TODO this should be in a file test/operators.jl
@testset "map_edgevals! directed" begin
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
g = SimpleValueDiGraph(CycleDiGraph(V(5)), E_VAL)
u = rand(vertices(g))
w1 = rand_sample(E_VAL)
w2 = rand_sample(E_VAL)
map_edgevals!(g) do s, d, value
return (s == u) ? w1 : w2
end
@test all(edges(g)) do e
return val(e) == (src(e) == u ? w1 : w2)
end
end
end

next!(pbar)

# TODO this should be in a file test/operators.jl
@testset "map_edgevals! with key undirected" begin
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
g = SimpleValueGraph(CycleGraph(V(5)), E_VAL)
u = rand(vertices(g))
key = rand(1:length(E_VAL.parameters))
w1 = rand_sample(E_VAL)[key]
w2 = rand_sample(E_VAL)[key]
map_edgevals!(g, key) do s, d, value
return (u (s, d)) ? w1 : w2
end
@test all(edges(g)) do e
return val(e)[key] == (u (src(e), dst(e)) ? w1 : w2)
end
end
end

next!(pbar)

# TODO this should be in a file test/operators.jl
@testset "map_edgevals! with key directed" begin
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
g = SimpleValueDiGraph(CycleDiGraph(V(5)), E_VAL)
u = rand(vertices(g))
key = rand(1:length(E_VAL.parameters))
w1 = rand_sample(E_VAL)[key]
w2 = rand_sample(E_VAL)[key]
map_edgevals!(g, key) do s, d, value
return (s == u) ? w1 : w2
end
@test all(edges(g)) do e
return val(e)[key] == (u == src(e) ? w1 : w2)
end
end
end

next!(pbar)


end # testset Interface
Empty file added test/matrices.jl
Empty file.
83 changes: 83 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

@testset "operators" begin

pbar = Progress(4, 0.2, "operators")

# TODO this should be in a file test/operators.jl
@testset "map_edgevals! undirected" begin
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
g = SimpleValueGraph(CycleGraph(V(5)), E_VAL)
u = rand(vertices(g))
w1 = rand_sample(E_VAL)
w2 = rand_sample(E_VAL)
map_edgevals!(g) do s, d, value
return (u (s, d)) ? w1 : w2
end
@test all(edges(g)) do e
return val(e) == (u (src(e), dst(e)) ? w1 : w2)
end
end
end

next!(pbar)

# TODO this should be in a file test/operators.jl
@testset "map_edgevals! directed" begin
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
g = SimpleValueDiGraph(CycleDiGraph(V(5)), E_VAL)
u = rand(vertices(g))
w1 = rand_sample(E_VAL)
w2 = rand_sample(E_VAL)
map_edgevals!(g) do s, d, value
return (s == u) ? w1 : w2
end
@test all(edges(g)) do e
return val(e) == (src(e) == u ? w1 : w2)
end
end
end

next!(pbar)

# TODO this should be in a file test/operators.jl
@testset "map_edgevals! with key undirected" begin
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
g = SimpleValueGraph(CycleGraph(V(5)), E_VAL)
u = rand(vertices(g))
key = rand(1:length(E_VAL.parameters))
w1 = rand_sample(E_VAL)[key]
w2 = rand_sample(E_VAL)[key]
map_edgevals!(g, key) do s, d, value
return (u (s, d)) ? w1 : w2
end
@test all(edges(g)) do e
return val(e)[key] == (u (src(e), dst(e)) ? w1 : w2)
end
end
end

next!(pbar)

# TODO this should be in a file test/operators.jl
@testset "map_edgevals! with key directed" begin
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
g = SimpleValueDiGraph(CycleDiGraph(V(5)), E_VAL)
u = rand(vertices(g))
key = rand(1:length(E_VAL.parameters))
w1 = rand_sample(E_VAL)[key]
w2 = rand_sample(E_VAL)[key]
map_edgevals!(g, key) do s, d, value
return (s == u) ? w1 : w2
end
@test all(edges(g)) do e
return val(e)[key] == (u == src(e) ? w1 : w2)
end
end
end

next!(pbar)

end # testset



5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ using ProgressMeter
using TerminalMenus
using Test

using Base.Iterators: product
import SimpleValueGraphs.TupleOrNamedTuple

include("testutils.jl")

tests = Dict("LightGraphs compability" => () -> include("lightgraphs_compatibility.jl"),
"SimpleValueGraph interface" => () -> include("interface.jl"),
"SimpleValueGraph operators" => () -> include("operators.jl"),
"SimpleValueGraph matrices" => () -> include("matrices.jl"),
)

@testset "SimpleValueGraphs" begin
Expand Down

0 comments on commit f441849

Please sign in to comment.