context("Generic tests")
test_that("Sanity checks", {
  expect_error(
    CST_QuantileMapping(exp = 1),
    paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ",
           "as output by CSTools::CST_Load."))

  exp <- 1 : 20
  dim(exp) <- c(time = 20)
  exp <- list(data = exp)
  class(exp) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp),
    'argument "obs" is missing, with no default')
  expect_error(
    CST_QuantileMapping(exp = exp, obs = 1),
    paste0("Parameter 'exp' and 'obs' must be of the class 's2dv_cube', ",
           "as output by CSTools::CST_Load."))

  obs <- 1 : 20
  dim(obs) <- c(time = 20)
  obs <- list(data = obs)
  class(obs) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs, exp_cor = 1),
    paste0("Parameter 'exp_cor' must be of the class 's2dv_cube', as output ",
    "by CSTools::CST_Load."))
  
  exp <- 1 : 20
  dim(exp) <- 20
  exp <- list(data = exp)
  class(exp) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs),
    "Parameter 'exp' must have dimension names.")
  
  exp <- 1 : 20
  dim(exp) <- c(time = 20)
  exp <- list(data = exp)
  class(exp) <- 's2dv_cube'
  obs <- 1 : 20
  dim(obs) <- 20
  obs <- list(data = obs)
  class(obs) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs),
    "Parameter 'obs' must have dimension names.")

  obs <- 1 : 20
  dim(obs) <- c(time = 20)
  obs <- list(data = obs)
  class(obs) <- 's2dv_cube'
  exp_cor <- 1 : 20
  dim(exp_cor) <- 20
  exp_cor <- list(data = exp_cor)
  class(exp_cor) <- 's2dv_cube'
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs, exp_cor = exp_cor),
    "Parameter 'exp_cor' must have dimension names.")
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs),
    paste0("Parameter 'exp' requires 'sdate_dim' and 'member_dim' dimensions."))
  
  dim(exp$data) <- c(sdate = 5, member = 4)
  dim(obs$data) <- c(sdate = 5, time = 4)
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs, method = 'x'),
    paste0("Parameter 'method' must be one of the following methods: ",
           "'PTF','DIST','RQUANT','QUANT','SSPLIN'."))
  expect_error(
    CST_QuantileMapping(exp = exp, obs = obs, window_dim = 'ftime'),
    paste0("Dimension 'window_dim' not found in 'obs'."))
  res <- NULL
  res$data <- t(array(c(0, 2:20), c(sdate = 5, member = 4)))
  names(dim(res$data)) <- c('member', 'sdate')
  class(res) <- 's2dv_cube'
  expect_equal(CST_QuantileMapping(exp = exp, obs = obs, window_dim = 'time'),
               res)
    
  exp$data[1] <- NA
  obs$data[1] <- NA
  res$data[1,c(1,2)] <- c(NA, 0)
    expect_equal(
    CST_QuantileMapping(exp = exp, obs = obs, window_dim = 'time', na.rm = T),
    res)

  res$data <- array(c(NA, 6, 11, 16, rep(NA, 16)), c(member = 4, sdate = 5))
  expect_equal(
    CST_QuantileMapping(exp = exp, obs = obs, window_dim = 'time'),
    res)

  exp$data[1] <- 1
  obs$data[1] <- NA
  res$data[1] <- 0
    expect_equal(
    CST_QuantileMapping(exp = exp, obs = obs, window_dim = 'time'),
    res)
  obs$data[1] <- 1
  exp <- CSTools::lonlat_data$exp
  exp$data <- exp$data[,,1:4,,,]
  dim(exp$data) <- c(dataset = 1, member = 15, sdate = 4, ftime = 3, 
                       lat = 22, lon = 53)
  obs <- CSTools::lonlat_data$obs
  obs$data <- obs$data[,,1:4, ,,]
  dim(obs$data) <- c(dataset = 1, member = 1, sdate = 4, ftime = 3, 
                         lat = 22, lon = 53)
  exp_cor <- CSTools::lonlat_data$exp
  exp_cor$data <- exp_cor$data[,,5:6,,,]
  dim(exp_cor$data) <- c(dataset = 1, member = 15, sdate = 2, ftime = 3, 
                         lat = 22, lon = 53)
  expect_equal(length(CST_QuantileMapping(exp, obs, exp_cor)),
                           9) 

})
