context("Generic tests")
test_that("Sanity checks", {

  expect_error(
    DailyAno(data = NULL),
    "Parameter 'data' cannot be NULL."
  )

  data <- 1:10
  dim(data) <- c(2,5)
  expect_error(  
    DailyAno(data),
    paste0("No dates provided in parameter 'dates' nor as attribute of ",
           "parameter 'data'. At least one of the parameters 'jdays' or ",
           "'dates' must be supplied.")
  )

  x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
  expect_error(
    DailyAno(data = 1:8, dates = c(x,x)),
    paste0("Dates provided in parameter 'dates' must be of class 'POSIXct' ",
           "or convertable to 'POSIXct'.")
  )

  dates <- as.PCICt(paste0(rep(1, 50), "-", rep(c(1, 2), 25), "-",
                    sort(rep(1901 : 1925, 2))), cal = "standard",
                    format = "%d-%m-%Y")
  expect_error(
    DailyAno(data = 1:50, dates =dates, storefreq = 'seasonal'),
    "Parameter 'storefreq' should be either 'daily' or 'monthly'."
  )

  data <- 1 : 100
  dim(data) <- c(day = 50, model = 2)
  dates <- as.PCICt(paste0(rep(1, 50), "-", rep(c(1, 2), 25), "-",
                    sort(rep(1901 : 1925, 2))), cal = "standard",
                    format = "%d-%m-%Y")
  expect_error(
    DailyAno(data = data, dates = dates),
    paste0("The time dimension is not found in data. Please specify with ",
           "parameter 'timedim'.")
  )

  expect_error(
    DailyAno(data = data, dates = dates, timedim = 1, na.rm = 1),
    "Parameter 'na.rm' must be logical."
  )

  expect_warning(
    DailyAno(data = 1:10, jdays = rep(1:2,5)),
    paste0("Parameter 'data' is a vector. Used as a time series and parameter ",
           "'timedim' is set to be 1.")
  )

  expect_warning(
    DailyAno(data = data, dates = dates, timedim = 1, na.rm = c(TRUE, TRUE)),
    "Parameter 'na.rm' has length > 1 and only the first element will be used."
  )

  tmp <- as.array(c(-4, -4, -2, -2, 0, 0, 2, 2, 4, 4))
  names(dim(tmp)) <- c('time')
  expect_equal(
    DailyAno(data = 1:10, jdays = rep(1:2,5)), 
    tmp
  )

  data <- 1 : 100
  dim(data) <- c(time = length(data))
  jj <- rep(1 : 2, 50)
  output <- rep(-49 : 49, each = 2)
  output <- output[which(output %% 2 != 0)]
  dim(output) <- c(time = length(output))
  expect_equal(
    DailyAno(data = data, jdays = jj), output
  )


  data <- rnorm(730 *3 * 2 * 1, 2, 1)
  dim(data) <- c(time = 730, memb = 3, lon = 2, lat = 1)
  time <- seq.Date(as.Date("1990-01-01", format = "%Y-%d-%m"),
          as.Date("1991-31-12", format = "%Y-%d-%m"), 1)
  expect_equal(
    dim(DailyAno(data, dates = time, timedim = 1, na.rm = TRUE, ncores = NULL)),
    dim(data)
  )
 
  data <- rnorm(730 *3 * 2 * 1, 2, 1)
  dim(data) <- c(time = 730, memb = 3, lon = 2, lat = 1)
  time <- seq.Date(as.Date("1990-01-01", format = "%Y-%d-%m"),
          as.Date("1991-31-12", format = "%Y-%d-%m"), 1)
  data[1,1,1,1] <- NA
  expect_equal(
    which(is.na(DailyAno(data, dates = time, timedim = 1, na.rm = TRUE,
          ncores = NULL))),
    c(1)
  )
  expect_equal(
    which(is.na(DailyAno(data, dates = time, timedim = 1, na.rm = FALSE,
          ncores = NULL))),
    c(1, 366)
  )


  data <- rnorm(800 * 3 * 6 * 2, 25, 8)
  dim(data) <- c(dates = 800, lat = 3, lon = 6, model = 2)
  dates <- seq(ISOdate(2000,1,1), ISOdate(2002,03,10), "days")
  metadata <- list(time = list(standard_name = 'time', long_name = 'time',
                               calendar = 'standard',
                               units = 'days since 1970-01-01 00:00:00', 
                               prec = 'double',
                               dim = list(list(name ='time', unlim = FALSE))))
  attr(dates, 'variables') <- metadata
  attr(data, 'Variables')$dat1$time <- dates
  attr(data, 'Variables')$dat2$time <- dates
  attr(data, 'Variables')$common[[2]]$dim[[3]]$len = length(dates)
  attr(data, 'Variables')$common[[2]]$dim[[3]]$vals <- dates
  expect_equal(
    dim(DailyAno(data)),
    c(dates = 800, lat = 3, lon = 6, model = 2)
  )

  data <- 1 : 100
  dim(data) <- c(time = length(data))
  output <- rep(-49 : 49, each = 2)
  output <- output[which(output %% 2 != 0)]
  dim(output) <- c(time = length(output))
  dates <- as.PCICt(paste0(rep(1, 100), "-", rep(c(1, 2), 50), "-",
                    sort(rep(1901 : 1950, 2))), cal = "standard", 
                    format = "%d-%m-%Y")
  expect_equal(
    DailyAno(data = data, dates = dates),
    output
  )

  data <- 1 : 24
  dim(data) <- c(time = 24, lat = 1, lon = 1)
  dates <- seq(ISOdate(2000,1,1), ISOdate(2001,12,31), "months")
  metadata <- list(time = list(standard_name = 'time', long_name = 'time',
                               calendar = 'standard',
                               units = 'days since 1970-01-01 00:00:00',
                               prec = 'double',
                               dim = list(list(name ='time', unlim = FALSE))))
  attr(dates, 'variables') <- metadata
  attr(data, 'Variables')$dat1$time <- dates
  attr(data, 'Variables')$dat2$time <- dates
  attr(data, 'Variables')$common[[2]]$dim[[3]]$len = length(dates)
  attr(data, 'Variables')$common[[2]]$dim[[3]]$vals <- dates
  tmp <- sort(rep(c(6,-6),12))
  dim(tmp) <- c(time = 24, lat = 1, lon = 1)
  expect_equal(
    DailyAno(data, storefreq = 'monthly'),
    tmp
  )

  data <- 1:(1 * 1 * 731 * 1)
  dim(data) <- c(lon = 1, lat = 1, time = 365 + 366, model = 1)
  time <- seq(ISOdate(1903,1,1), ISOdate(1904,12,31), "days")
  metadata <- list(time = list(standard_name = 'time', long_name = 'time',
                               calendar = 'standard',
                               units = 'days since 1970-01-01 00:00:00',
                               prec = 'double',
                               dim = list(list(name ='time', unlim = FALSE))))
  attr(time, 'variables') <- metadata
  attr(data, 'Variables')$dat1$time <- time
  attr(data, 'Variables')$dat2$time <- time
  attr(data, 'Variables')$common[[2]]$dim[[3]]$len = length(time)
  attr(data, 'Variables')$common[[2]]$dim[[3]]$vals <- time
  aa <- rep(-182.5,59)
  bb <- rep(-183,365-59)
  cc <- rep(182.5,59)
  dd <- 0
  ee <- rep(183,366-60)
  tmp <- c(aa, bb, cc, dd, ee)
  dim(tmp) <- c(time = 731, lon = 1, lat = 1, model = 1)
  expect_equal(
    DailyAno(data),
    tmp
  )


})
