Skip to content

Commit d525f46

Browse files
committed
added unit tests for directed line graph
1 parent 1b3282b commit d525f46

File tree

2 files changed

+58
-75
lines changed

2 files changed

+58
-75
lines changed

Project.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ ArnoldiMethod = "ec485272-7323-5ecc-a04f-4719b315124d"
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
88
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
99
Inflate = "d25df0c9-e2be-5dd7-82c8-3ad0b3e990b9"
10-
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
1110
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1211
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1312
SharedArrays = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

test/operators.jl

Lines changed: 58 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,48 @@
352352
@testset "Length: $(typeof(g))" for g in test_generic_graphs(SimpleGraph(100))
353353
@test length(g) == 10000
354354
end
355+
356+
@testset "Undirected Line Graph" begin
357+
@testset "Undirected Cycle Graphs" begin
358+
for n in 3:9
359+
g = cycle_graph(n)
360+
lg = line_graph(g) # checking if lg is an n-cycle
361+
@test nv(lg) == n
362+
@test ne(lg) == n
363+
@test is_connected(lg)
364+
@test all(degree(lg, v) == 2 for v in vertices(lg))
365+
end
366+
end
367+
368+
@testset "Undirected Path Graphs" begin
369+
for n in 2:9
370+
g = path_graph(n)
371+
lg = line_graph(g) # checking if lg is an n-1-path
372+
@test nv(lg) == n - 1
373+
@test ne(lg) == n - 2
374+
@test is_connected(lg)
375+
@test all(degree(lg, v) <= 2 for v in vertices(lg))
376+
@test any(degree(lg, v) == 1 for v in vertices(lg)) || n == 2 && ne(lg) == 0
377+
end
378+
end
379+
380+
@testset "Undirected Star Graphs" begin
381+
for n in 3:9
382+
g = star_graph(n)
383+
lg = line_graph(g) # checking if lg is a complete graph on n-1 vertices
384+
@test nv(lg) == n - 1
385+
@test ne(lg) == binomial(n - 1, 2) # lg must be a complete graph
386+
end
387+
end
388+
389+
@testset "Undirected Self-loops" begin
390+
g = SimpleGraph(2, [[2], [1, 2], Int[]])
391+
lg = line_graph(g)
392+
@test nv(lg) == 2 # only 2 edges (self-loop counts once)
393+
@test ne(lg) == 1 # only connection between edge 1-2 and self-loop 2-2
394+
end
395+
end
396+
355397
@testset "Directed Line Graph" begin
356398
@testset "Directed Cycle Graphs" begin
357399
for n in 3:9
@@ -360,6 +402,7 @@
360402
@test nv(lg) == n
361403
@test ne(lg) == n
362404
@test is_directed(lg)
405+
@test is_connected(lg)
363406
@test all(outdegree(lg, v) == 1 for v in vertices(lg))
364407
@test all(indegree(lg, v) == 1 for v in vertices(lg))
365408
end
@@ -373,91 +416,32 @@
373416
@test ne(lg) == n - 2
374417
@test is_directed(lg)
375418
@test is_connected(lg)
376-
@test all(outdegree(lg, v) <= 1 for v in vertices(lg))
377-
@test all(indegree(lg, v) <= 1 for v in vertices(lg))
378-
if n > 2
379-
@test indegree(lg, 1) == 0
380-
@test outdegree(lg, 1) == 1
381-
@test indegree(lg, nv(lg)) == 1
382-
@test outdegree(lg, nv(lg)) == 0
383-
end
419+
@test all(outdegree(lg, v) == (v < n - 1 ? 1 : 0) for v in vertices(lg))
420+
@test all(indegree(lg, v) == (v > 1 ? 1 : 0) for v in vertices(lg))
384421
end
385422
end
386423

387424
@testset "Directed Star Graphs" begin
388-
for n in 3:9
389-
g = star_digraph(n)
390-
lg = line_graph(g)
391-
@test nv(lg) == n - 1
392-
@test ne(lg) == 0
393-
end
394-
395-
for n in 3:9
396-
g = SimpleDiGraph(n)
397-
for i in 2:n
398-
add_edge!(g, 1, i)
399-
add_edge!(g, i, 1)
400-
end
401-
lg = line_graph(g)
402-
@test nv(lg) == 2*(n-1)
403-
@test ne(lg) == (n-1) + (n-1)*(n-1)
425+
for m in 0:4, n in 0:4
426+
g = SimpleDiGraph(m + n + 1)
427+
foreach(i -> add_edge!(g, i + 1, 1), 1:m)
428+
foreach(j -> add_edge!(g, 1, j + 1 + m), 1:n)
429+
lg = line_graph(g) # checking if lg is the complete bipartite digraph
430+
@test nv(lg) == m + n
431+
@test ne(lg) == m * n
432+
@test all(outdegree(lg, v) == 0 && indegree(lg, v) == m for v in 1:n)
433+
@test all(
434+
outdegree(lg, v) == n && indegree(lg, v) == 0 for v in (n + 1):(n + m)
435+
)
404436
end
405437
end
406438

407439
@testset "Directed Self-loops" begin
408-
g = SimpleDiGraph(2)
409-
add_edge!(g, 1, 1)
410-
add_edge!(g, 1, 2)
440+
g = SimpleDiGraph(2, [[1, 2], Int[], Int[]], [[1], [1], Int[]])
411441
lg = line_graph(g)
412442
@test nv(lg) == 2
413-
@test ne(lg) == 2
414-
@test has_edge(lg, 1, 1)
443+
@test ne(lg) == 1
415444
@test has_edge(lg, 1, 2)
416-
@test !has_edge(lg, 2, 1)
417-
@test !has_edge(lg, 2, 2)
418-
end
419-
end
420-
end
421-
422-
@testset "Undirected Line Graph" begin
423-
@testset "Undirected Cycle Graphs" begin
424-
for n in 3:9
425-
g = cycle_graph(n)
426-
lg = line_graph(g) # checking if lg is an n-cycle
427-
@test nv(lg) == n
428-
@test ne(lg) == n
429-
@test is_connected(lg)
430-
@test all(degree(lg, v) == 2 for v in vertices(lg))
431-
end
432-
end
433-
434-
@testset "Undirected Path Graphs" begin
435-
for n in 2:9
436-
g = path_graph(n)
437-
lg = line_graph(g) # checking if lg is an n-1-path
438-
@test nv(lg) == n - 1
439-
@test ne(lg) == n - 2
440-
@test is_connected(lg)
441-
@test all(degree(lg, v) <= 2 for v in vertices(lg))
442-
@test any(degree(lg, v) == 1 for v in vertices(lg)) || n == 2 && ne(lg) == 0
443445
end
444446
end
445-
446-
@testset "Undirected Star Graphs" begin
447-
for n in 3:9
448-
g = star_graph(n)
449-
lg = line_graph(g) # checking if lg is a complete graph on n-1 vertices
450-
@test nv(lg) == n - 1
451-
@test ne(lg) == binomial(n - 1, 2) # lg must be a complete graph
452-
end
453-
end
454-
455-
@testset "Undirected Self-loops" begin
456-
g = SimpleGraph(2, [[2], [1, 2], Int[]])
457-
lg = line_graph(g)
458-
@test nv(lg) == 2 # only 2 edges (self-loop counts once)
459-
@test ne(lg) == 1 # only connection between edge 1-2 and self-loop 2-2
460-
end
461-
462-
463447
end

0 commit comments

Comments
 (0)