context("Generic tests")
test_that("basic use case", {
  var_exp <- 1 : (2 * 1 * 3 * 3 * 2)
  dim(var_exp) <- c(time = 2, dataset = 1, member = 3, lat = 3, lon = 2)
  set.seed(23)
  weights_index_exp <- runif(2 * 1 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  var_exp <- list(data = var_exp)
  weights_index_exp <- list(data = weights_index_exp)
  class(var_exp) <- 's2dv_cube'
  class(weights_index_exp) <- 's2dv_cube'
  result <- array(c(5.669304, 5.831401, 16.03673, 13.97668, 
                                26.40415, 22.12196, 36.77157, 30.26724, 
                                47.13899, 38.41252, 57.50641, 46.55779), 
                              dim = c(time = 2, dataset = 1, season = 1, 
                                      lat = 3, lon = 2))
  
  expect_equal(CST_EMBestIndexWeighting(var_exp, weights_index_exp)$data, result, tolerance=1e-4)
  
})

test_that("Sanity checks", {
  expect_error(
    CST_EMBestIndexWeighting(var_exp = 1),
    paste0("Parameter 'var_exp' and 'weights_index_exp' must be of the class ",
           "'s2dv_cube', as output by CSTools::CST_Load."))
  expect_error(
    CST_EMBestIndexWeighting(var_exp = var_exp, weights_index_exp = 1),
    paste0("Parameter 'var_exp' and 'weights_index_exp' must be of the class ",
            "'s2dv_cube', as output by CSTools::CST_Load."))
  
  var_exp <- 1 : (2 * 1)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  set.seed(23)
  weights_index_exp <- runif(2 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  weights_index_exp <- list(data = weights_index_exp)
  class(weights_index_exp) <- 's2dv_cube'
  expect_error(
    CST_EMBestIndexWeighting(var_exp, weights_index_exp),
    paste0("Element 'data' from parameters 'var_exp' and 'weights_index_exp'",
           " should have dimmension names."))
  
  var_exp <- 1 : (2 * 1 * 3 * 3)
  dim(var_exp) <- c(time = 2, dataset = 1, member = 3, lat = 3)
  set.seed(23)
  weights_index_exp <- runif(2 * 1 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  var_exp <- list(data = var_exp)
  weights_index_exp <- list(data = weights_index_exp)
  class(var_exp) <- 's2dv_cube'
  class(weights_index_exp) <- 's2dv_cube'
  expect_error(
    CST_EMBestIndexWeighting(var_exp, weights_index_exp),
    paste0("Wrong dimensions of the component 'data' of parameter 'var_exp'"))
  
  var_exp <- 1 : (1 * 2 * 3)
  dim(var_exp) <- c(dataset = 1, time = 2, member = 3)
  set.seed(23)
  weights_index_exp <- runif(2 * 1 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  var_exp <- list(data = var_exp)
  weights_index_exp <- list(data = weights_index_exp)
  class(var_exp) <- 's2dv_cube'
  class(weights_index_exp) <- 's2dv_cube'
  expect_error(
    CST_EMBestIndexWeighting(var_exp, weights_index_exp),
    paste0("Wrong dimensions of the component 'data' of parameter 'var_exp'"))
  
  var_exp <- 1 : (2 * 1 * 3)
  dim(var_exp) <- c(time = 2, dataset = 1, member = 3)
  set.seed(23)
  weights_index_exp <- runif(2 * 1 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(time = 2, dataset = 1, member = 3)
  var_exp <- list(data = var_exp)
  weights_index_exp <- list(data = weights_index_exp)
  class(var_exp) <- 's2dv_cube'
  class(weights_index_exp) <- 's2dv_cube'
  expect_error(
    CST_EMBestIndexWeighting(var_exp, weights_index_exp),
    paste0("Wrong dimensions of the component 'data' of parameter 'weights_index_exp'"))
  
  var_exp <- 1 : (2 * 1 * 3)
  dim(var_exp) <- c(time = 2, dataset = 1, member = 3)
  set.seed(23)
  weights_index_exp <- runif(2 * 1 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(member = 3, season = 1, time = 2, dataset = 1)
  var_exp <- list(data = var_exp)
  weights_index_exp <- list(data = weights_index_exp)
  class(var_exp) <- 's2dv_cube'
  class(weights_index_exp) <- 's2dv_cube'
  expect_error(
    CST_EMBestIndexWeighting(var_exp, weights_index_exp),
    paste0("Wrong dimensions of the component 'data' of parameter 'weights_index_exp'"))
  
  
  var_exp <- 1 : (3 * 1 * 3)
  dim(var_exp) <- c(time = 3, dataset = 1, member = 3)
  set.seed(23)
  weights_index_exp <- runif(2 * 1 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  var_exp <- list(data = var_exp)
  weights_index_exp <- list(data = weights_index_exp)
  class(var_exp) <- 's2dv_cube'
  class(weights_index_exp) <- 's2dv_cube'
  expect_error(
    CST_EMBestIndexWeighting(var_exp, weights_index_exp),
    paste0("The length of the dimension 'time' of the parameter 'var_exp' ",
           "in the component 'data' must be equal to the length of the dimension",
           " 'time' of the parameter 'weights_index_exp' in the component 'data'"))
  
  var_exp <- 1 : (2 * 2 * 3)
  dim(var_exp) <- c(time = 2, dataset = 2, member = 3)
  set.seed(23)
  weights_index_exp <- runif(2 * 1 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  var_exp <- list(data = var_exp)
  weights_index_exp <- list(data = weights_index_exp)
  class(var_exp) <- 's2dv_cube'
  class(weights_index_exp) <- 's2dv_cube'
  expect_error(
    CST_EMBestIndexWeighting(var_exp, weights_index_exp),
    paste0("The length of the dimension 'dataset' of the parameter 'var_exp' ",
           "in the component 'data' must be equal to the length of the dimension",
           " 'dataset' of the parameter 'weights_index_exp' in the component 'data'"))
  
  
  var_exp <- 1 : (2 * 2 * 4)
  dim(var_exp) <- c(time = 2, dataset = 2, member = 4)
  set.seed(23)
  weights_index_exp <- runif(2 * 2 * 3, 0.0001, 0.9999)
  dim(weights_index_exp) <- c(time = 2, dataset = 2, member = 3, season = 1)
  var_exp <- list(data = var_exp)
  weights_index_exp <- list(data = weights_index_exp)
  class(var_exp) <- 's2dv_cube'
  class(weights_index_exp) <- 's2dv_cube'
  expect_error(
    CST_EMBestIndexWeighting(var_exp, weights_index_exp),
    paste0("The length of the dimension 'member' of the parameter 'var_exp' ",
           "in the component 'data' must be equal to the length of the dimension",
           " 'member' of the parameter 'weights_index_exp' in the component 'data'"))
  
})  
