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

  data1 <- 1 : 20
  data1 <- list(data = data1)
  class(data1) <- 's2dv_cube'
  expect_error(
    CST_RegimesAssign(data = data1,ref_maps=1),
    paste0("Parameter 'data' must be an array with named dimensions."))

  data1 <- 1 : 20
  dim(data1) <- c(lat = 5, lon=4)
  data1 <- list(data = data1 , lat=1:5)
  class(data1) <- 's2dv_cube'
  expect_error(
    CST_RegimesAssign(data = data1,ref_maps = 1),
    paste0("Parameter 'ref_maps' must be an array with named dimensions."))

  regimes <- 1:20
  dim(regimes) <- c(lat = 5, lon=2, cluster=2)
  expect_error(
    CST_RegimesAssign(data = data1,ref_maps = regimes),
    paste0("Parameter 'data' must have temporal dimensions."))

  
  data1 <- 1 : 20
  dim(data1) <- c(time=20)
  data1 <- list(data = data1)
  class(data1) <- 's2dv_cube'
  
  regimes <- 1:20
  dim(regimes) <- c(lat = 5, lon=2, cluster=2)
  
  expect_error(
    CST_RegimesAssign(data = data1,ref_maps = regimes),
    paste0("Parameter 'lat' must be specified."))


  data1 <- 1 : 20
  dim(data1) <- c(time=20)
  data1 <- list(data = data1,lat=1:5)
  class(data1) <- 's2dv_cube'
  
  expect_error(
    CST_RegimesAssign(data = data1,ref_maps = regimes),
    paste0("Parameter 'data' must contain the named dimensions 'lat' and 'lon'."))
  
  data1 <- 1: 20
  dim(data1) <- c(lat = 2, lon=5, time=2)
  data1 <- list(data = data1, lat=1:5)
  class(data1) <- 's2dv_cube'
  
  expect_error(
    CST_RegimesAssign(data = data1,ref_maps = regimes),
    " Parameter 'lat' does not match with the dimension 'lat' in the 
         parameter 'data' or in the parameter 'ref_maps'.")

  
  data1 <- 1: 20
  dim(data1) <- c(lat = 5, lon=2, time=2)
  data1 <- list(data = data1, lat=1:5)
  class(data1) <- 's2dv_cube'
  
  expect_equal(names(CST_RegimesAssign(data = data1, ref_maps = regimes)$statistics),
                 c('cluster', 'frequency'))
  
  expect_equal(names(
    CST_RegimesAssign(
      data = data1,
      ref_maps = regimes,
      composite = TRUE)$statistics), c('pvalue', 'cluster', 'frequency'))
  
  expect_equal(names(dim(
    CST_RegimesAssign(
      data = data1,
      ref_maps = regimes,
      composite = TRUE)$data)), c('lon', 'lat', 'composite.cluster'))
  
  data1 <- 1: 160
  dim(data1) <- c(lat = 5, lon=2, time=2, member=8)
  data1 <- list(data = data1, lat=1:5)
  class(data1) <- 's2dv_cube'
  
  expect_equal(names(dim(
    CST_RegimesAssign(
      data = data1,
      ref_maps = regimes,
      composite = TRUE)$data)), c('lon', 'lat', 'composite.cluster', 'member'))
  
  expect_equal(names(dim(
    CST_RegimesAssign(
      data = data1,
      ref_maps = regimes,
      composite = TRUE)$statistics$cluster)), c('time', 'member'))
  
  regimes <- 1:60
  dim(regimes) <- c(lat = 5, lon=2, cluster=6)
  
  expect_equal(max(CST_RegimesAssign(data = data1, ref_maps = regimes,
                                     composite = FALSE)$statistics$cluster), 
               unname(dim(regimes)['cluster']))
  
})
