context("s2dv::Filter tests")

##############################################
  # dat1
  set.seed(1)
  dat1 <- array(rnorm(10), dim = c(dat = 1, ftime = 5, sdate = 2))
  freq1 <- 0.015
  # dat2
  set.seed(10)
  dat2 <- c(1:10) + rnorm(10)
  freq2 <- freq1

  # dat3
  dat3 <- dat2
  dat3[2] <- NA
  freq3 <- freq1
##############################################

test_that("1. Input checks", {

  # data
  expect_error(
  Filter(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
    Filter(c(NA, NA)),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
    Filter(array(1:10, dim = c(2, 5))),
    "Parameter 'data' must have dimension names."
  )
  # freq
  expect_error(
  Filter(dat1, c()),
  "Parameter 'freq' cannot be NULL."
  )
  expect_error(
  Filter(dat1, c(0.1, 0.2)),
  "Parameter 'freq' must be a number."
  )
  # time_dim
  expect_error(
    Filter(array(c(1:25), dim = c(dat = 1, date = 5, sdate = 5)), freq1),
    "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
    Filter(dat1, freq1, time_dim = 2),
    "Parameter 'time_dim' must be a character string."
  )
  # ncores
  expect_error(
    Filter(dat1, freq1, ncore = 3.5),
    "Parameter 'ncores' must be a positive integer."
  )
})

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

  expect_equal(
  dim(Filter(dat1, freq1)),
  c(ftime = 5, dat = 1, sdate = 2)
  )
  expect_equal(
  Filter(dat1, freq1)[, 1, 2],
  c(-0.080093110, 0.141328669, -0.105230299, -0.004168101, 0.048162841),
  tolerance = 0.0001
  )

})

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

  expect_equal(
  dim(Filter(dat2, freq2)),
  c(ftime = 10)
  )
  expect_equal(
  as.vector(Filter(dat2, freq2)[2:5]),
  c(0.1215244, -1.0229749, -0.2053940, 0.7375181),
  tolerance = 0.0001
  )

})

##############################################
test_that("4. Output checks: dat3", {
  expect_equal(
  dim(Filter(dat3, freq3)),
  c(ftime = 10)
  )
  expect_equal(
  as.vector(Filter(dat3, freq3)[2:5]),
  c(NA, -0.9414294, -0.1265448, 0.7910344),
  tolerance = 0.0001
  )

})
