Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tests/testthat/helper-indexing.R
Original file line number Diff line number Diff line change
@@ -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
}
147 changes: 68 additions & 79 deletions tests/testthat/test-indexing.R
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -161,15 +150,17 @@ 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]))
)
})

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(
Expand All @@ -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(
Expand All @@ -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]))
Expand Down Expand Up @@ -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)))

Expand Down Expand Up @@ -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
Expand Down
Loading