Nothing
test_that("can sum along axis 1", {
x <- array(1:10, c(5L, 2L))
out <- rray_sum(x, axes = 1L)
expect_identical(as.vector(out), c(15L, 40L))
})
test_that("can sum along axis 2", {
x <- array(1:10, c(5L, 2L))
out <- rray_sum(x, axes = 2L)
expect_identical(as.vector(out), c(7L, 9L, 11L, 13L, 15L))
})
test_that("reduced axes become size 1", {
x <- array(1:10, c(5L, 2L))
expect_identical(rray_dimensions(rray_sum(x, axes = 1L)), c(1L, 2L))
expect_identical(rray_dimensions(rray_sum(x, axes = 2L)), c(5L, 1L))
})
test_that("can reduce over all axes", {
x <- array(1:10, c(5L, 2L))
out <- rray_sum(x, axes = c(1L, 2L))
expect_identical(as.vector(out), 55L)
expect_identical(rray_dimensions(out), c(1L, 1L))
})
test_that("reducing over no axes returns the input unchanged", {
x <- array(1:10, c(5L, 2L))
out <- rray_sum(x, axes = integer())
expect_identical(out, x)
})
test_that("can reduce over multiple axes of a 3D array", {
x <- array(1, c(2L, 3L, 4L))
out <- rray_sum(x, axes = c(1L, 2L))
expect_identical(as.vector(out), rep(6, 4L))
expect_identical(rray_dimensions(out), c(1L, 1L, 4L))
})
test_that("can reduce axis 3", {
x <- array(1:24, c(2L, 3L, 4L))
out <- rray_sum(x, axes = 3L)
expect_identical(rray_dimensions(out), c(2L, 3L, 1L))
expect_identical(as.vector(out), c(40L, 44L, 48L, 52L, 56L, 60L))
})
test_that("coalesces reduction axes", {
x <- array(1:24, c(1L, 3L, 2L, 4L))
expected <- function(axes) {
dimensions <- dim(x)
dimensions[axes] <- 1L
kept_axes <- setdiff(seq_along(dimensions), axes)
values <- if (length(kept_axes)) {
apply(x, kept_axes, sum)
} else {
sum(x)
}
array(values, dimensions)
}
axes <- list(1L, 2L, 3L, 4L, c(1L, 3L), c(2L, 4L), c(4L, 2L), 1:4)
for (axis in axes) {
expect_identical(rray_sum(x, axes = axis), expected(axis))
expect_identical(rray_sum_forced_fallback(x, axes = axis), expected(axis))
}
})
test_that("axes can be in any order", {
x <- array(
1:24,
c(2L, 3L, 4L),
dimnames = list(A = c("a", "b"), B = c("c", "d", "e"), C = NULL)
)
expect_identical(rray_sum(x, axes = c(3L, 1L)), rray_sum(x, axes = c(1L, 3L)))
})
test_that("axes can be titles", {
x <- array(
1:24,
c(2L, 3L, 4L),
dimnames = list(row = c("a", "b"), col = NULL, page = NULL)
)
expect_identical(rray_sum(x, axes = "row"), rray_sum(x, axes = 1L))
expect_identical(
rray_sum(x, axes = c("page", "row")),
rray_sum(x, axes = c(1L, 3L))
)
})
test_that("a title matches the first axis with that title", {
x <- array(1:6, c(2L, 3L), dimnames = list(a = NULL, a = NULL))
expect_identical(rray_sum(x, axes = "a"), rray_sum(x, axes = 1L))
})
test_that("dimension names are kept for non-reduced axes", {
x <- array(1:10, c(5L, 2L), dimnames = list(letters[1:5], c("c1", "c2")))
expect_identical(dimnames(rray_sum(x, axes = 1L)), list(NULL, c("c1", "c2")))
expect_identical(dimnames(rray_sum(x, axes = 2L)), list(letters[1:5], NULL))
})
test_that("dimension names are dropped when all named axes are reduced", {
x <- array(1:10, c(5L, 2L), dimnames = list(letters[1:5], NULL))
expect_null(dimnames(rray_sum(x, axes = 1L)))
})
test_that("titles are kept", {
x <- array(
1:24,
c(2L, 3L, 4L),
dimnames = list(A = c("a", "b"), B = NULL, C = NULL)
)
y <- array(1:24, c(2L, 3L, 4L), dimnames = list(A = NULL, B = NULL, C = NULL))
expect_identical(rray_titles(rray_sum(x, axes = 1L)), c("A", "B", "C"))
expect_identical(dimnames(rray_sum(y, axes = 1L)), dimnames(y))
})
test_that("output type matches input type", {
expect_identical(storage.mode(rray_sum(array(1L), axes = 1L)), "integer")
expect_identical(storage.mode(rray_sum(array(1), axes = 1L)), "double")
expect_identical(storage.mode(rray_sum(array(1i), axes = 1L)), "complex")
# Logical -> integer
expect_identical(storage.mode(rray_sum(array(TRUE), axes = 1L)), "integer")
})
test_that("logical TRUE is summed as 1", {
x <- array(c(TRUE, FALSE, TRUE, TRUE), c(2L, 2L))
out <- rray_sum(x, axes = 1L)
expect_identical(as.vector(out), c(1L, 2L))
})
test_that("integer NA propagates", {
x <- array(c(1L, NA_integer_), c(2L, 1L))
expect_identical(as.vector(rray_sum(x, axes = 1L)), NA_integer_)
expect_identical(
as.vector(rray_sum_forced_fallback(x, axes = 1L)),
NA_integer_
)
x <- array(c(NA_integer_, 1L), c(2L, 1L))
expect_identical(as.vector(rray_sum(x, axes = 1L)), NA_integer_)
expect_identical(
as.vector(rray_sum_forced_fallback(x, axes = 1L)),
NA_integer_
)
})
test_that("double NA / NaN propagates", {
x <- c(1, NA_real_)
expect_identical(as.vector(rray_sum(x, axes = 1L)), NA_real_)
x <- c(NA_real_, 1)
expect_identical(as.vector(rray_sum(x, axes = 1L)), NA_real_)
x <- c(1, NaN)
expect_identical(as.vector(rray_sum(x, axes = 1L)), NaN)
x <- c(NaN, 1)
expect_identical(as.vector(rray_sum(x, axes = 1L)), NaN)
# Purposefully not comparing directly, as the result is implementation defined
x <- c(NA, NaN)
expect_identical(is.na(rray_sum(x, axes = 1L)), array(TRUE, 1L))
x <- c(NaN, NA)
expect_identical(is.na(rray_sum(x, axes = 1L)), array(TRUE, 1L))
})
test_that("Inf matches base R sum", {
expect_identical(
as.vector(rray_sum(c(Inf, 1), axes = 1L)),
sum(Inf, 1)
)
expect_identical(
as.vector(rray_sum(c(-Inf, 1), axes = 1L)),
sum(-Inf, 1)
)
expect_identical(
as.vector(rray_sum(c(Inf, NA), axes = 1L)),
sum(Inf, NA)
)
expect_identical(
as.vector(rray_sum(c(-Inf, NA), axes = 1L)),
sum(-Inf, NA)
)
expect_identical(
as.vector(rray_sum(c(Inf, NaN), axes = 1L)),
sum(Inf, NaN)
)
expect_identical(
as.vector(rray_sum(c(-Inf, NaN), axes = 1L)),
sum(-Inf, NaN)
)
# `NaN` from `Inf` + `-Inf`
expect_identical(
as.vector(rray_sum(c(Inf, -Inf), axes = 1L)),
sum(Inf, -Inf)
)
# `NaN` from `Inf` + `-Inf`, so can't trust that added to `NA`
# across platforms, it is compiler defined!
expect_true(is.na(as.vector(rray_sum(c(Inf, -Inf, NA), axes = 1L))))
# `NaN` from `Inf` + `-Inf`, but added to a plain `NaN`, so predictable
expect_identical(
as.vector(rray_sum(c(Inf, -Inf, NaN), axes = 1L)),
sum(Inf, -Inf, NaN)
)
})
test_that("complex sum works", {
x <- array(c(1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i), c(2L, 2L))
expect_identical(as.vector(rray_sum(x, axes = 1L)), c(4 + 6i, 12 + 14i))
expect_identical(as.vector(rray_sum(x, axes = 2L)), c(6 + 8i, 10 + 12i))
})
test_that("complex NA propagates independently per component", {
x <- c(1 + 2i, NA_complex_)
expect_identical(as.vector(rray_sum(x, axes = 1L)), NA_complex_)
x <- c(complex(real = 1, imaginary = NaN), 1 + 1i)
out <- as.vector(rray_sum(x, axes = 1L))
expect_identical(Re(out), 2)
expect_identical(Im(out), NaN)
})
test_that("logical NA propagates", {
x <- array(c(TRUE, NA), c(2L, 1L))
expect_identical(as.vector(rray_sum(x, axes = 1L)), NA_integer_)
x <- array(c(NA, TRUE), c(2L, 1L))
expect_identical(as.vector(rray_sum(x, axes = 1L)), NA_integer_)
})
test_that("logical NA propagates along axis 2", {
x <- rbind(c(TRUE, NA), c(TRUE, TRUE))
expect_identical(as.vector(rray_sum(x, axes = 2L)), c(NA, 2L))
expect_identical(as.vector(rray_sum(x, axes = 2L, na_rm = TRUE)), c(1L, 2L))
})
test_that("na_rm removes integer NA", {
x <- array(c(1L, NA_integer_, 3L, 4L), c(2L, 2L))
out <- rray_sum(x, axes = 1L, na_rm = TRUE)
expect_identical(as.vector(out), c(1L, 7L))
out <- rray_sum_forced_fallback(x, axes = 1L, na_rm = TRUE)
expect_identical(as.vector(out), c(1L, 7L))
})
test_that("na_rm removes double NA and NaN", {
x <- c(1, NA_real_, 3)
expect_identical(as.vector(rray_sum(x, axes = 1L, na_rm = TRUE)), 4)
x <- c(1, NaN, 3)
expect_identical(as.vector(rray_sum(x, axes = 1L, na_rm = TRUE)), 4)
x <- c(NA_real_, NaN)
expect_identical(as.vector(rray_sum(x, axes = 1L, na_rm = TRUE)), 0)
})
test_that("na_rm removes logical NA", {
x <- array(c(TRUE, NA, FALSE, TRUE), c(2L, 2L))
out <- rray_sum(x, axes = 1L, na_rm = TRUE)
expect_identical(as.vector(out), c(1L, 1L))
})
test_that("na_rm removes complex NA", {
x <- c(1 + 2i, NA_complex_)
out <- as.vector(rray_sum(x, axes = 1L, na_rm = TRUE))
expect_identical(out, 1 + 2i)
x <- c(complex(real = 1, imaginary = NaN), 1 + 1i)
out <- as.vector(rray_sum(x, axes = 1L, na_rm = TRUE))
expect_identical(Re(out), 2)
expect_identical(Im(out), 1)
})
test_that("na_rm with all NA returns identity", {
x <- c(NA, NA)
expect_identical(as.vector(rray_sum(x, axes = 1L, na_rm = TRUE)), 0L)
x <- c(NA_integer_, NA_integer_)
expect_identical(as.vector(rray_sum(x, axes = 1L, na_rm = TRUE)), 0L)
expect_identical(
as.vector(rray_sum_forced_fallback(x, axes = 1L, na_rm = TRUE)),
0L
)
x <- c(NA_real_, NA_real_)
expect_identical(as.vector(rray_sum(x, axes = 1L, na_rm = TRUE)), 0)
x <- c(NA_complex_, NA_complex_)
expect_identical(as.vector(rray_sum(x, axes = 1L, na_rm = TRUE)), 0 + 0i)
})
test_that("na_rm with all NA returns identity along axis 2", {
x <- matrix(NA, 2L, 2L)
expect_identical(as.vector(rray_sum(x, axes = 2L, na_rm = TRUE)), c(0L, 0L))
x <- matrix(NA_integer_, 2L, 2L)
expect_identical(as.vector(rray_sum(x, axes = 2L, na_rm = TRUE)), c(0L, 0L))
expect_identical(
as.vector(rray_sum_forced_fallback(x, axes = 2L, na_rm = TRUE)),
c(0L, 0L)
)
})
test_that("na_rm with no NA matches default", {
x <- array(1:10, c(5L, 2L))
expect_identical(
rray_sum(x, axes = 1L, na_rm = TRUE),
rray_sum(x, axes = 1L)
)
})
test_that("plain vector input works", {
out <- rray_sum(1:3, axes = 1L)
expect_identical(as.vector(out), 6L)
expect_identical(rray_dimensions(out), 1L)
})
test_that("scalar reduction works", {
out <- rray_sum(5, axes = 1L)
expect_identical(as.vector(out), 5)
expect_identical(rray_dimensions(out), 1L)
})
test_that("axes are coerced to integer", {
x <- array(1:4, c(2L, 2L))
expect_identical(
rray_sum(x, axes = 1),
rray_sum(x, axes = 1L)
)
})
test_that("reducing a zero-length axis gives identity value", {
x <- matrix(numeric(), 0L, 2L)
out <- rray_sum(x, axes = 1L)
expect_identical(rray_dimensions(out), c(1L, 2L))
expect_identical(as.vector(out), c(0, 0))
})
test_that("reducing over a non-zero-length axis with a zero-length axis", {
x <- matrix(numeric(), 0L, 2L)
out <- rray_sum(x, axes = 2L)
expect_identical(rray_dimensions(out), c(0L, 1L))
expect_identical(as.vector(out), numeric())
})
test_that("errors when reduced dimensions overflow", {
# A special case when a 0 dimension becomes 1 via reduction
x <- array(integer(), c(0L, rep(65536L, 4)))
expect_snapshot(rray_sum(x, axes = 1L), error = TRUE)
})
test_that("reducing all axes of a zero-length array", {
x <- matrix(numeric(), 0L, 2L)
out <- rray_sum(x, axes = c(1L, 2L))
expect_identical(rray_dimensions(out), c(1L, 1L))
expect_identical(as.vector(out), 0)
})
test_that("the identity of an empty reduction has the output type", {
zero_size <- function(x) array(x, c(0L, 1L))
expect_identical(as.vector(rray_sum(zero_size(logical()), axes = 1L)), 0L)
expect_identical(as.vector(rray_sum(zero_size(integer()), axes = 1L)), 0L)
expect_identical(
as.vector(rray_sum_forced_fallback(zero_size(integer()), axes = 1L)),
0L
)
expect_identical(as.vector(rray_sum(zero_size(double()), axes = 1L)), 0)
expect_identical(as.vector(rray_sum(zero_size(complex()), axes = 1L)), 0 + 0i)
})
test_that("summing preserves accumulation order across runs", {
x <- array(c(1e16, 1, -1e16, 1), c(2L, 2L))
expect_identical(rray_sum(x, axes = c(1L, 2L)), array(1, c(1L, 1L)))
})
test_that("errors on axes out of range", {
x <- array(1:4, c(2L, 2L))
expect_snapshot(rray_sum(x, axes = 3L), error = TRUE)
})
test_that("errors on duplicate axes", {
x <- array(1:24, c(2L, 3L, 4L))
expect_snapshot(rray_sum(x, axes = c(1L, 1L)), error = TRUE)
expect_snapshot(rray_sum(x, axes = c(2L, 1L, 2L)), error = TRUE)
})
test_that("errors on axes less than 1", {
x <- array(1:4, c(2L, 2L))
expect_snapshot(rray_sum(x, axes = 0L), error = TRUE)
})
test_that("errors on axes with NA", {
x <- array(1:4, c(2L, 2L))
expect_snapshot(rray_sum(x, axes = NA_integer_), error = TRUE)
})
test_that("errors on titles that aren't found", {
x <- array(1:4, c(2L, 2L), dimnames = list(row = NULL, NULL))
expect_snapshot(rray_sum(x, axes = "nope"), error = TRUE)
expect_snapshot(rray_sum(x, axes = c("row", "nope")), error = TRUE)
})
test_that("errors on titles when `x` has no titles", {
x <- array(1:4, c(2L, 2L))
expect_snapshot(rray_sum(x, axes = "row"), error = TRUE)
expect_snapshot(rray_sum(x, axes = character()), error = TRUE)
})
test_that("errors on missing or empty titles", {
x <- array(1:4, c(2L, 2L), dimnames = list(row = NULL, NULL))
expect_snapshot(rray_sum(x, axes = c("row", NA)), error = TRUE)
expect_snapshot(rray_sum(x, axes = ""), error = TRUE)
})
test_that("errors on duplicate titles", {
x <- array(1:4, c(2L, 2L), dimnames = list(row = NULL, col = NULL))
expect_snapshot(rray_sum(x, axes = c("row", "row")), error = TRUE)
})
test_that("errors on axes of the wrong type", {
x <- array(1:4, c(2L, 2L), dimnames = list(row = NULL, col = NULL))
expect_snapshot(rray_sum(x, axes = list(1)), error = TRUE)
expect_snapshot(rray_sum(x, axes = factor("row")), error = TRUE)
})
test_that("errors on integer overflow", {
x <- array(c(.Machine$integer.max, 1L), c(2L, 1L))
expect_snapshot(rray_sum(x, axes = 1L), error = TRUE)
expect_snapshot(rray_sum_forced_fallback(x, axes = 1L), error = TRUE)
})
test_that("errors on integer underflow", {
x <- array(c(-.Machine$integer.max, -1L), c(2L, 1L))
expect_snapshot(rray_sum(x, axes = 1L), error = TRUE)
expect_snapshot(rray_sum_forced_fallback(x, axes = 1L), error = TRUE)
})
test_that("errors on integer overflow with `na_rm = TRUE`", {
x <- array(c(.Machine$integer.max, NA_integer_, 1L), c(3L, 1L))
expect_snapshot(rray_sum(x, axes = 1L, na_rm = TRUE), error = TRUE)
expect_snapshot(
rray_sum_forced_fallback(x, axes = 1L, na_rm = TRUE),
error = TRUE
)
})
test_that("errors on integer overflow along axis 2", {
x <- rbind(c(1L, 2L), c(.Machine$integer.max, 1L))
expect_snapshot(rray_sum(x, axes = 2L), error = TRUE)
expect_snapshot(rray_sum_forced_fallback(x, axes = 2L), error = TRUE)
x <- rbind(c(1L, 2L, 3L), c(.Machine$integer.max, NA, 1L))
expect_snapshot(rray_sum(x, axes = 2L, na_rm = TRUE), error = TRUE)
expect_snapshot(
rray_sum_forced_fallback(x, axes = 2L, na_rm = TRUE),
error = TRUE
)
})
test_that("only the final integer sum is checked for overflow", {
x <- rbind(
c(.Machine$integer.max, 1L, -1L),
c(-.Machine$integer.max, -1L, 1L)
)
expected <- c(.Machine$integer.max, -.Machine$integer.max)
expect_identical(as.vector(rray_sum(x, axes = 2L)), expected)
expect_identical(as.vector(rray_sum(t(x), axes = 1L)), expected)
expect_identical(as.vector(rray_sum_forced_fallback(x, axes = 2L)), expected)
expect_identical(
as.vector(rray_sum_forced_fallback(t(x), axes = 1L)),
expected
)
})
test_that("integer sums can come back into range from far out of range", {
x <- rbind(
c(rep(.Machine$integer.max, 3L), rep(-.Machine$integer.max, 3L), -5L),
c(rep(-.Machine$integer.max, 3L), rep(.Machine$integer.max, 3L), 5L)
)
expected <- c(-5L, 5L)
expect_identical(as.vector(rray_sum(x, axes = 2L)), expected)
expect_identical(as.vector(rray_sum(t(x), axes = 1L)), expected)
expect_identical(as.vector(rray_sum_forced_fallback(x, axes = 2L)), expected)
expect_identical(
as.vector(rray_sum_forced_fallback(t(x), axes = 1L)),
expected
)
})
test_that("integer sums can land exactly on the integer limits", {
x <- rbind(
c(.Machine$integer.max - 1L, 1L),
c(-.Machine$integer.max + 1L, -1L)
)
expected <- c(.Machine$integer.max, -.Machine$integer.max)
expect_identical(as.vector(rray_sum(x, axes = 2L)), expected)
expect_identical(as.vector(rray_sum(t(x), axes = 1L)), expected)
expect_identical(as.vector(rray_sum_forced_fallback(x, axes = 2L)), expected)
expect_identical(
as.vector(rray_sum_forced_fallback(t(x), axes = 1L)),
expected
)
})
test_that("integer NA wins over overflow", {
x <- rbind(
c(NA, .Machine$integer.max, 1L),
c(.Machine$integer.max, 1L, NA)
)
expected <- c(NA_integer_, NA_integer_)
expect_identical(as.vector(rray_sum(x, axes = 2L)), expected)
expect_identical(as.vector(rray_sum(t(x), axes = 1L)), expected)
expect_identical(as.vector(rray_sum_forced_fallback(x, axes = 2L)), expected)
expect_identical(
as.vector(rray_sum_forced_fallback(t(x), axes = 1L)),
expected
)
})
test_that("each output handles its own NA and overflow", {
x <- rbind(
c(.Machine$integer.max, 1L, NA),
c(1L, 2L, 3L)
)
expected <- c(NA, 6L)
expect_identical(as.vector(rray_sum(x, axes = 2L)), expected)
expect_identical(as.vector(rray_sum(t(x), axes = 1L)), expected)
expect_identical(as.vector(rray_sum_forced_fallback(x, axes = 2L)), expected)
expect_identical(
as.vector(rray_sum_forced_fallback(t(x), axes = 1L)),
expected
)
})
test_that("an output fed by several runs is only checked at the end", {
x <- array(0L, c(2L, 2L, 2L))
x[, 1L, 1L] <- c(.Machine$integer.max, .Machine$integer.max)
x[, 1L, 2L] <- c(NA, 1L)
x[, 2L, 1L] <- c(.Machine$integer.max, .Machine$integer.max)
x[, 2L, 2L] <- c(-.Machine$integer.max, -1L)
expected <- c(NA, .Machine$integer.max - 1L)
expect_identical(as.vector(rray_sum(x, axes = c(1L, 3L))), expected)
expect_identical(
as.vector(rray_sum_forced_fallback(x, axes = c(1L, 3L))),
expected
)
})
test_that("errors when a logical sum has too many `TRUE` values", {
skip_if_not_testing_long_vectors()
x <- array(TRUE, c(2^16, 2^15 + 1))
expect_snapshot(rray_sum(x, axes = 1:2), error = TRUE)
})
test_that("integer sums past 2^32 elements fall back and error on overflow", {
skip_if_not_testing_long_vectors()
x <- array(.Machine$integer.max, c(2^16 + 1, 2^16))
expect_snapshot(rray_sum(x, axes = 1:2), error = TRUE)
expect_snapshot(rray_sum(x, axes = 1:2, na_rm = TRUE), error = TRUE)
})
test_that("integer sums past 2^32 elements fall back with NA", {
skip_if_not_testing_long_vectors()
x <- array(0L, c(2^16 + 1, 2^16))
x[1L] <- NA
x[2L] <- 5L
expect_identical(as.vector(rray_sum(x, axes = 1:2)), NA_integer_)
expect_identical(as.vector(rray_sum(x, axes = 1:2, na_rm = TRUE)), 5L)
})
test_that("`axes` must be named", {
expect_snapshot(rray_sum(1:3, 1), error = TRUE)
})
test_that("`na_rm` must be `TRUE` or `FALSE`", {
x <- array(1:4, c(2L, 2L))
expect_snapshot(rray_sum(x, axes = 1L, na_rm = NA), error = TRUE)
expect_snapshot(rray_sum(x, axes = 1L, na_rm = logical()), error = TRUE)
expect_snapshot(rray_sum(x, axes = 1L, na_rm = c(TRUE, FALSE)), error = TRUE)
expect_snapshot(rray_sum(x, axes = 1L, na_rm = 1), error = TRUE)
})
test_that("errors on non-numeric input", {
x <- array(letters[1:4], c(2L, 2L))
expect_snapshot(rray_sum(x, axes = 1L), error = TRUE)
x <- array(as.raw(1:4), c(2L, 2L))
expect_snapshot(rray_sum(x, axes = 1L), error = TRUE)
x <- array(list(1, 2, 3, 4), c(2L, 2L))
expect_snapshot(rray_sum(x, axes = 1L), error = TRUE)
})
test_that("errors on classed input", {
x <- structure(array(1:4, c(2, 2)), class = "foo")
expect_snapshot(rray_sum(x, axes = 1L), 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.