context("Generic tests")
test_that("basic use case", {
  
  var_exp <- 1 : (2 * 4 * 3 * 2)
  dim(var_exp) <- c(time = 2, member = 4, lat = 3, lon = 2)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.1, 0.2, 0.4, 0.3, 0.1, 0.2, 0.4, 0.4, 0.1, 0.2, 0.4, 0.2)
  dim(aweights) <- c(time = 2, member = 4, dataset = 2)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  result <- array(c(4.4, 5.4, 12.4, 13.4, 20.4, 21.4,
                    28.4, 29.4, 36.4, 37.4, 44.4, 45.4,
                    4.6, 4.8, 12.6, 12.8, 20.6, 20.8,
                    28.6, 28.8, 36.6, 36.8, 44.6, 44.8), 
                  dim = c(time = 2, lat = 3, lon = 2, dataset =2))
  expect_equal(CST_BEI_Weighting(var_exp, aweights, type = 'ensembleMean', 
                                 time_dim_name = 'time')$data, result, tolerance=1e-4)
  
  var_exp <- 1 : (2 * 3 * 1 * 2)
  dim(var_exp) <- c(time = 2, member = 3, lat = 1, lon = 2)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(time = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  result <- array(c(0.5, 0.1, 0.0, 0.4, 0.5, 0.5, 0.5, 0.1, 0.0, 0.4, 0.5, 0.5), 
                  dim = c(time = 2, tercil = 3, lat = 1, lon = 2))
  expect_equal(CST_BEI_Weighting(var_exp, aweights, type = 'probs', 
                                 time_dim_name = 'time')$data, result, tolerance=1e-4)
  
  var_exp <- 1 : (2 * 3 * 1 * 2)
  dim(var_exp) <- c(sdate = 2, member = 3, lat = 1, lon = 2)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(sdate = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  result <- array(c(0.5, 0.1, 0.0, 0.4, 0.5, 0.5,
                    0.5, 0.1, 0.0, 0.4, 0.5, 0.5), 
                  dim = c(sdate = 2, tercil = 3, lat = 1, lon = 2))
  
  expect_equal(CST_BEI_Weighting(var_exp, aweights, type = 'probs', 
                                 time_dim_name = 'sdate')$data, result, tolerance=1e-4)
  
  var_exp <- 1 : (2 * 3)
  dim(var_exp) <- c(sdate = 2, member = 3)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(sdate = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  result <- array(c(0.5, 0.1, 0.0, 0.4, 0.5, 0.5), 
                  dim = c(sdate = 2, tercil = 3))
  
  expect_equal(CST_BEI_Weighting(var_exp, aweights, type = 'probs', 
                                 time_dim_name = 'sdate')$data, result, tolerance=1e-4)
})

test_that("Sanity checks", {
  expect_error(
    CST_BEI_Weighting(var_exp, aweights, type = 'probs', time_dim_name = 1),
    paste0("Parameter 'time_dim_name' must be a character string indicating",
           " the name of the temporal dimension."))
  
  expect_error(
    CST_BEI_Weighting(var_exp, aweights, type = 2),
    paste0("Parameter 'type' must be a character string, 'probs' or ",
           "'ensembleMean', indicating the type of output."))
  
  expect_error(
    CST_BEI_Weighting(var_exp = 1, aweights),
    paste0("Parameter 'var_exp' must be of the class 's2dv_cube', ",
           "as output by CSTools::CST_Load."))
  
  var_exp <- 1 : (2 * 3)
  dim(var_exp) <- c(sdate = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube' 
  expect_error(
    CST_BEI_Weighting(var_exp, aweights = 2),
    "Parameter 'aweights' must be an array.")
  
  var_exp <- 1 : (2 * 3)
  dim(var_exp) <- c(2, 3)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(2, 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  expect_error(
    CST_BEI_Weighting(var_exp, aweights),
    paste0("Element 'data' from parameter 'var_exp' and parameter 'aweights'",
           " should have dimmension names."))
  
  var_exp <- 1 : (2 * 3)
  dim(var_exp) <- c(sdate = 2, member = 3)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(sdate = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  expect_error(
    CST_BEI_Weighting(var_exp, aweights),
    paste0("Element 'data' from parameter 'var_exp' must have ",
           "temporal dimension."))
  
  var_exp <- 1 : (2 * 3)
  dim(var_exp) <- c(time = 2, member = 3)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(sdate = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  expect_error(
    CST_BEI_Weighting(var_exp, aweights),
    "Parameter 'aweights' must have temporal dimension.")
  
  var_exp <- 1 : (2 * 3)
  dim(var_exp) <- c(time = 2, season = 3)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(time = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  expect_error(
    CST_BEI_Weighting(var_exp, aweights),
    paste0("Element 'data' from parameter 'var_exp' must have ",
           "dimension 'member'."))
  
  var_exp <- 1 : (2 * 3)
  dim(var_exp) <- c(time = 2, member = 3)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(time = 2, season = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  expect_error(
    CST_BEI_Weighting(var_exp, aweights),
    paste0("Parameter 'aweights' must have ",
           "dimension 'member'."))
  
  var_exp <- 1 : (3 * 3)
  dim(var_exp) <- c(time = 3, member = 3)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(time = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  expect_error(
    CST_BEI_Weighting(var_exp, aweights),
    paste0("Length of temporal dimensions ",
           "of element 'data' from parameter 'var_exp' and parameter ",
           "'aweights' must be equals."))
  
  var_exp <- 1 : (3 * 4)
  dim(var_exp) <- c(time = 3, member = 4)
  aweights <- c(0.2, 0.1, 0.3, 0.4, 0.5, 0.5)
  dim(aweights) <- c(time = 2, member = 3)
  var_exp <- list(data = var_exp)
  class(var_exp) <- 's2dv_cube'
  
  expect_error(
    CST_BEI_Weighting(var_exp, aweights),
    paste0("Length of temporal dimensions of element 'data' from ",
           "parameter 'var_exp' and parameter 'aweights' must be equals."))
  
})  
