|
| 1 | +# Unit tests for size expression lowering/evaluation |
| 2 | + |
| 3 | +test_that("constant arithmetic in declared dims is evaluated", { |
| 4 | + fn <- function(x) { |
| 5 | + declare(type(x = double(2L + 3L))) |
| 6 | + sum(x) |
| 7 | + } |
| 8 | + qfn <- quick(fn) |
| 9 | + |
| 10 | + expect_error( |
| 11 | + qfn(as.double(1:4)), |
| 12 | + "length(x) must be 5, not 4", |
| 13 | + fixed = TRUE |
| 14 | + ) |
| 15 | + expect_identical(qfn(as.double(1:5)), 15) |
| 16 | + |
| 17 | + fn_pow <- function(x) { |
| 18 | + declare(type(x = double(2L^3L))) |
| 19 | + sum(x) |
| 20 | + } |
| 21 | + qfn_pow <- quick(fn_pow) |
| 22 | + |
| 23 | + expect_error( |
| 24 | + qfn_pow(as.double(1:7)), |
| 25 | + "length(x) must be 8, not 7", |
| 26 | + fixed = TRUE |
| 27 | + ) |
| 28 | + expect_identical(qfn_pow(as.double(1:8)), 36) |
| 29 | +}) |
| 30 | + |
| 31 | +test_that("dim/length/nrow/ncol are supported in allocation sizes", { |
| 32 | + vec <- function(x) { |
| 33 | + declare(type(x = double(NA))) |
| 34 | + out <- double(length(x) + 1L) |
| 35 | + length(out) |
| 36 | + } |
| 37 | + expect_quick_identical(vec, list(as.double(1:3)), list(as.double(1))) |
| 38 | + |
| 39 | + mat <- function(x) { |
| 40 | + declare(type(x = double(NA, NA))) |
| 41 | + out <- double(length(x) + 1L) |
| 42 | + length(out) |
| 43 | + } |
| 44 | + expect_quick_identical(mat, list(matrix(as.double(1:12), nrow = 3, ncol = 4))) |
| 45 | + |
| 46 | + mat_dims <- function(x) { |
| 47 | + declare(type(x = double(NA, NA))) |
| 48 | + out1 <- double(dim(x)[2L] + 1L) |
| 49 | + out2 <- double(nrow(x) + ncol(x)) |
| 50 | + c(length(out1), length(out2)) |
| 51 | + } |
| 52 | + expect_quick_identical( |
| 53 | + mat_dims, |
| 54 | + list(matrix(as.double(1:12), nrow = 3, ncol = 4)), |
| 55 | + list(matrix(as.double(1:6), nrow = 2, ncol = 3)) |
| 56 | + ) |
| 57 | +}) |
| 58 | + |
| 59 | +test_that("dim(x)[axis] errors when axis exceeds rank", { |
| 60 | + bad <- function(x) { |
| 61 | + declare(type(x = double(NA, NA))) |
| 62 | + double(dim(x)[3L]) |
| 63 | + } |
| 64 | + |
| 65 | + expect_error(quick(bad), "insufficient rank", fixed = TRUE) |
| 66 | +}) |
0 commit comments