1 2context("mean_distance") 3 4test_that("mean_distance works", { 5 library(igraph) 6 7 apl <- function(graph) { 8 sp <- distances(graph, mode="out") 9 if (is_directed(graph)) { 10 diag(sp) <- NA 11 } else { 12 sp[lower.tri(sp, diag=TRUE)] <- NA 13 } 14 sp[sp=="Inf"] <- NA 15 mean(sp, na.rm=TRUE) 16 } 17 18 giant.component <- function(graph, mode="weak") { 19 clu <- components(graph, mode=mode) 20 induced_subgraph(graph, which(clu$membership==which.max(clu$csize))) 21 } 22 23 g <- giant.component(sample_gnp(100, 3/100)) 24 expect_that(apl(g), equals(mean_distance(g))) 25 26 g <- giant.component(sample_gnp(100, 6/100, dir=TRUE), mode="strong") 27 expect_that(apl(g), equals(mean_distance(g))) 28 29 g <- sample_gnp(100, 2/100) 30 expect_that(apl(g), equals(mean_distance(g))) 31 32 g <- sample_gnp(100, 4/100, dir=TRUE) 33 expect_that(apl(g), equals(mean_distance(g))) 34}) 35