tests/testthat/test-region.R

test_that("standalone regions reuse partitions and preserve grid orientation", {
 r <- vc_region(function(x)x^2,function(x)2+x,c(0,2),n=4,n_u=3)
 expect_equal(r$partition,vc_partition(c(0,2),4))
 expect_equal(r$sections$outer,seq(0,2,length.out=5))
 expect_equal(r$sections$lower,r$sections$outer^2)
 expect_equal(r$sections$upper,2+r$sections$outer)
 expect_equal(dim(r$grid$x),c(4L,5L))
 expect_equal(r$grid$x[1,],r$sections$outer)
 expect_equal(r$grid$y[1,],r$sections$lower)
 expect_equal(r$grid$y[4,],r$sections$upper)
 expect_equal(r$grid$y[2,],r$sections$lower+(r$sections$upper-r$sections$lower)/3)
 expect_equal(r$strips,r$partition$cells)
 p <- vc_partition(breaks=c(0,.1,.4,1));p$cells$width <- 99
 q <- vc_region(identity,function(x)2,partition=p,n_u=2)
 expect_equal(q$partition,vc_partition(breaks=c(0,.1,.4,1)))
 expect_equal(q$sections$outer,c(0,.1,.4,1))
})
test_that("regions retain degeneracies and reject sampled reversals", {
 r <- vc_region(identity,identity,c(0,1),n=2,n_u=1)
 expect_equal(r$grid$y[1,],r$grid$y[2,])
 expect_error(vc_region(function(x)1,function(x)0,c(0,1)),"lower")
 expect_error(vc_region(function(x)NA_real_,identity,c(0,1)))
 expect_error(vc_region(function(x)c(0,1),identity,c(0,1)))
 expect_error(vc_region(identity,identity,c(0,0)))
 expect_error(vc_region(identity,identity,c(0,1),n_u=0))
 expect_error(vc_region(identity,identity,c(0,1),partition=list()))
 expect_error(vc_region(function(x)-1e308,function(x)1e308,c(0,1)),"width")
 # Crossing between nodes remains unknown: sampled order is not a certificate.
 r <- vc_region(function(x)0,function(x)cos(2*pi*x),c(0,1),n=1,n_u=1)
 expect_equal(r$sections$upper,c(1,1))
 expect_lt(cos(2*pi*.5),0)
 expect_match(r$note,"not exact")
})
test_that("2D and 3D views use stored physical region coordinates only", {
 skip_if_not_installed("plotly")
 r <- vc_region(function(x)x^2,function(x)2+x,c(0,2),n=4,n_u=3)
 a <- plotly::plotly_build(vc_plot_region(r))$x$data[[1]]
 expect_equal(as.numeric(a$x),c(r$sections$outer,rev(r$sections$outer),0))
 expect_equal(as.numeric(a$y),c(r$sections$lower,rev(r$sections$upper),0))
 p <- plotly::plotly_build(vc_plot_region(r,"3d",plane_z=-3))
 surf <- p$x$data[[1]]
 expect_equal(unname(surf$x),unname(r$grid$x),ignore_attr=TRUE)
 expect_equal(unname(surf$y),unname(r$grid$y),ignore_attr=TRUE)
 expect_true(all(surf$z == -3))
 expect_equal(dim(surf$z),c(4L,5L))
 expect_equal(length(p$x$data),2)
 line <- p$x$data[[2]]
 expect_equal(as.numeric(line$x)[1:3],c(0,0,NA))
 expect_equal(as.numeric(line$y)[1:3],c(0,2,NA))
 expect_equal(as.numeric(line$z)[1:3],c(-3,-3,NA))
 expect_equal(length(plotly::plotly_build(vc_plot_region(r,"3d",show_grid=FALSE))$x$data),1)
 expect_error(vc_plot_region(list()),"Invalid")
 expect_error(vc_plot_region(r,"3d",plane_z=Inf))
 expect_error(vc_plot_region(r,show_grid=NA))
})

Try the vectorialcalculus package in your browser

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

vectorialcalculus documentation built on Nov. 17, 2026, 1:09 a.m.