• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
15 
16 #include "ecma-builtin-helpers.h"
17 #include "ecma-globals.h"
18 #include "ecma-helpers.h"
19 
20 #include "test-common.h"
21 
22 #define MS_PER_DAY ((ecma_number_t) 86400000)
23 #define MS_PER_YEAR ((ecma_number_t) 365 * MS_PER_DAY)
24 #define START_OF_GREGORIAN_CALENDAR ((ecma_number_t) (-1970 * MS_PER_YEAR \
25   - (1970 / 4) * MS_PER_DAY \
26   + (1970 / 100) * MS_PER_DAY \
27   - (1970 / 400) * MS_PER_DAY \
28   - MS_PER_DAY))
29 
30 /**
31  * Unit test's main function.
32  */
33 int
main(void)34 main (void)
35 {
36   /* int ecma_date_day (time)*/
37 
38   TEST_ASSERT (ecma_date_day (0) == 0);
39   TEST_ASSERT (ecma_date_day (MS_PER_DAY) == 1);
40 
41   /* ecma_number_t ecma_date_time_within_day (time) */
42 
43   TEST_ASSERT (ecma_date_time_within_day (0) == 0);
44   TEST_ASSERT (ecma_date_time_within_day (42) == 42);
45   TEST_ASSERT (ecma_date_time_within_day (42.51) == 42.51);
46   TEST_ASSERT (ecma_date_time_within_day (MS_PER_DAY + 42) == 42);
47 
48   /* int ecma_date_year_from_time (time) */
49 
50   TEST_ASSERT (ecma_date_year_from_time (0) == 1970);
51   TEST_ASSERT (ecma_date_year_from_time (0) == 1970);
52   TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY) == 1970);
53   TEST_ASSERT (ecma_date_year_from_time ((MS_PER_DAY) * (ecma_number_t) 365 - 1) == 1970);
54   TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) 365) == 1971);
55   TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) (365 * (2015 - 1970)))
56                 == 2014);
57   TEST_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) (365.25 * (2015 - 1970)))
58                 == 2015);
59   TEST_ASSERT (ecma_date_year_from_time (-MS_PER_YEAR) == 1969);
60   TEST_ASSERT (ecma_date_year_from_time (-1970 * MS_PER_YEAR) == 1);
61   TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
62   TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR - 1) == -1);
63   TEST_ASSERT (ecma_date_year_from_time (START_OF_GREGORIAN_CALENDAR - 3 * MS_PER_YEAR) == -3);
64 
65   /* int ecma_date_month_from_time  (time) */
66 
67   TEST_ASSERT (ecma_date_month_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
68   TEST_ASSERT (ecma_date_month_from_time (0) == 0);
69   TEST_ASSERT (ecma_date_month_from_time (-MS_PER_DAY) == 11);
70   TEST_ASSERT (ecma_date_month_from_time (31 * MS_PER_DAY) == 1);
71 
72   /* int ecma_date_date_from_time  (time) */
73 
74   TEST_ASSERT (ecma_date_date_from_time (START_OF_GREGORIAN_CALENDAR) == 1);
75   TEST_ASSERT (ecma_date_date_from_time (0) == 1);
76   TEST_ASSERT (ecma_date_date_from_time (-MS_PER_DAY) == 31);
77   TEST_ASSERT (ecma_date_date_from_time (31 * MS_PER_DAY) == 1);
78 
79   /* int ecma_date_week_day (ecma_number_t time) */
80 
81   /* FIXME: Implement */
82 
83   /* ecma_number_t ecma_date_utc (time) */
84 
85   /* FIXME: Implement */
86 
87   /* ecma_number_t ecma_date_hour_from_time (time) */
88 
89   TEST_ASSERT (ecma_date_hour_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
90   TEST_ASSERT (ecma_date_hour_from_time (0) == 0);
91   TEST_ASSERT (ecma_date_hour_from_time (-MS_PER_DAY) == 0);
92   TEST_ASSERT (ecma_date_hour_from_time (-1) == 23);
93 
94   /* ecma_number_t ecma_date_min_from_time (time) */
95 
96   TEST_ASSERT (ecma_date_min_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
97   TEST_ASSERT (ecma_date_min_from_time (0) == 0);
98   TEST_ASSERT (ecma_date_min_from_time (-MS_PER_DAY) == 0);
99   TEST_ASSERT (ecma_date_min_from_time (-1) == 59);
100 
101   /* ecma_number_t ecma_date_sec_from_time (time) */
102 
103   TEST_ASSERT (ecma_date_sec_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
104   TEST_ASSERT (ecma_date_sec_from_time (0) == 0);
105   TEST_ASSERT (ecma_date_sec_from_time (-MS_PER_DAY) == 0);
106   TEST_ASSERT (ecma_date_sec_from_time (-1) == 59);
107 
108   /* ecma_number_t ecma_date_ms_from_time (time) */
109 
110   TEST_ASSERT (ecma_date_ms_from_time (START_OF_GREGORIAN_CALENDAR) == 0);
111   TEST_ASSERT (ecma_date_ms_from_time (0) == 0);
112   TEST_ASSERT (ecma_date_ms_from_time (-MS_PER_DAY) == 0);
113   TEST_ASSERT (ecma_date_ms_from_time (-1) == 999);
114 
115   /* ecma_number_t ecma_date_make_time (hour, min, sec, ms) */
116 
117   /* FIXME: Implement */
118 
119   /* ecma_number_t ecma_date_make_day (year, month, date) */
120 
121   TEST_ASSERT (ecma_date_make_day (1970, 0, 1) == 0);
122   TEST_ASSERT (ecma_date_make_day (1970, -1, 1) == -31);
123   TEST_ASSERT (ecma_date_make_day (1970, 0, 2.5) == 1);
124   TEST_ASSERT (ecma_date_make_day (1970, 1, 35) == 65);
125   TEST_ASSERT (ecma_date_make_day (1970, 13, 35) == 430);
126   TEST_ASSERT (ecma_date_make_day (2016, 2, 1) == 16861);
127   TEST_ASSERT (ecma_date_make_day (2016, 8, 31) == 17075);
128   TEST_ASSERT (ecma_date_make_day (2016, 9, 1) == 17075);
129 
130   /* ecma_number_t ecma_date_make_date (day, time) */
131 
132   /* FIXME: Implement */
133 
134   /* ecma_number_t ecma_date_time_clip (year) */
135 
136   /* FIXME: Implement */
137 
138   return 0;
139 } /* main */
140