From 243bc2b0c3aeec8e9e04e004b1d759398ad5a49e Mon Sep 17 00:00:00 2001 From: Uchida Mizuki <uchidamizuki@vivaldi.net> Date: Sun, 26 May 2024 18:01:34 +0900 Subject: [PATCH] Bug fix for grid_neighborhood() and add tests --- R/move.R | 4 ++-- tests/testthat/test-move.R | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/R/move.R b/R/move.R index 952ee74..e50b525 100644 --- a/R/move.R +++ b/R/move.R @@ -50,8 +50,8 @@ grid_neighborhood <- function(grid, n_X = -n:n, n_Y = -n:n) vec_slice(n_XY, - (type != "von_neumann" | abs(n_XY$n_X) == n | abs(n_XY$n_Y) == n) & - (type != "moore" | (abs(n_XY$n_X) + abs(n_XY$n_Y)) == n)) + (type != "von_neumann" | (abs(n_XY$n_X) + abs(n_XY$n_Y)) == n) & + (type != "moore" | abs(n_XY$n_X) == n | abs(n_XY$n_Y) == n)) }) neighbor <- tibble::tibble(grid = grid) |> diff --git a/tests/testthat/test-move.R b/tests/testthat/test-move.R index 15828ec..fae33d2 100644 --- a/tests/testthat/test-move.R +++ b/tests/testthat/test-move.R @@ -4,10 +4,17 @@ test_that("move", { expect_equal(grid_move(grid_10km, 1, 1), parse_grid(543900, "10km")) }) -test_that("neighbor", { +test_that("neighborhood", { grid_10km <- parse_grid(533900, "10km") - expect_true(setequal(as.character(grid_neighbor(grid_10km, n = 0:1)[[1]]), + expect_true(setequal(as.character(grid_neighborhood(grid_10km, n = 0:1, + type = "von_neumann")[[1]]), + as.character(parse_grid(c(533910, + 533807, 533900, 533901, + 523970), + grid_size = "10km")))) + expect_true(setequal(as.character(grid_neighborhood(grid_10km, n = 0:1, + type = "moore")[[1]]), as.character(parse_grid(c(533817, 533910, 533911, 533807, 533900, 533901, 523877, 523970, 523971),