From 8064dea7efccf711723df359f14dd9a96e5833de Mon Sep 17 00:00:00 2001 From: David Schoch Date: Thu, 27 Feb 2025 20:24:09 +0100 Subject: [PATCH] test: merged and refactored iterators.R and operators.R tests (#1723) --- R/iterators.R | 44 +- R/operators.R | 24 +- .../_snaps/{vs-es.md => iterators.md} | 0 tests/testthat/test-graph-ids.R | 61 - tests/testthat/test-graph.complementer.R | 6 - tests/testthat/test-graph.compose.R | 10 - tests/testthat/test-identical_graphs.R | 22 - tests/testthat/test-iterators.R | 350 +++++- tests/testthat/test-operators.R | 1079 ++++++++++++++++- tests/testthat/test-operators3.R | 39 - tests/testthat/test-operators4.R | 396 ------ tests/testthat/test-vs-es.R | 244 ---- tests/testthat/test-vs-operators.R | 609 ---------- 13 files changed, 1438 insertions(+), 1446 deletions(-) rename tests/testthat/_snaps/{vs-es.md => iterators.md} (100%) delete mode 100644 tests/testthat/test-graph-ids.R delete mode 100644 tests/testthat/test-graph.complementer.R delete mode 100644 tests/testthat/test-graph.compose.R delete mode 100644 tests/testthat/test-identical_graphs.R delete mode 100644 tests/testthat/test-operators3.R delete mode 100644 tests/testthat/test-operators4.R delete mode 100644 tests/testthat/test-vs-es.R delete mode 100644 tests/testthat/test-vs-operators.R diff --git a/R/iterators.R b/R/iterators.R index 876c15d987..2a3ce7ae6d 100644 --- a/R/iterators.R +++ b/R/iterators.R @@ -346,7 +346,7 @@ E <- function(graph, P = NULL, path = NULL, directed = TRUE) { update_es_ref(graph) if (!is.null(P) && !is.null(path)) { - stop("Cannot give both `P' and `path' at the same time") + cli::cli_abort("Cannot use both {.arg P} and {.arg path} at the same time.") } if (is.null(P) && is.null(path)) { @@ -386,7 +386,7 @@ create_es <- function(graph, idx, na_ok = FALSE) { simple_vs_index <- function(x, i, na_ok = FALSE) { res <- unclass(x)[i] - if (!na_ok && any(is.na(res))) stop("Unknown vertex selected") + if (!na_ok && any(is.na(res))) cli::cli_abort("Unknown vertex selected.") class(res) <- "igraph.vs" res } @@ -500,14 +500,14 @@ simple_vs_index <- function(x, i, na_ok = FALSE) { #' #' # ----------------------------------------------------------------- #' # The same with vertex names -#' g <- make_graph(~ A -+ B, B -+ C:D, D -+ B) +#' g <- make_graph(~ A - +B, B - +C:D, D - +B) #' V(g)[.nei(c("B", "D"))] #' V(g)[.nei(c("B", "D"), "in")] #' V(g)[.nei(c("B", "D"), "out")] #' #' # ----------------------------------------------------------------- #' # Resolving attributes -#' g <- make_graph(~ A -+ B, B -+ C:D, D -+ B) +#' g <- make_graph(~ A - +B, B - +C:D, D - +B) #' V(g)$color <- c("red", "red", "green", "green") #' V(g)[color == "red"] #' @@ -785,7 +785,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { } else { res <- unclass(x)[i] } - if (!na_ok && any(is.na(res))) stop("Unknown edge selected") + if (!na_ok && any(is.na(res))) cli::cli_abort("Unknown edge selected") attr(res, "env") <- attr(x, "env") attr(res, "graph") <- attr(x, "graph") class(res) <- "igraph.es" @@ -1054,9 +1054,9 @@ simple_es_index <- function(x, i, na_ok = FALSE) { `[[<-.igraph.vs` <- function(x, i, value) { if (!"name" %in% names(attributes(value)) || !"value" %in% names(attributes(value))) { - stop("invalid indexing") + cli::cli_abort("Invalid indexing.") } - if (is.null(get_vs_graph(x))) stop("Graph is unknown") + if (is.null(get_vs_graph(x))) stop("Graph is unknown.") value } @@ -1072,9 +1072,9 @@ simple_es_index <- function(x, i, na_ok = FALSE) { `[[<-.igraph.es` <- function(x, i, value) { if (!"name" %in% names(attributes(value)) || !"value" %in% names(attributes(value))) { - stop("invalid indexing") + stop("Invalid indexing.") } - if (is.null(get_es_graph(x))) stop("Graph is unknown") + if (is.null(get_es_graph(x))) stop("Graph is unknown.") value } @@ -1138,7 +1138,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { #' plot(g) `$.igraph.vs` <- function(x, name) { graph <- get_vs_graph(x) - if (is.null(graph)) stop("Graph is unknown") + if (is.null(graph)) cli::cli_abort("Graph is unknown") res <- vertex_attr(graph, name, x) if (is_single_index(x)) { res[[1]] @@ -1189,7 +1189,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { #' plot(g) `$.igraph.es` <- function(x, name) { graph <- get_es_graph(x) - if (is.null(graph)) stop("Graph is unknown") + if (is.null(graph)) cli::cli_abort("Graph is unknown") res <- edge_attr(graph, name, x) if (is_single_index(x)) { res[[1]] @@ -1205,7 +1205,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { #' @name igraph-vs-attributes #' @export `$<-.igraph.vs` <- function(x, name, value) { - if (is.null(get_vs_graph(x))) stop("Graph is unknown") + if (is.null(get_vs_graph(x))) cli::cli_abort("Graph is unknown") attr(x, "name") <- name attr(x, "value") <- value x @@ -1218,7 +1218,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { #' @export #' @family vertex and edge sequences `$<-.igraph.es` <- function(x, name, value) { - if (is.null(get_es_graph(x))) stop("Graph is unknown") + if (is.null(get_es_graph(x))) cli::cli_abort("Graph is unknown") attr(x, "name") <- name attr(x, "value") <- value x @@ -1230,7 +1230,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { ensure_igraph(x) if (!"name" %in% names(attributes(value)) || !"value" %in% names(attributes(value))) { - stop("invalid indexing") + cli::cli_abort("invalid indexing") } i_set_vertex_attr(x, attr(value, "name"), index = value, @@ -1249,7 +1249,7 @@ simple_es_index <- function(x, i, na_ok = FALSE) { ensure_igraph(x) if (!"name" %in% names(attributes(value)) || !"value" %in% names(attributes(value))) { - stop("invalid indexing") + cli::cli_abort("invalid indexing") } i_set_edge_attr(x, attr(value, "name"), index = value, @@ -1419,13 +1419,13 @@ as_igraph_vs <- function(graph, v, na.ok = FALSE) { if (inherits(v, "igraph.vs") && !is.null(graph) && !warn_version(graph)) { if (get_graph_id(graph) != get_vs_graph_id(v)) { - stop("Cannot use a vertex sequence from another graph.") + cli::cli_abort("Cannot use a vertex sequence from another graph.") } } if (is.character(v) && "name" %in% vertex_attr_names(graph)) { v <- as.numeric(match(v, V(graph)$name)) if (!na.ok && any(is.na(v))) { - stop("Invalid vertex names") + cli::cli_abort("Invalid vertex names") } v } else { @@ -1437,7 +1437,7 @@ as_igraph_vs <- function(graph, v, na.ok = FALSE) { res <- as.numeric(v) } if (!na.ok && any(is.na(res))) { - stop("Invalid vertex name(s)") + cli::cli_abort("Invalid vertex name(s)") } res } @@ -1447,7 +1447,7 @@ as_igraph_es <- function(graph, e) { if (inherits(e, "igraph.es") && !is.null(graph) && !warn_version(graph)) { if (get_graph_id(graph) != get_es_graph_id(e)) { - stop("Cannot use an edge sequence from another graph.") + cli::cli_abort("Cannot use an edge sequence from another graph.") } } if (is.character(e)) { @@ -1460,7 +1460,7 @@ as_igraph_es <- function(graph, e) { vv <- strsplit(e[Pairs], "|", fixed = TRUE) vl <- sapply(vv, length) if (any(vl != 2)) { - stop("Invalid edge name: ", e[Pairs][vl != 2][1]) + cli::cli_abort("Invalid edge name: ", e[Pairs][vl != 2][1]) } vp <- unlist(vv) if (!"name" %in% vertex_attr_names(graph)) { @@ -1481,7 +1481,7 @@ as_igraph_es <- function(graph, e) { res <- as.numeric(e) } if (any(is.na(res))) { - stop("Invalid edge names") + cli::cli_abort("Invalid edge names") } res } @@ -1500,7 +1500,7 @@ is_igraph_es <- function(x) { parse_op_args <- function(..., what, is_fun, as_fun, check_graph = TRUE) { args <- list(...) - if (any(!sapply(args, is_fun))) stop("Not ", what, " sequence") + if (any(!sapply(args, is_fun))) cli::cli_abort("Not {what} sequence") ## get the ids of all graphs graph_id <- sapply(args, get_vs_graph_id) %>% diff --git a/R/operators.R b/R/operators.R index 836455a9ab..f1ae2282c8 100644 --- a/R/operators.R +++ b/R/operators.R @@ -277,7 +277,7 @@ disjoint_union <- function(...) { })) lapply(graphs, ensure_igraph) if (byname != "auto" && !is.logical(byname)) { - stop("`bynam' must be \"auto\", or logical") + cli::cli_abort("{.arg bynam} must be \"auto\", or \"logical\".") } nonamed <- sum(sapply(graphs, is_named)) if (byname == "auto") { @@ -286,7 +286,7 @@ disjoint_union <- function(...) { cli::cli_warn("Some, but not all graphs are named, not using vertex names.") } } else if (byname && nonamed != length(graphs)) { - stop("Some graphs are not named") + cli::cli_abort("Some graphs are not named.") } edgemaps <- length(unlist(lapply(graphs, edge_attr_names))) != 0 @@ -613,7 +613,7 @@ difference.igraph <- function(big, small, byname = "auto", ...) { ensure_igraph(big) ensure_igraph(small) if (byname != "auto" && !is.logical(byname)) { - stop("`bynam' must be \"auto\", or logical") + cli::cli_abort("{.arg bynam} must be \"auto\", or \"logical\".") } nonamed <- is_named(big) + is_named(small) if (byname == "auto") { @@ -622,7 +622,7 @@ difference.igraph <- function(big, small, byname = "auto", ...) { cli::cli_warn("One, but not both graphs are named, not using vertex names.") } } else if (byname && nonamed != 2) { - stop("Some graphs are not named") + cli::cli_abort("Some graphs are not named.") } if (byname) { @@ -762,7 +762,7 @@ compose <- function(g1, g2, byname = "auto") { ensure_igraph(g2) if (byname != "auto" && !is.logical(byname)) { - stop("`byname' must be \"auto\", or logical") + cli::cli_abort("{.arg bynam} must be \"auto\", or \"logical\".") } nonamed <- is_named(g1) + is_named(g2) if (byname == "auto") { @@ -771,7 +771,7 @@ compose <- function(g1, g2, byname = "auto") { cli::cli_warn("One, but not both graphs are named, not using vertex names.") } } else if (byname && nonamed != 2) { - stop("Some graphs are not named") + cli::cli_abort("Some graphs are not named.") } if (byname) { @@ -1115,7 +1115,7 @@ path <- function(...) { ## Adding named vertices res <- add_vertices(e1, length(e2), name = e2) } else { - stop("Cannot add unknown type to igraph graph") + cli::cli_abort("Cannot add {.obj_type_friendly type} to igraph graph.") } res } @@ -1171,7 +1171,7 @@ path <- function(...) { #' @export `-.igraph` <- function(e1, e2) { if (missing(e2)) { - stop("Non-numeric argument to negation operator") + cli::cli_abort("Non-numeric argument to negation operator") } if (is_igraph(e2)) { res <- difference(e1, e2) @@ -1195,7 +1195,7 @@ path <- function(...) { } else if (is.numeric(e2) || is.character(e2)) { res <- delete_vertices(e1, e2) } else { - stop("Cannot substract unknown type from igraph graph") + cli::cli_abort("Cannot substract {.obj_type_friendly type} from igraph graph.") } res } @@ -1220,7 +1220,7 @@ path <- function(...) { #' @examples #' rings <- make_ring(5) * 5 rep.igraph <- function(x, n, mark = TRUE, ...) { - if (n < 0) stop("Number of replications must be positive") + if (n < 0) cli::cli_abort("Number of replications must be positive") res <- do_call(disjoint_union, .args = @@ -1245,7 +1245,7 @@ rep.igraph <- function(x, n, mark = TRUE, ...) { if (is.numeric(n) && length(n) == 1) { rep.igraph(x, n) } else { - stop("Cannot multiply igraph graph with this type") + cli::cli_abort("Cannot multiply igraph graph with {.obj_type_friendly type}.") } } @@ -1263,7 +1263,7 @@ rep.igraph <- function(x, n, mark = TRUE, ...) { #' #' @examples #' -#' g <- make_graph(~ 1 -+ 2, 2 -+ 3, 3 -+ 4) +#' g <- make_graph(~ 1 - +2, 2 - +3, 3 - +4) #' reverse_edges(g, 2) #' @family functions for manipulating graph structure #' @export diff --git a/tests/testthat/_snaps/vs-es.md b/tests/testthat/_snaps/iterators.md similarity index 100% rename from tests/testthat/_snaps/vs-es.md rename to tests/testthat/_snaps/iterators.md diff --git a/tests/testthat/test-graph-ids.R b/tests/testthat/test-graph-ids.R deleted file mode 100644 index a77b643c3b..0000000000 --- a/tests/testthat/test-graph-ids.R +++ /dev/null @@ -1,61 +0,0 @@ -test_that("ids change when updating the graph", { - g <- make_ring(10) - - g2 <- g + 1 - g3 <- g + edge(1, 5) - g4 <- set_vertex_attr(g, "color", value = "red") - - expect_false(graph_id(g) == graph_id(g2)) - expect_false(graph_id(g) == graph_id(g3)) -}) - - -test_that("ids don't change when attributes change", { - g <- make_ring(10) - V(g)$color <- "green" - E(g)$weight <- 1 - - g2 <- set_vertex_attr(g, "color", value = "red") - g3 <- set_edge_attr(g, "weight", value = 3) - g4 <- set_vertex_attr(g, "name", value = LETTERS[1:10]) - g5 <- set_edge_attr(g, "name", value = LETTERS[1:10]) - - expect_equal(graph_id(g), graph_id(g2)) - expect_equal(graph_id(g), graph_id(g3)) - expect_equal(graph_id(g), graph_id(g4)) - expect_equal(graph_id(g), graph_id(g5)) -}) - - -test_that("ids of vertex and edge sequences are correct", { - g <- make_ring(10) - - vs <- V(g) - vs2 <- vs[1:5] - es <- E(g) - es2 <- es[1:5] - - expect_equal(graph_id(g), graph_id(vs)) - expect_equal(graph_id(g), graph_id(vs2)) - expect_equal(graph_id(g), graph_id(es)) - expect_equal(graph_id(g), graph_id(es2)) -}) - - -test_that("ids of vertex and edge sequence remain after removing graph", { - g <- make_ring(10) - id <- graph_id(g) - - vs <- V(g) - vs2 <- vs[1:5] - es <- E(g) - es2 <- es[1:5] - - rm(g) - gc() - - expect_equal(id, graph_id(vs)) - expect_equal(id, graph_id(vs2)) - expect_equal(id, graph_id(es)) - expect_equal(id, graph_id(es2)) -}) diff --git a/tests/testthat/test-graph.complementer.R b/tests/testthat/test-graph.complementer.R deleted file mode 100644 index 2c9dcb8eac..0000000000 --- a/tests/testthat/test-graph.complementer.R +++ /dev/null @@ -1,6 +0,0 @@ -test_that("complementer works", { - g <- sample_gnp(50, 3 / 50) - g2 <- complementer(g) - g3 <- complementer(g2) - expect_isomorphic(g, g3) -}) diff --git a/tests/testthat/test-graph.compose.R b/tests/testthat/test-graph.compose.R deleted file mode 100644 index adb61ca33a..0000000000 --- a/tests/testthat/test-graph.compose.R +++ /dev/null @@ -1,10 +0,0 @@ -test_that("compose works", { - g1 <- sample_gnp(50, 3 / 50, directed = TRUE) - gi <- make_graph(rep(1:vcount(g1), each = 2), directed = TRUE) - - g2 <- compose(g1, gi) - g3 <- compose(gi, g1) - - expect_isomorphic(g1, g2) - expect_isomorphic(g1, g3) -}) diff --git a/tests/testthat/test-identical_graphs.R b/tests/testthat/test-identical_graphs.R deleted file mode 100644 index 654b6506e4..0000000000 --- a/tests/testthat/test-identical_graphs.R +++ /dev/null @@ -1,22 +0,0 @@ -test_that("identical_graphs works", { - g <- make_ring(5) - g2 <- make_ring(5) - - expect_identical_graphs(g, g2) - - g2 <- make_ring(6) - - expect_not_identical_graphs(g, g2) -}) - -test_that("identical_graphs considers attributes", { - g <- sample_pa(10) - g2 <- g - - expect_identical_graphs(g, g2) - - g2$m <- 2 - - expect_not_identical_graphs(g, g2) - expect_identical_graphs(g, g2, attrs = FALSE) -}) diff --git a/tests/testthat/test-iterators.R b/tests/testthat/test-iterators.R index e4d2d0a1bd..9df5fdadf8 100644 --- a/tests/testthat/test-iterators.R +++ b/tests/testthat/test-iterators.R @@ -1,21 +1,19 @@ test_that("iterators work", { - ## Create a small ring graph, assign attributes ring <- graph_from_literal(A - B - C - D - E - F - G - A) E(ring)$weight <- seq_len(ecount(ring)) - ## Selection based on attributes expect_equal(sort(E(ring)[weight < 4]$weight), 1:3) expect_equal(V(ring)[c("A", "C")]$name, c("A", "C")) withr::with_seed(42, { - g <- sample_pa(100, power = 0.3) + g_pa <- sample_pa(100, power = 0.3) }) - expect_equal(as.numeric(E(g)[1:3 %--% 2:6]), 1:4) - expect_equal(as.numeric(E(g)[1:5 %->% 1:6]), 1:4) - expect_length(as.numeric(E(g)[1:2 %->% 5:6]), 0) - expect_equal(as.numeric(E(g)[1:3 %<-% 30:60]), c(36, 38, 44, 56)) - expect_equal(as.numeric(E(g)[1:3 %<-% 5:6]), 4) + expect_equal(as.numeric(E(g_pa)[1:3 %--% 2:6]), 1:4) + expect_equal(as.numeric(E(g_pa)[1:5 %->% 1:6]), 1:4) + expect_length(as.numeric(E(g_pa)[1:2 %->% 5:6]), 0) + expect_equal(as.numeric(E(g_pa)[1:3 %<-% 30:60]), c(36, 38, 44, 56)) + expect_equal(as.numeric(E(g_pa)[1:3 %<-% 5:6]), 4) }) test_that("subsetting returns the whole if no argument", { @@ -48,7 +46,7 @@ test_that("complex attributes work", { expect_equal(V(g)[5]$foo, list(c(0, 0, 0, 4, 5))) }) -test_that("we got rid of confusing indexing by numbers", { +test_that("indexing by numbers works", { g <- make_ring(10) V(g)$name <- letters[1:10] E(g)$weight <- seq(ecount(g)) @@ -72,7 +70,6 @@ test_that("indexing with characters work as expected", { expect_equal(as.vector(V(g)[letters[3:6]]), 3:6) expect_equal(as.vector(E(g)[LETTERS[4:7]]), 4:7) - ## expect_equal(as.vector(E(g)[c('a|b', 'c|d')]), c(1,3)) expect_error(V(g)[1:5]["h"], "Unknown vertex selected") expect_error(E(g)[1:5]["H"], "Unknown edge selected") @@ -87,15 +84,12 @@ test_that("variable lookup in environment works", { name <- c("d", "e") index <- 3 - # attribute names take precedence over local variables by default... expect_equal(as.vector(V(g)[name]), 1:10) expect_error(E(g)[index], "Unknown edge selected") - # ...but you can use .env to get access to the variables expect_equal(as.vector(V(g)[.env$name]), c(4, 5)) expect_equal(as.vector(E(g)[.env$index]), 3) - # ...and you can use .data to get access to the attributes explicitly expect_equal(as.vector(E(g)[.data$index >= 15]), 6:10) }) @@ -116,3 +110,333 @@ test_that("E(g) returns complete iterator, completeness is lost with next subset expect_false(is_complete_iterator(E(g, P = 1:4))) expect_false(is_complete_iterator(E(g, path = 1:4))) }) + +test_that("ids change when updating the graph", { + g <- make_ring(10) + + g2 <- g + 1 + expect_false(graph_id(g) == graph_id(g2)) + + g3 <- g + edge(1, 5) + expect_false(graph_id(g) == graph_id(g3)) +}) + + +test_that("ids don't change when attributes change", { + g <- make_ring(10) + V(g)$color <- "green" + E(g)$weight <- 1 + + g2 <- set_vertex_attr(g, "color", value = "red") + expect_equal(graph_id(g), graph_id(g2)) + + g3 <- set_edge_attr(g, "weight", value = 3) + expect_equal(graph_id(g), graph_id(g3)) + + g4 <- set_vertex_attr(g, "name", value = LETTERS[1:10]) + expect_equal(graph_id(g), graph_id(g4)) + + g5 <- set_edge_attr(g, "name", value = LETTERS[1:10]) + expect_equal(graph_id(g), graph_id(g5)) +}) + + +test_that("ids of vertex and edge sequences are correct", { + g <- make_ring(10) + + vs <- V(g) + expect_equal(graph_id(g), graph_id(vs)) + + vs2 <- vs[1:5] + expect_equal(graph_id(g), graph_id(vs2)) + + es <- E(g) + expect_equal(graph_id(g), graph_id(es)) + + es2 <- es[1:5] + expect_equal(graph_id(g), graph_id(es2)) +}) + + +test_that("ids of vertex and edge sequence remain after removing graph", { + g <- make_ring(10) + id <- graph_id(g) + + vs <- V(g) + vs2 <- vs[1:5] + es <- E(g) + es2 <- es[1:5] + + rm(g) + gc() + + expect_equal(id, graph_id(vs)) + expect_equal(id, graph_id(vs2)) + expect_equal(id, graph_id(es)) + expect_equal(id, graph_id(es2)) +}) + +test_that("identical_graphs works", { + g <- make_ring(5) + g2 <- make_ring(5) + expect_identical_graphs(g, g2) + + g3 <- make_ring(6) + + expect_not_identical_graphs(g, g3) +}) + +test_that("identical_graphs considers attributes", { + g <- sample_pa(10) + g2 <- g + + expect_identical_graphs(g, g2) + + g2$m <- 2 + + expect_not_identical_graphs(g, g2) + expect_identical_graphs(g, g2, attrs = FALSE) +}) + +test_that("we can create vertex/edge seqs", { + g <- make_ring(10) + V(g) %&&% expect_true(TRUE) + E(g) %&&% expect_true(TRUE) + + V(g)$name <- letters[1:10] + V(g) %&&% expect_true(TRUE) + E(g) %&&% expect_true(TRUE) + + g <- make_ring(10) + E(g)$name <- LETTERS[1:10] + E(g) %&&% expect_true(TRUE) +}) + +test_that("vs/es keeps names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + vs <- V(g) + expect_equal(vs$name, names(vs)) + + vs2 <- vs[4:7] + expect_equal(vs2$name, names(vs2)) + + E(g)$name <- LETTERS[1:10] + es <- E(g) + expect_equal(es$name, names(es)) + + es2 <- es[4:7] + expect_equal(es2$name, names(es2)) +}) + +test_that("vs/es refers to the graph", { + g <- make_ring(10) + vs <- V(g) + expect_identical(get_vs_graph(vs), g) + + es <- E(g) + expect_identical(get_es_graph(es), g) +}) + +test_that("vs/es refers to the original graph", { + ring1 <- ring2 <- make_ring(10) + vs <- V(ring1) + es <- E(ring1) + + ring1 <- ring1 + 4 + + expect_identical(get_vs_graph(vs), ring2) + expect_identical(get_es_graph(es), ring2) +}) + +test_that("vs/es references are weak", { + g <- make_ring(10) + vs <- V(g) + es <- E(g) + + rm(g) + gc() + + expect_null(get_vs_graph(vs)) + expect_null(get_es_graph(es)) +}) + +test_that("save/load breaks references", { + g <- make_ring(10) + vs <- V(g) + es <- E(g) + + tmp <- tempfile() + on.exit(try(unlink(tmp))) + + save(vs, es, file = tmp) + rm(vs, es) + gc() + + load(tmp) + expect_null(get_vs_graph(vs)) + expect_null(get_es_graph(es)) +}) + +test_that("vs/es keeps names after graph is deleted", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + vs <- V(g) + + E(g)$name <- LETTERS[1:10] + es <- E(g) + + rm(g) + gc() + + expect_equal(names(vs), letters[1:10]) + + vs2 <- vs[4:7] + expect_equal(names(vs2), letters[4:7]) + + expect_equal(names(es), LETTERS[1:10]) + + es2 <- es[4:7] + expect_equal(names(es2), LETTERS[4:7]) +}) + +test_that("both edge and vertex names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + E(g)$name <- LETTERS[1:10] + + es <- E(g) + expect_equal(as.vector(es), 1:10) + expect_equal(names(es), LETTERS[1:10]) + el <- as_edgelist(g) + expect_equal(attr(es, "vnames"), paste(el[, 1], el[, 2], sep = "|")) + + x1 <- es[LETTERS[4:7]] + x2 <- E(g)[4:7] + expect_equal(as.vector(x1), as.vector(x2)) + expect_equal(names(x1), names(x2)) + expect_equal(attr(x1, "vnames"), attr(x2, "vnames")) + + y1 <- es[c("a|b", "d|e")] + y2 <- E(g)[c(1, 4)] + expect_equal(as.vector(y1), as.vector(y2)) + expect_equal(names(y1), names(y2)) + expect_equal(attr(y1, "vnames"), attr(y2, "vnames")) +}) + +test_that("printing connected vs/es works", { + local_igraph_options(print.id = FALSE) + + g <- make_ring(10) + vs <- V(g) + es <- E(g) + + expect_snapshot({ + vs + es + vs[1:5] + es[1:5] + vs[numeric()] + }) + + expect_snapshot({ + es[numeric()] + }) +}) + +test_that("printing named connected vs/es works", { + local_igraph_options(print.id = FALSE) + + g <- make_ring(10) + V(g)$name <- letters[1:10] + vs <- V(g) + es <- E(g) + + expect_snapshot({ + vs + es + vs[1:5] + es[1:5] + vs[numeric()] + }) + + expect_snapshot({ + es[numeric()] + }) +}) + +test_that("printing unconnected vs/es works", { + local_igraph_options(print.id = FALSE) + + g <- make_ring(10) + vs <- V(g) + es <- E(g) + + rm(g) + gc() + + expect_snapshot({ + vs + es + }) + + g <- make_ring(10) + V(g)$name <- letters[1:10] + vs <- V(g) + es <- E(g) + + rm(g) + gc() + + expect_snapshot({ + vs + es + }) +}) + +test_that("unconnected vs/es can be reused with the same graph", { + g <- make_ring(10) + vs <- V(g) + es <- E(g)[1:5] + + tmp <- tempfile() + on.exit(unlink(tmp)) + save(g, es, vs, file = tmp) + + rm(g, es, vs) + gc() + + load(tmp) + + expect_equal(degree(g, v = vs), rep(2, 10)) + expect_identical_graphs( + delete_edges(g, es), + delete_edges(g, 1:5) + ) +}) + +test_that("indexing without arguments", { + g <- make_ring(10) + + x <- V(g)[] + expect_equal(ignore_attr = TRUE, V(g), x) + + x2 <- V(g)[[]] + v <- set_single_index(V(g)) + + expect_equal(ignore_attr = TRUE, v, x2) +}) + +test_that("vertex indexes are stored as raw numbers", { + g <- make_ring(3, directed = TRUE) + V(g)$id <- V(g) + expect_identical(V(g)$id, as.numeric(1:3)) + expect_error(induced_subgraph(g, 1), NA) +}) + +test_that("edge indexes are stored as raw numbers", { + g <- make_ring(3, directed = TRUE) + E(g)$id <- E(g) + expect_identical(E(g)$id, as.numeric(1:3)) + expect_error(induced_subgraph(g, 1:2), NA) +}) diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index 6821d69b23..021debfaf7 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -33,8 +33,8 @@ test_that("disjoint_union() works", { test_that("disjoint_union() does not convert types", { # https://github.com/igraph/rigraph/issues/761 - g1 <- make_graph(~ A -- B) - g2 <- make_graph(~ D -- E) + g1 <- make_graph(~ A - -B) + g2 <- make_graph(~ D - -E) g1 <- set_edge_attr(g1, "date", value = as.POSIXct(c("2021-01-01 01:01:01"))) g2 <- set_edge_attr(g2, "date", value = as.POSIXct(c("2021-03-03 03:03:03"))) @@ -57,8 +57,14 @@ test_that("complementer() works", { x <- complementer(complementer(g2)) expect_identical_graphs(x, g2) + + gnp <- sample_gnp(50, 3 / 50) + gnp_comp <- complementer(gnp) + gnp_comp_comp <- complementer(gnp_comp) + expect_isomorphic(gnp, gnp_comp_comp) }) + test_that("compose() works", { g1 <- make_ring(10) g2 <- make_star(11, center = 11, mode = "undirected") @@ -68,13 +74,73 @@ test_that("compose() works", { expect_vcount(gc, 11) expect_ecount(gc, 60) expect_equal(diameter(gc), 2) + + gnp <- sample_gnp(50, 3 / 50, directed = TRUE) + gnp_i <- make_graph(rep(1:vcount(gnp), each = 2), directed = TRUE) + + gnp_comp1 <- compose(gnp, gnp_i) + gnp_comp2 <- compose(gnp_i, gnp) + + expect_isomorphic(gnp, gnp_comp1) + expect_isomorphic(gnp, gnp_comp2) +}) + +test_that("compose works for named graphs", { + g1 <- graph_from_literal(A - B:D:E, B - C:D, C - D, D - E) + g2 <- graph_from_literal(A - B - E - A) + + V(g1)$bar1 <- seq_len(vcount(g1)) + V(g2)$bar2 <- seq_len(vcount(g2)) + V(g1)$foo <- letters[seq_len(vcount(g1))] + V(g2)$foo <- letters[seq_len(vcount(g2))] + + E(g1)$bar1 <- seq_len(ecount(g1)) + E(g2)$bar2 <- seq_len(ecount(g2)) + E(g1)$foo <- letters[seq_len(ecount(g1))] + E(g2)$foo <- letters[seq_len(ecount(g2))] + + g <- compose(g1, g2) + df <- as_data_frame(g, what = "both") + + df.v <- read.table(stringsAsFactors = FALSE, textConnection(" + bar1 foo_1 foo_2 bar2 name +A 1 a a 1 A +B 2 b b 2 B +D 3 c NA NA D +E 4 d c 3 E +C 5 e NA NA C +")) + expect_equal(df$vertices, df.v) + + df.e <- read.table(stringsAsFactors = FALSE, textConnection(" + from to bar1 foo_1 foo_2 bar2 +1 A B 3 c c 3 +2 A A 3 c b 2 +3 A E 1 a c 3 +4 A A 1 a a 1 +5 B E 1 a b 2 +6 B B 1 a a 1 +7 B D 6 f c 3 +8 A D 6 f b 2 +9 D E 4 d c 3 +10 A D 4 d a 1 +11 D E 2 b b 2 +12 B D 2 b a 1 +13 E E 3 c b 2 +14 B E 3 c a 1 +15 E C 5 e c 3 +16 A C 5 e a 1 +")) + rownames(df$edges) <- rownames(df$edges) + expect_equal(df$edges, df.e) }) + test_that("Union of directed named graphs", { graphs <- list( - make_graph(~ 1:2:3:4:5, 1 -+ 2, 1 -+ 3, 2 -+ 3, 2 -+ 4, 3 -+ 4, 1 -+ 5, 3 -+ 5), - make_graph(~ 1:2:3:4:5, 2 -+ 3, 1 -+ 4, 2 -+ 4, 3 -+ 4, 2 -+ 5, 3 -+ 5), - make_graph(~ 1:2:3:4:5, 1 -+ 2, 1 -+ 3, 2 -+ 4, 3 -+ 4, 1 -+ 5, 4 -+ 5) + make_graph(~ 1:2:3:4:5, 1 - +2, 1 - +3, 2 - +3, 2 - +4, 3 - +4, 1 - +5, 3 - +5), + make_graph(~ 1:2:3:4:5, 2 - +3, 1 - +4, 2 - +4, 3 - +4, 2 - +5, 3 - +5), + make_graph(~ 1:2:3:4:5, 1 - +2, 1 - +3, 2 - +4, 3 - +4, 1 - +5, 4 - +5) ) gg <- union.igraph(graphs) @@ -84,9 +150,9 @@ test_that("Union of directed named graphs", { }) test_that("edge reversal works", { - directed_graph <- make_graph(~ 1 -+ 2, 1 -+ 3, 1 -+ 4, 2 -+ 3, 3 -+ 4) + directed_graph <- make_graph(~ 1 - +2, 1 - +3, 1 - +4, 2 - +3, 3 - +4) reverse_directed_graph <- reverse_edges(directed_graph, 1:3) - expected <- make_graph(~ 1 +- 2, 1 +- 3, 1 +- 4, 2 -+ 3, 3 -+ 4) + expected <- make_graph(~ 1 + -2, 1 + -3, 1 + -4, 2 - +3, 3 - +4) expect_isomorphic(reverse_directed_graph, expected) reverse_all_directed_graph <- reverse_edges(directed_graph) @@ -96,11 +162,11 @@ test_that("edge reversal works", { as_edgelist(directed_graph)[, c(2, 1)] ) - undirected_graph <- make_graph(~ 1 -- 2, 1 -- 3, 1 -- 4, 2 -- 3, 3 -- 4) + undirected_graph <- make_graph(~ 1 - -2, 1 - -3, 1 - -4, 2 - -3, 3 - -4) reverse_undirected_graph <- reverse_edges(undirected_graph, 1:3) expect_identical_graphs(undirected_graph, reverse_undirected_graph) - isolated_vertices_g <- make_graph(~ 1:2:3:4:5, 1 -+ 2, 1 -+ 4) + isolated_vertices_g <- make_graph(~ 1:2:3:4:5, 1 - +2, 1 - +4) reverse_isolated_vertices_g <- reverse_edges(isolated_vertices_g) expect_vcount(reverse_isolated_vertices_g, vcount(isolated_vertices_g)) expect_equal( @@ -110,7 +176,7 @@ test_that("edge reversal works", { }) test_that("t() is aliased to edge reversal for graphs", { - g <- make_graph(~ 1 -+ 2, 1 -+ 3, 1 -+ 4, 2 -+ 3, 3 -+ 4) + g <- make_graph(~ 1 - +2, 1 - +3, 1 - +4, 2 - +3, 3 - +4) expect_vcount(t(g), vcount(g)) expect_equal(as_edgelist(t(g)), as_edgelist(g)[, c(2, 1)]) }) @@ -151,7 +217,996 @@ test_that("vertices() works", { expect_null(V(g_none)$name) expect_snapshot_error(make_empty_graph(1) + vertices("a", "b", foo = 5:7)) +}) + +test_that("infix operators work", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + E(g)$name <- LETTERS[1:10] + + g <- g - c("a", "b") + expect_vcount(g, 8) + expect_ecount(g, 7) + expect_isomorphic(g, make_lattice(8)) + + g <- g - edge("e|f") + expect_isomorphic(g, make_lattice(5) + make_lattice(3)) + + g <- g - edge("H") + expect_isomorphic(g, graph_from_literal(a - b - c, d - e - f, g - h)) + + g <- make_ring(10) + V(g)$name <- letters[1:10] + g <- g - path("a", "b") + expect_isomorphic(g, graph_from_literal(a, b - c - d - e - f - g - h - i - j - a)) + g <- g + path("a", "b") + expect_isomorphic(g, make_ring(10)) + + g <- make_ring(10) + V(g)$name <- letters[1:10] + + g <- g - path("a", "b", "c", "d") + expect_isomorphic(g, make_lattice(8) + 2) + + expect_isomorphic( + g - V(g)[c("d", "g")], + make_lattice(4) + make_lattice(2) + 2 + ) + + expect_isomorphic( + g - E(g)["f" %--% "g"], + make_lattice(5) + make_lattice(3) + 2 + ) +}) + +test_that("disjoint union works for named graphs", { + g1 <- g2 <- make_ring(10) + g1$foo <- "bar" + V(g1)$name <- letters[1:10] + V(g2)$name <- letters[11:20] + E(g1)$weight <- 1:10 + E(g2)$weight <- 10:1 + + V(g1)$a1 <- 1:10 + V(g2)$a2 <- 11:20 + + E(g1)$b1 <- 1:10 + E(g2)$b2 <- 11:20 + + g <- disjoint_union(g1, g2) + + expect_equal( + sort(graph_attr_names(g)), + c("circular_1", "circular_2", "foo", "mutual_1", "mutual_2", "name_1", "name_2") + ) + expect_equal( + sort(vertex_attr_names(g)), + c("a1", "a2", "name") + ) + expect_equal( + sort(edge_attr_names(g)), + c("b1", "b2", "weight") + ) + + expect_equal(V(g)$name, letters[1:20]) + expect_equal(V(g)$a1, c(1:10, rep(NA, 10))) + expect_equal(V(g)$a2, c(rep(NA, 10), 11:20)) + + expect_equal(E(g)$weight, c(1:10, 10:1)) + expect_equal(E(g)$b1, c(1:10, rep(NA, 10))) + expect_equal(E(g)$b2, c(rep(NA, 10), 11:20)) +}) + +test_that("disjoint union gives warning for non-unique vertex names", { + g1 <- make_ring(5) + V(g1)$name <- letters[1:5] + g2 <- make_ring(5) + V(g2)$name <- letters[5:9] + + expect_warning(disjoint_union(g1, g2), "Duplicate vertex names in disjoint union") +}) + + +test_that("union of unnamed graphs works", { + g1 <- make_ring(10) + g2 <- make_ring(13) + g1$foo <- "bar" + E(g1)$weight <- 1:10 + E(g2)$weight <- 13:1 + + V(g1)$a1 <- 1:10 + V(g2)$a2 <- 11:23 + + E(g1)$b1 <- letters[1:10] + E(g2)$b2 <- letters[11:23] + + g <- union.igraph(g1, g2) + + expect_equal( + sort(graph_attr_names(g)), + c("circular_1", "circular_2", "foo", "mutual_1", "mutual_2", "name_1", "name_2") + ) + expect_equal( + sort(vertex_attr_names(g)), + c("a1", "a2") + ) + expect_equal( + sort(edge_attr_names(g)), + c("b1", "b2", "weight_1", "weight_2") + ) + + df1 <- as_data_frame(g) + df1 <- df1[order(df1$from, df1$to), c(1, 2, 3, 5, 4, 6)] + df2 <- merge(as_data_frame(g1), as_data_frame(g2), + by = c("from", "to"), all = TRUE + ) + rownames(df1) <- seq_len(nrow(df1)) + colnames(df2) <- c("from", "to", "weight_1", "b1", "weight_2", "b2") + expect_equal(df1, df2) +}) + +test_that("union of named graphs works", { + g1 <- make_ring(10) + g2 <- make_ring(13) + V(g1)$name <- letters[seq_len(vcount(g1))] + V(g2)$name <- letters[seq_len(vcount(g2))] + + g1$foo <- "bar" + E(g1)$weight <- 1:10 + E(g2)$weight <- 13:1 + + V(g1)$a1 <- 1:10 + V(g2)$a2 <- 11:23 + + E(g1)$b1 <- letters[1:10] + E(g2)$b2 <- letters[11:23] + + g <- union.igraph(g1, g2) + + expect_equal( + sort(graph_attr_names(g)), + c("circular_1", "circular_2", "foo", "mutual_1", "mutual_2", "name_1", "name_2") + ) + expect_equal( + sort(vertex_attr_names(g)), + c("a1", "a2", "name") + ) + expect_equal( + sort(edge_attr_names(g)), + c("b1", "b2", "weight_1", "weight_2") + ) + + df1 <- as_data_frame(g, what = "both") + + g.v <- read.table(stringsAsFactors = FALSE, textConnection(" + a1 a2 name +a 1 11 a +b 2 12 b +c 3 13 c +d 4 14 d +e 5 15 e +f 6 16 f +g 7 17 g +h 8 18 h +i 9 19 i +j 10 20 j +k NA 21 k +l NA 22 l +m NA 23 m +")) + expect_equal(df1$vertices, g.v) + + g.e <- read.table(stringsAsFactors = FALSE, textConnection(" + from to weight_1 weight_2 b1 b2 +1 l m NA 2 NA v +2 k l NA 3 NA u +3 j k NA 4 NA t +4 i j 9 5 i s +5 h i 8 6 h r +6 g h 7 7 g q +7 f g 6 8 f p +8 e f 5 9 e o +9 d e 4 10 d n +10 c d 3 11 c m +11 b c 2 12 b l +12 a m NA 1 NA w +13 a j 10 NA j NA +14 a b 1 13 a k +")) + rownames(df1$edges) <- rownames(df1$edges) + expect_equal(df1$edges, g.e) +}) + +test_that("intersection of named graphs works", { + g1 <- make_ring(10) + g2 <- make_ring(13) + V(g1)$name <- letters[V(g1)] + V(g2)$name <- letters[V(g2)] + + g1$foo <- "bar" + E(g1)$weight <- 1:10 + E(g2)$weight <- 13:1 + + V(g1)$a1 <- 1:10 + V(g2)$a2 <- 11:23 + + E(g1)$b1 <- letters[1:10] + E(g2)$b2 <- letters[11:23] + + g <- intersection(g1, g2, keep.all.vertices = FALSE) + + expect_equal( + sort(graph_attr_names(g)), + c("circular_1", "circular_2", "foo", "mutual_1", "mutual_2", "name_1", "name_2") + ) + expect_equal( + sort(vertex_attr_names(g)), + c("a1", "a2", "name") + ) + expect_equal( + sort(edge_attr_names(g)), + c("b1", "b2", "weight_1", "weight_2") + ) + + df1 <- as_data_frame(g, what = "both") + + g.e <- read.table(stringsAsFactors = FALSE, textConnection(" + from to weight_1 weight_2 b1 b2 +1 i j 9 5 i s +2 h i 8 6 h r +3 g h 7 7 g q +4 f g 6 8 f p +5 e f 5 9 e o +6 d e 4 10 d n +7 c d 3 11 c m +8 b c 2 12 b l +9 a b 1 13 a k +")) + rownames(df1$edges) <- rownames(df1$edges) + expect_equal(df1$edges, g.e) + + g.v <- read.table(stringsAsFactors = FALSE, textConnection(" + a1 a2 name +a 1 11 a +b 2 12 b +c 3 13 c +d 4 14 d +e 5 15 e +f 6 16 f +g 7 17 g +h 8 18 h +i 9 19 i +j 10 20 j +")) + expect_equal(df1$vertices, g.v) + + gg <- intersection(g1, g2, keep.all.vertices = TRUE) + + df2 <- as_data_frame(gg, what = "both") + + rownames(df2$edges) <- rownames(df2$edges) + expect_equal(df2$edges, g.e) + + gg.v <- read.table(stringsAsFactors = FALSE, textConnection(" + a1 a2 name +a 1 11 a +b 2 12 b +c 3 13 c +d 4 14 d +e 5 15 e +f 6 16 f +g 7 17 g +h 8 18 h +i 9 19 i +j 10 20 j +k NA 21 k +l NA 22 l +m NA 23 m +")) + expect_equal(df2$vertices, gg.v) +}) + +test_that("difference of named graphs works", { + g1 <- make_ring(10) + g2 <- make_star(11, center = 11, mode = "undirected") + V(g1)$name <- letters[1:10] + V(g2)$name <- letters[1:11] + g <- g1 %u% g2 + + sg <- make_ring(4) + V(sg)$name <- letters[c(1, 2, 3, 11)] + + df1 <- as_data_frame(g - sg, what = "both") + + t1.e <- read.table( + stringsAsFactors = FALSE, + textConnection(" + from to +1 a j +2 b k +3 c d +4 j k +5 i k +6 h k +7 g k +8 f k +9 e k +10 d k +11 d e +12 e f +13 f g +14 g h +15 h i +16 i j +") + ) + rownames(df1$edges) <- rownames(df1$edges) + expect_equal(df1$edges, t1.e) + + expect_equal(df1$vertices, data.frame(row.names = letters[1:11], name = letters[1:11], stringsAsFactors = FALSE)) + + gg <- sg - g + + expect_ecount(gg, 0) + expect_equal(V(gg)$name, letters[c(1:3, 11)]) +}) + + +test_that("intersection of non-named graphs keeps attributes properly", { + withr::local_seed(42) + + g <- sample_gnp(10, 1 / 2) + g2 <- sample_gnp(10, 1 / 2) + E(g)$weight <- sample(ecount(g)) + E(g2)$weight <- sample(ecount(g2)) + + gi <- intersection(g, g2) + + rn <- function(D) { + rownames(D) <- paste(D[, 1], D[, 2], sep = "-") + D + } + + df <- rn(as_data_frame(g)) + df2 <- rn(as_data_frame(g2)) + dfi <- rn(as_data_frame(gi)) + + expect_equal(df[rownames(dfi), ], dfi[, 1:3], ignore_attr = TRUE) + expect_equal(df2[rownames(dfi), ], dfi[, c(1, 2, 4)], ignore_attr = TRUE) +}) + +test_that("union of non-named graphs keeps attributes properly", { + withr::local_seed(42) + + g <- sample_gnp(10, 1 / 2) + g2 <- sample_gnp(10, 1 / 2) + E(g)$weight <- sample(ecount(g)) + E(g2)$weight <- sample(ecount(g2)) + + gu <- union.igraph(g, g2) + + rn <- function(D) { + rownames(D) <- paste(D[, 1], D[, 2], sep = "-") + D + } + + df <- rn(as_data_frame(g)) + df2 <- rn(as_data_frame(g2)) + dfu <- rn(as_data_frame(gu)) + + expect_equal(dfu[rownames(df), 1:3], df, ignore_attr = TRUE) + expect_equal(dfu[rownames(df2), c(1, 2, 4)], df2, ignore_attr = TRUE) + + expect_equal( + dfu[!rownames(dfu) %in% rownames(df), 3], + rep(NA_real_, ecount(gu) - ecount(g)) + ) + expect_equal( + dfu[!rownames(dfu) %in% rownames(df2), 4], + rep(NA_real_, ecount(gu) - ecount(g2)) + ) +}) + +test_that("c on attached vs", { + g <- make_ring(10) + + vg <- V(g)[1:5] + vg2 <- V(g)[6:10] + expect_equal(ignore_attr = TRUE, c(vg, vg2), V(g)) + expect_equal(get_vs_graph_id(c(vg, vg2)), get_graph_id(g)) + + vg <- V(g) + vg2 <- V(g)[FALSE] + expect_equal(ignore_attr = TRUE, c(vg, vg2), V(g)) + expect_equal(ignore_attr = TRUE, c(vg2, vg), V(g)) + + vg <- V(g)[c(2, 5, 6, 8)] + expect_equal(ignore_attr = TRUE, c(vg, vg), V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)]) +}) + +test_that("c on detached vs", { + g <- make_ring(10) + + vg <- V(g)[1:5] + vg2 <- V(g)[6:10] + + vg3 <- V(g) + vg4 <- V(g)[FALSE] + + vg5 <- V(g)[c(2, 5, 6, 8)] + vg6 <- V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)] + + rm(g) + gc() + + expect_equal(ignore_attr = TRUE, c(vg, vg2), vg3) + expect_equal(ignore_attr = TRUE, c(vg3, vg4), vg3) + expect_equal(ignore_attr = TRUE, c(vg4, vg3), vg3) + expect_equal(ignore_attr = TRUE, c(vg5, vg5), vg6) +}) + +test_that("c on attached vs, names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + + vg <- V(g)[1:5] + vg2 <- V(g)[6:10] + expect_equal(ignore_attr = TRUE, c(vg, vg2), V(g)) + expect_equal(names(c(vg, vg2)), names(V(g))) + + vg <- V(g) + vg2 <- V(g)[FALSE] + expect_equal(ignore_attr = TRUE, c(vg, vg2), V(g)) + expect_equal(names(c(vg, vg2)), names(V(g))) + expect_equal(ignore_attr = TRUE, c(vg2, vg), V(g)) + expect_equal(names(c(vg2, vg)), names(V(g))) + + vg <- V(g)[c(2, 5, 6, 8)] + expect_equal(ignore_attr = TRUE, c(vg, vg), V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)]) + expect_equal(names(c(vg, vg)), names(V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)])) +}) + +test_that("c on detached vs, names", { + g <- make_ring(10) + + vg <- V(g)[1:5] + vg2 <- V(g)[6:10] + + vg3 <- V(g) + vg4 <- V(g)[FALSE] + + vg5 <- V(g)[c(2, 5, 6, 8)] + vg6 <- V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)] + + rm(g) + gc() + + expect_equal(ignore_attr = TRUE, c(vg, vg2), vg3) + expect_equal(names(c(vg, vg2)), names(vg3)) + expect_equal(ignore_attr = TRUE, c(vg3, vg4), vg3) + expect_equal(names(c(vg3, vg4)), names(vg3)) + expect_equal(ignore_attr = TRUE, c(vg4, vg3), vg3) + expect_equal(names(c(vg3, vg4)), names(vg3)) + expect_equal(ignore_attr = TRUE, c(vg5, vg5), vg6) + expect_equal(names(c(vg5, vg5)), names(vg6)) +}) + + + +test_that("union on attached vs", { + g <- make_ring(10) + + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + vu <- union(v1, v2) + expect_equal(ignore_attr = TRUE, vu, V(g)) + + expect_equal(ignore_attr = TRUE, union(V(g)), V(g)) + + v3 <- V(g)[FALSE] + expect_equal(ignore_attr = TRUE, union(V(g), v3), V(g)) + expect_equal(ignore_attr = TRUE, union(v3, V(g), v3), V(g)) + expect_equal(ignore_attr = TRUE, union(v3), v3) + expect_equal(ignore_attr = TRUE, union(v3, v3, v3), v3) + expect_equal(ignore_attr = TRUE, union(v3, v3), v3) +}) + +test_that("union on detached vs", { + g <- make_ring(10) + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + vu <- union(v1, v2) + v3 <- V(g)[FALSE] + + rm(g) + gc() + + expect_equal(ignore_attr = TRUE, vu, vg) + + expect_equal(ignore_attr = TRUE, union(vg), vg) + + expect_equal(ignore_attr = TRUE, union(vg, v3), vg) + expect_equal(ignore_attr = TRUE, union(v3, vg, v3), vg) + expect_equal(ignore_attr = TRUE, union(v3), v3) + expect_equal(ignore_attr = TRUE, union(v3, v3, v3), v3) + expect_equal(ignore_attr = TRUE, union(v3, v3), v3) +}) + +test_that("union on attached vs, names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + vu <- union(v1, v2) + expect_equal(ignore_attr = TRUE, vu, V(g)) + expect_equal(names(vu), names(V(g))) + + expect_equal(ignore_attr = TRUE, union(V(g)), V(g)) + expect_equal(names(union(V(g))), names(V(g))) + + v3 <- V(g)[FALSE] + expect_equal(ignore_attr = TRUE, union(V(g), v3), V(g)) + expect_equal(names(union(V(g), v3)), names(V(g))) + + expect_equal(ignore_attr = TRUE, union(v3, V(g), v3), V(g)) + expect_equal(names(union(v3, V(g), v3)), names(V(g))) + + expect_equal(ignore_attr = TRUE, union(v3), v3) + expect_equal(names(union(v3)), names(v3)) + + expect_equal(ignore_attr = TRUE, union(v3, v3, v3), v3) + expect_equal(names(union(v3, v3, v3)), names(v3)) + + expect_equal(ignore_attr = TRUE, union(v3, v3), v3) + expect_equal(names(union(v3, v3)), names(v3)) +}) + +test_that("union on detached vs, names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + + rm(g) + gc() + + vu <- union(v1, v2) + expect_equal(ignore_attr = TRUE, vu, vg) + expect_equal(names(vu), names(vg)) + + expect_equal(ignore_attr = TRUE, union(vg), vg) + expect_equal(names(union(vg)), names(vg)) + + expect_equal(ignore_attr = TRUE, union(vg, v3), vg) + expect_equal(names(union(vg, v3)), names(vg)) + + expect_equal(ignore_attr = TRUE, union(v3, vg, v3), vg) + expect_equal(names(union(v3, vg, v3)), names(vg)) + + expect_equal(ignore_attr = TRUE, union(v3), v3) + expect_equal(names(union(v3)), names(v3)) + + expect_equal(ignore_attr = TRUE, union(v3, v3, v3), v3) + expect_equal(names(union(v3, v3, v3)), names(v3)) + + expect_equal(ignore_attr = TRUE, union(v3, v3), v3) + expect_equal(names(union(v3, v3)), names(v3)) +}) + +test_that("intersection on attached vs", { + g <- make_ring(10) + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + v4 <- V(g)[1:3] + + v12 <- V(g)[6:7] + v13 <- V(g)[FALSE] + v14 <- V(g)[1:3] + v24 <- V(g)[FALSE] + + vi1 <- intersection(v1, v2) + expect_equal(ignore_attr = TRUE, vi1, v12) + + vi2 <- intersection(v1, v3) + expect_equal(ignore_attr = TRUE, vi2, v13) + + vi3 <- intersection(v1, v4) + expect_equal(ignore_attr = TRUE, vi3, v14) + + vi4 <- intersection(v1, vg) + expect_equal(ignore_attr = TRUE, vi4, v1) + + vi5 <- intersection(v2, v4) + expect_equal(ignore_attr = TRUE, vi5, v24) + + vi6 <- intersection(v3, vg) + expect_equal(ignore_attr = TRUE, vi6, v3) +}) + +test_that("intersection on detached vs", { + g <- make_ring(10) + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + v4 <- V(g)[1:3] + + v12 <- V(g)[6:7] + v13 <- V(g)[FALSE] + v14 <- V(g)[1:3] + v24 <- V(g)[FALSE] + + rm(g) + gc() + + vi1 <- intersection(v1, v2) + expect_equal(ignore_attr = TRUE, vi1, v12) + + vi2 <- intersection(v1, v3) + expect_equal(ignore_attr = TRUE, vi2, v13) + + vi3 <- intersection(v1, v4) + expect_equal(ignore_attr = TRUE, vi3, v14) + + vi4 <- intersection(v1, vg) + expect_equal(ignore_attr = TRUE, vi4, v1) + + vi5 <- intersection(v2, v4) + expect_equal(ignore_attr = TRUE, vi5, v24) + + vi6 <- intersection(v3, vg) + expect_equal(ignore_attr = TRUE, vi6, v3) +}) + +test_that("intersection on attached vs, names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + v4 <- V(g)[1:3] + + v12 <- V(g)[6:7] + v13 <- V(g)[FALSE] + v14 <- V(g)[1:3] + v24 <- V(g)[FALSE] + + vi1 <- intersection(v1, v2) + expect_equal(ignore_attr = TRUE, vi1, v12) + expect_equal(names(vi1), names(v12)) + + vi2 <- intersection(v1, v3) + expect_equal(ignore_attr = TRUE, vi2, v13) + expect_equal(names(vi2), names(v13)) + + vi3 <- intersection(v1, v4) + expect_equal(ignore_attr = TRUE, vi3, v14) + expect_equal(names(vi3), names(v14)) + + vi4 <- intersection(v1, vg) + expect_equal(ignore_attr = TRUE, vi4, v1) + expect_equal(names(vi4), names(v1)) + + vi5 <- intersection(v2, v4) + expect_equal(ignore_attr = TRUE, vi5, v24) + expect_equal(names(vi5), names(v24)) + + vi6 <- intersection(v3, vg) + expect_equal(ignore_attr = TRUE, vi6, v3) + expect_equal(names(vi6), names(v3)) +}) + +test_that("intersection on detached vs, names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + v4 <- V(g)[1:3] + + v12 <- V(g)[6:7] + v13 <- V(g)[FALSE] + v14 <- V(g)[1:3] + v24 <- V(g)[FALSE] + + rm(g) + gc() + + vi1 <- intersection(v1, v2) + expect_equal(ignore_attr = TRUE, vi1, v12) + expect_equal(names(vi1), names(v12)) + + vi2 <- intersection(v1, v3) + expect_equal(ignore_attr = TRUE, vi2, v13) + expect_equal(names(vi2), names(v13)) + + vi3 <- intersection(v1, v4) + expect_equal(ignore_attr = TRUE, vi3, v14) + expect_equal(names(vi3), names(v14)) + + vi4 <- intersection(v1, vg) + expect_equal(ignore_attr = TRUE, vi4, v1) + expect_equal(names(vi4), names(v1)) + + vi5 <- intersection(v2, v4) + expect_equal(ignore_attr = TRUE, vi5, v24) + expect_equal(names(vi5), names(v24)) + + vi6 <- intersection(v3, vg) + expect_equal(ignore_attr = TRUE, vi6, v3) + expect_equal(names(vi6), names(v3)) +}) + +test_that("difference on attached vs", { + g <- make_ring(10) + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + v4 <- V(g)[1:3] + + vr1 <- V(g)[8:10] + vr2 <- V(g) + vr3 <- V(g)[1:5] + vr4 <- V(g)[4:7] + vr5 <- V(g)[FALSE] + vr6 <- V(g)[FALSE] + + vd1 <- difference(vg, v1) + vd2 <- difference(vg, v3) + vd3 <- difference(v1, v2) + vd4 <- difference(v1, v4) + vd5 <- difference(v3, v3) + vd6 <- difference(v3, v4) + + expect_equal(ignore_attr = TRUE, vd1, vr1) + expect_equal(ignore_attr = TRUE, vd2, vr2) + expect_equal(ignore_attr = TRUE, vd3, vr3) + expect_equal(ignore_attr = TRUE, vd4, vr4) + expect_equal(ignore_attr = TRUE, vd5, vr5) + expect_equal(ignore_attr = TRUE, vd6, vr6) +}) + +test_that("difference on detached vs", { + g <- make_ring(10) + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + v4 <- V(g)[1:3] + + vr1 <- V(g)[8:10] + vr2 <- V(g) + vr3 <- V(g)[1:5] + vr4 <- V(g)[4:7] + vr5 <- V(g)[FALSE] + vr6 <- V(g)[FALSE] + + rm(g) + gc() + + vd1 <- difference(vg, v1) + vd2 <- difference(vg, v3) + vd3 <- difference(v1, v2) + vd4 <- difference(v1, v4) + vd5 <- difference(v3, v3) + vd6 <- difference(v3, v4) + + expect_equal(ignore_attr = TRUE, vd1, vr1) + expect_equal(ignore_attr = TRUE, vd2, vr2) + expect_equal(ignore_attr = TRUE, vd3, vr3) + expect_equal(ignore_attr = TRUE, vd4, vr4) + expect_equal(ignore_attr = TRUE, vd5, vr5) + expect_equal(ignore_attr = TRUE, vd6, vr6) +}) + +test_that("difference on attached vs, names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + v4 <- V(g)[1:3] + + vr1 <- V(g)[8:10] + vr2 <- V(g) + vr3 <- V(g)[1:5] + vr4 <- V(g)[4:7] + vr5 <- V(g)[FALSE] + vr6 <- V(g)[FALSE] + + vd1 <- difference(vg, v1) + vd2 <- difference(vg, v3) + vd3 <- difference(v1, v2) + vd4 <- difference(v1, v4) + vd5 <- difference(v3, v3) + vd6 <- difference(v3, v4) + + expect_equal(ignore_attr = TRUE, vd1, vr1) + expect_equal(names(vd1), names(vr1)) + + expect_equal(ignore_attr = TRUE, vd2, vr2) + expect_equal(names(vd2), names(vr2)) + + expect_equal(ignore_attr = TRUE, vd3, vr3) + expect_equal(names(vd3), names(vr3)) + + expect_equal(ignore_attr = TRUE, vd4, vr4) + expect_equal(names(vd4), names(vr4)) + + expect_equal(ignore_attr = TRUE, vd5, vr5) + expect_equal(names(vd5), names(vr5)) + + expect_equal(ignore_attr = TRUE, vd6, vr6) + expect_equal(names(vd6), names(vr6)) +}) + +test_that("difference on detached vs, names", { + g <- make_ring(10) + V(g)$name <- letters[1:10] + + vg <- V(g) + v1 <- V(g)[1:7] + v2 <- V(g)[6:10] + v3 <- V(g)[FALSE] + v4 <- V(g)[1:3] + + vr1 <- V(g)[8:10] + vr2 <- V(g) + vr3 <- V(g)[1:5] + vr4 <- V(g)[4:7] + vr5 <- V(g)[FALSE] + vr6 <- V(g)[FALSE] + + rm(g) + gc() + + vd1 <- difference(vg, v1) + vd2 <- difference(vg, v3) + vd3 <- difference(v1, v2) + vd4 <- difference(v1, v4) + vd5 <- difference(v3, v3) + vd6 <- difference(v3, v4) + + expect_equal(ignore_attr = TRUE, vd1, vr1) + expect_equal(names(vd1), names(vr1)) + + expect_equal(ignore_attr = TRUE, vd2, vr2) + expect_equal(names(vd2), names(vr2)) + + expect_equal(ignore_attr = TRUE, vd3, vr3) + expect_equal(names(vd3), names(vr3)) + + expect_equal(ignore_attr = TRUE, vd4, vr4) + expect_equal(names(vd4), names(vr4)) + + expect_equal(ignore_attr = TRUE, vd5, vr5) + expect_equal(names(vd5), names(vr5)) + + expect_equal(ignore_attr = TRUE, vd6, vr6) + expect_equal(names(vd6), names(vr6)) +}) + +test_that("rev on attached vs", { + for (i in 1:10) { + g <- make_ring(10) + idx <- seq_len(i) + vg <- V(g)[idx] + vgr <- V(g)[rev(idx)] + vg2 <- rev(vg) + expect_equal(ignore_attr = TRUE, vg2, vgr) + } +}) + +test_that("rev on detached vs", { + for (i in 1:10) { + g <- make_ring(10) + idx <- seq_len(i) + vg <- V(g)[idx] + vgr <- V(g)[rev(idx)] + rm(g) + gc() + vg2 <- rev(vg) + expect_equal(ignore_attr = TRUE, vg2, vgr) + } +}) + +test_that("rev on attached vs, names", { + for (i in 1:10) { + g <- make_ring(10) + V(g)$name <- letters[1:10] + idx <- seq_len(i) + vg <- V(g)[idx] + vgr <- V(g)[rev(idx)] + vg2 <- rev(vg) + expect_equal(ignore_attr = TRUE, vg2, vgr) + expect_equal(names(vg2), names(vgr)) + } +}) + +test_that("rev on detached vs, names", { + for (i in 1:10) { + g <- make_ring(10) + V(g)$name <- letters[1:10] + idx <- seq_len(i) + vg <- V(g)[idx] + vgr <- V(g)[rev(idx)] + rm(g) + gc() + vg2 <- rev(vg) + expect_equal(ignore_attr = TRUE, vg2, vgr) + expect_equal(names(vg2), names(vgr)) + } +}) + +unique_tests <- list( + list(1:5, 1:5), + list(c(1, 1, 2:5), 1:5), + list(c(1, 1, 1, 1), 1), + list(c(1, 2, 2, 2), 1:2), + list(c(2, 2, 1, 1), 2:1), + list(c(1, 2, 1, 2), 1:2), + list(c(), c()) +) + +test_that("unique on attached vs", { + sapply(unique_tests, function(d) { + g <- make_ring(10) + vg <- unique(V(g)[d[[1]]]) + vr <- V(g)[d[[2]]] + expect_equal(ignore_attr = TRUE, vg, vr) + }) +}) + +test_that("unique on detached vs", { + sapply(unique_tests, function(d) { + g <- make_ring(10) + vg <- V(g)[d[[1]]] + vr <- V(g)[d[[2]]] + rm(g) + gc() + vg <- unique(vg) + expect_equal(ignore_attr = TRUE, vg, vr) + }) +}) + +test_that("unique on attached vs, names", { + sapply(unique_tests, function(d) { + g <- make_ring(10) + V(g)$name <- letters[1:10] + vg <- unique(V(g)[d[[1]]]) + vr <- V(g)[d[[2]]] + expect_equal(ignore_attr = TRUE, vg, vr) + }) +}) - # Undefined, - # make_empty_graph(1) + vertices("a", "b", name = "c") +test_that("unique on detached vs, names", { + sapply(unique_tests, function(d) { + g <- make_ring(10) + V(g)$name <- letters[1:10] + vg <- V(g)[d[[1]]] + vr <- V(g)[d[[2]]] + rm(g) + gc() + vg <- unique(vg) + expect_equal(ignore_attr = TRUE, vg, vr) + }) }) diff --git a/tests/testthat/test-operators3.R b/tests/testthat/test-operators3.R deleted file mode 100644 index ab60390542..0000000000 --- a/tests/testthat/test-operators3.R +++ /dev/null @@ -1,39 +0,0 @@ -test_that("infix operators work", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - E(g)$name <- LETTERS[1:10] - - g <- g - c("a", "b") - expect_vcount(g, 8) - expect_ecount(g, 7) - expect_isomorphic(g, make_lattice(8)) - - g <- g - edge("e|f") - expect_isomorphic(g, make_lattice(5) + make_lattice(3)) - - g <- g - edge("H") - expect_isomorphic(g, graph_from_literal(a - b - c, d - e - f, g - h)) - - g <- make_ring(10) - V(g)$name <- letters[1:10] - g <- g - path("a", "b") - expect_isomorphic(g, graph_from_literal(a, b - c - d - e - f - g - h - i - j - a)) - g <- g + path("a", "b") - expect_isomorphic(g, make_ring(10)) - - g <- make_ring(10) - V(g)$name <- letters[1:10] - - g <- g - path("a", "b", "c", "d") - expect_isomorphic(g, make_lattice(8) + 2) - - expect_isomorphic( - g - V(g)[c("d", "g")], - make_lattice(4) + make_lattice(2) + 2 - ) - - expect_isomorphic( - g - E(g)["f" %--% "g"], - make_lattice(5) + make_lattice(3) + 2 - ) -}) diff --git a/tests/testthat/test-operators4.R b/tests/testthat/test-operators4.R deleted file mode 100644 index 6f24be906a..0000000000 --- a/tests/testthat/test-operators4.R +++ /dev/null @@ -1,396 +0,0 @@ -test_that("disjoint union works for named graphs", { - g1 <- g2 <- make_ring(10) - g1$foo <- "bar" - V(g1)$name <- letters[1:10] - V(g2)$name <- letters[11:20] - E(g1)$weight <- 1:10 - E(g2)$weight <- 10:1 - - V(g1)$a1 <- 1:10 - V(g2)$a2 <- 11:20 - - E(g1)$b1 <- 1:10 - E(g2)$b2 <- 11:20 - - g <- disjoint_union(g1, g2) - - expect_equal( - sort(graph_attr_names(g)), - c("circular_1", "circular_2", "foo", "mutual_1", "mutual_2", "name_1", "name_2") - ) - expect_equal( - sort(vertex_attr_names(g)), - c("a1", "a2", "name") - ) - expect_equal( - sort(edge_attr_names(g)), - c("b1", "b2", "weight") - ) - - expect_equal(V(g)$name, letters[1:20]) - expect_equal(V(g)$a1, c(1:10, rep(NA, 10))) - expect_equal(V(g)$a2, c(rep(NA, 10), 11:20)) - - expect_equal(E(g)$weight, c(1:10, 10:1)) - expect_equal(E(g)$b1, c(1:10, rep(NA, 10))) - expect_equal(E(g)$b2, c(rep(NA, 10), 11:20)) -}) - -test_that("disjoint union gives warning for non-unique vertex names", { - g1 <- make_ring(5) - V(g1)$name <- letters[1:5] - g2 <- make_ring(5) - V(g2)$name <- letters[5:9] - - expect_warning(disjoint_union(g1, g2), "Duplicate vertex names in disjoint union") -}) - - -test_that("union of unnamed graphs works", { - g1 <- make_ring(10) - g2 <- make_ring(13) - g1$foo <- "bar" - E(g1)$weight <- 1:10 - E(g2)$weight <- 13:1 - - V(g1)$a1 <- 1:10 - V(g2)$a2 <- 11:23 - - E(g1)$b1 <- letters[1:10] - E(g2)$b2 <- letters[11:23] - - g <- union.igraph(g1, g2) - - expect_equal( - sort(graph_attr_names(g)), - c("circular_1", "circular_2", "foo", "mutual_1", "mutual_2", "name_1", "name_2") - ) - expect_equal( - sort(vertex_attr_names(g)), - c("a1", "a2") - ) - expect_equal( - sort(edge_attr_names(g)), - c("b1", "b2", "weight_1", "weight_2") - ) - - df1 <- as_data_frame(g) - df1 <- df1[order(df1$from, df1$to), c(1, 2, 3, 5, 4, 6)] - df2 <- merge(as_data_frame(g1), as_data_frame(g2), - by = c("from", "to"), all = TRUE - ) - rownames(df1) <- seq_len(nrow(df1)) - colnames(df2) <- c("from", "to", "weight_1", "b1", "weight_2", "b2") - expect_equal(df1, df2) -}) - -test_that("union of named graphs works", { - g1 <- make_ring(10) - g2 <- make_ring(13) - V(g1)$name <- letters[seq_len(vcount(g1))] - V(g2)$name <- letters[seq_len(vcount(g2))] - - g1$foo <- "bar" - E(g1)$weight <- 1:10 - E(g2)$weight <- 13:1 - - V(g1)$a1 <- 1:10 - V(g2)$a2 <- 11:23 - - E(g1)$b1 <- letters[1:10] - E(g2)$b2 <- letters[11:23] - - g <- union.igraph(g1, g2) - - expect_equal( - sort(graph_attr_names(g)), - c("circular_1", "circular_2", "foo", "mutual_1", "mutual_2", "name_1", "name_2") - ) - expect_equal( - sort(vertex_attr_names(g)), - c("a1", "a2", "name") - ) - expect_equal( - sort(edge_attr_names(g)), - c("b1", "b2", "weight_1", "weight_2") - ) - - df1 <- as_data_frame(g, what = "both") - - g.v <- read.table(stringsAsFactors = FALSE, textConnection(" - a1 a2 name -a 1 11 a -b 2 12 b -c 3 13 c -d 4 14 d -e 5 15 e -f 6 16 f -g 7 17 g -h 8 18 h -i 9 19 i -j 10 20 j -k NA 21 k -l NA 22 l -m NA 23 m -")) - expect_equal(df1$vertices, g.v) - - g.e <- read.table(stringsAsFactors = FALSE, textConnection(" - from to weight_1 weight_2 b1 b2 -1 l m NA 2 NA v -2 k l NA 3 NA u -3 j k NA 4 NA t -4 i j 9 5 i s -5 h i 8 6 h r -6 g h 7 7 g q -7 f g 6 8 f p -8 e f 5 9 e o -9 d e 4 10 d n -10 c d 3 11 c m -11 b c 2 12 b l -12 a m NA 1 NA w -13 a j 10 NA j NA -14 a b 1 13 a k -")) - rownames(df1$edges) <- rownames(df1$edges) - expect_equal(df1$edges, g.e) -}) - -test_that("intersection of named graphs works", { - g1 <- make_ring(10) - g2 <- make_ring(13) - V(g1)$name <- letters[V(g1)] - V(g2)$name <- letters[V(g2)] - - g1$foo <- "bar" - E(g1)$weight <- 1:10 - E(g2)$weight <- 13:1 - - V(g1)$a1 <- 1:10 - V(g2)$a2 <- 11:23 - - E(g1)$b1 <- letters[1:10] - E(g2)$b2 <- letters[11:23] - - g <- intersection(g1, g2, keep.all.vertices = FALSE) - - expect_equal( - sort(graph_attr_names(g)), - c("circular_1", "circular_2", "foo", "mutual_1", "mutual_2", "name_1", "name_2") - ) - expect_equal( - sort(vertex_attr_names(g)), - c("a1", "a2", "name") - ) - expect_equal( - sort(edge_attr_names(g)), - c("b1", "b2", "weight_1", "weight_2") - ) - - df1 <- as_data_frame(g, what = "both") - - g.e <- read.table(stringsAsFactors = FALSE, textConnection(" - from to weight_1 weight_2 b1 b2 -1 i j 9 5 i s -2 h i 8 6 h r -3 g h 7 7 g q -4 f g 6 8 f p -5 e f 5 9 e o -6 d e 4 10 d n -7 c d 3 11 c m -8 b c 2 12 b l -9 a b 1 13 a k -")) - rownames(df1$edges) <- rownames(df1$edges) - expect_equal(df1$edges, g.e) - - g.v <- read.table(stringsAsFactors = FALSE, textConnection(" - a1 a2 name -a 1 11 a -b 2 12 b -c 3 13 c -d 4 14 d -e 5 15 e -f 6 16 f -g 7 17 g -h 8 18 h -i 9 19 i -j 10 20 j -")) - expect_equal(df1$vertices, g.v) - - gg <- intersection(g1, g2, keep.all.vertices = TRUE) - - df2 <- as_data_frame(gg, what = "both") - - rownames(df2$edges) <- rownames(df2$edges) - expect_equal(df2$edges, g.e) - - gg.v <- read.table(stringsAsFactors = FALSE, textConnection(" - a1 a2 name -a 1 11 a -b 2 12 b -c 3 13 c -d 4 14 d -e 5 15 e -f 6 16 f -g 7 17 g -h 8 18 h -i 9 19 i -j 10 20 j -k NA 21 k -l NA 22 l -m NA 23 m -")) - expect_equal(df2$vertices, gg.v) -}) - -test_that("difference of named graphs works", { - g1 <- make_ring(10) - g2 <- make_star(11, center = 11, mode = "undirected") - V(g1)$name <- letters[1:10] - V(g2)$name <- letters[1:11] - g <- g1 %u% g2 - - sg <- make_ring(4) - V(sg)$name <- letters[c(1, 2, 3, 11)] - - df1 <- as_data_frame(g - sg, what = "both") - - t1.e <- read.table( - stringsAsFactors = FALSE, - textConnection(" - from to -1 a j -2 b k -3 c d -4 j k -5 i k -6 h k -7 g k -8 f k -9 e k -10 d k -11 d e -12 e f -13 f g -14 g h -15 h i -16 i j -") - ) - rownames(df1$edges) <- rownames(df1$edges) - expect_equal(df1$edges, t1.e) - - expect_equal(df1$vertices, data.frame(row.names = letters[1:11], name = letters[1:11], stringsAsFactors = FALSE)) - - gg <- sg - g - - expect_ecount(gg, 0) - expect_equal(V(gg)$name, letters[c(1:3, 11)]) -}) - -test_that("compose works for named graphs", { - g1 <- graph_from_literal(A - B:D:E, B - C:D, C - D, D - E) - g2 <- graph_from_literal(A - B - E - A) - - V(g1)$bar1 <- seq_len(vcount(g1)) - V(g2)$bar2 <- seq_len(vcount(g2)) - V(g1)$foo <- letters[seq_len(vcount(g1))] - V(g2)$foo <- letters[seq_len(vcount(g2))] - - E(g1)$bar1 <- seq_len(ecount(g1)) - E(g2)$bar2 <- seq_len(ecount(g2)) - E(g1)$foo <- letters[seq_len(ecount(g1))] - E(g2)$foo <- letters[seq_len(ecount(g2))] - - g <- compose(g1, g2) - df <- as_data_frame(g, what = "both") - - df.v <- read.table(stringsAsFactors = FALSE, textConnection(" - bar1 foo_1 foo_2 bar2 name -A 1 a a 1 A -B 2 b b 2 B -D 3 c NA NA D -E 4 d c 3 E -C 5 e NA NA C -")) - expect_equal(df$vertices, df.v) - - df.e <- read.table(stringsAsFactors = FALSE, textConnection(" - from to bar1 foo_1 foo_2 bar2 -1 A B 3 c c 3 -2 A A 3 c b 2 -3 A E 1 a c 3 -4 A A 1 a a 1 -5 B E 1 a b 2 -6 B B 1 a a 1 -7 B D 6 f c 3 -8 A D 6 f b 2 -9 D E 4 d c 3 -10 A D 4 d a 1 -11 D E 2 b b 2 -12 B D 2 b a 1 -13 E E 3 c b 2 -14 B E 3 c a 1 -15 E C 5 e c 3 -16 A C 5 e a 1 -")) - rownames(df$edges) <- rownames(df$edges) - expect_equal(df$edges, df.e) -}) - -test_that("intersection of non-named graphs keeps attributes properly", { - withr::local_seed(42) - - g <- sample_gnp(10, 1 / 2) - g2 <- sample_gnp(10, 1 / 2) - E(g)$weight <- sample(ecount(g)) - E(g2)$weight <- sample(ecount(g2)) - - gi <- intersection(g, g2) - - rn <- function(D) { - rownames(D) <- paste(D[, 1], D[, 2], sep = "-") - D - } - - df <- rn(as_data_frame(g)) - df2 <- rn(as_data_frame(g2)) - dfi <- rn(as_data_frame(gi)) - - expect_equal(df[rownames(dfi), ], dfi[, 1:3], ignore_attr = TRUE) - expect_equal(df2[rownames(dfi), ], dfi[, c(1, 2, 4)], ignore_attr = TRUE) -}) - -test_that("union of non-named graphs keeps attributes properly", { - withr::local_seed(42) - - g <- sample_gnp(10, 1 / 2) - g2 <- sample_gnp(10, 1 / 2) - E(g)$weight <- sample(ecount(g)) - E(g2)$weight <- sample(ecount(g2)) - - gu <- union.igraph(g, g2) - - rn <- function(D) { - rownames(D) <- paste(D[, 1], D[, 2], sep = "-") - D - } - - df <- rn(as_data_frame(g)) - df2 <- rn(as_data_frame(g2)) - dfu <- rn(as_data_frame(gu)) - - expect_equal(dfu[rownames(df), 1:3], df, ignore_attr = TRUE) - expect_equal(dfu[rownames(df2), c(1, 2, 4)], df2, ignore_attr = TRUE) - - expect_equal( - dfu[!rownames(dfu) %in% rownames(df), 3], - rep(NA_real_, ecount(gu) - ecount(g)) - ) - expect_equal( - dfu[!rownames(dfu) %in% rownames(df2), 4], - rep(NA_real_, ecount(gu) - ecount(g2)) - ) -}) diff --git a/tests/testthat/test-vs-es.R b/tests/testthat/test-vs-es.R deleted file mode 100644 index 33909e2caa..0000000000 --- a/tests/testthat/test-vs-es.R +++ /dev/null @@ -1,244 +0,0 @@ -test_that("we can create vertex/edge seqs", { - g <- make_ring(10) - V(g) %&&% expect_true(TRUE) - E(g) %&&% expect_true(TRUE) - - V(g)$name <- letters[1:10] - V(g) %&&% expect_true(TRUE) - E(g) %&&% expect_true(TRUE) - - g <- make_ring(10) - E(g)$name <- LETTERS[1:10] - E(g) %&&% expect_true(TRUE) -}) - -test_that("vs/es keeps names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - vs <- V(g) - - expect_equal(vs$name, names(vs)) - - vs2 <- vs[4:7] - expect_equal(vs2$name, names(vs2)) - - E(g)$name <- LETTERS[1:10] - es <- E(g) - - expect_equal(es$name, names(es)) - - es2 <- es[4:7] - expect_equal(es2$name, names(es2)) -}) - -test_that("vs/es refers to the graph", { - g <- make_ring(10) - vs <- V(g) - es <- E(g) - - expect_identical(get_vs_graph(vs), g) - expect_identical(get_es_graph(es), g) -}) - -test_that("vs/es refers to the original graph", { - g <- g2 <- make_ring(10) - vs <- V(g) - es <- E(g) - - g <- g + 4 - - expect_identical(get_vs_graph(vs), g2) - expect_identical(get_es_graph(es), g2) -}) - -test_that("vs/es references are weak", { - g <- make_ring(10) - vs <- V(g) - es <- E(g) - - rm(g) - gc() - - expect_null(get_vs_graph(vs)) - expect_null(get_es_graph(es)) -}) - -test_that("save/load breaks references", { - g <- make_ring(10) - vs <- V(g) - es <- E(g) - - tmp <- tempfile() - on.exit(try(unlink(tmp))) - - save(vs, es, file = tmp) - rm(vs, es) - gc() - - load(tmp) - expect_null(get_vs_graph(vs)) - expect_null(get_es_graph(es)) -}) - -test_that("vs/es keeps names after graph is deleted", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - vs <- V(g) - - E(g)$name <- LETTERS[1:10] - es <- E(g) - - rm(g) - gc() - - expect_equal(names(vs), letters[1:10]) - - vs2 <- vs[4:7] - expect_equal(names(vs2), letters[4:7]) - - expect_equal(names(es), LETTERS[1:10]) - - es2 <- es[4:7] - expect_equal(names(es2), LETTERS[4:7]) -}) - -test_that("both edge and vertex names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - E(g)$name <- LETTERS[1:10] - - es <- E(g) - expect_equal(as.vector(es), 1:10) - expect_equal(names(es), LETTERS[1:10]) - el <- as_edgelist(g) - expect_equal(attr(es, "vnames"), paste(el[, 1], el[, 2], sep = "|")) - - x1 <- es[LETTERS[4:7]] - x2 <- E(g)[4:7] - expect_equal(as.vector(x1), as.vector(x2)) - expect_equal(names(x1), names(x2)) - expect_equal(attr(x1, "vnames"), attr(x2, "vnames")) - - y1 <- es[c("a|b", "d|e")] - y2 <- E(g)[c(1, 4)] - expect_equal(as.vector(y1), as.vector(y2)) - expect_equal(names(y1), names(y2)) - expect_equal(attr(y1, "vnames"), attr(y2, "vnames")) -}) - -test_that("printing connected vs/es works", { - local_igraph_options(print.id = FALSE) - - g <- make_ring(10) - vs <- V(g) - es <- E(g) - - expect_snapshot({ - vs - es - vs[1:5] - es[1:5] - vs[numeric()] - }) - - expect_snapshot({ - es[numeric()] - }) -}) - -test_that("printing named connected vs/es works", { - local_igraph_options(print.id = FALSE) - - g <- make_ring(10) - V(g)$name <- letters[1:10] - vs <- V(g) - es <- E(g) - - expect_snapshot({ - vs - es - vs[1:5] - es[1:5] - vs[numeric()] - }) - - expect_snapshot({ - es[numeric()] - }) -}) - -test_that("printing unconnected vs/es works", { - local_igraph_options(print.id = FALSE) - - g <- make_ring(10) - vs <- V(g) - es <- E(g) - - rm(g) - gc() - - expect_snapshot({ - vs - es - }) - - g <- make_ring(10) - V(g)$name <- letters[1:10] - vs <- V(g) - es <- E(g) - - rm(g) - gc() - - expect_snapshot({ - vs - es - }) -}) - -test_that("unconnected vs/es can be reused with the same graph", { - g <- make_ring(10) - vs <- V(g) - es <- E(g)[1:5] - - tmp <- tempfile() - on.exit(unlink(tmp)) - save(g, es, vs, file = tmp) - - rm(g, es, vs) - gc() - - load(tmp) - - expect_equal(degree(g, v = vs), rep(2, 10)) - expect_identical_graphs( - delete_edges(g, es), - delete_edges(g, 1:5) - ) -}) - -test_that("indexing without arguments", { - g <- make_ring(10) - - x <- V(g)[] - expect_equal(ignore_attr = TRUE, V(g), x) - - x2 <- V(g)[[]] - v <- set_single_index(V(g)) - - expect_equal(ignore_attr = TRUE, v, x2) -}) - -test_that("vertex indexes are stored as raw numbers", { - g <- make_ring(3, directed = TRUE) - V(g)$id <- V(g) - expect_identical(V(g)$id, as.numeric(1:3)) - expect_error(induced_subgraph(g, 1), NA) -}) - -test_that("edge indexes are stored as raw numbers", { - g <- make_ring(3, directed = TRUE) - E(g)$id <- E(g) - expect_identical(E(g)$id, as.numeric(1:3)) - expect_error(induced_subgraph(g, 1:2), NA) -}) diff --git a/tests/testthat/test-vs-operators.R b/tests/testthat/test-vs-operators.R deleted file mode 100644 index 6fe23414be..0000000000 --- a/tests/testthat/test-vs-operators.R +++ /dev/null @@ -1,609 +0,0 @@ -test_that("c on attached vs", { - g <- make_ring(10) - - vg <- V(g)[1:5] - vg2 <- V(g)[6:10] - expect_equal(ignore_attr = TRUE, c(vg, vg2), V(g)) - expect_equal(get_vs_graph_id(c(vg, vg2)), get_graph_id(g)) - - vg <- V(g) - vg2 <- V(g)[FALSE] - expect_equal(ignore_attr = TRUE, c(vg, vg2), V(g)) - expect_equal(ignore_attr = TRUE, c(vg2, vg), V(g)) - - vg <- V(g)[c(2, 5, 6, 8)] - expect_equal(ignore_attr = TRUE, c(vg, vg), V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)]) -}) - -test_that("c on detached vs", { - g <- make_ring(10) - - vg <- V(g)[1:5] - vg2 <- V(g)[6:10] - - vg3 <- V(g) - vg4 <- V(g)[FALSE] - - vg5 <- V(g)[c(2, 5, 6, 8)] - vg6 <- V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)] - - rm(g) - gc() - - expect_equal(ignore_attr = TRUE, c(vg, vg2), vg3) - expect_equal(ignore_attr = TRUE, c(vg3, vg4), vg3) - expect_equal(ignore_attr = TRUE, c(vg4, vg3), vg3) - expect_equal(ignore_attr = TRUE, c(vg5, vg5), vg6) -}) - -test_that("c on attached vs, names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - - vg <- V(g)[1:5] - vg2 <- V(g)[6:10] - expect_equal(ignore_attr = TRUE, c(vg, vg2), V(g)) - expect_equal(names(c(vg, vg2)), names(V(g))) - - vg <- V(g) - vg2 <- V(g)[FALSE] - expect_equal(ignore_attr = TRUE, c(vg, vg2), V(g)) - expect_equal(names(c(vg, vg2)), names(V(g))) - expect_equal(ignore_attr = TRUE, c(vg2, vg), V(g)) - expect_equal(names(c(vg2, vg)), names(V(g))) - - vg <- V(g)[c(2, 5, 6, 8)] - expect_equal(ignore_attr = TRUE, c(vg, vg), V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)]) - expect_equal(names(c(vg, vg)), names(V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)])) -}) - -test_that("c on detached vs, names", { - g <- make_ring(10) - - vg <- V(g)[1:5] - vg2 <- V(g)[6:10] - - vg3 <- V(g) - vg4 <- V(g)[FALSE] - - vg5 <- V(g)[c(2, 5, 6, 8)] - vg6 <- V(g)[c(2, 5, 6, 8, 2, 5, 6, 8)] - - rm(g) - gc() - - expect_equal(ignore_attr = TRUE, c(vg, vg2), vg3) - expect_equal(names(c(vg, vg2)), names(vg3)) - expect_equal(ignore_attr = TRUE, c(vg3, vg4), vg3) - expect_equal(names(c(vg3, vg4)), names(vg3)) - expect_equal(ignore_attr = TRUE, c(vg4, vg3), vg3) - expect_equal(names(c(vg3, vg4)), names(vg3)) - expect_equal(ignore_attr = TRUE, c(vg5, vg5), vg6) - expect_equal(names(c(vg5, vg5)), names(vg6)) -}) - - - -test_that("union on attached vs", { - g <- make_ring(10) - - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - vu <- union(v1, v2) - expect_equal(ignore_attr = TRUE, vu, V(g)) - - expect_equal(ignore_attr = TRUE, union(V(g)), V(g)) - - v3 <- V(g)[FALSE] - expect_equal(ignore_attr = TRUE, union(V(g), v3), V(g)) - expect_equal(ignore_attr = TRUE, union(v3, V(g), v3), V(g)) - expect_equal(ignore_attr = TRUE, union(v3), v3) - expect_equal(ignore_attr = TRUE, union(v3, v3, v3), v3) - expect_equal(ignore_attr = TRUE, union(v3, v3), v3) -}) - -test_that("union on detached vs", { - g <- make_ring(10) - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - vu <- union(v1, v2) - v3 <- V(g)[FALSE] - - rm(g) - gc() - - expect_equal(ignore_attr = TRUE, vu, vg) - - expect_equal(ignore_attr = TRUE, union(vg), vg) - - expect_equal(ignore_attr = TRUE, union(vg, v3), vg) - expect_equal(ignore_attr = TRUE, union(v3, vg, v3), vg) - expect_equal(ignore_attr = TRUE, union(v3), v3) - expect_equal(ignore_attr = TRUE, union(v3, v3, v3), v3) - expect_equal(ignore_attr = TRUE, union(v3, v3), v3) -}) - -test_that("union on attached vs, names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - vu <- union(v1, v2) - expect_equal(ignore_attr = TRUE, vu, V(g)) - expect_equal(names(vu), names(V(g))) - - expect_equal(ignore_attr = TRUE, union(V(g)), V(g)) - expect_equal(names(union(V(g))), names(V(g))) - - v3 <- V(g)[FALSE] - expect_equal(ignore_attr = TRUE, union(V(g), v3), V(g)) - expect_equal(names(union(V(g), v3)), names(V(g))) - - expect_equal(ignore_attr = TRUE, union(v3, V(g), v3), V(g)) - expect_equal(names(union(v3, V(g), v3)), names(V(g))) - - expect_equal(ignore_attr = TRUE, union(v3), v3) - expect_equal(names(union(v3)), names(v3)) - - expect_equal(ignore_attr = TRUE, union(v3, v3, v3), v3) - expect_equal(names(union(v3, v3, v3)), names(v3)) - - expect_equal(ignore_attr = TRUE, union(v3, v3), v3) - expect_equal(names(union(v3, v3)), names(v3)) -}) - -test_that("union on detached vs, names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - - rm(g) - gc() - - vu <- union(v1, v2) - expect_equal(ignore_attr = TRUE, vu, vg) - expect_equal(names(vu), names(vg)) - - expect_equal(ignore_attr = TRUE, union(vg), vg) - expect_equal(names(union(vg)), names(vg)) - - expect_equal(ignore_attr = TRUE, union(vg, v3), vg) - expect_equal(names(union(vg, v3)), names(vg)) - - expect_equal(ignore_attr = TRUE, union(v3, vg, v3), vg) - expect_equal(names(union(v3, vg, v3)), names(vg)) - - expect_equal(ignore_attr = TRUE, union(v3), v3) - expect_equal(names(union(v3)), names(v3)) - - expect_equal(ignore_attr = TRUE, union(v3, v3, v3), v3) - expect_equal(names(union(v3, v3, v3)), names(v3)) - - expect_equal(ignore_attr = TRUE, union(v3, v3), v3) - expect_equal(names(union(v3, v3)), names(v3)) -}) - - - -test_that("intersection on attached vs", { - g <- make_ring(10) - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - v4 <- V(g)[1:3] - - v12 <- V(g)[6:7] - v13 <- V(g)[FALSE] - v14 <- V(g)[1:3] - v24 <- V(g)[FALSE] - - vi1 <- intersection(v1, v2) - expect_equal(ignore_attr = TRUE, vi1, v12) - - vi2 <- intersection(v1, v3) - expect_equal(ignore_attr = TRUE, vi2, v13) - - vi3 <- intersection(v1, v4) - expect_equal(ignore_attr = TRUE, vi3, v14) - - vi4 <- intersection(v1, vg) - expect_equal(ignore_attr = TRUE, vi4, v1) - - vi5 <- intersection(v2, v4) - expect_equal(ignore_attr = TRUE, vi5, v24) - - vi6 <- intersection(v3, vg) - expect_equal(ignore_attr = TRUE, vi6, v3) -}) - -test_that("intersection on detached vs", { - g <- make_ring(10) - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - v4 <- V(g)[1:3] - - v12 <- V(g)[6:7] - v13 <- V(g)[FALSE] - v14 <- V(g)[1:3] - v24 <- V(g)[FALSE] - - rm(g) - gc() - - vi1 <- intersection(v1, v2) - expect_equal(ignore_attr = TRUE, vi1, v12) - - vi2 <- intersection(v1, v3) - expect_equal(ignore_attr = TRUE, vi2, v13) - - vi3 <- intersection(v1, v4) - expect_equal(ignore_attr = TRUE, vi3, v14) - - vi4 <- intersection(v1, vg) - expect_equal(ignore_attr = TRUE, vi4, v1) - - vi5 <- intersection(v2, v4) - expect_equal(ignore_attr = TRUE, vi5, v24) - - vi6 <- intersection(v3, vg) - expect_equal(ignore_attr = TRUE, vi6, v3) -}) - -test_that("intersection on attached vs, names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - v4 <- V(g)[1:3] - - v12 <- V(g)[6:7] - v13 <- V(g)[FALSE] - v14 <- V(g)[1:3] - v24 <- V(g)[FALSE] - - vi1 <- intersection(v1, v2) - expect_equal(ignore_attr = TRUE, vi1, v12) - expect_equal(names(vi1), names(v12)) - - vi2 <- intersection(v1, v3) - expect_equal(ignore_attr = TRUE, vi2, v13) - expect_equal(names(vi2), names(v13)) - - vi3 <- intersection(v1, v4) - expect_equal(ignore_attr = TRUE, vi3, v14) - expect_equal(names(vi3), names(v14)) - - vi4 <- intersection(v1, vg) - expect_equal(ignore_attr = TRUE, vi4, v1) - expect_equal(names(vi4), names(v1)) - - vi5 <- intersection(v2, v4) - expect_equal(ignore_attr = TRUE, vi5, v24) - expect_equal(names(vi5), names(v24)) - - vi6 <- intersection(v3, vg) - expect_equal(ignore_attr = TRUE, vi6, v3) - expect_equal(names(vi6), names(v3)) -}) - -test_that("intersection on detached vs, names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - v4 <- V(g)[1:3] - - v12 <- V(g)[6:7] - v13 <- V(g)[FALSE] - v14 <- V(g)[1:3] - v24 <- V(g)[FALSE] - - rm(g) - gc() - - vi1 <- intersection(v1, v2) - expect_equal(ignore_attr = TRUE, vi1, v12) - expect_equal(names(vi1), names(v12)) - - vi2 <- intersection(v1, v3) - expect_equal(ignore_attr = TRUE, vi2, v13) - expect_equal(names(vi2), names(v13)) - - vi3 <- intersection(v1, v4) - expect_equal(ignore_attr = TRUE, vi3, v14) - expect_equal(names(vi3), names(v14)) - - vi4 <- intersection(v1, vg) - expect_equal(ignore_attr = TRUE, vi4, v1) - expect_equal(names(vi4), names(v1)) - - vi5 <- intersection(v2, v4) - expect_equal(ignore_attr = TRUE, vi5, v24) - expect_equal(names(vi5), names(v24)) - - vi6 <- intersection(v3, vg) - expect_equal(ignore_attr = TRUE, vi6, v3) - expect_equal(names(vi6), names(v3)) -}) - - - -test_that("difference on attached vs", { - g <- make_ring(10) - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - v4 <- V(g)[1:3] - - vr1 <- V(g)[8:10] - vr2 <- V(g) - vr3 <- V(g)[1:5] - vr4 <- V(g)[4:7] - vr5 <- V(g)[FALSE] - vr6 <- V(g)[FALSE] - - vd1 <- difference(vg, v1) - vd2 <- difference(vg, v3) - vd3 <- difference(v1, v2) - vd4 <- difference(v1, v4) - vd5 <- difference(v3, v3) - vd6 <- difference(v3, v4) - - expect_equal(ignore_attr = TRUE, vd1, vr1) - expect_equal(ignore_attr = TRUE, vd2, vr2) - expect_equal(ignore_attr = TRUE, vd3, vr3) - expect_equal(ignore_attr = TRUE, vd4, vr4) - expect_equal(ignore_attr = TRUE, vd5, vr5) - expect_equal(ignore_attr = TRUE, vd6, vr6) -}) - -test_that("difference on detached vs", { - g <- make_ring(10) - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - v4 <- V(g)[1:3] - - vr1 <- V(g)[8:10] - vr2 <- V(g) - vr3 <- V(g)[1:5] - vr4 <- V(g)[4:7] - vr5 <- V(g)[FALSE] - vr6 <- V(g)[FALSE] - - rm(g) - gc() - - vd1 <- difference(vg, v1) - vd2 <- difference(vg, v3) - vd3 <- difference(v1, v2) - vd4 <- difference(v1, v4) - vd5 <- difference(v3, v3) - vd6 <- difference(v3, v4) - - expect_equal(ignore_attr = TRUE, vd1, vr1) - expect_equal(ignore_attr = TRUE, vd2, vr2) - expect_equal(ignore_attr = TRUE, vd3, vr3) - expect_equal(ignore_attr = TRUE, vd4, vr4) - expect_equal(ignore_attr = TRUE, vd5, vr5) - expect_equal(ignore_attr = TRUE, vd6, vr6) -}) - -test_that("difference on attached vs, names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - v4 <- V(g)[1:3] - - vr1 <- V(g)[8:10] - vr2 <- V(g) - vr3 <- V(g)[1:5] - vr4 <- V(g)[4:7] - vr5 <- V(g)[FALSE] - vr6 <- V(g)[FALSE] - - vd1 <- difference(vg, v1) - vd2 <- difference(vg, v3) - vd3 <- difference(v1, v2) - vd4 <- difference(v1, v4) - vd5 <- difference(v3, v3) - vd6 <- difference(v3, v4) - - expect_equal(ignore_attr = TRUE, vd1, vr1) - expect_equal(names(vd1), names(vr1)) - - expect_equal(ignore_attr = TRUE, vd2, vr2) - expect_equal(names(vd2), names(vr2)) - - expect_equal(ignore_attr = TRUE, vd3, vr3) - expect_equal(names(vd3), names(vr3)) - - expect_equal(ignore_attr = TRUE, vd4, vr4) - expect_equal(names(vd4), names(vr4)) - - expect_equal(ignore_attr = TRUE, vd5, vr5) - expect_equal(names(vd5), names(vr5)) - - expect_equal(ignore_attr = TRUE, vd6, vr6) - expect_equal(names(vd6), names(vr6)) -}) - -test_that("difference on detached vs, names", { - g <- make_ring(10) - V(g)$name <- letters[1:10] - - vg <- V(g) - v1 <- V(g)[1:7] - v2 <- V(g)[6:10] - v3 <- V(g)[FALSE] - v4 <- V(g)[1:3] - - vr1 <- V(g)[8:10] - vr2 <- V(g) - vr3 <- V(g)[1:5] - vr4 <- V(g)[4:7] - vr5 <- V(g)[FALSE] - vr6 <- V(g)[FALSE] - - rm(g) - gc() - - vd1 <- difference(vg, v1) - vd2 <- difference(vg, v3) - vd3 <- difference(v1, v2) - vd4 <- difference(v1, v4) - vd5 <- difference(v3, v3) - vd6 <- difference(v3, v4) - - expect_equal(ignore_attr = TRUE, vd1, vr1) - expect_equal(names(vd1), names(vr1)) - - expect_equal(ignore_attr = TRUE, vd2, vr2) - expect_equal(names(vd2), names(vr2)) - - expect_equal(ignore_attr = TRUE, vd3, vr3) - expect_equal(names(vd3), names(vr3)) - - expect_equal(ignore_attr = TRUE, vd4, vr4) - expect_equal(names(vd4), names(vr4)) - - expect_equal(ignore_attr = TRUE, vd5, vr5) - expect_equal(names(vd5), names(vr5)) - - expect_equal(ignore_attr = TRUE, vd6, vr6) - expect_equal(names(vd6), names(vr6)) -}) - - - -test_that("rev on attached vs", { - for (i in 1:10) { - g <- make_ring(10) - idx <- seq_len(i) - vg <- V(g)[idx] - vgr <- V(g)[rev(idx)] - vg2 <- rev(vg) - expect_equal(ignore_attr = TRUE, vg2, vgr) - } -}) - -test_that("rev on detached vs", { - for (i in 1:10) { - g <- make_ring(10) - idx <- seq_len(i) - vg <- V(g)[idx] - vgr <- V(g)[rev(idx)] - rm(g) - gc() - vg2 <- rev(vg) - expect_equal(ignore_attr = TRUE, vg2, vgr) - } -}) - -test_that("rev on attached vs, names", { - for (i in 1:10) { - g <- make_ring(10) - V(g)$name <- letters[1:10] - idx <- seq_len(i) - vg <- V(g)[idx] - vgr <- V(g)[rev(idx)] - vg2 <- rev(vg) - expect_equal(ignore_attr = TRUE, vg2, vgr) - expect_equal(names(vg2), names(vgr)) - } -}) - -test_that("rev on detached vs, names", { - for (i in 1:10) { - g <- make_ring(10) - V(g)$name <- letters[1:10] - idx <- seq_len(i) - vg <- V(g)[idx] - vgr <- V(g)[rev(idx)] - rm(g) - gc() - vg2 <- rev(vg) - expect_equal(ignore_attr = TRUE, vg2, vgr) - expect_equal(names(vg2), names(vgr)) - } -}) - -unique_tests <- list( - list(1:5, 1:5), - list(c(1, 1, 2:5), 1:5), - list(c(1, 1, 1, 1), 1), - list(c(1, 2, 2, 2), 1:2), - list(c(2, 2, 1, 1), 2:1), - list(c(1, 2, 1, 2), 1:2), - list(c(), c()) -) - -test_that("unique on attached vs", { - sapply(unique_tests, function(d) { - g <- make_ring(10) - vg <- unique(V(g)[d[[1]]]) - vr <- V(g)[d[[2]]] - expect_equal(ignore_attr = TRUE, vg, vr) - }) -}) - -test_that("unique on detached vs", { - sapply(unique_tests, function(d) { - g <- make_ring(10) - vg <- V(g)[d[[1]]] - vr <- V(g)[d[[2]]] - rm(g) - gc() - vg <- unique(vg) - expect_equal(ignore_attr = TRUE, vg, vr) - }) -}) - -test_that("unique on attached vs, names", { - sapply(unique_tests, function(d) { - g <- make_ring(10) - V(g)$name <- letters[1:10] - vg <- unique(V(g)[d[[1]]]) - vr <- V(g)[d[[2]]] - expect_equal(ignore_attr = TRUE, vg, vr) - }) -}) - -test_that("unique on detached vs, names", { - sapply(unique_tests, function(d) { - g <- make_ring(10) - V(g)$name <- letters[1:10] - vg <- V(g)[d[[1]]] - vr <- V(g)[d[[2]]] - rm(g) - gc() - vg <- unique(vg) - expect_equal(ignore_attr = TRUE, vg, vr) - }) -})