diff --git a/R/topology.R b/R/topology.R index 917acb6180..a166925b0c 100644 --- a/R/topology.R +++ b/R/topology.R @@ -239,10 +239,10 @@ graph.subisomorphic.lad <- function(pattern, target, domains = NULL, all.maps <- as.logical(all.maps) if (!is.null(domains)) { if (!is.list(domains)) { - stop("`domains' must be a list of vertex vectors from `target'") + cli::cli_abort("{.arg domains} must be a list of vertex vectors from {.arg target}.") } if (length(domains) != vcount(pattern)) { - stop("`domains' length and `pattern' number of vertices must match") + cli::cli_abort("{.arg domains} length and {.arg pattern} number of vertices must match.") } domains <- lapply(domains, function(x) as_igraph_vs(target, x) - 1) } diff --git a/tests/testthat/_snaps/bug-501-rectangles/rectangle-edges.svg b/tests/testthat/_snaps/plot/rectangle-edges.svg similarity index 100% rename from tests/testthat/_snaps/bug-501-rectangles/rectangle-edges.svg rename to tests/testthat/_snaps/plot/rectangle-edges.svg diff --git a/tests/testthat/test-bug-1032819.R b/tests/testthat/test-bug-1032819.R deleted file mode 100644 index 04c8548f82..0000000000 --- a/tests/testthat/test-bug-1032819.R +++ /dev/null @@ -1,8 +0,0 @@ -test_that("VF2 isomorphism considers colors", { - g <- make_full_graph(3) - path <- make_ring(3, circular = F) - V(g)$color <- c(1, 1, 2) - V(path)$color <- c(1, 2, 1) - n <- count_subgraph_isomorphisms(path, g, method = "vf2") - expect_equal(n, 2) -}) diff --git a/tests/testthat/test-bug-154.R b/tests/testthat/test-bug-154.R deleted file mode 100644 index b8deee4032..0000000000 --- a/tests/testthat/test-bug-154.R +++ /dev/null @@ -1,6 +0,0 @@ -test_that("graph.get.subisomorphisms.vf2() works even if the graph has a vertex attribute named x", { - g <- make_full_graph(4) - V(g)$x <- 1:4 - subs <- graph.get.subisomorphisms.vf2(g, make_ring(4)) - expect_equal(length(subs), 24) -}) diff --git a/tests/testthat/test-bug-501-rectangles.R b/tests/testthat/test-bug-501-rectangles.R deleted file mode 100644 index 0b23738b43..0000000000 --- a/tests/testthat/test-bug-501-rectangles.R +++ /dev/null @@ -1,19 +0,0 @@ -test_that("Edges stop at outside of rectangle node", { - skip_if_not_installed("vdiffr") - - rectangle_edges <- function() { - g <- make_graph(c(1, 2, 1, 4, 2, 1, 2, 5, 2, 3, 4, 1, 5, 2, 3, 2)) - layout <- cbind( - c(-2.01, -1.16, -1.24, -2.74, -0.13), - c(1.27, 2.1, 3.14, 0.56, 2.01) - ) - plot(g, - vertex.size = 30, - vertex.color = rgb(0.1, 0.7, 0.8, 0.1), - vertex.shape = "rectangle", - layout = layout - ) - } - - vdiffr::expect_doppelganger("rectangle-edges", rectangle_edges) -}) diff --git a/tests/testthat/test-canonical.permutation.R b/tests/testthat/test-canonical.permutation.R deleted file mode 100644 index 5edfe109fa..0000000000 --- a/tests/testthat/test-canonical.permutation.R +++ /dev/null @@ -1,18 +0,0 @@ -test_that("canonical_permutation works", { - g1 <- sample_gnm(10, 20) - cp1 <- canonical_permutation(g1) - cf1 <- permute(g1, cp1$labeling) - - ## Do the same with a random permutation of it - g2 <- permute(g1, sample(vcount(g1))) - cp2 <- canonical_permutation(g2) - cf2 <- permute(g2, cp2$labeling) - - ## Check that they are the same - el1 <- as_edgelist(cf1) - el2 <- as_edgelist(cf2) - el1 <- el1[order(el1[, 1], el1[, 2]), ] - el2 <- el2[order(el2[, 1], el2[, 2]), ] - - expect_equal(el1, el2) -}) diff --git a/tests/testthat/test-graph.isoclass.R b/tests/testthat/test-graph.isoclass.R deleted file mode 100644 index f28a18af3d..0000000000 --- a/tests/testthat/test-graph.isoclass.R +++ /dev/null @@ -1,11 +0,0 @@ -test_that("isomorphism_class works", { - g1 <- graph_from_isomorphism_class(3, 10) - g2 <- graph_from_isomorphism_class(3, 11) - expect_equal(isomorphism_class(g1), 10) - expect_equal(isomorphism_class(g2), 11) - - g1 <- add_vertices(g1, 3) - expect_equal(graph.isoclass.subgraph(g1, 1:3), 10) - expect_equal(graph.isoclass.subgraph(g1 %du% g2, 1:3), 10) - expect_equal(graph.isoclass.subgraph(g1 %du% g2, 7:9), 11) -}) diff --git a/tests/testthat/test-graph.subisomorphic.lad.R b/tests/testthat/test-graph.subisomorphic.lad.R deleted file mode 100644 index 964c518022..0000000000 --- a/tests/testthat/test-graph.subisomorphic.lad.R +++ /dev/null @@ -1,56 +0,0 @@ -test_that("graph.subisomorphic, method = 'lad' works", { - pattern <- graph_from_literal( - 1:2:3:4:5, - 1 - 2:5, 2 - 1:5:3, 3 - 2:4, 4 - 3:5, 5 - 4:2:1 - ) - target <- graph_from_literal( - 1:2:3:4:5:6:7:8:9, - 1 - 2:5:7, 2 - 1:5:3, 3 - 2:4, 4 - 3:5:6:8:9, - 5 - 1:2:4:6:7, 6 - 7:5:4:9, 7 - 1:5:6, - 8 - 4:9, 9 - 6:4:8 - ) - domains <- list( - `1` = c(1, 3, 9), `2` = c(5, 6, 7, 8), `3` = c(2, 4, 6, 7, 8, 9), - `4` = c(1, 3, 9), `5` = c(2, 4, 8, 9) - ) - i1 <- subgraph_isomorphic(pattern, target, method = "lad") - i2 <- subgraph_isomorphic(pattern, target, induced = TRUE, method = "lad") - i3 <- subgraph_isomorphic(pattern, target, - domains = domains, - method = "lad" - ) - - expect_true(i1) - expect_true(i2) - expect_true(i3) -}) - -test_that("LAD stress test", { - local_rng_version("3.5.0") - withr::local_seed(42) - N <- 100 - - for (i in 1:N) { - target <- sample_gnp(20, .5) - pn <- sample(4:18, 1) - pattern <- induced_subgraph(target, sample(vcount(target), pn)) - iso <- subgraph_isomorphic(pattern, target, - induced = TRUE, - method = "lad" - ) - expect_true(iso) - } - - withr::local_seed(42) - - for (i in 1:N) { - target <- sample_gnp(20, 1 / 20) - pn <- sample(5:18, 1) - pattern <- sample_gnp(pn, .6) - iso <- subgraph_isomorphic(pattern, target, - induced = TRUE, - method = "lad" - ) - expect_false(iso) - } -}) diff --git a/tests/testthat/test-graph.subisomorphic.vf2.R b/tests/testthat/test-graph.subisomorphic.vf2.R deleted file mode 100644 index 3b41375134..0000000000 --- a/tests/testthat/test-graph.subisomorphic.vf2.R +++ /dev/null @@ -1,18 +0,0 @@ -test_that("graph.subisomorphic.vf2 works", { - withr::local_seed(42) - - g1 <- sample_gnp(20, 6 / 20) - g2 <- sample_gnp(20, 6 / 20) - g <- g1 %du% g2 - - ig1 <- graph.subisomorphic.vf2(g, g1) - ig2 <- graph.subisomorphic.vf2(g, g2) - - expect_true(ig1$iso) - expect_equal(ig1$map12, c(1:vcount(g1), rep(0, vcount(g2)))) - expect_equal(ig1$map21, 1:vcount(g1)) - - expect_true(ig2$iso) - expect_equal(ig2$map12, c(rep(0, vcount(g1)), 1:vcount(g2))) - expect_equal(ig2$map21, 1:vcount(g2) + vcount(g1)) -}) diff --git a/tests/testthat/test-isomorphism.R b/tests/testthat/test-isomorphism.R deleted file mode 100644 index 4fb497ccfa..0000000000 --- a/tests/testthat/test-isomorphism.R +++ /dev/null @@ -1,148 +0,0 @@ -test_that("isomorphic", { - g <- graph_from_literal(A - B - C - A) - expect_isomorphic(g, g) - expect_isomorphic(g, g, method = "direct") - expect_isomorphic(g, g, method = "vf2") - expect_isomorphic(g, g, method = "bliss") - - g2 <- graph_from_literal(A - B - C) - expect_not_isomorphic(g, g2) - expect_not_isomorphic(g, g2, method = "direct") - expect_not_isomorphic(g, g2, method = "vf2") - expect_not_isomorphic(g, g2, method = "bliss") -}) - -test_that("subgraph_isomorphic", { - g <- graph_from_literal(A - B - C - D - E - A) - g2 <- graph_from_literal(A - B - C - D) - - expect_true(subgraph_isomorphic(g2, g)) - expect_true(subgraph_isomorphic(g2, g, method = "vf2")) - expect_true(subgraph_isomorphic(g2, g, method = "lad")) - - g3 <- graph_from_literal(A - B - C - A) - expect_false(subgraph_isomorphic(g3, g)) - expect_false(subgraph_isomorphic(g3, g, method = "vf2")) - expect_false(subgraph_isomorphic(g3, g, method = "lad")) -}) - -test_that("count_isomorphisms", { - g <- graph_from_literal(A - B - C - D - A) - expect_equal(count_isomorphisms(g, g), 8) - - g2 <- graph_from_literal(A - B - C - A) - expect_equal(count_isomorphisms(g, g2), 0) -}) - -test_that("count_isomorphisms_with_colors", { - expect_equal( - count_isomorphisms(make_ring(3), - make_ring(3), - edge.color1 = c(2, 2, 2), - edge.color2 = c(2, 2, 2), - vertex.color1 = c(3, 3, 3), - vertex.color2 = c(3, 3, 3), - method = "vf2" - ), - 6 - ) - - expect_equal( - count_isomorphisms(make_ring(3), - make_ring(3), - edge.color1 = c(2, 2, 2), - edge.color2 = c(2, 2, 2), - vertex.color1 = c(1, 2, 3), - vertex.color2 = c(1, 2, 3), - method = "vf2" - ), - 1 - ) - - expect_equal( - count_isomorphisms(make_ring(3), - make_ring(3), - edge.color1 = c(2, 2, 3), - edge.color2 = c(3, 2, 2), - vertex.color1 = c(3, 3, 3), - vertex.color2 = c(3, 3, 3), - method = "vf2" - ), - 2 - ) -}) - -test_that("count_subgraph_isomorphisms", { - g <- graph_from_literal(A - B - C - D - A) - g2 <- graph_from_literal(A - B - C - D) - - expect_equal(count_subgraph_isomorphisms(g2, g, method = "lad"), 8) - expect_equal(count_subgraph_isomorphisms(g2, g, method = "vf2"), 8) - - g3 <- graph_from_literal(A - B - C - A) - expect_equal(count_subgraph_isomorphisms(g3, g, method = "lad"), 0) - expect_equal(count_subgraph_isomorphisms(g3, g, method = "vf2"), 0) -}) - -test_that("isomorphisms", { - g <- graph_from_literal(A - B - C - D - A) - g2 <- graph_from_literal(W - X - Y - Z - W) - - res <- list( - V(g2)[1, 2, 3, 4], - V(g2)[1, 4, 3, 2], - V(g2)[2, 1, 4, 3], - V(g2)[2, 3, 4, 1], - V(g2)[3, 2, 1, 4], - V(g2)[3, 4, 1, 2], - V(g2)[4, 1, 2, 3], - V(g2)[4, 3, 2, 1] - ) - - expect_equal(ignore_attr = TRUE, isomorphisms(g, g2), res) - - g3 <- graph_from_literal(X - Y - Z - X) - expect_equal(isomorphisms(g, g3), list()) -}) - -test_that("subgraph_isomorphisms, lad", { - g <- graph_from_literal(A - B - C - D - A) - g2 <- graph_from_literal(Z - X - Y) - - res <- list( - V(g)[1, 4, 3], - V(g)[1, 2, 3], - V(g)[2, 1, 4], - V(g)[2, 3, 4], - V(g)[3, 2, 1], - V(g)[3, 4, 1], - V(g)[4, 3, 2], - V(g)[4, 1, 2] - ) - - expect_equal(ignore_attr = TRUE, subgraph_isomorphisms(g2, g, method = "lad"), res) - - g3 <- graph_from_literal(X - Y - Z - X) - expect_equal(subgraph_isomorphisms(g3, g, method = "lad"), list()) -}) - -test_that("subgraph_isomorphisms, vf2", { - g <- graph_from_literal(A - B - C - D - A) - g2 <- graph_from_literal(Z - X - Y) - - res <- list( - V(g)[1, 2, 3], - V(g)[1, 4, 3], - V(g)[2, 1, 4], - V(g)[2, 3, 4], - V(g)[3, 2, 1], - V(g)[3, 4, 1], - V(g)[4, 1, 2], - V(g)[4, 3, 2] - ) - - expect_equal(ignore_attr = TRUE, subgraph_isomorphisms(g2, g, method = "vf2"), res) - - g3 <- graph_from_literal(X - Y - Z - X) - expect_equal(subgraph_isomorphisms(g3, g, method = "vf2"), list()) -}) diff --git a/tests/testthat/test-notable.R b/tests/testthat/test-notable.R deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index 5246e4a522..0874cc09d3 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -109,3 +109,23 @@ test_that("label colors are correct when loops are present", { } ) }) + +test_that("Edges stop at outside of rectangle node", { + skip_if_not_installed("vdiffr") + + rectangle_edges <- function() { + g <- make_graph(c(1, 2, 1, 4, 2, 1, 2, 5, 2, 3, 4, 1, 5, 2, 3, 2)) + layout <- cbind( + c(-2.01, -1.16, -1.24, -2.74, -0.13), + c(1.27, 2.1, 3.14, 0.56, 2.01) + ) + plot(g, + vertex.size = 30, + vertex.color = rgb(0.1, 0.7, 0.8, 0.1), + vertex.shape = "rectangle", + layout = layout + ) + } + + vdiffr::expect_doppelganger("rectangle-edges", rectangle_edges) +}) diff --git a/tests/testthat/test-topology.R b/tests/testthat/test-topology.R index 25a96b3b81..527a8de74a 100644 --- a/tests/testthat/test-topology.R +++ b/tests/testthat/test-topology.R @@ -1,9 +1,9 @@ test_that("automorphisms works", { - g <- make_ring(10) - expect_equal(count_automorphisms(g)$group_size, "20") + ring <- make_ring(10) + expect_equal(count_automorphisms(ring)$group_size, "20") - g <- make_full_graph(4) - expect_equal(count_automorphisms(g)$group_size, "24") + full <- make_full_graph(4) + expect_equal(count_automorphisms(full)$group_size, "24") }) @@ -18,51 +18,41 @@ test_that("automorphisms works with colored graphs", { test_that("automorphism_group works", { - g <- make_ring(10) - aut <- lapply(automorphism_group(g), as.vector) - aut <- aut[order(sapply(aut, "[[", 1))] - expect_equal(aut, list(c(1, 10:2), c(2:10, 1))) + ring <- make_ring(10) + aut_ring <- lapply(automorphism_group(ring), as.vector) + aut_ring <- aut_ring[order(sapply(aut_ring, "[[", 1))] + expect_equal(aut_ring, list(c(1, 10:2), c(2:10, 1))) - g <- make_full_graph(4) - aut <- lapply(automorphism_group(g), as.vector) - aut <- aut[order(sapply(aut, "[[", 1))] - expect_equal(aut, list(c(1, 2, 4, 3), c(1, 3, 2, 4), c(2, 1, 3, 4))) + full <- make_full_graph(4) + aut_full <- lapply(automorphism_group(full), as.vector) + aut_full <- aut_full[order(sapply(aut_full, "[[", 1))] + expect_equal(aut_full, list(c(1, 2, 4, 3), c(1, 3, 2, 4), c(2, 1, 3, 4))) }) test_that("automorphism_group works with colored graphs", { - g <- make_full_graph(4) - aut <- lapply(automorphism_group(g, colors = c(1, 2, 1, 2)), as.vector) - aut <- aut[order(sapply(aut, "[[", 1))] - expect_equal(aut, list(c(1, 4, 3, 2), c(3, 2, 1, 4))) + full <- make_full_graph(4) + aut_full <- lapply(automorphism_group(full, colors = c(1, 2, 1, 2)), as.vector) + aut_full <- aut_full[order(sapply(aut_full, "[[", 1))] + expect_equal(aut_full, list(c(1, 4, 3, 2), c(3, 2, 1, 4))) - V(g)$color <- c(1, 2, 1, 2) - aut <- lapply(automorphism_group(g), as.vector) - aut <- aut[order(sapply(aut, "[[", 1))] - expect_equal(aut, list(c(1, 4, 3, 2), c(3, 2, 1, 4))) -}) - -test_that("isomorphisms works", { - motif <- make_empty_graph(directed = FALSE) + - vertices("D1", "D2", type = c("type1", "type1")) + - edges("D1", "D2", type = c("type2")) - subgraph_isomorphisms( - target = motif, pattern = motif, method = "vf2", - vertex.color1 = 2:1, vertex.color2 = 1:2 - ) + V(full)$color <- c(1, 2, 1, 2) + aut_full_col <- lapply(automorphism_group(full), as.vector) + aut_full_col <- aut_full_col[order(sapply(aut_full_col, "[[", 1))] + expect_equal(aut_full_col, list(c(1, 4, 3, 2), c(3, 2, 1, 4))) }) test_that("isomorphisms() works", { motif <- make_empty_graph(directed = FALSE) + vertices("D1", "D2", type = c("type1", "type1")) + edges("D1", "D2", type = c("type2")) - out <- isomorphisms( + motif_iso <- isomorphisms( motif, motif, method = "vf2", vertex.color1 = 2:1, vertex.color2 = 1:2 ) - expect_length(out, 1) - expect_equal(as.numeric(out[[1]]), 2:1) + expect_length(motif_iso, 1) + expect_equal(as.numeric(motif_iso[[1]]), 2:1) }) test_that("subgraph_isomorphisms works", { @@ -76,3 +66,275 @@ test_that("subgraph_isomorphisms works", { expect_length(out, 1) expect_equal(as.numeric(out[[1]]), 2:1) }) + +test_that("graph.get.subisomorphisms.vf2() works even if the graph has a vertex attribute named x", { + g <- make_full_graph(4) + V(g)$x <- 1:4 + subs <- graph.get.subisomorphisms.vf2(g, make_ring(4)) + expect_equal(length(subs), 24) +}) + +test_that("VF2 isomorphism considers colors", { + full <- make_full_graph(3) + path <- make_ring(3, circular = FALSE) + V(full)$color <- c(1, 1, 2) + V(path)$color <- c(1, 2, 1) + n <- count_subgraph_isomorphisms(path, full, method = "vf2") + expect_equal(n, 2) +}) + +test_that("canonical_permutation works", { + g1 <- sample_gnm(10, 20) + cp1 <- canonical_permutation(g1) + cf1 <- permute(g1, cp1$labeling) + + ## Do the same with a random permutation of it + g2 <- permute(g1, sample(vcount(g1))) + cp2 <- canonical_permutation(g2) + cf2 <- permute(g2, cp2$labeling) + + ## Check that they are the same + el1 <- as_edgelist(cf1) + el2 <- as_edgelist(cf2) + el1 <- el1[order(el1[, 1], el1[, 2]), ] + el2 <- el2[order(el2[, 1], el2[, 2]), ] + + expect_equal(el1, el2) +}) + +test_that("isomorphism_class works", { + g_iso10 <- graph_from_isomorphism_class(3, 10) + g_iso11 <- graph_from_isomorphism_class(3, 11) + expect_equal(isomorphism_class(g_iso10), 10) + expect_equal(isomorphism_class(g_iso11), 11) + + g_iso10_1 <- add_vertices(g_iso10, 3) + expect_equal(graph.isoclass.subgraph(g_iso10_1, 1:3), 10) + expect_equal(graph.isoclass.subgraph(g_iso10_1 %du% g_iso11, 1:3), 10) + expect_equal(graph.isoclass.subgraph(g_iso10_1 %du% g_iso11, 7:9), 11) +}) + +test_that("graph.subisomorphic, method = 'lad' works", { + pattern <- graph_from_literal( + 1:2:3:4:5, + 1 - 2:5, 2 - 1:5:3, 3 - 2:4, 4 - 3:5, 5 - 4:2:1 + ) + target <- graph_from_literal( + 1:2:3:4:5:6:7:8:9, + 1 - 2:5:7, 2 - 1:5:3, 3 - 2:4, 4 - 3:5:6:8:9, + 5 - 1:2:4:6:7, 6 - 7:5:4:9, 7 - 1:5:6, + 8 - 4:9, 9 - 6:4:8 + ) + domains <- list( + `1` = c(1, 3, 9), `2` = c(5, 6, 7, 8), `3` = c(2, 4, 6, 7, 8, 9), + `4` = c(1, 3, 9), `5` = c(2, 4, 8, 9) + ) + sub_iso1 <- subgraph_isomorphic(pattern, target, method = "lad") + sub_iso2 <- subgraph_isomorphic(pattern, target, induced = TRUE, method = "lad") + sub_iso3 <- subgraph_isomorphic(pattern, target, + domains = domains, + method = "lad" + ) + + expect_true(sub_iso1) + expect_true(sub_iso2) + expect_true(sub_iso3) +}) + +test_that("LAD stress test", { + local_rng_version("3.5.0") + withr::local_seed(42) + N <- 100 + + for (i in 1:N) { + target <- sample_gnp(20, .5) + pn <- sample(4:18, 1) + pattern <- induced_subgraph(target, sample(vcount(target), pn)) + iso <- subgraph_isomorphic(pattern, target, + induced = TRUE, + method = "lad" + ) + expect_true(iso) + } + + withr::local_seed(42) + + for (i in 1:N) { + target <- sample_gnp(20, 1 / 20) + pn <- sample(5:18, 1) + pattern <- sample_gnp(pn, .6) + iso <- subgraph_isomorphic(pattern, target, + induced = TRUE, + method = "lad" + ) + expect_false(iso) + } +}) + +test_that("graph.subisomorphic.vf2 works", { + withr::local_seed(42) + + gnp1 <- sample_gnp(20, 6 / 20) + gnp2 <- sample_gnp(20, 6 / 20) + g <- gnp1 %du% gnp2 + + g1_iso <- graph.subisomorphic.vf2(g, gnp1) + + expect_true(g1_iso$iso) + expect_equal(g1_iso$map12, c(1:vcount(gnp1), rep(0, vcount(gnp2)))) + expect_equal(g1_iso$map21, 1:vcount(gnp1)) + + g2_iso <- graph.subisomorphic.vf2(g, gnp2) + expect_true(g2_iso$iso) + expect_equal(g2_iso$map12, c(rep(0, vcount(gnp1)), 1:vcount(gnp2))) + expect_equal(g2_iso$map21, 1:vcount(gnp2) + vcount(gnp1)) +}) + +test_that("isomorphic", { + g1 <- graph_from_literal(A - B - C - A) + expect_isomorphic(g1, g1) + expect_isomorphic(g1, g1, method = "direct") + expect_isomorphic(g1, g1, method = "vf2") + expect_isomorphic(g1, g1, method = "bliss") + + g2 <- graph_from_literal(A - B - C) + expect_not_isomorphic(g1, g2) + expect_not_isomorphic(g1, g2, method = "direct") + expect_not_isomorphic(g1, g2, method = "vf2") + expect_not_isomorphic(g1, g2, method = "bliss") +}) + +test_that("subgraph_isomorphic", { + g1 <- graph_from_literal(A - B - C - D - E - A) + g2 <- graph_from_literal(A - B - C - D) + + expect_true(subgraph_isomorphic(g2, g1)) + expect_true(subgraph_isomorphic(g2, g1, method = "vf2")) + expect_true(subgraph_isomorphic(g2, g1, method = "lad")) + + g3 <- graph_from_literal(A - B - C - A) + expect_false(subgraph_isomorphic(g3, g1)) + expect_false(subgraph_isomorphic(g3, g1, method = "vf2")) + expect_false(subgraph_isomorphic(g3, g1, method = "lad")) +}) + +test_that("count_isomorphisms", { + g1 <- graph_from_literal(A - B - C - D - A) + expect_equal(count_isomorphisms(g1, g1), 8) + + g2 <- graph_from_literal(A - B - C - A) + expect_equal(count_isomorphisms(g1, g2), 0) +}) + +test_that("count_isomorphisms_with_colors", { + expect_equal( + count_isomorphisms(make_ring(3), + make_ring(3), + edge.color1 = c(2, 2, 2), + edge.color2 = c(2, 2, 2), + vertex.color1 = c(3, 3, 3), + vertex.color2 = c(3, 3, 3), + method = "vf2" + ), + 6 + ) + + expect_equal( + count_isomorphisms(make_ring(3), + make_ring(3), + edge.color1 = c(2, 2, 2), + edge.color2 = c(2, 2, 2), + vertex.color1 = c(1, 2, 3), + vertex.color2 = c(1, 2, 3), + method = "vf2" + ), + 1 + ) + + expect_equal( + count_isomorphisms(make_ring(3), + make_ring(3), + edge.color1 = c(2, 2, 3), + edge.color2 = c(3, 2, 2), + vertex.color1 = c(3, 3, 3), + vertex.color2 = c(3, 3, 3), + method = "vf2" + ), + 2 + ) +}) + +test_that("count_subgraph_isomorphisms", { + g1 <- graph_from_literal(A - B - C - D - A) + g2 <- graph_from_literal(A - B - C - D) + + expect_equal(count_subgraph_isomorphisms(g2, g1, method = "lad"), 8) + expect_equal(count_subgraph_isomorphisms(g2, g1, method = "vf2"), 8) + + g3 <- graph_from_literal(A - B - C - A) + expect_equal(count_subgraph_isomorphisms(g3, g1, method = "lad"), 0) + expect_equal(count_subgraph_isomorphisms(g3, g1, method = "vf2"), 0) +}) + +test_that("isomorphisms", { + g1 <- graph_from_literal(A - B - C - D - A) + g2 <- graph_from_literal(W - X - Y - Z - W) + + res <- list( + V(g2)[1, 2, 3, 4], + V(g2)[1, 4, 3, 2], + V(g2)[2, 1, 4, 3], + V(g2)[2, 3, 4, 1], + V(g2)[3, 2, 1, 4], + V(g2)[3, 4, 1, 2], + V(g2)[4, 1, 2, 3], + V(g2)[4, 3, 2, 1] + ) + + expect_equal(ignore_attr = TRUE, isomorphisms(g1, g2), res) + + g3 <- graph_from_literal(X - Y - Z - X) + expect_equal(isomorphisms(g1, g3), list()) +}) + +test_that("subgraph_isomorphisms, lad", { + g1 <- graph_from_literal(A - B - C - D - A) + g2 <- graph_from_literal(Z - X - Y) + + res <- list( + V(g1)[1, 4, 3], + V(g1)[1, 2, 3], + V(g1)[2, 1, 4], + V(g1)[2, 3, 4], + V(g1)[3, 2, 1], + V(g1)[3, 4, 1], + V(g1)[4, 3, 2], + V(g1)[4, 1, 2] + ) + + expect_equal(ignore_attr = TRUE, subgraph_isomorphisms(g2, g1, method = "lad"), res) + + g3 <- graph_from_literal(X - Y - Z - X) + expect_equal(subgraph_isomorphisms(g3, g1, method = "lad"), list()) +}) + +test_that("subgraph_isomorphisms, vf2", { + g1 <- graph_from_literal(A - B - C - D - A) + g2 <- graph_from_literal(Z - X - Y) + + res <- list( + V(g1)[1, 2, 3], + V(g1)[1, 4, 3], + V(g1)[2, 1, 4], + V(g1)[2, 3, 4], + V(g1)[3, 2, 1], + V(g1)[3, 4, 1], + V(g1)[4, 1, 2], + V(g1)[4, 3, 2] + ) + + expect_equal(ignore_attr = TRUE, subgraph_isomorphisms(g2, g1, method = "vf2"), res) + + g3 <- graph_from_literal(X - Y - Z - X) + expect_equal(subgraph_isomorphisms(g3, g1, method = "vf2"), list()) +})