context("s2dv::CRPS tests")
                                                         
##############################################           

# dat1                                                   
set.seed(1)                                              
exp1 <- array(rnorm(60), dim = c(member = 3, sdate = 10, lat = 2))
set.seed(2)                                                        
obs1 <- array(rnorm(20), dim = c(sdate = 10, lat = 2))             
                                                                   
# dat2                                                             
set.seed(1)                                                        
exp2 <- array(rnorm(20), dim = c(member = 2, sdate = 10))          
set.seed(2)                                                        
obs2 <- array(rnorm(10), dim = c(sdate = 10))                      
                                                                   
# dat3                                                             
set.seed(1)                                                        
exp3 <- array(rnorm(40), dim = c(member = 2, sdate = 10, dataset = 2))
set.seed(2)                                                        
obs3 <- array(rnorm(30), dim = c(member = 1, sdate = 10, dataset = 3))
                                                                   
##############################################                     
test_that("1. Input checks", {                                     
  # exp and obs (1)                                                
  expect_error(                                                    
  CRPS(c()),                                                       
  "Parameter 'exp' must be a numeric array."                       
  )                                                                
  expect_error(                                                    
  CRPS(exp1, c()),                                                 
  "Parameter 'obs' must be a numeric array."                       
  )                                                                
                                                                   
  # time_dim                                                       
  expect_error(                                                    
  CRPS(exp1, obs1, time_dim = 1),                                  
  "Parameter 'time_dim' must be a character string."               
  )                                                                
  expect_error(                                                    
  CRPS(exp1, obs1, time_dim = 'time'),                             
  "Parameter 'time_dim' is not found in 'exp' or 'obs' dimension." 
  )                                                                
  # memb_dim                                                       
  expect_error(                                                                                               
  CRPS(exp1, obs1, memb_dim = TRUE),                                                                          
  "Parameter 'memb_dim' must be a character string."                                                          
  )                                                                                                           
  expect_error(                                                                                               
  CRPS(exp1, obs1, memb_dim = 'memb'),                                                                        
  "Parameter 'memb_dim' is not found in 'exp' dimension."                                                     
  )
  ## exp and obs (2)
  expect_error(CRPS(exp1, array(1:40, dim = c(sdate = 10, lat = 2, member = 2))),
  "Not implemented for observations with members ('obs' can have 'memb_dim', but it should be of length = 1).", fixed = TRUE
  )
  expect_error(                                                                                               
  CRPS(exp1, array(1:9, dim = c(sdate = 9))),                                                                 
  "Parameter 'exp' and 'obs' must have same length of all dimensions except 'memb_dim' and 'dat_dim'."        
  )                                                                                                           
  # Fair                                                                                                      
  expect_error(                                                                                               
  CRPS(exp1, obs1, Fair = 1),                                                                                 
  "Parameter 'Fair' must be either TRUE or FALSE."                                                            
  )                                                                                                           
  # ncores                                                                                            
  expect_error(
  CRPS(exp2, obs2, ncores = 1.5),
  "Parameter 'ncores' must be either NULL or a positive integer."
  )

})

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

  expect_equal(
  dim(CRPS(exp1, obs1)),
  c(lat = 2)
  )
  expect_equal(
  as.vector(CRPS(exp1, obs1)),
  c(0.5947612, 0.7511546),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(CRPS(exp1, obs1, Fair = TRUE)),
  c(0.4215127, 0.5891242),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(CRPS(exp1, obs1, Fair = FALSE)),
  c(0.5947612, 0.7511546),
  tolerance = 0.0001
  )

})

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

  expect_equal(
  dim(CRPS(exp2, obs2)),
  NULL
  )
  expect_equal(
  CRPS(exp2, obs2),
  0.9350226,
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(CRPS(exp2, obs2, Fair = TRUE)),
  as.vector(CRPS(array(exp2, dim = c(dim(exp2), dataset = 1)), array(obs2, dim = c(dim(obs2), dataset = 1)), dat_dim = 'dataset', Fair = TRUE)),
  tolerance = 0.0001
  )
})

  

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

  expect_equal(
  dim(CRPS(exp3, obs3, dat_dim = 'dataset')),
  c(nexp = 2, nobs = 3)
  )
  expect_equal(
  as.vector(CRPS(exp3, obs3, dat_dim = 'dataset', Fair = FALSE)),
  c(0.9350226, 0.8354833, 1.0047853, 0.9681745, 1.2192761, 1.0171790),
  tolerance = 0.0001
  )
  expect_equal(
  as.vector(CRPS(exp3, obs3, dat_dim = 'dataset', Fair = TRUE)),
  c(0.6701312, 0.6198684, 0.7398939, 0.7525596, 0.9543847, 0.8015641),
  tolerance = 0.0001
  )

})
