From bbc888ecce43b2ba639e92c8663cc42c3bac0519 Mon Sep 17 00:00:00 2001 From: schochastics Date: Thu, 13 Feb 2025 14:13:07 +0100 Subject: [PATCH] refactored and merged tests for make.R --- R/make.R | 193 ++++++++-------- tests/testthat/_snaps/make.md | 43 ++++ tests/testthat/test-graph.atlas.R | 11 - tests/testthat/test-graph.bipartite.R | 29 --- tests/testthat/test-graph.de.bruijn.R | 11 - tests/testthat/test-graph.kautz.R | 10 - tests/testthat/test-make.R | 309 +++++++++++++++++++++++--- tests/testthat/test-make_graph.R | 59 ----- tests/testthat/test-make_lattice.R | 49 ---- 9 files changed, 416 insertions(+), 298 deletions(-) delete mode 100644 tests/testthat/test-graph.atlas.R delete mode 100644 tests/testthat/test-graph.bipartite.R delete mode 100644 tests/testthat/test-graph.de.bruijn.R delete mode 100644 tests/testthat/test-graph.kautz.R delete mode 100644 tests/testthat/test-make_graph.R delete mode 100644 tests/testthat/test-make_lattice.R diff --git a/R/make.R b/R/make.R index ea8fbfe778..67ae90eeb9 100644 --- a/R/make.R +++ b/R/make.R @@ -1,4 +1,3 @@ - #' Create an igraph graph from a list of edges, or a notable graph #' #' @description @@ -9,15 +8,15 @@ #' @inheritParams make_graph #' @keywords internal #' @export -graph <- function(edges , ... , n = max(edges) , isolates = NULL , directed = TRUE , dir = directed , simplify = TRUE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph()", "make_graph()") - if (inherits(edges, "formula")) { - if (!missing(n)) stop("'n' should not be given for graph literals") +graph <- function(edges, ..., n = max(edges), isolates = NULL, directed = TRUE, dir = directed, simplify = TRUE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph()", "make_graph()") + if (inherits(edges, "formula")) { + if (!missing(n)) cli::cli_abort("{.arg n} should not be given for graph literals") if (!missing(isolates)) { - stop("'isolates' should not be given for graph literals") + cli::cli_abort("{.arg isolates} should not be given for graph literals") } if (!missing(directed)) { - stop("'directed' should not be given for graph literals") + cli::cli_abort("{.arg directed} should not be given for graph literals") } mf <- as.list(match.call())[-1] @@ -25,11 +24,11 @@ graph <- function(edges , ... , n = max(edges) , isolates = NULL , directed = TR graph_from_literal_i(mf) } else { if (!missing(simplify)) { - stop("'simplify' should only be used for graph literals") + cli::cli_abort("{.arg simplify} should only be used for graph literals") } if (!missing(dir) && !missing(directed)) { - stop("Only give one of 'dir' and 'directed'") + cli::cli_abort("Only give one of {.arg dir} and {.arg directed}") } if (!missing(dir) && missing(directed)) directed <- dir @@ -45,7 +44,7 @@ graph <- function(edges , ... , n = max(edges) , isolates = NULL , directed = TR if (!missing(dir)) { cli::cli_warn("{.arg dir} is ignored for the {.str {edges}} graph") } - if (length(list(...))) stop("Extra arguments in make_graph") + if (length(list(...))) cli::cli_abort("Extra arguments in make_graph") make_famous_graph(edges) @@ -77,7 +76,7 @@ graph <- function(edges , ... , n = max(edges) , isolates = NULL , directed = TR if (!missing(n)) { cli::cli_warn("{.arg n} is ignored for edge list with vertex names.") } - if (length(list(...))) stop("Extra arguments in make_graph") + if (length(list(...))) cli::cli_abort("Extra arguments in make_graph") el <- matrix(edges, ncol = 2, byrow = TRUE) res <- graph_from_edgelist(el, directed = directed) @@ -87,7 +86,7 @@ graph <- function(edges , ... , n = max(edges) , isolates = NULL , directed = TR } res } else { - stop("'edges' must be numeric or character") + cli::cli_abort("{.arg edges} must be numeric or character") } } } # nocov end @@ -102,15 +101,15 @@ graph <- function(edges , ... , n = max(edges) , isolates = NULL , directed = TR #' @inheritParams make_graph #' @keywords internal #' @export -graph.famous <- function(edges , ... , n = max(edges) , isolates = NULL , directed = TRUE , dir = directed , simplify = TRUE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.famous()", "make_graph()") - if (inherits(edges, "formula")) { - if (!missing(n)) stop("'n' should not be given for graph literals") +graph.famous <- function(edges, ..., n = max(edges), isolates = NULL, directed = TRUE, dir = directed, simplify = TRUE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.famous()", "make_graph()") + if (inherits(edges, "formula")) { + if (!missing(n)) cli::cli_abort("{.arg n} should not be given for graph literals") if (!missing(isolates)) { - stop("'isolates' should not be given for graph literals") + cli::cli_abort("{.arg isolates} should not be given for graph literals") } if (!missing(directed)) { - stop("'directed' should not be given for graph literals") + cli::cli_abort("{.arg directed} should not be given for graph literals") } mf <- as.list(match.call())[-1] @@ -118,11 +117,11 @@ graph.famous <- function(edges , ... , n = max(edges) , isolates = NULL , direct graph_from_literal_i(mf) } else { if (!missing(simplify)) { - stop("'simplify' should only be used for graph literals") + cli::cli_abort("{.arg simplify} should only be used for graph literals") } if (!missing(dir) && !missing(directed)) { - stop("Only give one of 'dir' and 'directed'") + cli::cli_abort("Only give one of {.arg dir} and {.arg directed}") } if (!missing(dir) && missing(directed)) directed <- dir @@ -138,7 +137,7 @@ graph.famous <- function(edges , ... , n = max(edges) , isolates = NULL , direct if (!missing(dir)) { cli::cli_warn("{.arg dir} is ignored for the {.str {edges}} graph.") } - if (length(list(...))) stop("Extra arguments in make_graph") + if (length(list(...))) cli::cli_abort("Extra arguments in make_graph") make_famous_graph(edges) @@ -170,7 +169,7 @@ graph.famous <- function(edges , ... , n = max(edges) , isolates = NULL , direct if (!missing(n)) { cli::cli_warn("{.arg n} is ignored for edge list with vertex names.") } - if (length(list(...))) stop("Extra arguments in make_graph") + if (length(list(...))) cli::cli_abort("Extra arguments in make_graph") el <- matrix(edges, ncol = 2, byrow = TRUE) res <- graph_from_edgelist(el, directed = directed) @@ -180,7 +179,7 @@ graph.famous <- function(edges , ... , n = max(edges) , isolates = NULL , direct } res } else { - stop("'edges' must be numeric or character") + cli::cli_abort("{.arg edges} must be numeric or character") } } } # nocov end @@ -196,8 +195,8 @@ graph.famous <- function(edges , ... , n = max(edges) , isolates = NULL , direct #' @keywords internal #' @export line.graph <- function(graph) { # nocov start - lifecycle::deprecate_soft("2.1.0", "line.graph()", "make_line_graph()") - ensure_igraph(graph) + lifecycle::deprecate_soft("2.1.0", "line.graph()", "make_line_graph()") + ensure_igraph(graph) on.exit(.Call(R_igraph_finalizer)) res <- .Call(R_igraph_linegraph, graph) @@ -217,9 +216,9 @@ line.graph <- function(graph) { # nocov start #' @inheritParams make_ring #' @keywords internal #' @export -graph.ring <- function(n , directed = FALSE , mutual = FALSE , circular = TRUE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.ring()", "make_ring()") - on.exit(.Call(R_igraph_finalizer)) +graph.ring <- function(n, directed = FALSE, mutual = FALSE, circular = TRUE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.ring()", "make_ring()") + on.exit(.Call(R_igraph_finalizer)) res <- .Call( R_igraph_ring, as.numeric(n), as.logical(directed), as.logical(mutual), as.logical(circular) @@ -242,9 +241,9 @@ graph.ring <- function(n , directed = FALSE , mutual = FALSE , circular = TRUE) #' @inheritParams make_tree #' @keywords internal #' @export -graph.tree <- function(n , children = 2 , mode = c("out","in","undirected")) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.tree()", "make_tree()") - mode <- igraph.match.arg(mode) +graph.tree <- function(n, children = 2, mode = c("out", "in", "undirected")) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.tree()", "make_tree()") + mode <- igraph.match.arg(mode) mode1 <- switch(mode, "out" = 0, "in" = 1, @@ -274,9 +273,9 @@ graph.tree <- function(n , children = 2 , mode = c("out","in","undirected")) { # #' @inheritParams make_star #' @keywords internal #' @export -graph.star <- function(n , mode = c("in","out","mutual","undirected") , center = 1) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.star()", "make_star()") - mode <- igraph.match.arg(mode) +graph.star <- function(n, mode = c("in", "out", "mutual", "undirected"), center = 1) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.star()", "make_star()") + mode <- igraph.match.arg(mode) mode1 <- switch(mode, "out" = 0, "in" = 1, @@ -311,19 +310,19 @@ graph.star <- function(n , mode = c("in","out","mutual","undirected") , center = #' @inheritParams graph_from_lcf #' @keywords internal #' @export -graph.lcf <- function(n , shifts , repeats = 1) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.lcf()", "graph_from_lcf()") - # Argument checks +graph.lcf <- function(n, shifts, repeats = 1) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.lcf()", "graph_from_lcf()") + # Argument checks n <- as.numeric(n) shifts <- as.numeric(shifts) repeats <- as.numeric(repeats) - on.exit( .Call(R_igraph_finalizer) ) + on.exit(.Call(R_igraph_finalizer)) # Function call res <- .Call(R_igraph_lcf_vector, n, shifts, repeats) if (igraph_opt("add.params")) { - res$name <- 'LCF graph' + res$name <- "LCF graph" } res @@ -340,7 +339,7 @@ graph.lcf <- function(n , shifts , repeats = 1) { # nocov start #' @keywords internal #' @export #' @cdocs igraph_square_lattice -graph.lattice <- function(dimvector = NULL , length = NULL , dim = NULL , nei = 1 , directed = FALSE , mutual = FALSE , periodic = FALSE, circular = deprecated()) { # nocov start +graph.lattice <- function(dimvector = NULL, length = NULL, dim = NULL, nei = 1, directed = FALSE, mutual = FALSE, periodic = FALSE, circular = deprecated()) { # nocov start lifecycle::deprecate_soft("2.1.0", "graph.lattice()", "make_lattice()") if (is.numeric(length) && length != floor(length)) { cli::cli_warn("{.arg length} was rounded to the nearest integer.") @@ -391,9 +390,9 @@ graph.lattice <- function(dimvector = NULL , length = NULL , dim = NULL , nei = #' @inheritParams make_kautz_graph #' @keywords internal #' @export -graph.kautz <- function(m , n) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.kautz()", "make_kautz_graph()") - on.exit(.Call(R_igraph_finalizer)) +graph.kautz <- function(m, n) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.kautz()", "make_kautz_graph()") + on.exit(.Call(R_igraph_finalizer)) res <- .Call(R_igraph_kautz, as.numeric(m), as.numeric(n)) if (igraph_opt("add.params")) { res$name <- sprintf("Kautz graph %i-%i", m, n) @@ -413,9 +412,9 @@ graph.kautz <- function(m , n) { # nocov start #' @inheritParams make_full_citation_graph #' @keywords internal #' @export -graph.full.citation <- function(n , directed = TRUE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.full.citation()", "make_full_citation_graph()") - # Argument checks +graph.full.citation <- function(n, directed = TRUE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.full.citation()", "make_full_citation_graph()") + # Argument checks n <- as.numeric(n) directed <- as.logical(directed) @@ -437,9 +436,9 @@ graph.full.citation <- function(n , directed = TRUE) { # nocov start #' @inheritParams make_full_bipartite_graph #' @keywords internal #' @export -graph.full.bipartite <- function(n1 , n2 , directed = FALSE , mode = c("all","out","in")) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.full.bipartite()", "make_full_bipartite_graph()") - n1 <- as.numeric(n1) +graph.full.bipartite <- function(n1, n2, directed = FALSE, mode = c("all", "out", "in")) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.full.bipartite()", "make_full_bipartite_graph()") + n1 <- as.numeric(n1) n2 <- as.numeric(n2) directed <- as.logical(directed) mode1 <- switch(igraph.match.arg(mode), @@ -470,9 +469,9 @@ graph.full.bipartite <- function(n1 , n2 , directed = FALSE , mode = c("all","ou #' @inheritParams make_full_graph #' @keywords internal #' @export -graph.full <- function(n , directed = FALSE , loops = FALSE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.full()", "make_full_graph()") - on.exit(.Call(R_igraph_finalizer)) +graph.full <- function(n, directed = FALSE, loops = FALSE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.full()", "make_full_graph()") + on.exit(.Call(R_igraph_finalizer)) res <- .Call( R_igraph_full, as.numeric(n), as.logical(directed), as.logical(loops) @@ -494,9 +493,9 @@ graph.full <- function(n , directed = FALSE , loops = FALSE) { # nocov start #' @inheritParams graph_from_literal #' @keywords internal #' @export -graph.formula <- function(... , simplify = TRUE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.formula()", "graph_from_literal()") - mf <- as.list(match.call())[-1] +graph.formula <- function(..., simplify = TRUE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.formula()", "graph_from_literal()") + mf <- as.list(match.call())[-1] graph_from_literal_i(mf) } # nocov end @@ -510,9 +509,9 @@ graph.formula <- function(... , simplify = TRUE) { # nocov start #' @inheritParams make_chordal_ring #' @keywords internal #' @export -graph.extended.chordal.ring <- function(n , w , directed = FALSE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.extended.chordal.ring()", "make_chordal_ring()") - on.exit(.Call(R_igraph_finalizer)) +graph.extended.chordal.ring <- function(n, w, directed = FALSE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.extended.chordal.ring()", "make_chordal_ring()") + on.exit(.Call(R_igraph_finalizer)) res <- .Call( R_igraph_extended_chordal_ring, as.numeric(n), as.matrix(w), as.logical(directed) @@ -534,13 +533,13 @@ graph.extended.chordal.ring <- function(n , w , directed = FALSE) { # nocov star #' @inheritParams make_empty_graph #' @keywords internal #' @export -graph.empty <- function(n = 0 , directed = TRUE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.empty()", "make_empty_graph()") - # Argument checks +graph.empty <- function(n = 0, directed = TRUE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.empty()", "make_empty_graph()") + # Argument checks n <- as.numeric(n) directed <- as.logical(directed) - on.exit( .Call(R_igraph_finalizer) ) + on.exit(.Call(R_igraph_finalizer)) # Function call res <- .Call(R_igraph_empty, n, directed) @@ -557,9 +556,9 @@ graph.empty <- function(n = 0 , directed = TRUE) { # nocov start #' @inheritParams make_de_bruijn_graph #' @keywords internal #' @export -graph.de.bruijn <- function(m , n) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.de.bruijn()", "make_de_bruijn_graph()") - on.exit(.Call(R_igraph_finalizer)) +graph.de.bruijn <- function(m, n) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.de.bruijn()", "make_de_bruijn_graph()") + on.exit(.Call(R_igraph_finalizer)) res <- .Call(R_igraph_de_bruijn, as.numeric(m), as.numeric(n)) if (igraph_opt("add.params")) { res$name <- sprintf("De-Bruijn graph %i-%i", m, n) @@ -579,17 +578,17 @@ graph.de.bruijn <- function(m , n) { # nocov start #' @inheritParams make_bipartite_graph #' @keywords internal #' @export -graph.bipartite <- function(types , edges , directed = FALSE) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.bipartite()", "make_bipartite_graph()") - vertex.names <- names(types) +graph.bipartite <- function(types, edges, directed = FALSE) { # nocov start + lifecycle::deprecate_soft("2.1.0", "graph.bipartite()", "make_bipartite_graph()") + vertex.names <- names(types) if (is.character(edges)) { if (is.null(vertex.names)) { - stop("`types` vector must be named when the edge vector contains strings") + cli::cli_abort("{.arg types} vector must be named when the edge vector contains strings") } edges <- match(edges, vertex.names) if (any(is.na(edges))) { - stop("edge vector contains a vertex name that is not found in `types`") + cli::cli_abort("edge vector contains a vertex name that is not found in {.arg types}") } } @@ -619,8 +618,8 @@ graph.bipartite <- function(types , edges , directed = FALSE) { # nocov start #' @keywords internal #' @export graph.atlas <- function(n) { # nocov start - lifecycle::deprecate_soft("2.1.0", "graph.atlas()", "graph_from_atlas()") - on.exit(.Call(R_igraph_finalizer)) + lifecycle::deprecate_soft("2.1.0", "graph.atlas()", "graph_from_atlas()") + on.exit(.Call(R_igraph_finalizer)) res <- .Call(R_igraph_atlas, as.numeric(n)) if (igraph_opt("add.params")) { res$name <- sprintf("Graph from the Atlas #%i", n) @@ -672,10 +671,10 @@ graph.atlas <- function(n) { # nocov start args <- list(...) cidx <- vapply(args, inherits, TRUE, what = "igraph_constructor_spec") if (sum(cidx) == 0) { - stop("Don't know how to ", .operation, ", nothing given") + cli::cli_abort("Don't know how to { .operation }, nothing given") } if (sum(cidx) > 1) { - stop("Don't know how to ", .operation, ", multiple constructors given") + cli::cli_abort("Don't know how to { .operation }, multiple constructors given") } cons <- args[cidx][[1]] args <- args[!cidx] @@ -700,7 +699,7 @@ graph.atlas <- function(n) { # nocov start if (.variant %in% variants) { cons$fun <- cons$fun[[.variant]] } else { - stop("Don't know how to ", .operation, ", unknown constructor") + cli::cli_abort("Don't know how to { .operation }, unknown constructor") } } @@ -1234,12 +1233,12 @@ with_graph_ <- function(...) { make_graph <- function(edges, ..., n = max(edges), isolates = NULL, directed = TRUE, dir = directed, simplify = TRUE) { if (inherits(edges, "formula")) { - if (!missing(n)) stop("'n' should not be given for graph literals") + if (!missing(n)) cli::cli_abort("{.arg n} should not be given for graph literals") if (!missing(isolates)) { - stop("'isolates' should not be given for graph literals") + cli::cli_abort("{.arg isolates} should not be given for graph literals") } if (!missing(directed)) { - stop("'directed' should not be given for graph literals") + cli::cli_abort("{.arg directed} should not be given for graph literals") } mf <- as.list(match.call())[-1] @@ -1247,11 +1246,11 @@ make_graph <- function(edges, ..., n = max(edges), isolates = NULL, graph_from_literal_i(mf) } else { if (!missing(simplify)) { - stop("'simplify' should only be used for graph literals") + cli::cli_abort("{.arg simplify} should only be used for graph literals") } if (!missing(dir) && !missing(directed)) { - stop("Only give one of 'dir' and 'directed'") + cli::cli_abort("Only give one of {.arg dir} and {.arg directed}") } if (!missing(dir) && missing(directed)) directed <- dir @@ -1267,7 +1266,7 @@ make_graph <- function(edges, ..., n = max(edges), isolates = NULL, if (!missing(dir)) { cli::cli_warn("{.arg dir} is ignored for the {.str {edges}} graph.") } - if (length(list(...))) stop("Extra arguments in make_graph") + if (length(list(...))) cli::cli_abort("Extra arguments in make_graph") make_famous_graph(edges) @@ -1299,7 +1298,7 @@ make_graph <- function(edges, ..., n = max(edges), isolates = NULL, if (!missing(n)) { cli::cli_warn("{.arg n} is ignored for edge list with vertex names.") } - if (length(list(...))) stop("Extra arguments in make_graph") + if (length(list(...))) cli::cli_abort("Extra arguments in make_graph") el <- matrix(edges, ncol = 2, byrow = TRUE) res <- graph_from_edgelist(el, directed = directed) @@ -1309,7 +1308,7 @@ make_graph <- function(edges, ..., n = max(edges), isolates = NULL, } res } else { - stop("'edges' must be numeric or character") + cli::cli_abort("{.arg edges} must be numeric or character") } } } @@ -1474,22 +1473,22 @@ empty_graph <- function(...) constructor_spec(make_empty_graph, ...) #' #' # A directed graph #' g3 <- graph_from_literal( -#' Alice +-+ Bob --+ Cecil +-- Daniel, -#' Eugene --+ Gordon:Helen +#' Alice + -+Bob - -+Cecil + --Daniel, +#' Eugene - -+Gordon:Helen #' ) #' g3 #' #' # A graph with isolate vertices -#' g4 <- graph_from_literal(Alice -- Bob -- Daniel, Cecil:Gordon, Helen) +#' g4 <- graph_from_literal(Alice - -Bob - -Daniel, Cecil:Gordon, Helen) #' g4 #' V(g4)$name #' #' # "Arrows" can be arbitrarily long -#' g5 <- graph_from_literal(Alice +---------+ Bob) +#' g5 <- graph_from_literal(Alice + ---------+Bob) #' g5 #' #' # Special vertex names -#' g6 <- graph_from_literal("+" -- "-", "*" -- "/", "%%" -- "%/%") +#' g6 <- graph_from_literal("+" - -"-", "*" - -"/", "%%" - -"%/%") #' g6 #' graph_from_literal <- function(..., simplify = TRUE) { @@ -1503,7 +1502,7 @@ graph_from_literal_i <- function(mf) { if ("simplify" %in% names(mf)) { w <- which(names(mf) == "simplify") if (length(w) > 1) { - stop("'simplify' specified multiple times") + cli::cli_abort("{.arg simplify} specified multiple times") } simplify <- eval(mf[[w]]) mf <- mf[-w] @@ -1523,7 +1522,7 @@ graph_from_literal_i <- function(mf) { } else if (all(ops %in% c("-", "+", ":"))) { directed <- TRUE } else { - stop("Invalid operator in formula") + cli::cli_abort("Invalid operator in formula") } f <- function(x) { @@ -2255,11 +2254,11 @@ make_bipartite_graph <- function(types, edges, directed = FALSE) { if (is.character(edges)) { if (is.null(vertex.names)) { - stop("`types` vector must be named when the edge vector contains strings") + cli::cli_abort("{.arg types} vector must be named when the edge vector contains strings") } edges <- match(edges, vertex.names) if (any(is.na(edges))) { - stop("edge vector contains a vertex name that is not found in `types`") + cli::cli_abort("edge vector contains a vertex name that is not found in {.arg types}") } } @@ -2490,11 +2489,11 @@ realize_bipartite_degseq <- function(degrees1, degrees2, ..., check_dots_empty() allowed.edge.types <- igraph.match.arg(allowed.edge.types) method <- igraph.match.arg(method) - g <- realize_bipartite_degree_sequence_impl(degrees1 = degrees1, degrees2 = degrees2, - allowed.edge.types = allowed.edge.types, - method = method) + g <- realize_bipartite_degree_sequence_impl( + degrees1 = degrees1, degrees2 = degrees2, + allowed.edge.types = allowed.edge.types, + method = method + ) V(g)$type <- c(rep(TRUE, length(degrees1)), rep(FALSE, length(degrees2))) g } - - diff --git a/tests/testthat/_snaps/make.md b/tests/testthat/_snaps/make.md index 03861931e9..54a996356f 100644 --- a/tests/testthat/_snaps/make.md +++ b/tests/testthat/_snaps/make.md @@ -1,3 +1,46 @@ +# error messages are proper + + Code + make_() + Condition + Error in `.extract_constructor_and_modifiers()`: + ! Don't know how to make_, nothing given + Code + make_(1:10) + Condition + Error in `.extract_constructor_and_modifiers()`: + ! Don't know how to make_, nothing given + Code + graph_() + Condition + Error in `.extract_constructor_and_modifiers()`: + ! Don't know how to graph_, nothing given + Code + graph_(1:10) + Condition + Error in `.extract_constructor_and_modifiers()`: + ! Don't know how to graph_, nothing given + Code + graph_(directed_graph(), directed_graph()) + Condition + Error in `.extract_constructor_and_modifiers()`: + ! Don't know how to graph_, multiple constructors given + Code + sample_() + Condition + Error in `.extract_constructor_and_modifiers()`: + ! Don't know how to sample_, nothing given + Code + sample_(1:10) + Condition + Error in `.extract_constructor_and_modifiers()`: + ! Don't know how to sample_, nothing given + Code + sample_(directed_graph(), directed_graph()) + Condition + Error in `.extract_constructor_and_modifiers()`: + ! Don't know how to sample_, multiple constructors given + # graph_from_literal() and simple undirected graphs Code diff --git a/tests/testthat/test-graph.atlas.R b/tests/testthat/test-graph.atlas.R deleted file mode 100644 index 5bd286b1c7..0000000000 --- a/tests/testthat/test-graph.atlas.R +++ /dev/null @@ -1,11 +0,0 @@ -test_that("graph.atlas works", { - g124 <- graph_from_atlas(124) - expect_isomorphic(g124, make_graph(c(1, 2, 2, 3, 3, 4, 4, 5, 1, 5, 1, 3, 2, 6), - directed = FALSE - )) - g234 <- graph_from_atlas(234) - expect_isomorphic(g234, make_graph(c(1, 6, 2, 6, 3, 6, 4, 6, 5, 6), - n = 7, - directed = FALSE - )) -}) diff --git a/tests/testthat/test-graph.bipartite.R b/tests/testthat/test-graph.bipartite.R deleted file mode 100644 index e7043164a0..0000000000 --- a/tests/testthat/test-graph.bipartite.R +++ /dev/null @@ -1,29 +0,0 @@ -test_that("make_bipartite_graph works", { - I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) - g <- graph_from_biadjacency_matrix(I) - - edges <- unlist(sapply(seq_len(nrow(I)), function(x) { - w <- which(I[x, ] != 0) + nrow(I) - if (length(w) != 0) { - as.vector(rbind(x, w)) - } else { - numeric() - } - })) - g2 <- make_bipartite_graph(seq_len(nrow(I) + ncol(I)) > nrow(I), edges) - I2 <- as_biadjacency_matrix(g2) - - expect_equal(I2, I, ignore_attr = TRUE) -}) - -test_that("make_bipartite_graph works with vertex names", { - types <- c(0, 1, 0, 1, 0, 1) - names(types) <- LETTERS[1:length(types)] - edges <- c("A", "B", "C", "D", "E", "F", "A", "D", "D", "E", "B", "C", "C", "F") - g <- make_bipartite_graph(types, edges) - - expect_equal(V(g)$name, c("A", "B", "C", "D", "E", "F"), ignore_attr = TRUE) - expect_equal(V(g)$type, c(FALSE, TRUE, FALSE, TRUE, FALSE, TRUE), ignore_attr = TRUE) - - expect_error(make_bipartite_graph(types, c(edges, "Q")), "edge vector contains a vertex name that is not found") -}) diff --git a/tests/testthat/test-graph.de.bruijn.R b/tests/testthat/test-graph.de.bruijn.R deleted file mode 100644 index deecd6f98f..0000000000 --- a/tests/testthat/test-graph.de.bruijn.R +++ /dev/null @@ -1,11 +0,0 @@ -test_that("make_de_bruijn_graph works", { - g <- make_de_bruijn_graph(2, 1) - g2 <- make_de_bruijn_graph(2, 2) - g3 <- make_line_graph(g) - - expect_isomorphic(g3, make_graph(c( - 1, 1, 3, 1, 1, 2, 3, 2, 2, 3, - 4, 3, 2, 4, 4, 4 - ))) - expect_isomorphic(g2, g3) -}) diff --git a/tests/testthat/test-graph.kautz.R b/tests/testthat/test-graph.kautz.R deleted file mode 100644 index 17cb09bf77..0000000000 --- a/tests/testthat/test-graph.kautz.R +++ /dev/null @@ -1,10 +0,0 @@ -test_that("make_kautz_graph works", { - g <- make_kautz_graph(2, 3) - expect_equal(g$name, "Kautz graph 2-3") - expect_equal(g$m, 2) - expect_equal(g$n, 3) - - el <- as_edgelist(g) - el <- el[order(el[, 1], el[, 2]), ] - expect_equal(el, structure(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 1, 2, 3, 4, 5, 6, 7, 8, 17, 18, 19, 20, 21, 22, 23, 24, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), .Dim = c(48L, 2L))) -}) diff --git a/tests/testthat/test-make.R b/tests/testthat/test-make.R index 5582189707..a63560b87a 100644 --- a/tests/testthat/test-make.R +++ b/tests/testthat/test-make.R @@ -1,58 +1,68 @@ test_that("make_ works, order of arguments does not matter", { g0 <- make_undirected_graph(1:10) g1 <- make_(undirected_graph(1:10)) - g2 <- make_(undirected_graph(), 1:10) - g3 <- make_(1:10, undirected_graph()) - expect_true(identical_graphs(g0, g1)) + + g2 <- make_(undirected_graph(), 1:10) expect_true(identical_graphs(g0, g2)) + + g3 <- make_(1:10, undirected_graph()) expect_true(identical_graphs(g0, g3)) }) +test_that("make_ works with n parameter", { + g0 <- make_undirected_graph(1:10, n = 15) + expect_equal(vcount(g0), 15) + + g1 <- make_directed_graph(1:10, n = 15) + expect_equal(vcount(g1), 15) +}) + test_that("sample_, graph_ also work", { - rlang::local_options(lifecycle_verbosity = "quiet") + rlang::local_options(lifecycle_verbosity = "quiet") g0 <- make_undirected_graph(1:10) g1 <- sample_(undirected_graph(1:10)) - g2 <- sample_(undirected_graph(), 1:10) - g3 <- sample_(1:10, undirected_graph()) - expect_true(identical_graphs(g0, g1)) + + g2 <- sample_(undirected_graph(), 1:10) expect_true(identical_graphs(g0, g2)) + + g3 <- sample_(1:10, undirected_graph()) expect_true(identical_graphs(g0, g3)) g4 <- graph_(undirected_graph(1:10)) - g5 <- graph_(undirected_graph(), 1:10) - g6 <- graph_(1:10, undirected_graph()) - expect_true(identical_graphs(g0, g4)) + + g5 <- graph_(undirected_graph(), 1:10) expect_true(identical_graphs(g0, g5)) + + g6 <- graph_(1:10, undirected_graph()) expect_true(identical_graphs(g0, g6)) }) test_that("error messages are proper", { rlang::local_options(lifecycle_verbosity = "quiet") - expect_error(make_(), "Don't know how to make_") - expect_error(make_(1:10), "Don't know how to make_") - - expect_error(graph_(), "Don't know how to graph_") - expect_error(graph_(1:10), "Don't know how to graph_") - expect_error( - graph_(directed_graph(), directed_graph()), - "Don't know how to graph_" - ) + expect_snapshot( + { + make_() + make_(1:10) - expect_error(sample_(), "Don't know how to sample_") - expect_error(sample_(1:10), "Don't know how to sample_") - expect_error( - sample_(directed_graph(), directed_graph()), - "Don't know how to sample_" + graph_() + graph_(1:10) + + graph_(directed_graph(), directed_graph()) + sample_() + sample_(1:10) + sample_(directed_graph(), directed_graph()) + }, + error = TRUE ) }) test_that("we pass arguments unevaluated", { rlang::local_options(lifecycle_verbosity = "quiet") - g0 <- graph_from_literal(A -+ B:C) - g1 <- graph_(from_literal(A -+ B:C)) + g0 <- graph_from_literal(A - +B:C) + g1 <- graph_(from_literal(A - +B:C)) expect_true(identical_graphs(g0, g1)) }) @@ -76,18 +86,18 @@ test_that("graph_from_literal() and undirected explosion", { test_that("graph_from_literal() and simple directed graphs", { local_igraph_options(print.id = FALSE) expect_snapshot({ - graph_from_literal(A -+ B) - graph_from_literal(A -+ B -+ C) - graph_from_literal(A -+ B -+ C -+ A) - graph_from_literal(A -+ B +- C -+ A) + graph_from_literal(A - +B) + graph_from_literal(A - +B - +C) + graph_from_literal(A - +B - +C - +A) + graph_from_literal(A - +B + -C - +A) }) }) test_that("graph_from_literal() and directed explosion", { local_igraph_options(print.id = FALSE) expect_snapshot({ - graph_from_literal(A:B:C -+ D:E, B:D +- C:E) - graph_from_literal(A:B:C -+ D:E +- F:G:H -+ I +- J:K:L:M) + graph_from_literal(A:B:C - +D:E, B:D + -C:E) + graph_from_literal(A:B:C - +D:E + -F:G:H - +I + -J:K:L:M) }) }) @@ -98,3 +108,238 @@ test_that("graph_from_literal(simplify = FALSE)", { graph_from_literal(1 - 1, 1 - 2, 1 - 2, simplify = FALSE) }) }) + +test_that("empty graph works", { + empty <- make_empty_graph() + expect_equal(vcount(empty), 0) + expect_equal(ecount(empty), 0) +}) + +test_that("make_star works", { + adj_mat <- matrix(0, 3, 3) + adj_mat[2:3, 1] <- 1 + expect_isomorphic( + make_star(3, "in"), + graph_from_adjacency_matrix(adj_mat) + ) + expect_isomorphic( + make_star(3, "out"), + graph_from_adjacency_matrix(t(adj_mat)) + ) + expect_isomorphic( + make_star(3, "undirected"), + graph_from_adjacency_matrix(adj_mat, mode = "max") + ) +}) + +test_that("make_full_graph works", { + adj_mat <- matrix(1, 3, 3) + diag(adj_mat) <- 0 + expect_isomorphic( + make_full_graph(3, directed = TRUE), + graph_from_adjacency_matrix(adj_mat, mode = "directed") + ) + expect_isomorphic( + make_full_graph(3, directed = FALSE), + graph_from_adjacency_matrix(adj_mat, mode = "undirected") + ) +}) + +test_that("make_lattice works", { + lattice_make <- make_lattice(dim = 2, length = 3, periodic = FALSE) + lattice_elist <- make_empty_graph(n = 9) + edges(c( + 1, 2, + 1, 4, + 2, 3, + 2, 5, + 3, 6, + 4, 5, + 4, 7, + 5, 6, + 5, 8, + 6, 9, + 7, 8, + 8, 9 + )) + expect_equal(as_edgelist(lattice_make), as_edgelist(lattice_elist)) + + lattice_make_periodic <- make_lattice(dim = 2, length = 3, periodic = TRUE) + lattice_elist_periodic <- make_empty_graph(n = 9) + edges(c( + 1, 2, + 1, 4, + 2, 3, + 2, 5, + 1, 3, + 3, 6, + 4, 5, + 4, 7, + 5, 6, + 5, 8, + 4, 6, + 6, 9, + 7, 8, + 1, 7, + 8, 9, + 2, 8, + 7, 9, + 3, 9 + )) + expect_equal(as_edgelist(lattice_make_periodic), as_edgelist(lattice_elist_periodic)) +}) + +test_that("make_lattice prints a warning for fractional length)", { + expect_warning(make_lattice(dim = 2, length = sqrt(2000)), "`length` was rounded") + + suppressWarnings(lattice_rounded <- make_lattice(dim = 2, length = sqrt(2000))) + lattice_integer <- make_lattice(dim = 2, length = 45) + expect_true(identical_graphs(lattice_rounded, lattice_integer)) +}) + +test_that("make_graph works", { + graph_make <- make_graph(1:10) + graph_elist <- make_empty_graph(n = 10) + edges(1:10) + expect_true(identical_graphs(graph_make, graph_elist)) +}) + +test_that("make_graph accepts an empty vector or NULL", { + graph_make <- make_graph(c()) + graph_empty <- make_empty_graph(n = 0) + expect_true(identical_graphs(graph_make, graph_empty)) + + graph_make_null <- make_graph(NULL, n = 0) + expect_true(identical_graphs(graph_make_null, graph_empty)) + + graph_make_c <- make_graph(edges = c(), n = 0) + expect_true(identical_graphs(graph_make_c, graph_empty)) +}) + +test_that("make_graph works for numeric edges and isolates", { + graph_make <- make_graph(1:10, n = 20) + graph_elist <- make_empty_graph(n = 20) + edges(1:10) + expect_true(identical_graphs(graph_make, graph_elist)) +}) + +test_that("make_graph handles names", { + graph_make_names <- make_graph(letters[1:10]) + graph_elist_names <- make_empty_graph() + vertices(letters[1:10]) + edges(letters[1:10]) + expect_true(identical_graphs(graph_make_names, graph_elist_names)) +}) + +test_that("make_graph handles names and isolates", { + graph_make_iso <- make_graph(letters[1:10], isolates = letters[11:20]) + graph_elist_iso <- make_empty_graph() + vertices(letters[1:20]) + edges(letters[1:10]) + expect_true(identical_graphs(graph_make_iso, graph_elist_iso)) +}) + +test_that("make_graph gives warning for ignored arguments", { + expect_warning( + make_graph(letters[1:10], n = 10) + ) + + expect_warning( + make_graph(1:10, isolates = 11:12) + ) +}) + +test_that("compatibility when arguments are not named", { + # Commit: eb46e5bb252e80780cf3c7f02ca44a57e7469752 + elist <- cbind(1, 3) + nodes <- 3 + graph_unnamed_args <- make_graph(as.vector(t(elist)), nodes, FALSE) + + expect_equal(vcount(graph_unnamed_args), 3) + expect_equal(ecount(graph_unnamed_args), 1) +}) + +test_that("make_empty_graph gives an error for invalid arguments", { + expect_error(make_empty_graph(NULL)) + expect_warning(expect_error(make_empty_graph("spam"))) +}) + +test_that("make_graph_atlas works", { + atlas_124 <- graph_from_atlas(124) + expect_isomorphic(atlas_124, make_graph(c(1, 2, 2, 3, 3, 4, 4, 5, 1, 5, 1, 3, 2, 6), + directed = FALSE + )) + atlas_234 <- graph_from_atlas(234) + expect_isomorphic(atlas_234, make_graph(c(1, 6, 2, 6, 3, 6, 4, 6, 5, 6), + n = 7, + directed = FALSE + )) +}) + +test_that("make_chordal_ring works", { + chord <- make_chordal_ring( + 15, + matrix(c(3, 12, 4, 7, 8, 11), nr = 2) + ) + expect_equal(degree(chord), rep(6, 15)) +}) + +test_that("make_line_graph works", { + graph_ring_n5 <- make_ring(n = 5) + graph_line_n5 <- make_line_graph(graph_ring_n5) + expect_isomorphic(graph_line_n5, graph_ring_n5) +}) + +test_that("make_de_bruijn_graph works", { + de_bruijn21 <- make_de_bruijn_graph(2, 1) + de_bruijn22 <- make_de_bruijn_graph(2, 2) + de_bruijn21_line <- make_line_graph(de_bruijn21) + + expect_isomorphic(de_bruijn21_line, make_graph(c( + 1, 1, 3, 1, 1, 2, 3, 2, 2, 3, + 4, 3, 2, 4, 4, 4 + ))) + expect_isomorphic(de_bruijn22, de_bruijn21_line) +}) + +test_that("make_bipartite_graph works", { + inc_mat_rand <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) + bip_from_inc <- graph_from_biadjacency_matrix(inc_mat_rand) + + edges <- unlist(sapply(seq_len(nrow(inc_mat_rand)), function(x) { + w <- which(inc_mat_rand[x, ] != 0) + nrow(inc_mat_rand) + if (length(w) != 0) { + as.vector(rbind(x, w)) + } else { + numeric() + } + })) + bip_from_make <- make_bipartite_graph(seq_len(nrow(inc_mat_rand) + ncol(inc_mat_rand)) > nrow(inc_mat_rand), edges) + inc_mat_bip <- as_biadjacency_matrix(bip_from_make) + + expect_equal(inc_mat_bip, inc_mat_rand, ignore_attr = TRUE) +}) + +test_that("make_bipartite_graph works with vertex names", { + types <- c(0, 1, 0, 1, 0, 1) + names(types) <- LETTERS[1:length(types)] + edges <- c("A", "B", "C", "D", "E", "F", "A", "D", "D", "E", "B", "C", "C", "F") + bip_grap <- make_bipartite_graph(types, edges) + + expect_equal(V(bip_grap)$name, c("A", "B", "C", "D", "E", "F"), ignore_attr = TRUE) + expect_equal(V(bip_grap)$type, c(FALSE, TRUE, FALSE, TRUE, FALSE, TRUE), ignore_attr = TRUE) + + expect_error(make_bipartite_graph(types, c(edges, "Q")), "edge vector contains a vertex name that is not found") +}) + +test_that("make_full_bipartite_graph works", { + full_bip_star <- make_full_bipartite_graph(5, 1) + expect_isomorphic(full_bip_star, make_star(6, "undirected")) + + full_bip <- make_full_bipartite_graph(5, 5) + expect_equal(vcount(full_bip), 10) + expect_equal(ecount(full_bip), 25) +}) + +test_that("make_kautz_graph works", { + kautz <- make_kautz_graph(2, 3) + expect_equal(kautz$name, "Kautz graph 2-3") + expect_equal(kautz$m, 2) + expect_equal(kautz$n, 3) + + el <- as_edgelist(kautz) + el <- el[order(el[, 1], el[, 2]), ] + expect_equal(el, structure(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 1, 2, 3, 4, 5, 6, 7, 8, 17, 18, 19, 20, 21, 22, 23, 24, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), .Dim = c(48L, 2L))) +}) diff --git a/tests/testthat/test-make_graph.R b/tests/testthat/test-make_graph.R deleted file mode 100644 index 98f827ab51..0000000000 --- a/tests/testthat/test-make_graph.R +++ /dev/null @@ -1,59 +0,0 @@ -test_that("make_graph works", { - g <- make_graph(1:10) - g2 <- make_empty_graph(n = 10) + edges(1:10) - expect_true(identical_graphs(g, g2)) -}) - -test_that("make_graph accepts an empty vector or NULL", { - g <- make_graph(c()) - g2 <- make_empty_graph(n = 0) - expect_true(identical_graphs(g, g2)) - - g <- make_graph(NULL, n = 0) - expect_true(identical_graphs(g, g2)) - - g <- make_graph(edges = c(), n = 0) - expect_true(identical_graphs(g, g2)) -}) - -test_that("make_graph works for numeric edges and isolates", { - g <- make_graph(1:10, n = 20) - g2 <- make_empty_graph(n = 20) + edges(1:10) - expect_true(identical_graphs(g, g2)) -}) - -test_that("make_graph handles names", { - g <- make_graph(letters[1:10]) - g2 <- make_empty_graph() + vertices(letters[1:10]) + edges(letters[1:10]) - expect_true(identical_graphs(g, g2)) -}) - -test_that("make_graph handles names and isolates", { - g <- make_graph(letters[1:10], isolates = letters[11:20]) - g2 <- make_empty_graph() + vertices(letters[1:20]) + edges(letters[1:10]) - expect_true(identical_graphs(g, g2)) -}) - -test_that("make_graph gives warning for ignored arguments", { - expect_warning( - make_graph(letters[1:10], n = 10) - ) - - expect_warning( - make_graph(1:10, isolates = 11:12) - ) -}) - -test_that("a make_graph bug is fixed", { - E <- cbind(1, 3) - d <- 3 - g <- make_graph(as.vector(t(E)), d, FALSE) - - expect_equal(vcount(g), 3) - expect_equal(ecount(g), 1) -}) - -test_that("make_empty_graph gives an error for invalid arguments", { - expect_error(make_empty_graph(NULL)) - expect_warning(expect_error(make_empty_graph("spam"))) -}) diff --git a/tests/testthat/test-make_lattice.R b/tests/testthat/test-make_lattice.R deleted file mode 100644 index edbf4a764e..0000000000 --- a/tests/testthat/test-make_lattice.R +++ /dev/null @@ -1,49 +0,0 @@ -test_that("make_lattice works", { - g <- make_lattice(dim = 2, length = 3, periodic = F) - g2 <- make_empty_graph(n = 9) + edges(c( - 1, 2, - 1, 4, - 2, 3, - 2, 5, - 3, 6, - 4, 5, - 4, 7, - 5, 6, - 5, 8, - 6, 9, - 7, 8, - 8, 9 - )) - expect_equal(as_edgelist(g), as_edgelist(g2)) - - g <- make_lattice(dim = 2, length = 3, periodic = T) - g2 <- make_empty_graph(n = 9) + edges(c( - 1, 2, - 1, 4, - 2, 3, - 2, 5, - 1, 3, - 3, 6, - 4, 5, - 4, 7, - 5, 6, - 5, 8, - 4, 6, - 6, 9, - 7, 8, - 1, 7, - 8, 9, - 2, 8, - 7, 9, - 3, 9 - )) - expect_equal(as_edgelist(g), as_edgelist(g2)) -}) - -test_that("make_lattice prints a warning for fractional length)", { - expect_warning(make_lattice(dim = 2, length = sqrt(2000)), "`length` was rounded") - - suppressWarnings(g <- make_lattice(dim = 2, length = sqrt(2000))) - g2 <- make_lattice(dim = 2, length = 45) - expect_true(identical_graphs(g, g2)) -})