context("Subset tests")

dat1 <- array(1:20, dim = c(dat = 1, lat = 2, lon = 10))

dat2 <- dat1
attributes(dat2)[['units']] <- 'K'

dat3 <- array(1:20, dim = c(1, 2, 10))
attributes(dat3)[['dimensions']] <- c('dat', 'lat', 'lon')

dat4 <- dat1
attributes(dat4)[['dimensions']] <- c('dataset', 'latitude', 'longitude')

#==============================================

test_that("1. Sanity checks", {

expect_error(
Subset(x = 1:4),
"Input array 'x' must be a numeric array."
)
expect_error(
Subset(x = array(1:20, dim = c(1, 2, 10)), along = 'a'),
"The input array 'x' doesn't have labels for the dimensions but the parameter 'along' contains dimension names."
)
expect_error(
Subset(x = dat1, along = T),
"All provided dimension indices in 'along' must be integers or character strings."
)
expect_error(
Subset(x = dat1, along = c('dat', 'dat')),
"The parameter 'along' must not contain repeated dimension names."
)
expect_error(
Subset(x = dat1, along = 'dataset'),
"Could not match all dimension names in 'indices' with dimension names in input array 'x'."
)
expect_error(
Subset(x = dat1, 'dat', 1, drop = 'yes'),
"Parameter 'drop' must be one of TRUE, FALSE, 'all', 'selected', 'non-selected', 'none'."
)

})

test_that("2. dat1", {
# drop: dat
expect_equal(
dim(Subset(dat1, 'dat', 1, drop = FALSE)),
dim(dat1)
)
expect_equal(
as.vector(Subset(dat1, 'dat', 1, drop = FALSE)),
as.vector(dat1)
)
expect_equal(
attributes(Subset(dat1, 'dat', 1, drop = FALSE)),
attributes(dat1)
)
expect_equal(
dim(Subset(dat1, 'dat', 1, drop = TRUE)),
dim(dat1)[-1]
)
expect_equal(
as.vector(Subset(dat1, 'dat', 1, drop = TRUE)),
as.vector(dat1)
)
expect_equal(
as.vector(Subset(dat1, 'dat', 1, drop = TRUE)),
as.vector(Subset(dat1, 1, 1, drop = TRUE))
)
expect_equal(
dim(Subset(dat1, 'dat', 1, drop = 'selected')),
dim(dat1)[-1]
)
expect_equal(
as.vector(Subset(dat1, 'dat', 1, drop = 'selected')),
as.vector(dat1)
)
# drop: lat
expect_equal(
dim(Subset(dat1, 'lat', 1, drop = FALSE)),
c(dat = 1, lat = 1, lon = 10)
)
expect_equal(
as.vector(Subset(dat1, 'lat', 1, drop = FALSE)),
as.vector(dat1[, 1, ])
)
expect_equal(
dim(Subset(dat1, 'lat', 1, drop = TRUE)),
dim(dat1)[3]
)
expect_equal(
as.vector(Subset(dat1, 'lat', 1, drop = TRUE)),
as.vector(dat1[, 1, ])
)
expect_equal(
Subset(dat1, 'lat', 1, drop = TRUE),
Subset(dat1, 2, 1, drop = TRUE)
)
expect_equal(
dim(Subset(dat1, 'lat', 1, drop = 'selected')),
dim(dat1)[c(1, 3)]
)
expect_equal(
as.vector(Subset(dat1, 'lat', 1, drop = 'selected')),
as.vector(dat1[, 1, ])
)
# drop: lat, lon
expect_equal(
dim(Subset(dat1, c('lat', 'lon'), list(1, 2), drop = FALSE)),
c(dat = 1, lat = 1, lon = 1)
)
expect_equal(
as.vector(Subset(dat1, c('lat', 'lon'), list(1, 2), drop = FALSE)),
3
)
expect_equal(
dim(Subset(dat1, c('lat', 'lon'), list(1, 2), drop = TRUE)),
1
)
expect_equal(
as.vector(Subset(dat1, c('lat', 'lon'), list(1, 2), drop = TRUE)),
3
)
expect_equal(
Subset(dat1, c('lat', 'lon'), list(1, 2), drop = TRUE),
Subset(dat1, c(2, 3), list(1, 2), drop = TRUE)
)
expect_equal(
dim(Subset(dat1, c('lat', 'lon'), list(1, 2), drop = 'selected')),
c(dat = 1)
)
expect_equal(
as.vector(Subset(dat1, c('lat', 'lon'), list(1, 2), drop = 'selected')),
3
)
# drop: dat, lat, lon
expect_equal(
dim(Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = FALSE)),
c(dat = 1, lat = 1, lon = 1)
)
expect_equal(
as.vector(Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = FALSE)),
3
)
expect_equal(
Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = FALSE),
Subset(dat1, c(1:3), list(1, 1, 2), drop = FALSE)
)
expect_equal(
dim(Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = TRUE)),
1
)
expect_equal(
as.vector(Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = TRUE)),
3
)
expect_equal(
Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = TRUE),
Subset(dat1, c(1:3), list(1, 1, 2), drop = TRUE)
)
expect_equal(
class(Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = 'selected')),
'array'
)
expect_equal(
dim(Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = 'selected')),
1
)
expect_equal(
as.vector(Subset(dat1, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = 'selected')),
3
)

})

