tests/testthat/test-lat2w.R

test_that("lat2w returns a correct rook contiguity matrix", {
  res <- lat2w(nrows = 3, ncols = 3, rook = TRUE)
  expect_named(res, c("nbs", "w"))
  w <- res$w
  expect_equal(dim(w), c(9, 9))
  expect_true(all(w %in% c(0, 1)))
  expect_true(all(diag(w) == 0))
  expect_equal(w, t(w))
  # corner cells have 2 neighbours, the centre cell has 4
  expect_equal(sum(w[1, ]), 2)
  expect_equal(sum(w[5, ]), 4)
})

test_that("lat2w queen case adds diagonal neighbours", {
  res <- lat2w(nrows = 3, ncols = 3, rook = FALSE)
  w <- res$w
  expect_equal(sum(w[5, ]), 8)
  expect_equal(sum(w[1, ]), 3)
})

Try the lctools package in your browser

Any scripts or data that you put into this service are public.

lctools documentation built on July 9, 2026, 9:07 a.m.