context("Generic tests")
test_that("basic use case", {
  index_hind <- 1 : (5 * 3 * 7 * 4)
  dim(index_hind) <- c(time = 5, dataset = 3, member = 7, season = 4)
  index_hind <- list(data = index_hind)
  class(index_hind) <- 's2dv_cube'
  index_obs <- 1 : (5 * 3 * 4)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 4)
  index_obs <- list(data = index_obs)
  class(index_obs) <- 's2dv_cube'
  index_fcst <- 1 : (3 * 7 * 4)
  dim(index_fcst) <- c(time = 1, dataset = 3, member = 7, season = 4)
  index_fcst <- list(data = index_fcst)
  class(index_fcst) <- 's2dv_cube'
  result <- list(data = array(c(10:12, rep(45,3), rep(NA,6), 
                              31:33, rep(135,3), rep(NA,6), 
                              52:54, rep(225,3), rep(NA,6), 
                              73:75, rep(315,3), rep(NA,6)), 
                              dim = c(time = 1, dataset = 3, statistic = 4, 
                                      season = 4)),
                 statistic = c("mean_notbc", "sigma_notbc", "mean_bc", 
                               "sigma_bc"))
  attr(result, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFIndexFcst(index_hind, index_obs, index_fcst, 
                                type_exp = 'ens', method = 'LMEV', 
                                na.rm = FALSE), result, tolerance=1e-4)
  
  index_hind <- 1 : (5 * 3 * 1* 2 * 2)
  dim(index_hind) <- c(time = 5, dataset = 3, member = 1, statistic = 2, 
                       season = 2)
  index_hind <- list(data = index_hind)
  class(index_hind) <- 's2dv_cube'
  index_obs <- 1 : (5 * 3 * 2)
  dim(index_obs) <- c(time = 5, dataset = 3, member = 1, season = 2)
  index_obs <- list(data = index_obs)
  class(index_obs) <- 's2dv_cube'
  index_fcst <- 1 : (3 * 2 * 2)
  dim(index_fcst) <- c(time = 1, dataset = 3, member = 1, statistic = 2, 
                       season = 2)
  index_fcst <- list(data = index_fcst)
  class(index_fcst) <- 's2dv_cube'
  result <- list(data = array(c(1:3, rep(0,3), 1:3, rep(0,3), 
                                7:9, rep(15,3), -8:-6, rep(0,3)), 
                              dim = c(time = 1, dataset = 3, statistic = 4, 
                                      season = 2)),
                 statistic = c("mean_notbc", "sigma_notbc", "mean_bc", 
                               "sigma_bc"))
  attr(result, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFIndexFcst(index_hind, index_obs, index_fcst, 
                                type_exp = 'sta', na.rm = FALSE), result, tolerance=1e-4)
  
  
  index_hind2 <- index_hind
  index_hind2$data[1, 1, 1, 1, 1] <- NA
  result <- list(data = array(c(1:3, rep(0,3), 1:3, NA, rep(0,2), 
                                7:9, rep(15,3), -8:-6, rep(0,3)), 
                              dim = c(time = 1, dataset = 3, statistic = 4, 
                                      season = 2)),
                 statistic = c("mean_notbc", "sigma_notbc", "mean_bc", 
                               "sigma_bc"))
  attr(result, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFIndexFcst(index_hind = index_hind2, index_obs = index_obs, 
                                index_fcst = index_fcst, type_exp = 'sta', 
                                na.rm = FALSE), result, tolerance=1e-4)
})

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