diff --git a/tests/testthat/test-average.path.length.R b/tests/testthat/test-average.path.length.R deleted file mode 100644 index 5a40827e6f3..00000000000 --- a/tests/testthat/test-average.path.length.R +++ /dev/null @@ -1,69 +0,0 @@ -test_that("mean_distance works", { - apl <- function(graph) { - sp <- distances(graph, mode = "out") - if (is_directed(graph)) { - diag(sp) <- NA - } else { - sp[lower.tri(sp, diag = TRUE)] <- NA - } - sp[sp == "Inf"] <- NA - mean(sp, na.rm = TRUE) - } - - giant.component <- function(graph, mode = "weak") { - clu <- components(graph, mode = mode) - induced_subgraph(graph, which(clu$membership == which.max(clu$csize))) - } - - g <- giant.component(sample_gnp(100, 3 / 100)) - expect_equal(apl(g), mean_distance(g)) - - g <- giant.component(sample_gnp(100, 6 / 100, directed = TRUE), mode = "strong") - expect_equal(apl(g), mean_distance(g)) - - g <- sample_gnp(100, 2 / 100) - expect_equal(apl(g), mean_distance(g)) - - g <- sample_gnp(100, 4 / 100, directed = TRUE) - expect_equal(apl(g), mean_distance(g)) -}) - -test_that("mean_distance works correctly for disconnected graphs", { - g <- make_full_graph(5) %du% make_full_graph(7) - md <- mean_distance(g, unconnected = FALSE) - expect_equal(Inf, md) - md <- mean_distance(g, unconnected = TRUE) - expect_equal(1, md) -}) - -test_that("mean_distance can provide details", { - apl <- function(graph) { - sp <- distances(graph, mode = "out") - if (is_directed(graph)) { - diag(sp) <- NA - } else { - sp[lower.tri(sp, diag = TRUE)] <- NA - } - sp[sp == "Inf"] <- NA - mean(sp, na.rm = TRUE) - } - - giant.component <- function(graph, mode = "weak") { - clu <- components(graph, mode = mode) - induced_subgraph(graph, which(clu$membership == which.max(clu$csize))) - } - - g <- giant.component(sample_gnp(100, 3 / 100)) - md <- mean_distance(g, details = TRUE) - expect_equal(apl(g), md$res) - - g <- make_full_graph(5) %du% make_full_graph(7) - md <- mean_distance(g, details = TRUE, unconnected = TRUE) - expect_equal(1, md$res) - expect_equal(70, md$unconnected) - - g <- make_full_graph(5) %du% make_full_graph(7) - md <- mean_distance(g, details = TRUE, unconnected = FALSE) - expect_equal(Inf, md$res) - expect_equal(70, md$unconnected) -}) diff --git a/tests/testthat/test-contract.vertices.R b/tests/testthat/test-contract.vertices.R deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/testthat/test-count.multiple.R b/tests/testthat/test-count.multiple.R deleted file mode 100644 index 8bec886d59e..00000000000 --- a/tests/testthat/test-count.multiple.R +++ /dev/null @@ -1,37 +0,0 @@ -test_that("any_multiple(), count_multiple(), which_multiple() works", { - # g <- sample_pa(10, m = 3, algorithm = "bag") - g <- graph_from_edgelist(cbind( - c(2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10), - c(1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 3, 4, 3, 1, 1, 1, 3, 1, 2, 4, 1, 1, 2, 4, 1, 4, 1) - )) - im <- which_multiple(g) - cm <- count_multiple(g) - expect_true(any_multiple(g)) - expect_equal(im, c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE)) - expect_equal(cm, c(3, 3, 3, 3, 3, 3, 1, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2)) - expect_equal( - count_multiple(simplify(g)), - rep(1, ecount(simplify(g))) - ) - - - ## Direction of the edge is important - expect_false(any_multiple(make_graph(c(1, 2, 2, 1)))) - expect_equal(which_multiple(make_graph(c(1, 2, 2, 1))), c(FALSE, FALSE)) - expect_equal( - which_multiple(make_graph(c(1, 2, 2, 1), dir = FALSE)), - c(FALSE, TRUE) - ) - - ## Remove multiple edges but keep multiplicity - # g <- sample_pa(10, m = 3, algorithm = "bag") - g <- graph_from_edgelist(cbind( - c(2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10), - c(1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 4, 1, 4, 1, 1, 6, 4, 1, 5, 8) - )) - E(g)$weight <- 1 - g <- simplify(g) - expect_false(any_multiple(g)) - expect_false(any(which_multiple(g))) - expect_equal(E(g)$weight, c(3, 2, 1, 2, 1, 3, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1)) -}) diff --git a/tests/testthat/test-dyad.census.R b/tests/testthat/test-dyad.census.R deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/testthat/test-graph.density.R b/tests/testthat/test-graph.density.R deleted file mode 100644 index ce7fb06436e..00000000000 --- a/tests/testthat/test-graph.density.R +++ /dev/null @@ -1,13 +0,0 @@ -test_that("edge_density works", { - g <- sample_gnp(50, 4 / 50) - gd <- edge_density(g) - gd2 <- ecount(g) / vcount(g) / (vcount(g) - 1) * 2 - expect_equal(gd, gd2) - - #### - - g <- sample_gnp(50, 4 / 50, directed = TRUE) - gd <- edge_density(g) - gd2 <- ecount(g) / vcount(g) / (vcount(g) - 1) - expect_equal(gd, gd2) -}) diff --git a/tests/testthat/test-graph.knn.R b/tests/testthat/test-graph.knn.R deleted file mode 100644 index f91dfca1ae7..00000000000 --- a/tests/testthat/test-graph.knn.R +++ /dev/null @@ -1,31 +0,0 @@ -test_that("knn works", { - withr::local_seed(42) - - ## Some trivial ones - g <- make_ring(10) - expect_equal(knn(g), list(knn = rep(2, 10), knnk = c(NaN, 2))) - - g2 <- make_star(10) - expect_equal(knn(g2), list(knn = c(1, rep(9, 9)), knnk = c(9, rep(NaN, 7), 1))) - - ## A scale-free one, try to plot 'knnk' - g3 <- simplify(sample_pa(1000, m = 5)) - r3 <- knn(g3) - expect_equal(r3$knn[43], 46) - expect_equal(r3$knn[1000], 192.4) - expect_equal(r3$knnk[100], 18.78) - expect_equal(length(r3$knnk), 359) - - ## A random graph - g4 <- sample_gnp(1000, p = 5 / 1000) - r4 <- knn(g4) - expect_equal(r4$knn[1000], 20 / 3) - expect_equal(length(r4$knnk), 15) - expect_equal(r4$knnk[12], 19 / 3) - - ## A weighted graph - g5 <- make_star(10) - E(g5)$weight <- seq(ecount(g5)) - r5 <- knn(g5) - expect_equal(r5, structure(list(knn = c(1, rep(9, 9)), knnk = c(9, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1)), .Names = c("knn", "knnk"))) -}) diff --git a/tests/testthat/test-reciprocity.R b/tests/testthat/test-reciprocity.R deleted file mode 100644 index f15aecc746e..00000000000 --- a/tests/testthat/test-reciprocity.R +++ /dev/null @@ -1,5 +0,0 @@ -test_that("reciprocity works", { - g <- make_graph(c(1, 2, 2, 1, 2, 3, 3, 4, 4, 4), directed = TRUE) - expect_equal(reciprocity(g), 0.5) - expect_equal(reciprocity(g, ignore.loops = FALSE), 0.6) -}) diff --git a/tests/testthat/test-structural.properties.R b/tests/testthat/test-structural.properties.R index b6bfae6a708..6fef196856c 100644 --- a/tests/testthat/test-structural.properties.R +++ b/tests/testthat/test-structural.properties.R @@ -26,31 +26,32 @@ test_that("bfs() deprecated argument", { }) test_that("degree() works", { - g <- sample_gnp(100, 1 / 100) - d <- degree(g) - el <- as_edgelist(g) - expect_equal(as.numeric(table(el)), d[d != 0]) + gnp1 <- sample_gnp(100, 1 / 100) + gnp1_deg <- degree(gnp1) + el <- as_edgelist(gnp1) + expect_equal(as.numeric(table(el)), gnp1_deg[gnp1_deg != 0]) - expect_equal(degree(g) / (vcount(g) - 1), degree(g, normalized = TRUE)) + expect_equal(gnp1_deg / (vcount(gnp1) - 1), degree(gnp1, normalized = TRUE)) - g2 <- sample_gnp(100, 2 / 100, directed = TRUE) - din <- degree(g2, mode = "in") - dout <- degree(g2, mode = "out") - el2 <- as_edgelist(g2) - expect_equal(as.numeric(table(el2[, 1])), dout[dout != 0]) - expect_equal(as.numeric(table(el2[, 2])), din[din != 0]) + gnp2 <- sample_gnp(100, 2 / 100, directed = TRUE) + gnp2_deg_in <- degree(gnp2, mode = "in") + el2 <- as_edgelist(gnp2) + expect_equal(as.numeric(table(el2[, 2])), gnp2_deg_in[gnp2_deg_in != 0]) + + gnp2_deg_out <- degree(gnp2, mode = "out") + expect_equal(as.numeric(table(el2[, 1])), gnp2_deg_out[gnp2_deg_out != 0]) expect_equal( - degree(g2, mode = "in") / (vcount(g2) - 1), - degree(g2, mode = "in", normalized = TRUE) + gnp2_deg_in / (vcount(gnp2) - 1), + degree(gnp2, mode = "in", normalized = TRUE) ) expect_equal( - degree(g2, mode = "out") / (vcount(g2) - 1), - degree(g2, mode = "out", normalized = TRUE) + gnp2_deg_out / (vcount(gnp2) - 1), + degree(gnp2, mode = "out", normalized = TRUE) ) expect_equal( - degree(g2, mode = "all") / (vcount(g2) - 1), - degree(g2, mode = "all", normalized = TRUE) + degree(gnp2, mode = "all") / (vcount(gnp2) - 1), + degree(gnp2, mode = "all", normalized = TRUE) ) }) @@ -94,7 +95,8 @@ test_that("BFS works from multiple root vertices", { ) }) -test_that("issue 133", { +test_that("BFS the restricted set is one indexed", { + # https://github.com/igraph/rigraph/issues/133 g <- graph_from_edgelist(matrix(c(1, 2, 2, 3), ncol = 2, byrow = TRUE)) expect_equal( @@ -502,15 +504,16 @@ test_that("local transitivity() produces named vectors", { g <- make_graph(~ a - b - c - a - d) E(g)$weight <- 1:4 t1 <- transitivity(g, type = "local") + expect_equal(names(t1), V(g)$name) + t2 <- transitivity(g, type = "barrat") + expect_equal(names(t2), V(g)$name) vs <- c("a", "c") t3 <- transitivity(g, type = "local", vids = vs) - t4 <- transitivity(g, type = "barrat", vids = vs) - - expect_equal(names(t1), V(g)$name) - expect_equal(names(t2), V(g)$name) expect_equal(names(t3), vs) + + t4 <- transitivity(g, type = "barrat", vids = vs) expect_equal(names(t4), vs) }) @@ -729,3 +732,163 @@ test_that("coreness() works", { gc <- coreness(g) expect_equal(gc, c(3, 3, 3, 2, 2, 2, 2, 2, 2, 2)) }) + +test_that("mean_distance works", { + avg_path_length <- function(graph) { + sp <- distances(graph, mode = "out") + if (is_directed(graph)) { + diag(sp) <- NA + } else { + sp[lower.tri(sp, diag = TRUE)] <- NA + } + sp[sp == "Inf"] <- NA + mean(sp, na.rm = TRUE) + } + + giant.component <- function(graph, mode = "weak") { + clu <- components(graph, mode = mode) + induced_subgraph(graph, which(clu$membership == which.max(clu$csize))) + } + + g <- giant.component(sample_gnp(100, 3 / 100)) + expect_equal(avg_path_length(g), mean_distance(g)) + + g <- giant.component(sample_gnp(100, 6 / 100, directed = TRUE), mode = "strong") + expect_equal(avg_path_length(g), mean_distance(g)) + + g <- sample_gnp(100, 2 / 100) + expect_equal(avg_path_length(g), mean_distance(g)) + + g <- sample_gnp(100, 4 / 100, directed = TRUE) + expect_equal(avg_path_length(g), mean_distance(g)) +}) + +test_that("mean_distance works correctly for disconnected graphs", { + g <- make_full_graph(5) %du% make_full_graph(7) + md <- mean_distance(g, unconnected = FALSE) + expect_equal(Inf, md) + md <- mean_distance(g, unconnected = TRUE) + expect_equal(1, md) +}) + +test_that("mean_distance can provide details", { + avg_path_length <- function(graph) { + sp <- distances(graph, mode = "out") + if (is_directed(graph)) { + diag(sp) <- NA + } else { + sp[lower.tri(sp, diag = TRUE)] <- NA + } + sp[sp == "Inf"] <- NA + mean(sp, na.rm = TRUE) + } + + giant.component <- function(graph, mode = "weak") { + clu <- components(graph, mode = mode) + induced_subgraph(graph, which(clu$membership == which.max(clu$csize))) + } + + g <- giant.component(sample_gnp(100, 3 / 100)) + md <- mean_distance(g, details = TRUE) + expect_equal(avg_path_length(g), md$res) + + g <- make_full_graph(5) %du% make_full_graph(7) + md <- mean_distance(g, details = TRUE, unconnected = TRUE) + expect_equal(1, md$res) + expect_equal(70, md$unconnected) + + g <- make_full_graph(5) %du% make_full_graph(7) + md <- mean_distance(g, details = TRUE, unconnected = FALSE) + expect_equal(Inf, md$res) + expect_equal(70, md$unconnected) +}) + +test_that("any_multiple(), count_multiple(), which_multiple() works", { + # g <- sample_pa(10, m = 3, algorithm = "bag") + g <- graph_from_edgelist(cbind( + c(2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10), + c(1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 3, 4, 3, 1, 1, 1, 3, 1, 2, 4, 1, 1, 2, 4, 1, 4, 1) + )) + im <- which_multiple(g) + cm <- count_multiple(g) + expect_true(any_multiple(g)) + expect_equal(im, c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE)) + expect_equal(cm, c(3, 3, 3, 3, 3, 3, 1, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2)) + expect_equal( + count_multiple(simplify(g)), + rep(1, ecount(simplify(g))) + ) + + + ## Direction of the edge is important + expect_false(any_multiple(make_graph(c(1, 2, 2, 1)))) + expect_equal(which_multiple(make_graph(c(1, 2, 2, 1))), c(FALSE, FALSE)) + expect_equal( + which_multiple(make_graph(c(1, 2, 2, 1), dir = FALSE)), + c(FALSE, TRUE) + ) + + ## Remove multiple edges but keep multiplicity + # g <- sample_pa(10, m = 3, algorithm = "bag") + g <- graph_from_edgelist(cbind( + c(2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10), + c(1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 4, 1, 4, 1, 1, 6, 4, 1, 5, 8) + )) + E(g)$weight <- 1 + g <- simplify(g) + expect_false(any_multiple(g)) + expect_false(any(which_multiple(g))) + expect_equal(E(g)$weight, c(3, 2, 1, 2, 1, 3, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1)) +}) + +test_that("edge_density works", { + g <- sample_gnp(50, 4 / 50) + gd <- edge_density(g) + gd2 <- ecount(g) / vcount(g) / (vcount(g) - 1) * 2 + expect_equal(gd, gd2) + + #### + + g <- sample_gnp(50, 4 / 50, directed = TRUE) + gd <- edge_density(g) + gd2 <- ecount(g) / vcount(g) / (vcount(g) - 1) + expect_equal(gd, gd2) +}) + +test_that("knn works", { + withr::local_seed(42) + + ## Some trivial ones + g <- make_ring(10) + expect_equal(knn(g), list(knn = rep(2, 10), knnk = c(NaN, 2))) + + g2 <- make_star(10) + expect_equal(knn(g2), list(knn = c(1, rep(9, 9)), knnk = c(9, rep(NaN, 7), 1))) + + ## A scale-free one, try to plot 'knnk' + g3 <- simplify(sample_pa(1000, m = 5)) + r3 <- knn(g3) + expect_equal(r3$knn[43], 46) + expect_equal(r3$knn[1000], 192.4) + expect_equal(r3$knnk[100], 18.78) + expect_equal(length(r3$knnk), 359) + + ## A random graph + g4 <- sample_gnp(1000, p = 5 / 1000) + r4 <- knn(g4) + expect_equal(r4$knn[1000], 20 / 3) + expect_equal(length(r4$knnk), 15) + expect_equal(r4$knnk[12], 19 / 3) + + ## A weighted graph + g5 <- make_star(10) + E(g5)$weight <- seq(ecount(g5)) + r5 <- knn(g5) + expect_equal(r5, structure(list(knn = c(1, rep(9, 9)), knnk = c(9, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1)), .Names = c("knn", "knnk"))) +}) + +test_that("reciprocity works", { + g <- make_graph(c(1, 2, 2, 1, 2, 3, 3, 4, 4, 4), directed = TRUE) + expect_equal(reciprocity(g), 0.5) + expect_equal(reciprocity(g, ignore.loops = FALSE), 0.6) +})