diff --git a/tests/testthat/_snaps/conversion.md b/tests/testthat/_snaps/conversion.md index 12b0bc0c7fd..ebadf3499b9 100644 --- a/tests/testthat/_snaps/conversion.md +++ b/tests/testthat/_snaps/conversion.md @@ -52,3 +52,38 @@ Error in `get.adjacency.dense()`: ! Matrices must be either numeric or logical, and the edge attribute is not +# as_long_data_frame() works correctly with and without names + + Code + ring <- make_ring(3) + as_long_data_frame(ring) + Output + from to + 1 1 2 + 2 2 3 + 3 1 3 + Code + V(ring)$name <- letters[1:3] + as_long_data_frame(ring) + Output + from to from_name to_name + 1 1 2 a b + 2 2 3 b c + 3 1 3 a c + Code + V(ring)$score <- LETTERS[1:3] + as_long_data_frame(ring) + Output + from to from_name from_score to_name to_score + 1 1 2 a A b B + 2 2 3 b B c C + 3 1 3 a A c C + Code + E(ring)$info <- 3:1 + as_long_data_frame(ring) + Output + from to info from_name from_score to_name to_score + 1 1 2 3 a A b B + 2 2 3 2 b B c C + 3 1 3 1 a A c C + diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index aa8ca51fe75..fd6a5f31b65 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -255,3 +255,151 @@ test_that("as_adjacency_matrix() works -- dense + not both", { ) ) }) + +test_that("as_edgelist() works", { + g <- sample_gnp(100, 3 / 100) + e <- as_edgelist(g) + g2 <- make_graph(t(e), n = vcount(g), dir = FALSE) + expect_isomorphic(g, g2) +}) + +test_that("as_adj_list() works", { + g <- sample_gnp(50, 2 / 50) + al <- as_adj_list(g) + g2 <- graph_from_adj_list(al, mode = "all") + expect_isomorphic(g, g2) + expect_true(isomorphic(g, g2, + vertex.color1 = 1:vcount(g), + vertex.color2 = 1:vcount(g2), + method = "vf2" + )) + + #### + + el <- as_adj_edge_list(g) + for (i in 1:vcount(g)) { + a <- E(g)[.inc(i)] + expect_equal(length(a), length(el[[i]]), ignore_attr = TRUE) + expect_equal(sort(el[[i]]), sort(a), ignore_attr = TRUE) + } + + g <- sample_gnp(50, 4 / 50, directed = TRUE) + el1 <- as_adj_edge_list(g, mode = "out") + el2 <- as_adj_edge_list(g, mode = "in") + for (i in 1:vcount(g)) { + a <- E(g)[.from(i)] + expect_equal(length(a), length(el1[[i]]), ignore_attr = TRUE) + expect_equal(sort(el1[[i]]), sort(a), ignore_attr = TRUE) + } + for (i in 1:vcount(g)) { + a <- E(g)[.to(i)] + expect_equal(length(a), length(el2[[i]]), ignore_attr = TRUE) + expect_equal(sort(el2[[i]]), sort(a), ignore_attr = TRUE) + } +}) + +test_that("graph_from_graphnel() works", { + skip_if_not_installed("graph") + suppressPackageStartupMessages(library(graph, warn.conflicts = FALSE)) + + g <- sample_gnp(100, 5 / 100) + N <- as_graphnel(g) + g2 <- graph_from_graphnel(N) + gi <- graph.isomorphic.vf2(g, g2) + expect_true(gi$iso) + expect_equal(gi$map12, 1:vcount(g)) + expect_equal(gi$map21, 1:vcount(g)) + + ## Attributes + + V(g)$name <- as.character(vcount(g):1) + E(g)$weight <- sample(1:10, ecount(g), replace = TRUE) + g$name <- "Foobar" + + N <- as_graphnel(g) + g2 <- graph_from_graphnel(N) + expect_isomorphic(g, g2) + expect_equal(V(g)$name, V(g2)$name) + + A <- as_adjacency_matrix(g, attr = "weight", sparse = FALSE) + A2 <- as_adjacency_matrix(g2, attr = "weight", sparse = FALSE) + expect_equal(A, A) + expect_equal(g$name, g2$name) +}) + +test_that("as_graphnel() does not duplicate loop edges", { + skip_if_not_installed("graph") + + mat <- matrix(c(1, 0.5, 0.5, 0), nrow = 2) + dimnames(mat) <- list(c("A", "B"), c("A", "B")) + + igr <- graph_from_adjacency_matrix(mat, mode = "undirected", weighted = TRUE) + + grNEL <- as_graphnel(igr) + expect_equal(graph::edgeL(grNEL)$A$edges, c(1, 2)) +}) + + +test_that("as_long_data_frame() works correctly with and without names", { + expect_snapshot({ + ring <- make_ring(3) + as_long_data_frame(ring) + + V(ring)$name <- letters[1:3] + as_long_data_frame(ring) + + V(ring)$score <- LETTERS[1:3] + as_long_data_frame(ring) + + E(ring)$info <- 3:1 + as_long_data_frame(ring) + }) +}) + +test_that("as_biadjacency_matrix() works -- dense", { + I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) + g <- graph_from_biadjacency_matrix(I) + I2 <- as_biadjacency_matrix(g) + expect_equal(I, I2, ignore_attr = TRUE) + expect_identical(rownames(I2), as.character(1:7)) + expect_identical(colnames(I2), as.character(8:12)) +}) + +test_that("as_biadjacency_matrix() works -- dense named", { + I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) + g <- graph_from_biadjacency_matrix(I) + V(g)$name <- letters[1:length(V(g))] + + expect_true(is_named(g)) + + I2 <- as_biadjacency_matrix(g) + expect_equal(I, I2, ignore_attr = TRUE) + expect_identical(rownames(I2), c("a", "b", "c", "d", "e", "f", "g")) + expect_identical(colnames(I2), c("h", "i", "j", "k", "l")) +}) + +test_that("as_biadjacency_matrix() works -- dense + attribute", { + withr::local_seed(42) + I <- matrix(sample(0:1, 9, replace = TRUE, prob = c(3, 1)), ncol = 3) + g <- graph_from_biadjacency_matrix(I) + E(g)$something <- letters[1:ecount(g)] + + I2 <- as_biadjacency_matrix(g, attr = "something") + expect_equal( + unname(I2), + matrix( + c("a", "c", "0", "b", "0", "0", "0", "0", "0"), + nrow = 3L, + ncol = 3L + ) + ) +}) + +test_that("as_biadjacency_matrix() works -- sparse", { + I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) + g <- graph_from_biadjacency_matrix(I) + I3 <- as_biadjacency_matrix(g, sparse = TRUE) + expect_equal(as.matrix(I3), I, ignore_attr = TRUE) + expect_identical(rownames(I3), as.character(1:7)) + expect_identical(colnames(I3), as.character(8:12)) +}) diff --git a/tests/testthat/test-get.adjlist.R b/tests/testthat/test-get.adjlist.R deleted file mode 100644 index 7c569726e1a..00000000000 --- a/tests/testthat/test-get.adjlist.R +++ /dev/null @@ -1,33 +0,0 @@ -test_that("as_adj_list works", { - g <- sample_gnp(50, 2 / 50) - al <- as_adj_list(g) - g2 <- graph_from_adj_list(al, mode = "all") - expect_isomorphic(g, g2) - expect_true(graph.isomorphic.vf2(g, g2, - vertex.color1 = 1:vcount(g), - vertex.color2 = 1:vcount(g2) - )$iso) - - #### - - el <- as_adj_edge_list(g) - for (i in 1:vcount(g)) { - a <- E(g)[.inc(i)] - expect_equal(length(a), length(el[[i]]), ignore_attr = TRUE) - expect_equal(sort(el[[i]]), sort(a), ignore_attr = TRUE) - } - - g <- sample_gnp(50, 4 / 50, directed = TRUE) - el1 <- as_adj_edge_list(g, mode = "out") - el2 <- as_adj_edge_list(g, mode = "in") - for (i in 1:vcount(g)) { - a <- E(g)[.from(i)] - expect_equal(length(a), length(el1[[i]]), ignore_attr = TRUE) - expect_equal(sort(el1[[i]]), sort(a), ignore_attr = TRUE) - } - for (i in 1:vcount(g)) { - a <- E(g)[.to(i)] - expect_equal(length(a), length(el2[[i]]), ignore_attr = TRUE) - expect_equal(sort(el2[[i]]), sort(a), ignore_attr = TRUE) - } -}) diff --git a/tests/testthat/test-get.edgelist.R b/tests/testthat/test-get.edgelist.R deleted file mode 100644 index f3e4bae9516..00000000000 --- a/tests/testthat/test-get.edgelist.R +++ /dev/null @@ -1,6 +0,0 @@ -test_that("as_edgelist works", { - g <- sample_gnp(100, 3 / 100) - e <- as_edgelist(g) - g2 <- make_graph(t(e), n = vcount(g), dir = FALSE) - expect_isomorphic(g, g2) -}) diff --git a/tests/testthat/test-get.incidence.R b/tests/testthat/test-get.incidence.R deleted file mode 100644 index a750290c07e..00000000000 --- a/tests/testthat/test-get.incidence.R +++ /dev/null @@ -1,30 +0,0 @@ -test_that("as_biadjacency_matrix() works -- dense", { - I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) - g <- graph_from_biadjacency_matrix(I) - I2 <- as_biadjacency_matrix(g) - expect_equal(I, I2, ignore_attr = TRUE) - expect_identical(rownames(I2), as.character(1:7)) - expect_identical(colnames(I2), as.character(8:12)) -}) - -test_that("as_biadjacency_matrix() works -- dense named", { - I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) - g <- graph_from_biadjacency_matrix(I) - V(g)$name <- letters[1:length(V(g))] - - expect_true(is_named(g)) - - I2 <- as_biadjacency_matrix(g) - expect_equal(I, I2, ignore_attr = TRUE) - expect_identical(rownames(I2), c("a", "b", "c", "d", "e", "f", "g")) - expect_identical(colnames(I2), c("h", "i", "j", "k", "l")) -}) - -test_that("as_biadjacency_matrix() works -- sparse", { - I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) - g <- graph_from_biadjacency_matrix(I) - I3 <- as_biadjacency_matrix(g, sparse = TRUE) - expect_equal(as.matrix(I3), I, ignore_attr = TRUE) - expect_identical(rownames(I3), as.character(1:7)) - expect_identical(colnames(I3), as.character(8:12)) -}) diff --git a/tests/testthat/test-graph.data.frame.R b/tests/testthat/test-graph.data.frame.R index 89f1e05868c..27a1a74bdc0 100644 --- a/tests/testthat/test-graph.data.frame.R +++ b/tests/testthat/test-graph.data.frame.R @@ -44,19 +44,3 @@ test_that("graph_from_data_frame works on matrices", { el2 <- as_data_frame(g) expect_equal(as.data.frame(el), el2, ignore_attr = TRUE) }) - -test_that("as_long_data_frame() works correctly with and without names", { - expect_snapshot({ - ring <- make_ring(3) - as_long_data_frame(ring) - - V(ring)$name <- letters[1:3] - as_long_data_frame(ring) - - V(ring)$score <- LETTERS[1:3] - as_long_data_frame(ring) - - E(ring)$info <- 3:1 - as_long_data_frame(ring) - }) -}) diff --git a/tests/testthat/test-graphNEL.R b/tests/testthat/test-graphNEL.R deleted file mode 100644 index 07374b9340a..00000000000 --- a/tests/testthat/test-graphNEL.R +++ /dev/null @@ -1,41 +0,0 @@ -test_that("graphNEL conversion works", { - skip_if_not_installed("graph") - - suppressPackageStartupMessages(library(graph, warn.conflicts = FALSE)) - - g <- sample_gnp(100, 5 / 100) - N <- as_graphnel(g) - g2 <- graph_from_graphnel(N) - gi <- graph.isomorphic.vf2(g, g2) - expect_true(gi$iso) - expect_equal(gi$map12, 1:vcount(g)) - expect_equal(gi$map21, 1:vcount(g)) - - ## Attributes - - V(g)$name <- as.character(vcount(g):1) - E(g)$weight <- sample(1:10, ecount(g), replace = TRUE) - g$name <- "Foobar" - - N <- as_graphnel(g) - g2 <- graph_from_graphnel(N) - expect_isomorphic(g, g2) - expect_equal(V(g)$name, V(g2)$name) - - A <- as_adjacency_matrix(g, attr = "weight", sparse = FALSE) - A2 <- as_adjacency_matrix(g2, attr = "weight", sparse = FALSE) - expect_equal(A, A) - expect_equal(g$name, g2$name) -}) - -test_that("graphNEL does not duplicate loop edges", { - skip_if_not_installed("graph") - - mat <- matrix(c(1, 0.5, 0.5, 0), nrow = 2) - dimnames(mat) <- list(c("A", "B"), c("A", "B")) - - igr <- graph_from_adjacency_matrix(mat, mode = "undirected", weighted = T) - - grNEL <- as_graphnel(igr) - expect_equal(graph::edgeL(grNEL)$A$edges, c(1, 2)) -})