From 1064771c047a9342e36920717e79e796859fce62 Mon Sep 17 00:00:00 2001 From: schochastics Date: Sat, 8 Feb 2025 19:47:16 +0100 Subject: [PATCH 1/8] merged and refactored flow.R tests --- R/flow.R | 73 ++----- .../{minimal.st.separators.md => flow.md} | 0 tests/testthat/test-all.st.cuts.R | 21 -- tests/testthat/test-bug-1033045.R | 6 - tests/testthat/test-dominator.tree.R | 21 -- tests/testthat/test-edge.connectivity.R | 39 ---- tests/testthat/test-flow.R | 205 ++++++++++++++++++ tests/testthat/test-graph.maxflow.R | 11 - tests/testthat/test-graph.mincut.R | 21 -- tests/testthat/test-minimal.st.separators.R | 16 -- tests/testthat/test-minimum.size.separators.R | 19 -- 11 files changed, 224 insertions(+), 208 deletions(-) rename tests/testthat/_snaps/{minimal.st.separators.md => flow.md} (100%) delete mode 100644 tests/testthat/test-all.st.cuts.R delete mode 100644 tests/testthat/test-bug-1033045.R delete mode 100644 tests/testthat/test-dominator.tree.R delete mode 100644 tests/testthat/test-edge.connectivity.R create mode 100644 tests/testthat/test-flow.R delete mode 100644 tests/testthat/test-graph.maxflow.R delete mode 100644 tests/testthat/test-graph.mincut.R delete mode 100644 tests/testthat/test-minimal.st.separators.R delete mode 100644 tests/testthat/test-minimum.size.separators.R diff --git a/R/flow.R b/R/flow.R index 607e70dc1ff..1fb693b503e 100644 --- a/R/flow.R +++ b/R/flow.R @@ -1,4 +1,3 @@ - #' Vertex connectivity #' #' @description @@ -287,27 +286,21 @@ dominator.tree <- function(graph, root, mode = c("out", "in", "all", "total")) { #' @export min_cut <- function(graph, source = NULL, target = NULL, capacity = NULL, value.only = TRUE) { ensure_igraph(graph) - if (is.null(capacity)) { - if ("capacity" %in% edge_attr_names(graph)) { - capacity <- E(graph)$capacity - } - } - if (length(source) == 0) { - source <- NULL + if (is.null(capacity) && "capacity" %in% edge_attr_names(graph)) { + capacity <- E(graph)$capacity } - if (length(target) == 0) { - target <- NULL - } - if (is.null(source) && !is.null(target) || - is.null(target) && !is.null(source)) { - stop("Please give both source and target or neither") + + if (xor(is.null(source), is.null(target))) { + cli::cli_abort("Please give both source and target or neither") } + if (!is.null(capacity)) { capacity <- as.numeric(capacity) } value.only <- as.logical(value.only) on.exit(.Call(R_igraph_finalizer)) + if (is.null(target) && is.null(source)) { if (value.only) { res <- .Call(R_igraph_mincut_value, graph, capacity) @@ -430,13 +423,6 @@ min_cut <- function(graph, source = NULL, target = NULL, capacity = NULL, value. vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE) { ensure_igraph(graph) - if (length(source) == 0) { - source <- NULL - } - if (length(target) == 0) { - target <- NULL - } - if (is.null(source) && is.null(target)) { on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_vertex_connectivity, graph, as.logical(checks)) @@ -534,13 +520,6 @@ vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TR edge_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE) { ensure_igraph(graph) - if (length(source) == 0) { - source <- NULL - } - if (length(target) == 0) { - target <- NULL - } - if (is.null(source) && is.null(target)) { on.exit(.Call(R_igraph_finalizer)) .Call(R_igraph_edge_connectivity, graph, as.logical(checks)) @@ -551,22 +530,15 @@ edge_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE as_igraph_vs(graph, source) - 1, as_igraph_vs(graph, target) - 1 ) } else { - stop("either give both source and target or neither") + cli::cli_abort("Either give both source and target or neither") } } #' @rdname edge_connectivity #' @export -edge_disjoint_paths <- function(graph, source, target) { +edge_disjoint_paths <- function(graph, source = NULL, target = NULL) { ensure_igraph(graph) - if (length(source) == 0) { - source <- NULL - } - if (length(target) == 0) { - target <- NULL - } - on.exit(.Call(R_igraph_finalizer)) .Call( R_igraph_edge_disjoint_paths, graph, @@ -579,13 +551,6 @@ edge_disjoint_paths <- function(graph, source, target) { vertex_disjoint_paths <- function(graph, source = NULL, target = NULL) { ensure_igraph(graph) - if (length(source) == 0) { - source <- NULL - } - if (length(target) == 0) { - target <- NULL - } - on.exit(.Call(R_igraph_finalizer)) .Call( R_igraph_vertex_disjoint_paths, graph, as_igraph_vs(graph, source) - 1, @@ -638,13 +603,13 @@ cohesion.igraph <- function(x, checks = TRUE, ...) { #' @examples #' #' # A very simple graph -#' g <- graph_from_literal(a -+ b -+ c -+ d -+ e) +#' g <- graph_from_literal(a - +b - +c - +d - +e) #' st_cuts(g, source = "a", target = "e") #' #' # A somewhat more difficult graph #' g2 <- graph_from_literal( -#' s --+ a:b, a:b --+ t, -#' a --+ 1:2:3, 1:2:3 --+ b +#' s - -+a:b, a:b - -+t, +#' a - -+1:2:3, 1:2:3 - -+b #' ) #' st_cuts(g2, source = "s", target = "t") #' @family flow @@ -693,8 +658,8 @@ st_cuts <- all_st_cuts_impl #' #' # A difficult graph, from the Provan-Shier paper #' g <- graph_from_literal( -#' s --+ a:b, a:b --+ t, -#' a --+ 1:2:3:4:5, 1:2:3:4:5 --+ b +#' s - -+a:b, a:b - -+t, +#' a - -+1:2:3:4:5, 1:2:3:4:5 - -+b #' ) #' st_min_cuts(g, source = "s", target = "t") #' @family flow @@ -746,9 +711,9 @@ st_min_cuts <- all_st_mincuts_impl #' #' ## The example from the paper #' g <- graph_from_literal( -#' R -+ A:B:C, A -+ D, B -+ A:D:E, C -+ F:G, D -+ L, -#' E -+ H, F -+ I, G -+ I:J, H -+ E:K, I -+ K, J -+ I, -#' K -+ I:R, L -+ H +#' R - +A:B:C, A - +D, B - +A:D:E, C - +F:G, D - +L, +#' E - +H, F - +I, G - +I:J, H - +E:K, I - +K, J - +I, +#' K - +I:R, L - +H #' ) #' dtree <- dominator_tree(g, root = "R") #' layout <- layout_as_tree(dtree$domtree, root = "R") @@ -758,10 +723,10 @@ st_min_cuts <- all_st_mincuts_impl #' @export dominator_tree <- function(graph, root, mode = c("out", "in", "all", "total")) { # Argument checks - ensure_igraph(graph) + ensure_igraph(graph) root <- as_igraph_vs(graph, root) if (length(root) == 0) { - stop("No vertex was specified") + cli::cli_abort("No vertex was specified") } mode <- switch(igraph.match.arg(mode), "out" = 1, diff --git a/tests/testthat/_snaps/minimal.st.separators.md b/tests/testthat/_snaps/flow.md similarity index 100% rename from tests/testthat/_snaps/minimal.st.separators.md rename to tests/testthat/_snaps/flow.md diff --git a/tests/testthat/test-all.st.cuts.R b/tests/testthat/test-all.st.cuts.R deleted file mode 100644 index 60563baec61..00000000000 --- a/tests/testthat/test-all.st.cuts.R +++ /dev/null @@ -1,21 +0,0 @@ -test_that("all.st.cuts works", { - - g <- graph_from_literal(a - +b - +c - +d - +e) - cc <- st_cuts(g, source = "a", target = "e") - expect_equal(unvs(cc$cuts), list(1, 2, 3, 4)) - expect_equal(unvs(cc$partition1s), list(1, 1:2, 1:3, 1:4)) - - g2 <- graph_from_literal(s - +a:b - +t, a - +1:2:3 - +b) - cc <- st_cuts(g2, source = "s", target = "t") - expect_equal(unvs(cc$cuts), list(c(1, 2), c(1, 7), c(2, 3, 4, 5, 6), c(2, 3, 4, 5, 10), c(2, 3, 4, 6, 9), c(2, 3, 4, 9, 10), c(2, 3, 5, 6, 8), c(2, 3, 5, 8, 10), c(2, 3, 6, 8, 9), c(2, 3, 8, 9, 10), c(3, 7))) - expect_equal( - unvs(cc$partition1s), - list(1, c(1, 3), c(1, 2), c(1, 2, 7), c(1, 2, 6), c(1, 2, 6, 7), c(1, 2, 5), c(1, 2, 5, 7), c(1, 2, 5, 6), c(1, 2, 5, 6, 7), c(1, 2, 5, 6, 7, 3)) - ) - - g3 <- graph_from_literal(s - +a:b - +t, a - +1:2:3:4:5 - +b) - cc <- st_min_cuts(g3, source = "s", target = "t") - expect_equal(cc$value, 2) - expect_equal(unvs(cc$cuts), list(c(1, 2), c(1, 9), c(3, 9))) - expect_equal(unvs(cc$partition1s), list(1, c(1, 3), c(1, 3, 2, 9, 8, 7, 6, 5))) -}) diff --git a/tests/testthat/test-bug-1033045.R b/tests/testthat/test-bug-1033045.R deleted file mode 100644 index 3b220297866..00000000000 --- a/tests/testthat/test-bug-1033045.R +++ /dev/null @@ -1,6 +0,0 @@ -test_that("Minimal s-t separators work", { - g <- graph_from_literal(a - -1:3 - -5 - -2:4 - -b, 1 - -2, 3 - -4) - stsep <- min_st_separators(g) - ims <- sapply(stsep, is_min_separator, graph = g) - expect_equal(ims, rep(TRUE, 9)) -}) diff --git a/tests/testthat/test-dominator.tree.R b/tests/testthat/test-dominator.tree.R deleted file mode 100644 index dba7eeadf18..00000000000 --- a/tests/testthat/test-dominator.tree.R +++ /dev/null @@ -1,21 +0,0 @@ -test_that("dominator_tree works", { - g <- graph_from_literal( - R - +A:B:C, A - +D, B - +A:D:E, C - +F:G, D - +L, - E - +H, F - +I, G - +I:J, H - +E:K, I - +K, J - +I, - K - +I:R, L - +H - ) - dtree <- dominator_tree(g, root = "R") - - # This is awkward; dtree$dom contains -1 for the root and normal vertex indices - # for the rest, and we want to map them to names. This seemed to be the cleanest - # way, but it is not nearly as user-friendly as it should be - names <- c("$root", V(g)$name) - dtree$dom <- names[ifelse(dtree$dom < 0, 1, dtree$dom + 1)] - dtree$leftout <- V(g)$name[dtree$leftout] - expect_equal(dtree$dom, c("$root", "R", "R", "R", "R", "R", "C", "C", "D", "R", "R", "G", "R")) - expect_equal(dtree$leftout, character()) - expect_equal( - as_edgelist(dtree$domtree), - structure(c("R", "R", "R", "R", "R", "C", "C", "D", "R", "R", "G", "R", "A", "B", "C", "D", "E", "F", "G", "L", "H", "I", "J", "K"), .Dim = c(12L, 2L)) - ) -}) diff --git a/tests/testthat/test-edge.connectivity.R b/tests/testthat/test-edge.connectivity.R deleted file mode 100644 index 274245cc986..00000000000 --- a/tests/testthat/test-edge.connectivity.R +++ /dev/null @@ -1,39 +0,0 @@ -test_that("edge_connectivity works", { - - g <- largest_component(sample_gnp(30, 8 / 30)) - ec <- edge_connectivity(g) - ecST <- Inf - for (j in 1:(vcount(g) - 1)) { - for (k in (j + 1):vcount(g)) { - ec2 <- edge_connectivity(g, source = j, target = k) - if (ec2 < ecST) { - ecST <- ec2 - } - } - } - expect_equal(ec, ecST) - -}) - -test_that("edge_connectivity works -- names", { - - kite <- graph_from_literal( - Andre - Beverly:Carol:Diane:Fernando, - Beverly - Andre:Diane:Ed:Garth, - Carol - Andre:Diane:Fernando, - Diane - Andre:Beverly:Carol:Ed:Fernando:Garth, - Ed - Beverly:Diane:Garth, - Fernando - Andre:Carol:Diane:Garth:Heather, - Garth - Beverly:Diane:Ed:Fernando:Heather, - Heather - Fernando:Garth:Ike, - Ike - Heather:Jane, - Jane - Ike - ) - - ec1 <- edge_connectivity(kite, source = "Heather", target = "Andre") - ec2 <- edge_connectivity(kite, source = "Garth", target = "Andre") - ec3 <- edge_connectivity(kite, source = "Garth", target = "Ike") - expect_equal(ec1, 2) - expect_equal(ec2, 4) - expect_equal(ec3, 1) -}) diff --git a/tests/testthat/test-flow.R b/tests/testthat/test-flow.R new file mode 100644 index 00000000000..cbc14307440 --- /dev/null +++ b/tests/testthat/test-flow.R @@ -0,0 +1,205 @@ +test_that("min_cut works -- value.only=FALSE", { + g_twosquares <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) + E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) + mc <- min_cut(g_twosquares, value.only = FALSE) + + expect_equal(mc$value, 1) + expect_equal(as.vector(mc$cut), 2) + expect_equal(as.vector(mc$partition1), 2) + expect_equal(as.vector(mc$partition2), c(1, 3:6)) +}) + +test_that("min_cut works -- value.only=TRUE", { + g_twosquares <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) + E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) + mc <- min_cut(g_twosquares, value.only = TRUE) + + expect_equal(mc, 1) +}) + +test_that("min_cut works -- value.only=FALSE source/target", { + g_twosquares <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) + E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) + mc <- min_cut(g_twosquares, source = 1, target = 4, value.only = FALSE) + + expect_equal(mc$value, 1) + expect_equal(as.vector(mc$cut), c(2, 5)) + expect_equal(as.vector(mc$partition1), c(1, 2, 6)) + expect_equal(as.vector(mc$partition2), c(3, 4, 5)) +}) + +test_that("min_cut works -- value.only=TRUE", { + g_twosquares <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) + E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) + mc <- min_cut(g_twosquares, source = 1, target = 4, value.only = TRUE) + + expect_equal(mc, 2) +}) + + +test_that("min_cut errors work", { + g_ring <- make_ring(5, directed = TRUE, circular = TRUE) + E(g_ring)$capacity <- c(1, 2, 3, 4, 5) + expect_error(min_cut(g_ring, source = 1)) + expect_error(min_cut(g_ring, target = 1)) +}) + +test_that("st_cuts works", { + g_path <- graph_from_literal(a - +b - +c - +d - +e) + all_cuts_path <- st_cuts(g_path, source = "a", target = "e") + expect_equal(unvs(all_cuts_path$cuts), list(1, 2, 3, 4)) + expect_equal(unvs(all_cuts_path$partition1s), list(1, 1:2, 1:3, 1:4)) + + g_star_v7 <- graph_from_literal(s - +a:b - +t, a - +1:2:3 - +b) + all_cuts_star_v7 <- st_cuts(g_star_v7, source = "s", target = "t") + expect_equal(unvs(all_cuts_star_v7$cuts), list(c(1, 2), c(1, 7), c(2, 3, 4, 5, 6), c(2, 3, 4, 5, 10), c(2, 3, 4, 6, 9), c(2, 3, 4, 9, 10), c(2, 3, 5, 6, 8), c(2, 3, 5, 8, 10), c(2, 3, 6, 8, 9), c(2, 3, 8, 9, 10), c(3, 7))) + expect_equal( + unvs(all_cuts_star_v7$partition1s), + list(1, c(1, 3), c(1, 2), c(1, 2, 7), c(1, 2, 6), c(1, 2, 6, 7), c(1, 2, 5), c(1, 2, 5, 7), c(1, 2, 5, 6), c(1, 2, 5, 6, 7), c(1, 2, 5, 6, 7, 3)) + ) + + g_star_v9 <- graph_from_literal(s - +a:b - +t, a - +1:2:3:4:5 - +b) + all_cuts_star_v9 <- st_min_cuts(g_star_v9, source = "s", target = "t") + expect_equal(all_cuts_star_v9$value, 2) + expect_equal(unvs(all_cuts_star_v9$cuts), list(c(1, 2), c(1, 9), c(3, 9))) + expect_equal(unvs(all_cuts_star_v9$partition1s), list(1, c(1, 3), c(1, 3, 2, 9, 8, 7, 6, 5))) +}) + +test_that("max_flow works", { + edge_mat <- rbind(c(1, 3, 3), c(3, 4, 1), c(4, 2, 2), c(1, 5, 1), c(5, 6, 2), c(6, 2, 10)) + colnames(edge_mat) <- c("from", "to", "capacity") + g_ring_acyc <- graph_from_data_frame(as.data.frame(edge_mat)) + flow <- max_flow(g_ring_acyc, source = "1", target = "2") + expect_equal(flow$value, 2) + expect_equal(as.vector(flow$flow), rep(1, 6)) + expect_equal(sort(as.vector(flow$cut)), c(2, 4)) + expect_equal(sort(as.vector(flow$partition1)), 1:2) + expect_equal(sort(as.vector(flow$partition2)), 3:6) +}) + +test_that("vertex_connectivity works", { + g_path <- make_ring(5, circular = FALSE) + expect_equal(vertex_connectivity(g_path), 1) + + g_disconnect <- make_graph(edges = c(1, 2, 3, 4), directed = FALSE) + expect_equal(vertex_connectivity(g_disconnect), 0) + + g_ring <- make_ring(5, circular = TRUE) + expect_equal(vertex_connectivity(g_ring, source = 1, target = 4), 2) +}) + +test_that("edge_connectivity works", { + g_full <- make_full_graph(5) + expect_equal(edge_connectivity(g_full), 4) + expect_equal(edge_connectivity(g_full, source = 1, target = 2), 4) + + + g_path <- make_ring(5, directed = TRUE, circular = FALSE) + expect_equal(edge_connectivity(g_path), 0) + expect_equal(edge_connectivity(g_path, source = 1, target = 3), 1) +}) + +test_that("edge_connectivity works -- names", { + g_kite <- graph_from_literal( + Andre - Beverly:Carol:Diane:Fernando, + Beverly - Andre:Diane:Ed:Garth, + Carol - Andre:Diane:Fernando, + Diane - Andre:Beverly:Carol:Ed:Fernando:Garth, + Ed - Beverly:Diane:Garth, + Fernando - Andre:Carol:Diane:Garth:Heather, + Garth - Beverly:Diane:Ed:Fernando:Heather, + Heather - Fernando:Garth:Ike, + Ike - Heather:Jane, + Jane - Ike + ) + + ec1 <- edge_connectivity(g_kite, source = "Heather", target = "Andre") + expect_equal(ec1, 2) + ec2 <- edge_connectivity(g_kite, source = "Garth", target = "Andre") + expect_equal(ec2, 4) + ec3 <- edge_connectivity(g_kite, source = "Garth", target = "Ike") + expect_equal(ec3, 1) +}) + +test_that("dominator_tree works", { + g_tree <- graph_from_edgelist(matrix(c(1, 2, 2, 3, 3, 4, 2, 5, 5, 6), byrow = TRUE, ncol = 2), directed = TRUE) + dom_tree_tree <- dominator_tree(g_tree, 1) + + expect_equal(dom_tree_tree$dom[2], 1) + expect_equal(dom_tree_tree$dom[3], 2) + expect_equal(dom_tree_tree$dom[5], 2) + expect_equal(dom_tree_tree$dom[6], 5) + + g_one_vertex <- make_empty_graph(n = 1, directed = TRUE) + dom_tree_one <- dominator_tree(g_one_vertex, 1) + + expect_equal(dom_tree_one$dom[1], -1) +}) + +test_that("dominator_tree errors work", { + g_tree <- graph_from_edgelist(matrix(c(1, 2, 2, 3, 3, 4, 2, 5, 5, 6), byrow = TRUE, ncol = 2), directed = TRUE) + expect_error(dominator_tree(g_tree)) +}) + +test_that("dominator_tree works -- legacy", { + g <- graph_from_literal( + R - +A:B:C, A - +D, B - +A:D:E, C - +F:G, D - +L, + E - +H, F - +I, G - +I:J, H - +E:K, I - +K, J - +I, + K - +I:R, L - +H + ) + dtree <- dominator_tree(g, root = "R") + names <- c("$root", V(g)$name) + dtree$dom <- names[ifelse(dtree$dom < 0, 1, dtree$dom + 1)] + dtree$leftout <- V(g)$name[dtree$leftout] + expect_equal(dtree$dom, c("$root", "R", "R", "R", "R", "R", "C", "C", "D", "R", "R", "G", "R")) + expect_equal(dtree$leftout, character()) + expect_equal( + as_edgelist(dtree$domtree), + structure(c("R", "R", "R", "R", "R", "C", "C", "D", "R", "R", "G", "R", "A", "B", "C", "D", "E", "F", "G", "L", "H", "I", "J", "K"), .Dim = c(12L, 2L)) + ) +}) + +test_that("min_st_separators works", { + g_zachary <- make_graph("Zachary") + msts <- min_st_separators(g_zachary) + is <- sapply(msts, is_separator, graph = g_zachary) + expect_equal(unique(is), TRUE) + + ## TODO: check that it is minimal +}) + +test_that("min_st_separators() works for the note case", { + g_note <- make_graph(~ 0 - 1 - 2 - 3 - 4 - 1) + expect_snapshot( + min_st_separators(g_note), + transform = function(x) gsub("from.*", "from something", x) + ) +}) + +test_that("Minimal s-t separators work", { + # bug 1033045 + g <- graph_from_literal(a - -1:3 - -5 - -2:4 - -b, 1 - -2, 3 - -4) + stsep <- min_st_separators(g) + ims <- sapply(stsep, is_min_separator, graph = g) + expect_equal(ims, rep(TRUE, 9)) +}) + +test_that("min_separators works", { + camp <- graph_from_literal( + Harry:Steve:Don:Bert - Harry:Steve:Don:Bert, + Pam:Brazey:Carol:Pat - Pam:Brazey:Carol:Pat, + Holly - Carol:Pat:Pam:Jennie:Bill, + Bill - Pauline:Michael:Lee:Holly, + Pauline - Bill:Jennie:Ann, + Jennie - Holly:Michael:Lee:Ann:Pauline, + Michael - Bill:Jennie:Ann:Lee:John, + Ann - Michael:Jennie:Pauline, + Lee - Michael:Bill:Jennie, + Gery - Pat:Steve:Russ:John, + Russ - Steve:Bert:Gery:John, + John - Gery:Russ:Michael + ) + camp <- simplify(camp) + sep <- min_separators(camp) + expect_true(all(sapply(sep, is_min_separator, graph = camp))) +}) diff --git a/tests/testthat/test-graph.maxflow.R b/tests/testthat/test-graph.maxflow.R deleted file mode 100644 index c8849665a7a..00000000000 --- a/tests/testthat/test-graph.maxflow.R +++ /dev/null @@ -1,11 +0,0 @@ -test_that("max_flow works", { - E <- rbind(c(1, 3, 3), c(3, 4, 1), c(4, 2, 2), c(1, 5, 1), c(5, 6, 2), c(6, 2, 10)) - colnames(E) <- c("from", "to", "capacity") - g1 <- graph_from_data_frame(as.data.frame(E)) - fl <- max_flow(g1, source = "1", target = "2") - expect_equal(fl$value, 2) - expect_equal(as.vector(fl$flow), rep(1, 6)) - expect_equal(sort(as.vector(fl$cut)), c(2, 4)) - expect_equal(sort(as.vector(fl$partition1)), 1:2) - expect_equal(sort(as.vector(fl$partition2)), 3:6) -}) diff --git a/tests/testthat/test-graph.mincut.R b/tests/testthat/test-graph.mincut.R deleted file mode 100644 index 18066226b03..00000000000 --- a/tests/testthat/test-graph.mincut.R +++ /dev/null @@ -1,21 +0,0 @@ -test_that("min_cut works", { - g2 <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) - E(g2)$capacity <- c(3, 1, 2, 10, 1, 3, 2) - mc <- min_cut(g2, value.only = FALSE) - - expect_equal(mc$value, 1) - expect_equal(as.vector(mc$cut), 2) - expect_equal(as.vector(mc$partition1), 2) - expect_equal(as.vector(mc$partition2), c(1, 3:6)) -}) - -test_that("s-t min_cut works", { - g2 <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) - E(g2)$capacity <- c(3, 1, 2, 10, 1, 3, 2) - mc <- min_cut(g2, source = 2, target = 4, value.only = FALSE) - - expect_equal(mc$value, 1) - expect_equal(as.vector(mc$cut), 2) - expect_equal(as.vector(mc$partition1), 2) - expect_equal(as.vector(mc$partition2), c(1, 3:6)) -}) diff --git a/tests/testthat/test-minimal.st.separators.R b/tests/testthat/test-minimal.st.separators.R deleted file mode 100644 index 7da2118492c..00000000000 --- a/tests/testthat/test-minimal.st.separators.R +++ /dev/null @@ -1,16 +0,0 @@ -test_that("min_st_separators works", { - g <- make_graph("Zachary") - msts <- min_st_separators(g) - is <- sapply(msts, is_separator, graph = g) - expect_equal(unique(is), TRUE) - - ## TODO: check that it is minimal -}) - -test_that("min_st_separators() works for the note case", { - g <- make_graph(~ 0 - 1 - 2 - 3 - 4 - 1) - expect_snapshot( - min_st_separators(g), - transform = function(x) gsub("from.*", "from something", x) - ) -}) diff --git a/tests/testthat/test-minimum.size.separators.R b/tests/testthat/test-minimum.size.separators.R deleted file mode 100644 index 92563101579..00000000000 --- a/tests/testthat/test-minimum.size.separators.R +++ /dev/null @@ -1,19 +0,0 @@ -test_that("min_separators works", { - camp <- graph_from_literal( - Harry:Steve:Don:Bert - Harry:Steve:Don:Bert, - Pam:Brazey:Carol:Pat - Pam:Brazey:Carol:Pat, - Holly - Carol:Pat:Pam:Jennie:Bill, - Bill - Pauline:Michael:Lee:Holly, - Pauline - Bill:Jennie:Ann, - Jennie - Holly:Michael:Lee:Ann:Pauline, - Michael - Bill:Jennie:Ann:Lee:John, - Ann - Michael:Jennie:Pauline, - Lee - Michael:Bill:Jennie, - Gery - Pat:Steve:Russ:John, - Russ - Steve:Bert:Gery:John, - John - Gery:Russ:Michael - ) - camp <- simplify(camp) - sep <- min_separators(camp) - expect_true(all(sapply(sep, is_min_separator, graph = camp))) -}) From 76dcbdd1a73f3c1b36dfd897c5acc9a187fb49c9 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 10 Feb 2025 05:25:55 +0100 Subject: [PATCH 2/8] fixed failing tests --- tests/testthat/_snaps/flow.md | 2 +- tests/testthat/test-flow.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/_snaps/flow.md b/tests/testthat/_snaps/flow.md index 7add2e27c28..539aebf30bc 100644 --- a/tests/testthat/_snaps/flow.md +++ b/tests/testthat/_snaps/flow.md @@ -1,7 +1,7 @@ # min_st_separators() works for the note case Code - min_st_separators(g) + min_st_separators(g_note) Output [[1]] + 1/5 vertex, named, from something diff --git a/tests/testthat/test-flow.R b/tests/testthat/test-flow.R index cbc14307440..eb0b31fbca4 100644 --- a/tests/testthat/test-flow.R +++ b/tests/testthat/test-flow.R @@ -22,7 +22,7 @@ test_that("min_cut works -- value.only=FALSE source/target", { E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) mc <- min_cut(g_twosquares, source = 1, target = 4, value.only = FALSE) - expect_equal(mc$value, 1) + expect_equal(mc$value, 2) expect_equal(as.vector(mc$cut), c(2, 5)) expect_equal(as.vector(mc$partition1), c(1, 2, 6)) expect_equal(as.vector(mc$partition2), c(3, 4, 5)) From 3bf022a98cf3212d0fe08717e1971939e7aa7e11 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 10 Feb 2025 06:03:31 +0100 Subject: [PATCH 3/8] added more error tests --- R/flow.R | 2 +- tests/testthat/test-flow.R | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/R/flow.R b/R/flow.R index 1fb693b503e..98ef822d88f 100644 --- a/R/flow.R +++ b/R/flow.R @@ -433,7 +433,7 @@ vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TR as_igraph_vs(graph, target) - 1 ) } else { - stop("either give both source and target or neither") + cli::cli_abort("Either give both source and target or neither") } } diff --git a/tests/testthat/test-flow.R b/tests/testthat/test-flow.R index eb0b31fbca4..75523512a7d 100644 --- a/tests/testthat/test-flow.R +++ b/tests/testthat/test-flow.R @@ -65,6 +65,14 @@ test_that("st_cuts works", { expect_equal(unvs(all_cuts_star_v9$partition1s), list(1, c(1, 3), c(1, 3, 2, 9, 8, 7, 6, 5))) }) +test_that("st_cuts errors work", { + g_path <- graph_from_literal(a - +b - +c - +d - +e) + expect_error(st_cuts(g_path, source = "a", target = NULL)) + expect_error(st_cuts(g_path, source = NULL, target = "a")) + expect_error(st_min_cuts(g_path, source = "a", target = NULL)) + expect_error(st_min_cuts(g_path, source = NULL, target = "a")) +}) + test_that("max_flow works", { edge_mat <- rbind(c(1, 3, 3), c(3, 4, 1), c(4, 2, 2), c(1, 5, 1), c(5, 6, 2), c(6, 2, 10)) colnames(edge_mat) <- c("from", "to", "capacity") @@ -88,6 +96,11 @@ test_that("vertex_connectivity works", { expect_equal(vertex_connectivity(g_ring, source = 1, target = 4), 2) }) +test_that("vertex_connectivity error works", { + g_path <- make_ring(5, circular = FALSE) + expect_error(vertex_connectivity(g_path, source = 1)) +}) + test_that("edge_connectivity works", { g_full <- make_full_graph(5) expect_equal(edge_connectivity(g_full), 4) @@ -121,6 +134,51 @@ test_that("edge_connectivity works -- names", { expect_equal(ec3, 1) }) +test_that("edge_connectivity error works", { + g_path <- make_ring(5, circular = FALSE) + expect_error(edge_connectivity(g_path, source = 1)) +}) + +test_that("edge_disjoint_paths works", { + g_full <- make_full_graph(5) + expect_equal(edge_disjoint_paths(g_full, source = 1, target = 2), 4) + + g_path <- make_ring(5, directed = TRUE, circular = FALSE) + expect_equal(edge_disjoint_paths(g_path, source = 1, target = 3), 1) +}) + +test_that("edge_disjoint_paths error works", { + g_path <- make_ring(5, circular = FALSE) + expect_error(edge_disjoint_paths(g_path, source = 1)) +}) + +test_that("vertex_disjoint_paths works", { + g_full <- make_full_graph(5) + expect_equal(vertex_disjoint_paths(g_full, source = 1, target = 2), 4) + + g_path <- make_ring(5, directed = TRUE, circular = FALSE) + expect_equal(vertex_disjoint_paths(g_path, source = 1, target = 3), 1) +}) + +test_that("vertex_disjoint_paths error works", { + g_path <- make_ring(5, circular = FALSE) + expect_error(vertex_disjoint_paths(g_path, source = 1)) +}) + +test_that("adhesion works", { + g_full <- make_full_graph(5) + expect_equal(adhesion(g_full), 4) + + g_path <- make_ring(5, directed = TRUE, circular = FALSE) + expect_equal(adhesion(g_path), 0) +}) + +test_that("vertex_disjoint_paths error works", { + g_path <- make_ring(5, circular = FALSE) + expect_error(vertex_disjoint_paths(g_path, source = 1)) +}) + + test_that("dominator_tree works", { g_tree <- graph_from_edgelist(matrix(c(1, 2, 2, 3, 3, 4, 2, 5, 5, 6), byrow = TRUE, ncol = 2), directed = TRUE) dom_tree_tree <- dominator_tree(g_tree, 1) @@ -139,6 +197,7 @@ test_that("dominator_tree works", { test_that("dominator_tree errors work", { g_tree <- graph_from_edgelist(matrix(c(1, 2, 2, 3, 3, 4, 2, 5, 5, 6), byrow = TRUE, ncol = 2), directed = TRUE) expect_error(dominator_tree(g_tree)) + expect_error(dominator_tree(g_tree, root = NULL)) }) test_that("dominator_tree works -- legacy", { From ba7393859e977cce7816506a3d690ab05cf0ff30 Mon Sep 17 00:00:00 2001 From: schochastics Date: Mon, 10 Feb 2025 10:34:51 +0100 Subject: [PATCH 4/8] added better R error handling --- R/flow.R | 7 ++++++- tests/testthat/test-flow.R | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/R/flow.R b/R/flow.R index 98ef822d88f..09c344f2c6c 100644 --- a/R/flow.R +++ b/R/flow.R @@ -538,7 +538,9 @@ edge_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE #' @export edge_disjoint_paths <- function(graph, source = NULL, target = NULL) { ensure_igraph(graph) - + if (is.null(source) || is.null(target)) { + cli::cli_abort("Both source and target must be given") + } on.exit(.Call(R_igraph_finalizer)) .Call( R_igraph_edge_disjoint_paths, graph, @@ -550,6 +552,9 @@ edge_disjoint_paths <- function(graph, source = NULL, target = NULL) { #' @export vertex_disjoint_paths <- function(graph, source = NULL, target = NULL) { ensure_igraph(graph) + if (is.null(source) || is.null(target)) { + cli::cli_abort("Both source and target must be given") + } on.exit(.Call(R_igraph_finalizer)) .Call( diff --git a/tests/testthat/test-flow.R b/tests/testthat/test-flow.R index 75523512a7d..cfcb8cdcdba 100644 --- a/tests/testthat/test-flow.R +++ b/tests/testthat/test-flow.R @@ -149,7 +149,8 @@ test_that("edge_disjoint_paths works", { test_that("edge_disjoint_paths error works", { g_path <- make_ring(5, circular = FALSE) - expect_error(edge_disjoint_paths(g_path, source = 1)) + expect_error(edge_disjoint_paths(g_path, source = 1, target = NULL)) + expect_error(edge_disjoint_paths(g_path, source = NULL, target = 1)) }) test_that("vertex_disjoint_paths works", { From b11a3c96c90a68793aeb8543bb2af4df5ff95838 Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 13 Feb 2025 18:49:54 +0100 Subject: [PATCH 5/8] fixed cli calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maëlle Salmon --- R/flow.R | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/R/flow.R b/R/flow.R index 09c344f2c6c..8a852f0f583 100644 --- a/R/flow.R +++ b/R/flow.R @@ -291,7 +291,10 @@ min_cut <- function(graph, source = NULL, target = NULL, capacity = NULL, value. } if (xor(is.null(source), is.null(target))) { - cli::cli_abort("Please give both source and target or neither") + cli::cli_abort(c( + "{.arg source} and {.arg target} must not be specified at the same time.", + i = "Specify either {.arg source} or {.arg target} or neither." + )) } if (!is.null(capacity)) { @@ -433,7 +436,10 @@ vertex_connectivity <- function(graph, source = NULL, target = NULL, checks = TR as_igraph_vs(graph, target) - 1 ) } else { - cli::cli_abort("Either give both source and target or neither") + cli::cli_abort(c( + "{.arg source} and {.arg target} must not be specified at the same time.", + i = "Specify either {.arg source} or {.arg target} or neither." + )) } } @@ -530,7 +536,10 @@ edge_connectivity <- function(graph, source = NULL, target = NULL, checks = TRUE as_igraph_vs(graph, source) - 1, as_igraph_vs(graph, target) - 1 ) } else { - cli::cli_abort("Either give both source and target or neither") + cli::cli_abort(c( + "{.arg source} and {.arg target} must not be specified at the same time.", + i = "Specify either {.arg source} or {.arg target} or neither." + )) } } @@ -731,7 +740,7 @@ dominator_tree <- function(graph, root, mode = c("out", "in", "all", "total")) { ensure_igraph(graph) root <- as_igraph_vs(graph, root) if (length(root) == 0) { - cli::cli_abort("No vertex was specified") + cli::cli_abort("{.arg root} must be specified.") } mode <- switch(igraph.match.arg(mode), "out" = 1, From f3ab2c5adf95957cfaa0eee4255b8eb8390bc00d Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 13 Feb 2025 19:11:31 +0100 Subject: [PATCH 6/8] fixed test names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maëlle Salmon --- tests/testthat/test-flow.R | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-flow.R b/tests/testthat/test-flow.R index cfcb8cdcdba..9e1fcd4bbe4 100644 --- a/tests/testthat/test-flow.R +++ b/tests/testthat/test-flow.R @@ -1,4 +1,4 @@ -test_that("min_cut works -- value.only=FALSE", { +test_that("min_cut() works -- value.only=FALSE", { g_twosquares <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) mc <- min_cut(g_twosquares, value.only = FALSE) @@ -9,7 +9,7 @@ test_that("min_cut works -- value.only=FALSE", { expect_equal(as.vector(mc$partition2), c(1, 3:6)) }) -test_that("min_cut works -- value.only=TRUE", { +test_that("min_cut() works -- value.only=TRUE", { g_twosquares <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) mc <- min_cut(g_twosquares, value.only = TRUE) @@ -17,7 +17,7 @@ test_that("min_cut works -- value.only=TRUE", { expect_equal(mc, 1) }) -test_that("min_cut works -- value.only=FALSE source/target", { +test_that("min_cut() works -- value.only=FALSE source/target", { g_twosquares <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) mc <- min_cut(g_twosquares, source = 1, target = 4, value.only = FALSE) @@ -28,7 +28,7 @@ test_that("min_cut works -- value.only=FALSE source/target", { expect_equal(as.vector(mc$partition2), c(3, 4, 5)) }) -test_that("min_cut works -- value.only=TRUE", { +test_that("min_cut() works -- value.only=TRUE", { g_twosquares <- make_graph(c(1, 2, 2, 3, 3, 4, 1, 6, 6, 5, 5, 4, 4, 1)) E(g_twosquares)$capacity <- c(3, 1, 2, 10, 1, 3, 2) mc <- min_cut(g_twosquares, source = 1, target = 4, value.only = TRUE) @@ -37,14 +37,14 @@ test_that("min_cut works -- value.only=TRUE", { }) -test_that("min_cut errors work", { +test_that("min_cut() errors work", { g_ring <- make_ring(5, directed = TRUE, circular = TRUE) E(g_ring)$capacity <- c(1, 2, 3, 4, 5) expect_error(min_cut(g_ring, source = 1)) expect_error(min_cut(g_ring, target = 1)) }) -test_that("st_cuts works", { +test_that("st_cuts() works", { g_path <- graph_from_literal(a - +b - +c - +d - +e) all_cuts_path <- st_cuts(g_path, source = "a", target = "e") expect_equal(unvs(all_cuts_path$cuts), list(1, 2, 3, 4)) @@ -80,12 +80,12 @@ test_that("max_flow works", { flow <- max_flow(g_ring_acyc, source = "1", target = "2") expect_equal(flow$value, 2) expect_equal(as.vector(flow$flow), rep(1, 6)) - expect_equal(sort(as.vector(flow$cut)), c(2, 4)) - expect_equal(sort(as.vector(flow$partition1)), 1:2) - expect_equal(sort(as.vector(flow$partition2)), 3:6) + expect_setequal(as.vector(flow$cut), c(2, 4)) + expect_setequal(as.vector(flow$partition1), 1:2) + expect_setequal(as.vector(flow$partition2), 3:6) }) -test_that("vertex_connectivity works", { +test_that("vertex_connectivity() works", { g_path <- make_ring(5, circular = FALSE) expect_equal(vertex_connectivity(g_path), 1) From ce6cb74e01853e87a5e4e883de4b5820e2fe56b0 Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 13 Feb 2025 19:40:39 +0100 Subject: [PATCH 7/8] converted error tests to snapshot tests --- tests/testthat/_snaps/flow.md | 98 +++++++++++++++++++++++++++++++++++ tests/testthat/test-flow.R | 40 +++++++++----- 2 files changed, 124 insertions(+), 14 deletions(-) diff --git a/tests/testthat/_snaps/flow.md b/tests/testthat/_snaps/flow.md index 539aebf30bc..bf24e513c67 100644 --- a/tests/testthat/_snaps/flow.md +++ b/tests/testthat/_snaps/flow.md @@ -1,3 +1,101 @@ +# st_cuts errors work + + Code + st_cuts(g_path, source = "a", target = NULL) + Condition + Error in `st_cuts()`: + ! No vertex was specified + +--- + + Code + st_cuts(g_path, source = NULL, target = "a") + Condition + Error in `st_cuts()`: + ! No vertex was specified + +--- + + Code + st_min_cuts(g_path, source = "a", target = NULL) + Condition + Error in `st_min_cuts()`: + ! No vertex was specified + +--- + + Code + st_min_cuts(g_path, source = NULL, target = "a") + Condition + Error in `st_min_cuts()`: + ! No vertex was specified + +# vertex_connectivity error works + + Code + vertex_connectivity(g_path, source = 1) + Condition + Error in `vertex_connectivity()`: + ! `source` and `target` must not be specified at the same time. + i Specify either `source` or `target` or neither. + +# edge_connectivity error works + + Code + edge_connectivity(g_path, source = 1) + Condition + Error in `edge_connectivity()`: + ! `source` and `target` must not be specified at the same time. + i Specify either `source` or `target` or neither. + +# edge_disjoint_paths error works + + Code + edge_disjoint_paths(g_path, source = 1, target = NULL) + Condition + Error in `edge_disjoint_paths()`: + ! Both source and target must be given + +--- + + Code + edge_disjoint_paths(g_path, source = NULL, target = 1) + Condition + Error in `edge_disjoint_paths()`: + ! Both source and target must be given + +# vertex_disjoint_paths error works + + Code + vertex_disjoint_paths(g_path, source = 1) + Condition + Error in `vertex_disjoint_paths()`: + ! Both source and target must be given + +--- + + Code + vertex_disjoint_paths(g_path, source = 1) + Condition + Error in `vertex_disjoint_paths()`: + ! Both source and target must be given + +# dominator_tree errors work + + Code + dominator_tree(g_tree) + Condition + Error in `dominator_tree()`: + ! argument "root" is missing, with no default + +--- + + Code + dominator_tree(g_tree, root = NULL) + Condition + Error in `dominator_tree()`: + ! `root` must be specified. + # min_st_separators() works for the note case Code diff --git a/tests/testthat/test-flow.R b/tests/testthat/test-flow.R index 9e1fcd4bbe4..655b6a33fb0 100644 --- a/tests/testthat/test-flow.R +++ b/tests/testthat/test-flow.R @@ -52,10 +52,21 @@ test_that("st_cuts() works", { g_star_v7 <- graph_from_literal(s - +a:b - +t, a - +1:2:3 - +b) all_cuts_star_v7 <- st_cuts(g_star_v7, source = "s", target = "t") - expect_equal(unvs(all_cuts_star_v7$cuts), list(c(1, 2), c(1, 7), c(2, 3, 4, 5, 6), c(2, 3, 4, 5, 10), c(2, 3, 4, 6, 9), c(2, 3, 4, 9, 10), c(2, 3, 5, 6, 8), c(2, 3, 5, 8, 10), c(2, 3, 6, 8, 9), c(2, 3, 8, 9, 10), c(3, 7))) + expect_equal( + unvs(all_cuts_star_v7$cuts), + list( + c(1, 2), c(1, 7), c(2, 3, 4, 5, 6), c(2, 3, 4, 5, 10), + c(2, 3, 4, 6, 9), c(2, 3, 4, 9, 10), c(2, 3, 5, 6, 8), + c(2, 3, 5, 8, 10), c(2, 3, 6, 8, 9), c(2, 3, 8, 9, 10), c(3, 7) + ) + ) expect_equal( unvs(all_cuts_star_v7$partition1s), - list(1, c(1, 3), c(1, 2), c(1, 2, 7), c(1, 2, 6), c(1, 2, 6, 7), c(1, 2, 5), c(1, 2, 5, 7), c(1, 2, 5, 6), c(1, 2, 5, 6, 7), c(1, 2, 5, 6, 7, 3)) + list( + 1, c(1, 3), c(1, 2), c(1, 2, 7), c(1, 2, 6), + c(1, 2, 6, 7), c(1, 2, 5), c(1, 2, 5, 7), + c(1, 2, 5, 6), c(1, 2, 5, 6, 7), c(1, 2, 5, 6, 7, 3) + ) ) g_star_v9 <- graph_from_literal(s - +a:b - +t, a - +1:2:3:4:5 - +b) @@ -67,10 +78,11 @@ test_that("st_cuts() works", { test_that("st_cuts errors work", { g_path <- graph_from_literal(a - +b - +c - +d - +e) - expect_error(st_cuts(g_path, source = "a", target = NULL)) - expect_error(st_cuts(g_path, source = NULL, target = "a")) - expect_error(st_min_cuts(g_path, source = "a", target = NULL)) - expect_error(st_min_cuts(g_path, source = NULL, target = "a")) + + expect_snapshot(st_cuts(g_path, source = "a", target = NULL), error = TRUE) + expect_snapshot(st_cuts(g_path, source = NULL, target = "a"), error = TRUE) + expect_snapshot(st_min_cuts(g_path, source = "a", target = NULL), error = TRUE) + expect_snapshot(st_min_cuts(g_path, source = NULL, target = "a"), error = TRUE) }) test_that("max_flow works", { @@ -98,7 +110,7 @@ test_that("vertex_connectivity() works", { test_that("vertex_connectivity error works", { g_path <- make_ring(5, circular = FALSE) - expect_error(vertex_connectivity(g_path, source = 1)) + expect_snapshot(vertex_connectivity(g_path, source = 1), error = TRUE) }) test_that("edge_connectivity works", { @@ -136,7 +148,7 @@ test_that("edge_connectivity works -- names", { test_that("edge_connectivity error works", { g_path <- make_ring(5, circular = FALSE) - expect_error(edge_connectivity(g_path, source = 1)) + expect_snapshot(edge_connectivity(g_path, source = 1), error = TRUE) }) test_that("edge_disjoint_paths works", { @@ -149,8 +161,8 @@ test_that("edge_disjoint_paths works", { test_that("edge_disjoint_paths error works", { g_path <- make_ring(5, circular = FALSE) - expect_error(edge_disjoint_paths(g_path, source = 1, target = NULL)) - expect_error(edge_disjoint_paths(g_path, source = NULL, target = 1)) + expect_snapshot(edge_disjoint_paths(g_path, source = 1, target = NULL), error = TRUE) + expect_snapshot(edge_disjoint_paths(g_path, source = NULL, target = 1), error = TRUE) }) test_that("vertex_disjoint_paths works", { @@ -163,7 +175,7 @@ test_that("vertex_disjoint_paths works", { test_that("vertex_disjoint_paths error works", { g_path <- make_ring(5, circular = FALSE) - expect_error(vertex_disjoint_paths(g_path, source = 1)) + expect_snapshot(vertex_disjoint_paths(g_path, source = 1), error = TRUE) }) test_that("adhesion works", { @@ -176,7 +188,7 @@ test_that("adhesion works", { test_that("vertex_disjoint_paths error works", { g_path <- make_ring(5, circular = FALSE) - expect_error(vertex_disjoint_paths(g_path, source = 1)) + expect_snapshot(vertex_disjoint_paths(g_path, source = 1), error = TRUE) }) @@ -197,8 +209,8 @@ test_that("dominator_tree works", { test_that("dominator_tree errors work", { g_tree <- graph_from_edgelist(matrix(c(1, 2, 2, 3, 3, 4, 2, 5, 5, 6), byrow = TRUE, ncol = 2), directed = TRUE) - expect_error(dominator_tree(g_tree)) - expect_error(dominator_tree(g_tree, root = NULL)) + expect_snapshot(dominator_tree(g_tree), error = TRUE) + expect_snapshot(dominator_tree(g_tree, root = NULL), error = TRUE) }) test_that("dominator_tree works -- legacy", { From 3dedcabb7f1fb5be309ecb70c6728ab1ca130be5 Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 13 Feb 2025 20:51:19 +0100 Subject: [PATCH 8/8] fixed dominator_tree tests --- R/flow.R | 6 ++++-- tests/testthat/_snaps/flow.md | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/R/flow.R b/R/flow.R index 8a852f0f583..46ba4993642 100644 --- a/R/flow.R +++ b/R/flow.R @@ -738,10 +738,12 @@ st_min_cuts <- all_st_mincuts_impl dominator_tree <- function(graph, root, mode = c("out", "in", "all", "total")) { # Argument checks ensure_igraph(graph) - root <- as_igraph_vs(graph, root) - if (length(root) == 0) { + + if (missing(root) || is.null(root)) { cli::cli_abort("{.arg root} must be specified.") } + root <- as_igraph_vs(graph, root) + mode <- switch(igraph.match.arg(mode), "out" = 1, "in" = 2, diff --git a/tests/testthat/_snaps/flow.md b/tests/testthat/_snaps/flow.md index bf24e513c67..01e1e52f55a 100644 --- a/tests/testthat/_snaps/flow.md +++ b/tests/testthat/_snaps/flow.md @@ -86,7 +86,7 @@ dominator_tree(g_tree) Condition Error in `dominator_tree()`: - ! argument "root" is missing, with no default + ! `root` must be specified. ---