

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

test_that("1. Input checks", {
  # s2dv_cube
  expect_error(
    CST_TrainIndex(eval.method = 1, 2),
    paste0("unknown sampling method: 1")
  )
  expect_error(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 12),
    paste0("k needs to be a positive odd integer less than the amt.points")
  )
  expect_error(
    CST_TrainIndex(eval.method = 'retrospective', 10, NULL, 10),
    paste0("k needs to be a positive odd integer less than the amt.points")
  )
})

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

test_that("2. Output checks: leave-k-out", {
  expect_equal(
    is.list(CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)),
    TRUE
  )
  expect_equal(
    is.list(CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)[[1]]),
    TRUE
  )
  expect_equal(
    length(CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)),
    10
  )
  expect_equal(
    names(CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)[[1]]),
    c("eval.dexes", "train.dexes")
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)[[1]],
    list(eval.dexes = 1,
         train.dexes = 4:8)
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)[[2]],
    list(eval.dexes = 2, 
         train.dexes = 5:9)
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)[[3]],
    list(eval.dexes = 3,
         train.dexes = 6:10)
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)[[9]],
    list(eval.dexes = 9,
         train.dexes = 2:6)
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5)[[10]],
    list(eval.dexes = 10,
         train.dexes = 3:7)
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5, tail.out = FALSE)[[1]],
    list(eval.dexes = 1,
         train.dexes = 4:10)
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5, tail.out = FALSE)[[5]],
    list(eval.dexes = 5,
         train.dexes = c(1,2,8:10))
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 10, NULL, 5, tail.out = FALSE)[[10]],
    list(eval.dexes = 10,
         train.dexes = c(1:7))
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'leave-k-out', 20, NULL, 1)[[1]],
    CST_TrainIndex(eval.method = 'leave-k-out', 20, NULL, 1, tail.out = FALSE)[[1]]
  )
  
})

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

test_that("3. Output checks: retrospective", {
  expect_equal(
    length(CST_TrainIndex(eval.method = 'retrospective', 10, NULL, 3)),
    7
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'retrospective', 10, NULL, 3)[[1]],
    list(eval.dexes = 4, train.dexes = 1:3)
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'retrospective', 10, NULL, 3)[[2]],
    list(eval.dexes = 5, train.dexes = 1:4)
  )
  expect_equal(
    CST_TrainIndex(eval.method = 'retrospective', 10, NULL, 3)[[7]],
    list(eval.dexes = 10, train.dexes = 1:9)
  )
})


  
# Train.Eval.dexes <- function(k, amt.points, tail.out = TRUE) {
#     amt.seq <- 1:amt.points
#     strike = (k - 1) / 2
#     alt_amt.seq <- c(tail(amt.seq, strike), amt.seq, head(amt.seq, strike))
#     
#     dexes.lst <- lapply(seq(1, amt.points), function(x, kfold = k) {
#       if (tail.out) {
#         ind = alt_amt.seq[x:(x + (kfold - 1))] 
#         if(x == 2){print(ind)}
#         # return(list(eval.dexes = x,  train.dexes = amt.seq[!(amt.seq %in% ind)]))
#       } else{
#         ind = ((x - strike):(x + strike))
#         ind = ind[ind > 0 & ind <= amt.points]
#         #return(list(eval.dexes = x,  train.dexes = amt.seq[!(amt.seq %in% ind)]))
#       }
#       return(list(eval.dexes = x,  train.dexes = amt.seq[!(amt.seq %in% ind)]))
#       
#     })
#   }
  
  
  