Skip to content

Commit c660306

Browse files
Add generic graphs to help with testing (#133)
1 parent 4cef890 commit c660306

File tree

5 files changed

+94
-8
lines changed

5 files changed

+94
-8
lines changed

src/Graphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ include("vertexcover/degree_vertex_cover.jl")
538538
include("vertexcover/random_vertex_cover.jl")
539539
include("Experimental/Experimental.jl")
540540
include("Parallel/Parallel.jl")
541+
include("Test/Test.jl")
541542

542543
using .LinAlg
543544
end # module

src/Test/Test.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
"""
3+
Graphs.Test
4+
5+
A module that provides utilities for testing functions that should work with any `Graphs.AbstractGraph`.
6+
"""
7+
module Test
8+
9+
using Graphs
10+
11+
export GenericEdge, GenericGraph, GenericDiGraph
12+
13+
"""
14+
GenericEdge <: Graphs.AbstractEdge
15+
16+
An edge type that can be used to tests functions that relay on the Graphs.jl interface.
17+
18+
"""
19+
struct GenericEdge{T} <: Graphs.AbstractEdge{T}
20+
e::Graphs.SimpleEdge{T}
21+
end
22+
23+
Graphs.src(e::GenericEdge) = Graphs.src(e.e)
24+
25+
Graphs.dst(e::GenericEdge) = Graphs.dst(e.e)
26+
27+
Base.reverse(e::GenericEdge) = GenericEdge(reverse(e.e))
28+
29+
"""
30+
GenericGraph{T} <: Graphs.AbstractGraph{T}
31+
32+
An undirected graph type that can be used to tests functions that relay on the Graphs.jl interface.
33+
34+
"""
35+
struct GenericGraph{T} <: Graphs.AbstractGraph{T}
36+
g::SimpleGraph{T}
37+
end
38+
39+
"""
40+
GenericDiGraph{T} <: Graphs.AbstractGraph{T}
41+
42+
A directed graph type that can be used to tests functions that relay on the Graphs.jl interface.
43+
44+
"""
45+
struct GenericDiGraph{T} <: Graphs.AbstractGraph{T}
46+
g::SimpleDiGraph{T}
47+
end
48+
49+
Graphs.is_directed(::Type{<:GenericGraph}) = false
50+
Graphs.is_directed(::Type{<:GenericDiGraph}) = true
51+
52+
Base.eltype(g::GenericGraph) = eltype(g.g)
53+
Base.eltype(g::GenericDiGraph) = eltype(g.g)
54+
55+
Graphs.edges(g::GenericGraph) = (GenericEdge(e) for e in Graphs.edges(g.g))
56+
Graphs.edges(g::GenericDiGraph) = (GenericEdge(e) for e in Graphs.edges(g.g))
57+
58+
Graphs.edgetype(g::GenericGraph) = GenericEdge{eltype(g)}
59+
Graphs.edgetype(g::GenericDiGraph) = GenericEdge{eltype(g)}
60+
61+
Graphs.has_edge(g::GenericGraph, s, d) = Graphs.has_edge(g.g, s, d)
62+
Graphs.has_edge(g::GenericDiGraph, s, d) = Graphs.has_edge(g.g, s, d)
63+
64+
Graphs.has_vertex(g::GenericGraph, v) = Graphs.has_vertex(g.g, v)
65+
Graphs.has_vertex(g::GenericDiGraph, v) = Graphs.has_vertex(g.g, v)
66+
67+
Graphs.inneighbors(g::GenericGraph, v) = (u for u in Graphs.inneighbors(g.g, v))
68+
Graphs.inneighbors(g::GenericDiGraph, v) = (u for u in Graphs.inneighbors(g.g, v))
69+
70+
Graphs.outneighbors(g::GenericGraph, v) = (u for u in Graphs.outneighbors(g.g, v))
71+
Graphs.outneighbors(g::GenericDiGraph, v) = (u for u in Graphs.outneighbors(g.g, v))
72+
73+
Graphs.ne(g::GenericGraph) = Graphs.ne(g.g)
74+
Graphs.ne(g::GenericDiGraph) = Graphs.ne(g.g)
75+
76+
Graphs.nv(g::GenericGraph) = Graphs.nv(g.g)
77+
Graphs.nv(g::GenericDiGraph) = Graphs.nv(g.g)
78+
79+
Graphs.vertices(g::GenericGraph) = (v for v in Graphs.vertices(g.g))
80+
Graphs.vertices(g::GenericDiGraph) = (v for v in Graphs.vertices(g.g))
81+
82+
end # module

test/core.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
@testset "Core" begin
2-
e2 = Edge(1, 3)
3-
e3 = Edge(1, 4)
2+
e2 = GenericEdge(Edge(1, 3))
3+
e3 = GenericEdge(Edge(1, 4))
4+
# TODO do these tests make sense? One might define an edge type for some undirected
5+
# graph that is more like a set than a tuple -then reverse would not change the order
46
@test @inferred(is_ordered(e2))
57
@test @inferred(!is_ordered(reverse(e3)))
68

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using Graphs
44
using Graphs.SimpleGraphs
55
using Graphs.Experimental
66
using JuliaFormatter
7+
using Graphs.Test
78
using Test
89
using SparseArrays
910
using LinearAlgebra

test/traversals/dfs.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626
@testset "dfs_tree" begin
2727
for g in testdigraphs(g5)
28-
z = @inferred(dfs_tree(g, 1))
28+
z = @inferred(dfs_tree(GenericDiGraph(g), 1))
2929
@test ne(z) == 3 && nv(z) == 4
3030
@test !has_edge(z, 1, 3)
3131
@test !is_cyclic(g)
@@ -49,19 +49,19 @@
4949

5050
@testset "topological_sort_by_dfs" begin
5151
for g in testdigraphs(g5)
52-
@test @inferred(topological_sort_by_dfs(g)) == [1, 2, 3, 4]
52+
@test @inferred(topological_sort_by_dfs(GenericDiGraph(g))) == [1, 2, 3, 4]
5353
end
5454

5555
for g in testdigraphs(gx)
56-
@test @inferred(is_cyclic(g))
57-
@test_throws ErrorException topological_sort_by_dfs(g)
56+
@test @inferred(is_cyclic(GenericDiGraph(g)))
57+
@test_throws ErrorException topological_sort_by_dfs(GenericDiGraph(g))
5858
end
5959
end
6060

6161
@testset "is_cyclic" begin
6262
for g in testgraphs(path_graph(2))
63-
@test !@inferred(is_cyclic(g))
64-
@test !@inferred(is_cyclic(zero(g)))
63+
@test !@inferred(is_cyclic(GenericGraph(g)))
64+
@test !@inferred(is_cyclic(GenericGraph(zero(g))))
6565
end
6666
for g in testgraphs(gcyclic)
6767
@test @inferred(is_cyclic(g))

0 commit comments

Comments
 (0)