context("s2dv::RPS tests")

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

# dat1
set.seed(1)
exp1 <- array(rnorm(60), dim = c(member = 3, sdate = 10, lat = 2))
set.seed(2)
obs1 <- array(rnorm(20), dim = c(sdate = 10, lat = 2))
set.seed(3)
weights1 <- array(abs(rnorm(30)), dim = c(member = 3, sdate = 10))
# dat2
set.seed(1)
exp2 <- array(rnorm(20), dim = c(member = 2, sdate = 10))
set.seed(2)
obs2 <- array(rnorm(10), dim = c(sdate = 10))

# dat2_1
set.seed(1)
exp2_1 <- array(rnorm(20), dim = c(member = 2, sdate = 10))
set.seed(2)
obs2_1 <- array(rnorm(10), dim = c(member = 1, sdate = 10))



##############################################
test_that("1. Input checks", {
  # exp and obs (1)
  expect_error(
  RPS(c()),
  'Parameter "exp" must be a numeric array.'
  )
  expect_error(
  RPS(exp1, c()),
  'Parameter "obs" must be a numeric array.'
  )

  # time_dim
  expect_error(
  RPS(exp1, obs1, time_dim = 1),
  'Parameter "time_dim" must be a character string.'
  )
  expect_error(
  RPS(exp1, obs1, time_dim = 'time'),
  "Parameter 'time_dim' is not found in 'exp' or 'obs' dimension."
  )
  # memb_dim
  expect_error(
  RPS(exp1, obs1, memb_dim = TRUE),
  "Parameter 'memb_dim' must be a character string."
  )
  expect_error(
  RPS(exp1, obs1, memb_dim = 'memb'),
  "Parameter 'memb_dim' is not found in 'exp' dimension."
  )
  # exp, ref, and obs (2)
  expect_error(
  RPS(exp1, array(1:9, dim = c(sdate = 9))),
  "Parameter 'exp' and 'obs' must have same length of all dimensions except 'memb_dim'."
  )
  # prob_thresholds
  expect_error(
  RPS(exp1, obs1, prob_thresholds = 1),
  "Parameter 'prob_thresholds' must be a numeric vector between 0 and 1."
  )
  # indices_for_clim
  expect_error(
  RPS(exp1, obs1, indices_for_clim = array(1:6, dim = c(2, 3))),
  "Parameter 'indices_for_clim' must be NULL or a numeric vector."
  )
  expect_error(
  RPS(exp1, obs1, indices_for_clim = 3:11),
  "Parameter 'indices_for_clim' should be the indices of 'time_dim'."
  )
  # Fair
  expect_error(
  RPS(exp1, obs1, Fair = 1),
  "Parameter 'Fair' must be either TRUE or FALSE."
  )
  # weights
  expect_error(
  RPS(exp1, obs1, weights = c(0, 1)),
  'Parameter "weights" must be a two-dimensional numeric array.'
  )
  expect_error(
  RPS(exp1, obs1, weights = array(1, dim = c(member = 3, time = 10))),
  "Parameter 'weights' must have two dimensions with the names of memb_dim and time_dim."
  )
  expect_error(
  RPS(exp1, obs1, weights = array(1, dim = c(member = 3, sdate = 1))),
  "Parameter 'weights' must have the same dimension lengths as memb_dim and time_dim in 'exp'."
  )
  # ncores
  expect_error(
  RPS(exp2, obs2, ncores = 1.5),
  "Parameter 'ncores' must be either NULL or a positive integer."
  )

})

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

expect_equal(
dim(RPS(exp1, obs1)),
c(lat = 2)
)
expect_equal(
as.vector(RPS(exp1, obs1)),
c(0.3555556, 0.4444444),
tolerance = 0.0001
)
expect_equal(
as.vector(RPS(exp1, obs1, Fair = T)),
c(0.2000000, 0.2666667),
tolerance = 0.0001
)
expect_equal(
as.vector(RPS(exp1, obs1, indices_for_clim = 3:5)),
c(0.5000000, 0.4666667),
tolerance = 0.0001
)
expect_equal(
as.vector(RPS(exp1, obs1, prob_thresholds = seq(0.1, 0.9, 0.1))),
c(1.600000, 1.888889),
tolerance = 0.0001
)
expect_equal(
as.vector(RPS(exp1, obs1, weights = weights1)),
c(0.3692964, 0.5346627),
tolerance = 0.0001
)

})

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

expect_equal(
dim(RPS(exp2, obs2)),
NULL
)
expect_equal(
RPS(exp2, obs2),
0.75,
tolerance = 0.0001
)
expect_equal(
RPS(exp2, obs2, indices_for_clim = 2:5, prob_thresholds = seq(0.1, 0.9, 0.1)),
2.75,
tolerance = 0.0001
)
expect_equal(
RPS(exp2, obs2),
RPS(exp2_1, obs2_1)
)
})
