Skip to content

Commit

Permalink
Allow NULL dimnames
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Oct 27, 2023
1 parent a0f6aa6 commit 0280ae2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 0 additions & 2 deletions R/type-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ new_table <- function(x = integer(), dim = NULL, dimnames = NULL) {
abort("`dim` must be an integer vector.")
}

dimnames <- dimnames %||% vec_init(list(), length(dim))

n_elements <- prod(dim)
n_x <- length(x)

Expand Down
20 changes: 14 additions & 6 deletions tests/testthat/test-type-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,42 @@ test_that("can find a common type among tables with identical dimensions", {
tab1 <- new_table()
tab2 <- new_table(1:2, dim = c(1L, 2L, 1L))

expect_identical(vec_ptype2(tab1, tab1), zap_dimnames(new_table()))
expect_identical(vec_ptype2(tab2, tab2), zap_dimnames(new_table(dim = c(0L, 2L, 1L))))
expect_identical(vec_ptype2(tab1, tab1), new_table())
expect_identical(vec_ptype2(tab2, tab2), new_table(dim = c(0L, 2L, 1L)))
})

test_that("size is not considered in the ptype", {
x <- new_table(1:2, dim = 2L)
y <- new_table(1:3, dim = 3L)

expect_identical(vec_ptype2(x, y), zap_dimnames(new_table()))
expect_identical(vec_ptype2(x, y), new_table())
})

test_that("vec_ptype2() can broadcast table shapes", {
x <- new_table(dim = c(0L, 1L))
y <- new_table(dim = c(0L, 2L))

expect_identical(vec_ptype2(x, y), zap_dimnames(new_table(dim = c(0L, 2L))))
expect_identical(vec_ptype2(x, y), new_table(dim = c(0L, 2L)))

x <- new_table(dim = c(0L, 1L, 3L))
y <- new_table(dim = c(0L, 2L, 1L))

expect_identical(vec_ptype2(x, y), zap_dimnames(new_table(dim = c(0L, 2L, 3L))))
expect_identical(vec_ptype2(x, y), new_table(dim = c(0L, 2L, 3L)))
})

test_that("vec_ptype2() never propagates dimnames", {
x <- new_table(dim = c(0L, 1L), dimnames = list(character(), "x1"))
y <- new_table(dim = c(0L, 2L), dimnames = list(character(), c("y1", "y2")))

expect_null(dimnames(vec_ptype2(x, x)))
expect_null(dimnames(vec_ptype2(x, y)))
})

test_that("implicit axes are broadcast", {
x <- new_table(dim = c(0L, 2L))
y <- new_table(dim = c(0L, 1L, 3L))

expect_identical(vec_ptype2(x, y), zap_dimnames(new_table(dim = c(0L, 2L, 3L))))
expect_identical(vec_ptype2(x, y), new_table(dim = c(0L, 2L, 3L)))
})

test_that("errors on non-broadcastable dimensions", {
Expand Down

0 comments on commit 0280ae2

Please sign in to comment.