##############################################
  # dat1
  set.seed(1)
  dat1 <- array(rnorm(24), dim = c(dataset = 1, member = 3, sdate = 4, ftime = 2))
##############################################

test_that("1. Input checks", {

  expect_error(
  ProbBins(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
    ProbBins(c(NA, NA)),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
    ProbBins(array(1:10, dim = c(2, 5))),
    "Parameter 'data' must have dimension names."
  )
  # thr
  expect_error(
    ProbBins(dat1, thr = c()),
    "Parameter 'thr' cannot be NULL."
  )
  expect_error(
    ProbBins(dat1, thr = TRUE),
    "Parameter 'thr' must be a numeric vector."
  )
  expect_error(
    ProbBins(dat1, thr = 1:10),
    "Parameter 'thr' must be within the range \\[0, 1\\] if 'quantile' is TRUE."
  )
  # time_dim
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), time_dim = 'time'),
    "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), time_dim = 2),
    "Parameter 'time_dim' must be a character string."
  )
  # memb_dim
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), memb_dim = 2),
    "Parameter 'memb_dim' must be a character string."
  )
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), memb_dim = 'ens'),
    "Parameter 'memb_dim' is not found in 'data' dimension."
  )
  # fcyr
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 'sdate'),
    "Parameter 'fcyr' must be a numeric vector or 'all'."
  )
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 2:6),
    "Parameter 'fcyr' must be the indices of 'time_dim' within the range \\[1, 4\\]."
  )
  # quantile
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), quantile = 0.9),
    "Parameter 'quantile' must be one logical value."
  )
  # compPeriod
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), compPeriod = TRUE),
    "Parameter 'compPeriod' must be either 'Full period', 'Without fcyr', or 'Cross-validation'."
  )
  # ncores
  expect_error(
    ProbBins(dat1, thr = c(1/3, 2/3), ncores = 0),
    "Parameter 'ncores' must be a positive integer."
  )

})


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

expect_equal(
dim(ProbBins(dat1, thr = c(1/3, 2/3))),
c(bin = 3, sdate = 4, member = 3, dataset = 1, ftime = 2)
)
expect_equal(
dim(ProbBins(dat1, thr = c(0.25, 0.5, 0.75))),
c(bin = 4, sdate = 4, member = 3, dataset = 1, ftime = 2)
)
expect_equal(
dim(ProbBins(dat1, thr = c(0.25, 0.5, 0.75), compPeriod = 'Cross-validation')),
c(bin = 4, sdate = 4, member = 3, dataset = 1, ftime = 2)
)
expect_equal(
dim(ProbBins(dat1, thr = c(0.25, 0.5, 0.75), compPeriod = 'Without fcyr')),
c(bin = 4, sdate = 4, member = 3, dataset = 1, ftime = 2)
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1/3, 2/3)) == 0)),
48
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1/3, 2/3)) == 1)),
24
)
expect_equal(
which(ProbBins(dat1, thr = c(1/3, 2/3)) == 1),
c(1, 6, 8, 10, 14, 17, 21, 24, 25, 28, 33, 35, 37, 40, 45, 47, 49, 53, 56, 59, 63, 66, 69, 70)
)
expect_equal(
all(is.na(ProbBins(dat1, thr = c(1/3, 2/3), compPeriod = 'Without fcyr'))),
TRUE
)
expect_equal(
dim(ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 2, compPeriod = 'Without fcyr')),
c(bin = 3, sdate = 1, member = 3, dataset = 1, ftime = 2)
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 2, compPeriod = 'Without fcyr') == 0)),
12
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 2, compPeriod = 'Without fcyr') == 1)),
6
)
expect_equal(
which(ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 2, compPeriod = 'Without fcyr') == 1),
c(3, 5, 7, 11, 14, 18)
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 2, compPeriod = 'Cross-validation') == 0)),
12
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 2, compPeriod = 'Cross-validation') == 1)),
6
)
expect_equal(
which(ProbBins(dat1, thr = c(1/3, 2/3), fcyr = 2, compPeriod = 'Cross-validation') == 1),
c(3, 5, 7, 11, 14, 18)
)

expect_equal(
dim(ProbBins(dat1, thr = c(1/3, 2/3), quantile = FALSE)),
c(bin = 3, sdate = 4, member = 3, dataset = 1, ftime = 2)
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1/3, 2/3), quantile = FALSE) == 0)),
48
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1/3, 2/3), quantile = FALSE) == 1)),
24
)
expect_equal(
which(ProbBins(dat1, thr = c(1/3, 2/3), quantile = FALSE) == 1),
c(1, 6, 8, 10, 13, 16, 21, 24, 25, 28, 32, 35, 37, 40, 45, 48, 49, 52, 56, 58, 63, 66, 69, 70)
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1:3), quantile = FALSE) == 0)),
72
)
expect_equal(
length(which(ProbBins(dat1, thr = c(1:3), quantile = FALSE) == 1)),
24
)


})
