context("s2dv::Histo2Hindcast tests")

##############################################
# dat1
set.seed(1)
dat1 <- array(rnorm(24), dim = c(sdate = 1, ftime = 24))
sdatesin1 <- '19901101'
sdatesout1 <- c('19901101', '19911101')
nleadtimesout1 <- 12

# dat2
set.seed(1)
dat2 <- array(rnorm(288), dim = c(dat = 1, member = 2, sdate = 1, ftime = 24, lat = 2, lon = 3))
sdatesin2 <- '19901101'
sdatesout2 <- c('19901101', '19911101')
nleadtimesout2 <- 12

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

  # dat
  expect_error(
  Histo2Hindcast(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
  Histo2Hindcast(c(NA, NA)),
  "Parameter 'data' must be a numeric array."
  )
  # sdatesin
  expect_error(
  Histo2Hindcast(dat1, c()),
  "Parameter 'sdatesin' cannot be NULL."
  )
  expect_error(
  Histo2Hindcast(dat1, sdatesin = '1999'),
  paste0("Parameter 'sdatesin' must be a character string in the format",
         " 'YYYYMMDD' or 'YYYYMM'.")
  )
  expect_error(
  Histo2Hindcast(dat1, sdatesin = c('19991101', '19991201')),
  paste0("Parameter 'sdatesin' must be a character string in the format",
         " 'YYYYMMDD' or 'YYYYMM'.")
  )
  # sdatesout
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, c()),
  "Parameter 'sdatesout' cannot be NULL."
  )
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = 1999:2000),
  paste0("Parameter 'sdatesout' must be a vector of character in the ",
         "format 'YYYYMMDD' or 'YYYYMM'.")
  )
  # nleadtimesout
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1, c()),
  "Parameter 'nleadtimesout' cannot be NULL."
  )
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = c(10, 12)),
  "Parameter 'nleadtimesout' must be a positive integer."
  )
  # sdate_dim
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1, sdate_dim = 1),
  "Parameter 'sdate_dim' must be a character string."
  )
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1, sdate_dim = 'time'),
  "Parameter 'sdate_dim' is not found in 'data' dimension."
  )
  expect_error(
  Histo2Hindcast(array(1:10, dim = c(sdate = 2, ftime = 5)),
                 sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1),
  "The dimension length of sdate_dim of 'data' must be 1."
  )
  # ftime_dim
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1, ftime_dim = 2),
  "Parameter 'ftime_dim' must be a character string."
  )
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1, ftime_dim = 'time'),
  "Parameter 'ftime_dim' is not found in 'data' dimension."
  )
  # ncores
  expect_error(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1, ncores = 0),
  "Parameter 'ncores' must be a positive integer."
  )

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

  expect_equal(
  dim(Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                     nleadtimesout = nleadtimesout1)),
  c(sdate = 2, ftime = 12)
  )
  expect_equal(
  mean(Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                      nleadtimesout = nleadtimesout1)),
  0.1498669,
  tolerance = 0.00001
  )
  expect_equal(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1)[1, 5:7],
  c(0.3295078, -0.8204684, 0.4874291),
  tolerance = 0.00001
  )
  expect_equal(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1)[2, 5:7],
  c(-0.01619026, 0.94383621, 0.82122120),
  tolerance = 0.00001
  )

sdatesout1 <- c('19901101', '19910101')
nleadtimesout1 <- 6

  expect_equal(
  dim(Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                     nleadtimesout = nleadtimesout1)),
  c(sdate = 2, ftime = 6)
  )
  expect_equal(
  mean(Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                      nleadtimesout = nleadtimesout1)),
  0.1100272,
  tolerance = 0.00001
  )
  expect_equal(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1)[1, 3:5],
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1)[2, 1:3],
  tolerance = 0.00001
  )


sdatesout1 <- c('19901101', '19911101')
nleadtimesout1 <- 15

  expect_equal(
  mean(Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                      nleadtimesout = nleadtimesout1), na.rm = T),
  0.06984426,
  tolerance = 0.00001
  )
  expect_equal(
  length(which(is.na(Histo2Hindcast(dat1, sdatesin = sdatesin1, 
                                    sdatesout = sdatesout1,
                                    nleadtimesout = nleadtimesout1)))),
  3
  )
  expect_equal(
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1)[1, 13:15],
  Histo2Hindcast(dat1, sdatesin = sdatesin1, sdatesout = sdatesout1,
                 nleadtimesout = nleadtimesout1)[2, 1:3]
  )

})
##############################################
test_that("3. dat2", {

  expect_equal(
  dim(Histo2Hindcast(dat2, sdatesin = sdatesin2, sdatesout = sdatesout2,
                     nleadtimesout = nleadtimesout2)),
  c(sdate = 2, ftime = 12, dat = 1, member = 2, lat = 2, lon = 3)
  )

})
