From 92654f232b75962b5d05e44ddcfb8398e68b1d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 30 Sep 2024 15:29:58 +0200 Subject: [PATCH 1/8] test: fold as_edgelist()'s test into test-conversion --- tests/testthat/test-conversion.R | 7 +++++++ tests/testthat/test-get.edgelist.R | 6 ------ 2 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 tests/testthat/test-get.edgelist.R diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index aa8ca51fe75..93809d57c4f 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -255,3 +255,10 @@ 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) +}) 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) -}) From 4c182906f3b0bd8633cd18fd6366664176eb0bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 30 Sep 2024 15:37:47 +0200 Subject: [PATCH 2/8] test: light refactoring of `as_adj_list()`'s test --- tests/testthat/test-get.adjlist.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-get.adjlist.R b/tests/testthat/test-get.adjlist.R index 7c569726e1a..5a474b1d470 100644 --- a/tests/testthat/test-get.adjlist.R +++ b/tests/testthat/test-get.adjlist.R @@ -1,12 +1,13 @@ -test_that("as_adj_list works", { +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, + expect_true(isomorphic(g, g2, vertex.color1 = 1:vcount(g), - vertex.color2 = 1:vcount(g2) - )$iso) + vertex.color2 = 1:vcount(g2), + method = "vf2" + )) #### From fbc12ddf8f94b9258d407f7164fd4b4245acff77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 30 Sep 2024 15:38:31 +0200 Subject: [PATCH 3/8] test: fold `as_adj_list()`'s test into test-conversion --- tests/testthat/test-conversion.R | 35 +++++++++++++++++++++++++++++++ tests/testthat/test-get.adjlist.R | 34 ------------------------------ 2 files changed, 35 insertions(+), 34 deletions(-) delete mode 100644 tests/testthat/test-get.adjlist.R diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 93809d57c4f..b8a55b753e0 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -262,3 +262,38 @@ test_that("as_edgelist() works", { 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) + } +}) diff --git a/tests/testthat/test-get.adjlist.R b/tests/testthat/test-get.adjlist.R deleted file mode 100644 index 5a474b1d470..00000000000 --- a/tests/testthat/test-get.adjlist.R +++ /dev/null @@ -1,34 +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(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) - } -}) From 195db3b012e280c3ad780c6741c8e856f05463fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 30 Sep 2024 15:41:54 +0200 Subject: [PATCH 4/8] test: fold graphnel tests into test-conversion --- tests/testthat/test-conversion.R | 41 ++++++++++++++++++++++++++++++++ tests/testthat/test-graphNEL.R | 41 -------------------------------- 2 files changed, 41 insertions(+), 41 deletions(-) delete mode 100644 tests/testthat/test-graphNEL.R diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index b8a55b753e0..158492d580f 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -297,3 +297,44 @@ test_that("as_adj_list() works", { 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)) +}) 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)) -}) From a95e2e6bdee4eee43939a71fe147ca8d4e63bc78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 30 Sep 2024 15:43:51 +0200 Subject: [PATCH 5/8] test: fold `as_long_data_frame()`'s test into test-conversion --- tests/testthat/test-conversion.R | 17 +++++++++++++++++ tests/testthat/test-graph.data.frame.R | 16 ---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 158492d580f..3384cc5e826 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -338,3 +338,20 @@ test_that("as_graphnel() does not duplicate loop edges", { 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) + }) +}) 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) - }) -}) From 3546100a7d73be96023a0a94a2997b3718e6ecc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 30 Sep 2024 15:53:16 +0200 Subject: [PATCH 6/8] test: fold `as_biadjacency_matrix()`'s test into test-conversion --- tests/testthat/test-conversion.R | 31 +++++++++++++++++++++++++++++ tests/testthat/test-get.incidence.R | 30 ---------------------------- 2 files changed, 31 insertions(+), 30 deletions(-) delete mode 100644 tests/testthat/test-get.incidence.R diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 3384cc5e826..2fd6c336b1e 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -355,3 +355,34 @@ test_that("as_long_data_frame() works correctly with and without names", { 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 -- 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.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)) -}) From 169dca27b6c413a30a647c6444e70d9165ba7c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 30 Sep 2024 16:04:49 +0200 Subject: [PATCH 7/8] test: add test case for `as_biadjacency_matrix()` dense + attribute --- tests/testthat/test-conversion.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 2fd6c336b1e..fd6a5f31b65 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -378,6 +378,23 @@ test_that("as_biadjacency_matrix() works -- dense named", { 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) From 9b275a2048a10f67d25eef30a09071938a9208ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 30 Sep 2024 16:04:59 +0200 Subject: [PATCH 8/8] test: snap --- tests/testthat/_snaps/conversion.md | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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 +