From 1fdd8642dce793245f07bb7daf1a023c4bd3216b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 23 May 2024 12:48:23 +0200 Subject: [PATCH 1/3] test: expect_equal() --- tests/testthat/test-operators.R | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index 6df27e322f4..f3ae7668759 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -4,20 +4,20 @@ test_that("operators work", { g1 <- make_ring(10) g2 <- make_star(11, center = 11, mode = "undirected") gu <- union(g1, g2) - expect_that(vcount(gu), equals(11)) - expect_that(ecount(gu), equals(20)) - expect_that( + expect_equal(vcount(gu), 11) + expect_equal(ecount(gu), 20) + expect_equal( o(rbind(as_edgelist(g1), as_edgelist(g2))), - equals(o(as_edgelist(gu))) + o(as_edgelist(gu)) ) gdu <- disjoint_union(g1, g2) - expect_that( + expect_equal( o(as_edgelist(gdu)), - equals(o(rbind( + o(rbind( as_edgelist(g1), as_edgelist(g2) + vcount(g1) - ))) + )) ) #### @@ -43,9 +43,9 @@ test_that("operators work", { #### gc <- compose(gu, g1) - expect_that(vcount(gc), equals(11)) - expect_that(ecount(gc), equals(60)) - expect_that(diameter(gc), equals(2)) + expect_equal(vcount(gc), 11) + expect_equal(ecount(gc), 60) + expect_equal(diameter(gc), 2) }) test_that("Union of directed named graphs", { @@ -76,20 +76,20 @@ test_that("edge reversal works", { # all edges g <- make_graph(~ 1 -+ 2, 1 -+ 3, 1 -+ 4, 2 -+ 3, 3 -+ 4) g2 <- reverse_edges(g) - expect_that(vcount(g2), equals(vcount(g))) - expect_that(as_edgelist(g2), equals(as_edgelist(g)[, c(2, 1)])) + expect_equal(vcount(g2), vcount(g)) + expect_equal(as_edgelist(g2), as_edgelist(g)[, c(2, 1)]) # graph with isolated vertices g <- make_graph(~ 1:2:3:4:5, 1 -+ 2, 1 -+ 4) g2 <- reverse_edges(g) - expect_that(vcount(g2), equals(vcount(g))) - expect_that(as_edgelist(g2), equals(as_edgelist(g)[, c(2, 1)])) + expect_equal(vcount(g2), vcount(g)) + expect_equal(as_edgelist(g2), as_edgelist(g)[, c(2, 1)]) }) test_that("t() is aliased to edge reversal for graphs", { g <- make_graph(~ 1 -+ 2, 1 -+ 3, 1 -+ 4, 2 -+ 3, 3 -+ 4) - expect_that(vcount(t(g)), equals(vcount(g))) - expect_that(as_edgelist(t(g)), equals(as_edgelist(g)[, c(2, 1)])) + expect_equal(vcount(t(g)), vcount(g)) + expect_equal(as_edgelist(t(g)), as_edgelist(g)[, c(2, 1)]) }) test_that("vertices() works", { From 4f12c88b7bffb802b6c81fa12e2950beb80f883f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 23 May 2024 12:52:21 +0200 Subject: [PATCH 2/3] test: more informative names for edge reversal test --- tests/testthat/test-operators.R | 39 +++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index f3ae7668759..853a9effa66 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -62,28 +62,29 @@ test_that("Union of directed named graphs", { }) test_that("edge reversal works", { - # directed graph - g <- make_graph(~ 1 -+ 2, 1 -+ 3, 1 -+ 4, 2 -+ 3, 3 -+ 4) - g2 <- reverse_edges(g, 1:3) + directed_graph <- make_graph(~ 1 -+ 2, 1 -+ 3, 1 -+ 4, 2 -+ 3, 3 -+ 4) + reverse_directed_graph <- reverse_edges(directed_graph, 1:3) expected <- make_graph(~ 1 +- 2, 1 +- 3, 1 +- 4, 2 -+ 3, 3 -+ 4) - expect_true(isomorphic(g2, expected)) + expect_true(isomorphic(reverse_directed_graph, expected)) - # undirected graph - g <- make_graph(~ 1 -- 2, 1 -- 3, 1 -- 4, 2 -- 3, 3 -- 4) - g2 <- reverse_edges(g, 1:3) - expect_true(identical_graphs(g, g2)) + reverse_all_directed_graph <- reverse_edges(directed_graph) + expect_equal(vcount(reverse_all_directed_graph), vcount(directed_graph)) + expect_equal( + as_edgelist(reverse_all_directed_graph), + as_edgelist(directed_graph)[, c(2, 1)] + ) - # all edges - g <- make_graph(~ 1 -+ 2, 1 -+ 3, 1 -+ 4, 2 -+ 3, 3 -+ 4) - g2 <- reverse_edges(g) - expect_equal(vcount(g2), vcount(g)) - expect_equal(as_edgelist(g2), as_edgelist(g)[, c(2, 1)]) - - # graph with isolated vertices - g <- make_graph(~ 1:2:3:4:5, 1 -+ 2, 1 -+ 4) - g2 <- reverse_edges(g) - expect_equal(vcount(g2), vcount(g)) - expect_equal(as_edgelist(g2), as_edgelist(g)[, c(2, 1)]) + undirected_graph <- make_graph(~ 1 -- 2, 1 -- 3, 1 -- 4, 2 -- 3, 3 -- 4) + reverse_undirected_graph <- reverse_edges(undirected_graph, 1:3) + expect_true(identical_graphs(undirected_graph, reverse_undirected_graph)) + + isolated_vertices_g <- make_graph(~ 1:2:3:4:5, 1 -+ 2, 1 -+ 4) + reverse_isolated_vertices_g <- reverse_edges(isolated_vertices_g) + expect_equal(vcount(reverse_isolated_vertices_g), vcount(isolated_vertices_g)) + expect_equal( + as_edgelist(reverse_isolated_vertices_g), + as_edgelist(isolated_vertices_g)[, c(2, 1)] + ) }) test_that("t() is aliased to edge reversal for graphs", { From 2e4ad310fff70cc4f8b916207ffd8c856f3f10a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 23 May 2024 13:02:36 +0200 Subject: [PATCH 3/3] test: split operators tests into specific tests --- tests/testthat/test-operators.R | 51 ++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index 853a9effa66..c70865be312 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -1,46 +1,57 @@ -test_that("operators work", { - o <- function(x) x[order(x[, 1], x[, 2]), ] +test_that("union() works", { + order_by_two_first_columns <- function(x) x[order(x[, 1], x[, 2]), ] g1 <- make_ring(10) g2 <- make_star(11, center = 11, mode = "undirected") gu <- union(g1, g2) + expect_equal(vcount(gu), 11) expect_equal(ecount(gu), 20) expect_equal( - o(rbind(as_edgelist(g1), as_edgelist(g2))), - o(as_edgelist(gu)) + order_by_two_first_columns(rbind(as_edgelist(g1), as_edgelist(g2))), + order_by_two_first_columns(as_edgelist(gu)) ) + expect_isomorphic(difference(gu, g1), g2) + expect_isomorphic(intersection(gu, g2), g2) +}) + +test_that("disjoint_union() works", { + order_by_two_first_columns <- function(x) x[order(x[, 1], x[, 2]), ] + g1 <- make_ring(10) + g2 <- make_star(11, center = 11, mode = "undirected") gdu <- disjoint_union(g1, g2) expect_equal( - o(as_edgelist(gdu)), - o(rbind( + order_by_two_first_columns(as_edgelist(gdu)), + order_by_two_first_columns(rbind( as_edgelist(g1), as_edgelist(g2) + vcount(g1) )) ) +}) - #### - - expect_isomorphic(difference(gu, g1), g2) - - #### +test_that("intersection() works", { - expect_isomorphic(intersection(gu, g2), g2) + g1 <- make_ring(10) + g2 <- make_star(11, center = 11, mode = "undirected") + gu <- union(g1, g2) - expect_isomorphic( - intersection(gu, g1, - keep.all.vertices = FALSE - ), - g1 - ) + expect_isomorphic(intersection(gu, g1, keep.all.vertices = FALSE), g1) +}) - #### +test_that("complementer() works", { + g2 <- make_star(11, center = 11, mode = "undirected") x <- complementer(complementer(g2)) expect_true(identical_graphs(x, g2)) - #### +}) + +test_that("compose() works", { + + g1 <- make_ring(10) + g2 <- make_star(11, center = 11, mode = "undirected") + gu <- union(g1, g2) gc <- compose(gu, g1) expect_equal(vcount(gc), 11)