tests/testthat/test-cpp_customize.R

test_that("default weights match the original graph cost", {
  g <- toy_graph()
  cch <- cpp_contract(g, customizable = TRUE, silent = TRUE)
  metric <- cpp_customize(cch)
  expect_s3_class(metric, "cppRouting_cch_metric")

  d_metric <- get_distance_pair(metric, "1", "6")
  d_normal <- get_distance_pair(g, "1", "6")
  expect_equal(d_metric, d_normal, tolerance = 1e-8)
})

test_that("custom weights change the result as expected", {
  g <- toy_graph()
  cch <- cpp_contract(g, customizable = TRUE, silent = TRUE)

  original_weights <- g$data$dist
  inflated_weights <- original_weights * 10 # scaling every edge inflates every path by the same factor
  metric_default <- cpp_customize(cch)
  metric_inflated <- cpp_customize(cch, weights = inflated_weights)

  d_default <- get_distance_pair(metric_default, "1", "6")
  d_inflated <- get_distance_pair(metric_inflated, "1", "6")
  expect_equal(d_inflated, d_default * 10, tolerance = 1e-6)
})

test_that("weights are validated: wrong length, NA and negative are rejected", {
  g <- toy_graph()
  cch <- cpp_contract(g, customizable = TRUE, silent = TRUE)
  n <- nrow(g$data)

  expect_error(cpp_customize(cch, weights = rep(1, n - 1)))
  expect_error(cpp_customize(cch, weights = c(NA, rep(1, n - 1))))
  expect_error(cpp_customize(cch, weights = c(-1, rep(1, n - 1))))
})

test_that("weights accepts a column name (dist and aux columns)", {
  e <- toy_edges()
  g <- cppRouting::makegraph(e, directed = TRUE, aux = data.frame(time = e$dist * 2))
  cch <- cpp_contract(g, customizable = TRUE, silent = TRUE)

  d_default <- get_distance_pair(cpp_customize(cch), "1", "6")
  d_dist_name <- get_distance_pair(cpp_customize(cch, weights = "dist"), "1", "6")
  expect_equal(d_dist_name, d_default, tolerance = 1e-8)

  d_time <- get_distance_pair(cpp_customize(cch, weights = "time"), "1", "6")
  expect_equal(d_time, d_default * 2, tolerance = 1e-8)
})

test_that("weights rejects unknown column names and unnameable aux", {
  e <- toy_edges()
  g <- cppRouting::makegraph(e, directed = TRUE)
  cch <- cpp_contract(g, customizable = TRUE, silent = TRUE)
  expect_error(cpp_customize(cch, weights = "time"), "Import additional weights")

  g_aux <- cppRouting::makegraph(e, directed = TRUE, aux = data.frame(time = e$dist))
  cch_aux <- cpp_contract(g_aux, customizable = TRUE, silent = TRUE)
  expect_error(cpp_customize(cch_aux, weights = "nope"), "not a known column")

  g_plain_aux <- cppRouting::makegraph(e, directed = TRUE, aux = rep(1, nrow(e)))
  cch_plain_aux <- cpp_contract(g_plain_aux, customizable = TRUE, silent = TRUE)
  expect_error(cpp_customize(cch_plain_aux, weights = "time"), "unnamed aux")

  expect_error(cpp_customize(cch_aux, weights = c("time", "time")))
})

test_that("only a cpp_contract(customizable = TRUE) object is accepted", {
  g <- toy_graph()
  ch <- cpp_contract(g, silent = TRUE) # classical, not customizable
  expect_error(cpp_customize(ch))
  expect_error(cpp_customize(g))
})

test_that("querying an un-customized CCH topology errors with a clear message", {
  g <- toy_graph()
  cch <- cpp_contract(g, customizable = TRUE, silent = TRUE)
  expect_error(get_distance_pair(cch, "1", "6"), "cpp_customize")
})

Try the cppRouting package in your browser

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

cppRouting documentation built on Nov. 12, 2026, 5:11 p.m.