context("s2dv::Spread test")

##############################################
  # dat1
  set.seed(1)
  dat1 <- array(rnorm(240), c(member = 3, sdate = 4, ftime = 20))

  # dat2
  set.seed(2)
  dat2 <- rnorm(20)

##############################################

test_that("1. Input checks", {

  # data
  expect_error(
  Spread(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
    Spread(c(NA, NA)),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
    Spread(array(1:10, dim = c(2, 5))),
    "Parameter 'data' must have dimension names."
  )
  # compute_dim
  expect_error(
  Spread(dat1, compute_dim = 1),
  "Parameter 'compute_dim' must be a character vector."
  )
  expect_error(
  Spread(dat1, compute_dim = 'memb'),
  "Parameter 'compute_dim' has some element not in 'data' dimension names."
  )
  # na.rm
  expect_error(
  Spread(dat1, na.rm = 1),
  "Parameter 'na.rm' must be one logical value."
  )
  # conf
  expect_error(
  Spread(dat1, conf = 0.1),
  "Parameter 'conf' must be one logical value."
  )
  # conf.lev
  expect_error(
  Spread(dat1, conf.lev = c(0.05, 0.95)),
  "Parameter 'conf.lev' must be a numeric number between 0 and 1."
  )
  # ncores
  expect_error(
  Spread(dat1, ncores = 0),
  "Parameter 'ncores' must be a positive integer."
  )

})

##############################################
test_that("2. dat1", {

res1_1 <- Spread(dat1)

expect_equal(
names(res1_1),
c('iqr', 'maxmin', 'sd', 'mad')
)
expect_equal(
dim(res1_1$iqr),
c(stats = 3, sdate = 4, ftime = 20)
)
expect_equal(
dim(res1_1$maxmin),
c(stats = 3, sdate = 4, ftime = 20)
)
expect_equal(
dim(res1_1$sd),
c(stats = 3, sdate = 4, ftime = 20)
)
expect_equal(
dim(res1_1$mad),
c(stats = 3, sdate = 4, ftime = 20)
)

res1_2 <- Spread(dat1, conf = F)

expect_equal(
names(res1_2),
c('iqr', 'maxmin', 'sd', 'mad')
)
expect_equal(
dim(res1_2$iqr),
c(stats = 1, sdate = 4, ftime = 20)
)
expect_equal(
dim(res1_2$iqr),
dim(res1_2$maxmin)
)
expect_equal(
dim(res1_2$iqr),
dim(res1_2$sd)
)
expect_equal(
dim(res1_2$iqr),
dim(res1_2$mad)
)

})

##############################################

test_that("3. dat2", {

res2_1 <- Spread(dat2)

expect_equal(
names(res2_1),
c('iqr', 'maxmin', 'sd', 'mad')
)
expect_equal(
dim(res2_1$iqr),
c(stats = 3)
)
expect_equal(
dim(res2_1$maxmin),
c(stats = 3)
)
expect_equal(
dim(res2_1$sd),
c(stats = 3)
)
expect_equal(
dim(res2_1$mad),
c(stats = 3)
)

res2_2 <- Spread(dat2, conf = F)

expect_equal(
names(res2_2),
c('iqr', 'maxmin', 'sd', 'mad')
)
expect_equal(
dim(res2_2$iqr),
c(stats = 1)
)
expect_equal(
dim(res2_2$maxmin),
c(stats = 1)
)
expect_equal(
dim(res2_2$sd),
c(stats = 1)
)
expect_equal(
dim(res2_2$mad),
c(stats = 1)
)

})


