Skip to content

Commit f441849

Browse files
committed
added tests for set_edgeval! and get_edgeval with key
1 parent 5d02260 commit f441849

File tree

8 files changed

+164
-87
lines changed

8 files changed

+164
-87
lines changed

src/SimpleValueGraphs.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ abstract type AbstractSimpleValueGraph{V<:Integer, E_VAL} <: AbstractGraph{V} en
3434
const TupleOrNamedTuple = Union{Tuple, NamedTuple} # TODO maybe somewhere else?
3535

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

41+
default_zero_edgeval(::Type{Nothing}) = nothing
4242
default_zero_edgeval(E_VAL) = zero(E_VAL)
4343
default_zero_edgeval(E_VAL::Type{<:TupleOrNamedTuple}) = E_VAL( default_zero_edgeval(T) for T in E_VAL.types )
4444

4545

4646
eltype(::AbstractSimpleValueGraph{V}) where {V} = V
4747
edgetype(::AbstractSimpleValueGraph{V, E_VAL}) where {V, E_VAL} = SimpleValueEdge{V, E_VAL}
48+
edgeval_type(g::AbstractSimpleValueGraph{V, E_VAL}) where {V, E_VAL} = E_VAL
49+
edgeval_type(::Type{<:AbstractSimpleValueGraph{V, E_VAL}}) where {V, E_VAL} = E_VAL
4850

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

src/matrices.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ function weights(g::AbstractSimpleValueGraph, key)
2020
end
2121

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

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

3030
function size(A::SimpleValueMatrix)

