context("s2dv::Persistence tests")

##############################################
  set.seed(1)
  dat1 <- array(rnorm(540), dim = c(member = 1, time = 90, lat = 2, lon = 3))
  dates1 <- seq(1920, 2009, 1)
 
##############################################
test_that("1. Input checks", {

  expect_error(
  Persistence(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
  Persistence(data = 'a'),
  "Parameter 'data' must be a numeric array."
  )
  expect_error(
  Persistence(data = array(1:10, dim = c(2, 5))),
  "Parameter 'data' must have dimension names."
  )
  expect_error(
  Persistence(data = dat1, dates = seq(1900, 2009, 1)),
  "Parameter 'dates' must have the same length as in 'time_dim'."
  )
  expect_error(
  Persistence(data = dat1, dates = dates1, time_dim = 12),
  "Parameter 'time_dim' must be a character string."
  )
  expect_error(
  Persistence(data = dat1, dates = dates1, time_dim = 'ftime'),
  "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
  Persistence(data = dat1, dates = dates1, start = 1961, end = 2005, ft_start = 0.5),
  "Parameter 'ft_start' must be a positive integer."
  )
  expect_error(
  Persistence(data = dat1, dates = dates1, start = 1961, end = 2005, ft_start = 1,
              ft_end = 12),
  "Parameter 'ft_end' must be a positive integer below 'max_ft'."
  )
  expect_error(
  Persistence(data = dat1, dates = dates1, start = 1961, end = 2005, ft_start = 1,
              ft_end = 12, max_ft = 13.5),
  "Parameter 'max_ft' must be a positive integer."
  )
  expect_error(
  Persistence(data = dat1, dates = dates1, start = 1961, end = 2005, ft_start = 1,
              nmemb = 0),
  "Parameter 'nmemb' must be a positive integer."
  )

  expect_error(
  Persistence(data = dat1, dates = dates1, start = 1961, end = 2005, ft_start = 1,
              na.action = T),
  paste0("Parameter 'na.action' must be a function for NA values or ",
         "a numeric indicating the number of NA values allowed ",
         "before returning NA.")
  )
  expect_error(
  Persistence(data = dat1, dates = dates1, start = 1961, end = 2005, ft_start = 1,
              ncores = 0),
  "Parameter 'ncores' must be a positive integer."
  )

})

##############################################
test_that("2. Output checks: dat1", {
  res <- Persistence(dat1, dates = dates1, start = 1961, end = 2005, ft_start = 1)

  expect_equal(
    names(res),
    c('persistence', 'persistence.mean', 'persistence.predint', 'AR.slope',
      'AR.intercept', 'AR.lowCI', 'AR.highCI')
  )
  expect_equal(
    dim(res$persistence),
    c(realization = 1, time = 45, member = 1, lat = 2, lon = 3)
  )
  expect_equal(
    dim(res$persistence.mean),
    c(45, member = 1, lat = 2, lon = 3)
  )
  expect_equal(
    mean(res$persistence),
    0.03481641,
    tolerance = 0.00001
  )
  expect_equal(
    range(res$persistence),
    c(-1.025059, 1.042929),
    tolerance = 0.0001
  )
  expect_equal(
    range(res$AR.slope),
    c(-0.2636489, 0.2334777),
    tolerance = 0.0001
  )
})

