context("s2dv::RMS tests")

##############################################
  # dat1
  set.seed(1)
  exp1 <- array(rnorm(120), dim = c(dataset = 3, sdate = 5, ftime = 2, lon = 1, lat = 4))

  set.seed(2)
  obs1 <- array(rnorm(80),  dim = c(dataset = 2, sdate = 5, ftime = 2, lon = 1, lat = 4))
  set.seed(2)
  na <- floor(runif(10, min = 1, max = 80))
  obs1[na] <- NA

  # dat 2: vector
  set.seed(5)
  exp2 <- rnorm(10)
  set.seed(6)
  obs2 <- rnorm(10)

  # dat3
  set.seed(1)
  exp3 <- array(rnorm(120), dim = c(sdate = 5, ftime = 2, lon = 1, lat = 4))

  set.seed(2)
  obs3 <- array(rnorm(80),  dim = c(sdate = 5, ftime = 2, lon = 1, lat = 4))


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

  expect_error(
  RMS(c(), c()),
  "Parameter 'exp' and 'obs' cannot be NULL."
  )
  expect_error(
  RMS(c('b'), c('a')),
  "Parameter 'exp' and 'obs' must be a numeric array."
  )
  expect_error(
  RMS(c(1:10), c(2:4)),
  "Parameter 'exp' and 'obs' must be array with as least two dimensions time_dim and dat_dim, or vector of same length."
  )
  expect_error(
  RMS(array(1:10, dim = c(2, 5)), array(1:4, dim = c(2, 2))),
  "Parameter 'exp' and 'obs' must have dimension names."
  )
  expect_error(
  RMS(array(1:10, dim = c(a = 2, c = 5)), array(1:4, dim = c(a = 2, b = 2))),
  "Parameter 'exp' and 'obs' must have same dimension name"
  )
  expect_error(
  RMS(exp1, obs1, dat_dim = 1),
  "Parameter 'dat_dim' must be a character string."
  )
  expect_error(
  RMS(exp1, obs1, dat_dim = 'a'),
  "Parameter 'dat_dim' is not found in 'exp' or 'obs' dimension."
  )
  expect_error(
  RMS(exp1, obs1, time_dim = c('sdate', 'a')),
  "Parameter 'time_dim' must be a character string."
  )
  expect_error(
  RMS(exp1, obs1, time_dim = 'a'),
  "Parameter 'time_dim' is not found in 'exp' or 'obs' dimension."
  )
  expect_error(
  RMS(exp1, obs1, comp_dim = c('sdate', 'ftime')),
  "Parameter 'comp_dim' must be a character string."
  )
  expect_error(
  RMS(exp1, obs1, comp_dim = 'a'),
  "Parameter 'comp_dim' is not found in 'exp' or 'obs' dimension."
  )
  expect_error(
  RMS(exp1, obs1, limits = c(1,3)),
  "Paramter 'comp_dim' cannot be NULL if 'limits' is assigned."
  )
  expect_error(
  RMS(exp1, obs1, comp_dim = 'ftime', limits = c(1)),
  paste0("Parameter 'limits' must be a vector of two positive ",
                  "integers smaller than the length of paramter 'comp_dim'.")
  )
  expect_error(
  RMS(exp1, obs1, conf.lev = -1),
  "Parameter 'conf.lev' must be a numeric number between 0 and 1."
  )
  expect_error(
  RMS(exp1, obs1, conf = 1),
  "Parameter 'conf' must be one logical value."
  )
  expect_error(
  RMS(exp1, obs1, ncores = 1.5),
  "Parameter 'ncores' must be a positive integer."
  )
  expect_error(
  RMS(exp = array(1:10, dim = c(sdate = 1, dataset = 5, a = 1)),
       obs = array(1:4, dim = c(a = 1, sdate = 2, dataset = 2))),
  "Parameter 'exp' and 'obs' must have same length of all dimension except 'dat_dim'."
  )
  expect_error(
  RMS(exp = array(1:5, dim = c(sdate = 1, dataset = 5, a = 1)),
       obs = array(1:2, dim = c(a = 1, sdate = 1, dataset = 2))),
  "The length of time_dim must be at least 2 to compute RMS."
  )



})

##############################################
test_that("2. Output checks: dat1", {
suppressWarnings(
  expect_equal(
    dim(RMS(exp1, obs1)$rms),
    c(nexp = 3, nobs = 2, ftime = 2, lon = 1, lat = 4)
  )
)
suppressWarnings(
  expect_equal(
    RMS(exp1, obs1)$rms[1:6],
    c(1.2815677, 2.0832803, 1.1894637, 1.3000403, 1.4053807, 0.8157563), 
    tolerance = 0.001
  )
)
suppressWarnings(
  expect_equal(
    length(which(is.na(RMS(exp1, obs1)$conf.lower))),
    4
  )
)
suppressWarnings(
  expect_equal(
    max(RMS(exp1, obs1)$conf.lower, na.rm = T),
    1.399509,
    tolerance = 0.001
  )
)
suppressWarnings(
  expect_equal(
    length(which(is.na(RMS(exp1, obs1, comp_dim = 'ftime')$rms))),
    0
  )
)
suppressWarnings(
  expect_equal(
    length(which(is.na(RMS(exp1, obs1, comp_dim = 'ftime')$conf.upper))),
    8
  )
)
suppressWarnings(
  expect_equal(
    length(which(is.na(RMS(exp1, obs1, comp_dim = 'lat')$conf.lower))),
    36
  )
)
suppressWarnings(
  expect_equal(
    length(which(is.na(RMS(exp1, obs1, comp_dim = 'lat', limits = c(1, 2))$conf.lower))),
    21
  )
)
suppressWarnings(
  expect_equal(
    min(RMS(exp1, obs1, conf.lev = 0.99)$conf.upper, na.rm = TRUE),
    1.406368,
    tolerance = 0.0001
  )
)
suppressWarnings(
  expect_equal(
    length(RMS(exp1, obs1, conf = FALSE)),
    1
  )
)


})

##############################################
test_that("3. Output checks: dat2", {

  expect_equal(
    dim(RMS(exp2, obs2)$rms),
    c(nexp = 1, nobs = 1)
  )

  expect_equal(
  as.vector(RMS(exp2, obs2)$rms),
  1.429815,
  tolerance = 0.00001
  )
})

##############################################
test_that("4. Output checks: dat3", {

  expect_equal(
    dim(RMS(exp3, obs3, dat_dim = NULL)$rms),
    c(ftime = 2, lon = 1, lat = 4)
  )

  expect_equal(
  as.vector(RMS(exp3, obs3, dat_dim = NULL)$rms),
  c(1.6458118, 0.8860392, 0.8261295, 1.1681939, 2.1693538, 1.3064454, 0.5384229, 1.1215333),
  tolerance = 0.00001
  )
})

##############################################