tests/testthat/test-fromJSON-array.R

test_that("fromJSON Array, row major", {
  # test high dimensional arrays
  lapply(2:5, function(n) {
    object <- array(1:prod(n), dim = 1:n)
    newobject <- fromJSON(toJSON(object))
    expect_equal(object, newobject)
  })

  # adding some flat dimensions
  lapply(1:5, function(n) {
    object <- array(1:prod(n), dim = c(1:n, 1))
    newobject <- fromJSON(toJSON(object))
    expect_equal(object, newobject)
  })
})

test_that("fromJSON Array, column major", {
  # test high dimensional arrays
  lapply(2:5, function(n) {
    object <- array(1:prod(n), dim = 1:n)
    newobject <- fromJSON(toJSON(object, matrix = "columnmajor"), columnmajor = TRUE)
    expect_equal(object, newobject)
  })

  # adding some flat dimensions
  lapply(1:5, function(n) {
    object <- array(1:prod(n), dim = c(1:n, 1))
    newobject <- fromJSON(toJSON(object, matrix = "columnmajor"), columnmajor = TRUE)
    expect_equal(object, newobject)
  })
})


test_that("fromJSON Array, character strings", {
  # test high dimensional arrays
  lapply(2:5, function(n) {
    object <- array(paste("cell", 1:prod(n)), dim = 1:n)
    newobject <- fromJSON(toJSON(object, matrix = "columnmajor"), columnmajor = TRUE)
    expect_equal(object, newobject)
  })

  # adding some flat dimensions
  lapply(1:5, function(n) {
    object <- array(paste("cell", 1:prod(n)), dim = c(1:n, 1))
    newobject <- fromJSON(toJSON(object, matrix = "columnmajor"), columnmajor = TRUE)
    expect_equal(object, newobject)
  })
})

Try the jsonlite package in your browser

Any scripts or data that you put into this service are public.

jsonlite documentation built on April 4, 2025, 5:24 a.m.