-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdate_time_tests.cpp
More file actions
246 lines (203 loc) · 7.95 KB
/
date_time_tests.cpp
File metadata and controls
246 lines (203 loc) · 7.95 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdexcept>
#include "catch.hpp"
#include "date_time.h"
using namespace geheb;
using namespace std;
using namespace std::chrono;
TEST_CASE("date_time now has time since epoch", "[date_time]") {
const auto now = date_time::now();
const auto tp = (system_clock::time_point)now;
REQUIRE(tp.time_since_epoch().count() > 0);
}
TEST_CASE("date_time with 1970-1-1 has 0 seconds since epoch", "[date_time]") {
const date_time other(1970, 1, 1);
const auto tp = (system_clock::time_point)other;
REQUIRE(tp.time_since_epoch().count() == 0);
}
TEST_CASE("date_time ctor eqals with properties", "[date_time]") {
const date_time other(1970, 1, 2, 3, 4, 5);
REQUIRE(other.year() == 1970);
REQUIRE(other.month() == 1);
REQUIRE(other.day() == 2);
REQUIRE(other.day_of_week() == 5);
REQUIRE(other.hour() == 3);
REQUIRE(other.minute() == 4);
REQUIRE(other.second() == 5);
}
TEST_CASE("date_time with 1970-1-1 at 1 o'clock has 3600 seconds since epoch", "[date_time]") {
const date_time other(1970, 1, 1, 1);
auto secondsSinceEpoch = duration_cast<seconds>(((system_clock::time_point)other).time_since_epoch()).count();
REQUIRE(secondsSinceEpoch == 3600);
}
TEST_CASE("date_time with 1970-1-3 is weekend", "[date_time]") {
const date_time other(1970, 1, 3);
REQUIRE(other.is_weekend());
}
TEST_CASE("date_time with 1970-1-4 is weekend", "[date_time]") {
const date_time other(1970, 1, 4);
REQUIRE(other.is_weekend());
}
TEST_CASE("date_time with 1970-1-5 is not weekend", "[date_time]") {
const date_time other(1970, 1, 5);
REQUIRE(!other.is_weekend());
}
TEST_CASE("date_time with 1970-1-5 is monday", "[date_time]") {
const date_time other(1970, 1, 5);
REQUIRE(other.day_of_week() == 1);
}
TEST_CASE("date_time with 1970-1-6 is tuesday", "[date_time]") {
const date_time other(1970, 1, 6);
REQUIRE(other.day_of_week() == 2);
}
TEST_CASE("date_time with 1970-1-7 is wednesday", "[date_time]") {
const date_time other(1970, 1, 7);
REQUIRE(other.day_of_week() == 3);
}
TEST_CASE("date_time with 1970-1-8 is thursday", "[date_time]") {
const date_time other(1970, 1, 8);
REQUIRE(other.day_of_week() == 4);
}
TEST_CASE("date_time with 1970-1-9 is friday", "[date_time]") {
const date_time other(1970, 1, 9);
REQUIRE(other.day_of_week() == 5);
}
TEST_CASE("date_time with 1970-1-10 is saturday", "[date_time]") {
const date_time other(1970, 1, 10);
REQUIRE(other.day_of_week() == 6);
}
TEST_CASE("date_time with 1970-1-11 is sunday", "[date_time]") {
const date_time other(1970, 1, 11);
REQUIRE(other.day_of_week() == 0);
}
TEST_CASE("date_time copy has same values", "[date_time]") {
const auto now = date_time::now();
const auto other = now;
REQUIRE(now == other);
}
TEST_CASE("date_time with now is greater then 2000-1-1", "[date_time]") {
const auto other = date_time(2000,1,1);
const auto now = date_time::now();
REQUIRE(now > other);
}
TEST_CASE("date_time with 2000-1-1 is less then now", "[date_time]") {
const auto other = date_time(2000, 1, 1);
const auto now = date_time::now();
REQUIRE(other < now);
}
TEST_CASE("date_time with now+1s is greater then or equal to now", "[date_time]") {
const auto other = date_time(date_time::now() - seconds(1));
const auto now = date_time::now();
REQUIRE(now >= other);
}
TEST_CASE("date_time with now-1s is less then or equal to now", "[date_time]") {
const auto other = date_time(date_time::now() - seconds(1));
const auto now = date_time::now();
REQUIRE(other <= other);
}
TEST_CASE("date_time with 2000-1-1 is leap year", "[date_time]") {
const auto other = date_time(2000, 1, 1);
REQUIRE(other.is_leap());
}
TEST_CASE("date_time with 2100-1-1 is not leap year", "[date_time]") {
const auto other = date_time(2100, 1, 1);
REQUIRE(!other.is_leap());
}
TEST_CASE("date_time with january has 31 days", "[date_time]") {
const auto other = date_time(2000, 1, 31);
REQUIRE(other.last_day_of_month() == 31);
REQUIRE(other.day() == 31);
}
TEST_CASE("date_time with february and leap year has 29 days", "[date_time]") {
const auto other = date_time(2000, 2, 29);
REQUIRE(other.last_day_of_month() == 29);
REQUIRE(other.day() == 29);
}
TEST_CASE("date_time with february and non leap year has 28 days", "[date_time]") {
const auto other = date_time(2001, 2, 28);
REQUIRE(other.last_day_of_month() == 28);
REQUIRE(other.day() == 28);
}
TEST_CASE("date_time with march has 31 days", "[date_time]") {
const auto other = date_time(2000, 3, 31);
REQUIRE(other.last_day_of_month() == 31);
REQUIRE(other.day() == 31);
}
TEST_CASE("date_time with april has 30 days", "[date_time]") {
const auto other = date_time(2000, 4, 30);
REQUIRE(other.last_day_of_month() == 30);
REQUIRE(other.day() == 30);
}
TEST_CASE("date_time with may has 31 days", "[date_time]") {
const auto other = date_time(2000, 5, 31);
REQUIRE(other.last_day_of_month() == 31);
REQUIRE(other.day() == 31);
}
TEST_CASE("date_time with june has 30 days", "[date_time]") {
const auto other = date_time(2000, 6, 30);
REQUIRE(other.last_day_of_month() == 30);
REQUIRE(other.day() == 30);
}
TEST_CASE("date_time with july has 31 days", "[date_time]") {
const auto other = date_time(2000, 7, 31);
REQUIRE(other.last_day_of_month() == 31);
REQUIRE(other.day() == 31);
}
TEST_CASE("date_time with august has 31 days", "[date_time]") {
const auto other = date_time(2000, 8, 31);
REQUIRE(other.last_day_of_month() == 31);
REQUIRE(other.day() == 31);
}
TEST_CASE("date_time with september has 30 days", "[date_time]") {
const auto other = date_time(2000, 9, 30);
REQUIRE(other.last_day_of_month() == 30);
REQUIRE(other.day() == 30);
}
TEST_CASE("date_time with october has 31 days", "[date_time]") {
const auto other = date_time(2000, 10, 31);
REQUIRE(other.last_day_of_month() == 31);
REQUIRE(other.day() == 31);
}
TEST_CASE("date_time with novmber has 30 days", "[date_time]") {
const auto other = date_time(2000, 11, 30);
REQUIRE(other.last_day_of_month() == 30);
REQUIRE(other.day() == 30);
}
TEST_CASE("date_time with december has 31 days", "[date_time]") {
const auto other = date_time(2000, 12, 31);
REQUIRE(other.last_day_of_month() == 31);
REQUIRE(other.day() == 31);
}
TEST_CASE("date_time with year 1969 or 5183 throws exception", "[date_time]") {
REQUIRE_THROWS_AS(date_time(1969, 1, 1), invalid_argument);
REQUIRE_THROWS_AS(date_time(5138, 1, 1), invalid_argument);
}
TEST_CASE("date_time with month 0 or 13 throws exception", "[date_time]") {
REQUIRE_THROWS_AS(date_time(2000, 0, 1), invalid_argument);
REQUIRE_THROWS_AS(date_time(2000, 13, 1), invalid_argument);
}
TEST_CASE("date_time with day 0 or 32 throws exception", "[date_time]") {
REQUIRE_THROWS_AS(date_time(2000, 1, 0), invalid_argument);
REQUIRE_THROWS_AS(date_time(2000, 1, 32), invalid_argument);
}
TEST_CASE("date_time with hour -1 or 24 throws exception", "[date_time]") {
REQUIRE_THROWS_AS(date_time(2000, 1, 1, -1), invalid_argument);
REQUIRE_THROWS_AS(date_time(2000, 1, 1, 24), invalid_argument);
}
TEST_CASE("date_time with minute -1 or 60 throws exception", "[date_time]") {
REQUIRE_THROWS_AS(date_time(2000, 1, 1, 0, -1), invalid_argument);
REQUIRE_THROWS_AS(date_time(2000, 1, 1, 0, 60), invalid_argument);
}
TEST_CASE("date_time with second -1 or 60 throws exception", "[date_time]") {
REQUIRE_THROWS_AS(date_time(2000, 1, 1, 0, 0, -1), invalid_argument);
REQUIRE_THROWS_AS(date_time(2000, 1, 1, 0, 0, 60), invalid_argument);
}
TEST_CASE("date_time with 31 february is 2 march", "[date_time]") {
const auto other = date_time(2000, 2, 31);
REQUIRE(other.month() == 3);
REQUIRE(other.day() == 2);
}
TEST_CASE("date_time with adding 50 miliseconds is ignored", "[date_time]") {
const auto now = date_time::now();
const auto other = now + milliseconds(50);
REQUIRE(other == now);
}