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
7 changes: 7 additions & 0 deletions tests/testthat/test-adjacency.R
Original file line number Diff line number Diff line change
Expand Up @@ -1002,3 +1002,10 @@ test_that("from_adjacency() covers tail args by name and recovers positional cal
make_(from_adjacency(M, mode = "undirected"))
)
})

test_that("graph_from_adjacency_matrix() creates edges row by row", {
# Pins the edge order of the C core for directed input.
m <- matrix(c(0, 1, 1, 1, 0, 1, 0, 0, 0), 3)
g <- graph_from_adjacency_matrix(m)
expect_equal(as_edgelist(g), rbind(c(1, 2), c(2, 1), c(3, 1), c(3, 2)))
})
38 changes: 37 additions & 1 deletion tests/testthat/test-centrality.R
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,20 @@ test_that("eigen_centrality() works", {
expect_ev(M, v, lambda, eps)
}

# Most of these graphs are disconnected, and warn that some centralities
# are nearly zero; no other warning is expected.
for (i in 1:1000) {
G <- sample_gnm(10, sample(1:20, 1))
ev <- eigen_centrality(G)
ev <- withCallingHandlers(
eigen_centrality(G),
warning = function(w) {
expect_match(
conditionMessage(w),
"eigenvector centralities are nearly zero"
)
invokeRestart("muffleWarning")
}
)
expect_good(
as_adjacency_matrix(G, sparse = FALSE),
ev$vector,
Expand Down Expand Up @@ -1258,3 +1269,28 @@ test_that("alpha_centrality() covers migrated tail args and positional recovery"
)
expect_identical(res_legacy, alpha_centrality(dag, V(dag), alpha = 0.75))
})

# ---- C core behaviour pins ----------------------------------------------
# These pin the behaviour of the vendored C core,
# so that the vendor commit that changes it also flips the expectation.

test_that("hits_scores() on an edgeless graph gives NaN silently", {
g <- make_empty_graph(3)
expect_silent(res <- hits_scores(g))
expect_identical(res$value, NaN)
})

test_that("hits_scores() on an undirected graph does not warn", {
expect_silent(res <- hits_scores(make_ring(3)))
expect_equal(res$value, 4)
})

test_that("eigen_centrality() on a disconnected graph does not warn", {
g <- make_star(3, mode = "undirected") + make_ring(3)
expect_silent(res <- eigen_centrality(g))
expect_equal(res$vector, c(0, 0, 0, 1, 1, 1))

g <- make_ring(3) + make_ring(4)
expect_silent(res <- eigen_centrality(g))
expect_equal(res$vector, rep(1, 7), tolerance = 1e-5)
})
7 changes: 7 additions & 0 deletions tests/testthat/test-community.R
Original file line number Diff line number Diff line change
Expand Up @@ -1042,3 +1042,10 @@ test_that("modularity_matrix() covers migrated tail args and positional recovery
modularity_matrix(karate, rep(1, vcount(karate)), rep(1, ecount(karate)))
})
})

test_that("cluster_infomap() code length on the karate graph", {
# Pins the vendored Infomap implementation.
karate <- make_graph("Zachary")
res <- cluster_infomap(karate, nb.trials = 1)
expect_equal(code_len(res), 4.606059, tolerance = 1e-6)
})
10 changes: 10 additions & 0 deletions tests/testthat/test-games.R
Original file line number Diff line number Diff line change
Expand Up @@ -1770,3 +1770,13 @@ test_that("sample_correlated_gnp() recovers positional p", {
sample_correlated_gnp(base_graph, 0.8, p = 0.3)
)
})

test_that("sample_islands() random stream for a fixed seed", {
# Pins the random numbers the C core draws for this generator.
igraph_local_seed(42)
g <- sample_islands(2, 3, 0.5, 1)
expect_equal(
as_edgelist(g),
rbind(c(1, 3), c(2, 3), c(3, 6), c(4, 5), c(4, 6))
)
})
7 changes: 7 additions & 0 deletions tests/testthat/test-incidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,10 @@ test_that("graph_from_biadjacency_matrix() covers add.names by name and recovers
)
expect_true(is_directed(res))
})

test_that("graph_from_biadjacency_matrix() creates edges row by row", {
# Pins the edge order of the C core for dense unweighted input.
m <- matrix(c(1, 1, 1, 0), 2)
g <- graph_from_biadjacency_matrix(m)
expect_equal(as_edgelist(g), rbind(c(1, 3), c(1, 4), c(2, 3)))
})
22 changes: 22 additions & 0 deletions tests/testthat/test-layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,25 @@ test_that("norm_coords() recovers positional calls", {
# xmin = 0 rescales the first column into [0, 1].
expect_equal(range(res[, 1]), c(0, 1))
})

test_that("layout_with_gem() random stream for a fixed seed", {
# Pins the random numbers the C core draws for this layout.
igraph_local_seed(42)
l <- layout_with_gem(make_ring(3))
expect_equal(
l,
cbind(c(130.218, -34.753, 88.118), c(238.780, 196.748, 71.715)),
tolerance = 1e-4
)
})

test_that("layout_with_dh() random stream for a fixed seed", {
# Pins the random numbers the C core draws for this layout.
igraph_local_seed(42)
l <- layout_with_dh(make_ring(3))
expect_equal(
l,
cbind(c(0.444, -1.310, -0.350), c(-7.937, -7.812, -6.384)),
tolerance = 1e-3
)
})
Loading