##############################################
  # dat1
  set.seed(1)
  dat1 <- array(rnorm(100), 
                dim = c(sdate = 50, space = 2))
  weights1 <- array(c(0.9, 1.1), dim = c(space = 2))

  # dat2
  set.seed(2)
  dat2 <- array(rnorm(300),
                dim = c(dat = 1, sdate = 50, lat = 2, lon = 3))
  weights2 <- array(c(0.9, 1.1), dim = c(lat = 2, lon = 3))

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

test_that("1. Input checks", {
  # data
  expect_error(
  Cluster(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
    Cluster(c(NA, NA)),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
    Cluster(array(1:10, dim = c(2, 5))),
    "Parameter 'data' must have dimension names."
  )
  # weights
  expect_error(
  Cluster(dat1, weights = 'lat'),
  "Parameter 'weights' must be a numeric array."
  )
  expect_error(
  Cluster(dat1, weights = 2),
  "Parameter 'weights' must have dimension names."
  )
  expect_error(
  Cluster(dat1, weights = array(2, dim = c(lat = 2))),
  "Parameter 'weights' must have dimensions that can be found in 'data' dimensions."
  )
  # time_dim
  expect_error(
    Cluster(dat1, weights1, time_dim = 'a'),
    "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
    Cluster(array(c(1:25), dim = c(dat = 1, time = 5, space = 2)), weights1),
    "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
    Cluster(dat1, weights1, time_dim = 2),
    "Parameter 'time_dim' must be a character string."
  )
  expect_error(
    Cluster(dat1, weights1, time_dim = c('a', 'sdate')),
    "Parameter 'time_dim' must be a character string."
  )
  # nclusters
  expect_error(
  Cluster(dat1, weights1, ncluster = 1, space_dim = 'space'),
  "Parameter 'nclusters' must be an integer bigger than 1."
  )
  # index
  expect_error(
  Cluster(dat1, weights1, index = 1, space_dim = 'space'),
  "Parameter 'index' should be a character strings accepted as 'index' by the function NbClust::NbClust."
  )
  # ncores
  expect_error(
    Cluster(dat1, weights1, ncore = 0, space_dim = 'space'),
    "Parameter 'ncores' must be a positive integer."
  )
})

##############################################
test_that("2. Output checks: dat1", {
  expect_equal(
    names(Cluster(dat1, weights1, space_dim = 'space')),
    c("cluster", "centers", "totss", "withinss", "tot.withinss",
      "betweenss", "size", "iter", "ifault")
  )
  expect_equal(
    dim(Cluster(dat1, weights1, space_dim = 'space')$cluster),
    c(sdate = 50)
  )
  expect_equal(
    dim(Cluster(dat1)$cluster),
    c(sdate = 50, space = 2)
  )
  expect_equal(
    dim(Cluster(dat1, weights1, space_dim = 'space')$centers),
    c(K = 7, space = 2)
  )
  expect_equal(
    dim(Cluster(dat1, weights1, nclusters = 3, space_dim = 'space')$centers),
    c(K = 3, space = 2)
  )
  expect_equal(
    dim(Cluster(dat1, weights1, space_dim = 'space')$withinss),
    c(K = 7)
  )
  expect_equal(
    dim(Cluster(dat1, weights1, space_dim = 'space')$size),
    c(K = 7)
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$cluster)[1:5],
  c(7, 5, 7, 5, 4)
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$centers[1:5, 2]),
  c(-0.08553708, -1.55834000, 1.60550527, 1.79873789, -0.74919031),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$totss),
  83.08558,
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$withinss)[1:3],
  c(1.8509071, 0.6242121, 0.7800697),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$tot.withinss),
  11.71979,
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$betweenss),
  71.36579,
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$size),
  c(4, 5, 4, 6, 10, 8, 13)
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$iter),
  2
  )
  expect_equal(
  as.vector(Cluster(dat1, weights1, space_dim = 'space')$ifault),
  0
  )
})


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

  expect_equal(
    names(Cluster(dat2, weights2, space_dim = c('lat', 'lon'))),
    c("cluster", "centers", "totss", "withinss", "tot.withinss",
      "betweenss", "size", "iter", "ifault")
  )
  expect_equal(
    dim(Cluster(dat2, weights2, space_dim = c('lat', 'lon'))$cluster),
    c(sdate = 50, dat = 1)
  )
  expect_equal(
    dim(Cluster(dat2)$cluster),
    c(sdate = 50, dat = 1, lat = 2, lon = 3)
  )
  expect_equal(
    dim(Cluster(dat2, weights2, space_dim = c('lat', 'lon'))$centers),
    c(K = 6, lat = 2, lon = 3, dat = 1)
  )
  expect_equal(
    dim(Cluster(dat2, weights2, nclusters = 3, space_dim = c('lat', 'lon'))$centers),
    c(K = 3, lat = 2, lon = 3, dat = 1)
  )
  expect_equal(
    dim(Cluster(dat2, weights2, space_dim = c('lat', 'lon'))$withinss),
    c(K = 6, dat = 1)
  )
  expect_equal(
    dim(Cluster(dat2, weights2, space_dim = c('lat', 'lon'))$size),
    c(K = 6, dat = 1)
  )
  expect_equal(
  as.vector(Cluster(dat2, weights2, space_dim = c('lat', 'lon'))$cluster)[1:5],
  c(5, 1, 2, 1, 5)
  )

})

