context("Generic tests")
test_that("basic use case", {
  index_obs <- 1 : 5
  dim(index_obs) <- c(time = 5, dataset = 1, member = 1, season = 1)
  index_exp <- 1 : (5 * 7)
  dim(index_exp) <- c(time = 5, dataset = 1, member = 7, season = 1)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  result <- list(data = array(c(16:20,rep(15,5),rep(NA,10)), 
                              dim = c(time = 5, dataset = 1, statistic = 4, 
                                      season = 1)),
                 statistic = c("mean_notbc", "sigma_notbc", "mean_bc", 
                               "sigma_bc"))
  attr(result, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, 
                                type_exp = 'ens', method = 'LMEV'), result, tolerance=1e-4)
  
  index_obs <- 1 : 5
  dim(index_obs) <- c(time = 5, dataset = 1, member = 1, season = 1)
  index_exp <- 1 : (5 * 2)
  dim(index_exp) <- c(time = 5, dataset = 1, member = 1, statistic = 2, season = 1)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  result <- list(data = array(c(1:5,rep(0,5),c(1:5), rep(0,5)), 
                              dim = c(time = 5, dataset = 1, statistic = 4, 
                                      season = 1)),
                 statistic = c("mean_notbc", "sigma_notbc", "mean_bc", 
                               "sigma_bc"))
  attr(result, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, 
                                type_exp = 'sta'), result, tolerance=1e-4)
  
  index_obs <- 1 : (5 * 3 * 4)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 4)
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  result <- list(data = array(c(46:60,rep(45,15),1:15,rep(0,15),151:165,
                              rep(135,15),16:30,rep(0,15),256:270,rep(225,15),
                              31:45,rep(0,15),361:375,rep(315,15),46:60,
                              rep(0,15)), dim = c(time = 5, dataset = 3, 
                                                  statistic = 4, season = 4)),
                 statistic = c("mean_notbc", "sigma_notbc", "mean_bc", 
                               "sigma_bc"))
  attr(result, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs), 
               result, tolerance=1e-4)
  
  index_exp2 <- index_exp
  index_exp2$data[1, 1, 1, 1] <- NA
  CST_PDFIndexHind(index_exp = index_exp2, index_obs = index_obs, na.rm = FALSE)
  result <- list(data = array(c(NA,47:60,45,rep(NA,4),rep(45,10),rep(NA,5),6:15,
                                rep(NA,5),rep(0,10),151:165,
                                rep(135,15),16:30,rep(0,15),256:270,rep(225,15),
                                31:45,rep(0,15),361:375,rep(315,15),46:60,
                                rep(0,15)), dim = c(time = 5, dataset = 3, 
                                                    statistic = 4, season = 4)),
                 statistic = c("mean_notbc", "sigma_notbc", "mean_bc", 
                               "sigma_bc"))
  attr(result, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFIndexHind(index_exp = index_exp2, index_obs = index_obs, 
                                na.rm = FALSE), result, tolerance=1e-4)
  
  
})

