##############################################
  # dat1
  dat1 <- array(c(1:30), dim = c(dat = 1, sdate = 3, ftime = 2, lon = 5))

  # dat2
  set.seed(10)
  dat2 <- array(rnorm(10), dim = c(2, 1, 5))
  dat2_1 <- dat2
  attr(dat2_1, 'dimensions') <- c('sdate', 'time', 'region')

  # dat3
  dat3 <- array(c(1:30), dim = c(dat = 1, 3, ftime = 2, 5))

  # dat4: A character array
  dat4 <- array(paste0('a', 1:24), dim = c(b = 2, c = 3, d = 4))

##############################################
test_that("1. Input checks", {

  expect_error(
  Reorder(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
  Reorder(c(1:3)),
  "Parameter 'data' must be an array."
  )
  expect_error(
  Reorder(data = dat1, c()),
  "Parameter 'order' cannot be NULL."
  )
  expect_error(
  Reorder(data = dat1, order = list(1,2)),
  "Parameter 'order' must be a vector of numeric or character string."
  )
  expect_error(
  Reorder(data = dat1, order = TRUE),
  "Parameter 'order' must be a vector of numeric or character string."
  )
  expect_error(
  Reorder(data = dat1, order = c(-1:2)),
  "Parameter 'order' must be positive integers."
  )
  expect_error(
  Reorder(data = dat1, order = c(1:5)),
  "Parameter 'order' exceeds the dimension length of parameter 'data'."
  )
  expect_error(
  Reorder(data = dat1, order = c('dat', 'time')),
  "Parameter 'order' do not match the dimension names of parameter 'data'."
  )
  expect_error(
  Reorder(data = dat1, order = 1:3),
  paste0("The length of parameter 'order' should be the same with the ",
         "dimension length of parameter 'data'.")
  )
  expect_error(
  Reorder(data = dat1, order = 'ftime'),
  paste0("The length of parameter 'order' should be the same with the ",
         "dimension length of parameter 'data'.")
  )

})

##############################################
test_that("2. Output checks: dat1", {

  expect_equal(
    dim(Reorder(dat1, c(2,1,4,3))),
    c(sdate = 3, dat = 1, lon = 5, ftime = 2)
  )
  expect_equal(
    c(Reorder(dat1, c(2,1,4,3))[2,1,,1]),
    c(2, 8, 14, 20, 26)
  )
  expect_equal(
    dim(Reorder(dat1, c('sdate', 'dat', 'lon', 'ftime'))),
    c(sdate = 3, dat = 1, lon = 5, ftime = 2)
  )
  expect_equal(
    c(Reorder(dat1, c('sdate', 'dat', 'lon', 'ftime'))[2,1,,1]),
    c(2, 8, 14, 20, 26)
  )
  expect_equal(
    max(Reorder(dat1, c(2, 1, 4, 3)), na.rm = TRUE),
    30
  )

})


##############################################
test_that("3. Output checks: dat2", {

  expect_equal(
    dim(Reorder(dat2, c(2, 1, 3))),
    c(1, 2, 5)
  )
  expect_equal(
    c(Reorder(dat2, c(2, 1, 3))[1, , 3]),
    c(0.2945451, 0.3897943),
    tolerance = 0.0001
  )
  expect_equal(
  suppressWarnings(dim(Reorder(dat2_1, c('time', 'sdate', 'region')))),
  c(1, 2, 5)
  )
  expect_equal(
  suppressWarnings(c(Reorder(dat2_1, c('time', 'sdate', 'region'))[1, , 3])),
    c(0.2945451, 0.3897943),
    tolerance = 0.0001
  )
  expect_equal(
  suppressWarnings(
    attributes(Reorder(dat2_1, c('time', 'sdate', 'region')))$dimensions
  ),
  c('time', 'sdate', 'region')
  )

})


##############################################
test_that("4. Output checks: dat3", {

  expect_equal(
    dim(Reorder(dat3, c(4, 2, 1, 3))),
    c(5, 3, dat = 1, ftime = 2)
  )

})

##############################################
test_that("5. Output checks: dat4", {
  res <- Reorder(dat4, c('d', 'c', 'b'))
  expect_equal(
  dim(res),
  c(d = 4, c = 3, b = 2)
  )
  expect_equal(
  c(res[1, 1, ]),
  c("a1", "a2")
  )

})

