context("s2dv::Spectrum tests")

##############################################
  # dat1
  set.seed(1)
  dat1 <- array(rnorm(10), dim = c(dat = 1, ftime = 5, sdate = 2))

  # dat2
  set.seed(10)
  dat2 <- c(1:10) + rnorm(10)

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

test_that("1. Input checks", {

  # data
  expect_error(
  Spectrum(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
    Spectrum(c(NA, NA)),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
    Spectrum(array(1:10, dim = c(2, 5))),
    "Parameter 'data' must have dimension names."
  )
  # time_dim
  expect_error(
    Spectrum(array(c(1:25), dim = c(dat = 1, date = 5, sdate = 5))),
    "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
    Spectrum(dat1, time_dim = 2),
    "Parameter 'time_dim' must be a character string."
  )
  # alpha
  expect_error(
  Spectrum(dat1, alpha = -1),
  "Parameter 'alpha' must be a numeric number between 0 and 1.",
  fixed = T
  )
  # ncores
  expect_error(
    Spectrum(dat1, ncore = 3.5),
    "Parameter 'ncores' must be a positive integer."
  )
})

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

  expect_equal(
  dim(Spectrum(dat1)),
  c(ftime = 2, stats = 3, dat = 1, sdate = 2)
  )
  expect_equal(
  Spectrum(dat1)[, 1, 1, 2],
  c(0.2, 0.4),
  tolerance = 0.0001
  )
  expect_equal(
  Spectrum(dat1)[, 2, 1, 2],
  c(0.89583007, 0.05516983),
  tolerance = 0.0001
  )

})

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

  expect_equal(
  dim(Spectrum(dat2)),
  c(ftime = 5, stats = 3)
  )
  expect_equal(
  Spectrum(dat2)[, 1],
  c(0.1, 0.2, 0.3, 0.4, 0.5),
  tolerance = 0.0001
  )
  expect_equal(
  Spectrum(dat2)[, 2],
  c(0.1767994, 1.0113808, 0.3341372, 0.1807377, 1.0594528),
  tolerance = 0.0001
  )

})

##############################################
test_that("4. Output checks: dat3", {
  expect_equal(
  dim(Spectrum(dat3)),
  c(ftime = 4, stats = 3)
  )
  expect_equal(
  Spectrum(dat3)[, 1],
  c(0.1111111, 0.2222222, 0.3333333, 0.4444444),
  tolerance = 0.0001
  )
  expect_equal(
  Spectrum(dat3)[, 2],
  c(0.7204816, 0.6529411, 0.2605188, 0.7009824),
  tolerance = 0.0001
  )

})
