Skip to content

Commit 91aa42d

Browse files
committed
Add unit tests for Forest
1 parent 3d52bc6 commit 91aa42d

4 files changed

Lines changed: 95 additions & 19 deletions

File tree

src/coloring.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,15 @@ end
432432
function TreeSet(forest::Forest{Int}, nvertices::Int)
433433
# Forest is a structure defined in forest.jl
434434
# - forest.intmap: a dictionary that maps an edge (i, j) to an integer k
435-
# - forest.ntrees: the number of trees in the forest
436-
ntrees = forest.ntrees
435+
# - forest.num_trees: the number of trees in the forest
436+
nt = forest.num_trees
437437

438438
# dictionary that maps a tree's root to the index of the tree
439439
roots = Dict{Int,Int}()
440-
sizehint!(roots, ntrees)
440+
sizehint!(roots, nt)
441441

442442
# vector of dictionaries where each dictionary stores the neighbors of each vertex in a tree
443-
trees = [Dict{Int,Vector{Int}}() for i in 1:ntrees]
443+
trees = [Dict{Int,Vector{Int}}() for i in 1:nt]
444444

445445
# counter of the number of roots found
446446
k = 0
@@ -476,11 +476,11 @@ function TreeSet(forest::Forest{Int}, nvertices::Int)
476476
degrees = Vector{Int}(undef, nvertices)
477477

478478
# reverse breadth first (BFS) traversal order for each tree in the forest
479-
reverse_bfs_orders = [Tuple{Int,Int}[] for i in 1:ntrees]
479+
reverse_bfs_orders = [Tuple{Int,Int}[] for i in 1:nt]
480480

481481
# nvmax is the number of vertices of the biggest tree in the forest
482482
nvmax = 0
483-
for k in 1:ntrees
483+
for k in 1:nt
484484
nb_vertices_tree = length(trees[k])
485485
nvmax = max(nvmax, nb_vertices_tree)
486486
end
@@ -490,9 +490,9 @@ function TreeSet(forest::Forest{Int}, nvertices::Int)
490490

491491
# Specify if each tree in the forest is a star,
492492
# meaning that one vertex is directly connected to all other vertices in the tree
493-
is_star = Vector{Bool}(undef, ntrees)
493+
is_star = Vector{Bool}(undef, nt)
494494

495-
for k in 1:ntrees
495+
for k in 1:nt
496496
tree = trees[k]
497497

498498
# Boolean indicating whether the current tree is a star (a single central vertex connected to all others)

src/forest.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ Structure that provides fast union-find operations for constructing a forest dur
1010
$TYPEDFIELDS
1111
"""
1212
mutable struct Forest{T<:Integer}
13-
"current number of edges added to the forest"
14-
counter::T
13+
"current number of edges in the forest"
14+
num_edges::T
15+
"current number of distinct trees in the forest"
16+
num_trees::T
1517
"dictionary mapping each edge represented as a tuple of vertices to its unique integer index"
1618
intmap::Dict{Tuple{T,T},T}
1719
"vector storing the index of a parent in the tree for each edge, used in union-find operations"
1820
parents::Vector{T}
1921
"vector approximating the depth of each tree to optimize path compression"
2022
ranks::Vector{T}
21-
"current number of distinct trees in the forest"
22-
ntrees::T
2323
end
2424

2525
function Forest{T}(n::Integer) where {T<:Integer}
26-
counter = zero(T)
26+
num_edges = zero(T)
27+
num_trees = zero(T)
2728
intmap = Dict{Tuple{T,T},T}()
2829
sizehint!(intmap, n)
2930
parents = collect(Base.OneTo(T(n)))
3031
ranks = zeros(T, T(n))
31-
ntrees = zero(T)
32-
return Forest{T}(counter, intmap, parents, ranks, ntrees)
32+
return Forest{T}(num_edges, num_trees, intmap, parents, ranks)
3333
end
3434

3535
function Base.push!(forest::Forest{T}, edge::Tuple{T,T}) where {T<:Integer}
36-
forest.counter += 1
37-
forest.intmap[edge] = forest.counter
38-
forest.ntrees += one(T)
36+
forest.num_edges += 1
37+
forest.intmap[edge] = forest.num_edges
38+
forest.num_trees += one(T)
3939
return forest
4040
end
4141

@@ -63,6 +63,6 @@ function root_union!(forest::Forest{T}, index_edge1::T, index_edge2::T) where {T
6363
rks[index_edge1] += one(T)
6464
end
6565
parents[index_edge2] = index_edge1
66-
forest.ntrees -= one(T)
66+
forest.num_trees -= one(T)
6767
return nothing
6868
end

test/forest.jl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
@testset "Constructor Forest" begin
2+
forest = Forest{Int}(5)
3+
4+
@test forest.num_edges == 0
5+
@test forest.num_trees == 0
6+
@test length(forest.intmap) == 0
7+
@test length(forest.parents) == 5
8+
@test all(forest.parents .== 1:5)
9+
@test all(forest.ranks .== 0)
10+
end
11+
12+
@testset "Push edge" begin
13+
forest = Forest{Int}(5)
14+
15+
push!(forest, (1, 2))
16+
@test forest.num_edges == 1
17+
@test forest.num_trees == 1
18+
@test haskey(forest.intmap, (1, 2))
19+
@test forest.intmap[(1, 2)] == 1
20+
@test forest.num_trees == 1
21+
22+
push!(forest, (3, 4))
23+
@test forest.num_edges == 2
24+
@test forest.num_trees == 2
25+
@test haskey(forest.intmap, (3, 4))
26+
@test forest.intmap[(3, 4)] == 2
27+
@test forest.num_trees == 2
28+
end
29+
30+
@testset "Find root" begin
31+
forest = Forest{Int}(5)
32+
push!(forest, (1, 2))
33+
push!(forest, (3, 4))
34+
35+
@test find_root!(forest, (1, 2)) == 1
36+
@test find_root!(forest, (3, 4)) == 2
37+
end
38+
39+
@testset "Root union" begin
40+
forest = Forest{Int}(5)
41+
push!(forest, (1, 2))
42+
push!(forest, (4, 5))
43+
push!(forest, (2, 4))
44+
@test forest.num_trees = 3
45+
46+
root1 = find_root!(forest, (1, 2))
47+
root3 = find_root!(forest, (2, 4))
48+
@test root1 != root3
49+
50+
root_union!(forest, root1, root3)
51+
@test find_root!(forest, (2,4)) == 1
52+
@test forest.parents[1] == 1
53+
@test forest.parents[3] == 1
54+
@test forest.ranks[1] == 1
55+
@test forest.ranks[3] == 0
56+
@test forest.num_trees = 2
57+
58+
root1 = find_root!(forest, (1, 2))
59+
root2 = find_root!(forest, (4, 5))
60+
@test root1 != root2
61+
root_union!(forest, root1, root2)
62+
@test find_root!(forest, (4, 5)) == 1
63+
@test forest.parents[1] == 1
64+
@test forest.parents[2] == 1
65+
@test forest.ranks[1] == 1
66+
@test forest.ranks[2] == 0
67+
@test forest.num_trees == 1
68+
69+
push!(forest, (1, 4))
70+
@test forest.num_trees == 2
71+
@test forest.intmap[(1, 4)] == 4
72+
@test forest.parents[4] == 4
73+
end

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ include("utils.jl")
3333
@testset "Graph" begin
3434
include("graph.jl")
3535
end
36+
@testset "Forest" begin
37+
include("forest.jl")
38+
end
3639
@testset "Order" begin
3740
include("order.jl")
3841
end

0 commit comments

Comments
 (0)