From f4250b5d75714bf1a4ac97e35578841f6374f4e5 Mon Sep 17 00:00:00 2001 From: schochastics Date: Fri, 28 Feb 2025 05:38:49 +0100 Subject: [PATCH] merged and refactored other.R tests --- R/other.R | 8 +-- tests/testthat/_snaps/old-data-type.md | 9 --- tests/testthat/_snaps/other.md | 34 +++++++++++ tests/testthat/_snaps/serialize.md | 17 ------ tests/testthat/test-convex_hull.R | 14 ----- tests/testthat/test-handler.R | 10 ---- .../{test-old-data-type.R => test-other.R} | 58 +++++++++++++++++++ tests/testthat/test-rng.R | 9 --- tests/testthat/test-serialize.R | 13 ----- 9 files changed, 96 insertions(+), 76 deletions(-) delete mode 100644 tests/testthat/_snaps/old-data-type.md create mode 100644 tests/testthat/_snaps/other.md delete mode 100644 tests/testthat/_snaps/serialize.md delete mode 100644 tests/testthat/test-convex_hull.R delete mode 100644 tests/testthat/test-handler.R rename tests/testthat/{test-old-data-type.R => test-other.R} (70%) delete mode 100644 tests/testthat/test-rng.R delete mode 100644 tests/testthat/test-serialize.R diff --git a/R/other.R b/R/other.R index af3adbaa249..983d42d4e9f 100644 --- a/R/other.R +++ b/R/other.R @@ -91,7 +91,7 @@ running_mean <- function(v, binwidth) { v <- as.numeric(v) binwidth <- as.numeric(binwidth) if (length(v) < binwidth) { - stop("Vector too short for this binwidth.") + cli::cli_abort("Vector too short for this binwidth.") } on.exit(.Call(R_igraph_finalizer)) @@ -127,7 +127,7 @@ running_mean <- function(v, binwidth) { #' sample_seq <- function(low, high, length) { if (length > high - low + 1) { - stop("length too big for this interval") + cli::cli_abort("length too big for this interval") } on.exit(.Call(R_igraph_finalizer)) @@ -165,11 +165,11 @@ handle_vertex_type_arg <- function(types, graph, required = T) { } types <- as.logical(types) if (any(is.na(types))) { - stop("`NA' is not allowed in vertex types") + cli::cli_abort("`NA' is not allowed in vertex types") } } if (is.null(types) && required) { - stop("Not a bipartite graph, supply `types' argument or add a vertex attribute named `type'") + cli::cli_abort("Not a bipartite graph, supply {.arg types} argument or add a vertex attribute named {.arg type}.") } return(types) } diff --git a/tests/testthat/_snaps/old-data-type.md b/tests/testthat/_snaps/old-data-type.md deleted file mode 100644 index 7643954a2c9..00000000000 --- a/tests/testthat/_snaps/old-data-type.md +++ /dev/null @@ -1,9 +0,0 @@ -# VS/ES require explicit conversion - - Code - V(karate) - Condition - Error in `warn_version()`: - ! This graph was created by a now unsupported old igraph version. - Call upgrade_graph() before using igraph functions on that object. - diff --git a/tests/testthat/_snaps/other.md b/tests/testthat/_snaps/other.md new file mode 100644 index 00000000000..5c2f5616b89 --- /dev/null +++ b/tests/testthat/_snaps/other.md @@ -0,0 +1,34 @@ +# running_mean works + + Code + running_mean(1:3, 4) + Condition + Error in `running_mean()`: + ! Vector too short for this binwidth. + +# serialization works + + Code + g + Output + IGRAPH D--- 3 3 -- Ring graph + + attr: name (g/c), mutual (g/l), circular (g/l) + + edges: + [1] 1->2 2->3 3->1 + Code + gs + Output + IGRAPH D--- 3 3 -- Ring graph + + attr: name (g/c), mutual (g/l), circular (g/l) + + edges: + [1] 1->2 2->3 3->1 + +# VS/ES require explicit conversion + + Code + V(karate) + Condition + Error in `warn_version()`: + ! This graph was created by a now unsupported old igraph version. + Call upgrade_graph() before using igraph functions on that object. + diff --git a/tests/testthat/_snaps/serialize.md b/tests/testthat/_snaps/serialize.md deleted file mode 100644 index c757d5ffa50..00000000000 --- a/tests/testthat/_snaps/serialize.md +++ /dev/null @@ -1,17 +0,0 @@ -# serialization works - - Code - g - Output - IGRAPH D--- 3 3 -- Ring graph - + attr: name (g/c), mutual (g/l), circular (g/l) - + edges: - [1] 1->2 2->3 3->1 - Code - gs - Output - IGRAPH D--- 3 3 -- Ring graph - + attr: name (g/c), mutual (g/l), circular (g/l) - + edges: - [1] 1->2 2->3 3->1 - diff --git a/tests/testthat/test-convex_hull.R b/tests/testthat/test-convex_hull.R deleted file mode 100644 index 9fa7158bf4d..00000000000 --- a/tests/testthat/test-convex_hull.R +++ /dev/null @@ -1,14 +0,0 @@ -test_that("convex_hull works", { - xy <- cbind(c(0, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2), c(0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2)) - vp <- convex_hull(xy) - expect_equal(vp$resverts, c(1, 6, 10, 11, 5)) - expect_equal(vp$rescoords, xy[vp$resverts, ]) -}) - -test_that("convex_hull uses 1-based indexing, #613", { - withr::local_seed(45) - n <- 10 - xy <- cbind(runif(n), runif(n)) - vp <- convex_hull(xy) - expect_equal(vp$resverts, c(8, 10, 7, 2, 1)) -}) diff --git a/tests/testthat/test-handler.R b/tests/testthat/test-handler.R deleted file mode 100644 index 7cf6572d643..00000000000 --- a/tests/testthat/test-handler.R +++ /dev/null @@ -1,10 +0,0 @@ -test_that("can create graphs when igraph is not attached", { - g <- callr::r(function() { - igraph::make_ring(3, directed = TRUE) - }) - g2 <- make_ring(3, directed = TRUE) - expect_identical( - unclass(g)[-igraph_t_idx_env], - unclass(g2)[-igraph_t_idx_env] - ) -}) diff --git a/tests/testthat/test-old-data-type.R b/tests/testthat/test-other.R similarity index 70% rename from tests/testthat/test-old-data-type.R rename to tests/testthat/test-other.R index 2bea91a070c..d872400ea7b 100644 --- a/tests/testthat/test-old-data-type.R +++ b/tests/testthat/test-other.R @@ -1,3 +1,61 @@ +test_that("convex_hull works", { + xy <- cbind(c(0, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2), c(0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2)) + vp <- convex_hull(xy) + expect_equal(vp$resverts, c(1, 6, 10, 11, 5)) + expect_equal(vp$rescoords, xy[vp$resverts, ]) +}) + +test_that("convex_hull uses 1-based indexing, #613", { + withr::local_seed(45) + n <- 10 + xy <- cbind(runif(n), runif(n)) + vp <- convex_hull(xy) + expect_equal(vp$resverts, c(8, 10, 7, 2, 1)) +}) + +test_that("can create graphs when igraph is not attached", { + g <- callr::r(function() { + igraph::make_ring(3, directed = TRUE) + }) + g2 <- make_ring(3, directed = TRUE) + expect_identical( + unclass(g)[-igraph_t_idx_env], + unclass(g2)[-igraph_t_idx_env] + ) +}) + +test_that("running_mean works", { + expect_equal(running_mean(1:10, 2), 2:10 - 0.5) + expect_snapshot( + running_mean(1:3, 4), + error = TRUE + ) +}) + +test_that("R help contains guarantee on number of RNG bits", { + skip_on_cran() + + # utils:::.getHelpFile + get_help_file <- get(".getHelpFile", envir = asNamespace("utils")) + text <- capture.output(tools::Rd2txt(get_help_file(help("Random")))) + + expect_true(any(grepl("all give at least 30 varying bits", text, fixed = TRUE))) +}) + +test_that("serialization works", { + local_igraph_options(print.id = FALSE) + + g <- make_ring(3, directed = TRUE) + gs <- unserialize(serialize(g, NULL)) + + expect_identical(unclass(g)[-igraph_t_idx_env], unclass(gs)[-igraph_t_idx_env]) + + expect_snapshot({ + g + gs + }) +}) + names <- c( "Mr Hi", "Actor 2", "Actor 3", "Actor 4", "Actor 5", "Actor 6", "Actor 7", "Actor 8", "Actor 9", "Actor 10", diff --git a/tests/testthat/test-rng.R b/tests/testthat/test-rng.R deleted file mode 100644 index 108d92a8133..00000000000 --- a/tests/testthat/test-rng.R +++ /dev/null @@ -1,9 +0,0 @@ -test_that("R help contains guarantee on number of RNG bits", { - skip_on_cran() - - # utils:::.getHelpFile - get_help_file <- get(".getHelpFile", envir = asNamespace("utils")) - text <- capture.output(tools::Rd2txt(get_help_file(help("Random")))) - - expect_true(any(grepl("all give at least 30 varying bits", text, fixed = TRUE))) -}) diff --git a/tests/testthat/test-serialize.R b/tests/testthat/test-serialize.R deleted file mode 100644 index 099ab76e3d8..00000000000 --- a/tests/testthat/test-serialize.R +++ /dev/null @@ -1,13 +0,0 @@ -test_that("serialization works", { - local_igraph_options(print.id = FALSE) - - g <- make_ring(3, directed = TRUE) - gs <- unserialize(serialize(g, NULL)) - - expect_identical(unclass(g)[-igraph_t_idx_env], unclass(gs)[-igraph_t_idx_env]) - - expect_snapshot({ - g - gs - }) -})