test_that("3. dat2", {
# Same as dat1 but with attributes

# drop: lat
expect_equal(
dim(Subset(dat2, 'lat', 1, drop = TRUE)),
dim(dat1)[3]
)
expect_equal(
as.vector(Subset(dat2, 'lat', 1, drop = TRUE)),
as.vector(dat1[, 1, ])
)
expect_equal(
attributes(Subset(dat2, 'lat', 1, drop = TRUE)),
list(dim = c(lon = 10), units = 'K')
)

# drop: dat, lat, lon
expect_equal(
dim(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = FALSE)),
c(dat = 1, lat = 1, lon = 1)
)
expect_equal(
as.vector(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = FALSE)),
3
)
expect_equal(
attributes(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = FALSE)),
list(dim = c(dat = 1, lat = 1, lon = 1), units = 'K')
)
expect_equal(
dim(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = TRUE)),
1
)
expect_equal(
as.vector(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = TRUE)),
3
)
expect_equal(
attributes(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = TRUE)),
list(dim = 1, units = 'K')
)
expect_equal(
class(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = 'selected')),
'array'
)
expect_equal(
dim(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = 'selected')),
1
)
expect_equal(
as.vector(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = 'selected')),
3
)
expect_equal(
attributes(Subset(dat2, c('dat', 'lat', 'lon'), list(1, 1, 2), drop = 'selected')),
list(dim = 1, units = 'K')
)

})

test_that("4. dat3", {
# drop: dat
expect_equal(
Subset(dat1, 'dat', 1, drop = FALSE),
Subset(dat3, 'dat', 1, drop = FALSE),
check.attributes = F
)
expect_equal(
attributes(Subset(dat3, 'dat', 1, drop = FALSE)),
list(dim = c(dat = 1, lat = 2, lon = 10), dimensions = c('dat', 'lat', 'lon'))
)

# drop: lat, lon
expect_equal(
Subset(dat1, c('lat', 'lon'), list(1, 2), drop = TRUE),
Subset(dat3, c('lat', 'lon'), list(1, 2), drop = TRUE),
check.attributes = F
)
expect_equal(
attributes(Subset(dat3, c('lat', 'lon'), list(1, 2), drop = TRUE)),
list(dim = 1, dimensions = c('dat', 'lat', 'lon'))
)

})

test_that("5. dat4", {

# drop: lat
expect_equal(
Subset(dat4, 'latitude', 1, drop = FALSE),
Subset(dat1, 'lat', 1, drop = FALSE),
check.attributes = F
)
expect_equal(
attributes(Subset(dat4, 'latitude', 1, drop = FALSE)),
list(dim = c(dataset = 1, latitude = 1, longitude = 10), dimensions = c('dataset', 'latitude', 'longitude'))
)
# drop: lat, lon
expect_equal(
Subset(dat4, c('latitude', 'longitude'), list(1, 2), drop = TRUE),
Subset(dat1, c('lat', 'lon'), list(1, 2), drop = TRUE),
check.attributes = F
)
expect_equal(
attributes(Subset(dat4, c('latitude', 'longitude'), list(1, 2), drop = TRUE)),
list(dim = 1, dimensions = c('dataset', 'latitude', 'longitude'))
)

})
