context("s2dv::RatioRMS tests")

##############################################
  # dat1
  set.seed(1)
  exp1_1 <- array(rnorm(120), dim = c(dataset = 1, sdate = 5, ftime = 3))
  set.seed(2)
  exp1_2 <- array(rnorm(120), dim = c(dataset = 1, sdate = 5, ftime = 3))
  set.seed(3)
  obs1 <- array(rnorm(80),  dim = c(dataset = 1, sdate = 5, ftime = 3))

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


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

  # exp1, exp2, obs
  expect_error(
  RatioRMS(c(), exp1_2, c()),
  "Parameter 'exp1', 'exp2', and 'obs' cannot be NULL."
  )
  expect_error(
  RatioRMS(c('b'), c('a'), obs1),
  "Parameter 'exp1', 'exp2', and 'obs' must be a numeric array."
  )
  expect_error(
  RatioRMS(exp1_1, array(1:10, dim = c(2, 5)), array(1:4, dim = c(2, 2))),
  "Parameter 'exp1', 'exp2', and 'obs' must have dimension names."
  )
  expect_error(
  RatioRMS(exp1_1, exp1_2, array(1:15, dim = c(data = 1, ftime = 3, sdates = 5))),
  "Parameter 'exp1', 'exp2', and 'obs' must have same dimension names."
  )
  expect_error(
  RatioRMS(exp1_1, exp1_2, array(1:12, dim = c(dataset = 1, ftime = 3, sdate = 4))),
  "Parameter 'exp1', 'exp2', and 'obs' must have the same length of all the dimensions."
  )
  # time_dim
  expect_error(
  RatioRMS(exp1_1, exp1_2, obs1, time_dim = c('sdate', 'ftime')),
  "Parameter 'time_dim' must be a character string."
  )
  expect_error(
  RatioRMS(exp1_1, exp1_2, obs1, time_dim = 'a'),
  "Parameter 'time_dim' is not found in 'exp1', 'exp2', and 'obs' dimensions."
  )
  # pval
  expect_error(
  RatioRMS(exp1_1, exp1_2, obs1, pval = 1),
  "Parameter 'pval' must be one logical value."
  )
  expect_error(
  RatioRMS(exp1_1, exp1_2, obs1, ncores = 1.5),
  "Parameter 'ncores' must be a positive integer."
  )


})

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

expect_equal(
names(RatioRMS(exp1_1, exp1_2, obs1)),
c('ratiorms', 'p.val')
)
expect_equal(
dim(RatioRMS(exp1_1, exp1_2, obs1)$ratiorms),
c(dataset = 1, ftime = 3)
)
expect_equal(
dim(RatioRMS(exp1_1, exp1_2, obs1)$p.val),
c(dataset = 1, ftime = 3)
)
expect_equal(
as.vector(RatioRMS(exp1_1, exp1_2, obs1)$p.val),
c(0.1811868, 0.4758232, 0.7473213),
tolerance = 0.0001
)
expect_equal(
as.vector(RatioRMS(exp1_1, exp1_2, obs1)$ratiorms),
c(2.0944471, 0.6814573, 1.1873955),
tolerance = 0.0001
)
expect_equal(
names(RatioRMS(exp1_1, exp1_2, obs1, pval = FALSE)),
c('ratiorms')
)
expect_equal(
as.vector(RatioRMS(exp1_1, exp1_2, obs1, pval = FALSE, time_dim = 'ftime')$ratiorms),
c(2.0832571, 0.7292987, 0.6031437, 1.1885930, 0.8542696),
tolerance = 0.0001
)
expect_equal(
as.vector(RatioRMS(exp1_1, exp1_2, obs1, time_dim = 'ftime')$p.val),
c(0.3745346, 0.6944118, 0.5334904, 0.8289285, 0.8437813),
tolerance = 0.0001
)

})

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

expect_equal(
names(RatioRMS(exp2_1, exp2_2, obs2)),
c('ratiorms', 'p.val')
)
expect_equal(
as.vector(RatioRMS(exp2_1, exp2_2, obs2)$p.val),
0.7418331,
tolerance = 0.0001
)
expect_equal(
as.vector(RatioRMS(exp2_1, exp2_2, obs2)$ratiorms),
0.8931399,
tolerance = 0.0001
)

})

