diff --git a/R/components.R b/R/components.R index 8e09960dd4..991cee4f5f 100644 --- a/R/components.R +++ b/R/components.R @@ -319,9 +319,7 @@ is_biconnected <- is_biconnected_impl #' @rdname components #' @export largest_component <- function(graph, mode = c("weak", "strong")) { - if (!is_igraph(graph)) { - stop("Not a graph object") - } + ensure_igraph(graph) comps <- components(graph, mode = mode) diff --git a/tests/testthat/test-articulation.points.R b/tests/testthat/test-articulation.points.R deleted file mode 100644 index a85f69b367..0000000000 --- a/tests/testthat/test-articulation.points.R +++ /dev/null @@ -1,9 +0,0 @@ -test_that("articulation_points works", { - g <- make_full_graph(5) + make_full_graph(5) - clu <- components(g)$membership - g <- add_edges(g, c(match(1, clu), match(2, clu))) - - ap <- as.vector(articulation_points(g)) - deg <- degree(g) - expect_equal(sort(which(deg == max(deg))), sort(ap)) -}) diff --git a/tests/testthat/test-biconnected.components.R b/tests/testthat/test-biconnected.components.R deleted file mode 100644 index 99dddeabb7..0000000000 --- a/tests/testthat/test-biconnected.components.R +++ /dev/null @@ -1,74 +0,0 @@ -test_that("biconnected_components works", { - g <- make_full_graph(5) + make_full_graph(5) - clu <- components(g)$membership - g <- add_edges(g, c(match(1, clu), match(2, clu))) - - sortlist <- function(list) { - list <- lapply(list, sort) - list <- lapply(list, as.vector) - list[order(sapply(list, paste, collapse = "x"))] - } - - bc <- biconnected_components(g) - expect_equal(bc$no, 3) - expect_equal(sortlist(bc$tree_edges), list(c(11, 15, 18, 20), c(1, 5, 8, 10), 21)) - expect_equal(sortlist(bc$component_edges), list(11:20, 1:10, 21)) - expect_equal(sortlist(bc$components), list(1:5, c(1, 6), 6:10)) - expect_equal(sort(as.vector(bc$articulation_points)), c(1, 6)) - - expect_equal(sort(names(bc)), c( - "articulation_points", - "component_edges", - "components", - "no", - "tree_edges" - )) - expect_s3_class(bc$articulation_points, "igraph.vs") - expect_s3_class(bc$components[[1]], "igraph.vs") - expect_s3_class(bc$component_edges[[1]], "igraph.es") -}) - -test_that("biconnected_components works without igraph.vs.es", { - local_igraph_options(return.vs.es = FALSE) - - g <- make_full_graph(5) + make_full_graph(5) - clu <- components(g)$membership - g <- add_edges(g, c(match(1, clu), match(2, clu))) - - sortlist <- function(list) { - list <- lapply(list, sort) - list[order(sapply(list, paste, collapse = "x"))] - } - - bc <- biconnected_components(g) - expect_equal(bc$no, 3) - expect_equal(sortlist(bc$tree_edges), list(c(11, 15, 18, 20), c(1, 5, 8, 10), 21)) - expect_equal(sortlist(bc$component_edges), list(11:20, 1:10, 21)) - expect_equal(sortlist(bc$components), list(1:5, c(1, 6), 6:10)) - expect_equal(sort(bc$articulation_points), c(1, 6)) - - expect_equal(sort(names(bc)), c( - "articulation_points", - "component_edges", - "components", - "no", - "tree_edges" - )) -}) - -test_that("is_biconnected works", { - g <- make_full_graph(0) - expect_false(is_biconnected(g)) - - g <- make_full_graph(1) - expect_false(is_biconnected(g)) - - g <- make_full_graph(2) - expect_true(is_biconnected(g)) - - g <- make_full_graph(3) - expect_true(is_biconnected(g)) - - g <- make_graph(c(1, 2, 2, 3, 3, 1, 1, 4, 4, 4)) - expect_false(is_biconnected(g)) -}) diff --git a/tests/testthat/test-bridges.R b/tests/testthat/test-bridges.R deleted file mode 100644 index 4249d8c89e..0000000000 --- a/tests/testthat/test-bridges.R +++ /dev/null @@ -1,4 +0,0 @@ -test_that("bridges works", { - g <- make_graph("krackhardt_kite") - expect_equal(sort(as.vector(bridges(g))), (ecount(g) - 1):(ecount(g))) -}) diff --git a/tests/testthat/test-clusters.R b/tests/testthat/test-clusters.R deleted file mode 100644 index d2ba45d8f8..0000000000 --- a/tests/testthat/test-clusters.R +++ /dev/null @@ -1,50 +0,0 @@ -test_that("components works", { - withr::local_seed(42) - - rg <- function(n) { - largest_component(sample_gnp(n, 1 / n)) - } - - G <- lapply(1:30, function(x) rg(sample(100, 1))) - Gsize <- sapply(G, vcount) - - allg <- disjoint_union(G) - clu <- components(allg) - - expect_equal(as.numeric(table(clu$membership)), clu$csize) - expect_equal(sort(clu$csize), sort(Gsize)) - expect_equal(clu$no, length(G)) -}) - -test_that("components names results", { - g <- make_ring(10) + make_full_graph(5) - V(g)$name <- letters[1:15] - - clu <- components(g) - expect_equal(names(clu$membership), letters[1:15]) -}) - -test_that("groups works", { - g <- make_ring(10) + make_full_graph(5) - gr <- groups(components(g)) - - expect_equal(gr, structure(list(`1` = 1:10, `2` = 11:15), .Dim = 2L, .Dimnames = list(c("1", "2")))) - - V(g)$name <- letters[1:15] - gr <- groups(components(g)) - - expect_equal(gr, structure(list(`1` = letters[1:10], `2` = letters[11:15]), .Dim = 2L, .Dimnames = list(c("1", "2")))) -}) - -test_that("is_connected works", { - g <- make_ring(10) - expect_true(is_connected(g)) - - g <- make_ring(10) + make_full_graph(5) - expect_false(is_connected(g)) -}) - -test_that("is_connected returns FALSE for the null graph", { - g <- make_empty_graph(0) - expect_false(is_connected(g)) -}) diff --git a/tests/testthat/test-community.R b/tests/testthat/test-community.R index 69859330b1..cb570740b9 100644 --- a/tests/testthat/test-community.R +++ b/tests/testthat/test-community.R @@ -435,3 +435,15 @@ test_that("split_join_distance works", { com_sjd <- unname(split_join_distance(karate_split1, karate_split2)) expect_equal(com_sjd, c(0, 17)) }) + +test_that("groups works", { + g <- make_ring(10) + make_full_graph(5) + gr <- groups(components(g)) + + expect_equal(gr, structure(list(`1` = 1:10, `2` = 11:15), .Dim = 2L, .Dimnames = list(c("1", "2")))) + + V(g)$name <- letters[1:15] + gr <- groups(components(g)) + + expect_equal(gr, structure(list(`1` = letters[1:10], `2` = letters[11:15]), .Dim = 2L, .Dimnames = list(c("1", "2")))) +}) diff --git a/tests/testthat/test-components.R b/tests/testthat/test-components.R new file mode 100644 index 0000000000..50733ffd31 --- /dev/null +++ b/tests/testthat/test-components.R @@ -0,0 +1,219 @@ +test_that("components works", { + withr::local_seed(42) + + random_largest_component <- function(n) { + largest_component(sample_gnp(n, 1 / n)) + } + + random_lg_list <- lapply(1:30, function(x) random_largest_component(sample(100, 1))) + lg_size <- sapply(random_lg_list, vcount) + + dis_union <- disjoint_union(random_lg_list) + clu <- components(dis_union) + + expect_equal(as.numeric(table(clu$membership)), clu$csize) + expect_equal(sort(clu$csize), sort(lg_size)) + expect_equal(clu$no, length(random_lg_list)) +}) + +test_that("components names results", { + g <- make_ring(10) + make_full_graph(5) + V(g)$name <- letters[1:15] + + clu <- components(g) + expect_named(clu$membership, letters[1:15]) +}) + +test_that("is_connected works", { + ring <- make_ring(10) + expect_true(is_connected(ring)) + + g <- make_ring(10) + make_full_graph(5) + expect_false(is_connected(g)) +}) + +test_that("is_connected returns FALSE for the null graph", { + empty <- make_empty_graph(0) + expect_false(is_connected(empty)) +}) + +test_that("decompose works", { + gnp <- sample_gnp(1000, 1 / 1500) + gnp_decomposed <- decompose(gnp) + gnp_comps <- components(gnp) + Gsizes <- sapply(gnp_decomposed, vcount) + expect_equal(sort(gnp_comps$csize), sort(Gsizes)) +}) + +test_that("decompose works for many components", { + large_empty <- make_empty_graph(5001) + large_empty_decompose <- decompose(large_empty) + expect_length(large_empty_decompose, 5001) +}) + +test_that("decompose works for many components and attributes", { + large_empty <- make_empty_graph(5001) + V(large_empty)$name <- seq_len(vcount(large_empty)) + large_empty_decompose <- decompose(large_empty) + expect_length(large_empty_decompose, 5001) +}) + +test_that("decompose keeps attributes", { + g <- make_ring(10) + make_ring(5) + V(g)$name <- letters[1:(10 + 5)] + E(g)$name <- apply(as_edgelist(g), 1, paste, collapse = "-") + g_decompose <- decompose(g) + g_decompose <- g_decompose[order(sapply(g_decompose, vcount))] + + expect_length(g_decompose, 2) + expect_equal(sapply(g_decompose, vcount), c(5, 10)) + expect_equal(V(g_decompose[[1]])$name, letters[1:5 + 10]) + expect_equal(V(g_decompose[[2]])$name, letters[1:10]) + e1 <- apply(as_edgelist(g_decompose[[1]]), 1, paste, collapse = "-") + e2 <- apply(as_edgelist(g_decompose[[2]]), 1, paste, collapse = "-") + expect_equal(E(g_decompose[[1]])$name, e1) + expect_equal(E(g_decompose[[2]])$name, e2) +}) + +test_that("component_distribution() finds correct distribution", { + g <- graph_from_literal( + A, + B - C, + D - E - F, + G - H + ) + + ref <- c(0.00, 0.25, 0.50, 0.25) + + expect_equal(component_distribution(g), ref) +}) + +test_that("largest component is actually the largest", { + star <- make_star(20, "undirected") + ring <- make_ring(10) + + dis_union <- disjoint_union(star, ring) + + expect_isomorphic(largest_component(dis_union), star) +}) + +test_that("largest strongly and weakly components are correct", { + g <- graph_from_literal( + A - +B, + B - +C, + C - +A, + C - +D, + E + ) + + strongly <- graph_from_literal( + A - +B, + B - +C, + C - +A + ) + expect_true(isomorphic(largest_component(g, "strong"), strongly)) + + weakly <- graph_from_literal( + A - +B, + B - +C, + C - +A, + C - +D + ) + expect_true(isomorphic(largest_component(g, "weak"), weakly)) +}) + +test_that("the largest component of a null graph is a valid null graph", { + nullgraph <- make_empty_graph(0) + + expect_true(isomorphic(largest_component(make_empty_graph(0)), nullgraph)) +}) + +test_that("articulation_points works", { + g <- make_full_graph(5) + make_full_graph(5) + g_comps <- components(g)$membership + g <- add_edges(g, c(match(1, g_comps), match(2, g_comps))) + + ap <- as.vector(articulation_points(g)) + deg <- degree(g) + expect_equal(sort(which(deg == max(deg))), sort(ap)) +}) + +test_that("bridges works", { + kite <- make_graph("krackhardt_kite") + expect_equal(sort(as.vector(bridges(kite))), (ecount(kite) - 1):(ecount(kite))) +}) + +test_that("biconnected_components works", { + g <- make_full_graph(5) + make_full_graph(5) + g_comps <- components(g)$membership + g <- add_edges(g, c(match(1, g_comps), match(2, g_comps))) + + sortlist <- function(list) { + list <- lapply(list, sort) + list <- lapply(list, as.vector) + list[order(sapply(list, paste, collapse = "x"))] + } + + bc <- biconnected_components(g) + expect_equal(bc$no, 3) + expect_equal(sortlist(bc$tree_edges), list(c(11, 15, 18, 20), c(1, 5, 8, 10), 21)) + expect_equal(sortlist(bc$component_edges), list(11:20, 1:10, 21)) + expect_equal(sortlist(bc$components), list(1:5, c(1, 6), 6:10)) + expect_equal(sort(as.vector(bc$articulation_points)), c(1, 6)) + + expect_equal(sort(names(bc)), c( + "articulation_points", + "component_edges", + "components", + "no", + "tree_edges" + )) + expect_s3_class(bc$articulation_points, "igraph.vs") + expect_s3_class(bc$components[[1]], "igraph.vs") + expect_s3_class(bc$component_edges[[1]], "igraph.es") +}) + +test_that("biconnected_components works without igraph.vs.es", { + local_igraph_options(return.vs.es = FALSE) + + g <- make_full_graph(5) + make_full_graph(5) + clu <- components(g)$membership + g <- add_edges(g, c(match(1, clu), match(2, clu))) + + sortlist <- function(list) { + list <- lapply(list, sort) + list[order(sapply(list, paste, collapse = "x"))] + } + + bc <- biconnected_components(g) + expect_equal(bc$no, 3) + expect_equal(sortlist(bc$tree_edges), list(c(11, 15, 18, 20), c(1, 5, 8, 10), 21)) + expect_equal(sortlist(bc$component_edges), list(11:20, 1:10, 21)) + expect_equal(sortlist(bc$components), list(1:5, c(1, 6), 6:10)) + expect_equal(sort(bc$articulation_points), c(1, 6)) + + expect_equal(sort(names(bc)), c( + "articulation_points", + "component_edges", + "components", + "no", + "tree_edges" + )) +}) + +test_that("is_biconnected works", { + g <- make_full_graph(0) + expect_false(is_biconnected(g)) + + g <- make_full_graph(1) + expect_false(is_biconnected(g)) + + g <- make_full_graph(2) + expect_true(is_biconnected(g)) + + g <- make_full_graph(3) + expect_true(is_biconnected(g)) + + g <- make_graph(c(1, 2, 2, 3, 3, 1, 1, 4, 4, 4)) + expect_false(is_biconnected(g)) +}) diff --git a/tests/testthat/test-decompose.graph.R b/tests/testthat/test-decompose.graph.R deleted file mode 100644 index 3b49ee23f5..0000000000 --- a/tests/testthat/test-decompose.graph.R +++ /dev/null @@ -1,47 +0,0 @@ -test_that("decompose works", { - g <- sample_gnp(1000, 1 / 1500) - G <- decompose(g) - clu <- components(g) - Gsizes <- sapply(G, vcount) - expect_equal(sort(clu$csize), sort(Gsizes)) -}) - -test_that("decompose works for many components", { - g <- make_empty_graph(50001) - tmp <- decompose(g) - expect_equal(1, 1) -}) - -test_that("decompose works for many components and attributes", { - g <- make_empty_graph(50001) - V(g)$name <- 1:vcount(g) - tmp <- decompose(g) - expect_equal(1, 1) -}) - -test_that("decompose keeps attributes", { - g <- make_ring(10) + make_ring(5) - V(g)$name <- letters[1:(10 + 5)] - E(g)$name <- apply(as_edgelist(g), 1, paste, collapse = "-") - d <- decompose(g) - d <- d[order(sapply(d, vcount))] - - expect_equal(length(d), 2) - expect_equal(sapply(d, vcount), c(5, 10)) - expect_equal(V(d[[1]])$name, letters[1:5 + 10]) - expect_equal(V(d[[2]])$name, letters[1:10]) - e1 <- apply(as_edgelist(d[[1]]), 1, paste, collapse = "-") - e2 <- apply(as_edgelist(d[[2]]), 1, paste, collapse = "-") - expect_equal(E(d[[1]])$name, e1) - expect_equal(E(d[[2]])$name, e2) -}) - -test_that("decompose protects correctly", { - g <- make_graph(integer(), n = 10001) - V(g)$a <- 1 - - torture <- gctorture2(10001) - on.exit(gctorture2(torture)) - - expect_equal(length(decompose(g)), 10001) -}) diff --git a/tests/testthat/test-structural.properties.R b/tests/testthat/test-structural.properties.R index 2912cff2b1..b6bfae6a70 100644 --- a/tests/testthat/test-structural.properties.R +++ b/tests/testthat/test-structural.properties.R @@ -180,7 +180,7 @@ test_that("bfs() works", { local_igraph_options(print.id = FALSE) expect_snapshot({ - g <- graph_from_literal(a -+ b -+ c, z -+ a, d) + g <- graph_from_literal(a - +b - +c, z - +a, d) bfs( g, root = 2, @@ -197,7 +197,7 @@ test_that("bfs() works", { }) test_that("bfs() deprecated argument", { - g <- graph_from_literal(a -+ b -+ c, z -+ a, d) + g <- graph_from_literal(a - +b - +c, z - +a, d) expect_snapshot(error = TRUE, { b <- bfs( @@ -709,59 +709,6 @@ test_that("a null graph has zero components", { expect_equal(count_components(g), 0L) }) -test_that("component_distribution() finds correct distribution", { - g <- graph_from_literal( - A, - B - C, - D - E - F, - G - H - ) - - ref <- c(0.00, 0.25, 0.50, 0.25) - - expect_equal(component_distribution(g), ref) -}) - -test_that("largest component is actually the largest", { - g <- make_star(20, "undirected") - h <- make_ring(10) - - G <- disjoint_union(g, h) - - expect_true(isomorphic(largest_component(G), g)) -}) - -test_that("largest strongly and weakly components are correct", { - g <- graph_from_literal( - A -+ B, - B -+ C, - C -+ A, - C -+ D, - E - ) - - strongly <- graph_from_literal( - A -+ B, - B -+ C, - C -+ A - ) - weakly <- graph_from_literal( - A -+ B, - B -+ C, - C -+ A, - C -+ D - ) - - expect_true(isomorphic(largest_component(g, "weak"), weakly)) - expect_true(isomorphic(largest_component(g, "strong"), strongly)) -}) - -test_that("the largest component of a null graph is a valid null graph", { - nullgraph <- make_empty_graph(0) - - expect_true(isomorphic(largest_component(make_empty_graph(0)), nullgraph)) -}) - test_that("girth() works", { ## No circle in a tree g <- make_tree(1000, 3)