From 33e15584e7f9b4f7e9d44aaafceec01c22dc67e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 23 Sep 2024 10:11:00 +0200 Subject: [PATCH 1/5] test: add tests for as_adjacency_matrix() in test-conversion --- tests/testthat/_snaps/conversion.md | 54 ++++++++ tests/testthat/test-conversion.R | 187 ++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+) create mode 100644 tests/testthat/_snaps/conversion.md diff --git a/tests/testthat/_snaps/conversion.md b/tests/testthat/_snaps/conversion.md new file mode 100644 index 00000000000..fb33adaa55c --- /dev/null +++ b/tests/testthat/_snaps/conversion.md @@ -0,0 +1,54 @@ +# as.directed() deprecation + + Code + is_directed(as.directed(g, mode = "mutual")) + Condition + Warning: + `as.directed()` was deprecated in igraph 2.0.4. + i Please use `as_directed()` instead. + Output + [1] TRUE + +# as.undirected() deprecation + + Code + is_directed(as.undirected(g, mode = "collapse")) + Condition + Warning: + `as.undirected()` was deprecated in igraph 2.0.4. + i Please use `as_undirected()` instead. + Output + [1] FALSE + +# as_adjacency_matrix() errors well -- sparse + + Code + as_adjacency_matrix(g, attr = "bla") + Condition + Error in `get.adjacency.sparse()`: + ! no such edge attribute + +--- + + Code + as_adjacency_matrix(g, attr = "bla") + Condition + Error in `get.adjacency.sparse()`: + ! Matrices must be either numeric or logical, and the edge attribute is not + +# as_adjacency_matrix() errors well -- dense + + Code + as_adjacency_matrix(g, attr = "bla", sparse = FALSE) + Condition + Error in `get.adjacency.dense()`: + ! no such edge attribute + +--- + + Code + as_adjacency_matrix(g, attr = "bla", sparse = FALSE) + Condition + Error in `get.adjacency.dense()`: + ! Matrices must be either numeric or logical, and the edge attribute is not + diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 27ec7370e56..3fb9f024eee 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -69,3 +69,190 @@ test_that("as_undirected() keeps attributes", { expect_equal(df3[order(df3[, 1], df3[, 2]), ]$weight, c(1, 3, 2, 4, 5)) expect_equal(df4[order(df4[, 1], df4[, 2]), ]$weight, c(4, 9)) }) + +test_that("as_adjacency_matrix() works -- sparse", { + withr::local_seed(42) + + g <- sample_gnp(10, 2 / 10) + basic_adj_matrix <- as_adjacency_matrix(g) + expected_matrix <- new( + "dgCMatrix" |> + structure(package = "Matrix"), + i = c( + 8L, 2L, 3L, 6L, 1L, 3L, 4L, 5L, 7L, 1L, 2L, 2L, 6L, 2L, 6L, 7L, 9L, 1L, 4L, + 5L, 7L, 9L, 2L, 5L, 6L, 0L, 5L, 6L + ), + p = c(0L, 1L, 4L, 9L, 11L, 13L, 17L, 22L, 25L, 26L, 28L), + Dim = c(10L, 10L), + Dimnames = list(NULL, NULL), + x = rep(1, 28L), + factors = list() + ) + expect_equal(basic_adj_matrix, expected_matrix) + + V(g)$name <- letters[1:vcount(g)] + letter_adj_matrix <- as_adjacency_matrix(g) + expect_s4_class(letter_adj_matrix, "dgCMatrix") + expect_setequal(rownames(letter_adj_matrix), letters[1:vcount(g)]) + expect_equal(basic_adj_matrix, unname(letter_adj_matrix)) + + E(g)$weight <- runif(ecount(g)) + weight_adj_matrix <- as_adjacency_matrix(g, attr = "weight") + expect_s4_class(weight_adj_matrix, "dgCMatrix") + expect_equal(weight_adj_matrix[3, 2], 0.9575766) +}) + +test_that("as_adjacency_matrix() works -- sparse + not both", { + withr::local_seed(42) + g <- sample_gnp(10, 2 / 10) + + lower_adj_matrix <- as_adjacency_matrix(g, type = "lower") + lower_expected_matrix <- new( + "dgCMatrix" |> + structure(package = "Matrix"), + i = c(8L, 2L, 3L, 6L, 3L, 4L, 5L, 7L, 6L, 6L, 7L, 9L, 7L, 9L), + p = c(0L, 1L, 4L, 8L, 8L, 9L, 12L, 14L, 14L, 14L, 14L), + Dim = c(10L, 10L), + Dimnames = list(NULL, NULL), + x = rep(1, 14L), + factors = list() + ) + expect_equal(lower_adj_matrix, lower_expected_matrix) + + upper_adj_matrix <- as_adjacency_matrix(g, type = "upper") + upper_expected_matrix <- new( + "dgCMatrix" |> + structure(package = "Matrix"), + i = c(1L, 1L, 2L, 2L, 2L, 1L, 4L, 5L, 2L, 5L, 6L, 0L, 5L, 6L), + p = c(0L, 0L, 0L, 1L, 3L, 4L, 5L, 8L, 11L, 12L, 14L), + Dim = c(10L, 10L), + Dimnames = list(NULL, NULL), + x = rep(1, 14L), + factors = list() + ) + + expect_equal(upper_adj_matrix, upper_expected_matrix) +}) + +test_that("as_adjacency_matrix() errors well -- sparse", { + g <- sample_gnp(10, 2 / 10) + expect_snapshot(as_adjacency_matrix(g, attr = "bla"), error = TRUE) + + E(g)$bla <- letters[1:ecount(g)] + expect_snapshot(as_adjacency_matrix(g, attr = "bla"), error = TRUE) + +}) + +test_that("as_adjacency_matrix() works -- sparse directed", { + g <- make_ring(10, directed = TRUE) + adj_matrix <- as_adjacency_matrix(g) + expect_equal( + adj_matrix, + new( + "dgCMatrix" |> + structure(package = "Matrix"), + i = c(9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), + p = 0:10, + Dim = c(10L, 10L), + Dimnames = list(NULL, NULL), + x = rep(1, 10L), + factors = list() + ) + ) +}) + +test_that("as_adjacency_matrix() works -- dense", { + withr::local_seed(42) + + g <- sample_gnp(10, 2 / 10) + basic_adj_matrix <- as_adjacency_matrix(g, sparse = FALSE) + expected_matrix <- matrix( + rep( + c( + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 + ), + c( + 8L, 1L, 3L, 2L, 2L, 1L, 4L, 1L, 1L, 3L, 1L, 1L, 3L, 2L, 9L, 1L, 3L, 1L, 5L, + 1L, 3L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 3L, 1L, + 14L, 2L, 3L + ) + ), + nrow = 10L, + ncol = 10L + ) + expect_equal(basic_adj_matrix, expected_matrix) + + V(g)$name <- letters[1:vcount(g)] + letter_adj_matrix <- as_adjacency_matrix(g, sparse = FALSE) + expect_true(inherits(letter_adj_matrix, "matrix")) + expect_setequal(rownames(letter_adj_matrix), letters[1:vcount(g)]) + expect_equal(basic_adj_matrix, unname(letter_adj_matrix)) + + E(g)$weight <- runif(ecount(g)) + weight_adj_matrix <- as_adjacency_matrix(g, attr = "weight", sparse = FALSE) + expect_true(inherits(weight_adj_matrix, "matrix")) + expect_equal(weight_adj_matrix[3, 2], 0.9575766) +}) + +test_that("as_adjacency_matrix() errors well -- dense", { + g <- sample_gnp(10, 2 / 10) + expect_snapshot(as_adjacency_matrix(g, attr = "bla", sparse = FALSE), error = TRUE) + + E(g)$bla <- letters[1:ecount(g)] + expect_snapshot(as_adjacency_matrix(g, attr = "bla", sparse = FALSE), error = TRUE) + +}) + + +test_that("as_adjacency_matrix() works -- dense directed", { + withr::local_seed(42) + g <- make_ring(10, directed = TRUE) + # no different treatment than undirected if no attribute?! + adj_matrix <- as_adjacency_matrix(g, sparse = FALSE) + expect_equal( + adj_matrix, + matrix( + rep( + c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0), + c(9L, 2L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 1L) + ), + nrow = 10L, + ncol = 10L + ) + ) + + E(g)$weight <- runif(ecount(g)) + weight_adj_matrix <- as_adjacency_matrix(g, sparse = FALSE, attr = "weight") + expect_true(inherits(weight_adj_matrix, "matrix")) + expect_equal(weight_adj_matrix[1, 2], 0.914806, tolerance = 1e-5) +}) + +test_that("as_adjacency_matrix() works -- dense + not both", { + withr::local_seed(42) + g <- sample_gnp(10, 2 / 10) + + lower_adj_matrix <- as_adjacency_matrix(g, type = "lower", sparse = FALSE) + lower_expected_matrix <- matrix( + rep( + c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0), + c(8L, 1L, 3L, 2L, 2L, 1L, 6L, 3L, 1L, 1L, 18L, 1L, 9L, 2L, 1L, 1L, 7L, 1L, 1L, 1L, 30L) + ), + nrow = 10L, + ncol = 10L + ) + expect_equal(lower_adj_matrix, lower_expected_matrix) + + upper_adj_matrix <- as_adjacency_matrix(g, type = "upper") + upper_expected_matrix <- new( + "dgCMatrix" |> + structure(package = "Matrix"), + i = c(1L, 1L, 2L, 2L, 2L, 1L, 4L, 5L, 2L, 5L, 6L, 0L, 5L, 6L), + p = c(0L, 0L, 0L, 1L, 3L, 4L, 5L, 8L, 11L, 12L, 14L), + Dim = c(10L, 10L), + Dimnames = list(NULL, NULL), + x = rep(1, 14L), + factors = list() + ) + expect_equal(upper_adj_matrix, upper_expected_matrix) +}) From a30c78f12b218e6b916ab702689bf2f8180ed3de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 23 Sep 2024 11:44:34 +0200 Subject: [PATCH 2/5] fix: don't use native pipe in tests just yet --- tests/testthat/test-conversion.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 3fb9f024eee..7386021995f 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -76,7 +76,7 @@ test_that("as_adjacency_matrix() works -- sparse", { g <- sample_gnp(10, 2 / 10) basic_adj_matrix <- as_adjacency_matrix(g) expected_matrix <- new( - "dgCMatrix" |> + "dgCMatrix" %>% structure(package = "Matrix"), i = c( 8L, 2L, 3L, 6L, 1L, 3L, 4L, 5L, 7L, 1L, 2L, 2L, 6L, 2L, 6L, 7L, 9L, 1L, 4L, @@ -108,7 +108,7 @@ test_that("as_adjacency_matrix() works -- sparse + not both", { lower_adj_matrix <- as_adjacency_matrix(g, type = "lower") lower_expected_matrix <- new( - "dgCMatrix" |> + "dgCMatrix" %>% structure(package = "Matrix"), i = c(8L, 2L, 3L, 6L, 3L, 4L, 5L, 7L, 6L, 6L, 7L, 9L, 7L, 9L), p = c(0L, 1L, 4L, 8L, 8L, 9L, 12L, 14L, 14L, 14L, 14L), @@ -121,7 +121,7 @@ test_that("as_adjacency_matrix() works -- sparse + not both", { upper_adj_matrix <- as_adjacency_matrix(g, type = "upper") upper_expected_matrix <- new( - "dgCMatrix" |> + "dgCMatrix" %>% structure(package = "Matrix"), i = c(1L, 1L, 2L, 2L, 2L, 1L, 4L, 5L, 2L, 5L, 6L, 0L, 5L, 6L), p = c(0L, 0L, 0L, 1L, 3L, 4L, 5L, 8L, 11L, 12L, 14L), @@ -149,7 +149,7 @@ test_that("as_adjacency_matrix() works -- sparse directed", { expect_equal( adj_matrix, new( - "dgCMatrix" |> + "dgCMatrix" %>% structure(package = "Matrix"), i = c(9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), p = 0:10, @@ -245,7 +245,7 @@ test_that("as_adjacency_matrix() works -- dense + not both", { upper_adj_matrix <- as_adjacency_matrix(g, type = "upper") upper_expected_matrix <- new( - "dgCMatrix" |> + "dgCMatrix" %>% structure(package = "Matrix"), i = c(1L, 1L, 2L, 2L, 2L, 1L, 4L, 5L, 2L, 5L, 6L, 0L, 5L, 6L), p = c(0L, 0L, 0L, 1L, 3L, 4L, 5L, 8L, 11L, 12L, 14L), From 436deacca9253a68c802bdac8d92089abe64398c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Tue, 24 Sep 2024 11:26:20 +0200 Subject: [PATCH 3/5] test: improve coverage of `as_adjacency_matrix()` tests --- tests/testthat/test-conversion.R | 39 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 7386021995f..aa98bb256e3 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -231,28 +231,27 @@ test_that("as_adjacency_matrix() works -- dense directed", { test_that("as_adjacency_matrix() works -- dense + not both", { withr::local_seed(42) g <- sample_gnp(10, 2 / 10) + E(g)$attribute <- runif(ecount(g)) - lower_adj_matrix <- as_adjacency_matrix(g, type = "lower", sparse = FALSE) - lower_expected_matrix <- matrix( - rep( - c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0), - c(8L, 1L, 3L, 2L, 2L, 1L, 6L, 3L, 1L, 1L, 18L, 1L, 9L, 2L, 1L, 1L, 7L, 1L, 1L, 1L, 30L) - ), - nrow = 10L, - ncol = 10L + lower_adj_matrix <- as_adjacency_matrix( + g, + type = "lower", + sparse = FALSE, + attr = "attribute" ) - expect_equal(lower_adj_matrix, lower_expected_matrix) - upper_adj_matrix <- as_adjacency_matrix(g, type = "upper") - upper_expected_matrix <- new( - "dgCMatrix" %>% - structure(package = "Matrix"), - i = c(1L, 1L, 2L, 2L, 2L, 1L, 4L, 5L, 2L, 5L, 6L, 0L, 5L, 6L), - p = c(0L, 0L, 0L, 1L, 3L, 4L, 5L, 8L, 11L, 12L, 14L), - Dim = c(10L, 10L), - Dimnames = list(NULL, NULL), - x = rep(1, 14L), - factors = list() + expect_true(inherits(lower_adj_matrix, "matrix")) + expect_equal(lower_adj_matrix[3, 2], 0.9575766) + expect_equal(sum(lower_adj_matrix), 7.83583, tolerance = 1e-4) + + upper_adj_matrix <- as_adjacency_matrix( + g, + type = "upper", + sparse = FALSE, + attr = "attribute" ) - expect_equal(upper_adj_matrix, upper_expected_matrix) + + expect_true(inherits(upper_adj_matrix, "matrix")) + expect_equal(upper_adj_matrix[2, 3], 0.9575766) + expect_equal(sum(upper_adj_matrix), 7.83583, tolerance = 1e-4) }) From 4192ecdcceece4e5ef42a3f7d5f6e7ed0262612c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Tue, 24 Sep 2024 13:52:05 +0200 Subject: [PATCH 4/5] test: improve tests based on @szhorvat's feedback --- tests/testthat/test-conversion.R | 182 +++++++++++++++---------------- 1 file changed, 86 insertions(+), 96 deletions(-) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index aa98bb256e3..262d21d8423 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -71,24 +71,14 @@ test_that("as_undirected() keeps attributes", { }) test_that("as_adjacency_matrix() works -- sparse", { - withr::local_seed(42) - - g <- sample_gnp(10, 2 / 10) + g <- make_graph(c(1,2, 2,1, 2,2, 3,3, 3,3, 3,4, 4,2, 4,2, 4,2), directed = TRUE) basic_adj_matrix <- as_adjacency_matrix(g) - expected_matrix <- new( - "dgCMatrix" %>% - structure(package = "Matrix"), - i = c( - 8L, 2L, 3L, 6L, 1L, 3L, 4L, 5L, 7L, 1L, 2L, 2L, 6L, 2L, 6L, 7L, 9L, 1L, 4L, - 5L, 7L, 9L, 2L, 5L, 6L, 0L, 5L, 6L - ), - p = c(0L, 1L, 4L, 9L, 11L, 13L, 17L, 22L, 25L, 26L, 28L), - Dim = c(10L, 10L), - Dimnames = list(NULL, NULL), - x = rep(1, 28L), - factors = list() + expect_s4_class(basic_adj_matrix, "dgCMatrix") + expected_matrix <- matrix( + c(0, 1, 0, 0, 1, 1, 0, 3, 0, 0, 2, 0, 0, 0, 1, 0), + nrow = 4L, ncol = 4L ) - expect_equal(basic_adj_matrix, expected_matrix) + expect_equal(as.matrix(basic_adj_matrix), expected_matrix) V(g)$name <- letters[1:vcount(g)] letter_adj_matrix <- as_adjacency_matrix(g) @@ -96,46 +86,42 @@ test_that("as_adjacency_matrix() works -- sparse", { expect_setequal(rownames(letter_adj_matrix), letters[1:vcount(g)]) expect_equal(basic_adj_matrix, unname(letter_adj_matrix)) - E(g)$weight <- runif(ecount(g)) + E(g)$weight <- c(1.2, 3.4, 2.7, 5.6, 6.0, 0.1, 6.1, 3.3, 4.3) weight_adj_matrix <- as_adjacency_matrix(g, attr = "weight") expect_s4_class(weight_adj_matrix, "dgCMatrix") - expect_equal(weight_adj_matrix[3, 2], 0.9575766) + expect_equal(as.matrix(weight_adj_matrix), + matrix( + c(0, 3.4, 0, 0, 1.2, 2.7, 0, 13.7, 0, 0, 11.6, 0, 0, 0, 0.1, 0), + nrow = 4L, + ncol = 4L, + dimnames = list(c("a", "b", "c", "d"), c("a", "b", "c", "d")) + )) }) test_that("as_adjacency_matrix() works -- sparse + not both", { - withr::local_seed(42) - g <- sample_gnp(10, 2 / 10) + dg <- make_graph(c(1,2, 2,1, 2,2, 3,3, 3,3, 3,4, 4,2, 4,2, 4,2), directed = TRUE) + g <- as_undirected(dg, mode = "each") lower_adj_matrix <- as_adjacency_matrix(g, type = "lower") - lower_expected_matrix <- new( - "dgCMatrix" %>% - structure(package = "Matrix"), - i = c(8L, 2L, 3L, 6L, 3L, 4L, 5L, 7L, 6L, 6L, 7L, 9L, 7L, 9L), - p = c(0L, 1L, 4L, 8L, 8L, 9L, 12L, 14L, 14L, 14L, 14L), - Dim = c(10L, 10L), - Dimnames = list(NULL, NULL), - x = rep(1, 14L), - factors = list() + expect_s4_class(lower_adj_matrix, "dgCMatrix") + lower_expected_matrix <- matrix( + c(0, 2, 0, 0, 0, 1, 0, 3, 0, 0, 2, 1, 0, 0, 0, 0), + nrow = 4L, ncol = 4L ) - expect_equal(lower_adj_matrix, lower_expected_matrix) + expect_equal(as.matrix(lower_adj_matrix), lower_expected_matrix) upper_adj_matrix <- as_adjacency_matrix(g, type = "upper") - upper_expected_matrix <- new( - "dgCMatrix" %>% - structure(package = "Matrix"), - i = c(1L, 1L, 2L, 2L, 2L, 1L, 4L, 5L, 2L, 5L, 6L, 0L, 5L, 6L), - p = c(0L, 0L, 0L, 1L, 3L, 4L, 5L, 8L, 11L, 12L, 14L), - Dim = c(10L, 10L), - Dimnames = list(NULL, NULL), - x = rep(1, 14L), - factors = list() + expect_s4_class(upper_adj_matrix, "dgCMatrix") + upper_expected_matrix <- matrix( + c(0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 2, 0, 0, 3, 1, 0), + nrow = 4L, ncol = 4L ) - expect_equal(upper_adj_matrix, upper_expected_matrix) + expect_equal(as.matrix(upper_adj_matrix), upper_expected_matrix) }) test_that("as_adjacency_matrix() errors well -- sparse", { - g <- sample_gnp(10, 2 / 10) + g <- make_graph(c(1,2, 2,1, 2,2, 3,3, 3,3, 3,4, 4,2, 4,2, 4,2), directed = TRUE) expect_snapshot(as_adjacency_matrix(g, attr = "bla"), error = TRUE) E(g)$bla <- letters[1:ecount(g)] @@ -143,43 +129,28 @@ test_that("as_adjacency_matrix() errors well -- sparse", { }) -test_that("as_adjacency_matrix() works -- sparse directed", { - g <- make_ring(10, directed = TRUE) - adj_matrix <- as_adjacency_matrix(g) +test_that("as_adjacency_matrix() works -- sparse undirected", { + dg <- make_graph(c(1,2, 2,1, 2,2, 3,3, 3,3, 3,4, 4,2, 4,2, 4,2), directed = TRUE) + ug <- as_undirected(dg, mode = "each") + adj_matrix <- as_adjacency_matrix(ug) + expect_s4_class(adj_matrix, "dgCMatrix") expect_equal( - adj_matrix, - new( - "dgCMatrix" %>% - structure(package = "Matrix"), - i = c(9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), - p = 0:10, - Dim = c(10L, 10L), - Dimnames = list(NULL, NULL), - x = rep(1, 10L), - factors = list() + as.matrix(adj_matrix), + matrix( + c(0, 2, 0, 0, 2, 1, 0, 3, 0, 0, 2, 1, 0, 3, 1, 0), + nrow = 4L, + ncol = 4L ) ) }) test_that("as_adjacency_matrix() works -- dense", { - withr::local_seed(42) + g <- make_graph(c(1,2, 2,1, 2,2, 3,3, 3,3, 3,4, 4,2, 4,2, 4,2), directed = TRUE) - g <- sample_gnp(10, 2 / 10) basic_adj_matrix <- as_adjacency_matrix(g, sparse = FALSE) expected_matrix <- matrix( - rep( - c( - 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, - 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 - ), - c( - 8L, 1L, 3L, 2L, 2L, 1L, 4L, 1L, 1L, 3L, 1L, 1L, 3L, 2L, 9L, 1L, 3L, 1L, 5L, - 1L, 3L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 3L, 1L, - 14L, 2L, 3L - ) - ), - nrow = 10L, - ncol = 10L + c(0, 1, 0, 0, 1, 1, 0, 3, 0, 0, 2, 0, 0, 0, 1, 0), + nrow = 4L, ncol = 4L ) expect_equal(basic_adj_matrix, expected_matrix) @@ -189,14 +160,21 @@ test_that("as_adjacency_matrix() works -- dense", { expect_setequal(rownames(letter_adj_matrix), letters[1:vcount(g)]) expect_equal(basic_adj_matrix, unname(letter_adj_matrix)) - E(g)$weight <- runif(ecount(g)) + E(g)$weight <- c(1.2, 3.4, 2.7, 5.6, 6.0, 0.1, 6.1, 3.3, 4.3) weight_adj_matrix <- as_adjacency_matrix(g, attr = "weight", sparse = FALSE) - expect_true(inherits(weight_adj_matrix, "matrix")) - expect_equal(weight_adj_matrix[3, 2], 0.9575766) + expect_equal( + weight_adj_matrix, + matrix( + c(0, 3.4, 0, 0, 1.2, 2.7, 0, 4.3, 0, 0, 6, 0, 0, 0, 0.1, 0), + nrow = 4L, + ncol = 4L, + dimnames = list(c("a", "b", "c", "d"), c("a", "b", "c", "d")) + ) + ) }) test_that("as_adjacency_matrix() errors well -- dense", { - g <- sample_gnp(10, 2 / 10) + g <- make_graph(c(1,2, 2,1, 2,2, 3,3, 3,3, 3,4, 4,2, 4,2, 4,2), directed = TRUE) expect_snapshot(as_adjacency_matrix(g, attr = "bla", sparse = FALSE), error = TRUE) E(g)$bla <- letters[1:ecount(g)] @@ -205,33 +183,35 @@ test_that("as_adjacency_matrix() errors well -- dense", { }) -test_that("as_adjacency_matrix() works -- dense directed", { - withr::local_seed(42) - g <- make_ring(10, directed = TRUE) +test_that("as_adjacency_matrix() works -- dense undirected", { + dg <- make_graph(c(1,2, 2,1, 2,2, 3,3, 3,3, 3,4, 4,2, 4,2, 4,2), directed = TRUE) + ug <- as_undirected(dg, mode = "each") # no different treatment than undirected if no attribute?! - adj_matrix <- as_adjacency_matrix(g, sparse = FALSE) + adj_matrix <- as_adjacency_matrix(ug, sparse = FALSE) expect_equal( adj_matrix, matrix( - rep( - c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0), - c(9L, 2L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 10L, 1L, 1L) - ), - nrow = 10L, - ncol = 10L + c(0, 2, 0, 0, 2, 1, 0, 3, 0, 0, 2, 1, 0, 3, 1, 0), + nrow = 4L, ncol = 4L ) ) - E(g)$weight <- runif(ecount(g)) - weight_adj_matrix <- as_adjacency_matrix(g, sparse = FALSE, attr = "weight") - expect_true(inherits(weight_adj_matrix, "matrix")) - expect_equal(weight_adj_matrix[1, 2], 0.914806, tolerance = 1e-5) + E(ug)$weight <- c(1.2, 3.4, 2.7, 5.6, 6.0, 0.1, 6.1, 3.3, 4.3) + weight_adj_matrix <- as_adjacency_matrix(ug, sparse = FALSE, attr = "weight") + expect_equal( + weight_adj_matrix, + matrix( + c(0, 3.4, 0, 0, 3.4, 2.7, 0, 4.3, 0, 0, 6, 0.1, 0, 4.3, 0.1, 0), + nrow = 4L, + ncol = 4L + ) + ) }) test_that("as_adjacency_matrix() works -- dense + not both", { - withr::local_seed(42) - g <- sample_gnp(10, 2 / 10) - E(g)$attribute <- runif(ecount(g)) + dg <- make_graph(c(1,2, 2,1, 2,2, 3,3, 3,3, 3,4, 4,2, 4,2, 4,2), directed = TRUE) + g <- as_undirected(dg, mode = "each") + E(g)$attribute <- c(1.2, 3.4, 2.7, 5.6, 6.0, 0.1, 6.1, 3.3, 4.3) lower_adj_matrix <- as_adjacency_matrix( g, @@ -240,9 +220,14 @@ test_that("as_adjacency_matrix() works -- dense + not both", { attr = "attribute" ) - expect_true(inherits(lower_adj_matrix, "matrix")) - expect_equal(lower_adj_matrix[3, 2], 0.9575766) - expect_equal(sum(lower_adj_matrix), 7.83583, tolerance = 1e-4) + expect_equal( + lower_adj_matrix, + matrix( + c(0, 3.4, 0, 0, 0, 2.7, 0, 4.3, 0, 0, 6, 0.1, 0, 0, 0, 0), + nrow = 4L, + ncol = 4L + ) + ) upper_adj_matrix <- as_adjacency_matrix( g, @@ -251,7 +236,12 @@ test_that("as_adjacency_matrix() works -- dense + not both", { attr = "attribute" ) - expect_true(inherits(upper_adj_matrix, "matrix")) - expect_equal(upper_adj_matrix[2, 3], 0.9575766) - expect_equal(sum(upper_adj_matrix), 7.83583, tolerance = 1e-4) + expect_equal( + upper_adj_matrix, + matrix( + c(0, 0, 0, 0, 3.4, 2.7, 0, 0, 0, 0, 6, 0, 0, 4.3, 0.1, 0), + nrow = 4L, + ncol = 4L + ) + ) }) From f99c72cef4891de57bbc4b3ac8e893d7f3150c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Tue, 24 Sep 2024 15:36:06 +0200 Subject: [PATCH 5/5] fix: for oldrel --- tests/testthat/test-conversion.R | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 262d21d8423..aa8ca51fe75 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -78,13 +78,17 @@ test_that("as_adjacency_matrix() works -- sparse", { c(0, 1, 0, 0, 1, 1, 0, 3, 0, 0, 2, 0, 0, 0, 1, 0), nrow = 4L, ncol = 4L ) - expect_equal(as.matrix(basic_adj_matrix), expected_matrix) + basic_adj_matrix <- as.matrix(basic_adj_matrix) + dimnames(basic_adj_matrix) <- NULL + expect_equal(basic_adj_matrix, expected_matrix) V(g)$name <- letters[1:vcount(g)] letter_adj_matrix <- as_adjacency_matrix(g) expect_s4_class(letter_adj_matrix, "dgCMatrix") expect_setequal(rownames(letter_adj_matrix), letters[1:vcount(g)]) - expect_equal(basic_adj_matrix, unname(letter_adj_matrix)) + letter_adj_matrix <- as.matrix(letter_adj_matrix) + dimnames(letter_adj_matrix) <- NULL + expect_equal(basic_adj_matrix, letter_adj_matrix) E(g)$weight <- c(1.2, 3.4, 2.7, 5.6, 6.0, 0.1, 6.1, 3.3, 4.3) weight_adj_matrix <- as_adjacency_matrix(g, attr = "weight") @@ -108,7 +112,9 @@ test_that("as_adjacency_matrix() works -- sparse + not both", { c(0, 2, 0, 0, 0, 1, 0, 3, 0, 0, 2, 1, 0, 0, 0, 0), nrow = 4L, ncol = 4L ) - expect_equal(as.matrix(lower_adj_matrix), lower_expected_matrix) + lower_expected_matrix <- as.matrix(lower_expected_matrix) + dimnames(lower_expected_matrix) <- NULL + expect_equal(lower_expected_matrix, lower_expected_matrix) upper_adj_matrix <- as_adjacency_matrix(g, type = "upper") expect_s4_class(upper_adj_matrix, "dgCMatrix") @@ -116,8 +122,9 @@ test_that("as_adjacency_matrix() works -- sparse + not both", { c(0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 2, 0, 0, 3, 1, 0), nrow = 4L, ncol = 4L ) - - expect_equal(as.matrix(upper_adj_matrix), upper_expected_matrix) + upper_adj_matrix <- as.matrix(upper_adj_matrix) + dimnames(upper_adj_matrix) <- NULL + expect_equal(upper_adj_matrix, upper_expected_matrix) }) test_that("as_adjacency_matrix() errors well -- sparse", { @@ -134,8 +141,11 @@ test_that("as_adjacency_matrix() works -- sparse undirected", { ug <- as_undirected(dg, mode = "each") adj_matrix <- as_adjacency_matrix(ug) expect_s4_class(adj_matrix, "dgCMatrix") + + adj_matrix <- as.matrix(adj_matrix) + dimnames(adj_matrix) <- NULL expect_equal( - as.matrix(adj_matrix), + adj_matrix, matrix( c(0, 2, 0, 0, 2, 1, 0, 3, 0, 0, 2, 1, 0, 3, 1, 0), nrow = 4L,