src/operators.jl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function blockdiag(::Type{SimpleValueGraph{V, E_VAL}}, iter::AbstractGraph{<:Integer}...) where {V<:Integer, E_VAL}
2-
n::V = zero(V)
1+
function blockdiag(::Type{<:SimpleValueGraph{V, E_VAL}}, iter::AbstractGraph{<:Integer}...) where {V, E_VAL}
2+
n::V = V(0)
33
for g in iter
44
n += nv(g)
55
# TODO check for overflow
@@ -13,7 +13,7 @@ function blockdiag(::Type{SimpleValueGraph{V, E_VAL}}, iter::AbstractGraph{<:Int
1313
w = weights(g)
1414
for u in vertices(g)
1515
for v in neighbors(g, u)
16-
add_edge!(resultg, V(u) + Δ, T(v) + Δ, convert(V, w[u, v]))
16+
add_edge!(resultg, V(u) + Δ, V(v) + Δ, convert(E_VAL, w[u, v]))
1717
end
1818
end
1919
Δ += nv(g)
@@ -22,7 +22,31 @@ function blockdiag(::Type{SimpleValueGraph{V, E_VAL}}, iter::AbstractGraph{<:Int
2222
return resultg
2323
end
2424

25-
blockdiag(g::SimpleValueGraph, iter::AbstractGraph...) = blockdiag(typeof(g), g, iter...)
25+
function blockdiag(::Type{<:SimpleValueDiGraph{V, E_VAL}}, iter::AbstractGraph{<:Integer}...) where {V, E_VAL}
26+
n::V = V(0)
27+
for g in iter
28+
n += nv(g)
29+
# TODO check for overflow
30+
end
31+
32+
resultg = SimpleValueDiGraph(n, E_VAL)
33+
34+
# TODO this is not very efficient
35+
Δ::V = zero(V)
36+
for g in iter
37+
w = weights(g)
38+
for u in vertices(g)
39+
for v in outneighbors(g, u)
40+
add_edge!(resultg, V(u) + Δ, V(v) + Δ, convert(E_VAL, w[u, v]))
41+
end
42+
end
43+
Δ += nv(g)
44+
end
45+
46+
return resultg
47+
end
48+
49+
blockdiag(g::AbstractSimpleValueGraph, iter::AbstractGraph...) = blockdiag(typeof(g), g, iter...)
2650

2751
#=
2852
function complement(g::SimpleValueGraph{T, U})

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const EdgeValContainer{T} = Union{Nothing,
1616
}
1717

1818

19-
create_edgeval_list(nv, E_VAL::Type{Nothing}) = Nothing
19+
create_edgeval_list(nv, E_VAL::Type{Nothing}) = nothing
2020
create_edgeval_list(nv, E_VAL::Type) = Adjlist{E_VAL}(nv)
2121
create_edgeval_list(nv, E_VAL::Type{<:Tuple}) = Tuple(Adjlist{T}(nv) for T in E_VAL.parameters)
2222
create_edgeval_list(nv, E_VAL::Type{<:NamedTuple}) = NamedTuple{Tuple(E_VAL.names)}(Adjlist{T}(nv) for T in E_VAL.types)

test/interface.jl

Lines changed: 41 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using Base.Iterators: product
21
using LinearAlgebra: issymmetric
3-
import SimpleValueGraphs.TupleOrNamedTuple
42

5-
pbar = Progress(14, 0.2, "interface")
3+
pbar = Progress(12, 0.2, "interface")
64

75
@testset "Interface" begin
86

@@ -146,6 +144,26 @@ end
146144

147145
next!(pbar)
148146

147+
@testset "get_edgeval & set_edgeval! with key undirected" begin
148+
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
149+
n = 5
150+
m = 6
151+
k = 10
152+
gs = erdos_renyi(V(5), 6)
153+
gv = SimpleValueGraph(gs, E_VAL)
154+
for i = 1:k
155+
u = rand(1:n)
156+
v = rand(1:n)
157+
w = rand_sample(E_VAL)
158+
key = rand(1:length(E_VAL.parameters))
159+
set_edgeval!(gv, u, v, key, w[key])
160+
@test get_edgeval(gv, u, v, key) == (has_edge(gs, u, v) ? w[key] : nothing)
161+
end
162+
end
163+
end
164+
165+
next!(pbar)
166+
149167
@testset "get_edgeval & set_edgeval! directed" begin
150168
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
151169
n = 5
@@ -165,6 +183,26 @@ end
165183

166184
next!(pbar)
167185

186+
@testset "get_edgeval & set_edgeval! with key directed" begin
187+
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
188+
n = 5
189+
m = 6
190+
k = 10
191+
gs = erdos_renyi(V(5), 6, is_directed=true)
192+
gv = SimpleValueDiGraph(gs, E_VAL)
193+
for i = 1:k
194+
u = rand(1:n)
195+
v = rand(1:n)
196+
w = rand_sample(E_VAL)
197+
key = rand(1:length(E_VAL.parameters))
198+
set_edgeval!(gv, u, v, key, w[key])
199+
@test get_edgeval(gv, u, v, key) == (has_edge(gs, u, v) ? w[key] : nothing)
200+
end
201+
end
202+
end
203+
204+
next!(pbar)
205+
168206
@testset "add_vertex! undirected" begin
169207
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
170208
g = SimpleValueGraph(CompleteGraph(V(4)), E_VAL)
@@ -195,79 +233,4 @@ end
195233

196234
next!(pbar)
197235

198-
# TODO this should be in a file test/operators.jl
199-
@testset "map_edgevals! undirected" begin
200-
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
201-
g = SimpleValueGraph(CycleGraph(V(5)), E_VAL)
202-
u = rand(vertices(g))
203-
w1 = rand_sample(E_VAL)
204-
w2 = rand_sample(E_VAL)
205-
map_edgevals!(g) do s, d, value
206-
return (u (s, d)) ? w1 : w2
207-
end
208-
@test all(edges(g)) do e
209-
return val(e) == (u (src(e), dst(e)) ? w1 : w2)
210-
end
211-
end
212-
end
213-
214-
next!(pbar)
215-
216-
# TODO this should be in a file test/operators.jl
217-
@testset "map_edgevals! directed" begin
218-
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
219-
g = SimpleValueDiGraph(CycleDiGraph(V(5)), E_VAL)
220-
u = rand(vertices(g))
221-
w1 = rand_sample(E_VAL)
222-
w2 = rand_sample(E_VAL)
223-
map_edgevals!(g) do s, d, value
224-
return (s == u) ? w1 : w2
225-
end
226-
@test all(edges(g)) do e
227-
return val(e) == (src(e) == u ? w1 : w2)
228-
end
229-
end
230-
end
231-
232-
next!(pbar)
233-
234-
# TODO this should be in a file test/operators.jl
235-
@testset "map_edgevals! with key undirected" begin
236-
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
237-
g = SimpleValueGraph(CycleGraph(V(5)), E_VAL)
238-
u = rand(vertices(g))
239-
key = rand(1:length(E_VAL.parameters))
240-
w1 = rand_sample(E_VAL)[key]
241-
w2 = rand_sample(E_VAL)[key]
242-
map_edgevals!(g, key) do s, d, value
243-
return (u (s, d)) ? w1 : w2
244-
end
245-
@test all(edges(g)) do e
246-
return val(e)[key] == (u (src(e), dst(e)) ? w1 : w2)
247-
end
248-
end
249-
end
250-
251-
next!(pbar)
252-
253-
# TODO this should be in a file test/operators.jl
254-
@testset "map_edgevals! with key directed" begin
255-
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
256-
g = SimpleValueDiGraph(CycleDiGraph(V(5)), E_VAL)
257-
u = rand(vertices(g))
258-
key = rand(1:length(E_VAL.parameters))
259-
w1 = rand_sample(E_VAL)[key]
260-
w2 = rand_sample(E_VAL)[key]
261-
map_edgevals!(g, key) do s, d, value
262-
return (s == u) ? w1 : w2
263-
end
264-
@test all(edges(g)) do e
265-
return val(e)[key] == (u == src(e) ? w1 : w2)
266-
end
267-
end
268-
end
269-
270-
next!(pbar)
271-
272-
273236
end # testset Interface

test/matrices.jl

Whitespace-only changes.

test/operators.jl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
@testset "operators" begin
3+
4+
pbar = Progress(4, 0.2, "operators")
5+
6+
# TODO this should be in a file test/operators.jl
7+
@testset "map_edgevals! undirected" begin
8+
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
9+
g = SimpleValueGraph(CycleGraph(V(5)), E_VAL)
10+
u = rand(vertices(g))
11+
w1 = rand_sample(E_VAL)
12+
w2 = rand_sample(E_VAL)
13+
map_edgevals!(g) do s, d, value
14+
return (u (s, d)) ? w1 : w2
15+
end
16+
@test all(edges(g)) do e
17+
return val(e) == (u (src(e), dst(e)) ? w1 : w2)
18+
end
19+
end
20+
end
21+
22+
next!(pbar)
23+
24+
# TODO this should be in a file test/operators.jl
25+
@testset "map_edgevals! directed" begin
26+
for (V, E_VAL) in product(test_vertex_types, test_edgeval_types)
27+
g = SimpleValueDiGraph(CycleDiGraph(V(5)), E_VAL)
28+
u = rand(vertices(g))
29+
w1 = rand_sample(E_VAL)
30+
w2 = rand_sample(E_VAL)
31+
map_edgevals!(g) do s, d, value
32+
return (s == u) ? w1 : w2
33+
end
34+
@test all(edges(g)) do e
35+
return val(e) == (src(e) == u ? w1 : w2)
36+
end
37+
end
38+
end
39+
40+
next!(pbar)
41+
42+
# TODO this should be in a file test/operators.jl
43+
@testset "map_edgevals! with key undirected" begin
44+
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
45+
g = SimpleValueGraph(CycleGraph(V(5)), E_VAL)
46+
u = rand(vertices(g))
47+
key = rand(1:length(E_VAL.parameters))
48+
w1 = rand_sample(E_VAL)[key]
49+
w2 = rand_sample(E_VAL)[key]
50+
map_edgevals!(g, key) do s, d, value
51+
return (u (s, d)) ? w1 : w2
52+
end
53+
@test all(edges(g)) do e
54+
return val(e)[key] == (u (src(e), dst(e)) ? w1 : w2)
55+
end
56+
end
57+
end
58+
59+
next!(pbar)
60+
61+
# TODO this should be in a file test/operators.jl
62+
@testset "map_edgevals! with key directed" begin
63+
for (V, E_VAL) in product(test_vertex_types, filter(T -> T <: TupleOrNamedTuple, test_edgeval_types))
64+
g = SimpleValueDiGraph(CycleDiGraph(V(5)), E_VAL)
65+
u = rand(vertices(g))
66+
key = rand(1:length(E_VAL.parameters))
67+
w1 = rand_sample(E_VAL)[key]
68+
w2 = rand_sample(E_VAL)[key]
69+
map_edgevals!(g, key) do s, d, value
70+
return (s == u) ? w1 : w2
71+
end
72+
@test all(edges(g)) do e
73+
return val(e)[key] == (u == src(e) ? w1 : w2)
74+
end
75+
end
76+
end
77+
78+
next!(pbar)
79+
80+
end # testset
81+
82+
83+

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ using ProgressMeter
44
using TerminalMenus
55
using Test
66

7+
using Base.Iterators: product
8+
import SimpleValueGraphs.TupleOrNamedTuple
9+
710
include("testutils.jl")
811

912
tests = Dict("LightGraphs compability" => () -> include("lightgraphs_compatibility.jl"),
1013
"SimpleValueGraph interface" => () -> include("interface.jl"),
14+
"SimpleValueGraph operators" => () -> include("operators.jl"),
15+
"SimpleValueGraph matrices" => () -> include("matrices.jl"),
1116
)
1217

1318
@testset "SimpleValueGraphs" begin

0 commit comments

Comments
 (0)