1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
context("attributes")
test_that("assigning and querying attributes work", {
library(igraph)
## Create a small ring graph, assign attributes
ring <- graph_from_literal( A-B-C-D-E-F-G-A )
E(ring)$weight <- seq_len(ecount(ring))
## Query attributes
expect_that(V(ring)$name, equals(LETTERS[seq_len(vcount(ring))]))
expect_that(E(ring)$weight, equals(seq_len(ecount(ring))))
})
test_that("brackering works", {
library(igraph)
g <- graph(c(1,2, 1,3, 3,4))
g <- set_vertex_attr(g, name="weight", value=1:vcount(g))
g <- set_edge_attr(g, name="weight", value=1:ecount(g))
g <- set_graph_attr(g, name="name", "foo")
graph2 <- set_vertex_attr(g, name="weight",
value=rep(1, vcount(g)))
graph2 <- set_edge_attr(g, name="weight",
value=rep(1, ecount(g)))
graph2 <- set_graph_attr(g, name="name", "foobar")
expect_that(vertex_attr(g, name="weight"),
equals(1:4))
expect_that(edge_attr(g, name="weight"),
equals(1:3))
expect_that(graph_attr(g, name="name"), equals("foo"))
})
test_that("brackering works with a function", {
library(igraph)
library(testthat)
g <- graph(c(1,2, 1,3, 3,4))
g <- set_vertex_attr(g, name="weight", value=1:vcount(g))
g <- set_edge_attr(g, name="weight", value=1:ecount(g))
g <- set_graph_attr(g, name="name", "foo")
run.test <- function(graph) {
graph2 <- set_vertex_attr(graph, name="weight",
value=rep(1, vcount(graph)))
graph2 <- set_edge_attr(graph, name="weight",
value=rep(1, ecount(graph)))
graph2 <- set_graph_attr(graph, name="name", "foobar")
}
g2 <- run.test(g)
expect_that(vertex_attr(g, name="weight"),
equals(1:4))
expect_that(edge_attr(g, name="weight"),
equals(1:3))
expect_that(graph_attr(g, name="name"), equals("foo"))
})
test_that("brackering works with shortcuts", {
library(igraph)
g <- graph(c(1,2, 1,3, 3,4))
g <- set_vertex_attr(g, name="weight", value=1:vcount(g))
g <- set_edge_attr(g, name="weight", value=1:ecount(g))
g <- set_graph_attr(g, name="name", "foo")
run.test <- function(graph) {
V(graph)$weight <- rep(1, vcount(graph))
E(graph)$weight <- rep(1, ecount(graph))
graph$name <- "foobar"
}
g2 <- run.test(g)
expect_that(vertex_attr(g, name="weight"),
equals(1:4))
expect_that(edge_attr(g, name="weight"),
equals(1:3))
expect_that(graph_attr(g, name="name"), equals("foo"))
})
## TODO: subsetting
test_that("we can query all attributes at once", {
g <- graph(c(1,2, 1,3, 2,4))
expect_equal(graph_attr(g), structure(list(), .Names = character(0)))
expect_equal(vertex_attr(g), list())
expect_equal(edge_attr(g), list())
g$name <- "toy"
g$layout <- cbind(1:4, 1:4)
V(g)$name <- letters[1:4]
V(g)$color <- rainbow(4)
E(g)$weight <- 1:3
E(g)$label <- LETTERS[1:3]
expect_equal(graph_attr(g), list(name = "toy", layout = cbind(1:4, 1:4)))
expect_equal(vertex_attr(g), list(name = letters[1:4], color = rainbow(4)))
expect_equal(edge_attr(g), list(weight = 1:3, label = LETTERS[1:3]))
})
test_that("we can query single attributes with the generic functions", {
g <- graph(c(1,2, 1,3, 2,4))
g$name <- "toy"
g$layout <- cbind(1:4, 1:4)
V(g)$name <- letters[1:4]
V(g)$color <- rainbow(4)
E(g)$weight <- 1:3
E(g)$label <- LETTERS[1:3]
expect_equal(graph_attr(g, "name"), "toy")
expect_equal(graph_attr(g, "layout"), cbind(1:4, 1:4))
expect_equal(vertex_attr(g, "name"), letters[1:4])
expect_equal(vertex_attr(g, "color"), rainbow(4))
expect_equal(edge_attr(g, "weight"), 1:3)
expect_equal(edge_attr(g, "label"), LETTERS[1:3])
})
test_that("we can query a subset of vertices", {
g <- graph(c(1,2, 1,3, 2,4))
V(g)$name <- letters[1:4]
V(g)$color <- as.list(rainbow(4))
E(g)$weight <- 1:3
E(g)$label <- as.list(LETTERS[1:3])
expect_equal(vertex_attr(g, "name", c(1,3)), letters[c(1,3)])
expect_equal(vertex_attr(g, "color", c("a", "c")),
as.list(rainbow(4))[c(1,3)])
expect_equal(edge_attr(g, "weight", 2:3), 2:3)
expect_equal(edge_attr(g, "label", 2:3), as.list(LETTERS[1:3])[2:3])
})
test_that("we can set all attributes at once", {
g <- graph(c(1,2, 1,3, 2,4))
g$name <- "toy"
g$layout <- cbind(1:4, 1:4)
V(g)$name <- letters[1:4]
V(g)$color <- as.list(rainbow(4))
E(g)$weight <- 1:3
E(g)$label <- as.list(LETTERS[1:3])
g2 <- graph(c(2,1, 3,1, 4,1))
graph_attr(g2) <- graph_attr(g)
expect_equal(graph_attr(g2), graph_attr(g))
vertex_attr(g2) <- vertex_attr(g)
expect_equal(vertex_attr(g2), vertex_attr(g))
edge_attr(g2) <- edge_attr(g)
expect_equal(edge_attr(g2), edge_attr(g))
})
test_that("we can set all attributes some vertices/edges", {
g <- graph(c(1,2, 1,3, 2,4))
V(g)$name <- letters[1:4]
V(g)$color <- as.list(rainbow(4))
E(g)$weight <- 1:3
E(g)$label <- as.list(LETTERS[1:3])
g2 <- graph(c(2,1, 3,1, 4,1, 2,5, 3,6))
vertex_attr(g2, index = c(1, 2, 4, 5)) <- vertex_attr(g)
expect_equal(vertex_attr(g2), list(name = c("a", "b", NA_character_,
"c", "d", NA_character_),color = list(rainbow(4)[1], rainbow(4)[2], NULL,
rainbow(4)[3], rainbow(4)[4], NULL)))
edge_attr(g2, index = c(1, 3, 5)) <- edge_attr(g)
expect_equal(edge_attr(g2), list(weight = c(1L, NA_integer_, 2L,
NA_integer_, 3L), label = list("A", NULL, "B", NULL, "C")))
})
test_that("cannot use vs/es from another graph", {
g <- graph.ring(10)
g2 <- g + 1
v <- V(g)[1:4]
expect_error(g2 - v, "Cannot use a vertex sequence from another graph")
e <- E(g)[1:2]
expect_error(g2 - e, "Cannot use an edge sequence from another graph")
})
|