context("s2dv::RatioSDRMS tests")

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

  # dat2
  exp2 <- exp1
  obs2 <- obs1
  exp2[1] <- NA

  # dat 3
  set.seed(3)
  exp3 <- array(rnorm(10), dim = c(member = 2, sdate = 5))
  set.seed(4)
  obs3 <- array(rnorm(5), dim = c(member = 1, sdate = 5))


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

  expect_error(
  RatioSDRMS(c(), c()),
  "Parameter 'exp' and 'obs' cannot be NULL."
  )
  expect_error(
  RatioSDRMS(c('b'), c('a')),
  "Parameter 'exp' and 'obs' must be a numeric array."
  )
  expect_error(
  RatioSDRMS(c(1:10), c(2:4)),
  "Parameter 'exp' and 'obs' must be array with as least two dimensions memb_dim and time_dim."
  )
  expect_error(
  RatioSDRMS(array(1:10, dim = c(2, 5)), array(1:4, dim = c(2, 2))),
  "Parameter 'exp' and 'obs' must have dimension names."
  )
  expect_error(
  RatioSDRMS(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."
  )
  expect_error(
  RatioSDRMS(exp1, obs1, dat_dim = 1),
  "Parameter 'dat_dim' must be a character string."
  )
  expect_error(
  RatioSDRMS(exp1, obs1, dat_dim = 'a'),
  "Parameter 'dat_dim' is not found in 'exp' or 'obs' dimension."
  )
  expect_error(
  RatioSDRMS(exp1, obs1, memb_dim = 1),
  "Parameter 'memb_dim' must be a character string."
  )
  expect_error(
  RatioSDRMS(exp1, obs1, memb_dim = 'a'),
  "Parameter 'memb_dim' is not found in 'exp' or 'obs' dimension."
  )
  expect_error(
  RatioSDRMS(exp1, obs1, time_dim = c('sdate', 'a')),
  "Parameter 'time_dim' must be a character string."
  )
  expect_error(
  RatioSDRMS(exp1, obs1, time_dim = 'a'),
  "Parameter 'time_dim' is not found in 'exp' or 'obs' dimension."
  )
  expect_error(
  RatioSDRMS(exp1, obs1, pval = 1),
  "Parameter 'pval' must be one logical value."
  )
  expect_error(
  RatioSDRMS(exp1, obs1, ncores = 1.5),
  "Parameter 'ncores' must be a positive integer."
  )
  expect_error(
  RatioSDRMS(exp = exp3,
             obs = array(1:2, dim = c(member = 1, sdate = 2)), dat_dim = NULL),
  "Parameter 'exp' and 'obs' must have same length of all the dimensions except 'dat_dim' and 'memb_dim'."
  )

})

##############################################
test_that("2. Output checks: dat1", {
expect_equal(
names(RatioSDRMS(exp1, obs1)),
c('ratio', 'p.val')
)
expect_equal(
dim(RatioSDRMS(exp1, obs1)$ratio),
c(nexp = 2, nobs = 1, ftime = 2)
)
expect_equal(
dim(RatioSDRMS(exp1, obs1)$p.val),
c(nexp = 2, nobs = 1, ftime = 2)
)
expect_equal(
as.vector(RatioSDRMS(exp1, obs1)$ratio),
c(0.7198164, 0.6525068, 0.6218262, 0.6101527),
tolerance = 0.0001
)
expect_equal(
as.vector(RatioSDRMS(exp1, obs1)$p.val),
c(0.8464094, 0.8959219, 0.9155102, 0.9224119),
tolerance = 0.0001
)
expect_equal(
names(RatioSDRMS(exp1, obs1, pval = F)),
c('ratio')
)
expect_equal(
as.vector(RatioSDRMS(exp1, obs1)$ratio),
as.vector(RatioSDRMS(exp1, obs1, pval = F)$ratio)
)

})

##############################################
test_that("3. Output checks: dat2", {
expect_equal(
dim(RatioSDRMS(exp2, obs2)$ratio),
c(nexp = 2, nobs = 1, ftime = 2)
)
expect_equal(
as.vector(RatioSDRMS(exp2, obs2)$ratio),
c(0.7635267, 0.6525068, 0.6218262, 0.6101527),
tolerance = 0.0001
)
expect_equal(
as.vector(RatioSDRMS(exp1, obs1)$p.val),
c(0.8464094, 0.8959219, 0.9155102, 0.9224119),
tolerance = 0.0001
)

})

##############################################
test_that("4. Output checks: dat3", {
expect_equal(
names(RatioSDRMS(exp3, obs3, dat_dim = NULL)),
c('ratio', 'p.val')
)
expect_equal(
dim(RatioSDRMS(exp3, obs3, dat_dim = NULL)$ratio),
NULL
)
expect_equal(
dim(RatioSDRMS(exp3, obs3, dat_dim = NULL)$p.val),
NULL
)
expect_equal(
as.numeric(RatioSDRMS(exp3, obs3, dat_dim = NULL)$ratio),
0.8291582,
tolerance = 0.0001
)
expect_equal(
as.numeric(RatioSDRMS(exp3, obs3, dat_dim = NULL)$p.val),
0.7525497,
tolerance = 0.0001
)


})
