1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15extern "C"
16{
17  #include "ecma-builtin-helpers.h"
18}
19
20#include "ecma-globals.h"
21#include "ecma-helpers.h"
22#include "test-common.h"
23#include <gtest/gtest.h>
24
25#define MS_PER_DAY ((ecma_number_t) 86400000)
26#define MS_PER_YEAR ((ecma_number_t) 365 * MS_PER_DAY)
27#define START_OF_GREGORIAN_CALENDAR ((ecma_number_t) (-1970 * MS_PER_YEAR \
28  - (1970 / 4) * MS_PER_DAY \
29  + (1970 / 100) * MS_PER_DAY \
30  - (1970 / 400) * MS_PER_DAY \
31  - MS_PER_DAY))
32
33/**
34 * Unit test's main function.
35 */
36class DateHelpersTest : public testing::Test{
37public:
38    static void SetUpTestCase()
39    {
40        GTEST_LOG_(INFO) << "DateHelpersTest SetUpTestCase";
41    }
42
43    static void TearDownTestCase()
44    {
45        GTEST_LOG_(INFO) << "DateHelpersTest TearDownTestCase";
46    }
47
48    void SetUp() override {}
49    void TearDown() override {}
50
51};
52
53HWTEST_F(DateHelpersTest, Test001, testing::ext::TestSize.Level1)
54{
55  /* int ecma_date_day (time)*/
56
57  TEST_ASSERT (ecma_date_day (0) == 0);
58  TEST_ASSERT (ecma_date_day (MS_PER_DAY) == 1);
59
60  /* ecma_number_t ecma_date_time_within_day (time) */
61
62  TEST_ASSERT (ecma_date_time_within_day (0) == 0);
63  TEST_ASSERT (ecma_date_time_within_day (42) == 42);
64  TEST_ASSERT (ecma_date_time_within_day (42.51) == 42.51);
65  TEST_ASSERT (ecma_date_time_within_day (MS_PER_DAY + 42) == 42);
66
67  /* int ecma_date_year_from_time (time) */
68
69  TEST_ASSERT (ecma_date_year_from_time (0) == 1970);
70  TEST_ASSERT (ecma_date_year_from_time (0) == 1970);
71  TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY) == 1970);
72  TEST_ASSERT (ecma_date_year_from_time ((MS_PER_DAY) * (ecma_number_t) 365 - 1) == 1970);
73  TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) 365) == 1971);
74  TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) (365 * (2015 - 1970)))
75                == 2014);
76  TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) (365.25 * (2015 - 1970)))
77                == 2015);
78  TEST_ASSERT (ecma_date_year_from_time (-MS_PER_YEAR) == 1969);
79  TEST_ASSERT (ecma_date_year_from_time (-1970 * MS_PER_YEAR) == 1);
80  TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
81  TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR - 1) == -1);
82  TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR - 3 * MS_PER_YEAR) == -3);
83
84  /* int ecma_date_month_from_time  (time) */
85
86  TEST_ASSERT (ecma_date_month_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
87  TEST_ASSERT (ecma_date_month_from_time (0) == 0);
88  TEST_ASSERT (ecma_date_month_from_time (-MS_PER_DAY) == 11);
89  TEST_ASSERT (ecma_date_month_from_time (31 * MS_PER_DAY) == 1);
90
91  /* int ecma_date_date_from_time  (time) */
92
93  TEST_ASSERT (ecma_date_date_from_time (START_OF_GREGORIAN_CALENDAR) == 1);
94  TEST_ASSERT (ecma_date_date_from_time (0) == 1);
95  TEST_ASSERT (ecma_date_date_from_time (-MS_PER_DAY) == 31);
96  TEST_ASSERT (ecma_date_date_from_time (31 * MS_PER_DAY) == 1);
97
98  /* int ecma_date_week_day (ecma_number_t time) */
99
100  /* FIXME: Implement */
101
102  /* ecma_number_t ecma_date_utc (time) */
103
104  /* FIXME: Implement */
105
106  /* ecma_number_t ecma_date_hour_from_time (time) */
107
108  TEST_ASSERT (ecma_date_hour_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
109  TEST_ASSERT (ecma_date_hour_from_time (0) == 0);
110  TEST_ASSERT (ecma_date_hour_from_time (-MS_PER_DAY) == 0);
111  TEST_ASSERT (ecma_date_hour_from_time (-1) == 23);
112
113  /* ecma_number_t ecma_date_min_from_time (time) */
114
115  TEST_ASSERT (ecma_date_min_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
116  TEST_ASSERT (ecma_date_min_from_time (0) == 0);
117  TEST_ASSERT (ecma_date_min_from_time (-MS_PER_DAY) == 0);
118  TEST_ASSERT (ecma_date_min_from_time (-1) == 59);
119
120  /* ecma_number_t ecma_date_sec_from_time (time) */
121
122  TEST_ASSERT (ecma_date_sec_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
123  TEST_ASSERT (ecma_date_sec_from_time (0) == 0);
124  TEST_ASSERT (ecma_date_sec_from_time (-MS_PER_DAY) == 0);
125  TEST_ASSERT (ecma_date_sec_from_time (-1) == 59);
126
127  /* ecma_number_t ecma_date_ms_from_time (time) */
128
129  TEST_ASSERT (ecma_date_ms_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
130  TEST_ASSERT (ecma_date_ms_from_time (0) == 0);
131  TEST_ASSERT (ecma_date_ms_from_time (-MS_PER_DAY) == 0);
132  TEST_ASSERT (ecma_date_ms_from_time (-1) == 999);
133
134  /* ecma_number_t ecma_date_make_time (hour, min, sec, ms) */
135
136  /* FIXME: Implement */
137
138  /* ecma_number_t ecma_date_make_day (year, month, date) */
139
140  TEST_ASSERT (ecma_date_make_day (1970, 0, 1) == 0);
141  TEST_ASSERT (ecma_date_make_day (1970, -1, 1) == -31);
142  TEST_ASSERT (ecma_date_make_day (1970, 0, 2.5) == 1);
143  TEST_ASSERT (ecma_date_make_day (1970, 1, 35) == 65);
144  TEST_ASSERT (ecma_date_make_day (1970, 13, 35) == 430);
145  TEST_ASSERT (ecma_date_make_day (2016, 2, 1) == 16861);
146  TEST_ASSERT (ecma_date_make_day (2016, 8, 31) == 17075);
147  TEST_ASSERT (ecma_date_make_day (2016, 9, 1) == 17075);
148
149  /* ecma_number_t ecma_date_make_date (day, time) */
150
151  /* FIXME: Implement */
152
153  /* ecma_number_t ecma_date_time_clip (year) */
154
155  /* FIXME: Implement */
156}
157