|
| 1 | +test_that("array() supports reshaping non-scalar data", { |
| 2 | + fn <- function(x) { |
| 3 | + declare(type(x = integer(2L, 3L, 4L))) |
| 4 | + array(as.double(x), dim = c(2L, 3L, 4L)) |
| 5 | + } |
| 6 | + |
| 7 | + set.seed(1) |
| 8 | + x <- array(sample(1:10, 24, replace = TRUE), dim = c(2L, 3L, 4L)) |
| 9 | + expect_quick_identical(fn, list(x)) |
| 10 | +}) |
| 11 | + |
| 12 | +test_that("array() reshape accepts numeric dim vectors", { |
| 13 | + fn <- function(x) { |
| 14 | + declare(type(x = integer(2L, 3L, 4L))) |
| 15 | + array(as.double(x), dim = c(2, 3, 4)) |
| 16 | + } |
| 17 | + |
| 18 | + set.seed(1) |
| 19 | + x <- array(sample(1:10, 24, replace = TRUE), dim = c(2L, 3L, 4L)) |
| 20 | + expect_quick_identical(fn, list(x)) |
| 21 | +}) |
| 22 | + |
| 23 | +test_that("array() reshape accepts scalar dims", { |
| 24 | + fn <- function(x) { |
| 25 | + declare(type(x = integer(24L))) |
| 26 | + # Rank-1 arrays carry a `dim` attribute in base R, but quickr treats them as |
| 27 | + # plain vectors; wrap in `c()` so both sides compare identically while still |
| 28 | + # exercising the `array(dim=scalar)` lowering. |
| 29 | + c(array(as.double(x), dim = 24)) |
| 30 | + } |
| 31 | + |
| 32 | + set.seed(1) |
| 33 | + x <- sample(1:10, 24, replace = TRUE) |
| 34 | + expect_quick_identical(fn, list(x)) |
| 35 | +}) |
| 36 | + |
| 37 | +test_that("array() reshape works when data is scalar-emitted (e.g. integer(n))", { |
| 38 | + fn <- function() { |
| 39 | + # `integer(3)` currently lowers to scalar `0` with a non-scalar value shape. |
| 40 | + # The array() reshape path must produce valid Fortran anyway. |
| 41 | + array(integer(3L), dim = c(1L, 3L)) |
| 42 | + } |
| 43 | + |
| 44 | + expect_quick_identical(fn, list()) |
| 45 | +}) |
| 46 | + |
| 47 | +test_that("array() reshape accepts literal dim vectors in the AST", { |
| 48 | + dim_const <- c(2L, 3L, 4L) |
| 49 | + fn <- eval(bquote(function(x) { |
| 50 | + declare(type(x = integer(2L, 3L, 4L))) |
| 51 | + array(as.double(x), dim = .(dim_const)) |
| 52 | + })) |
| 53 | + |
| 54 | + set.seed(1) |
| 55 | + x <- array(sample(1:10, 24, replace = TRUE), dim = c(2L, 3L, 4L)) |
| 56 | + expect_quick_identical(fn, list(x)) |
| 57 | +}) |
| 58 | + |
| 59 | +test_that("array() reshape accepts dim as a literal sequence (2:4)", { |
| 60 | + fn <- function(x) { |
| 61 | + declare(type(x = integer(2L, 3L, 4L))) |
| 62 | + array(as.double(x), dim = 2:4) |
| 63 | + } |
| 64 | + |
| 65 | + set.seed(1) |
| 66 | + x <- array(sample(1:10, 24, replace = TRUE), dim = c(2L, 3L, 4L)) |
| 67 | + expect_quick_identical(fn, list(x)) |
| 68 | +}) |
| 69 | + |
| 70 | +test_that("array() reshape accepts dim passed as a variable bound to a literal sequence", { |
| 71 | + fn <- function(x) { |
| 72 | + declare(type(x = integer(2L, 3L, 4L))) |
| 73 | + d <- 2:4 |
| 74 | + array(as.double(x), dim = d) |
| 75 | + } |
| 76 | + |
| 77 | + set.seed(1) |
| 78 | + x <- array(sample(1:10, 24, replace = TRUE), dim = c(2L, 3L, 4L)) |
| 79 | + expect_quick_identical(fn, list(x)) |
| 80 | +}) |
| 81 | + |
| 82 | +test_that("array() reshape fails early when recycling would be required", { |
| 83 | + fn <- function() { |
| 84 | + array(c(1L, 2L, 3L), dim = c(2L, 2L)) |
| 85 | + } |
| 86 | + |
| 87 | + # Fortran `reshape()` errors when the source is too short; quickr should stop |
| 88 | + # before generating uncompilable code. |
| 89 | + expect_error(quick(fn), "does not support recycling") |
| 90 | +}) |
| 91 | + |
| 92 | +test_that("array() fill reshape handles dim expressions that lower to comma-containing Fortran", { |
| 93 | + fn <- function(y, x) { |
| 94 | + declare(type(y = double(NA, NA)), type(x = double(nrow(y), ncol(y)))) |
| 95 | + |
| 96 | + # `dim(x)` uses the dims declared above, which dims2f() lowers to |
| 97 | + # `size(y, 1), size(y, 2)` (commas inside expressions). Codegen must not |
| 98 | + # split on commas in Fortran output. |
| 99 | + array(integer(nrow(y) * ncol(y)), dim = dim(x)) |
| 100 | + } |
| 101 | + |
| 102 | + set.seed(1) |
| 103 | + y <- matrix(runif(6), 2, 3) |
| 104 | + x <- y |
| 105 | + expect_quick_identical(fn, list(y, x)) |
| 106 | +}) |
| 107 | + |
| 108 | +test_that("array() reshape supports dim = 1 for non-scalar data", { |
| 109 | + fn <- function(x) { |
| 110 | + declare(type(x = integer(2L, 3L, 4L))) |
| 111 | + # Rank-1 length-1 arrays are scalar-like in quickr; index the first element |
| 112 | + # to compare against base R without relying on `dim` attributes. |
| 113 | + array(as.double(x), dim = 1L)[1] |
| 114 | + } |
| 115 | + |
| 116 | + set.seed(1) |
| 117 | + x <- array(sample(1:10, 24, replace = TRUE), dim = c(2L, 3L, 4L)) |
| 118 | + expect_quick_identical(fn, list(x)) |
| 119 | +}) |
| 120 | + |
| 121 | +test_that("array() forwards hoist when data needs hoisted temporaries", { |
| 122 | + fn <- function(x, y) { |
| 123 | + declare(type(x = double(2L, 2L)), type(y = double(2L, 2L))) |
| 124 | + array((x + y)[, 1], dim = c(2L, 1L)) |
| 125 | + } |
| 126 | + |
| 127 | + set.seed(1) |
| 128 | + x <- matrix(runif(4), 2, 2) |
| 129 | + y <- matrix(runif(4), 2, 2) |
| 130 | + expect_quick_identical(fn, list(x, y)) |
| 131 | +}) |
| 132 | + |
| 133 | +test_that("array() rejects empty dim vectors (dim=c())", { |
| 134 | + fn <- function(x) { |
| 135 | + declare(type(x = double(2L, 2L))) |
| 136 | + array(as.double(x), dim = c()) |
| 137 | + } |
| 138 | + |
| 139 | + # Base R errors here ("'dims' cannot be of length 0"); quickr should fail |
| 140 | + # early too, rather than emitting rank-mismatched Fortran. |
| 141 | + expect_error(quick(fn), "dim") |
| 142 | +}) |
0 commit comments