context("s2dv::UltimateBrier tests")

##############################################
# dat1
set.seed(1)
exp1 <- array(rnorm(30), dim = c(dataset = 1, member = 3, sdate = 5, ftime = 2))
set.seed(2)
obs1 <- array(round(rnorm(10)), dim = c(dataset = 1, sdate = 5, ftime = 2))

# dat2
set.seed(1)
exp2 <- array(rnorm(30), dim = c(member = 3, sdate = 5, ftime = 2))
set.seed(2)
obs2 <- array(round(rnorm(10)), dim = c(sdate = 5, ftime = 2))
##############################################
test_that("1. Input checks", {
  # exp and obs
  expect_error(
  UltimateBrier(exp1, c()),
  "Parameter 'exp' and 'obs' cannot be NULL."
  )
  expect_error(
  UltimateBrier(c('b'), obs1),
  "Parameter 'exp' and 'obs' must be a vector or a numeric array."
  )
  expect_error(
  UltimateBrier(array(1:10, dim = c(2, 5)), array(1:4, dim = c(2, 2))),
  "Parameter 'exp' and 'obs' must have dimension names."
  )
  # dat_dim
  expect_error(
  UltimateBrier(exp1, obs1, dat_dim = 2),
  "Parameter 'dat_dim' must be a character string or NULL."
  )
  expect_error(
  UltimateBrier(exp1, obs1, dat_dim = 'dat'),
  "Parameter 'dat_dim' is not found in 'exp' or 'obs' dimension."
  )
  # memb_dim
  expect_error(
  UltimateBrier(exp1, obs1, memb_dim = 2),
  "Parameter 'memb_dim' must be a character string."
  )
  expect_error(
  UltimateBrier(exp1, obs1, memb_dim = 'ensemble'),
  "Parameter 'memb_dim' is not found in 'exp' dimension."
  )
  expect_error(
  UltimateBrier(exp1, array(1:10, dim = c(dataset = 1, member = 2, sdate = 5, ftime = 2))),
  "The length of parameter 'memb_dim' in 'obs' must be 1."
  )
  # time_dim
  expect_error(
  UltimateBrier(exp1, obs1, time_dim = 1),
  "Parameter 'time_dim' must be a character string."
  )
  expect_error(
  UltimateBrier(exp1, obs1, memb_dim = 'member', time_dim = 'time'),
  "Parameter 'time_dim' is not found in 'exp' or 'obs' dimension."
  )
  # exp and obs (2)
  expect_error(
  UltimateBrier(exp1, array(1:10, dim = c(dataset = 1, sdate = 6, ftime = 2))),
  paste0("Parameter 'exp' and 'obs' must have the same names and lengths ",
                "of all the dimensions except 'dat_dim' and 'memb_dim'.")
  )
  expect_error(
  UltimateBrier(exp1, array(1:10, dim = c(dataset = 1, sdate = 5, time = 2))),
  paste0("Parameter 'exp' and 'obs' must have the same names and lengths ",
                "of all the dimensions except 'dat_dim' and 'memb_dim'.")
  )
  # quantile
  expect_error(
  UltimateBrier(exp1, obs1, quantile = c(0.05, 0.95)),
  "Parameter 'quantile' must be one logical value."
  )
  expect_error(
  UltimateBrier(exp1, obs1, quantile = FALSE, thr = 1:3, type = 'FairEnsembleBS'),
  "Parameter 'quantile' must be TRUE if 'type' is 'FairEnsembleBSS' or 'FairEnsembleBS'."
  )
  # thr
  expect_error(
  UltimateBrier(exp1, obs1, thr = TRUE),
  "Parameter 'thr' must be a numeric vector."
  )
  expect_error(
  UltimateBrier(exp1, obs1, quantile = TRUE, thr = 1:3),
  "Parameter 'thr' must be between 0 and 1 when quantile is TRUE."
  )
  # type
  expect_error(
  UltimateBrier(exp1, obs1, type = 'UltimateBrier'),
  "Parameter 'type' must be one of 'BS', 'BSS', 'FairEnsembleBS', 'FairEnsembleBSS', 'FairStartDatesBS' or 'FairStartDatesBSS'."
  )
  # decomposition
  expect_error(
  UltimateBrier(exp1, obs1, decomposition = 1),
  "Parameter 'decomposition' must be one logical value."
  )
  # ncores
  expect_error(
  UltimateBrier(exp1, obs1, ncores = 0),
  "Parameter 'ncores' must be a positive integer."
  )


})

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

  # 'BS'
  expect_equal(
  is.list(UltimateBrier(exp1, obs1)),
  TRUE
  )
  expect_equal(
  names(UltimateBrier(exp1, obs1)),
  c('bs', 'rel', 'res', 'unc')
  )
  expect_equal(
  is.list(UltimateBrier(exp1, obs1, decomposition = FALSE)),
  FALSE
  )
  expect_equal(
  dim(UltimateBrier(exp1, obs1, decomposition = FALSE)),
  c(nexp = 1, nobs = 1, bin = 3, ftime = 2)
  )
  expect_equal(
  dim(UltimateBrier(exp1, obs1, decomposition = FALSE, thr = c(0.25, 0.5, 0.75))),
  c(nexp = 1, nobs = 1, bin = 4, ftime = 2)
  )
  expect_equal(
  UltimateBrier(exp1, obs1)$bs,
  UltimateBrier(exp1, obs1, decomposition = FALSE)
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1)$bs),
  c(0.42222222, 0.44444444, 0.02222222, 0.48888889, 0.37777778, 0.02222222),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1)$rel),
  c(0.22222222, 0.31111111, 0.02222222, 0.28888889, 0.24444444, 0.02222222),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1)$res),
  c(0.0400000, 0.1066667, 0.0000000, 0.0400000, 0.1066667, 0.0000000),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1)$unc),
  c(0.24, 0.24, 0.00, 0.24, 0.24, 0.00),
  tolerance = 0.0001
  )

  # 'BSS'
  expect_equal(
  dim(UltimateBrier(exp1, obs1, type = 'BSS')),
  c(nexp = 1, nobs = 1, bin = 3, ftime = 2)
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1, type = 'BSS')),
  c(-0.7592593, -0.8518519, -Inf, -1.0370370, -0.5740741, -Inf),
  tolerance = 0.0001
  )

  # 'FairStartDatesBS'
  expect_equal(
  is.list(UltimateBrier(exp1, obs1, type = 'FairStartDatesBS')),
  TRUE
  )
  expect_equal(
  names(UltimateBrier(exp1, obs1, type = 'FairStartDatesBS')),
  c('bs', 'rel', 'res', 'unc')
  )
  expect_equal(
  is.list(UltimateBrier(exp1, obs1, decomposition = FALSE, type = 'FairStartDatesBS')),
  FALSE
  )
  expect_equal(
  dim(UltimateBrier(exp1, obs1, decomposition = FALSE, type = 'FairStartDatesBS')),
  c(nexp = 1, nobs = 1, bin = 3, ftime = 2)
  )
  expect_equal(
  UltimateBrier(exp1, obs1, type = 'FairStartDatesBS')$bs,
  UltimateBrier(exp1, obs1, decomposition = FALSE, type = 'FairStartDatesBS')
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1, type = 'FairStartDatesBS')$bs),
  c(0.42222222, 0.44444444, 0.02222222, 0.48888889, 0.37777778, 0.02222222),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1, type = 'FairStartDatesBS')$rel),
  c(0.22222222, 0.31111111, 0.02222222, 0.28888889, 0.24444444, 0.02222222),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1, type = 'FairStartDatesBS')$res),
  c(0.0400000, 0.1066667, 0.0000000, 0.0400000, 0.1066667, 0.0000000),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1, type = 'FairStartDatesBS')$unc),
  c(0.24, 0.24, 0.00, 0.24, 0.24, 0.00),
  tolerance = 0.0001
  )

  # 'FairStartDatesBSS'
  expect_equal(
  dim(UltimateBrier(exp1, obs1, type = 'FairStartDatesBSS')),
  c(nexp = 1, nobs = 1, bin = 3, ftime = 2)
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1, type = 'FairStartDatesBSS')),
  c(-0.7592593, -0.8518519, -Inf, -1.0370370, -0.5740741, -Inf),
  tolerance = 0.0001
  )
  # 'FairEnsembleBS'
  expect_equal(
  dim(UltimateBrier(exp1, obs1, type = 'FairEnsembleBS')),
  c(nexp = 1, nobs = 1, bin = 3, ftime = 2)
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1, type = 'FairEnsembleBS')),
  c(0.1333333, 0.2000000, 0.2000000, 0.1333333, 0.4000000, 0.2000000),
  tolerance = 0.0001
  )
  # 'FairEnsembleBSS'
  expect_equal(
  dim(UltimateBrier(exp1, obs1, type = 'FairEnsembleBSS')),
  c(nexp = 1, nobs = 1, bin = 3, ftime = 2)
  )
  expect_equal(
  as.vector(UltimateBrier(exp1, obs1, type = 'FairEnsembleBSS')),
  c(-0.1111111, -0.6666667, -0.6666667, 0.2592593, -1.2222222, -0.6666667),
  tolerance = 0.0001
  )

})

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

  # 'BS'
  expect_equal(
  is.list(UltimateBrier(exp2, obs2, dat_dim = NULL)),
  TRUE
  )
  expect_equal(
  names(UltimateBrier(exp2, obs2, dat_dim = NULL)),
  c('bs', 'rel', 'res', 'unc')
  )
  expect_equal(
  is.list(UltimateBrier(exp2, obs2, dat_dim = NULL, decomposition = FALSE)),
  FALSE
  )
  expect_equal(
  dim(UltimateBrier(exp2, obs2, dat_dim = NULL, decomposition = FALSE)),
  c(bin = 3, ftime = 2)
  )
  expect_equal(
  dim(UltimateBrier(exp2, obs2, dat_dim = NULL, decomposition = FALSE, thr = c(0.25, 0.5, 0.75))),
  c(bin = 4, ftime = 2)
  )
  expect_equal(
  UltimateBrier(exp2, obs2, dat_dim = NULL)$bs,
  UltimateBrier(exp2, obs2, dat_dim = NULL, decomposition = FALSE)
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL)$bs),
  c(0.42222222, 0.44444444, 0.02222222, 0.48888889, 0.37777778, 0.02222222),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL)$rel),
  c(0.22222222, 0.31111111, 0.02222222, 0.28888889, 0.24444444, 0.02222222),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL)$res),
  c(0.0400000, 0.1066667, 0.0000000, 0.0400000, 0.1066667, 0.0000000),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL)$unc),
  c(0.24, 0.24, 0.00, 0.24, 0.24, 0.00),
  tolerance = 0.0001
  )

  # 'BSS'
  expect_equal(
  dim(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'BSS')),
  c(bin = 3, ftime = 2)
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'BSS')),
  c(-0.7592593, -0.8518519, -Inf, -1.0370370, -0.5740741, -Inf),
  tolerance = 0.0001
  )

  # 'FairStartDatesBS'
  expect_equal(
  is.list(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBS')),
  TRUE
  )
  expect_equal(
  names(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBS')),
  c('bs', 'rel', 'res', 'unc')
  )
  expect_equal(
  is.list(UltimateBrier(exp2, obs2, dat_dim = NULL, decomposition = FALSE, type = 'FairStartDatesBS')),
  FALSE
  )
  expect_equal(
  dim(UltimateBrier(exp2, obs2, dat_dim = NULL, decomposition = FALSE, type = 'FairStartDatesBS')),
  c(bin = 3, ftime = 2)
  )
  expect_equal(
  UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBS')$bs,
  UltimateBrier(exp2, obs2, dat_dim = NULL, decomposition = FALSE, type = 'FairStartDatesBS')
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBS')$bs),
  c(0.42222222, 0.44444444, 0.02222222, 0.48888889, 0.37777778, 0.02222222),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBS')$rel),
  c(0.22222222, 0.31111111, 0.02222222, 0.28888889, 0.24444444, 0.02222222),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBS')$res),
  c(0.0400000, 0.1066667, 0.0000000, 0.0400000, 0.1066667, 0.0000000),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBS')$unc),
  c(0.24, 0.24, 0.00, 0.24, 0.24, 0.00),
  tolerance = 0.0001
  )

  # 'FairStartDatesBSS'
  expect_equal(
  dim(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBSS')),
  c(bin = 3, ftime = 2)
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairStartDatesBSS')),
  c(-0.7592593, -0.8518519, -Inf, -1.0370370, -0.5740741, -Inf),
  tolerance = 0.0001
  )
  # 'FairEnsembleBS'
  expect_equal(
  dim(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairEnsembleBS')),
  c(bin = 3, ftime = 2)
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairEnsembleBS')),
  c(0.1333333, 0.2000000, 0.2000000, 0.1333333, 0.4000000, 0.2000000),
  tolerance = 0.0001
  )
  # 'FairEnsembleBSS'
  expect_equal(
  dim(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairEnsembleBSS')),
  c(bin = 3, ftime = 2)
  )
  expect_equal(
  as.vector(UltimateBrier(exp2, obs2, dat_dim = NULL, type = 'FairEnsembleBSS')),
  c(-0.1111111, -0.6666667, -0.6666667, 0.2592593, -1.2222222, -0.6666667),
  tolerance = 0.0001
  )

})
