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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
test_that("is_tree works for non-trees", {
g <- make_graph("zachary")
expect_false(is_tree(g))
expect_equal(
ignore_attr = TRUE,
is_tree(g, details = TRUE),
list(res = FALSE, root = V(g)[1])
)
g <- sample_pa(15, m = 3)
expect_false(is_tree(g))
expect_false(is_tree(g, details = TRUE)$res)
})
test_that("is_tree works for undirected trees", {
# g <- permute(make_tree(7, 2), c(5, 2, 3, 4, 1, 6, 7))
g <- make_tree(7, 2)
expect_true(is_tree(g))
expect_equal(
ignore_attr = TRUE,
is_tree(g, details = TRUE),
list(res = TRUE, root = V(g)[1])
)
})
test_that("is_tree works for directed in-trees", {
g <- permute(make_tree(7, 2, mode = "in"), c(5, 2, 3, 4, 1, 6, 7))
expect_true(is_tree(g, mode = "in"))
expect_equal(
ignore_attr = TRUE,
is_tree(g, mode = "in", details = TRUE),
list(res = TRUE, root = V(g)[5])
)
expect_true(is_tree(g, mode = "all"))
expect_equal(
ignore_attr = TRUE,
is_tree(g, mode = "all", details = TRUE),
list(res = TRUE, root = V(g)[1])
)
expect_false(is_tree(g, mode = "out"))
expect_false(is_tree(g, mode = "out", details = TRUE)$res)
})
test_that("is_tree works for directed out-trees", {
g <- permute(make_tree(7, 2, mode = "out"), c(3, 2, 1, 4, 5, 6, 7))
expect_true(is_tree(g, mode = "out"))
expect_equal(
ignore_attr = TRUE,
is_tree(g, mode = "out", details = TRUE),
list(res = TRUE, root = V(g)[3])
)
expect_true(is_tree(g, mode = "all"))
expect_equal(
ignore_attr = TRUE,
is_tree(g, mode = "all", details = TRUE),
list(res = TRUE, root = V(g)[1])
)
expect_false(is_tree(g, mode = "in"))
expect_false(is_tree(g, mode = "in", details = TRUE)$res)
})
test_that("the null graph is not a tree", {
expect_false(is_tree(make_empty_graph(0)))
})
test_that("a graph with a single vertex and no edges is tree", {
expect_true(is_tree(make_empty_graph(1)))
})
test_that("is_forest takes edge directions into account correctly", {
g <- make_graph(c(1, 2, 2, 3, 2, 4, 5, 4), n = 6, directed = TRUE)
expect_true(is_forest(g, mode = "all"))
expect_false(is_forest(g, mode = "out"))
expect_false(is_forest(g, mode = "in"))
})
test_that("the null graph is a forest", {
expect_true(is_forest(make_empty_graph(0)))
})
test_that("a graph with a single vertex and no edges is a forest", {
expect_true(is_forest(make_empty_graph(1)))
})
test_that("to_prufer and make_from_prufer works for trees", {
g <- make_tree(13, 3, mode = "undirected")
seq <- to_prufer(g)
g2 <- make_from_prufer(seq)
expect_isomorphic(g, g2)
g <- make_tree(13, 3, mode = "out")
seq <- to_prufer(g)
g2 <- make_from_prufer(seq)
g3 <- as_undirected(g)
expect_isomorphic(g2, g3)
g <- make_tree(13, 3, mode = "in")
seq <- to_prufer(g)
g2 <- make_from_prufer(seq)
g3 <- as_undirected(g)
expect_isomorphic(g2, g3)
})
test_that("make_(from_prufer(...)) works", {
g <- make_tree(13, 3, mode = "undirected")
seq <- to_prufer(g)
g2 <- make_(from_prufer(seq))
expect_isomorphic(g, g2)
})
test_that("to_prufer prints an error for non-trees", {
expect_error(to_prufer(make_graph("zachary")), "must be a tree")
})
test_that("sample_tree works", {
g <- sample_tree(100)
expect_false(is_directed(g))
expect_ecount(g, 99)
expect_vcount(g, 100)
expect_true(is_tree(g))
g <- sample_tree(50, directed = T)
expect_true(is_directed(g))
expect_ecount(g, 49)
expect_vcount(g, 50)
expect_true(is_tree(g))
g <- sample_tree(200, method = "prufer")
expect_false(is_directed(g))
expect_ecount(g, 199)
expect_vcount(g, 200)
expect_true(is_tree(g))
g <- sample_tree(200, method = "lerw")
expect_false(is_directed(g))
expect_ecount(g, 199)
expect_vcount(g, 200)
expect_true(is_tree(g))
})
test_that("sample_(tree(...)) works", {
g <- sample_(tree(200, method = "prufer"))
expect_false(is_directed(g))
expect_ecount(g, 199)
expect_vcount(g, 200)
expect_true(is_tree(g))
g2 <- sample_(tree(200, method = "prufer"))
expect_false(is_directed(g2))
expect_ecount(g2, 199)
expect_vcount(g2, 200)
expect_true(is_tree(g2))
expect_not_identical_graphs(g, g2)
})
test_that("sample_tree yields a singleton graph for n=1", {
g <- sample_tree(1)
expect_false(is_directed(g))
expect_ecount(g, 0)
expect_vcount(g, 1)
expect_true(is_tree(g))
})
test_that("sample_tree yields a null graph for n=0", {
g <- sample_tree(0)
expect_false(is_directed(g))
expect_ecount(g, 0)
expect_vcount(g, 0)
expect_false(is_tree(g)) # edge case, the null graph is not a tree even though it was generated by sample_tree()
})
test_that("sample_tree throws an error for the Prufer method with directed graphs", {
expect_error(sample_tree(10, method = "prufer", directed = T), "nvalid value")
})
test_that("sample_spanning_tree works for connected graphs", {
g <- make_full_graph(8)
edges <- sample_spanning_tree(g)
expect_equal(length(edges), 7)
sg <- subgraph_from_edges(g, edges)
expect_vcount(sg, 8)
expect_ecount(sg, 7)
expect_true(is_tree(sg))
})
test_that("sample_spanning_tree works for disconnected graphs", {
g <- make_full_graph(8) %du% make_full_graph(5)
edges <- sample_spanning_tree(g, vid = 8)
sg <- subgraph_from_edges(g, edges, delete.vertices = TRUE)
expect_vcount(sg, 8)
expect_ecount(sg, 7)
expect_true(is_tree(sg))
edges <- sample_spanning_tree(g, vid = 9)
sg <- subgraph_from_edges(g, edges, delete.vertices = TRUE)
expect_vcount(sg, 5)
expect_ecount(sg, 4)
expect_true(is_tree(sg))
edges <- sample_spanning_tree(g)
sg <- subgraph_from_edges(g, edges, delete.vertices = FALSE)
expect_vcount(sg, 13)
expect_ecount(sg, 11)
expect_true(is_tree(induced_subgraph(sg, 1:8)))
expect_true(is_tree(induced_subgraph(sg, 9:13)))
})
test_that("subgraph.edges deprecation", {
local_igraph_options(print.id = FALSE)
withr::local_seed(42)
g <- make_full_graph(8) %du% make_full_graph(5)
edges <- sample_spanning_tree(g)
expect_snapshot(subgraph.edges(g, edges, delete.vertices = FALSE))
})
|