diff --git a/tests/testthat/_snaps/foreign.md b/tests/testthat/_snaps/foreign.md new file mode 100644 index 0000000000..1f4d040c57 --- /dev/null +++ b/tests/testthat/_snaps/foreign.md @@ -0,0 +1,20 @@ +# reading graph in NCOL format + + Code + read_graph(ncol_path, "ncol") + Output + IGRAPH UN-- 3 2 -- + + attr: name (v/c) + + edges (vertex names): + [1] 0--1 1--2 + +# reading graph in LGL format + + Code + read_graph(lgl_path, "lgl") + Output + IGRAPH UN-- 3 2 -- + + attr: name (v/c) + + edges (vertex names): + [1] 0--1 1--2 + diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index c0d7e6309c..03a7eabe58 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -20,8 +20,12 @@ local_rng_version <- function(version, .local_envir = parent.frame()) { orig } -expect_isomorphic <- function(g1, g2) { - expect_true(isomorphic(g1, g2)) +expect_isomorphic <- function(g1, g2, ...) { + expect_true(isomorphic(g1, g2, ...)) +} + +expect_not_isomorphic <- function(g1, g2, ...) { + expect_false(isomorphic(g1, g2, ...)) } expect_vcount <- function(graph, expected, ...) { diff --git a/tests/testthat/test-components.R b/tests/testthat/test-components.R index 50733ffd31..a30bde3338 100644 --- a/tests/testthat/test-components.R +++ b/tests/testthat/test-components.R @@ -111,7 +111,7 @@ test_that("largest strongly and weakly components are correct", { B - +C, C - +A ) - expect_true(isomorphic(largest_component(g, "strong"), strongly)) + expect_isomorphic(largest_component(g, "strong"), strongly) weakly <- graph_from_literal( A - +B, @@ -119,13 +119,13 @@ test_that("largest strongly and weakly components are correct", { C - +A, C - +D ) - expect_true(isomorphic(largest_component(g, "weak"), weakly)) + expect_isomorphic(largest_component(g, "weak"), weakly) }) test_that("the largest component of a null graph is a valid null graph", { nullgraph <- make_empty_graph(0) - expect_true(isomorphic(largest_component(make_empty_graph(0)), nullgraph)) + expect_isomorphic(largest_component(make_empty_graph(0)), nullgraph) }) test_that("articulation_points works", { diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index 93016bde07..f127975035 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -306,11 +306,11 @@ test_that("as_adj_list works", { expect_s3_class(adj_list[[1]], "igraph.vs") g_same <- graph_from_adj_list(adj_list, mode = "all") expect_isomorphic(g, g_same) - expect_true(isomorphic(g, g_same, + expect_isomorphic(g, g_same, method = "vf2", vertex.color1 = seq_len(vcount(g)), vertex.color2 = seq_len(vcount(g_same)) - )) + ) adj_el_list <- as_adj_edge_list(g) expect_s3_class(adj_el_list[[1]], "igraph.es") @@ -345,11 +345,11 @@ test_that("as_adj_list works when return.vs.es is FALSE", { expect_s3_class(adj_list[[1]], NA) g2 <- graph_from_adj_list(adj_list, mode = "all") expect_isomorphic(g, g2) - expect_true(isomorphic(g, g2, + expect_isomorphic(g, g2, method = "vf2", vertex.color1 = 1:vcount(g), vertex.color2 = 1:vcount(g2) - )) + ) adj_el_list <- as_adj_edge_list(g) for (i in seq_len(vcount(g))) { diff --git a/tests/testthat/test-foreign.R b/tests/testthat/test-foreign.R new file mode 100644 index 0000000000..31713d0d00 --- /dev/null +++ b/tests/testthat/test-foreign.R @@ -0,0 +1,40 @@ +test_that("writing Pajek files works", { + g <- make_ring(9) + V(g)$color <- rep_len(c("red", "green", "yellow"), length.out = 9) + + tc <- withr::local_connection(rawConnection(raw(0), "w")) + + write_graph(g, format = "pajek", file = tc) + + expect_equal( + rawToChar(rawConnectionValue(tc)), + "*Vertices 9\n1 \"1\" ic \"red\"\n2 \"2\" ic \"green\"\n3 \"3\" ic \"yellow\"\n4 \"4\" ic \"red\"\n5 \"5\" ic \"green\"\n6 \"6\" ic \"yellow\"\n7 \"7\" ic \"red\"\n8 \"8\" ic \"green\"\n9 \"9\" ic \"yellow\"\n*Edges\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n1 9\n" + ) +}) + +test_that("reading GraphML file works", { + skip_if_no_graphml() + + g <- read_graph(f <- gzfile("zachary.graphml.gz"), format = "graphml") + g2 <- make_graph("zachary") + + expect_isomorphic(g2, g) +}) + +test_that("reading graph in NCOL format", { + local_igraph_options(print.id = FALSE) + + ncol_path <- withr::local_tempfile(pattern = "testfile", fileext = ".ncol") + g <- make_graph(c(1, 2, 2, 3)) + write_graph(g, ncol_path, "ncol") + expect_snapshot(read_graph(ncol_path, "ncol")) +}) + +test_that("reading graph in LGL format", { + local_igraph_options(print.id = FALSE) + + lgl_path <- withr::local_tempfile(pattern = "testfile", fileext = ".lgl") + g <- make_graph(c(1, 2, 2, 3)) + write_graph(g, lgl_path, "lgl") + expect_snapshot(read_graph(lgl_path, "lgl")) +}) diff --git a/tests/testthat/test-isomorphism.R b/tests/testthat/test-isomorphism.R index cbb2b6cf11..4fb497ccfa 100644 --- a/tests/testthat/test-isomorphism.R +++ b/tests/testthat/test-isomorphism.R @@ -1,15 +1,15 @@ test_that("isomorphic", { g <- graph_from_literal(A - B - C - A) - expect_true(isomorphic(g, g)) - expect_true(isomorphic(g, g, method = "direct")) - expect_true(isomorphic(g, g, method = "vf2")) - expect_true(isomorphic(g, g, method = "bliss")) + expect_isomorphic(g, g) + expect_isomorphic(g, g, method = "direct") + expect_isomorphic(g, g, method = "vf2") + expect_isomorphic(g, g, method = "bliss") g2 <- graph_from_literal(A - B - C) - expect_false(isomorphic(g, g2)) - expect_false(isomorphic(g, g2, method = "direct")) - expect_false(isomorphic(g, g2, method = "vf2")) - expect_false(isomorphic(g, g2, method = "bliss")) + expect_not_isomorphic(g, g2) + expect_not_isomorphic(g, g2, method = "direct") + expect_not_isomorphic(g, g2, method = "vf2") + expect_not_isomorphic(g, g2, method = "bliss") }) test_that("subgraph_isomorphic", { diff --git a/tests/testthat/test-operators.R b/tests/testthat/test-operators.R index 28a7020b5a..6821d69b23 100644 --- a/tests/testthat/test-operators.R +++ b/tests/testthat/test-operators.R @@ -87,7 +87,7 @@ test_that("edge reversal works", { 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(reverse_directed_graph, expected)) + expect_isomorphic(reverse_directed_graph, expected) reverse_all_directed_graph <- reverse_edges(directed_graph) expect_vcount(reverse_all_directed_graph, vcount(directed_graph)) diff --git a/tests/testthat/test-pajek.R b/tests/testthat/test-pajek.R deleted file mode 100644 index 8a44c41f97..0000000000 --- a/tests/testthat/test-pajek.R +++ /dev/null @@ -1,11 +0,0 @@ -test_that("writing Pajek files works", { - g <- make_ring(9) - V(g)$color <- rep(c("red", "green", "yellow"), 3) - - tc <- rawConnection(raw(0), "w") - write_graph(g, format = "pajek", file = tc) - out <- rawToChar(rawConnectionValue(tc)) - close(tc) - - expect_equal(out, "*Vertices 9\n1 \"1\" ic \"red\"\n2 \"2\" ic \"green\"\n3 \"3\" ic \"yellow\"\n4 \"4\" ic \"red\"\n5 \"5\" ic \"green\"\n6 \"6\" ic \"yellow\"\n7 \"7\" ic \"red\"\n8 \"8\" ic \"green\"\n9 \"9\" ic \"yellow\"\n*Edges\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n1 9\n") -}) diff --git a/tests/testthat/test-read_graph.R b/tests/testthat/test-read_graph.R deleted file mode 100644 index 2f671f872e..0000000000 --- a/tests/testthat/test-read_graph.R +++ /dev/null @@ -1,22 +0,0 @@ -test_that("reading GraphML file works", { - skip_if_no_graphml() - - g <- read_graph(f <- gzfile("zachary.graphml.gz"), format = "graphml") - g2 <- make_graph("zachary") - - expect_true(isomorphic(g2, g)) -}) - -test_that("reading graph in NCOL format", { - ncol_path <- tempfile("testfile", fileext = ".ncol") - g <- make_graph(c(1, 2, 2, 3)) - write_graph(g, ncol_path, "ncol") - expect_no_error(g2 <- read_graph(ncol_path, "ncol")) -}) - -test_that("reading graph in LGL format", { - lgl_path <- tempfile("testfile", fileext = ".lgl") - g <- make_graph(c(1, 2, 2, 3)) - write_graph(g, lgl_path, "lgl") - expect_no_error(g2 <- read_graph(lgl_path, "lgl")) -}) diff --git a/tests/testthat/test-trees.R b/tests/testthat/test-trees.R index 1dba898ba5..b02ee6a4d7 100644 --- a/tests/testthat/test-trees.R +++ b/tests/testthat/test-trees.R @@ -73,26 +73,26 @@ test_that("to_prufer and make_from_prufer works for trees", { g <- make_tree(13, 3, mode = "undirected") seq <- to_prufer(g) g2 <- make_from_prufer(seq) - expect_true(isomorphic(g, g2)) + expect_isomorphic(g, g2) g <- make_tree(13, 3, mode = "out") seq <- to_prufer(g) g2 <- make_from_prufer(seq) g3 <- as_undirected(g) - expect_true(isomorphic(g2, g3)) + expect_isomorphic(g2, g3) g <- make_tree(13, 3, mode = "in") seq <- to_prufer(g) g2 <- make_from_prufer(seq) g3 <- as_undirected(g) - expect_true(isomorphic(g2, g3)) + expect_isomorphic(g2, g3) }) test_that("make_(from_prufer(...)) works", { g <- make_tree(13, 3, mode = "undirected") seq <- to_prufer(g) g2 <- make_(from_prufer(seq)) - expect_true(isomorphic(g, g2)) + expect_isomorphic(g, g2) }) test_that("to_prufer prints an error for non-trees", {