Skip to content

Commit

Permalink
test: merge all tests for foreign.R into one test file (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored Feb 27, 2025
1 parent cd45428 commit b23e7e1
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 55 deletions.
20 changes: 20 additions & 0 deletions tests/testthat/_snaps/foreign.md
Original file line number Diff line number Diff line change
@@ -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

8 changes: 6 additions & 2 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-components.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,21 @@ 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,
B - +C,
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", {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))) {
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test-foreign.R
Original file line number Diff line number Diff line change
@@ -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"))
})
16 changes: 8 additions & 8 deletions tests/testthat/test-isomorphism.R
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 0 additions & 11 deletions tests/testthat/test-pajek.R

This file was deleted.

22 changes: 0 additions & 22 deletions tests/testthat/test-read_graph.R

This file was deleted.

8 changes: 4 additions & 4 deletions tests/testthat/test-trees.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit b23e7e1

Please sign in to comment.