context("ShiftLon tests")

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

# dat1
dat1 <- array(1:360, dim = c(lon = 360, lat = 181))
lon1 <- 0:359
lat1 <- -90:90

# dat2
dat2 <- array(1:360, dim = c(lon = 360, lat = 181))
lon2 <- -180:179
lat2 <- -90:90

# dat3
dat3 <- array(1:512, dim = c(lon = 512, lat = 216, time = 2))
lon3 <- seq(0, 360, length.out = 513)[1:512]
lat3 <- seq(-90, 90, length.out = 216)

##############################################
test_that("1. Input checks", {
  # data
  expect_error(
    ShiftLon(c()),
    "Parameter 'data' cannot be NULL."
  )
  expect_error(
    ShiftLon(1:10),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
   ShiftLon(array(1:10)),
   "Parameter 'data' must have dimension names."
  )
  # lon_dim
  expect_error(
    ShiftLon(dat1, lon1, lon_dim = 1),
    "Parameter 'lon_dim' must be a character string."
  )
  expect_error(
    ShiftLon(dat1, lon1, lon_dim = 'a'),
    "Parameter 'lon_dim' is not found in 'data' dimensions."  
  )
  # lon
  expect_error(
    ShiftLon(dat1, lon = 'a'),
    "Parameter 'lon' must be numeric."
  )
  expect_error(
    ShiftLon(dat1, lon = 1:100),
    "The length of 'lon' must be the same as the length of 'lon_dim' in 'data'."
  )
  expect_error(
    ShiftLon(dat1, lon = 359:0),
    "Parameter 'lon' must be monotonic increasing."
  )
  # westB
  expect_error(
    ShiftLon(dat1, lon = lon1, westB = 'a'),
    "Parameter 'westB' must be numeric."
  )
  expect_warning(
    ShiftLon(dat1, lon = lon1, westB = 1:10),
    "Parameter 'westB' should be a number. Use the first element only."
  )
  # ncores
  expect_error(
    ShiftLon(dat1, lon = lon1, westB = 10, ncores = 'a'),
    "Parameter 'ncores' must be a positive integer."
  )
  
})


##############################################
test_that("2. dat1", {
  
  westB <- -180
  expect_equal(
    dim(ShiftLon(dat1, lon = lon1, westB = westB)$data),
    dim(dat1)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$lon,
    -180:179
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$data[,1],
    c(181:360, 1:180)
  )
  #-----------------
  westB <- 90
  expect_equal(
    dim(ShiftLon(dat1, lon = lon1, westB = westB)$data),
    dim(dat1)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$lon,
    90:(90 + 359)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$data[,1],
    c(91:360, 1:90)
  )
  #-----------------
  westB <- 300
  expect_equal(
    dim(ShiftLon(dat1, lon = lon1, westB = westB)$data),
    dim(dat1)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$lon,
    300:(300 + 359)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$data[,1],
    c(301:360, 1:300)
  )
  #-----------------
  westB <- -270
  expect_equal(
    dim(ShiftLon(dat1, lon = lon1, westB = westB)$data),
    dim(dat1)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$lon,
    -270:(-270+359)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$data[,1],
    c(91:360, 1:90)
  )
  #-----------------
  westB <- -10
  expect_equal(
    dim(ShiftLon(dat1, lon = lon1, westB = westB)$data),
    dim(dat1)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$lon,
    -10:(-10 + 359)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$data[,1],
    c(351:360, 1:350)
  )
  #-----------------
  westB <- -0.5
  expect_equal(
    dim(ShiftLon(dat1, lon = lon1, westB = westB)$data),
    dim(dat1)
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$lon,
    -1:358
  )
  expect_equal(
    ShiftLon(dat1, lon = lon1, westB = westB)$data[,1],
    c(360, 1:359)
  )
  
})


##############################################
test_that("3. dat2", {
  
  westB <- 0
  expect_equal(
    dim(ShiftLon(dat2, lon = lon2, westB = westB)$data),
    dim(dat2)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$lon,
    0:359
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$data[,1],
    c(181:360, 1:180)
  )
  #-----------------
  westB <- 90
  expect_equal(
    dim(ShiftLon(dat2, lon = lon2, westB = westB)$data),
    dim(dat2)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$lon,
    90:(90 + 359)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$data[,1],
    c(271:360, 1:270)
  )
  #-----------------
  westB <- 300
  expect_equal(
    dim(ShiftLon(dat2, lon = lon2, westB = westB)$data),
    dim(dat2)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$lon,
    300:(300 + 359)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$data[,1],
    c(121:360, 1:120)
  )
  #-----------------
  westB <- -270
  expect_equal(
    dim(ShiftLon(dat2, lon = lon2, westB = westB)$data),
    dim(dat2)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$lon,
    -270:(-270+359)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$data[,1],
    c(271:360, 1:270)
  )
  #-----------------
  westB <- -10
  expect_equal(
    dim(ShiftLon(dat2, lon = lon2, westB = westB)$data),
    dim(dat2)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$lon,
    -10:(-10 + 359)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$data[,1],
    c(171:360, 1:170)
  )
  #-----------------
  westB <- -0.5
  expect_equal(
    dim(ShiftLon(dat2, lon = lon2, westB = westB)$data),
    dim(dat2)
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$lon,
    -1:358
  )
  expect_equal(
    ShiftLon(dat2, lon = lon2, westB = westB)$data[,1],
    c(180:360, 1:179)
  )
  
})

##############################################
test_that("4. dat3", {
  
  westB <- -180
  expect_equal(
    dim(ShiftLon(dat3, lon = lon3, westB = westB)$data),
    dim(dat3)
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$lon,
    seq(westB, westB + 360, length.out = 513)[-513]
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$data[,1, 2],
    c(257:512, 1:256)
  )
  #-----------------
  westB <- 90
  expect_equal(
    dim(ShiftLon(dat3, lon = lon3, westB = westB)$data),
    dim(dat3)
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$lon,
    seq(westB, westB + 360, length.out = 513)[-513]
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$data[,1, 2],
    c(129:512, 1:128)
  )
  #-----------------
  westB <- 300
  expect_equal(
    dim(ShiftLon(dat3, lon = lon3, westB = westB)$data),
    dim(dat3)
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$lon,
    seq(300.2344, 300.2344 + 360, length.out = 513)[-513],
    tolerance = 0.001
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$data[,1, 2],
    c(428:512, 1:427)
  )
  #-----------------
  westB <- -270
  expect_equal(
    dim(ShiftLon(dat3, lon = lon3, westB = westB)$data),
    dim(dat3)
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$lon,
    seq(westB, westB + 360, length.out = 513)[-513]
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$data[,1, 2],
    c(129:512, 1:128)
  )
  #-----------------
  westB <- -10
  expect_equal(
    dim(ShiftLon(dat3, lon = lon3, westB = westB)$data),
    dim(dat3)
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$lon,
    seq(-9.843750, -9.843750 + 360, length.out = 513)[-513]
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$data[,1, 2],
    c(499:512, 1:498)
  )
  #-----------------
  westB <- -0.5
  expect_equal(
    dim(ShiftLon(dat3, lon = lon3, westB = westB)$data),
    dim(dat3)
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$lon,
    seq(-0.703125, -0.703125 + 360, length.out = 513)[-513]
  )
  expect_equal(
    ShiftLon(dat3, lon = lon3, westB = westB)$data[,1, 2],
    c(512, 1:511)
  )
  
})
