almost finished debug
[talweg.git] / pkg / tests / testthat / test.computeFilaments.R
CommitLineData
a65907cc 1context("Check that computeFilaments behaves as expected")
1e20780e 2
af3b84f4
BA
3#shorthand: map 1->1, 2->2, 3->3, 4->1, ..., 149->2, 150->3
4I = function(i)
5 (i-1) %% 3 + 1
6
44a9990b 7#MOCK data; NOTE: could be in inst/testdata as well
af3b84f4 8getDataTest = function(n)
a65907cc 9{
af3b84f4 10 data = Data$new()
a65907cc
BA
11 x = seq(0,10,0.1)
12 L = length(x)
13 s1 = cos(x)
14 s2 = sin(x)
15 s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] )
16 #sum((s1-s2)^2) == 97.59381
17 #sum((s1-s3)^2) == 57.03051
18 #sum((s2-s3)^2) == 40.5633
19 s = list( s1, s2, s3 )
a65907cc
BA
20 series = list()
21 for (i in seq_len(n))
1e20780e 22 {
af3b84f4
BA
23 serie = s[[I(i)]] + rnorm(L,sd=0.01)
24 level = mean(serie)
25 serie = serie - level
a65907cc 26 # 10 series with NAs for index 2
af3b84f4 27 if (I(i) == 2 && i >= 60 && i<= 90)
a65907cc 28 serie[sample(seq_len(L),1)] = NA
af3b84f4 29 data$append(c(), serie, level, c(), c()) #no need for more
6d97bfec 30 }
af3b84f4 31 data
6d97bfec
BA
32}
33
af3b84f4 34test_that("output is as expected on simulated series",
6d97bfec 35{
af3b84f4 36 data = getDataTest(150)
a65907cc 37
44a9990b
BA
38 # index 143 : serie type 2
39 f = computeFilaments(data, 143, limit=60, plot=FALSE)
af3b84f4
BA
40 # Expected output: 23 series of type 3 (closer), then 50-3-10 series of type 2, then 143
41 expect_identical(length(f$indices), as.integer(61)) #61 because result also contain today
44a9990b 42 expect_identical(length(f$colors), as.integer(61))
af3b84f4 43 expect_true(all(I(f$indices) >= 2))
44a9990b 44 for (i in 1:23)
6d97bfec 45 {
af3b84f4 46 expect_equal(I(f$indices[i]), 3)
44a9990b 47 expect_match(f$colors[i], f$colors[1])
6d97bfec 48 }
44a9990b 49 for (i in 24:60)
6d97bfec 50 {
af3b84f4 51 expect_equal(I(f$indices[i]), 2)
44a9990b 52 expect_match(f$colors[i], f$colors[24])
6d97bfec 53 }
44a9990b
BA
54 expect_equal(f$indices[61], 143)
55 expect_match(f$colors[61], "#FF0000") #special color: current day in red
56 expect_match(f$colors[1], "#E*")
57 expect_match(f$colors[24], "#1*")
6d97bfec 58
af3b84f4
BA
59 # index 142 : serie type 1
60 f = computeFilaments(data, 142, limit=50, plot=FALSE)
61 # Expected output: 13 series of type 3 (closer), then 50-10-3 series of type 1, then 142
44a9990b 62 # NOTE: -10 because only past days with no-NAs tomorrow => exclude type 1 in [60,90[
af3b84f4
BA
63 expect_identical(length(f$indices), as.integer(51))
64 expect_identical(length(f$colors), as.integer(51))
65 expect_true(all(I(f$indices) != 2))
66 for (i in 1:13)
6d97bfec 67 {
af3b84f4 68 expect_equal(I(f$indices[i]), 3)
6d97bfec
BA
69 expect_match(f$colors[i], f$colors[1])
70 }
af3b84f4 71 for (i in 14:50)
6d97bfec 72 {
af3b84f4
BA
73 expect_equal(I(f$indices[i]), 1)
74 expect_match(f$colors[i], f$colors[14])
6d97bfec 75 }
af3b84f4
BA
76 expect_equal(f$indices[51], 142)
77 expect_match(f$colors[51], "#FF0000")
44a9990b 78 expect_match(f$colors[1], "#E*")
af3b84f4 79 expect_match(f$colors[14], "#1*")
1e20780e 80})