context("s2dv::RandomWalkTest tests")

##############################################
  #dat1
  set.seed(1)
  dat1_A <- array(rnorm(64), dim = c(sdate = 4, ftime = 4, lat = 2, lon = 2))
  set.seed(2)  
  dat1_B <- array(rnorm(64), dim = c(sdate = 4, ftime = 4, lat = 2, lon = 2))
  #dat2
  set.seed(1)
  dat2_A <- array(rnorm(8), dim = c(sdate = 4, ftime = 2))
  set.seed(2)
  dat2_B <- array(rnorm(8), dim = c(sdate = 4, ftime = 2))

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

  expect_error(
  RandomWalkTest(c(), dat1_B),
  "Parameters 'skill_A' and 'skill_B' cannot be NULL."
  )
  expect_error(
  RandomWalkTest(dat1_A, 'a'),
  "Parameters 'skill_A' and 'skill_B' must be a numerical array."
  )
  expect_error(
  RandomWalkTest(array(1:10, dim = c(2, 5)), array(1:12, dim = c(2, 6))),
  "Parameters 'skill_A' and 'skill_B' must have the same dimensions."
  )
  expect_error(
  RandomWalkTest(1:10, 2:11, time_dim = 12),
  "Parameter 'time_dim' must be a character string."
  )
  expect_error(
  RandomWalkTest(dat1_A, dat1_B, time_dim = 'date'),
  "Parameter 'time_dim' is not found in 'skill_A' or 'skill_B' dimension."
  )
  expect_error(
  RandomWalkTest(dat1_A, dat1_B, alpha = 1),
  "Parameter 'alpha' must be a number between 0 and 1."
  )
  expect_error(  
  RandomWalkTest(dat1_A, dat1_B, test.type = 1),
  "Parameter 'test.type' must be 'two.sided.approx', 'two.sided', 'greater', or 'less'."
  )
  expect_error(
  RandomWalkTest(dat1_A, dat1_B, ncores = T, sign = T, pval = F),
  "Parameter 'ncores' must be a positive integer."
  )

})

##############################################
test_that("2. Output checks: dat1", {
  res <- RandomWalkTest(dat1_A, dat1_B, sign = T, pval = F)

  expect_equal(
  length(res),
  2
  )
  expect_equal(
  names(res),
  c("score", "sign")
  )
  expect_equal(
  dim(res$score),
  c(ftime = 4, lat = 2, lon = 2)
  )
  expect_equal(
  dim(res$sign),  
  c(ftime = 4, lat = 2, lon = 2)
  )
  expect_equal(
  is.numeric(res$score),
  TRUE
  )
  expect_equal(
  is.logical(res$sign),
  TRUE
  )
  expect_equal(
  mean(res$score, na.rm = T),
  0
  )
  expect_equal(
  res$score[, 1, 1],
  c(0, 0, 2, 2)
  )
  expect_equal(
  res$score[, 1, 2],
  c(0, -4, -2, 0)
  )

})

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

  res1 <- RandomWalkTest(dat2_A, dat2_B, sign = T, pval = T, test.type = "two.sided", alpha = 0.1)

  expect_equal(
  names(res1),
  c("score", "p.val", "sign")
  )
  expect_equal(
  dim(res1$score), 
  c(ftime = 2)
  )
  expect_equal(
  dim(res1$p.val),
  c(ftime = 2)
  )
  expect_equal(
  dim(res1$sign),
  c(ftime = 2)
  )
  expect_equal(
  as.vector(res1$score),
  c(0, 0)
  )
  expect_equal(
  as.vector(res1$p),
  c(1, 1)
  )
  expect_equal(
  as.vector(res1$sign),
  c(F, F)
  )

#------------------------------
  res2 <- RandomWalkTest(dat2_A + 1, dat2_B + 2, sign = T, pval = T, test.type = "greater", alpha = 0.1)

  expect_equal(
  as.vector(res2$score),
  c(2, 4)
  )
  expect_equal(
  as.vector(res2$p),
  c(0.3125, 0.0625)
  )
  expect_equal(
  as.vector(res2$sign),
  c(F, T)
  )

#------------------------------
  res3 <- RandomWalkTest(dat2_A, dat2_B - 2, sign = T, pval = T, test.type = "less", alpha = 0.1)
  expect_equal(
  as.vector(res3$score),
  c(-2, -4)
  )
  expect_equal(
  as.vector(res3$p),
  c(0.3125, 0.0625)
  )
  expect_equal(
  as.vector(res3$sign),
  c(F, T)
  )

})


