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
16 changes: 16 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ local_rng_version <- function(version, .local_envir = parent.frame()) {
expect_isomorphic <- function(g1, g2) {
expect_true(isomorphic(g1, g2))
}

expect_vcount <- function(graph, expected, ...) {
expect_equal(object = vcount(graph), expected = expected, ...)
}

expect_ecount <- function(graph, expected, ...) {
expect_equal(object = ecount(graph), expected = expected, ...)
}

expect_identical_graphs <- function(g1, g2, ...) {
expect_true(identical_graphs(g1, g2, ...))
}

expect_not_identical_graphs <- function(g1, g2, ...) {
expect_false(identical_graphs(g1, g2, ...))
}
24 changes: 12 additions & 12 deletions tests/testthat/test-adjacency.R
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,12 @@ test_that("sparse/dense matrices no loops works",{
A <- diag(1, 5)
A[1, 2] <- 1
g <- graph_from_adjacency_matrix(A, diag = FALSE)
expect_equal(ecount(g), 1)
expect_ecount(g, 1)
expect_equal(get_edge_ids(g, c(1, 2)), 1)

A <- as(A, "dgCMatrix")
g <- graph_from_adjacency_matrix(A, diag = FALSE)
expect_equal(ecount(g), 1)
expect_ecount(g, 1)
expect_equal(get_edge_ids(g,c(1, 2)), 1)

})
Expand All @@ -678,14 +678,14 @@ test_that("sparse/dense matrices multiple works",{
A <- matrix(0, 5, 5)
A[1, 2] <- 3
g <- graph_from_adjacency_matrix(A, diag = FALSE, weighted = FALSE)
expect_equal(ecount(g), 3)
expect_ecount(g, 3)
expect_equal(as_edgelist(g), matrix(c(1, 2), 3, 2, byrow = TRUE))

A <- as(A,"dgCMatrix")
g <- graph_from_adjacency_matrix(A,diag = FALSE)
expect_equal(ecount(g), 3)
expect_ecount(g, 3)
expect_equal(as_edgelist(g), matrix(c(1, 2), 3, 2, byrow = TRUE))

})

test_that("sparse/dense matrices min/max/plus",{
Expand All @@ -695,7 +695,7 @@ test_that("sparse/dense matrices min/max/plus",{
A[2, 1] <- 2
g <- graph_from_adjacency_matrix(A, diag = FALSE, mode = "max", weighted = TRUE)
expect_equal(E(g)$weight[1], 3)

g <- graph_from_adjacency_matrix(A, diag = FALSE, mode = "min", weighted = TRUE)
expect_equal(E(g)$weight[1], 2)

Expand All @@ -705,12 +705,12 @@ test_that("sparse/dense matrices min/max/plus",{
A <- as(A,"dgCMatrix")
g <- graph_from_adjacency_matrix(A, diag = FALSE, mode = "max", weighted = TRUE)
expect_equal(E(g)$weight[1], 3)

g <- graph_from_adjacency_matrix(A, diag = FALSE, mode = "min", weighted = TRUE)
expect_equal(E(g)$weight[1], 2)

g <- graph_from_adjacency_matrix(A, diag = FALSE, mode = "plus", weighted = TRUE)
expect_equal(E(g)$weight[1], 5)
})


})
2 changes: 1 addition & 1 deletion tests/testthat/test-coloring.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test_that("simplify_and_colorize works", {
result <- simplify_and_colorize(g)

expect_true(is_simple(result))
expect_equal(vcount(result), vcount(g))
expect_vcount(result, vcount(g))
expect_equal(as_edgelist(result), matrix(c(1:4, 2:5), ncol = 2))
expect_equal(V(result)$color, c(0, 0, 0, 0, 1))
expect_equal(E(result)$color, c(1, 4, 1, 2))
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-constructor-modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test_that("without_attr", {
withr::local_seed(42)
g2 <- sample_(gnp(10, 2 / 10), without_attr())

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)
expect_equal(graph_attr_names(g2), character())
expect_equal(vertex_attr_names(g2), character())
expect_equal(edge_attr_names(g2), character())
Expand All @@ -25,7 +25,7 @@ test_that("without_loops", {
without_loops()
)

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)
expect_true(all(!which_loop(g2)))
})

Expand All @@ -39,7 +39,7 @@ test_that("without_multiple", {
without_multiples()
)

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)
expect_true(all(!which_multiple(g2)))
})

Expand All @@ -52,7 +52,7 @@ test_that("simplified", {
simplified()
)

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)
expect_true(all(!which_multiple(g2)))
expect_true(all(!which_loop(g2)))
})
Expand All @@ -71,7 +71,7 @@ test_that("with_vertex_", {
)
)

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)
expect_equal(V(g2)$color, rep("red", gorder(g2)))
expect_equal(V(g2)$foo, paste0("xx", 1:3))
})
Expand All @@ -90,7 +90,7 @@ test_that("with_edge_", {
)
)

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)
expect_equal(E(g)$color, E(g2)$color)
expect_equal(E(g)$foo, E(g2)$foo)
})
Expand All @@ -109,7 +109,7 @@ test_that("with_graph_", {
)
)

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)
expect_equal(g$color, g2$color)
expect_equal(g$foo, g2$foo)
})
Expand All @@ -126,5 +126,5 @@ test_that("adding and removing attributes", {
V(g)$foo <- "bar"
g <- delete_vertex_attr(g, "foo")

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-degseq.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ test_that("realize_degseq supports the make_(...) syntax", {
expect_equal(degree(g1), degs)
expect_equal(degree(g2), degs)

expect_true(identical_graphs(g1, g2))
expect_identical_graphs(g1, g2)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-dot.product.game.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ test_that("Dot product rng works", {

vecs <- replicate(100, rep(sqrt(1 / 8), 4))
g <- sample_dot_product(vecs)
expect_equal(ecount(g), 2454)
expect_ecount(g, 2454)

g2 <- sample_dot_product(vecs, directed = TRUE)
expect_equal(ecount(g2), 4938)
expect_ecount(g2, 4938)
})

test_that("Dot product rng gives warnings", {
Expand Down
66 changes: 33 additions & 33 deletions tests/testthat/test-games.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ test_that("sample_degseq supports the sample_(...) syntax", {
expect_equal(degree(g1), degs)
expect_equal(degree(g2), degs)

expect_false(identical_graphs(g1, g2))
expect_not_identical_graphs(g1, g2)
})

test_that("sample_degseq works() -- old method names", {
Expand Down Expand Up @@ -252,18 +252,18 @@ test_that("sample_pa() works", {
withr::local_seed(20240209)

g_pa <- sample_pa(100, m = 2)
expect_equal(ecount(g_pa), 197)
expect_equal(vcount(g_pa), 100)
expect_ecount(g_pa, 197)
expect_vcount(g_pa, 100)
expect_true(is_simple(g_pa))

g_pa2 <- sample_pa(100, m = 2, algorithm = "psumtree-multiple")
expect_equal(ecount(g_pa2), 198)
expect_equal(vcount(g_pa2), 100)
expect_ecount(g_pa2, 198)
expect_vcount(g_pa2, 100)
expect_false(is_simple(g_pa2))

g_pa3 <- sample_pa(100, m = 2, algorithm = "bag")
expect_equal(ecount(g_pa3), 198)
expect_equal(vcount(g_pa3), 100)
expect_ecount(g_pa3, 198)
expect_vcount(g_pa3, 100)
expect_false(is_simple(g_pa3))

g_pa4 <- sample_pa(3, out.seq = 0:2, directed = FALSE)
Expand All @@ -277,8 +277,8 @@ test_that("sample_pa can start from a graph", {
withr::local_seed(20231029)

g_pa1 <- sample_pa(10, m = 1, algorithm = "bag", start.graph = make_empty_graph(5))
expect_equal(ecount(g_pa1), 5)
expect_equal(vcount(g_pa1), 10)
expect_ecount(g_pa1, 5)
expect_vcount(g_pa1, 10)

is_degree_zero <- (degree(g_pa1) == 0)
expect_true(sum(is_degree_zero) %in% 0:4)
Expand Down Expand Up @@ -329,16 +329,16 @@ test_that("sample_bipartite works -- undirected gnp", {

g_rand_bip <- sample_bipartite(10, 5, type = "gnp", p = .1)
expect_equal(g_rand_bip$name, "Bipartite Gnp random graph")
expect_equal(vcount(g_rand_bip), 15)
expect_equal(ecount(g_rand_bip), 7)
expect_vcount(g_rand_bip, 15)
expect_ecount(g_rand_bip, 7)
expect_true(bipartite_mapping(g_rand_bip)$res)
expect_false(is_directed(g_rand_bip))
})

test_that("sample_bipartite works -- directed gnp", {
g_rand_bip_dir <- sample_bipartite(10, 5, type = "gnp", p = .1, directed = TRUE)
expect_equal(vcount(g_rand_bip_dir), 15)
expect_equal(ecount(g_rand_bip_dir), 6)
expect_vcount(g_rand_bip_dir, 15)
expect_ecount(g_rand_bip_dir, 6)
expect_true(bipartite_mapping(g_rand_bip_dir)$res)
expect_true(is_directed(g_rand_bip_dir))
expect_output(print_all(g_rand_bip_dir), "5->11")
Expand All @@ -349,22 +349,22 @@ test_that("sample_bipartite works -- directed gnp", {

test_that("sample_bipartite works -- undirected gnm", {
g_rand_bip_gnm <- sample_bipartite(10, 5, type = "gnm", m = 8)
expect_equal(vcount(g_rand_bip_gnm), 15)
expect_equal(ecount(g_rand_bip_gnm), 8)
expect_vcount(g_rand_bip_gnm, 15)
expect_ecount(g_rand_bip_gnm, 8)
expect_true(bipartite_mapping(g_rand_bip_gnm)$res)
expect_false(is_directed(g_rand_bip_gnm))
})
test_that("sample_bipartite works -- directed gnm", {
g_rand_bip_gnm_dir <- sample_bipartite(10, 5, type = "gnm", m = 8, directed = TRUE)
expect_equal(vcount(g_rand_bip_gnm_dir), 15)
expect_equal(ecount(g_rand_bip_gnm_dir), 8)
expect_vcount(g_rand_bip_gnm_dir, 15)
expect_ecount(g_rand_bip_gnm_dir, 8)
expect_true(bipartite_mapping(g_rand_bip_gnm_dir)$res)
expect_true(is_directed(g_rand_bip_gnm_dir))
expect_output(print_all(g_rand_bip_gnm_dir), "5->12")

g_rand_bip_gnm_in <- sample_bipartite(10, 5, type = "gnm", m = 8, directed = TRUE, mode = "in")
expect_equal(vcount(g_rand_bip_gnm_in), 15)
expect_equal(ecount(g_rand_bip_gnm_in), 8)
expect_vcount(g_rand_bip_gnm_in, 15)
expect_ecount(g_rand_bip_gnm_in, 8)
expect_true(bipartite_mapping(g_rand_bip_gnm_in)$res)
expect_true(is_directed(g_rand_bip_gnm_in))
expect_output(print_all(g_rand_bip_gnm_in), "12->10")
Expand All @@ -373,13 +373,13 @@ test_that("sample_bipartite works -- directed gnm", {
type = "gnp", p = 0.9999, directed = TRUE,
mode = "all"
)
expect_equal(ecount(g_rand_bip_full), 100)
expect_ecount(g_rand_bip_full, 100)

g_rand_bip_edges <- sample_bipartite(10, 5,
type = "gnm", m = 99, directed = TRUE,
mode = "all"
)
expect_equal(ecount(g_rand_bip_edges), 99)
expect_ecount(g_rand_bip_edges, 99)
})


Expand Down Expand Up @@ -437,16 +437,16 @@ test_that("sample_correlated_gnp corner cases work", {
expect_true(is_full(cor_gnp_full))

cor_gnp_empty <- sample_correlated_gnp(gnp_graph, corr = 0.000001, p = 0.0000001)
expect_equal(ecount(cor_gnp_empty), 0)
expect_equal(vcount(cor_gnp_empty), 10)
expect_ecount(cor_gnp_empty, 0)
expect_vcount(cor_gnp_empty, 10)

gnp_graph_directed <- sample_gnp(10, .3, directed = TRUE)
cor_gnp_directed <- sample_correlated_gnp(gnp_graph_directed, corr = 0.000001, p = .99999999)
expect_true(is_full(cor_gnp_directed))

cor_gnp_directed_empty <- sample_correlated_gnp(gnp_graph_directed, corr = 0.000001, p = 0.0000001)
expect_equal(ecount(cor_gnp_directed_empty), 0)
expect_equal(vcount(cor_gnp_directed_empty), 10)
expect_ecount(cor_gnp_directed_empty, 0)
expect_vcount(cor_gnp_directed_empty, 10)
})

test_that("permutation works for sample_correlated_gnp", {
Expand Down Expand Up @@ -474,29 +474,29 @@ test_that("HSBM works", {
), nrow = 3)

g_hsbm1 <- sample_hierarchical_sbm(100, 10, rho = c(3, 3, 4) / 10, C = C, p = 0)
expect_equal(ecount(g_hsbm1), 172)
expect_equal(vcount(g_hsbm1), 100)
expect_ecount(g_hsbm1, 172)
expect_vcount(g_hsbm1, 100)
expect_false(is_directed(g_hsbm1))

withr::local_seed(42)

g_hsbm2 <- sample_hierarchical_sbm(100, 10, rho = c(3, 3, 4) / 10, C = C, p = 1)
expect_equal(ecount(g_hsbm2), ecount(g_hsbm1) + 10 * 9 * (90 + 10) / 2)
expect_equal(vcount(g_hsbm2), 100)
expect_ecount(g_hsbm2, ecount(g_hsbm1) + 10 * 9 * (90 + 10) / 2)
expect_vcount(g_hsbm2, 100)
expect_true(is_simple(g_hsbm2))

withr::local_seed(42)

g_hsbm3 <- sample_hierarchical_sbm(100, 10, rho = c(3, 3, 4) / 10, C = C, p = 1e-15)
expect_equal(ecount(g_hsbm3), ecount(g_hsbm1))
expect_equal(vcount(g_hsbm3), 100)
expect_ecount(g_hsbm3, ecount(g_hsbm1))
expect_vcount(g_hsbm3, 100)
expect_true(is_simple(g_hsbm3))

withr::local_seed(42)

g_hsbm4 <- sample_hierarchical_sbm(100, 10, rho = c(3, 3, 4) / 10, C = C, p = 1 - 1e-15)
expect_equal(ecount(g_hsbm4), ecount(g_hsbm2))
expect_equal(vcount(g_hsbm4), 100)
expect_ecount(g_hsbm4, ecount(g_hsbm2))
expect_vcount(g_hsbm4, 100)
expect_true(is_simple(g_hsbm4))
})

Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-identical_graphs.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ test_that("identical_graphs works", {
g <- make_ring(5)
g2 <- make_ring(5)

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)

g2 <- make_ring(6)

expect_false(identical_graphs(g, g2))
expect_not_identical_graphs(g, g2)
})

test_that("identical_graphs considers attributes", {
g <- sample_pa(10)
g2 <- g

expect_true(identical_graphs(g, g2))
expect_identical_graphs(g, g2)

g2$m <- 2

expect_false(identical_graphs(g, g2))
expect_true(identical_graphs(g, g2, attrs = FALSE))
expect_not_identical_graphs(g, g2)
expect_identical_graphs(g, g2, attrs = FALSE)
})
Loading