context("s2dv::Ano test")

##############################################
  # dat1
  set.seed(1)
  dat1 <- array(rnorm(72), c(dat = 1, member = 3, sdate = 4, ftime = 6))
  set.seed(2)
  clim1 <- array(rnorm(12), c(dat = 1, member = 3, sdate = 4))

  #dat2
  set.seed(1)
  dat2 <- array(rnorm(72), c(dat = 1, sdate = 4, ftime = 6, member = 3))
  clim2 <- clim1


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

test_that("1. Input checks", {

  expect_error(
    Ano(c()),
    "Parameter 'data' cannot be NULL."
  )
  expect_error(
    Ano(c(NA, NA)),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
    Ano(array(1:10, dim = c(2, 5))),
    "Parameter 'data' must have dimension names."
  )
  expect_error(
    Ano(dat1, c()),
    "Parameter 'clim' cannot be NULL."
  )
  expect_error(
    Ano(dat1, c(NA, NA)),
    "Parameter 'clim' must be a numeric array."
  )
  expect_error(
    Ano(dat1, array(1:10, dim = c(2, 5))),
    "Parameter 'clim' must have dimension names."
  )
  expect_error(
    Ano(dat1, array(1:10, dim = c(dat = 1, member = 3, sdate = 4, a = 2))),
    "Parameter 'data' must have all the dimensions of parameter 'clim'."
  )
  expect_error(
    Ano(dat1, array(1:10, dim = c(dat = 1, member = 3, sdate = 2))),
    "Some dimensions of parameter 'clim' have different length from parameter 'data'."
  )
  expect_error(
    Ano(dat1, clim1, ncore = 3.5),
    "Parameter 'ncores' must be a positive integer."
  )

})

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

  expect_equal(
    dim(Ano(dat1, clim1)),
    c(dat = 1, member = 3, sdate = 4, ftime = 6)
  )
  expect_equal(
    mean(Ano(dat1, clim1)),
    -0.1434844,
    tolerance = 0.0001
  )
  expect_equal(
    min(Ano(dat1, clim1)),
    -2.971104,
    tolerance = 0.0001
  )
  expect_equal(
    Ano(dat1, clim1)[1, 2, , 4],
    c(-0.24416258, -0.08427184, 0.79636122, -0.05306879),
    tolerance = 0.0001
  )
  expect_equal(
    Ano(dat1, clim1, ncores = 1),
    Ano(dat1, clim1, ncores = 2)
  )

})

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

  expect_equal(
    dim(Ano(dat2, clim2)),
    dim(dat2)
  )
  expect_equal(
    mean(Ano(dat2, clim2)),
    -0.1434844,
    tolerance = 0.0001
  )
  expect_equal(
    min(Ano(dat2, clim2)),
    -3.789433,
    tolerance = 0.0001
  )
  expect_equal(
    Ano(dat2, clim2)[1, 2, , 3],
    c(0.74868744, -1.26178338, -1.17655491, -0.17166029, 0.05637202, 2.04019139),
    tolerance = 0.0001
  )
  expect_equal(
    Ano(dat2, clim2, ncores = 1),
    Ano(dat2, clim2, ncores = 2)
  )

})


