context("Generic tests")
test_that("basic use case", {
  index_exp <- 1 : (2 * 1 * 3 * 1)
  dim(index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 2 * 1)
  dim(pdf_exp) <- c(time = 2, statistic = 2, season = 1)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  result <- array(c(0.4521103, 0.4017633, 0.3620216, 0.3545549,
                                0.1858681, 0.2436818),
                              dim = c(time = 2, dataset = 1, member = 3, 
                                      season = 1))
  
  expect_equal(CST_WeightIndex(index_exp, pdf_exp)$data, result, tolerance=1e-4)
  
})

test_that("Sanity checks", {
  expect_error(
    CST_WeightIndex(index_exp = 1),
    paste0("Parameter 'index_exp' and 'pdf_exp' must be of the class 's2dv_cube', ",
           "as output by CSTools::CST_Load."))
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = 1),
    paste0("Parameter 'index_exp' and 'pdf_exp' must be of the class 's2dv_cube', ",
           "as output by CSTools::CST_Load."))
  
  index_exp <- 1 : (2 * 1 * 3 * 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 2 * 1)
  dim(pdf_exp) <- c(time = 2, statistic = 2, season = 1)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("Element 'data' from parameters 'index_exp' and 'pdf_exp'",
           " should have dimmension names."))
  
  index_exp <- 1 : (2 * 1 * 3 * 1)
  dim(index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 2 * 1)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("Element 'data' from parameters 'index_exp' and 'pdf_exp'",
           " should have dimmension names."))
  
  index_exp <- 1 : (2 * 1 * 3 * 1 * 2)
  dim(index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1, 
                      statistic = 2)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 2 * 1)
  dim(pdf_exp) <- c(time = 2, statistic = 2, season = 1)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("The number of dimensions in the component 'data' ",
           "of the parameter 'index_exp' must be 4"))
  
  index_exp <- 1 : (3 * 2 * 1 * 1)
  dim(index_exp) <- c(member = 3, time = 2, dataset = 1, season = 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 2 * 1)
  dim(pdf_exp) <- c(time = 2, statistic = 2, season = 1)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_exp'"))
  
  index_exp <- 1 : (2 * 1 * 3 * 1)
  dim(index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 2)
  dim(pdf_exp) <- c(time = 2, statistic = 2)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("The number of dimensions in the component 'data' ",
           "of the parameter 'pdf_exp' must be 3"))
  
  index_exp <- 1 : (2 * 1 * 3 * 1)
  dim(index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 1 * 2)
  dim(pdf_exp) <- c(statistic = 2, season = 1, time = 2)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("Wrong dimensions of the component 'data' of parameter 'pdf_exp'"))
  
  index_exp <- 1 : (2 * 1 * 3 * 1)
  dim(index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 4 * 1)
  dim(pdf_exp) <- c(time = 2, statistic = 4, season = 1)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("The length of the dimension 'statistic' in the component 'data' ",
           "of the parameter 'pdf_exp' must be equal to 2."))
  
  index_exp <- 1 : (2 * 1 * 3 * 1)
  dim(index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (5 * 2 * 1)
  dim(pdf_exp) <- c(time = 5, statistic = 2, season = 1)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("The length of the dimensions 'time' in the component 'data' ",
           "of the parameter 'index_exp' and 'pdf_exp' must be equals."))
  
  index_exp <- 1 : (2 * 1 * 3 * 1)
  dim(index_exp) <- c(time = 2, dataset = 1, member = 3, season = 1)
  index_exp <- list(data = index_exp)
  pdf_exp <- 1 : (2 * 2 * 3)
  dim(pdf_exp) <- c(time = 2, statistic = 2, season = 3)
  pdf_exp <- list(data = pdf_exp)
  class(index_exp) <- 's2dv_cube'
  class(pdf_exp) <- 's2dv_cube'
  expect_error(
    CST_WeightIndex(index_exp = index_exp, pdf_exp = pdf_exp),
    paste0("The length of the dimensions 'season' in the component 'data' ",
           "of the parameter 'index_exp' and 'pdf_exp' must be equals."))
})  