test_that("Sanity checks", {
  expect_error(
    CST_PDFIndexHind(index_exp = 1),
    paste0("Parameter 'index_exp' and 'index_obs' must be of the class 's2dv_cube', ",
         "as output by CSTools::CST_Load."))
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  class(index_exp) <- 's2dv_cube'
  index_obs <- 1 : (5 * 3 * 1 * 4)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 4)
  index_obs <- list(data = index_obs)
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = 1),
    paste0("Parameter 'index_exp' and 'index_obs' must be of the class 's2dv_cube', ",
           "as output by CSTools::CST_Load."))
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, method = 'PEPE'),
    "The parameter 'method' must be equal to 'ME' or 'LMEV'")
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, method = 2),
    "The parameter 'method' must be equal to 'ME' or 'LMEV'")
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, method = NA),
    "The parameter 'method' must be equal to 'ME' or 'LMEV'")
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, na.rm = 'PEPE'),
    "The parameter 'na.rm' must be a logical value.")
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, type_exp = 'PEPE'),
    "The parameter 'type_exp' must be equal to 'ens' or 'sta'")
  
  index_obs <- 1 : (5 * 3 * 2 * 4)
  dim(index_obs) <- c(5, 3, 2, 4)
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Element 'data' from parameters 'index_exp' and 'index_obs'",
           " should have dimmension names."))
  
  index_obs <- 1 : (2 * 3 * 1 * 5)
  dim(index_obs) <- c(season = 2, dataset = 3, member = 1, time = 5)
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_obs'"))
  
  index_obs <- 1 : (5 * 3 * 1 * 2)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 2)
  index_exp <- 1 : (3 * 5 * 2 * 7)
  dim(index_exp) <- c(dataset = 3, time = 5, season = 2, member = 7)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_exp'"))
  
  index_obs <- 1 : (1 * 5 * 3 * 2)
  dim(index_obs) <- c(member = 1, time = 5, dataset = 3, season = 2)
  index_exp <- 1 : (3 * 5 * 2 * 7)
  dim(index_exp) <- c(dataset = 3, time = 5, season = 2, member = 7)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_obs'"))
  
  index_obs <- 1 : (5 * 3 * 1)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1)
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_obs'"))
  
  index_obs <- 1 : (5 * 3 * 1 * 1)
  dim(index_obs) <- c(reftime = 5, dataset = 3, member = 1, season = 1)
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_obs'"))
  
  index_obs <- 1 : (5 * 3 * 1 * 2)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 2)
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, seasonal = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_exp'"))
  
  index_obs <- 1 : (5 * 3 * 1 * 2)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 2)
  index_exp <- 1 : (5 * 3 * 7)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_exp'"))
  
  index_obs <- 1 : (5 * 3 * 1 * 2)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 2)
  index_exp <- 1 : (5 * 3 * 1 * 2 * 1)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 1, myStatistic = 2, season = 1)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("Wrong dimensions of the component 'data' of parameter 'index_exp'"))
  
  
  index_obs <- 1 : (3 * 3 * 1 * 4)
  dim(index_obs) <- c(time = 3, dataset = 3, member = 1, season = 4)
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("The length of the dimensions 'time' in the component 'data' ",
           "of the parameter 'index_obs' and 'index_exp' must be equals."))
  
  index_obs <- 1 : (3 * 3 * 1 * 4)
  dim(index_obs) <- c(time = 3, dataset = 3, member = 1, season = 4)
  index_exp <- 1 : (3 * 4 * 7 * 4)
  dim(index_exp) <- c(time = 3, dataset = 4, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("The length of the dimensions 'dataset' in the component 'data' ",
           "of the parameter 'index_obs' and 'index_exp' must be equals."))
  
  index_obs <- 1 : (3 * 3 * 1 * 2)
  dim(index_obs) <- c(time = 3, dataset = 3, member = 1, season = 2)
  index_exp <- 1 : (3 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 3, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("The length of the dimensions 'season' in the component 'data' ",
           "of the parameter 'index_obs' and 'index_exp' must be equals."))
  
  
  index_obs <- 1 : (1 * 3 * 2 * 4)
  dim(index_obs) <- c(time = 1, dataset = 3, member = 2, season = 4)
  index_exp <- 1 : (1 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 1, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("The length of the dimension 'time' in the component 'data' ",
           "of the parameter 'index_obs' and 'index_exp' must be greater than 2."))
  
  index_obs <- 1 : (5 * 3 * 2 * 4)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 2, season = 4)
  index_exp <- 1 : (5 * 3 * 7 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("The length of the dimension 'member' in the component 'data' ",
           "of the parameter 'index_obs' must be equal to 1."))
  
  index_obs <- 1 : (5 * 3 * 1 * 4)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 4)
  index_exp <- 1 : (5 * 3 * 1 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 1, season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs),
    paste0("The dimension 'statistic' must be exist in the component 'data' ",
           "of the parameter 'index_exp' if the length of the dimension ",
           "'member' is equal to 1"))
  
  index_obs <- 1 : (5 * 3 * 1 * 4)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 4)
  index_exp <- 1 : (5 * 3 * 7 * 2 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 7, statistic = 2, 
                      season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, type_exp = 'sta'),
    paste0("The length of the dimension 'member' in the component 'data' of the ",
           "parameter 'index_exp' must be equal to 1 if the component 'data' ",
           "of the parameter 'index_exp' has the dimension 'statistic'"))
  
  index_obs <- 1 : (5 * 3 * 1 * 4)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 4)
  index_exp <- 1 : (5 * 3 * 1 * 3 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 1, statistic = 3, 
                      season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, type_exp = 'sta'),
    paste0("The length of the dimension 'statistic' in the component 'data' of the ",
           "parameter 'index_exp' must be equal to 2 if the component 'data' ",
           "of the parameter 'index_exp' has the dimension 'member' equal to 1."))
  
  index_obs <- 1 : (5 * 3 * 1 * 4)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 4)
  index_exp <- 1 : (5 * 3 * 1 * 2 * 4)
  dim(index_exp) <- c(time = 5, dataset = 3, member = 1, statistic = 2, 
                      season = 4)
  index_exp <- list(data = index_exp)
  index_obs <- list(data = index_obs)
  class(index_exp) <- 's2dv_cube'
  class(index_obs) <- 's2dv_cube'
  expect_error(
    CST_PDFIndexHind(index_exp = index_exp, index_obs = index_obs, type_exp = 'ens'),
    paste0("The parameter 'type_exp' must be equal to 'sta' because the ",
         "dimension 'statistic' in the component 'data' of the ",
         "parameter 'index_exp' exits"))
})  
