context("s2dv::Consist_Trend tests")

##############################################
  # dat1
  set.seed(1)
  exp1 <- array(rnorm(30), dim = c(dataset = 2, sdate = 5, ftime = 3))
  set.seed(2)
  obs1 <- array(rnorm(15), dim = c(dataset = 1, sdate = 5, ftime = 3))
  # dat2
  exp2 <- exp1
  set.seed(1)
  exp2[1, 1, 1] <- NA
  obs2 <- obs1
  obs2[1, 2, 3] <- NA


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

  # exp and obs (1)
  expect_error(
  Consist_Trend(c(), c()),
  "Parameter 'exp' and 'obs' cannot be NULL."
  )
  expect_error(
  Consist_Trend(c('b'), c('a')),
  "Parameter 'exp' and 'obs' must be a numeric array."
  )
  expect_error(
  Consist_Trend(c(1:10), c(2:4)),
  paste0("Parameter 'exp' and 'obs' must be at least two dimensions ",
         "containing time_dim and dat_dim.")
  )
  expect_error(
  Consist_Trend(array(1:10, dim = c(2, 5)), array(1:4, dim = c(2, 2))),
  "Parameter 'exp' and 'obs' must have dimension names."
  )
  expect_error(
  Consist_Trend(array(1:10, dim = c(a = 2, c = 5)), array(1:4, dim = c(a = 2, b = 2))),
  "Parameter 'exp' and 'obs' must have the same dimension names."
  )
  # time_dim
  expect_error(
  Consist_Trend(exp1, obs1, time_dim = c('sdate', 'ftime')),
  "Parameter 'time_dim' must be a character string."
  )
  expect_error(
  Consist_Trend(exp1, obs1, time_dim = 'asd'),
  "Parameter 'time_dim' is not found in 'exp' or 'obs' dimension."
  )
  # dat_dim
  expect_error(
  Consist_Trend(exp1, obs1, dat_dim = c(1, 2)),
  "Parameter 'dat_dim' must be a character string."
  )
  expect_error(
  Consist_Trend(exp1, obs1, dat_dim = c('member')),
  "Parameter 'dat_dim' is not found in 'exp' or 'obs' dimension."
  )
  # exp and obs (2)
  expect_error(
  Consist_Trend(array(1:10, dim = c(dataset = 2, member = 5, sdate = 4, ftime = 3)),
       array(1:4, dim = c(dataset = 2, member = 2, sdate = 5, ftime = 3))),
  paste0("Parameter 'exp' and 'obs' must have same length of ",
                "all dimension expect 'dat_dim'.")
  )
  # interval
  expect_error(
  Consist_Trend(exp1, obs1, interval = 0),
  "Parameter 'interval' must be a positive number."
  )
  # ncores
  expect_error(
  Consist_Trend(exp1, obs1, ncores = 0),
  "Parameter 'ncores' must be a positive integer."
  )

})

##############################################
test_that("2. Output checks: dat1", {

expect_equal(
names(Consist_Trend(exp1, obs1)),
c('trend', 'conf.lower', 'conf.upper', 'detrended_exp', 'detrended_obs')
)
expect_equal(
dim(Consist_Trend(exp1, obs1)$trend),
c(stats = 2, dataset = 3, ftime = 3)
)
expect_equal(
dim(Consist_Trend(exp1, obs1)$conf.lower),
c(stats = 2, dataset = 3, ftime = 3)
)
expect_equal(
dim(Consist_Trend(exp1, obs1)$conf.upper),
c(stats = 2, dataset = 3, ftime = 3)
)
expect_equal(
dim(Consist_Trend(exp1, obs1)$detrended_exp),
c(dataset = 2, sdate = 5, ftime = 3)
)
expect_equal(
dim(Consist_Trend(exp1, obs1)$detrended_obs),
c(dataset = 1, sdate = 5, ftime = 3)
)
expect_equal(
Consist_Trend(exp1, obs1)$trend[, 2, 1],
c(0.8287843, -0.1835020),
tolerance = 0.0001
)
expect_equal(
Consist_Trend(exp1, obs1)$conf.lower[, 2, 2],
c(-5.449028, -0.943639),
tolerance = 0.0001 
)
expect_equal(
Consist_Trend(exp1, obs1)$conf.upper[, 2, 2],
c(3.176215, 1.656969),
tolerance = 0.0001
)
expect_equal(
Consist_Trend(exp1, obs1)$detrended_exp[, 2, 1],
c(-0.449003, 1.133500),
tolerance = 0.0001
)
expect_equal(
Consist_Trend(exp1, obs1)$detrended_obs[, 2, 1],
c(0.2836287),
tolerance = 0.0001
)

})

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

expect_equal(
Consist_Trend(exp2, obs2)$trend[, 2, 1],
c(1.7520623, -0.4143214),
tolerance = 0.0001
)
expect_equal(
Consist_Trend(exp2, obs2)$detrended_exp[1, , 1],
c(NA, -0.3160783, 0.4098429, 0.1285491, -0.2223137),
tolerance = 0.0001
)
expect_equal(
Consist_Trend(exp2, obs2)$detrended_obs[1, , 1],
c(NA, -0.4826962, 1.2716524, -1.0952163, 0.3062600),
tolerance = 0.0001
)
expect_equal(
as.vector(Consist_Trend(exp2, obs2)$detrended_obs),
c(NA, -0.4826962, 1.2716524, -1.0952163, 0.3062600, -0.2100316, 0.2920924,
  -0.7289708, 1.4217907, -0.7748807, 0.6108713, NA, -0.5417854, -1.3599143,
  1.2908284),
tolerance = 0.0001
)
expect_equal(
mean(Consist_Trend(exp2, obs2)$trend, na.rm = TRUE),
0.1662461,
tolerance = 0.0001
)

})


