diff --git a/tests/testthat/helper-indexing.R b/tests/testthat/helper-indexing.R new file mode 100644 index 00000000000..c1c00ae9b84 --- /dev/null +++ b/tests/testthat/helper-indexing.R @@ -0,0 +1,24 @@ +vector_to_square_matrix <- function(...) { + v <- as.numeric(as.vector(list(...))) + matrix(v, nrow = sqrt(length(v))) +} + +canonicalize_matrix <- function(x) { + x <- as.matrix(x) + dimnames(x) <- NULL + x +} + +make_test_named_tree <- function() { + g <- make_tree(20) + V(g)$name <- letters[1:vcount(g)] + g +} + +make_test_weighted_tree <- function() { + g <- make_tree(20) + V(g)$name <- letters[1:vcount(g)] + el <- as_edgelist(g, names = FALSE) + E(g)$weight <- el[, 1] * el[, 2] + g +} diff --git a/tests/testthat/test-indexing.R b/tests/testthat/test-indexing.R index 4a847933af1..64c103c9409 100644 --- a/tests/testthat/test-indexing.R +++ b/tests/testthat/test-indexing.R @@ -1,46 +1,35 @@ -mm <- function(...) { - v <- as.numeric(as.vector(list(...))) - matrix(v, nrow = sqrt(length(v))) -} -am <- function(x) { - x <- as.matrix(x) - dimnames(x) <- NULL - x -} - -g <- make_tree(20) - test_that("[ indexing works", { + g <- make_tree(20) ## Are these vertices connected? expect_that(g[1, 2], equals(1)) - expect_that(am(g[c(1, 1, 7), c(2, 3, 14)]), equals(mm(1, 1, 0, 1, 1, 0, 0, 0, 1))) - expect_that(am(g[c(1, 1, 7), c(5, 3, 12)]), equals(mm(0, 0, 0, 1, 1, 0, 0, 0, 0))) - expect_that(am(g[c(1, 1, 1, 1), c(2, 3, 2, 2)]), equals(matrix(1, 4, 4))) - expect_that(am(g[c(8, 17), c(17, 8)]), equals(mm(1, 0, 0, 0))) + expect_that(canonicalize_matrix(g[c(1, 1, 7), c(2, 3, 14)]), equals(vector_to_square_matrix(1, 1, 0, 1, 1, 0, 0, 0, 1))) + expect_that(canonicalize_matrix(g[c(1, 1, 7), c(5, 3, 12)]), equals(vector_to_square_matrix(0, 0, 0, 1, 1, 0, 0, 0, 0))) + expect_that(canonicalize_matrix(g[c(1, 1, 1, 1), c(2, 3, 2, 2)]), equals(matrix(1, 4, 4))) + expect_that(canonicalize_matrix(g[c(8, 17), c(17, 8)]), equals(vector_to_square_matrix(1, 0, 0, 0))) }) -V(g)$name <- letters[1:vcount(g)] - test_that("[ indexing works with symbolic names", { - ## The same with symbolic names + g <- make_test_named_tree() + expect_that(g["a", "b"], equals(1)) expect_that( - am(g[c("a", "a", "g"), c("b", "c", "n")]), - equals(mm(1, 1, 0, 1, 1, 0, 0, 0, 1)) + canonicalize_matrix(g[c("a", "a", "g"), c("b", "c", "n")]), + equals(vector_to_square_matrix(1, 1, 0, 1, 1, 0, 0, 0, 1)) ) expect_that( - am(g[c("a", "a", "g"), c("e", "c", "l")]), - equals(mm(0, 0, 0, 1, 1, 0, 0, 0, 0)) + canonicalize_matrix(g[c("a", "a", "g"), c("e", "c", "l")]), + equals(vector_to_square_matrix(0, 0, 0, 1, 1, 0, 0, 0, 0)) ) expect_that( - am(g[c("a", "a", "a", "a"), c("b", "c", "b", "b")]), + canonicalize_matrix(g[c("a", "a", "a", "a"), c("b", "c", "b", "b")]), equals(matrix(1, 4, 4)) ) - expect_that(am(g[c("h", "q"), c("q", "h")]), equals(mm(1, 0, 0, 0))) + expect_that(canonicalize_matrix(g[c("h", "q"), c("q", "h")]), equals(vector_to_square_matrix(1, 0, 0, 0))) }) test_that("[ indexing works with logical vectors", { - ## Logical vectors + g <- make_test_named_tree() + lres <- structure( c( 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, @@ -59,7 +48,8 @@ test_that("[ indexing works with logical vectors", { }) test_that("[ indexing works with negative indices", { - ## Negative indices + g <- make_test_named_tree() + nres <- structure( c( 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, @@ -79,43 +69,41 @@ test_that("[ indexing works with negative indices", { expect_that(as.matrix(g[2:3, -1]), equals(nres)) }) -el <- as_edgelist(g, names = FALSE) -E(g)$weight <- el[, 1] * el[, 2] - test_that("[ indexing works with weighted graphs", { - ## Weighted graphs + g <- make_test_weighted_tree() + expect_that(g[1, 2], equals(2)) - expect_that(am(g[c(1, 1, 7), c(2, 3, 14)]), equals(mm(2, 2, 0, 3, 3, 0, 0, 0, 98))) - expect_that(am(g[c(1, 1, 7), c(5, 3, 12)]), equals(mm(0, 0, 0, 3, 3, 0, 0, 0, 0))) + expect_that(canonicalize_matrix(g[c(1, 1, 7), c(2, 3, 14)]), equals(vector_to_square_matrix(2, 2, 0, 3, 3, 0, 0, 0, 98))) + expect_that(canonicalize_matrix(g[c(1, 1, 7), c(5, 3, 12)]), equals(vector_to_square_matrix(0, 0, 0, 3, 3, 0, 0, 0, 0))) expect_that( - am(g[c(1, 1, 1, 1), c(2, 3, 2, 2)]), - equals(mm(2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2)) + canonicalize_matrix(g[c(1, 1, 1, 1), c(2, 3, 2, 2)]), + equals(vector_to_square_matrix(2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2)) ) - expect_that(am(g[c(8, 17), c(17, 8)]), equals(mm(136, 0, 0, 0))) + expect_that(canonicalize_matrix(g[c(8, 17), c(17, 8)]), equals(vector_to_square_matrix(136, 0, 0, 0))) }) test_that("[ indexing works with weighted graphs and symbolic names", { - ## Weighted graph, with symbolic names + g <- make_test_weighted_tree() + expect_that(g["a", "b"], equals(2)) expect_that( - am(g[c("a", "a", "g"), c("b", "c", "n")]), - equals(mm(2, 2, 0, 3, 3, 0, 0, 0, 98)) + canonicalize_matrix(g[c("a", "a", "g"), c("b", "c", "n")]), + equals(vector_to_square_matrix(2, 2, 0, 3, 3, 0, 0, 0, 98)) ) expect_that( - am(g[c("a", "a", "g"), c("e", "c", "l")]), - equals(mm(0, 0, 0, 3, 3, 0, 0, 0, 0)) + canonicalize_matrix(g[c("a", "a", "g"), c("e", "c", "l")]), + equals(vector_to_square_matrix(0, 0, 0, 3, 3, 0, 0, 0, 0)) ) expect_that( - am(g[c("a", "a", "a", "a"), c("b", "c", "b", "b")]), - equals(mm(2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2)) + canonicalize_matrix(g[c("a", "a", "a", "a"), c("b", "c", "b", "b")]), + equals(vector_to_square_matrix(2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2)) ) - expect_that(am(g[c("h", "q"), c("q", "h")]), equals(mm(136, 0, 0, 0))) + expect_that(canonicalize_matrix(g[c("h", "q"), c("q", "h")]), equals(vector_to_square_matrix(136, 0, 0, 0))) }) -################################################################ +test_that("[[ indexing works with adjacent vertices", { + g <- make_test_named_tree() -test_that("[[ indexing works", { - ## Adjacent vertices expect_that(g[[1, ]], is_equivalent_to(list(a = V(g)[2:3]))) expect_that(g[[, 2]], is_equivalent_to(list(b = V(g)[1]))) expect_that( @@ -138,7 +126,8 @@ test_that("[[ indexing works", { }) test_that("[[ indexing works with symbolic names", { - ## Same with vertex names + g <- make_test_named_tree() + expect_that(g[["a", ]], is_equivalent_to(list(a = V(g)[2:3]))) expect_that(g[[, "b"]], is_equivalent_to(list(b = V(g)[1]))) expect_that( @@ -161,7 +150,8 @@ test_that("[[ indexing works with symbolic names", { }) test_that("[[ indexing works with logical vectors", { - ## Logical vectors + g <- make_test_named_tree() + expect_that( g[[degree(g, mode = "in") == 0, ]], is_equivalent_to(list(a = V(g)[2:3])) @@ -169,7 +159,8 @@ test_that("[[ indexing works with logical vectors", { }) test_that("[[ indexing works with filtering on both ends", { - ## Filtering on both ends + g <- make_test_named_tree() + expect_that( g[[1:10, 1:10]], is_equivalent_to(list( @@ -181,57 +172,57 @@ test_that("[[ indexing works with filtering on both ends", { }) test_that("[[ indexing is consistent with length()", { + g <- make_test_named_tree() expect_that(length(g), equals(vcount(g))) }) -################################################################ - test_that("[ can query edge ids", { - ## Query edge ids + g <- make_test_named_tree() + expect_that(g[1, 2, edges = TRUE], equals(1)) expect_that( - am(g[c(1, 1, 7), c(2, 3, 14), edges = TRUE]), - equals(mm(1, 1, 0, 2, 2, 0, 0, 0, 13)) + canonicalize_matrix(g[c(1, 1, 7), c(2, 3, 14), edges = TRUE]), + equals(vector_to_square_matrix(1, 1, 0, 2, 2, 0, 0, 0, 13)) ) expect_that( - am(g[c(1, 1, 7), c(5, 3, 12), edges = TRUE]), - equals(mm(0, 0, 0, 2, 2, 0, 0, 0, 0)) + canonicalize_matrix(g[c(1, 1, 7), c(5, 3, 12), edges = TRUE]), + equals(vector_to_square_matrix(0, 0, 0, 2, 2, 0, 0, 0, 0)) ) expect_that( - am(g[c(1, 1, 1, 1), c(2, 3, 2, 2), edges = TRUE]), - equals(mm(1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1)) + canonicalize_matrix(g[c(1, 1, 1, 1), c(2, 3, 2, 2), edges = TRUE]), + equals(vector_to_square_matrix(1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1)) ) expect_that( - am(g[c(8, 17), c(17, 8), edges = TRUE]), - equals(mm(16, 0, 0, 0)) + canonicalize_matrix(g[c(8, 17), c(17, 8), edges = TRUE]), + equals(vector_to_square_matrix(16, 0, 0, 0)) ) }) test_that("[ can query edge ids with symbolic names", { - ## The same with symbolic names + g <- make_test_named_tree() + expect_that(g["a", "b", edges = TRUE], equals(1)) expect_that( - am(g[c("a", "a", "g"), c("b", "c", "n"), edges = TRUE]), - equals(mm(1, 1, 0, 2, 2, 0, 0, 0, 13)) + canonicalize_matrix(g[c("a", "a", "g"), c("b", "c", "n"), edges = TRUE]), + equals(vector_to_square_matrix(1, 1, 0, 2, 2, 0, 0, 0, 13)) ) expect_that( - am(g[c("a", "a", "g"), c("e", "c", "l"), edges = TRUE]), - equals(mm(0, 0, 0, 2, 2, 0, 0, 0, 0)) + canonicalize_matrix(g[c("a", "a", "g"), c("e", "c", "l"), edges = TRUE]), + equals(vector_to_square_matrix(0, 0, 0, 2, 2, 0, 0, 0, 0)) ) expect_that( - am(g[c("a", "a", "a", "a"), c("b", "c", "b", "b"), edges = TRUE]), - equals(mm(1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1)) + canonicalize_matrix(g[c("a", "a", "a", "a"), c("b", "c", "b", "b"), edges = TRUE]), + equals(vector_to_square_matrix(1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1)) ) expect_that( - am(g[c("h", "q"), c("q", "h"), edges = TRUE]), - equals(mm(16, 0, 0, 0)) + canonicalize_matrix(g[c("h", "q"), c("q", "h"), edges = TRUE]), + equals(vector_to_square_matrix(16, 0, 0, 0)) ) }) -################################################################ - test_that("[[ can query incident edges", { - ## Incident edges of vertices + g <- make_test_named_tree() + expect_that(g[[1, , edges = TRUE]], is_equivalent_to(list(a = E(g)[1:2]))) expect_that(g[[, 2, edges = TRUE]], is_equivalent_to(list(b = E(g)[1]))) expect_that( @@ -254,7 +245,8 @@ test_that("[[ can query incident edges", { }) test_that("[[ queries edges with vertex names", { - ## Same with vertex names + g <- make_test_named_tree() + expect_that( g[["a", , edges = TRUE]], is_equivalent_to(list(a = E(g)[1:2])) @@ -291,11 +283,9 @@ test_that("[[ queries edges with vertex names", { )) ) }) - -################################################################# - test_that("[ handles from and to properly", { - ## from & to + g <- make_test_named_tree() + g <- make_tree(20) expect_that(g[from = c(1, 2, 2, 3), to = c(3, 4, 8, 7)], equals(c(1, 1, 0, 1))) @@ -337,7 +327,6 @@ test_that("[[ returns vertex and edges sequences", { }) test_that("[[ handles from and to properly even if the graph has conflicting vertex attributes", { - ## from & to g <- make_tree(20) V(g)$i <- 200:219 V(g)$j <- 200:219 diff --git a/tests/testthat/test-indexing2.R b/tests/testthat/test-indexing2.R index 060e6eba79d..864bc03ee40 100644 --- a/tests/testthat/test-indexing2.R +++ b/tests/testthat/test-indexing2.R @@ -1,30 +1,24 @@ -am <- function(x) { - x <- as.matrix(x) - dimnames(x) <- NULL - x -} - test_that("[ can add and delete edges", { g <- make_empty_graph(10) A <- matrix(0, 10, 10) A[1, 2] <- g[1, 2] <- TRUE - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) A[2, 1] <- g[2, 1] <- TRUE - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) g[2, 1] <- NULL A[2, 1] <- 0 - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) A[1, 2] <- g[1, 2] <- FALSE - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) g <- make_empty_graph(10) A <- matrix(0, 10, 10) A[-1, 1] <- g[-1, 1] <- 1 - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) }) test_that("[ can set weights and delete weighted edges", { @@ -32,20 +26,20 @@ test_that("[ can set weights and delete weighted edges", { A <- matrix(0, 10, 10) g <- set_edge_attr(g, "weight", c(), 1) A[1, 2] <- g[1, 2] <- 1 - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) A[2, 1] <- g[2, 1] <- 2 - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) A[1, 2] <- g[1, 2] <- 3 - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) A[1:2, 2:3] <- g[1:2, 2:3] <- -1 - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) g[1, 2] <- NULL A[1, 2] <- 0 - expect_that(am(g[]), equals(A)) + expect_that(canonicalize_matrix(g[]), equals(A)) }) test_that("[ can add edges and ste weights via vertex names", { @@ -56,15 +50,15 @@ test_that("[ can add edges and ste weights via vertex names", { A["a", "b"] <- g["a", "b"] <- TRUE A["b", "c"] <- g["b", "c"] <- TRUE - expect_that(am(g[]), equals(am(A))) + expect_that(canonicalize_matrix(g[]), equals(canonicalize_matrix(A))) A[c("a", "f"), c("f", "a")] <- g[c("a", "f"), c("f", "a")] <- TRUE - expect_that(am(g[]), equals(am(A))) + expect_that(canonicalize_matrix(g[]), equals(canonicalize_matrix(A))) A[A == 1] <- NA A[c("a", "c", "h"), c("a", "b", "c")] <- g[c("a", "c", "h"), c("a", "b", "c"), attr = "weight"] <- 3 - expect_that(am(g[]), equals(am(A))) + expect_that(canonicalize_matrix(g[]), equals(canonicalize_matrix(A))) }) test_that("[ and the from-to notation", { @@ -79,7 +73,7 @@ test_that("[ and the from-to notation", { g[from = c("a", "c", "h", "d"), to = c("a", "b", "c", "e")], equals(c(1, 1, 1, 0)) ) - expect_that(am(g[]), equals(am(A))) + expect_that(canonicalize_matrix(g[]), equals(canonicalize_matrix(A))) g[from = c("a", "c", "h", "a"), to = c("a", "a", "a", "e"), attr = "weight"] <- 3 A[A != 0] <- NA @@ -88,7 +82,7 @@ test_that("[ and the from-to notation", { from = c("a", "c", "h", "a", "c", "c"), to = c("a", "a", "a", "e", "f", "b") ], equals(c(3, 3, 3, 3, 0, NA))) - expect_that(am(g[]), equals(am(A))) + expect_that(canonicalize_matrix(g[]), equals(canonicalize_matrix(A))) }) test_that("[ and from-to with multiple values", { @@ -109,5 +103,5 @@ test_that("[ and from-to with multiple values", { from = c("a", "c", "h", "a", "c", "c"), to = c("a", "a", "a", "e", "f", "b") ], equals(c(5:8, 0, NA))) - expect_that(am(g[]), equals(am(A))) + expect_that(canonicalize_matrix(g[]), equals(canonicalize_matrix(A))) })