context("Generic tests")
test_that("Sanity checks", {
  expect_error(
    CST_SplitDim(data = 1),
    paste0("Parameter 'data' must be of the class 's2dv_cube', as output by ",
           "CSTools::CST_Load."))

  data <- 1 : 20
  dim(data) <- c(time = 20)
  data <- list(data = data)
  class(data) <- 's2dv_cube'
  expect_error(
    CST_SplitDim(data = data),
    paste0("Parameter 'freq' must be a integer number indicating ", 
           " the length of each chunk."))
indices <- c(rep(1,5), rep(2,5), rep (3, 5), rep(4, 5))
output = matrix(data$data, nrow = 5, ncol = 4)
names(dim(output)) <- c('time', 'monthly')
output <- list(data = output) 
class(output) <- 's2dv_cube'
  expect_equal(
    CST_SplitDim(data = data, indices = indices), output)
output = matrix(data$data, nrow = 5, ncol = 4)
names(dim(output)) <- c('time', 'index')
output <- list(data = output)
class(output) <- 's2dv_cube'
  expect_equal(
    CST_SplitDim(data = data, freq = 5), output)

time <- c(seq(ISOdate(1903, 1, 1), ISOdate(1903, 1, 4), "days"),
          seq(ISOdate(1903, 2, 1), ISOdate(1903, 2, 4), "days"),
          seq(ISOdate(1904, 1, 1), ISOdate(1904, 1, 2), "days"))
data <- list(data = data$data, Dates = time)
class(data) <- 's2dv_cube' 
  expect_error(
    CST_SplitDim(data = data),
    paste0("Parameter 'indices' has different length of parameter data ",
           "in the dimension supplied in 'split_dim'."))
time <- c(seq(ISOdate(1903, 1, 1), ISOdate(1903, 1, 8), "days"),
          seq(ISOdate(1903, 2, 1), ISOdate(1903, 2, 8), "days"),
          seq(ISOdate(1904, 1, 1), ISOdate(1904, 1, 4), "days"))
data <- list(data = data$data, Dates = time)
class(data) <- 's2dv_cube'
output <- c(data$data, rep(NA, 4))
dim(output) <- c(time = 8, monthly = 3)
result <- data
result$data <- output

  expect_equal(
    CST_SplitDim(data = data), result)

  exp_cor <- 1 : 20
  dim(exp_cor) <- 20
  exp_cor <- list(data = exp_cor)
  class(exp_cor) <- 's2dv_cube'
  expect_error(
    CST_SplitDim(data = exp_cor, freq = 5),
    "Parameter 'data' must have dimension names.")
 # expect_error(
 #   CST_SplitDim(data, freq = 'x'),
 #   paste0("Parameter 'freq' must be numeric or a character: by 'day', ",
 #          "'month', 'year' or 'monthly' (for distinguishable month)."))
  
library(CSTools)
  expect_error(
    CST_SplitDim(data = lonlat_data$exp),
    "Parameter 'split_dims' must be one of the dimension names in parameter 'data'.")           
  output <- lonlat_data$exp$data
  output <- abind(output[, , , 1, ,], output[, , , 2, ,], output[, , , 3, ,], along = 5)
  dim(output) <- c(dataset = 1, member = 15, sdate = 6, ftime = 1, 
                   lat = 22, lon = 53, monthly = 3)
  result <- lonlat_data$exp
  result$data <- output
  expect_equal(CST_SplitDim(data = lonlat_data$exp, split_dim = 'ftime'),
               result)
})
