context("s2dv::WeightCells tests")

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

# dat1
set.seed(1)
data1 <- array(rnorm(5), dim = c(lat = 5))
set.seed(2)
lat1 <- array(rnorm(5)*5)

# dat2
set.seed(1)
data2 <- array(rnorm(60), dim = c(sdate = 10, lat = 2, lon = 3))
set.seed(2)
lat2 <- array(rnorm(2)*10)


##############################################
test_that("1. Input checks", {
  # data
  expect_error(
    WeightCells(c()),
    "Parameter 'data' cannot be NULL."
  )
  expect_error(
    WeightCells('lat'),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
    WeightCells(rnorm(5)),
    "Parameter 'data' must be at least latitude dimension."
  )
  expect_error(
    WeightCells(array(rnorm(5), dim = c(5))),
    "Parameter 'data' must have dimension names."
  )
  #lat_dim
  expect_error(
    WeightCells(data1, lat1, 3),
    "Parameter 'lat_dim' must be a character string."
  )
  expect_error(
    WeightCells(data1, lat1, 'latitude'),
    "Parameter 'lat_dim' is not found in 'data'."
  )
  #lat
  expect_error(
    WeightCells(data1, NULL),
    "Parameter 'lat' cannot be NULL."
  )
  expect_error(
    WeightCells(data1, rnorm(10)),
    "Length of parameter 'lat' doesn't match the length of latitudinal dimension in parameter 'data'."
  )
  # ncores
  expect_error(
    WeightCells(data1, lat1, ncores = 1.5),
    "Parameter 'ncores' must be either NULL or a positive integer."
  )

})

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

  expect_equal(
    dim(WeightCells(data1, lat1)),
    c(lat = 5)
  )
  expect_equal(
    as.vector(WeightCells(data1, lat1)),
    c(-0.6254941, 0.1836314, -0.8316143, 1.5913985, 0.3295037),
    tolerance = 0.0001
  )

})

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

  expect_equal(
    dim(WeightCells(data2, lat2)),
    c(sdate = 10, lat = 2, lon = 3)
  )
  expect_equal(
    as.vector(WeightCells(data2, lat2))[1:5],
    c( -0.6226120, 0.1825171, -0.8305040, 1.5854976, 0.3274870),
    tolerance = 0.0001
  )
  expect_equal(
    dim(WeightCells(data2, lat2)),
    dim(data2)
  )
})
