Nothing
test_that("adds elementwise", {
x <- array(1:6, c(3L, 2L))
y <- array(6:1, c(3L, 2L))
expect_identical(rray_add(x, y), array(rep(7L, 6L), c(3L, 2L)))
})
test_that("broadcasts along an axis", {
x <- array(1:6, c(3L, 2L))
y <- array(c(10L, 20L), c(1L, 2L))
expect_identical(
rray_add(x, y),
array(c(11L, 12L, 13L, 24L, 25L, 26L), c(3L, 2L))
)
})
test_that("broadcasts both inputs at once", {
x <- array(1:3, c(3L, 1L))
y <- array(c(10L, 20L), c(1L, 2L))
expect_identical(
rray_add(x, y),
array(c(11L, 12L, 13L, 21L, 22L, 23L), c(3L, 2L))
)
})
test_that("dimensionality grows to the larger input", {
x <- array(1:3, 3L)
y <- array(1:6, c(3L, 2L))
out <- rray_add(x, y)
expect_identical(rray_dimensions(out), c(3L, 2L))
expect_identical(as.vector(out), c(2L, 4L, 6L, 5L, 7L, 9L))
})
test_that("works with 3D arrays", {
x <- array(1:24, c(2L, 3L, 4L))
y <- array(1L, c(2L, 1L, 1L))
out <- rray_add(x, y)
expect_identical(rray_dimensions(out), c(2L, 3L, 4L))
expect_identical(as.vector(out), 2:25)
})
test_that("plain vectors are normalized to 1D arrays", {
out <- rray_add(1:3, 1L)
expect_identical(rray_dimensions(out), 3L)
expect_identical(as.vector(out), 2:4)
})
test_that("the output type of every pair of native types", {
expect_snapshot(native_ptype_matrix(rray_add, c("x", "y")))
})
test_that("logical and integer combinations give an integer array", {
expect_identical(rray_add(TRUE, TRUE), array(2L, 1L))
expect_identical(rray_add(TRUE, 1L), array(2L, 1L))
expect_identical(rray_add(1L, TRUE), array(2L, 1L))
expect_identical(rray_add(1L, 1L), array(2L, 1L))
})
test_that("double wins over logical and integer, in either position", {
expect_identical(rray_add(TRUE, 1.5), array(2.5, 1L))
expect_identical(rray_add(1.5, TRUE), array(2.5, 1L))
expect_identical(rray_add(1L, 1.5), array(2.5, 1L))
expect_identical(rray_add(1.5, 1L), array(2.5, 1L))
expect_identical(rray_add(1.5, 1.5), array(3, 1L))
})
test_that("complex wins over everything else, in either position", {
expect_identical(rray_add(TRUE, 1i), array(1 + 1i, 1L))
expect_identical(rray_add(1i, TRUE), array(1 + 1i, 1L))
expect_identical(rray_add(1L, 1i), array(1 + 1i, 1L))
expect_identical(rray_add(1i, 1L), array(1 + 1i, 1L))
expect_identical(rray_add(1.5, 1i), array(1.5 + 1i, 1L))
expect_identical(rray_add(1i, 1.5), array(1.5 + 1i, 1L))
expect_identical(rray_add(1 + 2i, 3 + 4i), array(4 + 6i, 1L))
})
test_that("mixed type missing values match base R", {
expect_identical(as.vector(rray_add(NA, 1L)), NA + 1L)
expect_identical(as.vector(rray_add(NA, 1.5)), NA + 1.5)
expect_identical(as.vector(rray_add(1.5, NA)), 1.5 + NA)
expect_identical(as.vector(rray_add(NA_integer_, 1.5)), NA_integer_ + 1.5)
expect_identical(as.vector(rray_add(1.5, NA_integer_)), 1.5 + NA_integer_)
})
test_that("casting into complex zeroes the imaginary part", {
out <- as.vector(rray_add(NA, 1i))
expect_identical(Re(out), NA_real_)
expect_identical(Im(out), 1)
out <- as.vector(rray_add(NA_integer_, 1i))
expect_identical(Re(out), NA_real_)
expect_identical(Im(out), 1)
})
test_that("logical missing values propagate", {
expect_identical(rray_add(NA, TRUE), array(NA_integer_, 1L))
expect_identical(rray_add(TRUE, NA), array(NA_integer_, 1L))
expect_identical(rray_add(1L, NA), array(NA_integer_, 1L))
})
test_that("integer missing values propagate", {
expect_identical(rray_add(NA_integer_, 1L), array(NA_integer_, 1L))
expect_identical(rray_add(1L, NA_integer_), array(NA_integer_, 1L))
})
test_that("double missing values propagate", {
expect_identical(rray_add(NA_real_, 1), array(NA_real_, 1L))
expect_identical(rray_add(1, NA_real_), array(NA_real_, 1L))
expect_identical(rray_add(NaN, 1), array(NaN, 1L))
expect_identical(rray_add(1, NaN), array(NaN, 1L))
})
test_that("complex missing values propagate", {
expect_identical(rray_add(NA_complex_, 1 + 1i), array(NA_complex_, 1L))
out <- as.vector(rray_add(complex(real = 1, imaginary = NaN), 1 + 1i))
expect_identical(Re(out), 2)
expect_identical(Im(out), NaN)
})
test_that("infinities match base R", {
expect_identical(as.vector(rray_add(Inf, 1)), Inf + 1)
expect_identical(as.vector(rray_add(Inf, -Inf)), Inf + -Inf)
expect_identical(as.vector(rray_add(Inf, NA_real_)), Inf + NA_real_)
expect_identical(as.vector(rray_add(Inf, NaN)), Inf + NaN)
})
test_that("missing values win over integer overflow", {
expect_identical(
rray_add(NA_integer_, .Machine$integer.max),
array(NA_integer_, 1L)
)
expect_identical(
rray_add(-.Machine$integer.max, NA_integer_),
array(NA_integer_, 1L)
)
})
test_that("broadcasting carries through alternating singleton axes", {
x <- array(1:6, c(2L, 1L, 3L, 1L))
y <- array(as.double(11:18), c(1L, 4L, 1L, 2L))
expected_x <- x[, rep(1L, 4L), , rep(1L, 2L), drop = FALSE]
expected_y <- y[rep(1L, 2L), , rep(1L, 3L), , drop = FALSE]
expect_identical(rray_add(x, y), expected_x + expected_y)
expect_identical(rray_add(y, x), expected_y + expected_x)
})
test_that("coalesces compatible iterator axes", {
x <- array(1:12, c(1L, 3L, 4L))
y <- array(12:1, c(1L, 3L, 4L))
expect_identical(rray_add(x, y), x + y)
x <- array(1:4, c(1L, 1L, 4L))
y <- array(10:12, c(1L, 3L, 1L))
expected_x <- x[, rep(1L, 3L), , drop = FALSE]
expected_y <- y[,, rep(1L, 4L), drop = FALSE]
expect_identical(rray_add(x, y), expected_x + expected_y)
x <- array(1:8, c(2L, 1L, 4L))
y <- array(11:22, c(1L, 3L, 4L))
expected_x <- x[, rep(1L, 3L), , drop = FALSE]
expected_y <- y[rep(1L, 2L), , , drop = FALSE]
expect_identical(rray_add(x, y), expected_x + expected_y)
x <- array(1:6, c(1L, 1L, 1L, 6L))
y <- array(11:16, c(1L, 1L, 1L, 6L))
expect_identical(rray_add(x, y), x + y)
})
test_that("coalesces the maximum number of axes", {
dimensions <- c(rep(1L, 62L), 2L, 3L)
x <- array(1:6, dimensions)
expect_identical(rray_add(x, 1L), array(2:7, dimensions))
dimensions <- rep(1L, 64L)
expect_identical(
rray_add(array(1L, dimensions), 1L),
array(2L, dimensions)
)
})
test_that("names are kept for axes that aren't broadcast", {
x <- array(1:6, c(3L, 2L), dimnames = list(c("a", "b", "c"), NULL))
y <- array(1:2, c(1L, 2L), dimnames = list("z", c("c1", "c2")))
expect_identical(
rray_names(rray_add(x, y)),
list(c("a", "b", "c"), c("c1", "c2"))
)
})
test_that("names must agree on an axis both inputs name", {
x <- array(1:3, 3L, dimnames = list(c("a", "b", "c")))
y <- array(1:3, 3L, dimnames = list(c("x", "y", "z")))
expect_identical(rray_names(rray_add(x, x)), list(c("a", "b", "c")))
expect_snapshot(error = TRUE, rray_add(x, y))
})
test_that("titles must agree on an axis", {
x <- array(1:2, 2L, dimnames = list(Row = c("r1", "r2")))
y <- array(1:2, 2L, dimnames = list(Row = NULL))
z <- array(1:2, 2L, dimnames = list(Sample = c("r1", "r2")))
expect_identical(rray_names(rray_add(x, y)), list(Row = c("r1", "r2")))
expect_snapshot(error = TRUE, rray_add(x, z))
})
test_that("an input without a title has no say on titles", {
x <- array(1:2, 2L, dimnames = list(Row = c("r1", "r2")))
y <- array(1:2, 2L, dimnames = list(c("r1", "r2")))
expect_identical(rray_titles(rray_add(x, y)), "Row")
expect_identical(rray_titles(rray_add(y, x)), "Row")
})
test_that("a broadcast axis still has a say on titles", {
x <- array(
1:3,
c(1L, 3L),
dimnames = list(Row = "total", Col = c("a", "b", "c"))
)
y <- array(
1:6,
c(2L, 3L),
dimnames = list(Sample = c("r1", "r2"), Col = c("a", "b", "c"))
)
expect_snapshot(error = TRUE, rray_add(x, y))
})
test_that("removing names from one input resolves a conflict", {
x <- array(1:2, 2L, dimnames = list(c("r1", "r2")))
y <- array(1:2, 2L, dimnames = list(c("a", "b")))
expect_identical(
rray_add(x, rray_set_names(y, NULL)),
array(c(2L, 4L), 2L, dimnames = list(c("r1", "r2")))
)
})
test_that("names of a named vector must agree", {
x <- array(1:4, c(2L, 2L), dimnames = list(c("r1", "r2"), c("c1", "c2")))
expect_snapshot(error = TRUE, rray_add(x, c(a = 10L, b = 20L)))
expect_identical(
rray_names(rray_add(x, c(r1 = 10L, r2 = 20L))),
rray_names(x)
)
})
test_that("names are dropped for a broadcast axis", {
x <- array(1L, c(1L, 2L), dimnames = list("z", NULL))
y <- array(1:6, c(3L, 2L))
expect_null(rray_names(rray_add(x, y)))
})
test_that("a broadcast axis has no say on names", {
x <- array(1:3, c(1L, 3L), dimnames = list("total", c("a", "b", "c")))
y <- array(
1:6,
c(2L, 3L),
dimnames = list(c("r1", "r2"), c("a", "b", "c"))
)
expect_identical(rray_names(rray_add(x, y)), rray_names(y))
})
test_that("names travel to a new axis of a larger input", {
x <- array(1:3, 3L, dimnames = list(c("a", "b", "c")))
y <- array(1:6, c(3L, 2L))
expect_identical(rray_names(rray_add(x, y)), list(c("a", "b", "c"), NULL))
})
test_that("unnamed inputs give an unnamed output", {
x <- array(1:6, c(3L, 2L))
expect_null(rray_names(rray_add(x, x)))
})
test_that("a zero dimension broadcasts against a dimension of 1", {
x <- array(1L, c(1L, 2L))
y <- array(integer(), c(0L, 2L))
out <- rray_add(x, y)
expect_identical(rray_dimensions(out), c(0L, 2L))
expect_identical(as.vector(out), integer())
})
test_that("every axis can be zero", {
x <- array(1L, c(1L, 1L))
y <- array(integer(), c(0L, 0L))
expect_identical(rray_dimensions(rray_add(x, y)), c(0L, 0L))
})
test_that("zero size 1D arrays work", {
out <- rray_add(integer(), 1L)
expect_identical(rray_dimensions(out), 0L)
expect_identical(as.vector(out), integer())
})
test_that("works with long vectors", {
skip_if_not_testing_long_vectors()
x <- array(FALSE, c(2^16, 2^15))
x[length(x)] <- TRUE
out <- rray_add(x, 1L)
expect_identical(out[c(1, length(out))], c(1L, 2L))
})
test_that("errors on incompatible dimensions", {
x <- array(1:6, c(3L, 2L))
expect_snapshot(rray_add(x, array(1L, c(2L, 2L))), error = TRUE)
expect_snapshot(rray_add(x, array(integer(), c(0L, 2L))), error = TRUE)
})
test_that("errors on types `+` doesn't support", {
expect_snapshot(rray_add("a", "b"), error = TRUE)
expect_snapshot(rray_add(as.raw(1), as.raw(1)), error = TRUE)
expect_snapshot(rray_add(list(1), list(2)), error = TRUE)
expect_snapshot(rray_add(1L, "a"), error = TRUE)
expect_snapshot(rray_add(as.raw(1), 1L), error = TRUE)
})
test_that("a type error beats a dimension error", {
x <- array("a", c(2L, 2L))
y <- array("b", c(3L, 3L))
expect_snapshot(rray_add(x, y), error = TRUE)
})
test_that("errors on integer overflow", {
expect_snapshot(rray_add(.Machine$integer.max, 1L), error = TRUE)
})
test_that("errors on integer underflow", {
expect_snapshot(rray_add(-.Machine$integer.max, -1L), error = TRUE)
})
test_that("can reach the integer limits without overflowing", {
expect_identical(
rray_add(.Machine$integer.max - 1L, 1L),
array(.Machine$integer.max, 1L)
)
expect_identical(
rray_add(-.Machine$integer.max + 1L, -1L),
array(-.Machine$integer.max, 1L)
)
expect_identical(
rray_add(.Machine$integer.max, -.Machine$integer.max),
array(0L, 1L)
)
})
test_that("errors on scalar input", {
expect_snapshot(rray_add(NULL, 1L), error = TRUE)
expect_snapshot(rray_add(1L, NULL), error = TRUE)
})
test_that("errors on classed input", {
x <- structure(array(1:4, c(2L, 2L)), class = "foo")
expect_snapshot(rray_add(x, 1L), error = TRUE)
expect_snapshot(rray_add(1L, x), error = TRUE)
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.