inst/tinytest/binary/test-binary1d.R

# set-up ===
enumerate <- 0 # to count number of tests performed using iterations in loops
loops <- 0 # to count number of loops
errorfun <- function(tt) {
  
  if(isFALSE(tt)) stop(print(tt))
}
source(file.path(getwd(), "source.R"))


# test 1d equivalence ====

for(i in seq_along(funs)) {
  x <- array(datagens[[i]](), c(10, 1))
  y <- array(datagens[[i]](), c(10, 1))
  expect_equal(
    funs[[i]](x, y, ops[[i]]),
    funs[[i]](undim(x), y, ops[[i]])
  ) |> errorfun()
  expect_equal(
    funs[[i]](y, x, ops[[i]]),
    funs[[i]](y, undim(x), ops[[i]])
  ) |> errorfun()
  expect_equal(
    funs[[i]](y, x, ops[[i]]) |> undim(),
    funs[[i]](undim(y), undim(x), ops[[i]])
  ) |> errorfun()
  
  enumerate <- enumerate + 3L
}


# test orthogonal vectors equivalence ====

for(i in seq_along(funs)) {
  x <- array(datagens[[i]](), c(10, 1))
  y <- array(datagens[[i]](), c(1, 10))
  expect_equal(
    funs[[i]](x, y, ops[[i]]),
    funs[[i]](undim(x), y, ops[[i]])
  ) |> errorfun()
  expect_equal(
    funs[[i]](y, x, ops[[i]]),
    funs[[i]](y, undim(x), ops[[i]])
  ) |> errorfun()
  
  enumerate <- enumerate + 2L
}


# test if dimension is correctly 1d or NULL ====

for(i in seq_along(funs)) {
  x <- array(datagens[[i]](), 10L)
  y <- datagens[[i]]()[1:10]
  expect_equal(
    dim(funs[[i]](x, y, ops[[i]])),
    10L
  ) |> errorfun()
  expect_equal(
    dim(funs[[i]](y, x, ops[[i]])),
    10L
  ) |> errorfun()
  expect_equal(
    dim(funs[[i]](undim(x), y, ops[[i]])),
    NULL
  ) |> errorfun()
  expect_equal(
    dim(funs[[i]](y, undim(x), ops[[i]])),
    NULL
  ) |> errorfun()
  
  enumerate <- enumerate + 4L
}
  

Try the broadcast package in your browser

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

broadcast documentation built on Nov. 28, 2026, 1:07 a.m.