Skip to content

Commit 27fc759

Browse files
authored
test: merged and refactored components.R tests (#1702)
1 parent 9556c84 commit 27fc759

9 files changed

+234
-242
lines changed

R/components.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,7 @@ is_biconnected <- is_biconnected_impl
319319
#' @rdname components
320320
#' @export
321321
largest_component <- function(graph, mode = c("weak", "strong")) {
322-
if (!is_igraph(graph)) {
323-
stop("Not a graph object")
324-
}
322+
ensure_igraph(graph)
325323

326324
comps <- components(graph, mode = mode)
327325

tests/testthat/test-articulation.points.R

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/testthat/test-biconnected.components.R

Lines changed: 0 additions & 74 deletions
This file was deleted.

tests/testthat/test-bridges.R

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/testthat/test-clusters.R

Lines changed: 0 additions & 50 deletions
This file was deleted.

tests/testthat/test-community.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,15 @@ test_that("split_join_distance works", {
435435
com_sjd <- unname(split_join_distance(karate_split1, karate_split2))
436436
expect_equal(com_sjd, c(0, 17))
437437
})
438+
439+
test_that("groups works", {
440+
g <- make_ring(10) + make_full_graph(5)
441+
gr <- groups(components(g))
442+
443+
expect_equal(gr, structure(list(`1` = 1:10, `2` = 11:15), .Dim = 2L, .Dimnames = list(c("1", "2"))))
444+
445+
V(g)$name <- letters[1:15]
446+
gr <- groups(components(g))
447+
448+
expect_equal(gr, structure(list(`1` = letters[1:10], `2` = letters[11:15]), .Dim = 2L, .Dimnames = list(c("1", "2"))))
449+
})

tests/testthat/test-components.R

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
test_that("components works", {
2+
withr::local_seed(42)
3+
4+
random_largest_component <- function(n) {
5+
largest_component(sample_gnp(n, 1 / n))
6+
}
7+
8+
random_lg_list <- lapply(1:30, function(x) random_largest_component(sample(100, 1)))
9+
lg_size <- sapply(random_lg_list, vcount)
10+
11+
dis_union <- disjoint_union(random_lg_list)
12+
clu <- components(dis_union)
13+
14+
expect_equal(as.numeric(table(clu$membership)), clu$csize)
15+
expect_equal(sort(clu$csize), sort(lg_size))
16+
expect_equal(clu$no, length(random_lg_list))
17+
})
18+
19+
test_that("components names results", {
20+
g <- make_ring(10) + make_full_graph(5)
21+
V(g)$name <- letters[1:15]
22+
23+
clu <- components(g)
24+
expect_named(clu$membership, letters[1:15])
25+
})
26+
27+
test_that("is_connected works", {
28+
ring <- make_ring(10)
29+
expect_true(is_connected(ring))
30+
31+
g <- make_ring(10) + make_full_graph(5)
32+
expect_false(is_connected(g))
33+
})
34+
35+
test_that("is_connected returns FALSE for the null graph", {
36+
empty <- make_empty_graph(0)
37+
expect_false(is_connected(empty))
38+
})
39+
40+
test_that("decompose works", {
41+
gnp <- sample_gnp(1000, 1 / 1500)
42+
gnp_decomposed <- decompose(gnp)
43+
gnp_comps <- components(gnp)
44+
Gsizes <- sapply(gnp_decomposed, vcount)
45+
expect_equal(sort(gnp_comps$csize), sort(Gsizes))
46+
})
47+
48+
test_that("decompose works for many components", {
49+
large_empty <- make_empty_graph(5001)
50+
large_empty_decompose <- decompose(large_empty)
51+
expect_length(large_empty_decompose, 5001)
52+
})
53+
54+
test_that("decompose works for many components and attributes", {
55+
large_empty <- make_empty_graph(5001)
56+
V(large_empty)$name <- seq_len(vcount(large_empty))
57+
large_empty_decompose <- decompose(large_empty)
58+
expect_length(large_empty_decompose, 5001)
59+
})
60+
61+
test_that("decompose keeps attributes", {
62+
g <- make_ring(10) + make_ring(5)
63+
V(g)$name <- letters[1:(10 + 5)]
64+
E(g)$name <- apply(as_edgelist(g), 1, paste, collapse = "-")
65+
g_decompose <- decompose(g)
66+
g_decompose <- g_decompose[order(sapply(g_decompose, vcount))]
67+
68+
expect_length(g_decompose, 2)
69+
expect_equal(sapply(g_decompose, vcount), c(5, 10))
70+
expect_equal(V(g_decompose[[1]])$name, letters[1:5 + 10])
71+
expect_equal(V(g_decompose[[2]])$name, letters[1:10])
72+
e1 <- apply(as_edgelist(g_decompose[[1]]), 1, paste, collapse = "-")
73+
e2 <- apply(as_edgelist(g_decompose[[2]]), 1, paste, collapse = "-")
74+
expect_equal(E(g_decompose[[1]])$name, e1)
75+
expect_equal(E(g_decompose[[2]])$name, e2)
76+
})
77+
78+
test_that("component_distribution() finds correct distribution", {
79+
g <- graph_from_literal(
80+
A,
81+
B - C,
82+
D - E - F,
83+
G - H
84+
)
85+
86+
ref <- c(0.00, 0.25, 0.50, 0.25)
87+
88+
expect_equal(component_distribution(g), ref)
89+
})
90+
91+
test_that("largest component is actually the largest", {
92+
star <- make_star(20, "undirected")
93+
ring <- make_ring(10)
94+
95+
dis_union <- disjoint_union(star, ring)
96+
97+
expect_isomorphic(largest_component(dis_union), star)
98+
})
99+
100+
test_that("largest strongly and weakly components are correct", {
101+
g <- graph_from_literal(
102+
A - +B,
103+
B - +C,
104+
C - +A,
105+
C - +D,
106+
E
107+
)
108+
109+
strongly <- graph_from_literal(
110+
A - +B,
111+
B - +C,
112+
C - +A
113+
)
114+
expect_true(isomorphic(largest_component(g, "strong"), strongly))
115+
116+
weakly <- graph_from_literal(
117+
A - +B,
118+
B - +C,
119+
C - +A,
120+
C - +D
121+
)
122+
expect_true(isomorphic(largest_component(g, "weak"), weakly))
123+
})
124+
125+
test_that("the largest component of a null graph is a valid null graph", {
126+
nullgraph <- make_empty_graph(0)
127+
128+
expect_true(isomorphic(largest_component(make_empty_graph(0)), nullgraph))
129+
})
130+
131+
test_that("articulation_points works", {
132+
g <- make_full_graph(5) + make_full_graph(5)
133+
g_comps <- components(g)$membership
134+
g <- add_edges(g, c(match(1, g_comps), match(2, g_comps)))
135+
136+
ap <- as.vector(articulation_points(g))
137+
deg <- degree(g)
138+
expect_equal(sort(which(deg == max(deg))), sort(ap))
139+
})
140+
141+
test_that("bridges works", {
142+
kite <- make_graph("krackhardt_kite")
143+
expect_equal(sort(as.vector(bridges(kite))), (ecount(kite) - 1):(ecount(kite)))
144+
})
145+
146+
test_that("biconnected_components works", {
147+
g <- make_full_graph(5) + make_full_graph(5)
148+
g_comps <- components(g)$membership
149+
g <- add_edges(g, c(match(1, g_comps), match(2, g_comps)))
150+
151+
sortlist <- function(list) {
152+
list <- lapply(list, sort)
153+
list <- lapply(list, as.vector)
154+
list[order(sapply(list, paste, collapse = "x"))]
155+
}
156+
157+
bc <- biconnected_components(g)
158+
expect_equal(bc$no, 3)
159+
expect_equal(sortlist(bc$tree_edges), list(c(11, 15, 18, 20), c(1, 5, 8, 10), 21))
160+
expect_equal(sortlist(bc$component_edges), list(11:20, 1:10, 21))
161+
expect_equal(sortlist(bc$components), list(1:5, c(1, 6), 6:10))
162+
expect_equal(sort(as.vector(bc$articulation_points)), c(1, 6))
163+
164+
expect_equal(sort(names(bc)), c(
165+
"articulation_points",
166+
"component_edges",
167+
"components",
168+
"no",
169+
"tree_edges"
170+
))
171+
expect_s3_class(bc$articulation_points, "igraph.vs")
172+
expect_s3_class(bc$components[[1]], "igraph.vs")
173+
expect_s3_class(bc$component_edges[[1]], "igraph.es")
174+
})
175+
176+
test_that("biconnected_components works without igraph.vs.es", {
177+
local_igraph_options(return.vs.es = FALSE)
178+
179+
g <- make_full_graph(5) + make_full_graph(5)
180+
clu <- components(g)$membership
181+
g <- add_edges(g, c(match(1, clu), match(2, clu)))
182+
183+
sortlist <- function(list) {
184+
list <- lapply(list, sort)
185+
list[order(sapply(list, paste, collapse = "x"))]
186+
}
187+
188+
bc <- biconnected_components(g)
189+
expect_equal(bc$no, 3)
190+
expect_equal(sortlist(bc$tree_edges), list(c(11, 15, 18, 20), c(1, 5, 8, 10), 21))
191+
expect_equal(sortlist(bc$component_edges), list(11:20, 1:10, 21))
192+
expect_equal(sortlist(bc$components), list(1:5, c(1, 6), 6:10))
193+
expect_equal(sort(bc$articulation_points), c(1, 6))
194+
195+
expect_equal(sort(names(bc)), c(
196+
"articulation_points",
197+
"component_edges",
198+
"components",
199+
"no",
200+
"tree_edges"
201+
))
202+
})
203+
204+
test_that("is_biconnected works", {
205+
g <- make_full_graph(0)
206+
expect_false(is_biconnected(g))
207+
208+
g <- make_full_graph(1)
209+
expect_false(is_biconnected(g))
210+
211+
g <- make_full_graph(2)
212+
expect_true(is_biconnected(g))
213+
214+
g <- make_full_graph(3)
215+
expect_true(is_biconnected(g))
216+
217+
g <- make_graph(c(1, 2, 2, 3, 3, 1, 1, 4, 4, 4))
218+
expect_false(is_biconnected(g))
219+
})

0 commit comments

Comments
 (0)