context("Generic tests")
test_that("basic use case", {
  pdf_exp <- c(1: (3 * 2 * 1 * 4 * 1))
  dim(pdf_exp) <-  c(time = 3, dataset = 2, member = 1, statistic = 4, season = 1)
  pdf_exp <- list(data = pdf_exp)
  class(pdf_exp) <- 's2dv_cube'
  result <- list(data = array(c(14.28166, 15.29171, 16.30088, 
                                14.37964, 15.09211, 15.80411), 
                              dim = c(time = 3, statistic = 2, 
                                      season = 1)),
                 statistic = c("mean", "sigma"))
  attr(result, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFBest(pdf_exp, bc_dataset1 = TRUE, bc_dataset2 = TRUE), result, tolerance=1e-4)
  
  
  result2 <- list(data = array(c(5.952278, 7.090211, 8.215385, 
                                 8.849182, 9.638375, 10.418918), 
                              dim = c(time = 3, statistic = 2, 
                                      season = 1)),
                 statistic = c("mean", "sigma"))
  attr(result2, 'class') <- 's2dv_cube'
  expect_equal(CST_PDFBest(pdf_exp, bc_dataset1 = TRUE, bc_dataset2 = FALSE), result2, tolerance=1e-4)
  
   
})

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

})  
