Skip to content

Commit

Permalink
Bug fix for grid_neighborhood() and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UchidaMizuki committed May 26, 2024
1 parent 0183e3e commit 243bc2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions R/move.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) |>
Expand Down
11 changes: 9 additions & 2 deletions tests/testthat/test-move.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 243bc2b

Please sign in to comment.