/
test_graph.jl
239 lines (209 loc) · 9.15 KB
/
test_graph.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Graphs.flip_direction!(edge::Edge{Float64}) = (edge.data = -edge.data)
@testset "graphs" begin
@testset "disconnected" begin
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
verts = [Vertex{Int64}(rand(Int64)) for i = 1 : 10]
for v in verts
add_vertex!(graph, v)
end
@test num_vertices(graph) == length(verts)
@test num_edges(graph) == 0
for v in verts
@test length(in_edges(v, graph)) == 0
@test length(out_edges(v, graph)) == 0
@test length(in_neighbors(v, graph)) == 0
@test length(out_neighbors(v, graph)) == 0
end
@test isempty(setdiff(vertices(graph), verts))
@test isempty(edges(graph))
show(DevNull, graph)
end
@testset "tree graph" begin
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
root = Vertex(rand(Int64))
add_vertex!(graph, root)
nedges = 15
for i = 1 : nedges
parent = rand(vertices(graph))
child = Vertex(rand(Int64))
edge = Edge(rand())
add_edge!(graph, parent, child, edge)
end
@test num_vertices(graph) == nedges + 1
@test num_edges(graph) == nedges
for v in vertices(graph)
if v == root
@test length(in_edges(v, graph)) == 0
@test length(out_edges(v, graph)) > 0
@test length(in_neighbors(v, graph)) == 0
@test length(out_neighbors(v, graph)) > 0
else
@test length(in_edges(v, graph)) == 1
@test length(in_neighbors(v, graph)) == 1
end
end
show(DevNull, graph)
end
@testset "remove_vertex!" begin
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
edge = Edge(rand())
add_edge!(graph, Vertex(rand(Int64)), Vertex(rand(Int64)), edge)
for v in vertices(graph)
@test_throws ErrorException remove_vertex!(graph, v)
end
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
for i = 1 : 100
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float64(i)))
end
original = deepcopy(graph)
vertex = rand(collect(filter(v -> isempty(in_edges(v, graph)) && isempty(out_edges(v, graph)), vertices(graph))))
remove_vertex!(graph, vertex)
@test vertex ∉ vertices(graph)
for v in vertices(graph)
v_orig = vertices(original)[findfirst(v_orig -> data(v) == data(v_orig), vertices(original))]
for (e, e_orig) in zip(in_edges(v, graph), in_edges(v_orig, original))
@test data(e) == data(e_orig)
end
for (e, e_orig) in zip(out_edges(v, graph), out_edges(v_orig, original))
@test data(e) == data(e_orig)
end
end
end
@testset "remove_edge!" begin
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
for i = 1 : 100
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float64(i)))
end
original = deepcopy(graph)
edge = rand(edges(graph))
remove_edge!(graph, edge)
@test edge ∉ edges(graph)
@test num_edges(graph) == num_edges(original) - 1
for v in vertices(graph)
@test edge ∉ in_edges(v, graph)
@test edge ∉ out_edges(v, graph)
end
for e in edges(graph)
e_orig = edges(original)[findfirst(e_orig -> data(e) == data(e_orig), edges(original))]
@test data(source(e, graph)) == data(source(e_orig, original))
@test data(target(e, graph)) == data(target(e_orig, original))
end
end
@testset "rewire! / flip_direction!" begin
graph = DirectedGraph{Vertex{Int64}, Edge{Float64}}()
for i = 1 : 100
add_vertex!(graph, Vertex(i))
end
for i = 1 : num_vertices(graph) - 1
add_edge!(graph, rand(vertices(graph)), rand(vertices(graph)), Edge(Float64(i)))
end
original = deepcopy(graph)
edge = rand(edges(graph))
oldsource = source(edge, graph)
oldtarget = target(edge, graph)
non_source_vertices = delete!(Set(vertices(graph)), oldsource)
non_target_vertices = delete!(Set(vertices(graph)), oldtarget)
newsource = rand(collect(non_source_vertices))
newtarget = rand(collect(non_target_vertices))
rewire!(graph, edge, newsource, newtarget)
@test source(edge, graph) == newsource
@test target(edge, graph) == newtarget
@test edge ∈ out_edges(newsource, graph)
@test edge ∈ in_edges(newtarget, graph)
@test edge ∉ out_edges(oldsource, graph)
@test edge ∉ in_edges(oldtarget, graph)
@test data.(vertices(original)) == data.(vertices(graph))
for e in filter(e -> e != edge, edges(graph))
e_orig = edges(original)[findfirst(e_orig -> data(e) == data(e_orig), edges(original))]
@test data(source(e, graph)) == data(source(e_orig, original))
@test data(target(e, graph)) == data(target(e_orig, original))
end
olddata = data(edge)
flip_direction!(edge, graph)
@test source(edge, graph) == newtarget
@test target(edge, graph) == newsource
@test data(edge) == -olddata
end
@testset "SpanningTree" begin
rootdata = 0
# graph1: tree grown incrementally
graph1 = DirectedGraph{Vertex{Int64}, Edge{Int32}}()
root1 = Vertex(rootdata)
add_vertex!(graph1, root1)
tree1 = SpanningTree(graph1, root1)
# graph2: tree constructed after graph is built
graph2 = DirectedGraph{Vertex{Int64}, Edge{Int32}}()
root2 = Vertex(rootdata)
add_vertex!(graph2, root2)
nedges = 15
for i = 1 : nedges
parentind = rand(1 : num_vertices(graph1))
childdata = i
edgedata = Int32(i + 3)
add_edge!(tree1, vertices(tree1)[parentind], Vertex(childdata), Edge(edgedata))
add_edge!(graph2, vertices(graph2)[parentind], Vertex(childdata), Edge(edgedata))
end
tree2 = SpanningTree(graph2, root2)
@test all(data.(vertices(tree1)) == data.(vertices(tree2)))
for (v1, v2) in zip(vertices(tree1), vertices(tree2))
if v1 == root(tree1)
@test v2 == root(tree2)
else
@test data(edge_to_parent(v1, tree1)) == data(edge_to_parent(v2, tree2))
outedgedata1 = data.(collect(edges_to_children(v1, tree1)))
outedgedata2 = data.(collect(edges_to_children(v2, tree2)))
@test isempty(setdiff(outedgedata1, outedgedata2))
@test isempty(setdiff(out_edges(v1, graph1), edges_to_children(v1, tree1)))
@test isempty(setdiff(out_edges(v2, graph2), edges_to_children(v2, tree2)))
end
end
tree = tree1
show(DevNull, tree)
@test_throws AssertionError add_edge!(tree, rand(vertices(tree)), rand(vertices(tree)), Edge(rand(Int32)))
for src in vertices(tree)
for dest in vertices(tree)
src_ancestors = ancestors(src, tree)
dest_ancestors = ancestors(dest, tree)
lca = lowest_common_ancestor(src, dest, tree)
p = path(src, dest, tree)
show(DevNull, p)
@inferred collect(p)
@test source(p) == src
@test target(p) == dest
source_to_lca = collect(edge for (edge, direction) in p if direction == :up)
target_to_lca = reverse!(collect(edge for (edge, direction) in p if direction == :down))
for (v, v_ancestors, pathsegment) in [(src, src_ancestors, source_to_lca); (dest, dest_ancestors, target_to_lca)]
if v == root(tree)
@test tree_index(v, tree) == 1
@test lca == v
@test length(v_ancestors) == 1
@test isempty(pathsegment)
end
for v_ancestor in v_ancestors
@test tree_index(v_ancestor, tree) <= tree_index(v, tree)
end
@test lca ∈ v_ancestors
if v != lca
@test source(last(pathsegment), tree) == lca
end
end
for v in intersect(src_ancestors, dest_ancestors)
@test tree_index(v, tree) <= tree_index(lca, tree)
if v != lca
@test v ∉ (v -> source(v, tree)).(source_to_lca)
@test v ∉ (v -> source(v, tree)).(target_to_lca)
end
end
for v in setdiff(src_ancestors, dest_ancestors)
@test tree_index(v, tree) > tree_index(lca, tree)
end
end
end
end
end