context("Generic tests")
test_that("basic use case", {
  mod <- 1 : (2 * 3 * 4 * 5 * 6 * 8)
  dim(mod) <- c(dataset = 2, member = 3, sdate = 4, ftime = 5, lat = 6, lon = 8)
  obs <- 1 : (1 * 1 * 4 * 5 * 6 * 8)
  dim(obs) <- c(dataset = 1, member = 1, sdate = 4, ftime = 5, lat = 6, lon = 8)
  lon <- seq(0, 30, 5)
  lat <- seq(0, 30, 5)
  dat <- list(mod = mod, obs = obs, lat = lat, lon = lon)
  
  result <- list(ano_exp = array(rep(
                c(rep(-12, 6), rep(-4, 6), rep(4, 6), rep(12, 6)), 360),
                dim = c(dataset = 2, member = 3, sdate = 4, ftime = 5, 
                lat = 6, lon = 8)),
                ano_obs = array(rep(c(-2, -2/3,  2/3,  2), 360),
                dim = c(dataset = 1, member = 1, sdate = 4, ftime = 5,
                lat = 6, lon = 8)),
                lat = lat,
                lon = lon,
                metric = array(rep(c(rep(1, 9), rep(0.9, 3)), 48), 
                dim = c(dataset = 3, dataset = 1, statistics = 4, 
                       lat = 6, lon = 8))) 
  attr(result[[1]], "experiments") <- c("mod1", "mod2", "MMM")
  expect_equal(CST_MultiMetric(dat), result)
  
  res <- CST_MultiMetric(dat, metric = 'rms')
  expect_equal(length(res), 5)
  expect_equal(dim(res$ano_exp), 
        c(dataset = 2, member = 3, sdate = 4, ftime = 5, lat = 6, lon = 8))
  expect_equal(dim(res$ano_obs), 
        c(dataset = 1, member = 1, sdate = 4, ftime = 5, lat = 6, lon = 8))
  expect_equal(dim(res$metric), 
        c(dataset = 3, dataset = 1, statistics = 3, lat = 6, lon = 8))
  res <- CST_MultiMetric(dat, metric = 'rms', multimodel = FALSE)
  expect_equal(dim(res$metric), 
        c(dataset = 2, dataset = 1, statistics = 3, lat = 6, lon = 8))
  res <- CST_MultiMetric(dat, metric = 'rmsss')
  expect_equal(dim(res$metric), 
        c(dataset = 3, dataset = 1, statistics = 2, lat = 6, lon = 8))
  res <- CST_MultiMetric(dat, metric = 'rmsss', multimodel = FALSE)
  expect_equal(dim(res$metric), 
        c(dataset = 2, dataset = 1, statistics = 2, lat = 6, lon =8))
  res <- CST_MultiMetric(dat, names = c('A', 'B'))
  expect_equal(attributes(res$ano_exp)$experiments, c('A', 'B', 'MMM'))
})


test_that("Sanity checks", {
  expect_error(
    CST_MultiMetric(data = 1),
      "Parameter 'data' must be a list as output of Load function
from s2dverification package")
  mod <- 1 : (2 * 3 * 4 * 5 * 6 * 8)
  dim(mod) <- c(dataset = 2, member = 3, sdate = 4, ftime = 5, lat = 6, lon = 8)
  obs <- 1 : (1 * 1 * 4 * 5 * 6 * 8)
  dim(obs) <- c(dataset = 1, member = 1, sdate = 4, ftime = 5, lat = 6, lon = 8)
  lon <- seq(0, 30, 5)
  lat <- seq(0, 30, 5)
  dat <- list(mod = mod, obs = obs, lat = lat, lon = lon)
  
  expect_error(
    CST_MultiMetric(data = dat, metric = 1),
    "Parameter 'metric' must be a character string indicating one of the 
         options: 'correlation', 'rms' or 'rmse'")
  expect_error(
    CST_MultiMetric(data = dat, metric = NA),
    "missing value where TRUE/FALSE needed")
  expect_error(
    CST_MultiMetric(data = dat, metric = NULL),
    "argument is of length zero")
  expect_error(
    CST_MultiMetric(data = dat, metric = "correlation", multimodel = NULL),
    "Parameter 'multimodel' must be a logical value.")
  expect_error(
    CST_MultiMetric(data = dat, metric = "correlation", multimodel = FALSE, names = 1),
    "Parameter 'names' must be the same length as the number of 
           models in the element 'mod' of parameter 'data'.")
})  
