Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
context("s2dv::Cluster tests")
##############################################
# dat1
set.seed(1)
dat1 <- array(rnorm(100),
dim = c(sdate = 50, space = 2))
weights1 <- array(c(0.9, 1.1), dim = c(space = 2))
# dat2
set.seed(2)
dat2 <- array(rnorm(300),
dim = c(sdate = 50, lat = 2, lon = 3))
weights2 <- array(c(0.9, 1.1), dim = c(lat = 2, lon = 3))
##############################################
test_that("1. Input checks", {
# data
expect_error(
Cluster(c()),
"Parameter 'data' cannot be NULL."
)
expect_error(
Cluster(c(NA, NA)),
"Parameter 'data' must be a numeric array."
)
expect_error(
Cluster(array(1:10, dim = c(2, 5))),
"Parameter 'data' must have dimension names."
)
# weights
expect_error(
Cluster(dat1, weights = 'lat'),
"Parameter 'weights' must be a numeric array."
)
expect_error(
Cluster(dat1, weights = 2),
"Parameter 'weights' must have dimension names."
)
expect_error(
Cluster(dat1, weights = array(2, dim = c(lat = 2))),
"Parameter 'weights' must have dimensions that can be found in 'data' dimensions."
)
# time_dim
expect_error(
Cluster(dat1, weights1, time_dim = 'a'),
"Parameter 'time_dim' is not found in 'data' dimension."
)
expect_error(
Cluster(array(c(1:25), dim = c(dat = 1, time = 5, space = 2)), weights1),
"Parameter 'time_dim' is not found in 'data' dimension."
)
expect_error(
Cluster(dat1, weights1, time_dim = 2),
"Parameter 'time_dim' must be a character string."
)
expect_error(
Cluster(dat1, weights1, time_dim = c('a', 'sdate')),
"Parameter 'time_dim' must be a character string."
)
# nclusters
expect_error(
Cluster(dat1, weights1, ncluster = 1),
"Parameter 'nclusters' must be an integer bigger than 1."
)
# index
expect_error(
Cluster(dat1, weights1, index = 1),
"Parameter 'index' should be a character strings accepted as 'index' by the function NbClust::NbClust."
)
# ncores
expect_error(
Cluster(dat1, weights1, ncore = 0),
"Parameter 'ncores' must be a positive integer."
)
})
##############################################
test_that("2. Output checks: dat1", {
# The output is random. Only check dimensions.
expect_equal(
length(Cluster(dat1, weights1)$cluster),
50
)
expect_equal(
length(Cluster(dat1)$cluster),
100
)
expect_equal(
dim(Cluster(dat1, weights1)$centers),
c(8, 2)
)
expect_equal(
dim(Cluster(dat1, weights1, nclusters = 3)$centers),
c(3, 2)
)
})
##############################################
test_that("3. Output checks: dat2", {
expect_equal(
length(Cluster(dat2, weights2)$cluster),
50
)
expect_equal(
length(Cluster(dat2)$cluster),
300
)
expect_equal(
length(Cluster(dat2, space_dim = c('lon', 'lat'))$cluster),
50
)