From 18532d8147be1bf7c5834596cf8432c826fce51b Mon Sep 17 00:00:00 2001 From: mpadge Date: Fri, 27 Sep 2024 12:32:06 +0200 Subject: [PATCH] add tests to close #254 --- DESCRIPTION | 2 +- codemeta.json | 2 +- tests/testthat/test-dists.R | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e03cbed5b..fa73794b2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: dodgr Title: Distances on Directed Graphs -Version: 0.4.1.030 +Version: 0.4.1.031 Authors@R: c( person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre")), person("Andreas", "Petutschnig", role = "aut"), diff --git a/codemeta.json b/codemeta.json index 6090c526a..080b02703 100644 --- a/codemeta.json +++ b/codemeta.json @@ -11,7 +11,7 @@ "codeRepository": "https://github.com/UrbanAnalyst/dodgr", "issueTracker": "https://github.com/UrbanAnalyst/dodgr/issues", "license": "https://spdx.org/licenses/GPL-3.0", - "version": "0.4.1.030", + "version": "0.4.1.031", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R", diff --git a/tests/testthat/test-dists.R b/tests/testthat/test-dists.R index 4acb3828d..4ee62f9b0 100644 --- a/tests/testthat/test-dists.R +++ b/tests/testthat/test-dists.R @@ -189,6 +189,48 @@ test_that ("all dists", { expect_equal (nrow (d), nrow (v)) }) +test_that ("to-from-as-integerish", { + # Integer-ish to-from cols (#254 from @luukvdmeer) + graph <- data.frame ( + from = c (1, 2, 2, 2, 3, 3, 4, 4), + to = c (2, 1, 3, 4, 2, 4, 3, 1), + d = c (1, 2, 1, 3, 2, 1, 2, 1) + ) + expect_silent (d0 <- dodgr_dists (graph, from = 1, to = 2)) + expect_silent (d1 <- dodgr_dists (graph, from = 1L, to = 2L)) + expect_identical (d0, d1) + v <- dodgr_vertices (graph) + # Verts are numeric, but 'graph$from' has them in sequence, so: + expect_identical (v$id, as.numeric (seq_along (unique (graph$from)))) + + graph <- data.frame ( + from = c (1, 3, 2, 2, 3, 3, 4, 4), # no longer in sequence! + to = c (2, 1, 3, 4, 2, 4, 3, 1), + d = c (1, 2, 1, 3, 2, 1, 2, 1) + ) + expect_silent (d2 <- dodgr_dists (graph, from = 1, to = 2)) + expect_silent (d3 <- dodgr_dists (graph, from = 1L, to = 2L)) + v <- dodgr_vertices (graph) + expect_false (identical (v$id, as.numeric (seq_along (unique (graph$from))))) + expect_true (rownames (d2) == "1") + expect_true (rownames (d3) == "1") + expect_false (colnames (d2) == "2") + expect_false (colnames (d3) == "2") + real_id <- v$id [2] # = 3 + expect_true (colnames (d2) == as.character (real_id)) + expect_true (colnames (d3) == as.character (real_id)) + + graph <- data.frame ( + from = as.character (c (1, 3, 2, 2, 3, 3, 4, 4)), + to = as.character (c (2, 1, 3, 4, 2, 4, 3, 1)), + d = c (1, 2, 1, 3, 2, 1, 2, 1) + ) + d4 <- dodgr_dists (graph, from = "1", to = "2") + expect_equal (rownames (d4), "1") + expect_equal (colnames (d4), "2") + +}) + test_that ("to-from-cols", { graph <- weight_streetnet (hampi) nf <- 100