'update'
[talweg.git] / pkg / tests / testthat / test-DateIntegerConv.R
CommitLineData
3ddf1c12 1context("Date <--> integer conversions")
1e20780e 2
44a9990b
BA
3ts_data = system.file("testdata","ts_test.csv",package="talweg")
4exo_data = system.file("testdata","exo_test.csv",package="talweg")
d2ab47a7
BA
5data0 <<- getData(ts_data, exo_data, date_format="%Y-%m-%d %H:%M", limit=Inf)
6data7 <<- getData(ts_data, exo_data, date_format="%Y-%m-%d %H:%M", limit=Inf)
44a9990b 7
3ddf1c12 8test_that("dateIndexToInteger works as expected",
44a9990b
BA
9{
10 expect_identical( dateIndexToInteger("2007-01-01",data0), 1 )
11 expect_identical( dateIndexToInteger("2007-01-02",data0), 2 )
12 expect_identical( dateIndexToInteger("2007-02-01",data0), 32 )
13 expect_identical( dateIndexToInteger("2007-03-01",data0), 60 )
14 expect_identical( dateIndexToInteger("2007-05-31",data0), 151 )
44a9990b 15
44a9990b 16 expect_identical( dateIndexToInteger("2007-01-01",data7), 1 )
af3b84f4
BA
17 expect_identical( dateIndexToInteger("2007-01-02",data7), 2 )
18 expect_identical( dateIndexToInteger("2007-02-01",data7), 32 )
19 expect_identical( dateIndexToInteger("2007-03-01",data7), 60 )
20 expect_identical( dateIndexToInteger("2007-05-31",data7), 151 )
44a9990b
BA
21})
22
3ddf1c12 23test_that("integerIndexToDate works as expected",
a65907cc 24{
44a9990b
BA
25 expect_identical( integerIndexToDate( 1,data0), as.Date("2007-01-01") )
26 expect_identical( integerIndexToDate( 2,data0), as.Date("2007-01-02") )
27 expect_identical( integerIndexToDate( 32,data0), as.Date("2007-02-01") )
28 expect_identical( integerIndexToDate( 60,data0), as.Date("2007-03-01") )
29 expect_identical( integerIndexToDate(151,data0), as.Date("2007-05-31") )
6d97bfec 30
44a9990b 31 expect_identical( integerIndexToDate( 1,data7), as.Date("2007-01-01") )
af3b84f4
BA
32 expect_identical( integerIndexToDate( 2,data7), as.Date("2007-01-02") )
33 expect_identical( integerIndexToDate( 32,data7), as.Date("2007-02-01") )
34 expect_identical( integerIndexToDate( 60,data7), as.Date("2007-03-01") )
35 expect_identical( integerIndexToDate(151,data7), as.Date("2007-05-31") )
44a9990b
BA
36})
37
38test_that("dateIndexToInteger(integerIndexToDate) == Id",
39{
44a9990b
BA
40 expect_identical( dateIndexToInteger(integerIndexToDate( 1,data0),data0), 1 )
41 expect_identical( dateIndexToInteger(integerIndexToDate( 1,data7),data7), 1 )
42 expect_identical( dateIndexToInteger(integerIndexToDate( 2,data0),data0), 2 )
af3b84f4 43 expect_identical( dateIndexToInteger(integerIndexToDate( 2,data7),data7), 2 )
44a9990b
BA
44 expect_identical( dateIndexToInteger(integerIndexToDate( 32,data0),data0), 32 )
45 expect_identical( dateIndexToInteger(integerIndexToDate( 32,data7),data7), 32 )
46 expect_identical( dateIndexToInteger(integerIndexToDate( 60,data0),data0), 60 )
47 expect_identical( dateIndexToInteger(integerIndexToDate( 60,data7),data7), 60 )
48 expect_identical( dateIndexToInteger(integerIndexToDate(151,data0),data0), 151 )
49 expect_identical( dateIndexToInteger(integerIndexToDate(151,data7),data7), 151 )
50})
51
52test_that("integerIndexToDate(dateIndexToInteger) == Id",
53{
3ddf1c12
BA
54 expect_identical(integerIndexToDate(dateIndexToInteger("2007-01-01",data0),data0),
55 as.Date("2007-01-01") )
56 expect_identical(integerIndexToDate(dateIndexToInteger("2007-01-01",data7),data7),
57 as.Date("2007-01-01") )
58 expect_identical(integerIndexToDate(dateIndexToInteger("2007-01-02",data0),data0),
59 as.Date("2007-01-02") )
60 expect_identical(integerIndexToDate(dateIndexToInteger("2007-01-02",data7),data7),
61 as.Date("2007-01-02") )
62 expect_identical(integerIndexToDate(dateIndexToInteger("2007-02-01",data0),data0),
63 as.Date("2007-02-01") )
64 expect_identical(integerIndexToDate(dateIndexToInteger("2007-02-01",data0),data0),
65 as.Date("2007-02-01") )
66 expect_identical(integerIndexToDate(dateIndexToInteger("2007-03-01",data0),data0),
67 as.Date("2007-03-01") )
68 expect_identical(integerIndexToDate(dateIndexToInteger("2007-03-01",data0),data0),
69 as.Date("2007-03-01") )
70 expect_identical(integerIndexToDate(dateIndexToInteger("2007-05-31",data0),data0),
71 as.Date("2007-05-31") )
72 expect_identical(integerIndexToDate(dateIndexToInteger("2007-05-31",data0),data0),
73 as.Date("2007-05-31") )
44a9990b 74})