context("s2dv::Ano_CrossValid tests")

##############################################
  # dat1
set.seed(1)
exp1 <- array(rnorm(60), dim = c(dataset = 2, member = 3, sdate = 5, ftime = 2))
set.seed(2)
obs1 <- array(rnorm(20), dim = c(dataset = 1, member = 2, sdate = 5, ftime = 2))

# dat2
set.seed(1)
exp2 <- array(rnorm(30), dim = c(member = 3, ftime = 2, sdate = 5))
set.seed(2)
obs2 <- array(rnorm(20), dim = c(ftime = 2, member = 2, sdate = 5))


##############################################
test_that("1. Input checks", {

  # exp and obs (1)
  expect_error(
  Ano_CrossValid(c(), c()),
  "Parameter 'exp' and 'obs' cannot be NULL."
  )
  expect_error(
  Ano_CrossValid(c('b'), c('a')),
  "Parameter 'exp' and 'obs' must be a numeric array."
  )
  expect_error(
  Ano_CrossValid(c(1:10), c(2:4)),
  paste0("Parameter 'exp' and 'obs' must have at least dimensions ",
         "time_dim and dat_dim.")
  )
  expect_error(
  Ano_CrossValid(array(1:10, dim = c(2, 5)), array(1:4, dim = c(2, 2))),
  "Parameter 'exp' and 'obs' must have dimension names."
  )
  # time_dim
  expect_error(
    Ano_CrossValid(exp1, obs1, time_dim = 2),
    "Parameter 'time_dim' must be a character string."
  )
  expect_error(
    Ano_CrossValid(exp1, obs1, time_dim = c('a', 'sdate')),
    "Parameter 'time_dim' must be a character string."
  )
  # dat_dim
  expect_error(
    Ano_CrossValid(exp1, obs1, dat_dim = 1),
    "Parameter 'dat_dim' must be a character vector."
  )
  expect_error(
    Ano_CrossValid(exp1, obs1, dat_dim = 'dat'),
    "Parameter 'dat_dim' is not found in 'exp' or 'obs' dimension."
  )
  # memb
  expect_error(
    Ano_CrossValid(exp1, obs1, memb = 'member'),
    "Parameter 'memb' must be one logical value."
  )
  # memb_dim
  expect_error(
    Ano_CrossValid(exp1, obs1, memb = FALSE, memb_dim = 2),
    "Parameter 'memb_dim' must be a character string."
  )
  expect_error(
    Ano_CrossValid(exp1, obs1, memb = FALSE, memb_dim = 'memb'),
    "Parameter 'memb_dim' is not found in 'exp' or 'obs' dimension."
  )
  expect_error(
    Ano_CrossValid(exp1, obs1, memb = FALSE, memb_dim = 'ftime'),
    "Parameter 'memb_dim' must be one element in parameter 'dat_dim'."
  )
  # ncores
  expect_error(
    Ano_CrossValid(exp1, obs1, memb = FALSE, ncores = -1),
    "Parameter 'ncores' must be a positive integer."
  )
  # exp and obs (2)
  expect_error(
    Ano_CrossValid(exp1, array(1:20, dim = c(dataset = 1, member = 2, sdate = 4, ftime = 2))),
    "Parameter 'exp' and 'obs' must have the same length of all dimensions except 'dat_dim'."
  )


})
##############################################
test_that("2. dat1", {

  expect_equal(
  names(Ano_CrossValid(exp1, obs1)),
  c("exp", "obs")
  )
  expect_equal(
  dim(Ano_CrossValid(exp1, obs1)$exp),
  c(sdate = 5, dataset = 2, member = 3, ftime = 2)
  )
  expect_equal(
  Ano_CrossValid(exp1, obs1)$exp[, 1, 2, 2],
  c(0.2771331, 1.1675753, -1.0684010, 0.2901759, -0.6664833),
  tolerance = 0.0001
  )
  expect_equal(
  Ano_CrossValid(exp1, obs1)$obs[, 1, 2, 2],
  c(1.7024193, -0.8243579, -2.4136080, 0.5199868, 1.0155598),
  tolerance = 0.0001
  )
  expect_equal(
  Ano_CrossValid(exp1, obs1, memb = FALSE)$exp[, 1, 2, 2],
  c(0.1229714, 0.8496518, -0.9531644, 0.1548713, -0.5264025),
  tolerance = 0.0001
  )

})

##############################################
test_that("3. dat2", {
  expect_equal(
  names(Ano_CrossValid(exp2, obs2, dat_dim = 'member')),
  c("exp", "obs")
  )
  expect_equal(
  dim(Ano_CrossValid(exp2, obs2, dat_dim = 'member')$exp),
  c(sdate = 5, member = 3, ftime = 2)
  )
  expect_equal(
  Ano_CrossValid(exp2, obs2, dat_dim = 'member')$exp[, 2, 2],
  c(0.05650631, 1.53434806, -0.37561623, -0.26217217, -0.95306597),
  tolerance = 0.0001
  )
  expect_equal(
  Ano_CrossValid(exp2, obs2, dat_dim = 'member', memb = FALSE)$exp[, 2, 2],
  c(0.34489635, 1.56816273, -0.01926901, -0.09646066, -0.68236823),
  tolerance = 0.0001
  )

})





